Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / wm / ash_native_cursor_manager_unittest.cc
blobabb22c96dcbcd661fafa8d653c99b7e07ff958cc
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"
9 #include "ash/display/display_util.h"
10 #include "ash/shell.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/cursor_manager_test_api.h"
13 #include "ui/aura/test/aura_test_utils.h"
14 #include "ui/aura/test/test_window_delegate.h"
15 #include "ui/aura/test/test_windows.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/base/cursor/image_cursors.h"
19 #include "ui/gfx/screen.h"
21 #if defined(OS_WIN)
22 #include "base/win/windows_version.h"
23 #include "ui/base/cursor/cursor_loader_win.h"
24 #endif
26 #if defined(USE_X11)
27 #include "ui/base/cursor/cursor_loader_x11.h"
28 #include "ui/resources/grit/ui_resources.h"
29 #endif
31 namespace ash {
32 namespace test {
34 namespace {
36 // A delegate for recording a mouse event location.
37 class MouseEventLocationDelegate : public aura::test::TestWindowDelegate {
38 public:
39 MouseEventLocationDelegate() {}
40 ~MouseEventLocationDelegate() override {}
42 gfx::Point GetMouseEventLocationAndReset() {
43 gfx::Point p = mouse_event_location_;
44 mouse_event_location_.SetPoint(-100, -100);
45 return p;
48 void OnMouseEvent(ui::MouseEvent* event) override {
49 mouse_event_location_ = event->location();
50 event->SetHandled();
53 private:
54 gfx::Point mouse_event_location_;
56 DISALLOW_COPY_AND_ASSIGN(MouseEventLocationDelegate);
59 } // namespace
61 typedef test::AshTestBase AshNativeCursorManagerTest;
63 TEST_F(AshNativeCursorManagerTest, LockCursor) {
64 ::wm::CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
65 CursorManagerTestApi test_api(cursor_manager);
67 #if defined(OS_WIN)
68 ui::CursorLoaderWin::SetCursorResourceModule(L"ash_unittests.exe");
69 #endif
70 cursor_manager->SetCursor(ui::kCursorCopy);
71 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());
72 UpdateDisplay("800x800*2/r");
73 EXPECT_EQ(2.0f, test_api.GetCurrentCursor().device_scale_factor());
74 EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
75 EXPECT_EQ(gfx::Display::ROTATE_90, test_api.GetCurrentCursorRotation());
76 EXPECT_TRUE(test_api.GetCurrentCursor().platform());
78 cursor_manager->LockCursor();
79 EXPECT_TRUE(cursor_manager->IsCursorLocked());
81 // Cursor type does not change while cursor is locked.
82 EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
83 cursor_manager->SetCursorSet(ui::CURSOR_SET_NORMAL);
84 EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
85 cursor_manager->SetCursorSet(ui::CURSOR_SET_LARGE);
86 EXPECT_EQ(ui::CURSOR_SET_LARGE, test_api.GetCurrentCursorSet());
87 cursor_manager->SetCursorSet(ui::CURSOR_SET_NORMAL);
88 EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
90 // Cursor type does not change while cursor is locked.
91 cursor_manager->SetCursor(ui::kCursorPointer);
92 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());
94 // Device scale factor and rotation do change even while cursor is locked.
95 UpdateDisplay("800x800/u");
96 EXPECT_EQ(1.0f, test_api.GetCurrentCursor().device_scale_factor());
97 EXPECT_EQ(gfx::Display::ROTATE_180, test_api.GetCurrentCursorRotation());
99 cursor_manager->UnlockCursor();
100 EXPECT_FALSE(cursor_manager->IsCursorLocked());
102 // Cursor type changes to the one specified while cursor is locked.
103 EXPECT_EQ(ui::kCursorPointer, test_api.GetCurrentCursor().native_type());
104 EXPECT_EQ(1.0f, test_api.GetCurrentCursor().device_scale_factor());
105 EXPECT_TRUE(test_api.GetCurrentCursor().platform());
108 TEST_F(AshNativeCursorManagerTest, SetCursor) {
109 ::wm::CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
110 CursorManagerTestApi test_api(cursor_manager);
111 #if defined(OS_WIN)
112 ui::CursorLoaderWin::SetCursorResourceModule(L"ash_unittests.exe");
113 #endif
114 cursor_manager->SetCursor(ui::kCursorCopy);
115 EXPECT_EQ(ui::kCursorCopy, test_api.GetCurrentCursor().native_type());
116 EXPECT_TRUE(test_api.GetCurrentCursor().platform());
117 cursor_manager->SetCursor(ui::kCursorPointer);
118 EXPECT_EQ(ui::kCursorPointer, test_api.GetCurrentCursor().native_type());
119 EXPECT_TRUE(test_api.GetCurrentCursor().platform());
122 TEST_F(AshNativeCursorManagerTest, SetCursorSet) {
123 ::wm::CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
124 CursorManagerTestApi test_api(cursor_manager);
126 EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
128 cursor_manager->SetCursorSet(ui::CURSOR_SET_NORMAL);
129 EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
131 cursor_manager->SetCursorSet(ui::CURSOR_SET_LARGE);
132 EXPECT_EQ(ui::CURSOR_SET_LARGE, test_api.GetCurrentCursorSet());
134 cursor_manager->SetCursorSet(ui::CURSOR_SET_NORMAL);
135 EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
138 TEST_F(AshNativeCursorManagerTest, SetDeviceScaleFactorAndRotation) {
139 ::wm::CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
140 CursorManagerTestApi test_api(cursor_manager);
141 UpdateDisplay("800x100*2");
142 EXPECT_EQ(2.0f, test_api.GetCurrentCursor().device_scale_factor());
143 EXPECT_EQ(gfx::Display::ROTATE_0, test_api.GetCurrentCursorRotation());
145 UpdateDisplay("800x100/l");
146 EXPECT_EQ(1.0f, test_api.GetCurrentCursor().device_scale_factor());
147 EXPECT_EQ(gfx::Display::ROTATE_270, test_api.GetCurrentCursorRotation());
150 #if defined(OS_CHROMEOS)
151 // TODO(oshima): crbug.com/143619
152 TEST_F(AshNativeCursorManagerTest, FractionalScale) {
153 ::wm::CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
154 CursorManagerTestApi test_api(cursor_manager);
155 // Cursor should use the resource scale factor.
156 UpdateDisplay("800x100*1.25");
157 EXPECT_EQ(1.0f, test_api.GetCurrentCursor().device_scale_factor());
159 #endif
161 TEST_F(AshNativeCursorManagerTest, UIScaleShouldNotChangeCursor) {
162 int64 display_id = Shell::GetScreen()->GetPrimaryDisplay().id();
163 gfx::Display::SetInternalDisplayId(display_id);
165 ::wm::CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
166 CursorManagerTestApi test_api(cursor_manager);
168 SetDisplayUIScale(display_id, 0.5f);
169 EXPECT_EQ(1.0f,
170 Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
171 EXPECT_EQ(1.0f, test_api.GetCurrentCursor().device_scale_factor());
173 SetDisplayUIScale(display_id, 1.0f);
175 // 2x display should keep using 2x cursor regardless of the UI scale.
176 UpdateDisplay("800x800*2");
177 EXPECT_EQ(2.0f,
178 Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
179 EXPECT_EQ(2.0f, test_api.GetCurrentCursor().device_scale_factor());
180 SetDisplayUIScale(display_id, 2.0f);
181 EXPECT_EQ(1.0f,
182 Shell::GetScreen()->GetPrimaryDisplay().device_scale_factor());
183 EXPECT_EQ(2.0f, test_api.GetCurrentCursor().device_scale_factor());
186 #if defined(USE_X11)
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;
198 loader.UnloadAll();
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));
207 #endif
209 } // namespace test
210 } // namespace ash