touch_editable_impl_aura_browsertest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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
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  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  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, 37),
156        gfx::Point(30, 37),
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 TestTouchCursorInTextfield() {
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    ExecuteSyncJSFunction(view_host, "focus_textfield()");
229
230    // Tap textfield
231    touch_editable->Reset();
232    generator.GestureTapAt(gfx::Point(bounds.x() + 50, bounds.y() + 40));
233    touch_editable->WaitForGestureAck();  // Wait for Tap Down Ack
234    touch_editable->Reset();
235    touch_editable->WaitForGestureAck();  // Wait for Tap Ack.
236
237    // Check if cursor handle is showing.
238    ui::TouchSelectionController* controller =
239        touch_editable->touch_selection_controller_.get();
240    EXPECT_NE(ui::TEXT_INPUT_TYPE_NONE, touch_editable->text_input_type_);
241    EXPECT_TRUE(controller);
242
243    scoped_ptr<base::Value> value =
244        content::ExecuteScriptAndGetValue(view_host, "get_cursor_position()");
245    int cursor_pos = -1;
246    value->GetAsInteger(&cursor_pos);
247    EXPECT_NE(-1, cursor_pos);
248
249    // Move the cursor handle.
250    generator.GestureScrollSequence(
251        gfx::Point(50, 59),
252        gfx::Point(10, 59),
253        base::TimeDelta::FromMilliseconds(20),
254        1);
255    EXPECT_TRUE(touch_editable->touch_selection_controller_.get());
256    value = content::ExecuteScriptAndGetValue(
257        view_host, "get_cursor_position()");
258    int new_cursor_pos = -1;
259    value->GetAsInteger(&new_cursor_pos);
260    EXPECT_NE(-1, new_cursor_pos);
261    // Cursor should have moved.
262    EXPECT_NE(new_cursor_pos, cursor_pos);
263  }
264
265 private:
266  DISALLOW_COPY_AND_ASSIGN(TouchEditableImplAuraTest);
267};
268
269IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest,
270                       TouchSelectionOriginatingFromWebpageTest) {
271  TestTouchSelectionOriginatingFromWebpage();
272}
273
274IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest,
275                       TouchSelectionOnLongPressTest) {
276  TestTouchSelectionOnLongPress();
277}
278
279// TODO(miu): Disabled test due to flakiness.  http://crbug.com/235991
280IN_PROC_BROWSER_TEST_F(TouchEditableImplAuraTest,
281                       DISABLED_TouchCursorInTextfieldTest) {
282  TestTouchCursorInTextfield();
283}
284
285}  // namespace content
286