1 // Copyright (c) 2012 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/wm/ash_native_cursor_manager.h"
8 #include "ash/test/ash_test_base.h"
9 #include "ash/test/cursor_manager_test_api.h"
10 #include "ash/wm/image_cursors.h"
11 #include "ui/aura/root_window.h"
12 #include "ui/aura/test/test_window_delegate.h"
13 #include "ui/aura/test/test_windows.h"
14 #include "ui/aura/window.h"
15 #include "ui/gfx/screen.h"
18 #include "base/win/windows_version.h"
19 #include "ui/base/cursor/cursor_loader_win.h"
22 using views::corewm::CursorManager
;
29 // A delegate for recording a mouse event location.
30 class MouseEventLocationDelegate
: public aura::test::TestWindowDelegate
{
32 MouseEventLocationDelegate() {}
33 virtual ~MouseEventLocationDelegate() {}
35 gfx::Point
GetMouseEventLocationAndReset() {
36 gfx::Point p
= mouse_event_location_
;
37 mouse_event_location_
.SetPoint(-100, -100);
41 virtual void OnMouseEvent(ui::MouseEvent
* event
) OVERRIDE
{
42 mouse_event_location_
= event
->location();
47 gfx::Point mouse_event_location_
;
49 DISALLOW_COPY_AND_ASSIGN(MouseEventLocationDelegate
);
54 typedef test::AshTestBase AshNativeCursorManagerTest
;
56 TEST_F(AshNativeCursorManagerTest
, LockCursor
) {
57 CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
58 CursorManagerTestApi
test_api(cursor_manager
);
59 gfx::Display
display(0);
61 ui::CursorLoaderWin::SetCursorResourceModule(L
"ash_unittests.exe");
63 cursor_manager
->SetCursor(ui::kCursorCopy
);
64 EXPECT_EQ(ui::kCursorCopy
, test_api
.GetCurrentCursor().native_type());
65 display
.set_device_scale_factor(2.0f
);
66 display
.set_rotation(gfx::Display::ROTATE_90
);
67 cursor_manager
->SetScale(2.5f
);
68 cursor_manager
->SetDisplay(display
);
69 EXPECT_EQ(2.5f
, test_api
.GetCurrentScale());
70 EXPECT_EQ(2.0f
, test_api
.GetDisplay().device_scale_factor());
71 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
72 EXPECT_EQ(gfx::Display::ROTATE_90
, test_api
.GetDisplay().rotation());
73 EXPECT_TRUE(test_api
.GetCurrentCursor().platform());
75 cursor_manager
->LockCursor();
76 EXPECT_TRUE(cursor_manager
->IsCursorLocked());
78 // Cursor type does not change while cursor is locked.
79 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
80 cursor_manager
->SetCursorSet(ui::CURSOR_SET_NORMAL
);
81 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
82 cursor_manager
->SetCursorSet(ui::CURSOR_SET_LARGE
);
83 EXPECT_EQ(ui::CURSOR_SET_LARGE
, test_api
.GetCurrentCursorSet());
84 cursor_manager
->SetCursorSet(ui::CURSOR_SET_NORMAL
);
85 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
87 // Cusror scale does change even while cursor is locked.
88 EXPECT_EQ(2.5f
, test_api
.GetCurrentScale());
89 cursor_manager
->SetScale(1.f
);
90 EXPECT_EQ(1.f
, test_api
.GetCurrentScale());
91 cursor_manager
->SetScale(1.5f
);
92 EXPECT_EQ(1.5f
, test_api
.GetCurrentScale());
94 // Cursor type does not change while cursor is locked.
95 cursor_manager
->SetCursor(ui::kCursorPointer
);
96 EXPECT_EQ(ui::kCursorCopy
, test_api
.GetCurrentCursor().native_type());
98 // Device scale factor and rotation do change even while cursor is locked.
99 display
.set_device_scale_factor(1.0f
);
100 display
.set_rotation(gfx::Display::ROTATE_180
);
101 cursor_manager
->SetDisplay(display
);
102 EXPECT_EQ(1.0f
, test_api
.GetDisplay().device_scale_factor());
103 EXPECT_EQ(gfx::Display::ROTATE_180
, test_api
.GetDisplay().rotation());
105 cursor_manager
->UnlockCursor();
106 EXPECT_FALSE(cursor_manager
->IsCursorLocked());
108 // Cursor type changes to the one specified while cursor is locked.
109 EXPECT_EQ(1.5f
, test_api
.GetCurrentScale());
110 EXPECT_EQ(ui::kCursorPointer
, test_api
.GetCurrentCursor().native_type());
111 EXPECT_EQ(1.0f
, test_api
.GetDisplay().device_scale_factor());
112 EXPECT_TRUE(test_api
.GetCurrentCursor().platform());
115 TEST_F(AshNativeCursorManagerTest
, SetCursor
) {
116 CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
117 CursorManagerTestApi
test_api(cursor_manager
);
119 ui::CursorLoaderWin::SetCursorResourceModule(L
"ash_unittests.exe");
121 cursor_manager
->SetCursor(ui::kCursorCopy
);
122 EXPECT_EQ(ui::kCursorCopy
, test_api
.GetCurrentCursor().native_type());
123 EXPECT_TRUE(test_api
.GetCurrentCursor().platform());
124 cursor_manager
->SetCursor(ui::kCursorPointer
);
125 EXPECT_EQ(ui::kCursorPointer
, test_api
.GetCurrentCursor().native_type());
126 EXPECT_TRUE(test_api
.GetCurrentCursor().platform());
129 TEST_F(AshNativeCursorManagerTest
, SetCursorSet
) {
130 CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
131 CursorManagerTestApi
test_api(cursor_manager
);
133 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
135 cursor_manager
->SetCursorSet(ui::CURSOR_SET_NORMAL
);
136 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
138 cursor_manager
->SetCursorSet(ui::CURSOR_SET_LARGE
);
139 EXPECT_EQ(ui::CURSOR_SET_LARGE
, test_api
.GetCurrentCursorSet());
141 cursor_manager
->SetCursorSet(ui::CURSOR_SET_NORMAL
);
142 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
145 TEST_F(AshNativeCursorManagerTest
, SetScale
) {
146 CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
147 CursorManagerTestApi
test_api(cursor_manager
);
149 EXPECT_EQ(1.f
, test_api
.GetCurrentScale());
151 cursor_manager
->SetScale(2.5f
);
152 EXPECT_EQ(2.5f
, test_api
.GetCurrentScale());
154 cursor_manager
->SetScale(1.f
);
155 EXPECT_EQ(1.f
, test_api
.GetCurrentScale());
158 TEST_F(AshNativeCursorManagerTest
, SetDeviceScaleFactorAndRotation
) {
159 CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
160 CursorManagerTestApi
test_api(cursor_manager
);
162 gfx::Display
display(0);
163 display
.set_device_scale_factor(2.0f
);
164 cursor_manager
->SetDisplay(display
);
165 EXPECT_EQ(2.0f
, test_api
.GetDisplay().device_scale_factor());
166 EXPECT_EQ(gfx::Display::ROTATE_0
, test_api
.GetDisplay().rotation());
168 display
.set_device_scale_factor(1.0f
);
169 display
.set_rotation(gfx::Display::ROTATE_270
);
170 cursor_manager
->SetDisplay(display
);
171 EXPECT_EQ(1.0f
, test_api
.GetDisplay().device_scale_factor());
172 EXPECT_EQ(gfx::Display::ROTATE_270
, test_api
.GetDisplay().rotation());
175 TEST_F(AshNativeCursorManagerTest
, DisabledQueryMouseLocation
) {
176 aura::Window
* root_window
= Shell::GetInstance()->GetPrimaryRootWindow();
178 if (base::win::GetVersion() < base::win::VERSION_WIN8
)
180 // On Windows 8 the ASH environment has two processes, the viewer process
181 // which runs in Windows 8 mode and the browser process. The initialization
182 // happens when the viewer process connects to the browser channel and sends
183 // the initial IPC message.
184 RunAllPendingInMessageLoop();
186 root_window
->MoveCursorTo(gfx::Point(10, 10));
188 // The MoveCursor operation on Windows 8 is implemented in the viewer process
189 // which is notified by an IPC message to perform the MoveCursor operation.
190 // We need to ensure that the IPC is delivered to the viewer process and it
191 // the ACK is sent back from the viewer indicating that the operation
194 RunAllPendingInMessageLoop();
196 aura::WindowEventDispatcher
* dispatcher
= root_window
->GetDispatcher();
197 gfx::Point mouse_location
;
198 EXPECT_TRUE(dispatcher
->host()->QueryMouseLocation(&mouse_location
));
199 EXPECT_EQ("10,10", mouse_location
.ToString());
200 Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
201 EXPECT_FALSE(dispatcher
->host()->QueryMouseLocation(&mouse_location
));
202 EXPECT_EQ("0,0", mouse_location
.ToString());