- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / corewm / compound_event_filter_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 "ui/views/corewm/compound_event_filter.h"
6
7 #include "ui/aura/client/activation_client.h"
8 #include "ui/aura/client/cursor_client.h"
9 #include "ui/aura/env.h"
10 #include "ui/aura/root_window.h"
11 #include "ui/aura/test/aura_test_base.h"
12 #include "ui/aura/test/event_generator.h"
13 #include "ui/aura/test/test_cursor_client.h"
14 #include "ui/aura/test/test_windows.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_utils.h"
17
18 namespace {
19
20 base::TimeDelta GetTime() {
21   return ui::EventTimeForNow();
22 }
23
24 }
25
26 namespace views {
27 namespace corewm {
28
29 namespace {
30
31 // An event filter that consumes all gesture events.
32 class ConsumeGestureEventFilter : public ui::EventHandler {
33  public:
34   ConsumeGestureEventFilter() {}
35   virtual ~ConsumeGestureEventFilter() {}
36
37  private:
38   // Overridden from ui::EventHandler:
39   virtual void OnGestureEvent(ui::GestureEvent* e) OVERRIDE {
40     e->StopPropagation();
41   }
42
43   DISALLOW_COPY_AND_ASSIGN(ConsumeGestureEventFilter);
44 };
45
46 }  // namespace
47
48 typedef aura::test::AuraTestBase CompoundEventFilterTest;
49
50 #if defined(OS_CHROMEOS)
51 // A keypress only hides the cursor on ChromeOS (crbug.com/304296).
52 TEST_F(CompoundEventFilterTest, CursorVisibilityChange) {
53   scoped_ptr<CompoundEventFilter> compound_filter(new CompoundEventFilter);
54   aura::Env::GetInstance()->AddPreTargetHandler(compound_filter.get());
55   aura::test::TestWindowDelegate delegate;
56   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(&delegate, 1234,
57       gfx::Rect(5, 5, 100, 100), root_window()));
58   window->Show();
59   window->SetCapture();
60
61   aura::test::TestCursorClient cursor_client(root_window());
62
63   // Send key event to hide the cursor.
64   ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, true);
65   root_window()->AsRootWindowHostDelegate()->OnHostKeyEvent(&key);
66   EXPECT_FALSE(cursor_client.IsCursorVisible());
67
68   // Synthesized mouse event should not show the cursor.
69   ui::MouseEvent enter(ui::ET_MOUSE_ENTERED, gfx::Point(10, 10),
70                        gfx::Point(10, 10), 0);
71   enter.set_flags(enter.flags() | ui::EF_IS_SYNTHESIZED);
72   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&enter);
73   EXPECT_FALSE(cursor_client.IsCursorVisible());
74
75   ui::MouseEvent move(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
76                       gfx::Point(10, 10), 0);
77   move.set_flags(enter.flags() | ui::EF_IS_SYNTHESIZED);
78   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&move);
79   EXPECT_FALSE(cursor_client.IsCursorVisible());
80
81   ui::MouseEvent real_move(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
82                            gfx::Point(10, 10), 0);
83   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&real_move);
84   EXPECT_TRUE(cursor_client.IsCursorVisible());
85
86   // Send key event to hide the cursor again.
87   root_window()->AsRootWindowHostDelegate()->OnHostKeyEvent(&key);
88   EXPECT_FALSE(cursor_client.IsCursorVisible());
89
90   // Mouse synthesized exit event should not show the cursor.
91   ui::MouseEvent exit(ui::ET_MOUSE_EXITED, gfx::Point(10, 10),
92                       gfx::Point(10, 10), 0);
93   exit.set_flags(enter.flags() | ui::EF_IS_SYNTHESIZED);
94   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&exit);
95   EXPECT_FALSE(cursor_client.IsCursorVisible());
96 }
97
98 TEST_F(CompoundEventFilterTest, TouchHidesCursor) {
99   scoped_ptr<CompoundEventFilter> compound_filter(new CompoundEventFilter);
100   aura::Env::GetInstance()->AddPreTargetHandler(compound_filter.get());
101   aura::test::TestWindowDelegate delegate;
102   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(&delegate, 1234,
103       gfx::Rect(5, 5, 100, 100), root_window()));
104   window->Show();
105   window->SetCapture();
106
107   aura::test::TestCursorClient cursor_client(root_window());
108
109   ui::MouseEvent mouse0(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
110                         gfx::Point(10, 10), 0);
111   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&mouse0);
112   EXPECT_TRUE(cursor_client.IsMouseEventsEnabled());
113
114   // This press is required for the GestureRecognizer to associate a target
115   // with kTouchId
116   ui::TouchEvent press0(
117       ui::ET_TOUCH_PRESSED, gfx::Point(90, 90), 1, GetTime());
118   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press0);
119   EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
120
121   ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 10), 1, GetTime());
122   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&move);
123   EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
124
125   ui::TouchEvent release(
126       ui::ET_TOUCH_RELEASED, gfx::Point(10, 10), 1, GetTime());
127   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
128   EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
129
130   ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
131                         gfx::Point(10, 10), 0);
132   // Move the cursor again. The cursor should be visible.
133   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&mouse1);
134   EXPECT_TRUE(cursor_client.IsMouseEventsEnabled());
135
136   // Now activate the window and press on it again.
137   ui::TouchEvent press1(
138       ui::ET_TOUCH_PRESSED, gfx::Point(90, 90), 1, GetTime());
139   aura::client::GetActivationClient(
140       root_window())->ActivateWindow(window.get());
141   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press1);
142   EXPECT_FALSE(cursor_client.IsMouseEventsEnabled());
143   aura::Env::GetInstance()->RemovePreTargetHandler(compound_filter.get());
144 }
145 #endif
146
147 // Tests that if an event filter consumes a gesture, then it doesn't focus the
148 // window.
149 TEST_F(CompoundEventFilterTest, FilterConsumedGesture) {
150   scoped_ptr<CompoundEventFilter> compound_filter(new CompoundEventFilter);
151   scoped_ptr<ui::EventHandler> gesure_handler(new ConsumeGestureEventFilter);
152   compound_filter->AddHandler(gesure_handler.get());
153   aura::Env::GetInstance()->AddPreTargetHandler(compound_filter.get());
154   aura::test::TestWindowDelegate delegate;
155   DCHECK(root_window());
156   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(&delegate, 1234,
157       gfx::Rect(5, 5, 100, 100), root_window()));
158   window->Show();
159
160   EXPECT_TRUE(window->CanFocus());
161   EXPECT_FALSE(window->HasFocus());
162
163   // Tap on the window should not focus it since the filter will be consuming
164   // the gestures.
165   aura::test::EventGenerator generator(root_window(), gfx::Point(50, 50));
166   generator.PressTouch();
167   EXPECT_FALSE(window->HasFocus());
168
169   compound_filter->RemoveHandler(gesure_handler.get());
170   aura::Env::GetInstance()->AddPreTargetHandler(compound_filter.get());
171 }
172
173 // Verifies we don't attempt to hide the mouse when the mouse is down and a
174 // touch event comes in.
175 TEST_F(CompoundEventFilterTest, DontHideWhenMouseDown) {
176   aura::test::EventGenerator event_generator(root_window());
177
178   scoped_ptr<CompoundEventFilter> compound_filter(new CompoundEventFilter);
179   aura::Env::GetInstance()->AddPreTargetHandler(compound_filter.get());
180   aura::test::TestWindowDelegate delegate;
181   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(&delegate, 1234,
182       gfx::Rect(5, 5, 100, 100), root_window()));
183   window->Show();
184
185   aura::test::TestCursorClient cursor_client(root_window());
186
187   // Move and press the mouse over the window.
188   event_generator.MoveMouseTo(10, 10);
189   EXPECT_TRUE(cursor_client.IsMouseEventsEnabled());
190   event_generator.PressLeftButton();
191   EXPECT_TRUE(cursor_client.IsMouseEventsEnabled());
192   EXPECT_TRUE(aura::Env::GetInstance()->IsMouseButtonDown());
193
194   // Do a touch event. As the mouse button is down this should not disable mouse
195   // events.
196   event_generator.PressTouch();
197   EXPECT_TRUE(cursor_client.IsMouseEventsEnabled());
198   aura::Env::GetInstance()->RemovePreTargetHandler(compound_filter.get());
199 }
200
201 }  // namespace corewm
202 }  // namespace views