Commit a944703e authored by Sammie Quon's avatar Sammie Quon Committed by Commit Bot

capture_mode: Add tests for region capture mode.

Adds tests for crrev.com/c/2404945.

Test: added
Bug: 1120132
Change-Id: Ifb42fa1f8979ef7762614d6edd90dbfefa4d2f1f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2422459
Commit-Queue: Sammie Quon <sammiequon@chromium.org>
Reviewed-by: default avatarAhmed Fakhry <afakhry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810705}
parent d14233fb
...@@ -20,6 +20,9 @@ ...@@ -20,6 +20,9 @@
#include "ash/test/ash_test_base.h" #include "ash/test/ash_test_base.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "ui/events/keycodes/keyboard_codes_posix.h" #include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace ash { namespace ash {
...@@ -118,6 +121,27 @@ class CaptureModeTest : public AshTestBase { ...@@ -118,6 +121,27 @@ class CaptureModeTest : public AshTestBase {
->close_button(); ->close_button();
} }
// Start Capture Mode with source region and type image.
CaptureModeController* StartImageRegionCapture() {
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kRegion);
controller->SetType(CaptureModeType::kImage);
controller->Start();
DCHECK(controller->IsActive());
return controller;
}
// Select a region by pressing and dragging the mouse.
void SelectRegion(const gfx::Rect& region) {
auto* controller = CaptureModeController::Get();
ASSERT_TRUE(controller->IsActive());
ASSERT_EQ(CaptureModeSource::kRegion, controller->source());
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(region.origin());
event_generator->DragMouseTo(region.bottom_right());
EXPECT_EQ(region, controller->user_capture_region());
}
private: private:
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
}; };
...@@ -208,4 +232,219 @@ TEST_F(CaptureModeTest, VideoRecordingUiBehavior) { ...@@ -208,4 +232,219 @@ TEST_F(CaptureModeTest, VideoRecordingUiBehavior) {
EXPECT_FALSE(IsCursorCompositingEnabled()); EXPECT_FALSE(IsCursorCompositingEnabled());
} }
// Tests the behavior of repositioning a region with capture mode.
TEST_F(CaptureModeTest, CaptureRegionRepositionBehavior) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x800");
auto* controller = StartImageRegionCapture();
// The first time selecting a region, the region is a default rect.
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
// Press down and drag to select a region.
SelectRegion(gfx::Rect(100, 100, 600, 600));
// Click somewhere in the center on the region and drag. The whole region
// should move. Note that the point cannot be in the capture button bounds,
// which is located in the center of the region.
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(gfx::Point(200, 200));
event_generator->DragMouseBy(-50, -50);
EXPECT_EQ(gfx::Rect(50, 50, 600, 600), controller->user_capture_region());
// Try to drag the region offscreen. The region should be bound by the display
// size.
event_generator->set_current_screen_location(gfx::Point(100, 100));
event_generator->DragMouseBy(-150, -150);
EXPECT_EQ(gfx::Rect(600, 600), controller->user_capture_region());
}
// Tests the behavior of resizing a region with capture mode using the corner
// drag affordances.
TEST_F(CaptureModeTest, CaptureRegionCornerResizeBehavior) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x800");
auto* controller = StartImageRegionCapture();
// Create the initial region.
const gfx::Rect target_region(gfx::Rect(200, 200, 400, 400));
SelectRegion(target_region);
// For each corner point try dragging to several points and verify that the
// capture region is as expected.
struct {
std::string trace;
gfx::Point drag_point;
// The point that stays the same while dragging. It is the opposite vertex
// to |drag_point| on |target_region|.
gfx::Point anchor_point;
} kDragCornerCases[] = {
{"origin", target_region.origin(), target_region.bottom_right()},
{"top_right", target_region.top_right(), target_region.bottom_left()},
{"bottom_right", target_region.bottom_right(), target_region.origin()},
{"bottom_left", target_region.bottom_left(), target_region.top_right()},
};
// The test corner points are one in each corner outside |target_region| and
// one point inside |target_region|.
auto drag_test_points = {gfx::Point(100, 100), gfx::Point(700, 100),
gfx::Point(700, 700), gfx::Point(100, 700),
gfx::Point(400, 400)};
auto* event_generator = GetEventGenerator();
for (auto test_case : kDragCornerCases) {
SCOPED_TRACE(test_case.trace);
event_generator->set_current_screen_location(test_case.drag_point);
event_generator->PressLeftButton();
// At each drag test point, the region rect should be the rect created by
// the given |corner_point| and the drag test point. That is, the width
// should match the x distance between the two points, the height should
// match the y distance between the two points and that both points are
// contained in the region.
for (auto drag_test_point : drag_test_points) {
event_generator->MoveMouseTo(drag_test_point);
gfx::Rect region = controller->user_capture_region();
const gfx::Vector2d distance = test_case.anchor_point - drag_test_point;
EXPECT_EQ(std::abs(distance.x()), region.width());
EXPECT_EQ(std::abs(distance.y()), region.height());
// gfx::Rect::Contains returns the point (x+width, y+height) as false, so
// make the region one unit bigger to account for this.
region.Inset(gfx::Insets(-1));
EXPECT_TRUE(region.Contains(drag_test_point));
EXPECT_TRUE(region.Contains(test_case.anchor_point));
}
// Make sure the region is reset for the next iteration.
event_generator->MoveMouseTo(test_case.drag_point);
event_generator->ReleaseLeftButton();
ASSERT_EQ(target_region, controller->user_capture_region());
}
}
// Tests the behavior of resizing a region with capture mode using the edge drag
// affordances.
TEST_F(CaptureModeTest, CaptureRegionEdgeResizeBehavior) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x800");
auto* controller = StartImageRegionCapture();
// Create the initial region.
const gfx::Rect target_region(gfx::Rect(200, 200, 400, 400));
SelectRegion(target_region);
// For each edge point try dragging to several points and verify that the
// capture region is as expected.
struct {
std::string trace;
gfx::Point drag_point;
// True if horizontal direction (left, right). Height stays the same while
// dragging if true, width stays the same while dragging if false.
bool horizontal;
// The edge that stays the same while dragging. It is the opposite edge to
// |drag_point|. For example, if |drag_point| is the left center of
// |target_region|, then |anchor_edge| is the right edge.
int anchor_edge;
} kDragEdgeCases[] = {
{"left", target_region.left_center(), true, target_region.right()},
{"top", target_region.top_center(), false, target_region.bottom()},
{"right", target_region.right_center(), true, target_region.x()},
{"bottom", target_region.bottom_center(), false, target_region.y()},
};
// Drag to a couple of points that change both x and y. In all these cases,
// only the width or height should change.
auto drag_test_points = {gfx::Point(350, 350), gfx::Point(450, 450)};
auto* event_generator = GetEventGenerator();
for (auto test_case : kDragEdgeCases) {
SCOPED_TRACE(test_case.trace);
event_generator->set_current_screen_location(test_case.drag_point);
event_generator->PressLeftButton();
for (auto drag_test_point : drag_test_points) {
event_generator->MoveMouseTo(drag_test_point);
const gfx::Rect region = controller->user_capture_region();
// One of width/height will always be the same as |target_region|'s
// initial width/height, depending on the edge affordance. The other
// dimension will be the distance from |drag_test_point| to the anchor
// edge.
const int variable_length = std::abs(
(test_case.horizontal ? drag_test_point.x() : drag_test_point.y()) -
test_case.anchor_edge);
const int expected_width =
test_case.horizontal ? variable_length : target_region.width();
const int expected_height =
test_case.horizontal ? target_region.height() : variable_length;
EXPECT_EQ(expected_width, region.width());
EXPECT_EQ(expected_height, region.height());
}
// Make sure the region is reset for the next iteration.
event_generator->MoveMouseTo(test_case.drag_point);
event_generator->ReleaseLeftButton();
ASSERT_EQ(target_region, controller->user_capture_region());
}
}
// Tests that the capture region persists after exiting and reentering capture
// mode.
TEST_F(CaptureModeTest, CaptureRegionPersistsAfterExit) {
auto* controller = StartImageRegionCapture();
const gfx::Rect region(100, 100, 200, 200);
SelectRegion(region);
controller->Stop();
controller->Start();
EXPECT_EQ(region, controller->user_capture_region());
}
// Tests that the capture region resets when clicking outside the current
// capture regions bounds.
TEST_F(CaptureModeTest, CaptureRegionResetsOnClickOutside) {
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(100, 100, 200, 200));
// Click on an area outside of the current capture region. The capture region
// should reset to default rect.
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(gfx::Point(400, 400));
event_generator->ClickLeftButton();
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
}
// Tests that buttons on the capture mode bar still work when a region is
// "covering" them.
TEST_F(CaptureModeTest, CaptureRegionCoversCaptureModeBar) {
UpdateDisplay("800x800");
auto* controller = StartImageRegionCapture();
// Select a region such that the capture mode bar is covered.
SelectRegion(gfx::Rect(5, 5, 795, 795));
EXPECT_TRUE(controller->user_capture_region().Contains(
controller->capture_mode_session()
->capture_mode_bar_view()
->GetBoundsInScreen()));
// Click on the fullscreen toggle button to verify that we enter fullscreen
// capture mode. Then click on the region toggle button to verify that we
// reenter region capture mode and that the region is still covering the
// capture mode bar.
auto* event_generator = GetEventGenerator();
ClickOnView(GetFullscreenToggleButton(), event_generator);
EXPECT_EQ(CaptureModeSource::kFullscreen, controller->source());
ClickOnView(GetRegionToggleButton(), GetEventGenerator());
ASSERT_EQ(CaptureModeSource::kRegion, controller->source());
ASSERT_TRUE(controller->user_capture_region().Contains(
controller->capture_mode_session()
->capture_mode_bar_view()
->GetBoundsInScreen()));
ClickOnView(GetCloseButton(), event_generator);
EXPECT_FALSE(controller->IsActive());
}
} // namespace ash } // namespace ash
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment