- add sources.
[platform/framework/web/crosswalk.git] / src / ash / wm / ash_native_cursor_manager_unittest.cc
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.
4
5 #include "ash/wm/ash_native_cursor_manager.h"
6
7 #include "ash/shell.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"
16
17 #if defined(OS_WIN)
18 #include "base/win/windows_version.h"
19 #include "ui/base/cursor/cursor_loader_win.h"
20 #endif
21
22 using views::corewm::CursorManager;
23
24 namespace ash {
25 namespace test {
26
27 namespace {
28
29 // A delegate for recording a mouse event location.
30 class MouseEventLocationDelegate : public aura::test::TestWindowDelegate {
31  public:
32   MouseEventLocationDelegate() {}
33   virtual ~MouseEventLocationDelegate() {}
34
35   gfx::Point GetMouseEventLocationAndReset() {
36     gfx::Point p = mouse_event_location_;
37     mouse_event_location_.SetPoint(-100, -100);
38     return p;
39   }
40
41   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
42     mouse_event_location_ = event->location();
43     event->SetHandled();
44   }
45
46  private:
47   gfx::Point mouse_event_location_;
48
49   DISALLOW_COPY_AND_ASSIGN(MouseEventLocationDelegate);
50 };
51
52 }  // namespace
53
54 typedef test::AshTestBase AshNativeCursorManagerTest;
55
56 TEST_F(AshNativeCursorManagerTest, LockCursor) {
57   CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
58   CursorManagerTestApi test_api(cursor_manager);
59   gfx::Display display(0);
60 #if defined(OS_WIN)
61   ui::CursorLoaderWin::SetCursorResourceModule(L"ash_unittests.exe");
62 #endif
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());
74
75   cursor_manager->LockCursor();
76   EXPECT_TRUE(cursor_manager->IsCursorLocked());
77
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());
86
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());
93
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());
97
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());
104
105   cursor_manager->UnlockCursor();
106   EXPECT_FALSE(cursor_manager->IsCursorLocked());
107
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());
113 }
114
115 TEST_F(AshNativeCursorManagerTest, SetCursor) {
116   CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
117   CursorManagerTestApi test_api(cursor_manager);
118 #if defined(OS_WIN)
119   ui::CursorLoaderWin::SetCursorResourceModule(L"ash_unittests.exe");
120 #endif
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());
127 }
128
129 TEST_F(AshNativeCursorManagerTest, SetCursorSet) {
130   CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
131   CursorManagerTestApi test_api(cursor_manager);
132
133   EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
134
135   cursor_manager->SetCursorSet(ui::CURSOR_SET_NORMAL);
136   EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
137
138   cursor_manager->SetCursorSet(ui::CURSOR_SET_LARGE);
139   EXPECT_EQ(ui::CURSOR_SET_LARGE, test_api.GetCurrentCursorSet());
140
141   cursor_manager->SetCursorSet(ui::CURSOR_SET_NORMAL);
142   EXPECT_EQ(ui::CURSOR_SET_NORMAL, test_api.GetCurrentCursorSet());
143 }
144
145 TEST_F(AshNativeCursorManagerTest, SetScale) {
146   CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
147   CursorManagerTestApi test_api(cursor_manager);
148
149   EXPECT_EQ(1.f, test_api.GetCurrentScale());
150
151   cursor_manager->SetScale(2.5f);
152   EXPECT_EQ(2.5f, test_api.GetCurrentScale());
153
154   cursor_manager->SetScale(1.f);
155   EXPECT_EQ(1.f, test_api.GetCurrentScale());
156 }
157
158 TEST_F(AshNativeCursorManagerTest, SetDeviceScaleFactorAndRotation) {
159   CursorManager* cursor_manager = Shell::GetInstance()->cursor_manager();
160   CursorManagerTestApi test_api(cursor_manager);
161
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());
167
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());
173 }
174
175 TEST_F(AshNativeCursorManagerTest, DisabledQueryMouseLocation) {
176   aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
177 #if defined(OS_WIN)
178   if (base::win::GetVersion() < base::win::VERSION_WIN8)
179     return;
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();
185 #endif
186   root_window->MoveCursorTo(gfx::Point(10, 10));
187 #if defined(OS_WIN)
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
192   // completed.
193   Sleep(100);
194   RunAllPendingInMessageLoop();
195 #endif
196   aura::WindowEventDispatcher* dispatcher = root_window->GetDispatcher();
197   gfx::Point mouse_location;
198   EXPECT_TRUE(dispatcher->QueryMouseLocationForTest(&mouse_location));
199   EXPECT_EQ("10,10", mouse_location.ToString());
200   Shell::GetInstance()->cursor_manager()->DisableMouseEvents();
201   EXPECT_FALSE(dispatcher->QueryMouseLocationForTest(&mouse_location));
202   EXPECT_EQ("0,0", mouse_location.ToString());
203 }
204
205 }  // namespace test
206 }  // namespace ash