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