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"
7 #include "ash/display/display_info.h"
8 #include "ash/display/display_manager.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ash/test/cursor_manager_test_api.h"
12 #include "ui/aura/test/aura_test_utils.h"
13 #include "ui/aura/test/test_window_delegate.h"
14 #include "ui/aura/test/test_windows.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/base/cursor/image_cursors.h"
18 #include "ui/gfx/screen.h"
21 #include "base/win/windows_version.h"
22 #include "ui/base/cursor/cursor_loader_win.h"
26 #include "ui/base/cursor/cursor_loader_x11.h"
27 #include "ui/resources/grit/ui_resources.h"
35 // A delegate for recording a mouse event location.
36 class MouseEventLocationDelegate
: public aura::test::TestWindowDelegate
{
38 MouseEventLocationDelegate() {}
39 ~MouseEventLocationDelegate() override
{}
41 gfx::Point
GetMouseEventLocationAndReset() {
42 gfx::Point p
= mouse_event_location_
;
43 mouse_event_location_
.SetPoint(-100, -100);
47 void OnMouseEvent(ui::MouseEvent
* event
) override
{
48 mouse_event_location_
= event
->location();
53 gfx::Point mouse_event_location_
;
55 DISALLOW_COPY_AND_ASSIGN(MouseEventLocationDelegate
);
60 typedef test::AshTestBase AshNativeCursorManagerTest
;
62 TEST_F(AshNativeCursorManagerTest
, LockCursor
) {
63 ::wm::CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
64 CursorManagerTestApi
test_api(cursor_manager
);
67 ui::CursorLoaderWin::SetCursorResourceModule(L
"ash_unittests.exe");
69 cursor_manager
->SetCursor(ui::kCursorCopy
);
70 EXPECT_EQ(ui::kCursorCopy
, test_api
.GetCurrentCursor().native_type());
71 UpdateDisplay("800x800*2/r");
72 EXPECT_EQ(2.0f
, test_api
.GetCurrentCursor().device_scale_factor());
73 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
74 EXPECT_EQ(gfx::Display::ROTATE_90
, test_api
.GetCurrentCursorRotation());
75 EXPECT_TRUE(test_api
.GetCurrentCursor().platform());
77 cursor_manager
->LockCursor();
78 EXPECT_TRUE(cursor_manager
->IsCursorLocked());
80 // Cursor type does not change while cursor is locked.
81 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
82 cursor_manager
->SetCursorSet(ui::CURSOR_SET_NORMAL
);
83 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
84 cursor_manager
->SetCursorSet(ui::CURSOR_SET_LARGE
);
85 EXPECT_EQ(ui::CURSOR_SET_LARGE
, test_api
.GetCurrentCursorSet());
86 cursor_manager
->SetCursorSet(ui::CURSOR_SET_NORMAL
);
87 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
89 // Cursor type does not change while cursor is locked.
90 cursor_manager
->SetCursor(ui::kCursorPointer
);
91 EXPECT_EQ(ui::kCursorCopy
, test_api
.GetCurrentCursor().native_type());
93 // Device scale factor and rotation do change even while cursor is locked.
94 UpdateDisplay("800x800/u");
95 EXPECT_EQ(1.0f
, test_api
.GetCurrentCursor().device_scale_factor());
96 EXPECT_EQ(gfx::Display::ROTATE_180
, test_api
.GetCurrentCursorRotation());
98 cursor_manager
->UnlockCursor();
99 EXPECT_FALSE(cursor_manager
->IsCursorLocked());
101 // Cursor type changes to the one specified while cursor is locked.
102 EXPECT_EQ(ui::kCursorPointer
, test_api
.GetCurrentCursor().native_type());
103 EXPECT_EQ(1.0f
, test_api
.GetCurrentCursor().device_scale_factor());
104 EXPECT_TRUE(test_api
.GetCurrentCursor().platform());
107 TEST_F(AshNativeCursorManagerTest
, SetCursor
) {
108 ::wm::CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
109 CursorManagerTestApi
test_api(cursor_manager
);
111 ui::CursorLoaderWin::SetCursorResourceModule(L
"ash_unittests.exe");
113 cursor_manager
->SetCursor(ui::kCursorCopy
);
114 EXPECT_EQ(ui::kCursorCopy
, test_api
.GetCurrentCursor().native_type());
115 EXPECT_TRUE(test_api
.GetCurrentCursor().platform());
116 cursor_manager
->SetCursor(ui::kCursorPointer
);
117 EXPECT_EQ(ui::kCursorPointer
, test_api
.GetCurrentCursor().native_type());
118 EXPECT_TRUE(test_api
.GetCurrentCursor().platform());
121 TEST_F(AshNativeCursorManagerTest
, SetCursorSet
) {
122 ::wm::CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
123 CursorManagerTestApi
test_api(cursor_manager
);
125 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
127 cursor_manager
->SetCursorSet(ui::CURSOR_SET_NORMAL
);
128 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
130 cursor_manager
->SetCursorSet(ui::CURSOR_SET_LARGE
);
131 EXPECT_EQ(ui::CURSOR_SET_LARGE
, test_api
.GetCurrentCursorSet());
133 cursor_manager
->SetCursorSet(ui::CURSOR_SET_NORMAL
);
134 EXPECT_EQ(ui::CURSOR_SET_NORMAL
, test_api
.GetCurrentCursorSet());
137 TEST_F(AshNativeCursorManagerTest
, SetDeviceScaleFactorAndRotation
) {
138 ::wm::CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
139 CursorManagerTestApi
test_api(cursor_manager
);
140 UpdateDisplay("800x100*2");
141 EXPECT_EQ(2.0f
, test_api
.GetCurrentCursor().device_scale_factor());
142 EXPECT_EQ(gfx::Display::ROTATE_0
, test_api
.GetCurrentCursorRotation());
144 UpdateDisplay("800x100/l");
145 EXPECT_EQ(1.0f
, test_api
.GetCurrentCursor().device_scale_factor());
146 EXPECT_EQ(gfx::Display::ROTATE_270
, test_api
.GetCurrentCursorRotation());
149 #if defined(OS_CHROMEOS)
150 // TODO(oshima): crbug.com/143619
151 TEST_F(AshNativeCursorManagerTest
, FractionalScale
) {
152 ::wm::CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
153 CursorManagerTestApi
test_api(cursor_manager
);
154 // Cursor should use the resource scale factor.
155 UpdateDisplay("800x100*1.25");
156 EXPECT_EQ(1.0f
, test_api
.GetCurrentCursor().device_scale_factor());
160 TEST_F(AshNativeCursorManagerTest
, UIScaleShouldNotChangeCursor
) {
161 int64 display_id
= Shell::GetScreen()->GetPrimaryDisplay().id();
162 gfx::Display::SetInternalDisplayId(display_id
);
164 ::wm::CursorManager
* cursor_manager
= Shell::GetInstance()->cursor_manager();
165 CursorManagerTestApi
test_api(cursor_manager
);
166 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
168 display_manager
->SetDisplayUIScale(display_id
, 0.5f
);
170 Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
171 EXPECT_EQ(1.0f
, test_api
.GetCurrentCursor().device_scale_factor());
173 display_manager
->SetDisplayUIScale(display_id
, 1.0f
);
175 // 2x display should keep using 2x cursor regardless of the UI scale.
176 UpdateDisplay("800x800*2");
178 Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
179 EXPECT_EQ(2.0f
, test_api
.GetCurrentCursor().device_scale_factor());
180 display_manager
->SetDisplayUIScale(display_id
, 2.0f
);
182 Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
183 EXPECT_EQ(2.0f
, test_api
.GetCurrentCursor().device_scale_factor());
187 // This test is in ash_unittests because ui_base_unittests does not include
188 // 2x assets. crbug.com/372541.
189 TEST_F(AshNativeCursorManagerTest
, CursorLoaderX11Test
) {
190 const int kCursorId
= 1;
191 ui::CursorLoaderX11 loader
;
192 loader
.set_scale(1.0f
);
194 loader
.LoadImageCursor(kCursorId
, IDR_AURA_CURSOR_MOVE
, gfx::Point());
195 const XcursorImage
* image
= loader
.GetXcursorImageForTest(kCursorId
);
196 int height
= image
->height
;
197 int width
= image
->width
;
200 // Load 2x cursor and make sure its size is 2x of the 1x cusor.
201 loader
.set_scale(2.0f
);
202 loader
.LoadImageCursor(kCursorId
, IDR_AURA_CURSOR_MOVE
, gfx::Point());
203 image
= loader
.GetXcursorImageForTest(kCursorId
);
204 EXPECT_EQ(height
* 2, static_cast<int>(image
->height
));
205 EXPECT_EQ(width
* 2, static_cast<int>(image
->width
));