Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "content/browser/web_contents/touch_editable_impl_aura.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/test/test_timeouts.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "base/values.h" | |
| 12 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 13 #include "content/browser/web_contents/web_contents_impl.h" | |
| 14 #include "content/browser/web_contents/web_contents_view_aura.h" | |
| 15 #include "content/public/browser/web_contents_view.h" | |
| 16 #include "content/public/common/content_switches.h" | |
| 17 #include "content/public/test/browser_test_utils.h" | |
| 18 #include "content/public/test/test_utils.h" | |
| 19 #include "content/shell/shell.h" | |
| 20 #include "content/test/content_browser_test.h" | |
| 21 #include "content/test/content_browser_test_utils.h" | |
| 22 #include "ui/aura/root_window.h" | |
| 23 #include "ui/aura/test/event_generator.h" | |
| 24 #include "ui/aura/window.h" | |
| 25 #include "ui/base/events/event_utils.h" | |
| 26 #include "ui/base/ui_base_switches.h" | |
| 27 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | |
| 28 | |
| 29 namespace content { | |
| 30 | |
| 31 class TestTouchEditableImplAura : public TouchEditableImplAura { | |
| 32 public: | |
| 33 TestTouchEditableImplAura() | |
| 34 : selection_changed_callback_arrived_(false), | |
| 35 waiting_for_selection_changed_callback_(false), | |
| 36 gesture_ack_callback_arrived_(false), | |
| 37 waiting_for_gesture_ack_callback_(false) {} | |
| 38 | |
| 39 void Reset() { | |
| 40 selection_changed_callback_arrived_ = false; | |
| 41 waiting_for_selection_changed_callback_ = false; | |
| 42 gesture_ack_callback_arrived_ = false; | |
| 43 waiting_for_gesture_ack_callback_ = false; | |
| 44 } | |
| 45 | |
| 46 void OnSelectionOrCursorChanged(const gfx::Rect& anchor, | |
| 47 const gfx::Rect& focus) OVERRIDE { | |
| 48 selection_changed_callback_arrived_ = true; | |
| 49 TouchEditableImplAura::OnSelectionOrCursorChanged(anchor, focus); | |
| 50 if (waiting_for_selection_changed_callback_) | |
| 51 MessageLoop::current()->Quit(); | |
| 52 } | |
| 53 | |
| 54 void GestureEventAck(int gesture_event_type) OVERRIDE { | |
| 55 gesture_ack_callback_arrived_ = true; | |
| 56 TouchEditableImplAura::GestureEventAck(gesture_event_type); | |
| 57 if (waiting_for_gesture_ack_callback_) | |
| 58 MessageLoop::current()->Quit(); | |
| 59 } | |
| 60 | |
| 61 void WaitForSelectionChangeCallback() { | |
| 62 if (selection_changed_callback_arrived_) | |
| 63 return; | |
| 64 MessageLoop* loop = MessageLoop::current(); | |
| 65 waiting_for_selection_changed_callback_ = true; | |
| 66 loop->Run(); | |
|
piman
2013/04/18 22:28:42
The recommended way nowadays is to use base::RunLo
varunjain
2013/04/19 05:14:12
Done.
| |
| 67 } | |
| 68 | |
| 69 void WaitForGestureAck() { | |
| 70 if (gesture_ack_callback_arrived_) | |
| 71 return; | |
| 72 MessageLoop* loop = MessageLoop::current(); | |
| 73 waiting_for_gesture_ack_callback_ = true; | |
| 74 loop->Run(); | |
| 75 } | |
| 76 | |
| 77 protected: | |
| 78 virtual ~TestTouchEditableImplAura() {} | |
| 79 | |
| 80 private: | |
| 81 bool selection_changed_callback_arrived_; | |
| 82 bool waiting_for_selection_changed_callback_; | |
| 83 bool gesture_ack_callback_arrived_; | |
| 84 bool waiting_for_gesture_ack_callback_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(TestTouchEditableImplAura); | |
| 87 }; | |
| 88 | |
| 89 class TouchEditableImplAuraTest : public ContentBrowserTest { | |
| 90 public: | |
| 91 TouchEditableImplAuraTest() {} | |
| 92 | |
| 93 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
| 94 command_line->AppendSwitch(switches::kEnableTouchEditing); | |
| 95 } | |
| 96 | |
| 97 // Executes the javascript synchronously and makes sure the returned value is | |
| 98 // freed properly. | |
| 99 void ExecuteSyncJSFunction(RenderViewHost* rvh, const std::string& jscript) { | |
| 100 scoped_ptr<base::Value> value = | |
| 101 content::ExecuteScriptAndGetValue(rvh, jscript); | |
| 102 } | |
| 103 | |
| 104 // Starts the test server and navigates to the given url. Sets a large enough | |
| 105 // size to the root window. Returns after the navigation to the url is | |
| 106 // complete. | |
| 107 void StartTestWithPage(const std::string& url) { | |
| 108 ASSERT_TRUE(test_server()->Start()); | |
| 109 GURL test_url(test_server()->GetURL(url)); | |
| 110 NavigateToURL(shell(), test_url); | |
| 111 aura::Window* content = | |
| 112 shell()->web_contents()->GetView()->GetContentNativeView(); | |
| 113 content->GetRootWindow()->SetHostSize(gfx::Size(800, 600)); | |
| 114 } | |
| 115 | |
| 116 void TestTouchSelectionOriginatingFromWebpage() { | |
| 117 ASSERT_NO_FATAL_FAILURE( | |
| 118 StartTestWithPage("files/touch_selection.html")); | |
| 119 WebContentsImpl* web_contents = | |
| 120 static_cast<WebContentsImpl*>(shell()->web_contents()); | |
| 121 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>( | |
| 122 web_contents->GetRenderViewHost()); | |
| 123 WebContentsViewAura* view_aura = static_cast<WebContentsViewAura*>( | |
| 124 web_contents->GetView()); | |
| 125 TestTouchEditableImplAura* touch_editable = new TestTouchEditableImplAura; | |
| 126 view_aura->SetTouchEditableForTest(touch_editable); | |
| 127 RenderWidgetHostViewAura* rwhva = static_cast<RenderWidgetHostViewAura*>( | |
| 128 web_contents->GetRenderWidgetHostView()); | |
| 129 aura::Window* content = web_contents->GetView()->GetContentNativeView(); | |
| 130 aura::test::EventGenerator generator(content->GetRootWindow(), content); | |
| 131 gfx::Rect bounds = content->GetBoundsInRootWindow(); | |
| 132 | |
| 133 touch_editable->Reset(); | |
| 134 ExecuteSyncJSFunction(view_host, "select_all_text()"); | |
| 135 touch_editable->WaitForSelectionChangeCallback(); | |
| 136 | |
| 137 // Tap inside selection to bring up selection handles. | |
| 138 generator.GestureTapAt(gfx::Point(bounds.x() + 10, bounds.y() + 10)); | |
| 139 EXPECT_EQ(touch_editable->rwhva_, rwhva); | |
| 140 | |
| 141 scoped_ptr<base::Value> value = | |
| 142 content::ExecuteScriptAndGetValue(view_host, "get_selection()"); | |
| 143 std::string selection; | |
| 144 value->GetAsString(&selection); | |
| 145 | |
| 146 // Check if selection handles are showing. | |
| 147 EXPECT_TRUE(touch_editable->touch_selection_controller_.get()); | |
| 148 EXPECT_STREQ("Some text we can select", selection.c_str()); | |
| 149 | |
| 150 // Lets move the handles a bit to modify the selection | |
| 151 touch_editable->Reset(); | |
| 152 generator.GestureScrollSequence( | |
| 153 gfx::Point(10, 37), | |
| 154 gfx::Point(30, 37), | |
| 155 base::TimeDelta::FromMilliseconds(20), | |
| 156 1); | |
| 157 EXPECT_TRUE(touch_editable->touch_selection_controller_.get()); | |
| 158 value = content::ExecuteScriptAndGetValue(view_host, "get_selection()"); | |
| 159 value->GetAsString(&selection); | |
| 160 | |
| 161 // It is hard to tell what exactly the selection would be now. But it would | |
| 162 // definitely be less than whatever was selected before. | |
| 163 EXPECT_GT(std::strlen("Some text we can select"), selection.size()); | |
| 164 } | |
| 165 | |
| 166 void TestTouchSelectionOnLongPress() { | |
| 167 ASSERT_NO_FATAL_FAILURE( | |
| 168 StartTestWithPage("files/touch_selection.html")); | |
| 169 WebContentsImpl* web_contents = | |
| 170 static_cast<WebContentsImpl*>(shell()->web_contents()); | |
| 171 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>( | |
| 172 web_contents->GetRenderViewHost()); | |
| 173 WebContentsViewAura* view_aura = static_cast<WebContentsViewAura*>( | |
| 174 web_contents->GetView()); | |
| 175 TestTouchEditableImplAura* touch_editable = new TestTouchEditableImplAura; | |
| 176 view_aura->SetTouchEditableForTest(touch_editable); | |
| 177 RenderWidgetHostViewAura* rwhva = static_cast<RenderWidgetHostViewAura*>( | |
| 178 web_contents->GetRenderWidgetHostView()); | |
| 179 aura::Window* content = web_contents->GetView()->GetContentNativeView(); | |
| 180 aura::test::EventGenerator generator(content->GetRootWindow(), content); | |
| 181 gfx::Rect bounds = content->GetBoundsInRootWindow(); | |
| 182 EXPECT_EQ(touch_editable->rwhva_, rwhva); | |
| 183 | |
| 184 // Long press to select word. | |
| 185 ui::GestureEvent long_press(ui::ET_GESTURE_LONG_PRESS, | |
| 186 10, | |
| 187 10, | |
| 188 0, | |
| 189 ui::EventTimeForNow(), | |
| 190 ui::GestureEventDetails( | |
| 191 ui::ET_GESTURE_LONG_PRESS, 0, 0), | |
| 192 1); | |
| 193 touch_editable->Reset(); | |
| 194 rwhva->OnGestureEvent(&long_press); | |
| 195 touch_editable->WaitForSelectionChangeCallback(); | |
| 196 | |
| 197 // Check if selection handles are showing. | |
| 198 ui::TouchSelectionController* controller = | |
| 199 touch_editable->touch_selection_controller_.get(); | |
| 200 EXPECT_TRUE(controller); | |
| 201 | |
| 202 scoped_ptr<base::Value> value = | |
| 203 content::ExecuteScriptAndGetValue(view_host, "get_selection()"); | |
| 204 std::string selection; | |
| 205 value->GetAsString(&selection); | |
| 206 EXPECT_STREQ("Some", selection.c_str()); | |
| 207 } | |
| 208 | |
| 209 void TestTouchCursorInTextfield() { | |
| 210 ASSERT_NO_FATAL_FAILURE( | |
| 211 StartTestWithPage("files/touch_selection.html")); | |
| 212 WebContentsImpl* web_contents = | |
| 213 static_cast<WebContentsImpl*>(shell()->web_contents()); | |
| 214 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>( | |
| 215 web_contents->GetRenderViewHost()); | |
| 216 WebContentsViewAura* view_aura = static_cast<WebContentsViewAura*>( | |
| 217 web_contents->GetView()); | |
| 218 TestTouchEditableImplAura* touch_editable = new TestTouchEditableImplAura; | |
| 219 view_aura->SetTouchEditableForTest(touch_editable); | |
| 220 RenderWidgetHostViewAura* rwhva = static_cast<RenderWidgetHostViewAura*>( | |
| 221 web_contents->GetRenderWidgetHostView()); | |
| 222 aura::Window* content = web_contents->GetView()->GetContentNativeView(); | |
| 223 aura::test::EventGenerator generator(content->GetRootWindow(), content); | |
| 224 gfx::Rect bounds = content->GetBoundsInRootWindow(); | |
| 225 EXPECT_EQ(touch_editable->rwhva_, rwhva); | |
| 226 ExecuteSyncJSFunction(view_host, "focus_textfield()"); | |
| 227 | |
| 228 // Tap textfield | |
| 229 touch_editable->Reset(); | |
| 230 generator.GestureTapAt(gfx::Point(bounds.x() + 50, bounds.y() + 40)); | |
| 231 touch_editable->WaitForGestureAck(); | |
| 232 | |
| 233 // Check if cursor handle is showing. | |
| 234 ui::TouchSelectionController* controller = | |
| 235 touch_editable->touch_selection_controller_.get(); | |
| 236 EXPECT_TRUE(controller); | |
| 237 | |
| 238 scoped_ptr<base::Value> value = | |
| 239 content::ExecuteScriptAndGetValue(view_host, "get_cursor_position()"); | |
| 240 int cursor_pos = -1; | |
| 241 value->GetAsInteger(&cursor_pos); | |
| 242 EXPECT_NE(-1, cursor_pos); | |
| 243 | |
| 244 // Move the cursor handle. | |
| 245 generator.GestureScrollSequence( | |
| 246 gfx::Point(50, 59), | |
| 247 gfx::Point(10, 59), | |
| 248 base::TimeDelta::FromMilliseconds(20), | |
| 249 1); | |
| 250 EXPECT_TRUE(touch_editable->touch_selection_controller_.get()); | |
| 251 value = content::ExecuteScriptAndGetValue( | |
| 252 view_host, "get_cursor_position()"); | |
| 253 int new_cursor_pos = -1; | |
| 254 value->GetAsInteger(&new_cursor_pos); | |
| 255 EXPECT_NE(-1, new_cursor_pos); | |
| 256 // Cursor should have moved. | |
| 257 EXPECT_NE(new_cursor_pos, cursor_pos); | |
| 258 } | |
| 259 | |
| 260 private: | |
| 261 DISALLOW_COPY_AND_ASSIGN(TouchEditableImplAuraTest); | |
| 262 }; | |
| 263 | |
| 264 IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest, | |
| 265 TouchSelectionOriginatingFromWebpageTest) { | |
| 266 TestTouchSelectionOriginatingFromWebpage(); | |
| 267 } | |
| 268 | |
| 269 IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest, | |
| 270 TouchSelectionOnLongPressTest) { | |
| 271 TestTouchSelectionOnLongPress(); | |
| 272 } | |
| 273 | |
| 274 IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest, | |
| 275 TouchCursorInTextfieldTest) { | |
| 276 TestTouchCursorInTextfield(); | |
| 277 } | |
| 278 | |
| 279 } // namespace content | |
| OLD | NEW |