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/strings/utf_string_conversions.h" 10#include "base/test/test_timeouts.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 29namespace content { 30 31class 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 virtual 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 selection_changed_wait_run_loop_->Quit(); 52 } 53 54 virtual 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 gesture_ack_wait_run_loop_->Quit(); 59 } 60 61 void WaitForSelectionChangeCallback() { 62 if (selection_changed_callback_arrived_) 63 return; 64 waiting_for_selection_changed_callback_ = true; 65 selection_changed_wait_run_loop_.reset(new base::RunLoop()); 66 selection_changed_wait_run_loop_->Run(); 67 } 68 69 void WaitForGestureAck() { 70 if (gesture_ack_callback_arrived_) 71 return; 72 waiting_for_gesture_ack_callback_ = true; 73 gesture_ack_wait_run_loop_.reset(new base::RunLoop()); 74 gesture_ack_wait_run_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 scoped_ptr<base::RunLoop> selection_changed_wait_run_loop_; 86 scoped_ptr<base::RunLoop> gesture_ack_wait_run_loop_; 87 88 DISALLOW_COPY_AND_ASSIGN(TestTouchEditableImplAura); 89}; 90 91class TouchEditableImplAuraTest : public ContentBrowserTest { 92 public: 93 TouchEditableImplAuraTest() {} 94 95 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 96 command_line->AppendSwitch(switches::kEnableTouchEditing); 97 } 98 99 // Executes the javascript synchronously and makes sure the returned value is 100 // freed properly. 101 void ExecuteSyncJSFunction(RenderViewHost* rvh, const std::string& jscript) { 102 scoped_ptr<base::Value> value = 103 content::ExecuteScriptAndGetValue(rvh, jscript); 104 } 105 106 // Starts the test server and navigates to the given url. Sets a large enough 107 // size to the root window. Returns after the navigation to the url is 108 // complete. 109 void StartTestWithPage(const std::string& url) { 110 ASSERT_TRUE(test_server()->Start()); 111 GURL test_url(test_server()->GetURL(url)); 112 NavigateToURL(shell(), test_url); 113 aura::Window* content = 114 shell()->web_contents()->GetView()->GetContentNativeView(); 115 content->GetRootWindow()->SetHostSize(gfx::Size(800, 600)); 116 } 117 118 void TestTouchSelectionOriginatingFromWebpage() { 119 ASSERT_NO_FATAL_FAILURE( 120 StartTestWithPage("files/touch_selection.html")); 121 WebContentsImpl* web_contents = 122 static_cast<WebContentsImpl*>(shell()->web_contents()); 123 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>( 124 web_contents->GetRenderViewHost()); 125 WebContentsViewAura* view_aura = static_cast<WebContentsViewAura*>( 126 web_contents->GetView()); 127 TestTouchEditableImplAura* touch_editable = new TestTouchEditableImplAura; 128 view_aura->SetTouchEditableForTest(touch_editable); 129 RenderWidgetHostViewAura* rwhva = static_cast<RenderWidgetHostViewAura*>( 130 web_contents->GetRenderWidgetHostView()); 131 aura::Window* content = web_contents->GetView()->GetContentNativeView(); 132 aura::test::EventGenerator generator(content->GetRootWindow(), content); 133 gfx::Rect bounds = content->GetBoundsInRootWindow(); 134 135 touch_editable->Reset(); 136 ExecuteSyncJSFunction(view_host, "select_all_text()"); 137 touch_editable->WaitForSelectionChangeCallback(); 138 139 // Tap inside selection to bring up selection handles. 140 generator.GestureTapAt(gfx::Point(bounds.x() + 10, bounds.y() + 10)); 141 EXPECT_EQ(touch_editable->rwhva_, rwhva); 142 143 scoped_ptr<base::Value> value = 144 content::ExecuteScriptAndGetValue(view_host, "get_selection()"); 145 std::string selection; 146 value->GetAsString(&selection); 147 148 // Check if selection handles are showing. 149 EXPECT_TRUE(touch_editable->touch_selection_controller_.get()); 150 EXPECT_STREQ("Some text we can select", selection.c_str()); 151 152 // Lets move the handles a bit to modify the selection 153 touch_editable->Reset(); 154 generator.GestureScrollSequence( 155 gfx::Point(10, 47), 156 gfx::Point(30, 47), 157 base::TimeDelta::FromMilliseconds(20), 158 1); 159 EXPECT_TRUE(touch_editable->touch_selection_controller_.get()); 160 value = content::ExecuteScriptAndGetValue(view_host, "get_selection()"); 161 value->GetAsString(&selection); 162 163 // It is hard to tell what exactly the selection would be now. But it would 164 // definitely be less than whatever was selected before. 165 EXPECT_GT(std::strlen("Some text we can select"), selection.size()); 166 } 167 168 void TestTouchSelectionOnLongPress() { 169 ASSERT_NO_FATAL_FAILURE( 170 StartTestWithPage("files/touch_selection.html")); 171 WebContentsImpl* web_contents = 172 static_cast<WebContentsImpl*>(shell()->web_contents()); 173 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>( 174 web_contents->GetRenderViewHost()); 175 WebContentsViewAura* view_aura = static_cast<WebContentsViewAura*>( 176 web_contents->GetView()); 177 TestTouchEditableImplAura* touch_editable = new TestTouchEditableImplAura; 178 view_aura->SetTouchEditableForTest(touch_editable); 179 RenderWidgetHostViewAura* rwhva = static_cast<RenderWidgetHostViewAura*>( 180 web_contents->GetRenderWidgetHostView()); 181 aura::Window* content = web_contents->GetView()->GetContentNativeView(); 182 aura::test::EventGenerator generator(content->GetRootWindow(), content); 183 gfx::Rect bounds = content->GetBoundsInRootWindow(); 184 EXPECT_EQ(touch_editable->rwhva_, rwhva); 185 186 // Long press to select word. 187 ui::GestureEvent long_press(ui::ET_GESTURE_LONG_PRESS, 188 10, 189 10, 190 0, 191 ui::EventTimeForNow(), 192 ui::GestureEventDetails( 193 ui::ET_GESTURE_LONG_PRESS, 0, 0), 194 1); 195 touch_editable->Reset(); 196 rwhva->OnGestureEvent(&long_press); 197 touch_editable->WaitForSelectionChangeCallback(); 198 199 // Check if selection handles are showing. 200 ui::TouchSelectionController* controller = 201 touch_editable->touch_selection_controller_.get(); 202 EXPECT_TRUE(controller); 203 204 scoped_ptr<base::Value> value = 205 content::ExecuteScriptAndGetValue(view_host, "get_selection()"); 206 std::string selection; 207 value->GetAsString(&selection); 208 EXPECT_STREQ("Some", selection.c_str()); 209 } 210 211 void TestTouchSelectionHiddenWhenScrolling() { 212 ASSERT_NO_FATAL_FAILURE( 213 StartTestWithPage("files/touch_selection.html")); 214 WebContentsImpl* web_contents = 215 static_cast<WebContentsImpl*>(shell()->web_contents()); 216 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>( 217 web_contents->GetRenderViewHost()); 218 WebContentsViewAura* view_aura = static_cast<WebContentsViewAura*>( 219 web_contents->GetView()); 220 TestTouchEditableImplAura* touch_editable = new TestTouchEditableImplAura; 221 view_aura->SetTouchEditableForTest(touch_editable); 222 RenderWidgetHostViewAura* rwhva = static_cast<RenderWidgetHostViewAura*>( 223 web_contents->GetRenderWidgetHostView()); 224 aura::Window* content = web_contents->GetView()->GetContentNativeView(); 225 aura::test::EventGenerator generator(content->GetRootWindow(), content); 226 gfx::Rect bounds = content->GetBoundsInRootWindow(); 227 EXPECT_EQ(touch_editable->rwhva_, rwhva); 228 229 // Long press to select word. 230 ui::GestureEvent long_press(ui::ET_GESTURE_LONG_PRESS, 231 10, 232 10, 233 0, 234 ui::EventTimeForNow(), 235 ui::GestureEventDetails( 236 ui::ET_GESTURE_LONG_PRESS, 0, 0), 237 1); 238 touch_editable->Reset(); 239 rwhva->OnGestureEvent(&long_press); 240 touch_editable->WaitForSelectionChangeCallback(); 241 242 // Check if selection handles are showing. 243 ui::TouchSelectionController* controller = 244 touch_editable->touch_selection_controller_.get(); 245 EXPECT_TRUE(controller); 246 247 scoped_ptr<base::Value> value = 248 content::ExecuteScriptAndGetValue(view_host, "get_selection()"); 249 std::string selection; 250 value->GetAsString(&selection); 251 EXPECT_STREQ("Some", selection.c_str()); 252 253 // Start scrolling. Handles should get hidden. 254 ui::GestureEvent scroll_begin(ui::ET_GESTURE_SCROLL_BEGIN, 255 10, 256 10, 257 0, 258 ui::EventTimeForNow(), 259 ui::GestureEventDetails( 260 ui::ET_GESTURE_LONG_PRESS, 0, 0), 261 1); 262 rwhva->OnGestureEvent(&scroll_begin); 263 EXPECT_FALSE(touch_editable->touch_selection_controller_.get()); 264 265 // Handles should come back after scroll ends. 266 ui::GestureEvent scroll_end(ui::ET_GESTURE_SCROLL_END, 267 10, 268 10, 269 0, 270 ui::EventTimeForNow(), 271 ui::GestureEventDetails( 272 ui::ET_GESTURE_LONG_PRESS, 0, 0), 273 1); 274 rwhva->OnGestureEvent(&scroll_end); 275 EXPECT_TRUE(touch_editable->touch_selection_controller_.get()); 276 } 277 278 void TestTouchCursorInTextfield() { 279 ASSERT_NO_FATAL_FAILURE( 280 StartTestWithPage("files/touch_selection.html")); 281 WebContentsImpl* web_contents = 282 static_cast<WebContentsImpl*>(shell()->web_contents()); 283 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>( 284 web_contents->GetRenderViewHost()); 285 WebContentsViewAura* view_aura = static_cast<WebContentsViewAura*>( 286 web_contents->GetView()); 287 TestTouchEditableImplAura* touch_editable = new TestTouchEditableImplAura; 288 view_aura->SetTouchEditableForTest(touch_editable); 289 RenderWidgetHostViewAura* rwhva = static_cast<RenderWidgetHostViewAura*>( 290 web_contents->GetRenderWidgetHostView()); 291 aura::Window* content = web_contents->GetView()->GetContentNativeView(); 292 aura::test::EventGenerator generator(content->GetRootWindow(), content); 293 gfx::Rect bounds = content->GetBoundsInRootWindow(); 294 EXPECT_EQ(touch_editable->rwhva_, rwhva); 295 ExecuteSyncJSFunction(view_host, "focus_textfield()"); 296 297 // Tap textfield 298 touch_editable->Reset(); 299 generator.GestureTapAt(gfx::Point(bounds.x() + 50, bounds.y() + 40)); 300 touch_editable->WaitForGestureAck(); // Wait for Tap Down Ack 301 touch_editable->Reset(); 302 touch_editable->WaitForGestureAck(); // Wait for Tap Ack. 303 304 // Check if cursor handle is showing. 305 ui::TouchSelectionController* controller = 306 touch_editable->touch_selection_controller_.get(); 307 EXPECT_NE(ui::TEXT_INPUT_TYPE_NONE, touch_editable->text_input_type_); 308 EXPECT_TRUE(controller); 309 310 scoped_ptr<base::Value> value = 311 content::ExecuteScriptAndGetValue(view_host, "get_cursor_position()"); 312 int cursor_pos = -1; 313 value->GetAsInteger(&cursor_pos); 314 EXPECT_NE(-1, cursor_pos); 315 316 // Move the cursor handle. 317 generator.GestureScrollSequence( 318 gfx::Point(50, 59), 319 gfx::Point(10, 59), 320 base::TimeDelta::FromMilliseconds(20), 321 1); 322 EXPECT_TRUE(touch_editable->touch_selection_controller_.get()); 323 value = content::ExecuteScriptAndGetValue( 324 view_host, "get_cursor_position()"); 325 int new_cursor_pos = -1; 326 value->GetAsInteger(&new_cursor_pos); 327 EXPECT_NE(-1, new_cursor_pos); 328 // Cursor should have moved. 329 EXPECT_NE(new_cursor_pos, cursor_pos); 330 } 331 332 private: 333 DISALLOW_COPY_AND_ASSIGN(TouchEditableImplAuraTest); 334}; 335 336IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest, 337 TouchSelectionOriginatingFromWebpageTest) { 338 TestTouchSelectionOriginatingFromWebpage(); 339} 340 341IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest, 342 TestTouchSelectionHiddenWhenScrolling) { 343 TestTouchSelectionHiddenWhenScrolling(); 344} 345 346IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest, 347 TouchSelectionOnLongPressTest) { 348 TestTouchSelectionOnLongPress(); 349} 350 351// TODO(miu): Disabled test due to flakiness. http://crbug.com/235991 352IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest, 353 DISABLED_TouchCursorInTextfieldTest) { 354 TestTouchCursorInTextfield(); 355} 356 357} // namespace content 358