1 // Copyright 2014 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.
5 #include "ui/chromeos/touch_exploration_controller.h"
7 #include "ash/accessibility_delegate.h"
9 #include "ash/test/ash_test_base.h"
10 #include "base/test/simple_test_tick_clock.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "content/public/test/browser_test_utils.h"
16 #include "ui/aura/client/cursor_client.h"
17 #include "ui/aura/window_tree_host.h"
18 #include "ui/compositor/compositor.h"
19 #include "ui/compositor/test/draw_waiter_for_test.h"
20 #include "ui/events/event.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"
27 class TouchExplorationTest
: public InProcessBrowserTest
{
29 TouchExplorationTest() : simulated_clock_(new base::SimpleTestTickClock()) {
30 // Tests fail if time is ever 0.
31 simulated_clock_
->Advance(base::TimeDelta::FromMilliseconds(10));
33 ~TouchExplorationTest() override
{}
36 void SetUpOnMainThread() override
{
37 // The RenderView for WebContents is created as a result of the
38 // navigation to the New Tab page which is done as part of the test
39 // SetUp. The creation involves sending a resize message to the renderer
40 // process. Here we wait for the resize ack to be received, because
41 // currently WindowEventDispatcher has code to hold touch and mouse
42 // move events until resize is complete (crbug.com/384342) which
43 // interferes with this test.
44 content::WebContents
* web_contents
=
45 browser()->tab_strip_model()->GetActiveWebContents();
46 content::WaitForResizeComplete(web_contents
);
47 root_window_
= ash::Shell::GetInstance()->GetPrimaryRootWindow();
48 event_handler_
.reset(new ui::test::TestEventHandler());
49 root_window_
->AddPreTargetHandler(event_handler_
.get());
52 void TearDownOnMainThread() override
{
53 SwitchTouchExplorationMode(false);
54 root_window_
->RemovePreTargetHandler(event_handler_
.get());
57 void SwitchTouchExplorationMode(bool on
) {
58 ash::AccessibilityDelegate
* ad
=
59 ash::Shell::GetInstance()->accessibility_delegate();
60 if (on
!= ad
->IsSpokenFeedbackEnabled())
61 ad
->ToggleSpokenFeedback(ui::A11Y_NOTIFICATION_NONE
);
64 base::TimeDelta
Now() {
65 return base::TimeDelta::FromInternalValue(
66 simulated_clock_
->NowTicks().ToInternalValue());
69 ui::GestureDetector::Config gesture_detector_config_
;
70 base::SimpleTestTickClock
* simulated_clock_
;
71 aura::Window
* root_window_
;
72 scoped_ptr
<ui::test::TestEventHandler
> event_handler_
;
75 DISALLOW_COPY_AND_ASSIGN(TouchExplorationTest
);
78 #if defined(OS_CHROMEOS)
80 #define MAYBE_NoRewritingEventsWhenOff DISABLED_NoRewritingEventsWhenOff
82 #define MAYBE_NoRewritingEventsWhenOff NoRewritingEventsWhenOff
85 // This test turns the touch exploration mode off and confirms that events
87 IN_PROC_BROWSER_TEST_F(TouchExplorationTest
, MAYBE_NoRewritingEventsWhenOff
) {
88 SwitchTouchExplorationMode(false);
89 ui::test::EventGenerator
generator(root_window_
);
91 base::TimeDelta initial_time
= Now();
92 ui::TouchEvent
initial_press(
93 ui::ET_TOUCH_PRESSED
, gfx::Point(99, 200), 1, initial_time
);
94 generator
.Dispatch(&initial_press
);
96 // Since the touch exploration controller doesn't know if the user is
97 // double-tapping or not, touch exploration is only initiated if the
98 // 300 ms has elapsed and the finger does not move fast enough to begin
99 // gestures. Here, the touch move event is not important as a move, but
100 // a way to create time advancement.
101 ui::TouchEvent
touch_time_advance(ui::ET_TOUCH_MOVED
,
102 gfx::Point(100, 200),
105 gesture_detector_config_
.double_tap_timeout
+
106 base::TimeDelta::FromMilliseconds(1));
107 generator
.Dispatch(&touch_time_advance
);
109 EXPECT_EQ(0, event_handler_
->num_mouse_events());
110 EXPECT_EQ(2, event_handler_
->num_touch_events());
111 event_handler_
->Reset();
113 generator
.MoveTouchId(gfx::Point(11, 12), 1);
114 EXPECT_EQ(0, event_handler_
->num_mouse_events());
115 EXPECT_EQ(1, event_handler_
->num_touch_events());
116 event_handler_
->Reset();
118 initial_time
= Now();
119 ui::TouchEvent
second_initial_press(
120 ui::ET_TOUCH_PRESSED
, gfx::Point(499, 600), 2, initial_time
);
121 generator
.Dispatch(&second_initial_press
);
122 ui::TouchEvent
second_touch_time_advance(
124 gfx::Point(500, 600),
126 initial_time
+ gesture_detector_config_
.double_tap_timeout
+
127 base::TimeDelta::FromMilliseconds(1));
128 generator
.Dispatch(&second_touch_time_advance
);
129 EXPECT_EQ(0, event_handler_
->num_mouse_events());
130 EXPECT_EQ(2, event_handler_
->num_touch_events());
133 // This test turns the touch exploration mode on and confirms that events get
135 IN_PROC_BROWSER_TEST_F(TouchExplorationTest
, RewritesEventsWhenOn
) {
136 SwitchTouchExplorationMode(true);
137 ui::test::EventGenerator
generator(root_window_
);
139 base::TimeDelta initial_time
= Now();
140 ui::TouchEvent
initial_press(
141 ui::ET_TOUCH_PRESSED
, gfx::Point(100, 200), 1, initial_time
);
142 generator
.Dispatch(&initial_press
);
144 // Since the touch exploration controller doesn't know if the user is
145 // double-tapping or not, touch exploration is only initiated if the
146 // 300 ms has elapsed and the finger does not move fast enough to begin
147 // gestures. Here, the touch move event is not important as a move, but
148 // a way to create time advancement.
149 ui::TouchEvent
touch_time_advance(ui::ET_TOUCH_MOVED
,
150 gfx::Point(100, 200),
153 gesture_detector_config_
.double_tap_timeout
+
154 base::TimeDelta::FromMilliseconds(1));
155 generator
.Dispatch(&touch_time_advance
);
157 // Number of mouse events may be greater than 1 because of ET_MOUSE_ENTERED.
158 EXPECT_GT(event_handler_
->num_mouse_events(), 0);
159 EXPECT_EQ(0, event_handler_
->num_touch_events());
160 event_handler_
->Reset();
162 initial_time
= Now();
163 ui::TouchEvent
second_initial_press(
164 ui::ET_TOUCH_PRESSED
, gfx::Point(500, 600), 2, initial_time
);
165 generator
.Dispatch(&second_initial_press
);
166 ui::TouchEvent
second_touch_time_advance(
168 gfx::Point(500, 600),
170 initial_time
+ gesture_detector_config_
.double_tap_timeout
+
171 base::TimeDelta::FromMilliseconds(1));
172 generator
.Dispatch(&second_touch_time_advance
);
173 EXPECT_GT(event_handler_
->num_mouse_events(), 0);
174 EXPECT_EQ(1, event_handler_
->num_touch_events());
175 event_handler_
->Reset();
177 // Stop the pending long press event. In some configurations, shutting down
178 // the browser can take longer than the long press timeout, and a long press
179 // event can come after the browser is already partly shut down, which causes
180 // the test to crash.
181 ui::TouchEvent
release_second_touch(
182 ui::ET_TOUCH_RELEASED
,
183 gfx::Point(500, 600),
185 initial_time
+ gesture_detector_config_
.double_tap_timeout
+
186 base::TimeDelta::FromMilliseconds(1));
187 generator
.Dispatch(&release_second_touch
);
188 EXPECT_GT(event_handler_
->num_mouse_events(), 0);
189 EXPECT_EQ(1, event_handler_
->num_touch_events());
192 // This test makes sure that after the user clicks with split tap,
193 // they continue to touch exploration mode if the original touch exploration
194 // finger is still on the screen.
195 IN_PROC_BROWSER_TEST_F(TouchExplorationTest
, SplitTapExplore
) {
196 SwitchTouchExplorationMode(true);
197 ui::test::EventGenerator
generator(root_window_
);
198 aura::client::CursorClient
* cursor_client
=
199 aura::client::GetCursorClient(root_window_
);
201 // Mouse events should show the cursor.
202 generator
.MoveMouseTo(gfx::Point(30, 31));
203 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
204 EXPECT_TRUE(cursor_client
->IsCursorVisible());
206 // The cursor should be shown immediately after the press, and hidden
208 base::TimeDelta initial_time
= Now();
209 ui::TouchEvent
initial_press(
210 ui::ET_TOUCH_PRESSED
, gfx::Point(100, 200), 1, initial_time
);
211 generator
.Dispatch(&initial_press
);
212 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
213 EXPECT_TRUE(cursor_client
->IsCursorVisible());
215 // Initiate touch explore by waiting for the tap timer timeout. Time is
216 // advanced by sending a move event after the timeout period.
217 ui::TouchEvent
touch_time_advance(
219 gfx::Point(100, 200),
221 initial_time
+ gesture_detector_config_
.double_tap_timeout
+
222 base::TimeDelta::FromMilliseconds(1));
223 generator
.Dispatch(&touch_time_advance
);
224 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
225 EXPECT_FALSE(cursor_client
->IsCursorVisible());
226 event_handler_
->Reset();
228 // Press and release with a second finger for split tap. This should send
229 // touch press and release events which should send a click press and release.
230 // Once the press is passed through, mouse events should be disabled.
231 // Mouse events are reenabled after the release.
232 generator
.set_current_location(gfx::Point(102, 202));
233 generator
.PressTouchId(2);
234 EXPECT_FALSE(cursor_client
->IsMouseEventsEnabled());
235 EXPECT_FALSE(cursor_client
->IsCursorVisible());
236 generator
.ReleaseTouchId(2);
237 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
238 EXPECT_FALSE(cursor_client
->IsCursorVisible());
239 EXPECT_EQ(2, event_handler_
->num_touch_events());
240 event_handler_
->Reset();
242 // Continuing to move the touch exploration finger should send more mouse
244 generator
.MoveTouchId(gfx::Point(509, 609), 1);
245 EXPECT_EQ(0, event_handler_
->num_touch_events());
246 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
247 EXPECT_FALSE(cursor_client
->IsCursorVisible());