1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/utility/partial_screenshot_controller.h"
7 #include "ash/screenshot_delegate.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/test/test_screenshot_delegate.h"
11 #include "ui/aura/window_event_dispatcher.h"
12 #include "ui/events/test/event_generator.h"
16 class PartialScreenshotControllerTest
: public test::AshTestBase
{
18 PartialScreenshotControllerTest() {}
19 ~PartialScreenshotControllerTest() override
{}
21 void SetUp() override
{
22 test::AshTestBase::SetUp();
23 PartialScreenshotController::StartPartialScreenshotSession(
24 GetScreenshotDelegate());
28 static PartialScreenshotController
* GetInstance() {
29 return PartialScreenshotController::GetInstanceForTest();
32 const gfx::Point
& GetStartPosition() const {
33 return GetInstance()->start_position_
;
37 DISALLOW_COPY_AND_ASSIGN(PartialScreenshotControllerTest
);
40 TEST_F(PartialScreenshotControllerTest
, BasicMouse
) {
41 test::TestScreenshotDelegate
* test_delegate
= GetScreenshotDelegate();
42 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
44 generator
.MoveMouseTo(100, 100);
45 generator
.PressLeftButton();
46 EXPECT_EQ("100,100", GetStartPosition().ToString());
47 EXPECT_EQ(0, test_delegate
->handle_take_partial_screenshot_count());
49 generator
.MoveMouseTo(200, 200);
50 EXPECT_EQ(0, test_delegate
->handle_take_partial_screenshot_count());
52 generator
.ReleaseLeftButton();
53 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString());
54 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
56 RunAllPendingInMessageLoop();
57 EXPECT_EQ(nullptr, GetInstance());
60 TEST_F(PartialScreenshotControllerTest
, JustClick
) {
61 test::TestScreenshotDelegate
* test_delegate
= GetScreenshotDelegate();
62 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
64 generator
.MoveMouseTo(100, 100);
66 // No moves, just clicking at the same position.
67 generator
.ClickLeftButton();
68 EXPECT_EQ(0, test_delegate
->handle_take_partial_screenshot_count());
70 RunAllPendingInMessageLoop();
71 EXPECT_EQ(nullptr, GetInstance());
74 TEST_F(PartialScreenshotControllerTest
, BasicTouch
) {
75 test::TestScreenshotDelegate
* test_delegate
= GetScreenshotDelegate();
76 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
78 generator
.set_current_location(gfx::Point(100, 100));
79 generator
.PressTouch();
80 EXPECT_EQ(0, test_delegate
->handle_take_partial_screenshot_count());
81 EXPECT_EQ("100,100", GetStartPosition().ToString());
83 generator
.MoveTouch(gfx::Point(200, 200));
84 EXPECT_EQ(0, test_delegate
->handle_take_partial_screenshot_count());
86 generator
.ReleaseTouch();
87 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString());
88 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
90 RunAllPendingInMessageLoop();
91 EXPECT_EQ(nullptr, GetInstance());
94 TEST_F(PartialScreenshotControllerTest
, TwoFingerTouch
) {
95 test::TestScreenshotDelegate
* test_delegate
= GetScreenshotDelegate();
96 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
98 generator
.set_current_location(gfx::Point(100, 100));
99 generator
.PressTouch();
100 EXPECT_EQ(0, test_delegate
->handle_take_partial_screenshot_count());
101 EXPECT_EQ("100,100", GetStartPosition().ToString());
103 generator
.set_current_location(gfx::Point(200, 200));
104 generator
.PressTouchId(1);
105 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString());
106 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
108 RunAllPendingInMessageLoop();
109 EXPECT_EQ(nullptr, GetInstance());
112 TEST_F(PartialScreenshotControllerTest
, DontStartTwice
) {
113 PartialScreenshotController
* controller
= GetInstance();
115 PartialScreenshotController::StartPartialScreenshotSession(
116 GetScreenshotDelegate());
118 // The same instance.
119 EXPECT_EQ(controller
, GetInstance());
122 TEST_F(PartialScreenshotControllerTest
, MultipleDisplays
) {
123 if (!SupportsMultipleDisplays())
126 EXPECT_NE(nullptr, GetInstance());
127 UpdateDisplay("400x400,500x500");
128 RunAllPendingInMessageLoop();
129 EXPECT_EQ(nullptr, GetInstance());
131 PartialScreenshotController::StartPartialScreenshotSession(
132 GetScreenshotDelegate());
133 EXPECT_NE(nullptr, GetInstance());
134 UpdateDisplay("400x400");
135 RunAllPendingInMessageLoop();
136 EXPECT_EQ(nullptr, GetInstance());