Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / ash / utility / partial_screenshot_controller_unittest.cc
blob71d66197ee1f3f2d55f2d4e8744906a58bcdc149
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/display/cursor_window_controller.h"
8 #include "ash/display/mouse_cursor_event_filter.h"
9 #include "ash/display/window_tree_host_manager.h"
10 #include "ash/screenshot_delegate.h"
11 #include "ash/shell.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/test/display_manager_test_api.h"
14 #include "ash/test/mirror_window_test_api.h"
15 #include "ash/test/test_screenshot_delegate.h"
16 #include "ui/aura/env.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/base/cursor/cursor.h"
19 #include "ui/events/test/event_generator.h"
20 #include "ui/wm/core/cursor_manager.h"
22 namespace ash {
24 class PartialScreenshotControllerTest : public test::AshTestBase {
25 public:
26 PartialScreenshotControllerTest() {}
27 ~PartialScreenshotControllerTest() override {}
29 protected:
30 PartialScreenshotController* partial_screenshot_controller() {
31 return Shell::GetInstance()->partial_screenshot_controller();
34 bool TestIfMouseWarpsAt(const gfx::Point& point_in_screen) {
35 return test::DisplayManagerTestApi::TestIfMouseWarpsAt(GetEventGenerator(),
36 point_in_screen);
39 void StartPartialScreenshotSession() {
40 partial_screenshot_controller()->StartPartialScreenshotSession(
41 GetScreenshotDelegate());
44 void Cancel() { partial_screenshot_controller()->Cancel(); }
46 bool IsActive() {
47 return partial_screenshot_controller()->screenshot_delegate_ != nullptr;
50 const gfx::Point& GetStartPosition() const {
51 return Shell::GetInstance()
52 ->partial_screenshot_controller()
53 ->start_position_;
56 private:
57 DISALLOW_COPY_AND_ASSIGN(PartialScreenshotControllerTest);
60 TEST_F(PartialScreenshotControllerTest, BasicMouse) {
61 StartPartialScreenshotSession();
62 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate();
63 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
65 generator.MoveMouseTo(100, 100);
66 generator.PressLeftButton();
67 EXPECT_EQ("100,100", GetStartPosition().ToString());
68 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count());
70 generator.MoveMouseTo(200, 200);
71 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count());
73 generator.ReleaseLeftButton();
74 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString());
75 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
77 RunAllPendingInMessageLoop();
78 EXPECT_FALSE(IsActive());
81 TEST_F(PartialScreenshotControllerTest, JustClick) {
82 StartPartialScreenshotSession();
83 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate();
84 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
86 generator.MoveMouseTo(100, 100);
88 // No moves, just clicking at the same position.
89 generator.ClickLeftButton();
90 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count());
92 RunAllPendingInMessageLoop();
93 EXPECT_FALSE(IsActive());
96 TEST_F(PartialScreenshotControllerTest, BasicTouch) {
97 StartPartialScreenshotSession();
98 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate();
99 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
101 generator.set_current_location(gfx::Point(100, 100));
102 generator.PressTouch();
103 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count());
104 EXPECT_EQ("100,100", GetStartPosition().ToString());
106 generator.MoveTouch(gfx::Point(200, 200));
107 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count());
109 generator.ReleaseTouch();
110 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString());
111 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
113 RunAllPendingInMessageLoop();
114 EXPECT_FALSE(IsActive());
117 TEST_F(PartialScreenshotControllerTest, TwoFingerTouch) {
118 StartPartialScreenshotSession();
119 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate();
120 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
122 generator.set_current_location(gfx::Point(100, 100));
123 generator.PressTouch();
124 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count());
125 EXPECT_EQ("100,100", GetStartPosition().ToString());
127 generator.set_current_location(gfx::Point(200, 200));
128 generator.PressTouchId(1);
129 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString());
130 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
132 RunAllPendingInMessageLoop();
133 EXPECT_FALSE(IsActive());
136 TEST_F(PartialScreenshotControllerTest, MultipleDisplays) {
137 if (!SupportsMultipleDisplays())
138 return;
140 StartPartialScreenshotSession();
141 EXPECT_TRUE(IsActive());
142 UpdateDisplay("400x400,500x500");
143 RunAllPendingInMessageLoop();
144 EXPECT_FALSE(IsActive());
146 StartPartialScreenshotSession();
147 EXPECT_TRUE(IsActive());
148 UpdateDisplay("400x400");
149 RunAllPendingInMessageLoop();
150 EXPECT_FALSE(IsActive());
153 // Make sure PartialScreenshotController doesn't allow taking screenshot
154 // across multiple monitors
155 // cursor. See http://crbug.com/462229
156 #if defined(OS_CHROMEOS)
157 TEST_F(PartialScreenshotControllerTest, MouseWarpTest) {
158 if (!SupportsMultipleDisplays())
159 return;
161 // Create two displays.
162 Shell* shell = Shell::GetInstance();
163 UpdateDisplay("500x500,500x500");
164 EXPECT_EQ(2U, shell->display_manager()->GetNumDisplays());
166 StartPartialScreenshotSession();
167 EXPECT_FALSE(TestIfMouseWarpsAt(gfx::Point(499, 11)));
168 EXPECT_EQ("499,11",
169 aura::Env::GetInstance()->last_mouse_location().ToString());
171 Cancel();
172 EXPECT_TRUE(TestIfMouseWarpsAt(gfx::Point(499, 11)));
173 EXPECT_EQ("501,11",
174 aura::Env::GetInstance()->last_mouse_location().ToString());
177 TEST_F(PartialScreenshotControllerTest, VisibilityTest) {
178 aura::client::CursorClient* client = Shell::GetInstance()->cursor_manager();
180 GetEventGenerator().PressKey(ui::VKEY_A, 0);
181 GetEventGenerator().ReleaseKey(ui::VKEY_A, 0);
183 EXPECT_FALSE(client->IsCursorVisible());
185 StartPartialScreenshotSession();
186 EXPECT_TRUE(IsActive());
187 EXPECT_TRUE(client->IsCursorVisible());
189 Cancel();
190 EXPECT_TRUE(client->IsCursorVisible());
193 // Make sure PartialScreenshotController doesn't prevent handling of large
194 // cursor. See http://crbug.com/459214
195 TEST_F(PartialScreenshotControllerTest, LargeCursor) {
196 Shell::GetInstance()->cursor_manager()->SetCursorSet(ui::CURSOR_SET_LARGE);
197 Shell::GetInstance()
198 ->window_tree_host_manager()
199 ->cursor_window_controller()
200 ->SetCursorCompositingEnabled(true);
202 // Large cursor is represented as cursor window.
203 test::MirrorWindowTestApi test_api;
204 ASSERT_NE(nullptr, test_api.GetCursorWindow());
206 ui::test::EventGenerator event_generator(Shell::GetPrimaryRootWindow());
207 gfx::Point cursor_location;
208 event_generator.MoveMouseTo(cursor_location);
209 EXPECT_EQ(cursor_location.ToString(),
210 test_api.GetCursorLocation().ToString());
212 StartPartialScreenshotSession();
213 EXPECT_TRUE(IsActive());
215 cursor_location += gfx::Vector2d(1, 1);
216 event_generator.MoveMouseTo(cursor_location);
217 EXPECT_EQ(cursor_location.ToString(),
218 test_api.GetCursorLocation().ToString());
220 event_generator.PressLeftButton();
221 cursor_location += gfx::Vector2d(5, 5);
222 event_generator.MoveMouseTo(cursor_location);
223 EXPECT_EQ(cursor_location.ToString(),
224 test_api.GetCursorLocation().ToString());
226 event_generator.ReleaseLeftButton();
228 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
229 EXPECT_EQ("1,1 5x5", GetScreenshotDelegate()->last_rect().ToString());
230 RunAllPendingInMessageLoop();
231 EXPECT_FALSE(IsActive());
233 #endif
235 } // namespace ash