Upstream version 9.38.198.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   details = dispatcher->OnEventFromSource(&keyev);
203   ASSERT_FALSE(details.dispatcher_destroyed);
204   EXPECT_EQ(ui::VKEY_E, w122delegate->last_key_code());
205
206   // Hiding the focused window will set the focus to its parent if
207   // it's focusable.
208   w122->Hide();
209   EXPECT_EQ(aura::client::GetFocusClient(w12.get()),
210             aura::client::GetFocusClient(w122.get()));
211   EXPECT_EQ(w12.get(),
212             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
213
214   // Sets the focus back to w122.
215   w122->Show();
216   w122->Focus();
217   EXPECT_EQ(w122.get(),
218             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
219
220   // Removing the focused window from parent should set the focus to
221   // its parent if it's focusable.
222   w12->RemoveChild(w122.get());
223   EXPECT_EQ(NULL, aura::client::GetFocusClient(w122.get()));
224   EXPECT_EQ(w12.get(),
225             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
226
227   // Set the focus to w123, but make the w1 not activatable.
228   test::TestActivationDelegate activation_delegate(false);
229   w123->Focus();
230   EXPECT_EQ(w123.get(),
231             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
232   aura::client::SetActivationDelegate(w1.get(), &activation_delegate);
233
234   // Hiding the focused window will set the focus to NULL because
235   // parent window is not focusable.
236   w123->Hide();
237   EXPECT_EQ(aura::client::GetFocusClient(w12.get()),
238             aura::client::GetFocusClient(w123.get()));
239   EXPECT_EQ(NULL, aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
240   details = dispatcher->OnEventFromSource(&keyev);
241   EXPECT_FALSE(keyev.handled() || details.dispatcher_destroyed);
242
243   // Set the focus back to w123
244   aura::client::SetActivationDelegate(w1.get(), NULL);
245   w123->Show();
246   w123->Focus();
247   EXPECT_EQ(w123.get(),
248             aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
249   aura::client::SetActivationDelegate(w1.get(), &activation_delegate);
250
251   // Removing the focused window will set the focus to NULL because
252   // parent window is not focusable.
253   w12->RemoveChild(w123.get());
254   EXPECT_EQ(NULL, aura::client::GetFocusClient(w123.get()));
255   details = dispatcher->OnEventFromSource(&keyev);
256   EXPECT_FALSE(keyev.handled() || details.dispatcher_destroyed);
257 }
258
259 // Various assertion testing for activating windows.
260 TEST_F(WindowManagerTest, ActivateOnMouse) {
261   aura::Window* root_window = Shell::GetPrimaryRootWindow();
262
263   test::TestActivationDelegate d1;
264   aura::test::TestWindowDelegate wd;
265   scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate(
266       &wd, -1, gfx::Rect(10, 10, 50, 50)));
267   d1.SetWindow(w1.get());
268   test::TestActivationDelegate d2;
269   scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate(
270       &wd, -2, gfx::Rect(70, 70, 50, 50)));
271   d2.SetWindow(w2.get());
272
273   aura::client::FocusClient* focus_client =
274       aura::client::GetFocusClient(w1.get());
275
276   d1.Clear();
277   d2.Clear();
278
279   // Activate window1.
280   wm::ActivateWindow(w1.get());
281   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
282   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
283   EXPECT_EQ(1, d1.activated_count());
284   EXPECT_EQ(0, d1.lost_active_count());
285   d1.Clear();
286
287   {
288     // Click on window2.
289     ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(), w2.get());
290     generator.ClickLeftButton();
291
292     // Window2 should have become active.
293     EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
294     EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
295     EXPECT_EQ(0, d1.activated_count());
296     EXPECT_EQ(1, d1.lost_active_count());
297     EXPECT_EQ(1, d2.activated_count());
298     EXPECT_EQ(0, d2.lost_active_count());
299     d1.Clear();
300     d2.Clear();
301   }
302
303   {
304     // Click back on window1, but set it up so w1 doesn't activate on click.
305     ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(), w1.get());
306     d1.set_activate(false);
307     generator.ClickLeftButton();
308
309     // Window2 should still be active and focused.
310     EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
311     EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
312     EXPECT_EQ(0, d1.activated_count());
313     EXPECT_EQ(0, d1.lost_active_count());
314     EXPECT_EQ(0, d2.activated_count());
315     EXPECT_EQ(0, d2.lost_active_count());
316     d1.Clear();
317     d2.Clear();
318   }
319
320   // Destroy window2, this should make window1 active.
321   d1.set_activate(true);
322   w2.reset();
323   EXPECT_EQ(0, d2.activated_count());
324   EXPECT_EQ(1, d2.lost_active_count());
325   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
326   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
327   EXPECT_EQ(1, d1.activated_count());
328   EXPECT_EQ(0, d1.lost_active_count());
329
330   // Clicking an active window with a child shouldn't steal the
331   // focus from the child.
332   {
333     scoped_ptr<aura::Window> w11(CreateTestWindowWithDelegate(
334           &wd, -11, gfx::Rect(10, 10, 10, 10), w1.get()));
335     ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
336                                        w11.get());
337     // First set the focus to the child |w11|.
338     generator.ClickLeftButton();
339     EXPECT_EQ(w11.get(), focus_client->GetFocusedWindow());
340     EXPECT_EQ(w1.get(), wm::GetActiveWindow());
341
342     // Then click the parent active window. The focus shouldn't move.
343     gfx::Point left_top = w1->bounds().origin();
344     aura::Window::ConvertPointToTarget(w1->parent(), root_window, &left_top);
345     left_top.Offset(1, 1);
346     generator.MoveMouseTo(left_top);
347     generator.ClickLeftButton();
348     EXPECT_EQ(w11.get(), focus_client->GetFocusedWindow());
349     EXPECT_EQ(w1.get(), wm::GetActiveWindow());
350   }
351
352   // Clicking on a non-focusable window inside a background window should still
353   // give focus to the background window.
354   {
355     NonFocusableDelegate nfd;
356     scoped_ptr<aura::Window> w11(CreateTestWindowWithDelegate(
357           &nfd, -1, gfx::Rect(10, 10, 10, 10), w1.get()));
358     // Move focus to |w2| first.
359     scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate(
360           &wd, -1, gfx::Rect(70, 70, 50, 50)));
361     ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(), w2.get());
362     generator.ClickLeftButton();
363     EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
364     EXPECT_FALSE(w11->CanFocus());
365
366     // Click on |w11|. This should focus w1.
367     generator.MoveMouseToCenterOf(w11.get());
368     generator.ClickLeftButton();
369     EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
370   }
371 }
372
373 TEST_F(WindowManagerTest, PanelActivation) {
374   aura::test::TestWindowDelegate wd;
375   scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate(
376       &wd, -1, gfx::Rect(10, 10, 50, 50)));
377   aura::test::TestWindowDelegate pd;
378   scoped_ptr<aura::Window> p1(CreateTestWindowInShellWithDelegateAndType(
379       &pd, ui::wm::WINDOW_TYPE_PANEL, -1, gfx::Rect(10, 10, 50, 50)));
380   aura::client::FocusClient* focus_client =
381       aura::client::GetFocusClient(w1.get());
382
383   // Activate w1.
384   wm::ActivateWindow(w1.get());
385   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
386
387   // Activate p1.
388   wm::ActivateWindow(p1.get());
389   EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
390   EXPECT_EQ(p1.get(), focus_client->GetFocusedWindow());
391
392   // Activate w1.
393   wm::ActivateWindow(w1.get());
394   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
395   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
396
397   // Clicking on a non-activatable window should not change the active window.
398   {
399     NonFocusableDelegate nfd;
400     scoped_ptr<aura::Window> w3(CreateTestWindowInShellWithDelegate(
401           &nfd, -1, gfx::Rect(70, 70, 50, 50)));
402     ui::test::EventGenerator generator3(Shell::GetPrimaryRootWindow(),
403                                         w3.get());
404     wm::ActivateWindow(p1.get());
405     EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
406     generator3.ClickLeftButton();
407     EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
408   }
409 }
410
411 // Essentially the same as ActivateOnMouse, but for touch events.
412 TEST_F(WindowManagerTest, ActivateOnTouch) {
413   aura::Window* root_window = Shell::GetPrimaryRootWindow();
414
415   test::TestActivationDelegate d1;
416   aura::test::TestWindowDelegate wd;
417   scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate(
418       &wd, -1, gfx::Rect(10, 10, 50, 50)));
419   d1.SetWindow(w1.get());
420   test::TestActivationDelegate d2;
421   scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate(
422       &wd, -2, gfx::Rect(70, 70, 50, 50)));
423   d2.SetWindow(w2.get());
424
425   aura::client::FocusClient* focus_client =
426       aura::client::GetFocusClient(w1.get());
427
428   d1.Clear();
429   d2.Clear();
430
431   // Activate window1.
432   wm::ActivateWindow(w1.get());
433   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
434   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
435   EXPECT_EQ(1, d1.activated_count());
436   EXPECT_EQ(0, d1.lost_active_count());
437   d1.Clear();
438
439   // Touch window2.
440   gfx::Point press_point = w2->bounds().CenterPoint();
441   aura::Window::ConvertPointToTarget(w2->parent(), root_window, &press_point);
442   ui::TouchEvent touchev1(ui::ET_TOUCH_PRESSED, press_point, 0, getTime());
443
444   ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor();
445   ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&touchev1);
446   ASSERT_FALSE(details.dispatcher_destroyed);
447
448   // Window2 should have become active.
449   EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
450   EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
451   EXPECT_EQ(0, d1.activated_count());
452   EXPECT_EQ(1, d1.lost_active_count());
453   EXPECT_EQ(1, d2.activated_count());
454   EXPECT_EQ(0, d2.lost_active_count());
455   d1.Clear();
456   d2.Clear();
457
458   // Touch window1, but set it up so w1 doesn't activate on touch.
459   press_point = w1->bounds().CenterPoint();
460   aura::Window::ConvertPointToTarget(w1->parent(), root_window, &press_point);
461   d1.set_activate(false);
462   ui::TouchEvent touchev2(ui::ET_TOUCH_PRESSED, press_point, 1, getTime());
463   details = dispatcher->OnEventFromSource(&touchev2);
464   ASSERT_FALSE(details.dispatcher_destroyed);
465
466   // Window2 should still be active and focused.
467   EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
468   EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
469   EXPECT_EQ(0, d1.activated_count());
470   EXPECT_EQ(0, d1.lost_active_count());
471   EXPECT_EQ(0, d2.activated_count());
472   EXPECT_EQ(0, d2.lost_active_count());
473   d1.Clear();
474   d2.Clear();
475
476   // Destroy window2, this should make window1 active.
477   d1.set_activate(true);
478   w2.reset();
479   EXPECT_EQ(0, d2.activated_count());
480   EXPECT_EQ(1, d2.lost_active_count());
481   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
482   EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
483   EXPECT_EQ(1, d1.activated_count());
484   EXPECT_EQ(0, d1.lost_active_count());
485 }
486
487 TEST_F(WindowManagerTest, MouseEventCursors) {
488   aura::Window* root_window = Shell::GetPrimaryRootWindow();
489
490   // Create a window.
491   const int kWindowLeft = 123;
492   const int kWindowTop = 45;
493   HitTestWindowDelegate window_delegate;
494   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
495     &window_delegate,
496     -1,
497     gfx::Rect(kWindowLeft, kWindowTop, 640, 480)));
498
499   // Create two mouse movement events we can switch between.
500   gfx::Point point1(kWindowLeft, kWindowTop);
501   aura::Window::ConvertPointToTarget(window->parent(), root_window, &point1);
502
503   gfx::Point point2(kWindowLeft + 1, kWindowTop + 1);
504   aura::Window::ConvertPointToTarget(window->parent(), root_window, &point2);
505
506   aura::WindowTreeHost* host = root_window->GetHost();
507   ui::EventProcessor* dispatcher = host->event_processor();
508
509   // Cursor starts as a pointer (set during Shell::Init()).
510   EXPECT_EQ(ui::kCursorPointer, host->last_cursor().native_type());
511
512   {
513     // Resize edges and corners show proper cursors.
514     window_delegate.set_hittest_code(HTBOTTOM);
515     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
516     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
517     ASSERT_FALSE(details.dispatcher_destroyed);
518     EXPECT_EQ(ui::kCursorSouthResize, host->last_cursor().native_type());
519   }
520
521   {
522     window_delegate.set_hittest_code(HTBOTTOMLEFT);
523     ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
524     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
525     ASSERT_FALSE(details.dispatcher_destroyed);
526     EXPECT_EQ(ui::kCursorSouthWestResize, host->last_cursor().native_type());
527   }
528
529   {
530     window_delegate.set_hittest_code(HTBOTTOMRIGHT);
531     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
532     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
533     ASSERT_FALSE(details.dispatcher_destroyed);
534     EXPECT_EQ(ui::kCursorSouthEastResize, host->last_cursor().native_type());
535   }
536
537   {
538     window_delegate.set_hittest_code(HTLEFT);
539     ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
540     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
541     ASSERT_FALSE(details.dispatcher_destroyed);
542     EXPECT_EQ(ui::kCursorWestResize, host->last_cursor().native_type());
543   }
544
545   {
546     window_delegate.set_hittest_code(HTRIGHT);
547     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
548     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
549     ASSERT_FALSE(details.dispatcher_destroyed);
550     EXPECT_EQ(ui::kCursorEastResize, host->last_cursor().native_type());
551   }
552
553   {
554     window_delegate.set_hittest_code(HTTOP);
555     ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
556     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
557     ASSERT_FALSE(details.dispatcher_destroyed);
558     EXPECT_EQ(ui::kCursorNorthResize, host->last_cursor().native_type());
559   }
560
561   {
562     window_delegate.set_hittest_code(HTTOPLEFT);
563     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
564     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
565     ASSERT_FALSE(details.dispatcher_destroyed);
566     EXPECT_EQ(ui::kCursorNorthWestResize, host->last_cursor().native_type());
567   }
568
569   {
570     window_delegate.set_hittest_code(HTTOPRIGHT);
571     ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
572     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
573     ASSERT_FALSE(details.dispatcher_destroyed);
574     EXPECT_EQ(ui::kCursorNorthEastResize, host->last_cursor().native_type());
575   }
576
577   {
578     // Client area uses null cursor.
579     window_delegate.set_hittest_code(HTCLIENT);
580     ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
581     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
582     ASSERT_FALSE(details.dispatcher_destroyed);
583     EXPECT_EQ(ui::kCursorNull, host->last_cursor().native_type());
584   }
585 }
586
587 #if defined(OS_WIN)
588 #define MAYBE_TransformActivate DISABLED_TransformActivate
589 #else
590 #define MAYBE_TransformActivate TransformActivate
591 #endif
592 TEST_F(WindowManagerTest, MAYBE_TransformActivate) {
593   aura::Window* root_window = Shell::GetPrimaryRootWindow();
594   gfx::Size size = root_window->bounds().size();
595   EXPECT_EQ(gfx::Rect(size).ToString(),
596             Shell::GetScreen()->GetDisplayNearestPoint(
597                 gfx::Point()).bounds().ToString());
598
599   // Rotate it clock-wise 90 degrees.
600   gfx::Transform transform;
601   transform.Translate(size.width(), 0);
602   transform.Rotate(90.0f);
603   root_window->GetHost()->SetRootTransform(transform);
604
605   test::TestActivationDelegate d1;
606   aura::test::TestWindowDelegate wd;
607   scoped_ptr<aura::Window> w1(
608       CreateTestWindowInShellWithDelegate(&wd, 1, gfx::Rect(0, 15, 50, 50)));
609   d1.SetWindow(w1.get());
610   w1->Show();
611
612   gfx::Point miss_point(5, 5);
613   transform.TransformPoint(&miss_point);
614   ui::MouseEvent mouseev1(ui::ET_MOUSE_PRESSED,
615                           miss_point,
616                           miss_point,
617                           ui::EF_LEFT_MOUSE_BUTTON,
618                           ui::EF_LEFT_MOUSE_BUTTON);
619   ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor();
620   ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&mouseev1);
621   ASSERT_FALSE(details.dispatcher_destroyed);
622   EXPECT_EQ(NULL, aura::client::GetFocusClient(w1.get())->GetFocusedWindow());
623   ui::MouseEvent mouseup(ui::ET_MOUSE_RELEASED,
624                          miss_point,
625                          miss_point,
626                          ui::EF_LEFT_MOUSE_BUTTON,
627                          ui::EF_LEFT_MOUSE_BUTTON);
628   details = dispatcher->OnEventFromSource(&mouseup);
629   ASSERT_FALSE(details.dispatcher_destroyed);
630
631   gfx::Point hit_point(5, 15);
632   transform.TransformPoint(&hit_point);
633   ui::MouseEvent mouseev2(ui::ET_MOUSE_PRESSED,
634                           hit_point,
635                           hit_point,
636                           ui::EF_LEFT_MOUSE_BUTTON,
637                           ui::EF_LEFT_MOUSE_BUTTON);
638   details = dispatcher->OnEventFromSource(&mouseev2);
639   ASSERT_FALSE(details.dispatcher_destroyed);
640   EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
641   EXPECT_EQ(w1.get(),
642             aura::client::GetFocusClient(w1.get())->GetFocusedWindow());
643 }
644
645 TEST_F(WindowManagerTest, AdditionalFilters) {
646   // The IME event filter interferes with the basic key event propagation we
647   // attempt to do here, so we remove it.
648   test::ShellTestApi shell_test(Shell::GetInstance());
649   Shell::GetInstance()->RemovePreTargetHandler(
650       shell_test.input_method_event_filter());
651
652   aura::Window* root_window = Shell::GetPrimaryRootWindow();
653
654   // Creates a window and make it active
655   scoped_ptr<aura::Window> w1(CreateTestWindowInShell(
656       SK_ColorWHITE, -1, gfx::Rect(0, 0, 100, 100)));
657   wm::ActivateWindow(w1.get());
658
659   // Creates two addition filters
660   scoped_ptr<CustomEventHandler> f1(new CustomEventHandler);
661   scoped_ptr<CustomEventHandler> f2(new CustomEventHandler);
662
663   // Adds them to root window event filter.
664   ::wm::CompoundEventFilter* env_filter =
665       Shell::GetInstance()->env_filter();
666   env_filter->AddHandler(f1.get());
667   env_filter->AddHandler(f2.get());
668
669   // Dispatches mouse and keyboard events.
670   ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
671   ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor();
672   ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&key_event);
673   ASSERT_FALSE(details.dispatcher_destroyed);
674   ui::MouseEvent mouse_pressed(
675       ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
676   details = dispatcher->OnEventFromSource(&mouse_pressed);
677   ASSERT_FALSE(details.dispatcher_destroyed);
678
679   // Both filters should get the events.
680   EXPECT_EQ(1, f1->num_key_events());
681   EXPECT_EQ(1, f1->num_mouse_events());
682   EXPECT_EQ(1, f2->num_key_events());
683   EXPECT_EQ(1, f2->num_mouse_events());
684
685   f1->Reset();
686   f2->Reset();
687
688   // Makes f1 consume events.
689   f1->set_key_event_handling_result(ui::ER_CONSUMED);
690   f1->set_mouse_event_handling_result(ui::ER_CONSUMED);
691
692   // Dispatches events.
693   details = dispatcher->OnEventFromSource(&key_event);
694   ASSERT_FALSE(details.dispatcher_destroyed);
695   ui::MouseEvent mouse_released(
696       ui::ET_MOUSE_RELEASED, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
697   details = dispatcher->OnEventFromSource(&mouse_released);
698   ASSERT_FALSE(details.dispatcher_destroyed);
699
700   // f1 should still get the events but f2 no longer gets them.
701   EXPECT_EQ(1, f1->num_key_events());
702   EXPECT_EQ(1, f1->num_mouse_events());
703   EXPECT_EQ(0, f2->num_key_events());
704   EXPECT_EQ(0, f2->num_mouse_events());
705
706   f1->Reset();
707   f2->Reset();
708
709   // Remove f1 from additonal filters list.
710   env_filter->RemoveHandler(f1.get());
711
712   // Dispatches events.
713   details = dispatcher->OnEventFromSource(&key_event);
714   ASSERT_FALSE(details.dispatcher_destroyed);
715   details = dispatcher->OnEventFromSource(&mouse_pressed);
716   ASSERT_FALSE(details.dispatcher_destroyed);
717
718   // f1 should get no events since it's out and f2 should get them.
719   EXPECT_EQ(0, f1->num_key_events());
720   EXPECT_EQ(0, f1->num_mouse_events());
721   EXPECT_EQ(1, f2->num_key_events());
722   EXPECT_EQ(1, f2->num_mouse_events());
723
724   env_filter->RemoveHandler(f2.get());
725 }
726
727 #if defined(OS_CHROMEOS) || defined(OS_WIN)
728 // Touch visually hides the cursor on ChromeOS and Windows
729 TEST_F(WindowManagerTest, UpdateCursorVisibility) {
730   ui::test::EventGenerator& generator = GetEventGenerator();
731   ::wm::CursorManager* cursor_manager =
732       ash::Shell::GetInstance()->cursor_manager();
733
734   generator.MoveMouseTo(gfx::Point(0, 0));
735   EXPECT_TRUE(cursor_manager->IsCursorVisible());
736   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
737   generator.PressTouch();
738   EXPECT_FALSE(cursor_manager->IsCursorVisible());
739   EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
740   generator.MoveMouseTo(gfx::Point(0, 0));
741   EXPECT_TRUE(cursor_manager->IsCursorVisible());
742   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
743   generator.ReleaseTouch();
744   EXPECT_TRUE(cursor_manager->IsCursorVisible());
745   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
746 }
747 #endif  // defined(OS_CHROMEOS) || defined(OS_WIN)
748
749 #if defined(OS_CHROMEOS)
750 // ChromeOS is the only platform for which the cursor is hidden on keypress
751 // (crbug.com/304296).
752 TEST_F(WindowManagerTest, UpdateCursorVisibilityOnKeyEvent) {
753   ui::test::EventGenerator& generator = GetEventGenerator();
754   ::wm::CursorManager* cursor_manager =
755       ash::Shell::GetInstance()->cursor_manager();
756
757   // Pressing a key hides the cursor but does not disable mouse events.
758   generator.PressKey(ui::VKEY_A, ui::EF_NONE);
759   EXPECT_FALSE(cursor_manager->IsCursorVisible());
760   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
761   // Moving mouse shows the cursor.
762   generator.MoveMouseTo(gfx::Point(0, 0));
763   EXPECT_TRUE(cursor_manager->IsCursorVisible());
764   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
765   // Releasing a key also hides the cursor but does not disable mouse events.
766   generator.ReleaseKey(ui::VKEY_A, ui::EF_NONE);
767   EXPECT_FALSE(cursor_manager->IsCursorVisible());
768   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
769   // Moving mouse shows the cursor again.
770   generator.MoveMouseTo(gfx::Point(0, 0));
771   EXPECT_TRUE(cursor_manager->IsCursorVisible());
772   EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
773 }
774
775 TEST_F(WindowManagerTest, TestCursorClientObserver) {
776   ui::test::EventGenerator& generator = GetEventGenerator();
777   ::wm::CursorManager* cursor_manager =
778       ash::Shell::GetInstance()->cursor_manager();
779
780   scoped_ptr<aura::Window> w1(CreateTestWindowInShell(
781       SK_ColorWHITE, -1, gfx::Rect(0, 0, 100, 100)));
782   wm::ActivateWindow(w1.get());
783
784   // Add two observers. Both should have OnCursorVisibilityChanged()
785   // invoked when an event changes the visibility of the cursor.
786   TestingCursorClientObserver observer_a;
787   TestingCursorClientObserver observer_b;
788   cursor_manager->AddObserver(&observer_a);
789   cursor_manager->AddObserver(&observer_b);
790
791   // Initial state before any events have been sent.
792   observer_a.reset();
793   observer_b.reset();
794   EXPECT_FALSE(observer_a.did_visibility_change());
795   EXPECT_FALSE(observer_b.did_visibility_change());
796   EXPECT_FALSE(observer_a.is_cursor_visible());
797   EXPECT_FALSE(observer_b.is_cursor_visible());
798
799   // Keypress should hide the cursor.
800   generator.PressKey(ui::VKEY_A, ui::EF_NONE);
801   EXPECT_TRUE(observer_a.did_visibility_change());
802   EXPECT_TRUE(observer_b.did_visibility_change());
803   EXPECT_FALSE(observer_a.is_cursor_visible());
804   EXPECT_FALSE(observer_b.is_cursor_visible());
805
806   // Mouse move should show the cursor.
807   observer_a.reset();
808   observer_b.reset();
809   generator.MoveMouseTo(50, 50);
810   EXPECT_TRUE(observer_a.did_visibility_change());
811   EXPECT_TRUE(observer_b.did_visibility_change());
812   EXPECT_TRUE(observer_a.is_cursor_visible());
813   EXPECT_TRUE(observer_b.is_cursor_visible());
814
815   // Remove observer_b. Its OnCursorVisibilityChanged() should
816   // not be invoked past this point.
817   cursor_manager->RemoveObserver(&observer_b);
818
819   // Gesture tap should hide the cursor.
820   observer_a.reset();
821   observer_b.reset();
822   generator.GestureTapAt(gfx::Point(25, 25));
823   EXPECT_TRUE(observer_a.did_visibility_change());
824   EXPECT_FALSE(observer_b.did_visibility_change());
825   EXPECT_FALSE(observer_a.is_cursor_visible());
826
827   // Mouse move should show the cursor.
828   observer_a.reset();
829   observer_b.reset();
830   generator.MoveMouseTo(50, 50);
831   EXPECT_TRUE(observer_a.did_visibility_change());
832   EXPECT_FALSE(observer_b.did_visibility_change());
833   EXPECT_TRUE(observer_a.is_cursor_visible());
834 }
835 #endif  // defined(OS_CHROMEOS)
836
837 }  // namespace ash