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