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"
8 #include "ash/ash_switches.h"
10 #include "ash/test/ash_test_base.h"
11 #include "base/command_line.h"
12 #include "base/test/simple_test_tick_clock.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "ui/aura/client/cursor_client.h"
20 #include "ui/aura/test/event_generator.h"
21 #include "ui/aura/window_tree_host.h"
22 #include "ui/compositor/compositor.h"
23 #include "ui/compositor/test/draw_waiter_for_test.h"
24 #include "ui/events/event.h"
25 #include "ui/events/event_utils.h"
26 #include "ui/events/test/test_event_handler.h"
30 class TouchExplorationTest
: public InProcessBrowserTest
{
32 TouchExplorationTest() : simulated_clock_(new base::SimpleTestTickClock()) {
33 // Tests fail if time is ever 0.
34 simulated_clock_
->Advance(base::TimeDelta::FromMilliseconds(10));
36 virtual ~TouchExplorationTest() {}
39 virtual void SetUpCommandLine(base::CommandLine
* command_line
) OVERRIDE
{
40 base::CommandLine::ForCurrentProcess()->AppendSwitch(
41 ash::switches::kAshEnableTouchExplorationMode
);
44 virtual void SetUpOnMainThread() OVERRIDE
{
45 // The RenderView for WebContents is created as a result of the
46 // navigation to the New Tab page which is done as part of the test
47 // SetUp. The creation involves sending a resize message to the renderer
48 // process. Here we wait for the resize ack to be received, because
49 // currently WindowEventDispatcher has code to hold touch and mouse
50 // move events until resize is complete (crbug.com/384342) which
51 // interferes with this test.
52 content::WebContents
* web_contents
=
53 browser()->tab_strip_model()->GetActiveWebContents();
54 content::WaitForResizeComplete(web_contents
);
55 root_window_
= ash::Shell::GetInstance()->GetPrimaryRootWindow();
56 event_handler_
.reset(new ui::test::TestEventHandler());
57 root_window_
->AddPreTargetHandler(event_handler_
.get());
60 virtual void TearDownOnMainThread() OVERRIDE
{
61 SwitchTouchExplorationMode(false);
62 root_window_
->RemovePreTargetHandler(event_handler_
.get());
65 void SwitchTouchExplorationMode(bool on
) {
66 ash::AccessibilityDelegate
* ad
=
67 ash::Shell::GetInstance()->accessibility_delegate();
68 if (on
!= ad
->IsSpokenFeedbackEnabled())
69 ad
->ToggleSpokenFeedback(ash::A11Y_NOTIFICATION_NONE
);
72 base::TimeDelta
Now() {
73 return base::TimeDelta::FromInternalValue(
74 simulated_clock_
->NowTicks().ToInternalValue());
77 ui::GestureDetector::Config gesture_detector_config_
;
78 base::SimpleTestTickClock
* simulated_clock_
;
79 aura::Window
* root_window_
;
80 scoped_ptr
<ui::test::TestEventHandler
> event_handler_
;
83 DISALLOW_COPY_AND_ASSIGN(TouchExplorationTest
);
86 // This test turns the touch exploration mode on/off and confirms that events
87 // get rewritten when the touch exploration mode is on, and aren't affected
88 // after the touch exploration mode is turned off.
89 IN_PROC_BROWSER_TEST_F(TouchExplorationTest
, ToggleOnOff
) {
90 SwitchTouchExplorationMode(true);
91 aura::test::EventGenerator
generator(root_window_
);
93 base::TimeDelta initial_time
= Now();
94 ui::TouchEvent
initial_press(
95 ui::ET_TOUCH_PRESSED
, gfx::Point(100, 200), 1, initial_time
);
96 generator
.Dispatch(&initial_press
);
98 // Since the touch exploration controller doesn't know if the user is
99 // double-tapping or not, touch exploration is only initiated if the
100 // 300 ms has elapsed and the finger does not move fast enough to begin
101 // gestures. Here, the touch move event is not important as a move, but
102 // a way to create time advancement.
103 ui::TouchEvent
touch_time_advance(ui::ET_TOUCH_MOVED
,
104 gfx::Point(100, 200),
107 gesture_detector_config_
.double_tap_timeout
+
108 base::TimeDelta::FromMilliseconds(1));
109 generator
.Dispatch(&touch_time_advance
);
111 // Number of mouse events may be greater than 1 because of ET_MOUSE_ENTERED.
112 EXPECT_GT(event_handler_
->num_mouse_events(), 0);
113 EXPECT_EQ(0, event_handler_
->num_touch_events());
114 event_handler_
->Reset();
116 SwitchTouchExplorationMode(false);
117 generator
.MoveTouchId(gfx::Point(11, 12), 1);
118 EXPECT_EQ(0, event_handler_
->num_mouse_events());
119 EXPECT_EQ(1, event_handler_
->num_touch_events());
120 event_handler_
->Reset();
122 SwitchTouchExplorationMode(true);
123 initial_time
= Now();
124 ui::TouchEvent
second_initial_press(
125 ui::ET_TOUCH_PRESSED
, gfx::Point(500, 600), 2, initial_time
);
126 generator
.Dispatch(&second_initial_press
);
127 ui::TouchEvent
second_touch_time_advance(
129 gfx::Point(500, 600),
131 initial_time
+ gesture_detector_config_
.double_tap_timeout
+
132 base::TimeDelta::FromMilliseconds(1));
133 generator
.Dispatch(&second_touch_time_advance
);
134 EXPECT_GT(event_handler_
->num_mouse_events(), 0);
135 EXPECT_EQ(0, event_handler_
->num_touch_events());
138 // This test makes sure that after the user clicks with split tap,
139 // they continue to touch exploration mode if the original touch exploration
140 // finger is still on the screen.
141 IN_PROC_BROWSER_TEST_F(TouchExplorationTest
, SplitTapExplore
) {
142 SwitchTouchExplorationMode(true);
143 aura::test::EventGenerator
generator(root_window_
);
144 aura::client::CursorClient
* cursor_client
=
145 aura::client::GetCursorClient(root_window_
);
147 // Mouse events should show the cursor.
148 generator
.MoveMouseTo(gfx::Point(30, 31));
149 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
150 EXPECT_TRUE(cursor_client
->IsCursorVisible());
152 // The cursor should be shown immediately after the press, and hidden
154 base::TimeDelta initial_time
= Now();
155 ui::TouchEvent
initial_press(
156 ui::ET_TOUCH_PRESSED
, gfx::Point(100, 200), 1, initial_time
);
157 generator
.Dispatch(&initial_press
);
158 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
159 EXPECT_TRUE(cursor_client
->IsCursorVisible());
161 // Initiate touch explore by waiting for the tap timer timeout. Time is
162 // advanced by sending a move event after the timeout period.
163 ui::TouchEvent
touch_time_advance(
165 gfx::Point(100, 200),
167 initial_time
+ gesture_detector_config_
.double_tap_timeout
+
168 base::TimeDelta::FromMilliseconds(1));
169 generator
.Dispatch(&touch_time_advance
);
170 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
171 EXPECT_FALSE(cursor_client
->IsCursorVisible());
172 event_handler_
->Reset();
174 // Press and release with a second finger for split tap. This should send
175 // touch press and release events which should send a click press and release.
176 // Once the press is passed through, mouse events should be disabled.
177 // Mouse events are reenabled after the release.
178 generator
.set_current_location(gfx::Point(102, 202));
179 generator
.PressTouchId(2);
180 EXPECT_FALSE(cursor_client
->IsMouseEventsEnabled());
181 EXPECT_FALSE(cursor_client
->IsCursorVisible());
182 generator
.ReleaseTouchId(2);
183 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
184 EXPECT_FALSE(cursor_client
->IsCursorVisible());
185 EXPECT_EQ(2, event_handler_
->num_touch_events());
186 event_handler_
->Reset();
188 // Continuing to move the touch exploration finger should send more mouse
190 generator
.MoveTouchId(gfx::Point(509, 609), 1);
191 EXPECT_EQ(0, event_handler_
->num_touch_events());
192 EXPECT_TRUE(cursor_client
->IsMouseEventsEnabled());
193 EXPECT_FALSE(cursor_client
->IsCursorVisible());