Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / wm / window_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/shell.h"
6 #include "ash/shell_window_ids.h"
7 #include "ash/test/ash_test_base.h"
8 #include "ash/test/shell_test_api.h"
9 #include "ash/test/test_activation_delegate.h"
10 #include "ash/wm/window_util.h"
11 #include "ui/aura/client/activation_client.h"
12 #include "ui/aura/client/activation_delegate.h"
13 #include "ui/aura/client/cursor_client_observer.h"
14 #include "ui/aura/client/focus_client.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/root_window.h"
17 #include "ui/aura/test/aura_test_base.h"
18 #include "ui/aura/test/event_generator.h"
19 #include "ui/aura/test/test_event_handler.h"
20 #include "ui/aura/test/test_window_delegate.h"
21 #include "ui/aura/test/test_windows.h"
22 #include "ui/base/cursor/cursor.h"
23 #include "ui/base/hit_test.h"
24 #include "ui/events/event.h"
25 #include "ui/events/event_utils.h"
26 #include "ui/gfx/screen.h"
27 #include "ui/views/corewm/compound_event_filter.h"
28 #include "ui/views/corewm/corewm_switches.h"
29 #include "ui/views/corewm/input_method_event_filter.h"
30
31 namespace {
32
33 class TestingCursorClientObserver : public aura::client::CursorClientObserver {
34  public:
35   TestingCursorClientObserver()
36       : cursor_visibility_(false),
37         did_visibility_change_(false) {}
38   void reset() { cursor_visibility_ = did_visibility_change_ = false; }
39   bool is_cursor_visible() const { return cursor_visibility_; }
40   bool did_visibility_change() const { return did_visibility_change_; }
41
42   // Overridden from aura::client::CursorClientObserver:
43   virtual void OnCursorVisibilityChanged(bool is_visible) OVERRIDE {
44     cursor_visibility_ = is_visible;
45     did_visibility_change_ = true;
46   }
47
48  private:
49   bool cursor_visibility_;
50   bool did_visibility_change_;
51
52   DISALLOW_COPY_AND_ASSIGN(TestingCursorClientObserver);
53 };
54
55 base::TimeDelta getTime() {
56   return ui::EventTimeForNow();
57 }
58
59 // A slightly changed TestEventHandler which can be configured to return a
60 // specified value for key/mouse event handling.
61 class CustomEventHandler : public aura::test::TestEventHandler {
62  public:
63   CustomEventHandler()
64       : key_result_(ui::ER_UNHANDLED),
65         mouse_result_(ui::ER_UNHANDLED) {
66   }
67
68   virtual ~CustomEventHandler() {}
69
70   void set_key_event_handling_result(ui::EventResult result) {
71     key_result_ = result;
72   }
73
74   void set_mouse_event_handling_result(ui::EventResult result) {
75     mouse_result_ = result;
76   }
77
78   // Overridden from ui::EventHandler:
79   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
80     aura::test::TestEventHandler::OnKeyEvent(event);
81     if (key_result_ & ui::ER_HANDLED)
82       event->SetHandled();
83     if (key_result_ & ui::ER_CONSUMED)
84       event->StopPropagation();
85   }
86
87   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
88     aura::test::TestEventHandler::OnMouseEvent(event);
89     if (mouse_result_ & ui::ER_HANDLED)
90       event->SetHandled();
91     if (mouse_result_ & ui::ER_CONSUMED)
92       event->StopPropagation();
93   }
94
95  private:
96   ui::EventResult key_result_;
97   ui::EventResult mouse_result_;
98
99   DISALLOW_COPY_AND_ASSIGN(CustomEventHandler);
100 };
101
102 }  // namespace
103
104 namespace ash {
105
106 typedef test::AshTestBase WindowManagerTest;
107
108 class NonFocusableDelegate : public aura::test::TestWindowDelegate {
109  public:
110   NonFocusableDelegate() {}
111
112  private:
113   virtual bool CanFocus() OVERRIDE {
114     return false;
115   }
116
117   DISALLOW_COPY_AND_ASSIGN(NonFocusableDelegate);
118 };
119
120 class HitTestWindowDelegate : public aura::test::TestWindowDelegate {
121  public:
122   HitTestWindowDelegate()
123       : hittest_code_(HTNOWHERE) {
124   }
125   virtual ~HitTestWindowDelegate() {}
126   void set_hittest_code(int hittest_code) { hittest_code_ = hittest_code; }
127
128  private:
129   // Overridden from TestWindowDelegate:
130   virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
131     return hittest_code_;
132   }
133
134   int hittest_code_;
135
136   DISALLOW_COPY_AND_ASSIGN(HitTestWindowDelegate);
137 };
138
139 TEST_F(WindowManagerTest, Focus) {
140   // The IME event filter interferes with the basic key event propagation we
141   // attempt to do here, so we remove it.
142   test::ShellTestApi shell_test(Shell::GetInstance());
143   Shell::GetInstance()->RemovePreTargetHandler(
144       shell_test.input_method_event_filter());
145
146   aura::Window* root_window = Shell::GetPrimaryRootWindow();
147   root_window->SetBounds(gfx::Rect(0, 0, 510, 510));
148
149   // Supplied ids are negative so as not to collide with shell ids.
150   // TODO(beng): maybe introduce a MAKE_SHELL_ID() macro that generates a safe
151   //             id beyond shell id max?
152   scoped_ptr<aura::Window> w1(CreateTestWindowInShell(
153       SK_ColorWHITE, -1, gfx::Rect(10, 10, 500, 500)));
154   scoped_ptr<aura::Window> w11(aura::test::CreateTestWindow(
155       SK_ColorGREEN, -11, gfx::Rect(5, 5, 100, 100), w1.get()));
156   scoped_ptr<aura::Window> w111(aura::test::CreateTestWindow(
157       SK_ColorCYAN, -111, gfx::Rect(5, 5, 75, 75), w11.get()));
158   scoped_ptr<aura::Window> w1111(aura::test::CreateTestWindow(
159       SK_ColorRED, -1111, gfx::Rect(5, 5, 50, 50), w111.get()));
160   scoped_ptr<aura::Window> w12(aura::test::CreateTestWindow(
161       SK_ColorMAGENTA, -12, gfx::Rect(10, 420, 25, 25), w1.get()));
162   aura::test::ColorTestWindowDelegate* w121delegate =
163       new aura::test::ColorTestWindowDelegate(SK_ColorYELLOW);
164   scoped_ptr<aura::Window> w121(aura::test::CreateTestWindowWithDelegate(
165       w121delegate, -121, gfx::Rect(5, 5, 5, 5), w12.get()));
166   aura::test::ColorTestWindowDelegate* w122delegate =
167       new aura::test::ColorTestWindowDelegate(SK_ColorRED);
168   scoped_ptr<aura::Window> w122(aura::test::CreateTestWindowWithDelegate(
169       w122delegate, -122, gfx::Rect(10, 5, 5, 5), w12.get()));
170   aura::test::ColorTestWindowDelegate* w123delegate =
171       new aura::test::ColorTestWindowDelegate(SK_ColorRED);
172   scoped_ptr<aura::Window> w123(aura::test::CreateTestWindowWithDelegate(
173       w123delegate, -123, gfx::Rect(15, 5, 5, 5), w12.get()));
174   scoped_ptr<aura::Window> w13(aura::test::CreateTestWindow(
175       SK_ColorGRAY, -13, gfx::Rect(5, 470, 50, 50), w1.get()));
176
177   // Click on a sub-window (w121) to focus it.
178   aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
179                                        w121.get());
180   generator.ClickLeftButton();
181
182   aura::client::FocusClient* focus_client =
183       aura::client::GetFocusClient(w121.get());
184   EXPECT_EQ(w121.get(), focus_client->GetFocusedWindow());
185
186   aura::WindowEventDispatcher* dispatcher = root_window->GetDispatcher();
187
188   // The key press should be sent to the focused sub-window.
189   ui::KeyEvent keyev(ui::ET_KEY_PRESSED, ui::VKEY_E, 0, false);
190   ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&keyev);
191   ASSERT_FALSE(details.dispatcher_destroyed);
192   EXPECT_EQ(ui::VKEY_E, w121delegate->last_key_code());
193
194   // Touch on a sub-window (w122) to focus it.
195   gfx::Point click_point = w122->bounds().CenterPoint();
196   aura::Window::ConvertPointToTarget(w122->parent(), root_window, &click_point);
197   ui::TouchEvent touchev(ui::ET_TOUCH_PRESSED, click_point, 0, getTime());
198   details = dispatcher->OnEventFromSource(&touchev);
199   ASSERT_FALSE(details.dispatcher_destroyed);
200   focus_client = aura::client::GetFocusClient(w122.get());
201   EXPECT_EQ(w122.get(), focus_client->GetFocusedWindow());
202
203   // The key press should be sent to the focused sub-window.
204   details = dispatcher->OnEventFromSource(&keyev);
205   ASSERT_FALSE(details.dispatcher_destroyed);
206   EXPECT_EQ(ui::VKEY_E, w122delegate->last_key_code());
207
208   // Hiding the focused window will set the focus to its parent if
209   // it's focusable.
210   w122->Hide();
211   EXPECT_EQ(aura::client::GetFocusClient(w12.get()),
212             aura::client::GetFocusClient(w122.get()));
213   EXPECT_EQ(w12.get(),
214             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
215
216   // Sets the focus back to w122.
217   w122->Show();
218   w122->Focus();
219   EXPECT_EQ(w122.get(),
220             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
221
222   // Removing the focused window from parent should set the focus to
223   // its parent if it's focusable.
224   w12->RemoveChild(w122.get());
225   EXPECT_EQ(NULL, aura::client::GetFocusClient(w122.get()));
226   EXPECT_EQ(w12.get(),
227             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
228
229   // Set the focus to w123, but make the w1 not activatable.
230   test::TestActivationDelegate activation_delegate(false);
231   w123->Focus();
232   EXPECT_EQ(w123.get(),
233             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
234   aura::client::SetActivationDelegate(w1.get(), &activation_delegate);
235
236   // Hiding the focused window will set the focus to NULL because
237   // parent window is not focusable.
238   w123->Hide();
239   EXPECT_EQ(aura::client::GetFocusClient(w12.get()),
240             aura::client::GetFocusClient(w123.get()));
241   EXPECT_EQ(NULL, aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
242   details = dispatcher->OnEventFromSource(&keyev);
243   EXPECT_FALSE(keyev.handled() || details.dispatcher_destroyed);
244
245   // Set the focus back to w123
246   aura::client::SetActivationDelegate(w1.get(), NULL);
247   w123->Show();
248   w123->Focus();
249   EXPECT_EQ(w123.get(),
250             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
251   aura::client::SetActivationDelegate(w1.get(), &activation_delegate);
252
253   // Removing the focused window will set the focus to NULL because
254   // parent window is not focusable.
255   w12->RemoveChild(w123.get());
256   EXPECT_EQ(NULL, aura::client::GetFocusClient(w123.get()));
257   details = dispatcher->OnEventFromSource(&keyev);
258   EXPECT_FALSE(keyev.handled() || details.dispatcher_destroyed);
259 }
260
261 // Various assertion testing for activating windows.
262 TEST_F(WindowManagerTest, ActivateOnMouse) {
263   aura::Window* root_window = Shell::GetPrimaryRootWindow();
264
265   test::TestActivationDelegate d1;
266   aura::test::TestWindowDelegate wd;
267   scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate(
268       &wd, -1, gfx::Rect(10, 10, 50, 50)));
269   d1.SetWindow(w1.get());
270   test::TestActivationDelegate d2;
271   scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate(
272       &wd, -2, gfx::Rect(70, 70, 50, 50)));
273   d2.SetWindow(w2.get());
274
275   aura::client::FocusClient* focus_client =
276       aura::client::GetFocusClient(w1.get());
277
278   d1.Clear();
279   d2.Clear();
280
281   // Activate window1.
282   wm::ActivateWindow(w1.get());
283   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
284   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
285   EXPECT_EQ(1, d1.activated_count());
286   EXPECT_EQ(0, d1.lost_active_count());
287   d1.Clear();
288
289   {
290     // Click on window2.
291     aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
292                                          w2.get());
293     generator.ClickLeftButton();
294
295     // Window2 should have become active.
296     EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
297     EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
298     EXPECT_EQ(0, d1.activated_count());
299     EXPECT_EQ(1, d1.lost_active_count());
300     EXPECT_EQ(1, d2.activated_count());
301     EXPECT_EQ(0, d2.lost_active_count());
302     d1.Clear();
303     d2.Clear();
304   }
305
306   {
307     // Click back on window1, but set it up so w1 doesn't activate on click.
308     aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
309                                          w1.get());
310     d1.set_activate(false);
311     generator.ClickLeftButton();
312
313     // Window2 should still be active and focused.
314     EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
315     EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
316     EXPECT_EQ(0, d1.activated_count());
317     EXPECT_EQ(0, d1.lost_active_count());
318     EXPECT_EQ(0, d2.activated_count());
319     EXPECT_EQ(0, d2.lost_active_count());
320     d1.Clear();
321     d2.Clear();
322   }
323
324   // Destroy window2, this should make window1 active.
325   d1.set_activate(true);
326   w2.reset();
327   EXPECT_EQ(0, d2.activated_count());
328   EXPECT_EQ(1, d2.lost_active_count());
329   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
330   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
331   EXPECT_EQ(1, d1.activated_count());
332   EXPECT_EQ(0, d1.lost_active_count());
333
334   // Clicking an active window with a child shouldn't steal the
335   // focus from the child.
336   {
337     scoped_ptr<aura::Window> w11(CreateTestWindowWithDelegate(
338           &wd, -11, gfx::Rect(10, 10, 10, 10), w1.get()));
339     aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
340                                          w11.get());
341     // First set the focus to the child |w11|.
342     generator.ClickLeftButton();
343     EXPECT_EQ(w11.get(), focus_client->GetFocusedWindow());
344     EXPECT_EQ(w1.get(), wm::GetActiveWindow());
345
346     // Then click the parent active window. The focus shouldn't move.
347     gfx::Point left_top = w1->bounds().origin();
348     aura::Window::ConvertPointToTarget(w1->parent(), root_window, &left_top);
349     left_top.Offset(1, 1);
350     generator.MoveMouseTo(left_top);
351     generator.ClickLeftButton();
352     EXPECT_EQ(w11.get(), focus_client->GetFocusedWindow());
353     EXPECT_EQ(w1.get(), wm::GetActiveWindow());
354   }
355
356   // Clicking on a non-focusable window inside a background window should still
357   // give focus to the background window.
358   {
359     NonFocusableDelegate nfd;
360     scoped_ptr<aura::Window> w11(CreateTestWindowWithDelegate(
361           &nfd, -1, gfx::Rect(10, 10, 10, 10), w1.get()));
362     // Move focus to |w2| first.
363     scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate(
364           &wd, -1, gfx::Rect(70, 70, 50, 50)));
365     aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
366                                          w2.get());
367     generator.ClickLeftButton();
368     EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
369     EXPECT_FALSE(w11->CanFocus());
370
371     // Click on |w11|. This should focus w1.
372     generator.MoveMouseToCenterOf(w11.get());
373     generator.ClickLeftButton();
374     EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
375   }
376 }
377
378 TEST_F(WindowManagerTest, PanelActivation) {
379   aura::test::TestWindowDelegate wd;
380   scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate(
381       &wd, -1, gfx::Rect(10, 10, 50, 50)));
382   aura::test::TestWindowDelegate pd;
383   scoped_ptr<aura::Window> p1(CreateTestWindowInShellWithDelegateAndType(
384       &pd, ui::wm::WINDOW_TYPE_PANEL, -1, gfx::Rect(10, 10, 50, 50)));
385   aura::client::FocusClient* focus_client =
386       aura::client::GetFocusClient(w1.get());
387
388   // Activate w1.
389   wm::ActivateWindow(w1.get());
390   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
391
392   // Activate p1.
393   wm::ActivateWindow(p1.get());
394   EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
395   EXPECT_EQ(p1.get(), focus_client->GetFocusedWindow());
396
397   // Activate w1.
398   wm::ActivateWindow(w1.get());
399   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
400   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
401
402   // Clicking on a non-activatable window should not change the active window.
403   {
404     NonFocusableDelegate nfd;
405     scoped_ptr<aura::Window> w3(CreateTestWindowInShellWithDelegate(
406           &nfd, -1, gfx::Rect(70, 70, 50, 50)));
407     aura::test::EventGenerator generator3(Shell::GetPrimaryRootWindow(),
408                                          w3.get());
409     wm::ActivateWindow(p1.get());
410     EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
411     generator3.ClickLeftButton();
412     EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
413   }
414 }
415
416 // Essentially the same as ActivateOnMouse, but for touch events.
417 TEST_F(WindowManagerTest, ActivateOnTouch) {
418   aura::Window* root_window = Shell::GetPrimaryRootWindow();
419
420   test::TestActivationDelegate d1;
421   aura::test::TestWindowDelegate wd;
422   scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate(
423       &wd, -1, gfx::Rect(10, 10, 50, 50)));
424   d1.SetWindow(w1.get());
425   test::TestActivationDelegate d2;
426   scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate(
427       &wd, -2, gfx::Rect(70, 70, 50, 50)));
428   d2.SetWindow(w2.get());
429
430   aura::client::FocusClient* focus_client =
431       aura::client::GetFocusClient(w1.get());
432
433   d1.Clear();
434   d2.Clear();
435
436   // Activate window1.
437   wm::ActivateWindow(w1.get());
438   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
439   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
440   EXPECT_EQ(1, d1.activated_count());
441   EXPECT_EQ(0, d1.lost_active_count());
442   d1.Clear();
443
444   // Touch window2.
445   gfx::Point press_point = w2->bounds().CenterPoint();
446   aura::Window::ConvertPointToTarget(w2->parent(), root_window, &press_point);
447   ui::TouchEvent touchev1(ui::ET_TOUCH_PRESSED, press_point, 0, getTime());
448
449   aura::WindowEventDispatcher* dispatcher = root_window->GetDispatcher();
450   ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&touchev1);
451   ASSERT_FALSE(details.dispatcher_destroyed);
452
453   // Window2 should have become active.
454   EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
455   EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
456   EXPECT_EQ(0, d1.activated_count());
457   EXPECT_EQ(1, d1.lost_active_count());
458   EXPECT_EQ(1, d2.activated_count());
459   EXPECT_EQ(0, d2.lost_active_count());
460   d1.Clear();
461   d2.Clear();
462
463   // Touch window1, but set it up so w1 doesn't activate on touch.
464   press_point = w1->bounds().CenterPoint();
465   aura::Window::ConvertPointToTarget(w1->parent(), root_window, &press_point);
466   d1.set_activate(false);
467   ui::TouchEvent touchev2(ui::ET_TOUCH_PRESSED, press_point, 1, getTime());
468   details = dispatcher->OnEventFromSource(&touchev2);
469   ASSERT_FALSE(details.dispatcher_destroyed);
470
471   // Window2 should still be active and focused.
472   EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
473   EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
474   EXPECT_EQ(0, d1.activated_count());
475   EXPECT_EQ(0, d1.lost_active_count());
476   EXPECT_EQ(0, d2.activated_count());
477   EXPECT_EQ(0, d2.lost_active_count());
478   d1.Clear();
479   d2.Clear();
480
481   // Destroy window2, this should make window1 active.
482   d1.set_activate(true);
483   w2.reset();
484   EXPECT_EQ(0, d2.activated_count());
485   EXPECT_EQ(1, d2.lost_active_count());
486   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
487   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
488   EXPECT_EQ(1, d1.activated_count());
489   EXPECT_EQ(0, d1.lost_active_count());
490 }
491
492 TEST_F(WindowManagerTest, MouseEventCursors) {
493   aura::Window* root_window = Shell::GetPrimaryRootWindow();
494
495   // Create a window.
496   const int kWindowLeft = 123;
497   const int kWindowTop = 45;
498   HitTestWindowDelegate window_delegate;
499   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
500     &window_delegate,
501     -1,
502     gfx::Rect(kWindowLeft, kWindowTop, 640, 480)));
503
504   // Create two mouse movement events we can switch between.
505   gfx::Point point1(kWindowLeft, kWindowTop);
506   aura::Window::ConvertPointToTarget(window->parent(), root_window, &point1);
507
508   gfx::Point point2(kWindowLeft + 1, kWindowTop + 1);
509   aura::Window::ConvertPointToTarget(window->parent(), root_window, &point2);
510
511   aura::WindowEventDispatcher* dispatcher = root_window->GetDispatcher();
512
513   // Cursor starts as a pointer (set during Shell::Init()).
514   EXPECT_EQ(ui::kCursorPointer,
515             dispatcher->host()->last_cursor().native_type());
516
517   {
518     // Resize edges and corners show proper cursors.
519     window_delegate.set_hittest_code(HTBOTTOM);
520     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
521     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
522     ASSERT_FALSE(details.dispatcher_destroyed);
523     EXPECT_EQ(ui::kCursorSouthResize,
524               dispatcher->host()->last_cursor().native_type());
525   }
526
527   {
528     window_delegate.set_hittest_code(HTBOTTOMLEFT);
529     ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
530     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
531     ASSERT_FALSE(details.dispatcher_destroyed);
532     EXPECT_EQ(ui::kCursorSouthWestResize,
533               dispatcher->host()->last_cursor().native_type());
534   }
535
536   {
537     window_delegate.set_hittest_code(HTBOTTOMRIGHT);
538     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
539     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
540     ASSERT_FALSE(details.dispatcher_destroyed);
541     EXPECT_EQ(ui::kCursorSouthEastResize,
542               dispatcher->host()->last_cursor().native_type());
543   }
544
545   {
546     window_delegate.set_hittest_code(HTLEFT);
547     ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
548     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
549     ASSERT_FALSE(details.dispatcher_destroyed);
550     EXPECT_EQ(ui::kCursorWestResize,
551               dispatcher->host()->last_cursor().native_type());
552   }
553
554   {
555     window_delegate.set_hittest_code(HTRIGHT);
556     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
557     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
558     ASSERT_FALSE(details.dispatcher_destroyed);
559     EXPECT_EQ(ui::kCursorEastResize,
560               dispatcher->host()->last_cursor().native_type());
561   }
562
563   {
564     window_delegate.set_hittest_code(HTTOP);
565     ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
566     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
567     ASSERT_FALSE(details.dispatcher_destroyed);
568     EXPECT_EQ(ui::kCursorNorthResize,
569               dispatcher->host()->last_cursor().native_type());
570   }
571
572   {
573     window_delegate.set_hittest_code(HTTOPLEFT);
574     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
575     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
576     ASSERT_FALSE(details.dispatcher_destroyed);
577     EXPECT_EQ(ui::kCursorNorthWestResize,
578               dispatcher->host()->last_cursor().native_type());
579   }
580
581   {
582     window_delegate.set_hittest_code(HTTOPRIGHT);
583     ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
584     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
585     ASSERT_FALSE(details.dispatcher_destroyed);
586     EXPECT_EQ(ui::kCursorNorthEastResize,
587               dispatcher->host()->last_cursor().native_type());
588   }
589
590   {
591     // Client area uses null cursor.
592     window_delegate.set_hittest_code(HTCLIENT);
593     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
594     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
595     ASSERT_FALSE(details.dispatcher_destroyed);
596     EXPECT_EQ(ui::kCursorNull, dispatcher->host()->last_cursor().native_type());
597   }
598 }
599
600 #if defined(OS_WIN)
601 #define MAYBE_TransformActivate DISABLED_TransformActivate
602 #else
603 #define MAYBE_TransformActivate TransformActivate
604 #endif
605 TEST_F(WindowManagerTest, MAYBE_TransformActivate) {
606   aura::Window* root_window = Shell::GetPrimaryRootWindow();
607   gfx::Size size = root_window->bounds().size();
608   EXPECT_EQ(gfx::Rect(size).ToString(),
609             Shell::GetScreen()->GetDisplayNearestPoint(
610                 gfx::Point()).bounds().ToString());
611
612   // Rotate it clock-wise 90 degrees.
613   gfx::Transform transform;
614   transform.Translate(size.width(), 0);
615   transform.Rotate(90.0f);
616   root_window->GetDispatcher()->host()->SetTransform(transform);
617
618   test::TestActivationDelegate d1;
619   aura::test::TestWindowDelegate wd;
620   scoped_ptr<aura::Window> w1(
621       CreateTestWindowInShellWithDelegate(&wd, 1, gfx::Rect(0, 15, 50, 50)));
622   d1.SetWindow(w1.get());
623   w1->Show();
624
625   gfx::Point miss_point(5, 5);
626   transform.TransformPoint(&miss_point);
627   ui::MouseEvent mouseev1(ui::ET_MOUSE_PRESSED,
628                           miss_point,
629                           miss_point,
630                           ui::EF_LEFT_MOUSE_BUTTON,
631                           ui::EF_LEFT_MOUSE_BUTTON);
632   aura::WindowEventDispatcher* dispatcher = root_window->GetDispatcher();
633   ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&mouseev1);
634   ASSERT_FALSE(details.dispatcher_destroyed);
635   EXPECT_EQ(NULL, aura::client::GetFocusClient(w1.get())->GetFocusedWindow());
636   ui::MouseEvent mouseup(ui::ET_MOUSE_RELEASED,
637                          miss_point,
638                          miss_point,
639                          ui::EF_LEFT_MOUSE_BUTTON,
640                          ui::EF_LEFT_MOUSE_BUTTON);
641   details = dispatcher->OnEventFromSource(&mouseup);
642   ASSERT_FALSE(details.dispatcher_destroyed);
643
644   gfx::Point hit_point(5, 15);
645   transform.TransformPoint(&hit_point);
646   ui::MouseEvent mouseev2(ui::ET_MOUSE_PRESSED,
647                           hit_point,
648                           hit_point,
649                           ui::EF_LEFT_MOUSE_BUTTON,
650                           ui::EF_LEFT_MOUSE_BUTTON);
651   details = dispatcher->OnEventFromSource(&mouseev2);
652   ASSERT_FALSE(details.dispatcher_destroyed);
653   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
654   EXPECT_EQ(w1.get(),
655             aura::client::GetFocusClient(w1.get())->GetFocusedWindow());
656 }
657
658 TEST_F(WindowManagerTest, AdditionalFilters) {
659   // The IME event filter interferes with the basic key event propagation we
660   // attempt to do here, so we remove it.
661   test::ShellTestApi shell_test(Shell::GetInstance());
662   Shell::GetInstance()->RemovePreTargetHandler(
663       shell_test.input_method_event_filter());
664
665   aura::Window* root_window = Shell::GetPrimaryRootWindow();
666
667   // Creates a window and make it active
668   scoped_ptr<aura::Window> w1(CreateTestWindowInShell(
669       SK_ColorWHITE, -1, gfx::Rect(0, 0, 100, 100)));
670   wm::ActivateWindow(w1.get());
671
672   // Creates two addition filters
673   scoped_ptr<CustomEventHandler> f1(new CustomEventHandler);
674   scoped_ptr<CustomEventHandler> f2(new CustomEventHandler);
675
676   // Adds them to root window event filter.
677   views::corewm::CompoundEventFilter* env_filter =
678       Shell::GetInstance()->env_filter();
679   env_filter->AddHandler(f1.get());
680   env_filter->AddHandler(f2.get());
681
682   // Dispatches mouse and keyboard events.
683   ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, false);
684   aura::WindowEventDispatcher* dispatcher = root_window->GetDispatcher();
685   ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&key_event);
686   ASSERT_FALSE(details.dispatcher_destroyed);
687   ui::MouseEvent mouse_pressed(
688       ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
689   details = dispatcher->OnEventFromSource(&mouse_pressed);
690   ASSERT_FALSE(details.dispatcher_destroyed);
691
692   // Both filters should get the events.
693   EXPECT_EQ(1, f1->num_key_events());
694   EXPECT_EQ(1, f1->num_mouse_events());
695   EXPECT_EQ(1, f2->num_key_events());
696   EXPECT_EQ(1, f2->num_mouse_events());
697
698   f1->Reset();
699   f2->Reset();
700
701   // Makes f1 consume events.
702   f1->set_key_event_handling_result(ui::ER_CONSUMED);
703   f1->set_mouse_event_handling_result(ui::ER_CONSUMED);
704
705   // Dispatches events.
706   details = dispatcher->OnEventFromSource(&key_event);
707   ASSERT_FALSE(details.dispatcher_destroyed);
708   ui::MouseEvent mouse_released(
709       ui::ET_MOUSE_RELEASED, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
710   details = dispatcher->OnEventFromSource(&mouse_released);
711   ASSERT_FALSE(details.dispatcher_destroyed);
712
713   // f1 should still get the events but f2 no longer gets them.
714   EXPECT_EQ(1, f1->num_key_events());
715   EXPECT_EQ(1, f1->num_mouse_events());
716   EXPECT_EQ(0, f2->num_key_events());
717   EXPECT_EQ(0, f2->num_mouse_events());
718
719   f1->Reset();
720   f2->Reset();
721
722   // Remove f1 from additonal filters list.
723   env_filter->RemoveHandler(f1.get());
724
725   // Dispatches events.
726   details = dispatcher->OnEventFromSource(&key_event);
727   ASSERT_FALSE(details.dispatcher_destroyed);
728   details = dispatcher->OnEventFromSource(&mouse_pressed);
729   ASSERT_FALSE(details.dispatcher_destroyed);
730
731   // f1 should get no events since it's out and f2 should get them.
732   EXPECT_EQ(0, f1->num_key_events());
733   EXPECT_EQ(0, f1->num_mouse_events());
734   EXPECT_EQ(1, f2->num_key_events());
735   EXPECT_EQ(1, f2->num_mouse_events());
736
737   env_filter->RemoveHandler(f2.get());
738 }
739
740 #if defined(OS_CHROMEOS)
741 // Touch visually hides the cursor on ChromeOS and Windows, but we only update
742 // our internal tracking of the cursor state on ChromeOS (crbug.com/333952).
743 TEST_F(WindowManagerTest, UpdateCursorVisibility) {
744   aura::test::EventGenerator& generator = GetEventGenerator();
745   views::corewm::CursorManager* cursor_manager =
746       ash::Shell::GetInstance()->cursor_manager();
747
748   generator.MoveMouseTo(gfx::Point(0, 0));
749   EXPECT_TRUE(cursor_manager->IsCursorVisible());
750   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
751   generator.PressTouch();
752   EXPECT_FALSE(cursor_manager->IsCursorVisible());
753   EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
754   generator.MoveMouseTo(gfx::Point(0, 0));
755   EXPECT_TRUE(cursor_manager->IsCursorVisible());
756   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
757   generator.ReleaseTouch();
758   EXPECT_TRUE(cursor_manager->IsCursorVisible());
759   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
760 }
761
762 // ChromeOS is the only platform for which the cursor is hidden on keypress
763 // (crbug.com/304296).
764 TEST_F(WindowManagerTest, UpdateCursorVisibilityOnKeyEvent) {
765   aura::test::EventGenerator& generator = GetEventGenerator();
766   views::corewm::CursorManager* cursor_manager =
767       ash::Shell::GetInstance()->cursor_manager();
768
769   // Pressing a key hides the cursor but does not disable mouse events.
770   generator.PressKey(ui::VKEY_A, ui::EF_NONE);
771   EXPECT_FALSE(cursor_manager->IsCursorVisible());
772   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
773   // Moving mouse shows the cursor.
774   generator.MoveMouseTo(gfx::Point(0, 0));
775   EXPECT_TRUE(cursor_manager->IsCursorVisible());
776   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
777   // Releasing a key also hides the cursor but does not disable mouse events.
778   generator.ReleaseKey(ui::VKEY_A, ui::EF_NONE);
779   EXPECT_FALSE(cursor_manager->IsCursorVisible());
780   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
781   // Moving mouse shows the cursor again.
782   generator.MoveMouseTo(gfx::Point(0, 0));
783   EXPECT_TRUE(cursor_manager->IsCursorVisible());
784   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
785 }
786
787 TEST_F(WindowManagerTest, TestCursorClientObserver) {
788   aura::test::EventGenerator& generator = GetEventGenerator();
789   views::corewm::CursorManager* cursor_manager =
790       ash::Shell::GetInstance()->cursor_manager();
791
792   scoped_ptr<aura::Window> w1(CreateTestWindowInShell(
793       SK_ColorWHITE, -1, gfx::Rect(0, 0, 100, 100)));
794   wm::ActivateWindow(w1.get());
795
796   // Add two observers. Both should have OnCursorVisibilityChanged()
797   // invoked when an event changes the visibility of the cursor.
798   TestingCursorClientObserver observer_a;
799   TestingCursorClientObserver observer_b;
800   cursor_manager->AddObserver(&observer_a);
801   cursor_manager->AddObserver(&observer_b);
802
803   // Initial state before any events have been sent.
804   observer_a.reset();
805   observer_b.reset();
806   EXPECT_FALSE(observer_a.did_visibility_change());
807   EXPECT_FALSE(observer_b.did_visibility_change());
808   EXPECT_FALSE(observer_a.is_cursor_visible());
809   EXPECT_FALSE(observer_b.is_cursor_visible());
810
811   // Keypress should hide the cursor.
812   generator.PressKey(ui::VKEY_A, ui::EF_NONE);
813   EXPECT_TRUE(observer_a.did_visibility_change());
814   EXPECT_TRUE(observer_b.did_visibility_change());
815   EXPECT_FALSE(observer_a.is_cursor_visible());
816   EXPECT_FALSE(observer_b.is_cursor_visible());
817
818   // Mouse move should show the cursor.
819   observer_a.reset();
820   observer_b.reset();
821   generator.MoveMouseTo(50, 50);
822   EXPECT_TRUE(observer_a.did_visibility_change());
823   EXPECT_TRUE(observer_b.did_visibility_change());
824   EXPECT_TRUE(observer_a.is_cursor_visible());
825   EXPECT_TRUE(observer_b.is_cursor_visible());
826
827   // Remove observer_b. Its OnCursorVisibilityChanged() should
828   // not be invoked past this point.
829   cursor_manager->RemoveObserver(&observer_b);
830
831   // Gesture tap should hide the cursor.
832   observer_a.reset();
833   observer_b.reset();
834   generator.GestureTapAt(gfx::Point(25, 25));
835   EXPECT_TRUE(observer_a.did_visibility_change());
836   EXPECT_FALSE(observer_b.did_visibility_change());
837   EXPECT_FALSE(observer_a.is_cursor_visible());
838
839   // Mouse move should show the cursor.
840   observer_a.reset();
841   observer_b.reset();
842   generator.MoveMouseTo(50, 50);
843   EXPECT_TRUE(observer_a.did_visibility_change());
844   EXPECT_FALSE(observer_b.did_visibility_change());
845   EXPECT_TRUE(observer_a.is_cursor_visible());
846 }
847 #endif  // defined(OS_CHROMEOS)
848
849 }  // namespace ash