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 // Disabling due to failing over 10% of the time. (crbug.com/469119)
136 IN_PROC_BROWSER_TEST_F(TouchExplorationTest
, DISABLED_RewritesEventsWhenOn
) {
137 SwitchTouchExplorationMode(true);
138 ui::test::EventGenerator
generator(root_window_
);
140 base::TimeDelta initial_time
= Now();
141 ui::TouchEvent
initial_press(
142 ui::ET_TOUCH_PRESSED
, gfx::Point(100, 200), 1, initial_time
);
143 generator
.Dispatch(&initial_press
);
145 // Since the touch exploration controller doesn't know if the user is
146 // double-tapping or not, touch exploration is only initiated if the
147 // 300 ms has elapsed and the finger does not move fast enough to begin
148 // gestures. Here, the touch move event is not important as a move, but
149 // a way to create time advancement.
150 ui::TouchEvent
touch_time_advance(ui::ET_TOUCH_MOVED
,
151 gfx::Point(100, 200),
154 gesture_detector_config_
.double_tap_timeout
+
155 base::TimeDelta::FromMilliseconds(1));
156 generator
.Dispatch(&touch_time_advance
);
158 // Number of mouse events may be greater than 1 because of ET_MOUSE_ENTERED.
159 EXPECT_GT(event_handler_
->num_mouse_events(), 0);
160 EXPECT_EQ(0, event_handler_
->num_touch_events());
161 event_handler_
->Reset();
163 initial_time
= Now();
164 ui::TouchEvent
second_initial_press(
165 ui::ET_TOUCH_PRESSED
, gfx::Point(500, 600), 2, initial_time
);
166 generator
.Dispatch(&second_initial_press
);
167 ui::TouchEvent
second_touch_time_advance(
169 gfx::Point(500, 600),
171 initial_time
+ gesture_detector_config_
.double_tap_timeout
+
172 base::TimeDelta::FromMilliseconds(1));
173 generator
.Dispatch(&second_touch_time_advance
);
174 EXPECT_GT(event_handler_
->num_mouse_events(), 0);
175 EXPECT_EQ(1, event_handler_
->num_touch_events());
176 event_handler_
->Reset();
178 // Stop the pending long press event. In some configurations, shutting down
179 // the browser can take longer than the long press timeout, and a long press
180 // event can come after the browser is already partly shut down, which causes
181 // the test to crash.
182 ui::TouchEvent
release_second_touch(
183 ui::ET_TOUCH_RELEASED
,
184 gfx::Point(500, 600),
186 initial_time
+ gesture_detector_config_
.double_tap_timeout
+
187 base::TimeDelta::FromMilliseconds(1));
188 generator
.Dispatch(&release_second_touch
);
189 EXPECT_GT(event_handler_
->num_mouse_events(), 0);
190 EXPECT_EQ(1, event_handler_
->num_touch_events());
193 // This test makes sure that after the user clicks with split tap,
194 // they continue to touch exploration mode if the original touch exploration
195 // finger is still on the screen.
196 // Disabled due to failing upwards of 50% of the time (crbug.com/475923)
197 IN_PROC_BROWSER_TEST_F(TouchExplorationTest
, DISABLED_SplitTapExplore
) {
198 SwitchTouchExplorationMode(true);
199 ui::test::EventGenerator
generator(root_window_
);
200 aura::client::CursorClient
* cursor_client
=
201 aura::client::GetCursorClient(root_window_
);
203 // Mouse events should show the cursor.
204 generator
.MoveMouseTo(gfx::Point(30, 31));
205 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
206 EXPECT_TRUE(cursor_client
->IsCursorVisible());
208 // The cursor should be shown immediately after the press, and hidden
210 base::TimeDelta initial_time
= Now();
211 ui::TouchEvent
initial_press(
212 ui::ET_TOUCH_PRESSED
, gfx::Point(100, 200), 1, initial_time
);
213 generator
.Dispatch(&initial_press
);
214 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
215 EXPECT_TRUE(cursor_client
->IsCursorVisible());
217 // Initiate touch explore by waiting for the tap timer timeout. Time is
218 // advanced by sending a move event after the timeout period.
219 ui::TouchEvent
touch_time_advance(
221 gfx::Point(100, 200),
223 initial_time
+ gesture_detector_config_
.double_tap_timeout
+
224 base::TimeDelta::FromMilliseconds(1));
225 generator
.Dispatch(&touch_time_advance
);
226 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
227 EXPECT_FALSE(cursor_client
->IsCursorVisible());
228 event_handler_
->Reset();
230 // Press and release with a second finger for split tap. This should send
231 // touch press and release events which should send a click press and release.
232 // Once the press is passed through, mouse events should be disabled.
233 // Mouse events are reenabled after the release.
234 generator
.set_current_location(gfx::Point(102, 202));
235 generator
.PressTouchId(2);
236 EXPECT_FALSE(cursor_client
->IsMouseEventsEnabled());
237 EXPECT_FALSE(cursor_client
->IsCursorVisible());
238 generator
.ReleaseTouchId(2);
239 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
240 EXPECT_FALSE(cursor_client
->IsCursorVisible());
241 EXPECT_EQ(2, event_handler_
->num_touch_events());
242 event_handler_
->Reset();
244 // Continuing to move the touch exploration finger should send more mouse
246 generator
.MoveTouchId(gfx::Point(509, 609), 1);
247 EXPECT_EQ(0, event_handler_
->num_touch_events());
248 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
249 EXPECT_FALSE(cursor_client
->IsCursorVisible());