blob: cc1a6759dd21429d0380f1516509c786c393162f [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00008#include "ash/test/shell_test_api.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00009#include "ash/test/test_activation_delegate.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010#include "ash/wm/window_util.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010011#include "ui/aura/client/cursor_client_observer.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000012#include "ui/aura/client/focus_client.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#include "ui/aura/env.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "ui/aura/test/aura_test_base.h"
15#include "ui/aura/test/event_generator.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "ui/aura/test/test_window_delegate.h"
17#include "ui/aura/test/test_windows.h"
18#include "ui/base/cursor/cursor.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019#include "ui/base/hit_test.h"
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +010020#include "ui/events/event.h"
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000021#include "ui/events/event_processor.h"
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +010022#include "ui/events/event_utils.h"
Torne (Richard Coles)23730a62014-03-21 14:25:57 +000023#include "ui/events/test/test_event_handler.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000024#include "ui/gfx/screen.h"
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000025#include "ui/wm/core/compound_event_filter.h"
26#include "ui/wm/core/input_method_event_filter.h"
Ben Murdocheffb81e2014-03-31 11:51:25 +010027#include "ui/wm/public/activation_client.h"
28#include "ui/wm/public/activation_delegate.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029
30namespace {
31
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010032class 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
Torne (Richard Coles)58218062012-11-14 11:43:16 +000054base::TimeDelta getTime() {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000055 return ui::EventTimeForNow();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000056}
57
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000058// A slightly changed TestEventHandler which can be configured to return a
59// specified value for key/mouse event handling.
Torne (Richard Coles)23730a62014-03-21 14:25:57 +000060class CustomEventHandler : public ui::test::TestEventHandler {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000061 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 {
Torne (Richard Coles)23730a62014-03-21 14:25:57 +000079 ui::test::TestEventHandler::OnKeyEvent(event);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000080 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 {
Torne (Richard Coles)23730a62014-03-21 14:25:57 +000087 ui::test::TestEventHandler::OnMouseEvent(event);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000088 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
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000101} // namespace
102
103namespace ash {
104
105typedef test::AshTestBase WindowManagerTest;
106
107class 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
119class 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
138TEST_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.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000141 test::ShellTestApi shell_test(Shell::GetInstance());
142 Shell::GetInstance()->RemovePreTargetHandler(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000143 shell_test.input_method_event_filter());
144
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000145 aura::Window* root_window = Shell::GetPrimaryRootWindow();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000146 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?
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000151 scoped_ptr<aura::Window> w1(CreateTestWindowInShell(
152 SK_ColorWHITE, -1, gfx::Rect(10, 10, 500, 500)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000153 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 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
178 w121.get());
179 generator.ClickLeftButton();
180
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000181 aura::client::FocusClient* focus_client =
182 aura::client::GetFocusClient(w121.get());
183 EXPECT_EQ(w121.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000184
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000185 ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor();
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000186
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000187 // The key press should be sent to the focused sub-window.
188 ui::KeyEvent keyev(ui::ET_KEY_PRESSED, ui::VKEY_E, 0, false);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000189 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&keyev);
190 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000191 EXPECT_EQ(ui::VKEY_E, w121delegate->last_key_code());
192
193 // Touch on a sub-window (w122) to focus it.
194 gfx::Point click_point = w122->bounds().CenterPoint();
195 aura::Window::ConvertPointToTarget(w122->parent(), root_window, &click_point);
196 ui::TouchEvent touchev(ui::ET_TOUCH_PRESSED, click_point, 0, getTime());
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000197 details = dispatcher->OnEventFromSource(&touchev);
198 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000199 focus_client = aura::client::GetFocusClient(w122.get());
200 EXPECT_EQ(w122.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000201
202 // The key press should be sent to the focused sub-window.
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000203 details = dispatcher->OnEventFromSource(&keyev);
204 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000205 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();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000210 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());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000214
215 // Sets the focus back to w122.
216 w122->Show();
217 w122->Focus();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000218 EXPECT_EQ(w122.get(),
219 aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000220
221 // Removing the focused window from parent should set the focus to
222 // its parent if it's focusable.
223 w12->RemoveChild(w122.get());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000224 EXPECT_EQ(NULL, aura::client::GetFocusClient(w122.get()));
225 EXPECT_EQ(w12.get(),
226 aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000227
228 // Set the focus to w123, but make the w1 not activatable.
229 test::TestActivationDelegate activation_delegate(false);
230 w123->Focus();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000231 EXPECT_EQ(w123.get(),
232 aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000233 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();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000238 EXPECT_EQ(aura::client::GetFocusClient(w12.get()),
239 aura::client::GetFocusClient(w123.get()));
240 EXPECT_EQ(NULL, aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000241 details = dispatcher->OnEventFromSource(&keyev);
242 EXPECT_FALSE(keyev.handled() || details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000243
244 // Set the focus back to w123
245 aura::client::SetActivationDelegate(w1.get(), NULL);
246 w123->Show();
247 w123->Focus();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000248 EXPECT_EQ(w123.get(),
249 aura::client::GetFocusClient(w12.get())->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000250 aura::client::SetActivationDelegate(w1.get(), &activation_delegate);
251
252 // Removing the focused window will set the focus to NULL because
253 // parent window is not focusable.
254 w12->RemoveChild(w123.get());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000255 EXPECT_EQ(NULL, aura::client::GetFocusClient(w123.get()));
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000256 details = dispatcher->OnEventFromSource(&keyev);
257 EXPECT_FALSE(keyev.handled() || details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000258}
259
260// Various assertion testing for activating windows.
261TEST_F(WindowManagerTest, ActivateOnMouse) {
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000262 aura::Window* root_window = Shell::GetPrimaryRootWindow();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000263
264 test::TestActivationDelegate d1;
265 aura::test::TestWindowDelegate wd;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000266 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate(
267 &wd, -1, gfx::Rect(10, 10, 50, 50)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000268 d1.SetWindow(w1.get());
269 test::TestActivationDelegate d2;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000270 scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate(
271 &wd, -2, gfx::Rect(70, 70, 50, 50)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000272 d2.SetWindow(w2.get());
273
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000274 aura::client::FocusClient* focus_client =
275 aura::client::GetFocusClient(w1.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000276
277 d1.Clear();
278 d2.Clear();
279
280 // Activate window1.
281 wm::ActivateWindow(w1.get());
282 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000283 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000284 EXPECT_EQ(1, d1.activated_count());
285 EXPECT_EQ(0, d1.lost_active_count());
286 d1.Clear();
287
288 {
289 // Click on window2.
290 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
291 w2.get());
292 generator.ClickLeftButton();
293
294 // Window2 should have become active.
295 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000296 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000297 EXPECT_EQ(0, d1.activated_count());
298 EXPECT_EQ(1, d1.lost_active_count());
299 EXPECT_EQ(1, d2.activated_count());
300 EXPECT_EQ(0, d2.lost_active_count());
301 d1.Clear();
302 d2.Clear();
303 }
304
305 {
306 // Click back on window1, but set it up so w1 doesn't activate on click.
307 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
308 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()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000314 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000315 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());
Torne (Richard Coles)58537e22013-09-12 12:10:22 +0100327 EXPECT_EQ(1, d2.lost_active_count());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000328 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000329 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000330 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(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000337 &wd, -11, gfx::Rect(10, 10, 10, 10), w1.get()));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000338 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
339 w11.get());
340 // First set the focus to the child |w11|.
341 generator.ClickLeftButton();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000342 EXPECT_EQ(w11.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000343 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();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000351 EXPECT_EQ(w11.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000352 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.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000362 scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate(
363 &wd, -1, gfx::Rect(70, 70, 50, 50)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000364 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
365 w2.get());
366 generator.ClickLeftButton();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000367 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000368 EXPECT_FALSE(w11->CanFocus());
369
370 // Click on |w11|. This should focus w1.
371 generator.MoveMouseToCenterOf(w11.get());
372 generator.ClickLeftButton();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000373 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000374 }
375}
376
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100377TEST_F(WindowManagerTest, PanelActivation) {
378 aura::test::TestWindowDelegate wd;
379 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate(
380 &wd, -1, gfx::Rect(10, 10, 50, 50)));
381 aura::test::TestWindowDelegate pd;
382 scoped_ptr<aura::Window> p1(CreateTestWindowInShellWithDelegateAndType(
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000383 &pd, ui::wm::WINDOW_TYPE_PANEL, -1, gfx::Rect(10, 10, 50, 50)));
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100384 aura::client::FocusClient* focus_client =
385 aura::client::GetFocusClient(w1.get());
386
387 // Activate w1.
388 wm::ActivateWindow(w1.get());
389 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
390
391 // Activate p1.
392 wm::ActivateWindow(p1.get());
393 EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
394 EXPECT_EQ(p1.get(), focus_client->GetFocusedWindow());
395
396 // Activate w1.
397 wm::ActivateWindow(w1.get());
398 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
399 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
400
401 // Clicking on a non-activatable window should not change the active window.
402 {
403 NonFocusableDelegate nfd;
404 scoped_ptr<aura::Window> w3(CreateTestWindowInShellWithDelegate(
405 &nfd, -1, gfx::Rect(70, 70, 50, 50)));
406 aura::test::EventGenerator generator3(Shell::GetPrimaryRootWindow(),
407 w3.get());
408 wm::ActivateWindow(p1.get());
409 EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
410 generator3.ClickLeftButton();
411 EXPECT_TRUE(wm::IsActiveWindow(p1.get()));
412 }
413}
414
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000415// Essentially the same as ActivateOnMouse, but for touch events.
416TEST_F(WindowManagerTest, ActivateOnTouch) {
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000417 aura::Window* root_window = Shell::GetPrimaryRootWindow();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000418
419 test::TestActivationDelegate d1;
420 aura::test::TestWindowDelegate wd;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000421 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithDelegate(
422 &wd, -1, gfx::Rect(10, 10, 50, 50)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000423 d1.SetWindow(w1.get());
424 test::TestActivationDelegate d2;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000425 scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegate(
426 &wd, -2, gfx::Rect(70, 70, 50, 50)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000427 d2.SetWindow(w2.get());
428
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000429 aura::client::FocusClient* focus_client =
430 aura::client::GetFocusClient(w1.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000431
432 d1.Clear();
433 d2.Clear();
434
435 // Activate window1.
436 wm::ActivateWindow(w1.get());
437 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000438 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000439 EXPECT_EQ(1, d1.activated_count());
440 EXPECT_EQ(0, d1.lost_active_count());
441 d1.Clear();
442
443 // Touch window2.
444 gfx::Point press_point = w2->bounds().CenterPoint();
445 aura::Window::ConvertPointToTarget(w2->parent(), root_window, &press_point);
446 ui::TouchEvent touchev1(ui::ET_TOUCH_PRESSED, press_point, 0, getTime());
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000447
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000448 ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor();
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000449 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&touchev1);
450 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000451
452 // Window2 should have become active.
453 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000454 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000455 EXPECT_EQ(0, d1.activated_count());
456 EXPECT_EQ(1, d1.lost_active_count());
457 EXPECT_EQ(1, d2.activated_count());
458 EXPECT_EQ(0, d2.lost_active_count());
459 d1.Clear();
460 d2.Clear();
461
462 // Touch window1, but set it up so w1 doesn't activate on touch.
463 press_point = w1->bounds().CenterPoint();
464 aura::Window::ConvertPointToTarget(w1->parent(), root_window, &press_point);
465 d1.set_activate(false);
466 ui::TouchEvent touchev2(ui::ET_TOUCH_PRESSED, press_point, 1, getTime());
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000467 details = dispatcher->OnEventFromSource(&touchev2);
468 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000469
470 // Window2 should still be active and focused.
471 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000472 EXPECT_EQ(w2.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000473 EXPECT_EQ(0, d1.activated_count());
474 EXPECT_EQ(0, d1.lost_active_count());
475 EXPECT_EQ(0, d2.activated_count());
476 EXPECT_EQ(0, d2.lost_active_count());
477 d1.Clear();
478 d2.Clear();
479
480 // Destroy window2, this should make window1 active.
481 d1.set_activate(true);
482 w2.reset();
483 EXPECT_EQ(0, d2.activated_count());
Torne (Richard Coles)58537e22013-09-12 12:10:22 +0100484 EXPECT_EQ(1, d2.lost_active_count());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000485 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000486 EXPECT_EQ(w1.get(), focus_client->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000487 EXPECT_EQ(1, d1.activated_count());
488 EXPECT_EQ(0, d1.lost_active_count());
489}
490
491TEST_F(WindowManagerTest, MouseEventCursors) {
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000492 aura::Window* root_window = Shell::GetPrimaryRootWindow();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000493
494 // Create a window.
495 const int kWindowLeft = 123;
496 const int kWindowTop = 45;
497 HitTestWindowDelegate window_delegate;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000498 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000499 &window_delegate,
500 -1,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000501 gfx::Rect(kWindowLeft, kWindowTop, 640, 480)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000502
503 // Create two mouse movement events we can switch between.
504 gfx::Point point1(kWindowLeft, kWindowTop);
505 aura::Window::ConvertPointToTarget(window->parent(), root_window, &point1);
506
507 gfx::Point point2(kWindowLeft + 1, kWindowTop + 1);
508 aura::Window::ConvertPointToTarget(window->parent(), root_window, &point2);
509
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000510 aura::WindowTreeHost* host = root_window->GetHost();
511 ui::EventProcessor* dispatcher = host->event_processor();
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000512
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000513 // Cursor starts as a pointer (set during Shell::Init()).
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000514 EXPECT_EQ(ui::kCursorPointer, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000515
516 {
517 // Resize edges and corners show proper cursors.
518 window_delegate.set_hittest_code(HTBOTTOM);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000519 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
520 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
521 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000522 EXPECT_EQ(ui::kCursorSouthResize, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000523 }
524
525 {
526 window_delegate.set_hittest_code(HTBOTTOMLEFT);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000527 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
528 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
529 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000530 EXPECT_EQ(ui::kCursorSouthWestResize, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000531 }
532
533 {
534 window_delegate.set_hittest_code(HTBOTTOMRIGHT);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000535 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
536 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
537 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000538 EXPECT_EQ(ui::kCursorSouthEastResize, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000539 }
540
541 {
542 window_delegate.set_hittest_code(HTLEFT);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000543 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
544 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
545 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000546 EXPECT_EQ(ui::kCursorWestResize, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000547 }
548
549 {
550 window_delegate.set_hittest_code(HTRIGHT);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000551 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
552 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
553 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000554 EXPECT_EQ(ui::kCursorEastResize, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000555 }
556
557 {
558 window_delegate.set_hittest_code(HTTOP);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000559 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
560 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
561 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000562 EXPECT_EQ(ui::kCursorNorthResize, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000563 }
564
565 {
566 window_delegate.set_hittest_code(HTTOPLEFT);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000567 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
568 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
569 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000570 EXPECT_EQ(ui::kCursorNorthWestResize, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000571 }
572
573 {
574 window_delegate.set_hittest_code(HTTOPRIGHT);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000575 ui::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, point2, 0, 0);
576 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move2);
577 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000578 EXPECT_EQ(ui::kCursorNorthEastResize, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000579 }
580
581 {
582 // Client area uses null cursor.
583 window_delegate.set_hittest_code(HTCLIENT);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000584 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, point1, 0, 0);
585 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move1);
586 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000587 EXPECT_EQ(ui::kCursorNull, host->last_cursor().native_type());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000588 }
589}
590
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100591#if defined(OS_WIN)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000592#define MAYBE_TransformActivate DISABLED_TransformActivate
593#else
594#define MAYBE_TransformActivate TransformActivate
595#endif
596TEST_F(WindowManagerTest, MAYBE_TransformActivate) {
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000597 aura::Window* root_window = Shell::GetPrimaryRootWindow();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000598 gfx::Size size = root_window->bounds().size();
599 EXPECT_EQ(gfx::Rect(size).ToString(),
600 Shell::GetScreen()->GetDisplayNearestPoint(
601 gfx::Point()).bounds().ToString());
602
603 // Rotate it clock-wise 90 degrees.
604 gfx::Transform transform;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000605 transform.Translate(size.width(), 0);
606 transform.Rotate(90.0f);
Ben Murdocha02191e2014-04-16 11:17:03 +0100607 root_window->GetHost()->SetRootTransform(transform);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000608
609 test::TestActivationDelegate d1;
610 aura::test::TestWindowDelegate wd;
611 scoped_ptr<aura::Window> w1(
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000612 CreateTestWindowInShellWithDelegate(&wd, 1, gfx::Rect(0, 15, 50, 50)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000613 d1.SetWindow(w1.get());
614 w1->Show();
615
616 gfx::Point miss_point(5, 5);
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100617 transform.TransformPoint(&miss_point);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000618 ui::MouseEvent mouseev1(ui::ET_MOUSE_PRESSED,
619 miss_point,
620 miss_point,
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000621 ui::EF_LEFT_MOUSE_BUTTON,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000622 ui::EF_LEFT_MOUSE_BUTTON);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000623 ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor();
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000624 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&mouseev1);
625 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000626 EXPECT_EQ(NULL, aura::client::GetFocusClient(w1.get())->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000627 ui::MouseEvent mouseup(ui::ET_MOUSE_RELEASED,
628 miss_point,
629 miss_point,
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000630 ui::EF_LEFT_MOUSE_BUTTON,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000631 ui::EF_LEFT_MOUSE_BUTTON);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000632 details = dispatcher->OnEventFromSource(&mouseup);
633 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000634
635 gfx::Point hit_point(5, 15);
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100636 transform.TransformPoint(&hit_point);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000637 ui::MouseEvent mouseev2(ui::ET_MOUSE_PRESSED,
638 hit_point,
639 hit_point,
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000640 ui::EF_LEFT_MOUSE_BUTTON,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000641 ui::EF_LEFT_MOUSE_BUTTON);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000642 details = dispatcher->OnEventFromSource(&mouseev2);
643 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000644 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000645 EXPECT_EQ(w1.get(),
646 aura::client::GetFocusClient(w1.get())->GetFocusedWindow());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000647}
648
649TEST_F(WindowManagerTest, AdditionalFilters) {
650 // The IME event filter interferes with the basic key event propagation we
651 // attempt to do here, so we remove it.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000652 test::ShellTestApi shell_test(Shell::GetInstance());
653 Shell::GetInstance()->RemovePreTargetHandler(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000654 shell_test.input_method_event_filter());
655
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000656 aura::Window* root_window = Shell::GetPrimaryRootWindow();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000657
658 // Creates a window and make it active
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000659 scoped_ptr<aura::Window> w1(CreateTestWindowInShell(
660 SK_ColorWHITE, -1, gfx::Rect(0, 0, 100, 100)));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000661 wm::ActivateWindow(w1.get());
662
663 // Creates two addition filters
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000664 scoped_ptr<CustomEventHandler> f1(new CustomEventHandler);
665 scoped_ptr<CustomEventHandler> f2(new CustomEventHandler);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000666
667 // Adds them to root window event filter.
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000668 ::wm::CompoundEventFilter* env_filter =
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000669 Shell::GetInstance()->env_filter();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000670 env_filter->AddHandler(f1.get());
671 env_filter->AddHandler(f2.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000672
673 // Dispatches mouse and keyboard events.
674 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, false);
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000675 ui::EventProcessor* dispatcher = root_window->GetHost()->event_processor();
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000676 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&key_event);
677 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000678 ui::MouseEvent mouse_pressed(
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000679 ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
680 details = dispatcher->OnEventFromSource(&mouse_pressed);
681 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000682
683 // Both filters should get the events.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000684 EXPECT_EQ(1, f1->num_key_events());
685 EXPECT_EQ(1, f1->num_mouse_events());
686 EXPECT_EQ(1, f2->num_key_events());
687 EXPECT_EQ(1, f2->num_mouse_events());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000688
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000689 f1->Reset();
690 f2->Reset();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000691
692 // Makes f1 consume events.
693 f1->set_key_event_handling_result(ui::ER_CONSUMED);
694 f1->set_mouse_event_handling_result(ui::ER_CONSUMED);
695
696 // Dispatches events.
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000697 details = dispatcher->OnEventFromSource(&key_event);
698 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000699 ui::MouseEvent mouse_released(
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000700 ui::ET_MOUSE_RELEASED, gfx::Point(0, 0), gfx::Point(0, 0), 0, 0);
701 details = dispatcher->OnEventFromSource(&mouse_released);
702 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000703
704 // f1 should still get the events but f2 no longer gets them.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000705 EXPECT_EQ(1, f1->num_key_events());
706 EXPECT_EQ(1, f1->num_mouse_events());
707 EXPECT_EQ(0, f2->num_key_events());
708 EXPECT_EQ(0, f2->num_mouse_events());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000709
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000710 f1->Reset();
711 f2->Reset();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000712
713 // Remove f1 from additonal filters list.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000714 env_filter->RemoveHandler(f1.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000715
716 // Dispatches events.
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000717 details = dispatcher->OnEventFromSource(&key_event);
718 ASSERT_FALSE(details.dispatcher_destroyed);
719 details = dispatcher->OnEventFromSource(&mouse_pressed);
720 ASSERT_FALSE(details.dispatcher_destroyed);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000721
722 // f1 should get no events since it's out and f2 should get them.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000723 EXPECT_EQ(0, f1->num_key_events());
724 EXPECT_EQ(0, f1->num_mouse_events());
725 EXPECT_EQ(1, f2->num_key_events());
726 EXPECT_EQ(1, f2->num_mouse_events());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000727
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000728 env_filter->RemoveHandler(f2.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000729}
730
Ben Murdochc5cede92014-04-10 11:22:14 +0100731#if defined(OS_CHROMEOS) || defined(OS_WIN)
732// Touch visually hides the cursor on ChromeOS and Windows
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000733TEST_F(WindowManagerTest, UpdateCursorVisibility) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000734 aura::test::EventGenerator& generator = GetEventGenerator();
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000735 ::wm::CursorManager* cursor_manager =
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000736 ash::Shell::GetInstance()->cursor_manager();
737
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000738 generator.MoveMouseTo(gfx::Point(0, 0));
739 EXPECT_TRUE(cursor_manager->IsCursorVisible());
740 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
741 generator.PressTouch();
742 EXPECT_FALSE(cursor_manager->IsCursorVisible());
743 EXPECT_FALSE(cursor_manager->IsMouseEventsEnabled());
744 generator.MoveMouseTo(gfx::Point(0, 0));
745 EXPECT_TRUE(cursor_manager->IsCursorVisible());
746 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
747 generator.ReleaseTouch();
748 EXPECT_TRUE(cursor_manager->IsCursorVisible());
749 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000750}
Ben Murdochc5cede92014-04-10 11:22:14 +0100751#endif // defined(OS_CHROMEOS) || defined(OS_WIN)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000752
Ben Murdochc5cede92014-04-10 11:22:14 +0100753#if defined(OS_CHROMEOS)
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000754// ChromeOS is the only platform for which the cursor is hidden on keypress
755// (crbug.com/304296).
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000756TEST_F(WindowManagerTest, UpdateCursorVisibilityOnKeyEvent) {
757 aura::test::EventGenerator& generator = GetEventGenerator();
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000758 ::wm::CursorManager* cursor_manager =
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000759 ash::Shell::GetInstance()->cursor_manager();
760
761 // Pressing a key hides the cursor but does not disable mouse events.
762 generator.PressKey(ui::VKEY_A, ui::EF_NONE);
763 EXPECT_FALSE(cursor_manager->IsCursorVisible());
764 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
765 // Moving mouse shows the cursor.
766 generator.MoveMouseTo(gfx::Point(0, 0));
767 EXPECT_TRUE(cursor_manager->IsCursorVisible());
768 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
769 // Releasing a key also hides the cursor but does not disable mouse events.
770 generator.ReleaseKey(ui::VKEY_A, ui::EF_NONE);
771 EXPECT_FALSE(cursor_manager->IsCursorVisible());
772 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
773 // Moving mouse shows the cursor again.
774 generator.MoveMouseTo(gfx::Point(0, 0));
775 EXPECT_TRUE(cursor_manager->IsCursorVisible());
776 EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000777}
778
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100779TEST_F(WindowManagerTest, TestCursorClientObserver) {
780 aura::test::EventGenerator& generator = GetEventGenerator();
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000781 ::wm::CursorManager* cursor_manager =
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100782 ash::Shell::GetInstance()->cursor_manager();
783
784 scoped_ptr<aura::Window> w1(CreateTestWindowInShell(
785 SK_ColorWHITE, -1, gfx::Rect(0, 0, 100, 100)));
786 wm::ActivateWindow(w1.get());
787
788 // Add two observers. Both should have OnCursorVisibilityChanged()
789 // invoked when an event changes the visibility of the cursor.
790 TestingCursorClientObserver observer_a;
791 TestingCursorClientObserver observer_b;
792 cursor_manager->AddObserver(&observer_a);
793 cursor_manager->AddObserver(&observer_b);
794
795 // Initial state before any events have been sent.
796 observer_a.reset();
797 observer_b.reset();
798 EXPECT_FALSE(observer_a.did_visibility_change());
799 EXPECT_FALSE(observer_b.did_visibility_change());
800 EXPECT_FALSE(observer_a.is_cursor_visible());
801 EXPECT_FALSE(observer_b.is_cursor_visible());
802
803 // Keypress should hide the cursor.
804 generator.PressKey(ui::VKEY_A, ui::EF_NONE);
805 EXPECT_TRUE(observer_a.did_visibility_change());
806 EXPECT_TRUE(observer_b.did_visibility_change());
807 EXPECT_FALSE(observer_a.is_cursor_visible());
808 EXPECT_FALSE(observer_b.is_cursor_visible());
809
810 // Mouse move should show the cursor.
811 observer_a.reset();
812 observer_b.reset();
813 generator.MoveMouseTo(50, 50);
814 EXPECT_TRUE(observer_a.did_visibility_change());
815 EXPECT_TRUE(observer_b.did_visibility_change());
816 EXPECT_TRUE(observer_a.is_cursor_visible());
817 EXPECT_TRUE(observer_b.is_cursor_visible());
818
819 // Remove observer_b. Its OnCursorVisibilityChanged() should
820 // not be invoked past this point.
821 cursor_manager->RemoveObserver(&observer_b);
822
823 // Gesture tap should hide the cursor.
824 observer_a.reset();
825 observer_b.reset();
826 generator.GestureTapAt(gfx::Point(25, 25));
827 EXPECT_TRUE(observer_a.did_visibility_change());
828 EXPECT_FALSE(observer_b.did_visibility_change());
829 EXPECT_FALSE(observer_a.is_cursor_visible());
830
831 // Mouse move should show the cursor.
832 observer_a.reset();
833 observer_b.reset();
834 generator.MoveMouseTo(50, 50);
835 EXPECT_TRUE(observer_a.did_visibility_change());
836 EXPECT_FALSE(observer_b.did_visibility_change());
837 EXPECT_TRUE(observer_a.is_cursor_visible());
838}
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000839#endif // defined(OS_CHROMEOS)
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100840
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000841} // namespace ash