Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Side by Side Diff: chrome/browser/drag_and_drop_browsertest.cc

Issue 2478583005: Browser tests for starting a drag-and-drop out of an OOPIF. (Closed)
Patch Set: Rebasing... Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <memory> 5 #include <memory>
6 #include <string> 6 #include <string>
7 7
8 #include "base/callback.h"
8 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/sequenced_task_runner.h"
9 #include "base/strings/pattern.h" 12 #include "base/strings/pattern.h"
10 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
11 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
13 #include "content/browser/frame_host/frame_tree.h" 16 #include "base/threading/sequenced_task_runner_handle.h"
14 #include "content/browser/frame_host/frame_tree_node.h" 17 #include "chrome/browser/ui/browser.h"
15 #include "content/browser/frame_host/render_frame_host_impl.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "content/browser/web_contents/web_contents_impl.h" 19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/interactive_test_utils.h"
21 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/render_frame_host.h" 22 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/browser_test_utils.h" 24 #include "content/public/test/browser_test_utils.h"
20 #include "content/public/test/content_browser_test.h" 25 #include "content/public/test/content_browser_test.h"
21 #include "content/public/test/content_browser_test_utils.h" 26 #include "content/public/test/content_browser_test_utils.h"
27 #include "content/public/test/test_frame_navigation_observer.h"
22 #include "content/public/test/test_utils.h" 28 #include "content/public/test/test_utils.h"
23 #include "content/shell/browser/shell.h"
24 #include "net/base/escape.h" 29 #include "net/base/escape.h"
25 #include "net/dns/mock_host_resolver.h" 30 #include "net/dns/mock_host_resolver.h"
26 #include "net/test/embedded_test_server/embedded_test_server.h" 31 #include "net/test/embedded_test_server/embedded_test_server.h"
27 #include "testing/gmock/include/gmock/gmock.h" 32 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "ui/aura/client/drag_drop_client.h"
29 #include "ui/aura/client/drag_drop_delegate.h" 35 #include "ui/aura/client/drag_drop_delegate.h"
30 #include "ui/aura/client/screen_position_client.h" 36 #include "ui/aura/client/screen_position_client.h"
31 #include "ui/aura/window.h" 37 #include "ui/aura/window.h"
32 #include "ui/base/dragdrop/drag_drop_types.h" 38 #include "ui/base/dragdrop/drag_drop_types.h"
33 #include "ui/base/dragdrop/drop_target_event.h" 39 #include "ui/base/dragdrop/drop_target_event.h"
34 #include "ui/base/dragdrop/os_exchange_data.h" 40 #include "ui/base/dragdrop/os_exchange_data.h"
35 #include "ui/gfx/geometry/point.h" 41 #include "ui/gfx/geometry/point.h"
36 #include "ui/gfx/geometry/rect.h" 42 #include "ui/gfx/geometry/rect.h"
37 #include "url/gurl.h" 43 #include "url/gurl.h"
38 44
39 namespace content { 45 namespace chrome {
40 46
41 namespace { 47 namespace {
42 48
43 // TODO(lukasza): Support testing on non-Aura platforms (i.e. Android + Mac?). 49 // TODO(lukasza): Support testing on non-Aura platforms (i.e. Android + Mac?).
44 // 50 //
45 // Notes for the TODO above: 51 // Notes for the TODO above:
46 // 52 //
47 // - Why inject/simulate drag-and-drop events at the aura::Window* level. 53 // - Why inject/simulate drag-and-drop events at the aura::Window* level.
48 // 54 //
49 // - It seems better to inject into UI libraries to cover code *inside* these 55 // - It seems better to inject into UI libraries to cover code *inside* these
50 // libraries. This might complicate simulation a little bit (i.e. picking 56 // libraries. This might complicate simulation a little bit (i.e. picking
51 // the right aura::Window and/or aura::client::DragDropDelegate to target), 57 // the right aura::Window and/or aura::client::DragDropDelegate to target),
52 // but otherwise important bits of code wouldn't get test coverage (i.e. 58 // but otherwise important bits of code wouldn't get test coverage (i.e.
53 // directly injecting into RenderViewHost->DragTargetDragEnter seems wrong). 59 // directly injecting into RenderViewHost->DragTargetDragEnter seems wrong).
54 // 60 //
55 // - In theory, we could introduce WebContentsImpl::DragTargetDragEnter (to be 61 // - In theory, we could introduce WebContentsImpl::DragTargetDragEnter (to be
56 // used by all UI platforms - so reused by web_contents_view_android.cc, 62 // used by all UI platforms - so reused by web_contents_view_android.cc,
57 // web_contents_view_aura.cc, web_drag_dest_mac.mm), but it feels wrong - UI 63 // web_contents_view_aura.cc, web_drag_dest_mac.mm), but it feels wrong - UI
58 // libraries should already know which widget is the target of the event and 64 // libraries should already know which widget is the target of the event and
59 // so should be able to talk directly to the right widget (i.e. WebContents 65 // so should be able to talk directly to the right widget (i.e. WebContents
60 // should not be responsible for mapping coordinates to a widget - this is 66 // should not be responsible for mapping coordinates to a widget - this is
61 // the job of the UI library). 67 // the job of the UI library).
62 // 68 //
63 // - Unknowns: 69 // - Unknowns:
64 // 70 //
65 // - Will this work for WebView and Plugin testing. 71 // - Will this work for WebView and Plugin testing.
66 //
67 // - Will this work for simulating dragging from WebContents into outside of
68 // the browser (without leaving OS drag&drop machinery in a weird state).
69 72
70 // Test helper for simulating drag and drop happening in WebContents. 73 // Test helper for simulating drag and drop happening in WebContents.
71 class DragAndDropSimulator { 74 class DragAndDropSimulator {
72 public: 75 public:
73 explicit DragAndDropSimulator(WebContents* web_contents) 76 explicit DragAndDropSimulator(content::WebContents* web_contents)
74 : web_contents_(web_contents) {} 77 : web_contents_(web_contents) {}
75 78
76 // Simulates notification that |text| was dragged from outside of the browser, 79 // Simulates notification that |text| was dragged from outside of the browser,
77 // into the specified |location| inside |web_contents|. 80 // into the specified |location| inside |web_contents|.
78 // |location| is relative to |web_contents|. 81 // |location| is relative to |web_contents|.
79 // Returns true upon success. 82 // Returns true upon success.
80 bool SimulateDragEnter(gfx::Point location, const std::string& text) { 83 bool SimulateDragEnter(gfx::Point location, const std::string& text) {
81 ui::OSExchangeData data; 84 ui::OSExchangeData data;
82 data.SetString(base::UTF8ToUTF16(text)); 85 data.SetString(base::UTF8ToUTF16(text));
83 return SimulateDragEnter(location, data); 86 return SimulateDragEnter(location, data);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 *out_event_location = root_location; 153 *out_event_location = root_location;
151 } 154 }
152 155
153 // These are ui::DropTargetEvent::source_operations_ being sent when manually 156 // These are ui::DropTargetEvent::source_operations_ being sent when manually
154 // trying out drag&drop of an image file from Nemo (Ubuntu's file explorer) 157 // trying out drag&drop of an image file from Nemo (Ubuntu's file explorer)
155 // into a content_shell. 158 // into a content_shell.
156 static constexpr int kDefaultSourceOperations = ui::DragDropTypes::DRAG_MOVE | 159 static constexpr int kDefaultSourceOperations = ui::DragDropTypes::DRAG_MOVE |
157 ui::DragDropTypes::DRAG_COPY | 160 ui::DragDropTypes::DRAG_COPY |
158 ui::DragDropTypes::DRAG_LINK; 161 ui::DragDropTypes::DRAG_LINK;
159 162
160 WebContents* web_contents_; 163 content::WebContents* web_contents_;
161 std::unique_ptr<ui::DropTargetEvent> active_drag_event_; 164 std::unique_ptr<ui::DropTargetEvent> active_drag_event_;
162 165
163 DISALLOW_COPY_AND_ASSIGN(DragAndDropSimulator); 166 DISALLOW_COPY_AND_ASSIGN(DragAndDropSimulator);
164 }; 167 };
165 168
169 // Helper for waiting until a drag-and-drop starts (e.g. in response to a
170 // mouse-down + mouse-move simulated by the test).
171 class DragStartWaiter : public aura::client::DragDropClient {
172 public:
173 // Starts monitoring |web_contents| for a start of a drag-and-drop.
174 // While alive, prevents a real, OS-level drag-and-drop from starting
175 // for this particular |web_contents|.
176 explicit DragStartWaiter(content::WebContents* web_contents)
177 : web_contents_(web_contents),
178 message_loop_runner_(new content::MessageLoopRunner),
179 drag_started_(false) {
180 DCHECK(web_contents_);
181
182 // Intercept calls to the old DragDropClient.
183 gfx::NativeWindow root_window =
184 web_contents_->GetContentNativeView()->GetRootWindow();
185 old_client_ = aura::client::GetDragDropClient(root_window);
186 aura::client::SetDragDropClient(root_window, this);
187 }
188
189 ~DragStartWaiter() override {
190 // Restore the original DragDropClient.
191 gfx::NativeWindow root_window =
192 web_contents_->GetContentNativeView()->GetRootWindow();
193 aura::client::SetDragDropClient(root_window, old_client_);
194 }
195
196 // Waits until we almost report a drag-and-drop start to the OS
197 // (notifying the OS will be prevented to help the test continue
198 // without entering a nested message loop).
199 //
200 // Returns true if drag and drop has indeed started (and in this
201 // case populates |text|, |html| and other parameters with data
202 // that would have been passed to the OS).
203 bool WaitUntilDragStartIsIntercepted(
204 std::string* text,
205 std::string* html,
206 int* operation,
207 gfx::Point* location_inside_web_contents) {
208 message_loop_runner_->Run();
209
210 if (!drag_started_)
211 return false;
212
213 *text = text_;
214 *html = html_;
215 *operation = operation_;
216 *location_inside_web_contents = location_inside_web_contents_;
217 return true;
218 }
219
ncarter (slow) 2016/11/11 20:56:36 // aura::client::DragDropClient:
Łukasz Anforowicz 2016/11/14 17:53:22 Done.
220 int StartDragAndDrop(const ui::OSExchangeData& data,
221 aura::Window* root_window,
222 aura::Window* source_window,
223 const gfx::Point& screen_location,
224 int operation,
225 ui::DragDropTypes::DragEventSource source) override {
226 if (!drag_started_) {
227 drag_started_ = true;
228 message_loop_runner_->Quit();
229
230 base::string16 text;
231 if (data.GetString(&text))
232 text_ = base::UTF16ToUTF8(text);
233 else
234 text_ = "<no text>";
235
236 GURL base_url;
237 base::string16 html;
238 if (data.GetHtml(&html, &base_url))
239 html_ = base::UTF16ToUTF8(html);
240 else
241 html_ = "<no html>";
242
243 gfx::Rect bounds =
244 web_contents_->GetContentNativeView()->GetBoundsInScreen();
245 location_inside_web_contents_ =
246 screen_location - gfx::Vector2d(bounds.x(), bounds.y());
247
248 operation_ = operation;
249 }
250
251 // Forwarding to |old_client_| is undesirable, because test cannot control
252 // next steps after a nested drag-and-drop loop is entered at the OS level
253 // (as is the case in Windows, via DoDragDrop). Instead, in the test we
254 // kind of ignore renderer's request to start drag and drop and never
255 // forward this request to the OS-specific layers.
256 return 0;
257 }
258
259 void DragCancel() override {
260 ADD_FAILURE() << "Unexpected call to DragCancel";
261 }
262
263 bool IsDragDropInProgress() override { return drag_started_; }
264
265 private:
266 content::WebContents* web_contents_;
267 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
268 aura::client::DragDropClient* old_client_;
269
270 // Data captured during the first intercepted StartDragAndDrop call.
271 bool drag_started_;
272 std::string text_;
273 std::string html_;
274 int operation_;
275 gfx::Point location_inside_web_contents_;
276 };
277
166 // Helper for waiting for notifications from 278 // Helper for waiting for notifications from
167 // content/test/data/drag_and_drop/event_monitoring.js 279 // content/test/data/drag_and_drop/event_monitoring.js
168 class DOMDragEventWaiter { 280 class DOMDragEventWaiter {
169 public: 281 public:
170 explicit DOMDragEventWaiter(const std::string& event_type_to_wait_for, 282 explicit DOMDragEventWaiter(const std::string& event_type_to_wait_for,
171 const ToRenderFrameHost& target) 283 const content::ToRenderFrameHost& target)
172 : target_frame_name_(target.render_frame_host()->GetFrameName()), 284 : target_frame_name_(target.render_frame_host()->GetFrameName()),
173 event_type_to_wait_for_(event_type_to_wait_for), 285 event_type_to_wait_for_(event_type_to_wait_for),
174 dom_message_queue_( 286 dom_message_queue_(content::WebContents::FromRenderFrameHost(
175 WebContents::FromRenderFrameHost(target.render_frame_host())) {} 287 target.render_frame_host())) {}
176 288
177 // Waits until |target| calls reportDragEvent in 289 // Waits until |target| calls reportDragEvent in
178 // content/test/data/drag_and_drop/event_monitoring.js with event_type 290 // chrome/test/data/drag_and_drop/event_monitoring.js with event_type
179 // property set to |event_type_to_wait_for|. (|target| and 291 // property set to |event_type_to_wait_for|. (|target| and
180 // |event_type_to_wait_for| are passed to the constructor). 292 // |event_type_to_wait_for| are passed to the constructor).
181 // 293 //
182 // Returns the event details via |found_event| (in form of a JSON-encoded 294 // Returns the event details via |found_event| (in form of a JSON-encoded
183 // object). See content/test/data/drag_and_drop/event_monitoring.js for keys 295 // object). See chrome/test/data/drag_and_drop/event_monitoring.js for keys
184 // and properties that |found_event| is expected to have. 296 // and properties that |found_event| is expected to have.
185 // 297 //
186 // Returns true upon success. It is okay if |response| is null. 298 // Returns true upon success. It is okay if |response| is null.
187 bool WaitForNextMatchingEvent(std::string* found_event) WARN_UNUSED_RESULT { 299 bool WaitForNextMatchingEvent(std::string* found_event) WARN_UNUSED_RESULT {
188 std::string candidate_event; 300 std::string candidate_event;
189 bool got_right_event_type = false; 301 bool got_right_event_type = false;
190 bool got_right_window_name = false; 302 bool got_right_window_name = false;
191 do { 303 do {
192 if (!dom_message_queue_.WaitForMessage(&candidate_event)) 304 if (!dom_message_queue_.WaitForMessage(&candidate_event))
193 return false; 305 return false;
194 306
195 got_right_event_type = base::MatchPattern( 307 got_right_event_type = base::MatchPattern(
196 candidate_event, base::StringPrintf("*\"event_type\":\"%s\"*", 308 candidate_event, base::StringPrintf("*\"event_type\":\"%s\"*",
197 event_type_to_wait_for_.c_str())); 309 event_type_to_wait_for_.c_str()));
198 310
199 got_right_window_name = base::MatchPattern( 311 got_right_window_name = base::MatchPattern(
200 candidate_event, base::StringPrintf("*\"window_name\":\"%s\"*", 312 candidate_event, base::StringPrintf("*\"window_name\":\"%s\"*",
201 target_frame_name_.c_str())); 313 target_frame_name_.c_str()));
202 } while (!got_right_event_type || !got_right_window_name); 314 } while (!got_right_event_type || !got_right_window_name);
203 315
204 if (found_event) 316 if (found_event)
205 *found_event = candidate_event; 317 *found_event = candidate_event;
206 318
207 return true; 319 return true;
208 } 320 }
209 321
210 private: 322 private:
211 std::string target_frame_name_; 323 std::string target_frame_name_;
212 std::string event_type_to_wait_for_; 324 std::string event_type_to_wait_for_;
213 DOMMessageQueue dom_message_queue_; 325 content::DOMMessageQueue dom_message_queue_;
326 };
327
328 // Helper for verifying contents of DOM events associated with drag-and-drop.
329 class DOMDragEventVerifier {
330 public:
331 DOMDragEventVerifier(){};
332
333 void set_expected_drop_effect(const std::string& value) {
334 expected_drop_effect_ = value;
335 }
336
337 void set_expected_effect_allowed(const std::string& value) {
338 expected_effect_allowed_ = value;
339 }
340
341 void set_expected_mime_types(const std::string& value) {
342 expected_mime_types_ = value;
343 }
344
345 // Returns a matcher that will match a std::string (drag event data - e.g.
346 // one returned by DOMDragEventWaiter::WaitForNextMatchingEvent) if it matches
347 // the expectations of this DOMDragEventVerifier.
348 testing::Matcher<std::string> Matches() const {
349 return testing::AllOf(
350 FieldMatches("drop_effect", expected_drop_effect_),
351 FieldMatches("effect_allowed", expected_effect_allowed_),
352 FieldMatches("mime_types", expected_mime_types_));
353 }
354
355 private:
356 static testing::Matcher<std::string> FieldMatches(
357 const std::string& field_name,
358 const std::string& expected_value) {
359 if (expected_value == "<no expectation>")
360 return testing::A<std::string>();
361
362 return testing::HasSubstr(base::StringPrintf(
363 "\"%s\":\"%s\"", field_name.c_str(), expected_value.c_str()));
364 }
365
366 std::string expected_drop_effect_ = "<no expectation>";
367 std::string expected_effect_allowed_ = "<no expectation>";
368 std::string expected_mime_types_ = "<no expectation>";
214 }; 369 };
215 370
216 const char kTestPagePath[] = "/drag_and_drop/page.html"; 371 const char kTestPagePath[] = "/drag_and_drop/page.html";
217 372
218 } // namespace 373 } // namespace
219 374
220 class DragAndDropBrowserTest : public ContentBrowserTest { 375 class DragAndDropBrowserTest : public InProcessBrowserTest {
221 public: 376 public:
222 DragAndDropBrowserTest(){}; 377 DragAndDropBrowserTest(){};
223 378
224 protected: 379 protected:
225 void SetUpOnMainThread() override { 380 void SetUpOnMainThread() override {
226 host_resolver()->AddRule("*", "127.0.0.1"); 381 host_resolver()->AddRule("*", "127.0.0.1");
227 ASSERT_TRUE(embedded_test_server()->Start()); 382 ASSERT_TRUE(embedded_test_server()->Start());
228 content::SetupCrossSiteRedirector(embedded_test_server()); 383 content::SetupCrossSiteRedirector(embedded_test_server());
229 drag_simulator_.reset(new DragAndDropSimulator(shell()->web_contents())); 384 drag_simulator_.reset(new DragAndDropSimulator(web_contents()));
230 } 385 }
231 386
232 // Navigates to content/test/data/drag_and_drop/page.html, with page origin 387 content::RenderFrameHost* left_frame() {
233 // and frame contents being controlled by the parameters. 388 AssertTestPageIsLoaded();
234 bool NavigateToTestPage(const std::string& main_origin, 389 return GetFrameByName("left");
235 const std::string& left_frame, 390 }
236 const std::string& right_frame) {
237 GURL base_url = embedded_test_server()->GetURL(main_origin, kTestPagePath);
238 391
239 std::string left_arg; 392 content::RenderFrameHost* right_frame() {
240 if (!left_frame.empty()) { 393 AssertTestPageIsLoaded();
241 left_arg = net::EscapeQueryParamValue( 394 return GetFrameByName("right");
242 std::string("/cross-site/") + left_frame, true); 395 }
243 }
244 std::string right_arg;
245 if (!right_frame.empty()) {
246 right_arg = net::EscapeQueryParamValue(
247 std::string("/cross-site/") + right_frame, true);
248 }
249 std::string query = base::StringPrintf("left=%s&right=%s", left_arg.c_str(),
250 right_arg.c_str());
251 GURL::Replacements query_replacement;
252 query_replacement.SetQueryStr(query);
253 GURL target_url = base_url.ReplaceComponents(query_replacement);
254 396
255 return NavigateToURL(shell(), target_url); 397 content::WebContents* web_contents() {
398 return browser()->tab_strip_model()->GetActiveWebContents();
256 } 399 }
257 400
401 //////////////////////
402 // Navigation helpers.
403
404 bool NavigateToTestPage(const std::string& origin_of_main_frame) {
405 GURL url =
406 embedded_test_server()->GetURL(origin_of_main_frame, kTestPagePath);
407 ui_test_utils::NavigateToURL(browser(), url);
408 return web_contents()->GetLastCommittedURL() == url;
409 }
410
411 // Navigates the left frame to |filename| (found under
412 // chrome/test/data/drag_and_drop directory).
413 bool NavigateLeftFrame(const std::string& origin,
414 const std::string& filename) {
415 AssertTestPageIsLoaded();
416 return NavigateNamedFrame("left", origin, filename);
417 }
418
419 // Navigates the right frame to |filename| (found under
420 // chrome/test/data/drag_and_drop directory).
421 bool NavigateRightFrame(const std::string& origin,
422 const std::string& filename) {
423 AssertTestPageIsLoaded();
424 return NavigateNamedFrame("right", origin, filename);
425 }
426
427 ////////////////////////////////////////////////////////////
428 // Simulation of starting a drag-and-drop (using the mouse).
429
430 bool SimulateMouseDownAndDragStartInLeftFrame() {
431 AssertTestPageIsLoaded();
432 if (!SimulateMouseMove(kMiddleOfLeftFrame) || !SimulateMouseDown() ||
433 !SimulateMouseMove(expected_location_of_drag_start_in_left_frame()))
434 return false;
435
436 return true;
437 }
438
439 gfx::Point expected_location_of_drag_start_in_left_frame() {
440 return kMiddleOfLeftFrame + gfx::Vector2d(5, 5);
441 }
442
443 bool SimulateMouseUp() {
444 return ui_test_utils::SendMouseEventsSync(ui_controls::LEFT,
445 ui_controls::UP);
446 }
447
448 ////////////////////////////////////////////////////////////////////
449 // Simulation of dragging from outside the browser into web contents
450 // (using DragAndDropSimulator, not simulating mouse events).
451
258 bool SimulateDragEnterToRightFrame(const std::string& text) { 452 bool SimulateDragEnterToRightFrame(const std::string& text) {
259 AssertTestPageIsLoaded(); 453 AssertTestPageIsLoaded();
260 return drag_simulator_->SimulateDragEnter(kMiddleOfRightFrame, text); 454 return drag_simulator_->SimulateDragEnter(kMiddleOfRightFrame, text);
261 } 455 }
262 456
263 bool SimulateDropInRightFrame() { 457 bool SimulateDropInRightFrame() {
264 AssertTestPageIsLoaded(); 458 AssertTestPageIsLoaded();
265 return drag_simulator_->SimulateDrop(kMiddleOfRightFrame); 459 return drag_simulator_->SimulateDrop(kMiddleOfRightFrame);
266 } 460 }
267 461
268 RenderFrameHost* right_frame() { 462 private:
269 AssertTestPageIsLoaded(); 463 bool SimulateMouseDown() {
270 return GetFrameByName("right"); 464 return ui_test_utils::SendMouseEventsSync(ui_controls::LEFT,
465 ui_controls::DOWN);
271 } 466 }
272 467
273 private: 468 bool SimulateMouseMove(const gfx::Point& location_inside_web_contents) {
274 RenderFrameHost* GetFrameByName(const std::string& name_to_find) { 469 gfx::Rect bounds = web_contents()->GetContainerBounds();
275 WebContentsImpl* web_contents = 470 return ui_test_utils::SendMouseMoveSync(
276 static_cast<WebContentsImpl*>(shell()->web_contents()); 471 gfx::Point(bounds.x() + location_inside_web_contents.x(),
277 FrameTree* frame_tree = web_contents->GetFrameTree(); 472 bounds.y() + location_inside_web_contents.y()));
278 return frame_tree->FindByName(name_to_find)->current_frame_host(); 473 }
474
475 bool NavigateNamedFrame(const std::string& frame_name,
476 const std::string& origin,
477 const std::string& filename) {
478 content::RenderFrameHost* frame = GetFrameByName(frame_name);
479 if (!frame)
480 return false;
481
482 std::string script;
483 int response = 0;
484
485 // Navigate the frame and wait for the load event.
486 script = base::StringPrintf(
487 "location.href = '/cross-site/%s/drag_and_drop/%s';\n"
488 "setTimeout(function() { domAutomationController.send(42); }, 0);",
489 origin.c_str(), filename.c_str());
490 content::TestFrameNavigationObserver observer(frame);
491 if (!content::ExecuteScriptAndExtractInt(frame, script, &response))
492 return false;
493 if (response != 42)
494 return false;
495 observer.Wait();
496
497 // Wait until frame contents (e.g. images) have painted (which should happen
498 // in the animation frame that *starts* after the onload event - therefore
499 // we need to wait for 2 animation frames).
500 script = std::string(
501 "requestAnimationFrame(function() {\n"
502 " requestAnimationFrame(function() {\n"
503 " domAutomationController.send(43);\n"
504 " });\n"
505 "});\n");
506 if (!content::ExecuteScriptAndExtractInt(frame, script, &response))
507 return false;
508 if (response != 43)
509 return false;
510
511 return true;
512 }
513
514 content::RenderFrameHost* GetFrameByName(const std::string& name_to_find) {
515 content::RenderFrameHost* result = nullptr;
516 for (content::RenderFrameHost* rfh : web_contents()->GetAllFrames()) {
517 if (rfh->GetFrameName() == name_to_find) {
518 if (result) {
519 ADD_FAILURE() << "More than one frame named "
520 << "'" << name_to_find << "'";
521 return nullptr;
522 }
523 result = rfh;
524 }
525 }
526
527 EXPECT_TRUE(result) << "Couldn't find a frame named "
528 << "'" << name_to_find << "'";
529 return result;
279 } 530 }
280 531
281 void AssertTestPageIsLoaded() { 532 void AssertTestPageIsLoaded() {
282 ASSERT_EQ(kTestPagePath, 533 ASSERT_EQ(kTestPagePath, web_contents()->GetLastCommittedURL().path());
283 shell()->web_contents()->GetLastCommittedURL().path());
284 } 534 }
285 535
286 std::unique_ptr<DragAndDropSimulator> drag_simulator_; 536 std::unique_ptr<DragAndDropSimulator> drag_simulator_;
287 537
288 // Constants with coordinates within content/test/data/drag_and_drop/page.html 538 // Constants with coordinates within content/test/data/drag_and_drop/page.html
289 const gfx::Point kMiddleOfLeftFrame = gfx::Point(200, 200); 539 const gfx::Point kMiddleOfLeftFrame = gfx::Point(200, 200);
290 const gfx::Point kMiddleOfRightFrame = gfx::Point(400, 200); 540 const gfx::Point kMiddleOfRightFrame = gfx::Point(400, 200);
291 541
292 DISALLOW_COPY_AND_ASSIGN(DragAndDropBrowserTest); 542 DISALLOW_COPY_AND_ASSIGN(DragAndDropBrowserTest);
293 }; 543 };
294 544
295 IN_PROC_BROWSER_TEST_F(DragAndDropBrowserTest, DropTextFromOutside) { 545 IN_PROC_BROWSER_TEST_F(DragAndDropBrowserTest, DropTextFromOutside) {
296 ASSERT_TRUE(NavigateToTestPage("a.com", 546 ASSERT_TRUE(NavigateToTestPage("a.com"));
297 "", // Left frame is unused by this test. 547 ASSERT_TRUE(NavigateRightFrame("b.com", "drop_target.html"));
298 "c.com/drag_and_drop/drop_target.html"));
299 548
549 // Setup test expectations.
550 DOMDragEventVerifier expected_dom_event_data;
551 expected_dom_event_data.set_expected_drop_effect("none");
552 expected_dom_event_data.set_expected_effect_allowed("all");
553 expected_dom_event_data.set_expected_mime_types("text/plain");
554
555 // Drag text from outside the browser into/over the right frame.
300 { 556 {
301 std::string dragover_event;
302 DOMDragEventWaiter dragover_waiter("dragover", right_frame()); 557 DOMDragEventWaiter dragover_waiter("dragover", right_frame());
303 ASSERT_TRUE(SimulateDragEnterToRightFrame("Dragged test text")); 558 ASSERT_TRUE(SimulateDragEnterToRightFrame("Dragged test text"));
559
560 std::string dragover_event;
304 ASSERT_TRUE(dragover_waiter.WaitForNextMatchingEvent(&dragover_event)); 561 ASSERT_TRUE(dragover_waiter.WaitForNextMatchingEvent(&dragover_event));
305 EXPECT_THAT(dragover_event, testing::HasSubstr("\"drop_effect\":\"none\"")); 562 EXPECT_THAT(dragover_event, expected_dom_event_data.Matches());
306 EXPECT_THAT(dragover_event,
307 testing::HasSubstr("\"effect_allowed\":\"all\""));
308 EXPECT_THAT(dragover_event,
309 testing::HasSubstr("\"mime_types\":\"text/plain\""));
310 } 563 }
311 564
565 // Drop into the right frame.
312 { 566 {
313 std::string drop_event;
314 DOMDragEventWaiter drop_waiter("drop", right_frame()); 567 DOMDragEventWaiter drop_waiter("drop", right_frame());
315 ASSERT_TRUE(SimulateDropInRightFrame()); 568 ASSERT_TRUE(SimulateDropInRightFrame());
569
570 std::string drop_event;
316 ASSERT_TRUE(drop_waiter.WaitForNextMatchingEvent(&drop_event)); 571 ASSERT_TRUE(drop_waiter.WaitForNextMatchingEvent(&drop_event));
317 EXPECT_THAT(drop_event, testing::HasSubstr("\"drop_effect\":\"none\"")); 572 EXPECT_THAT(drop_event, expected_dom_event_data.Matches());
318 EXPECT_THAT(drop_event, testing::HasSubstr("\"effect_allowed\":\"all\""));
319 EXPECT_THAT(drop_event,
320 testing::HasSubstr("\"mime_types\":\"text/plain\""));
321 } 573 }
322 } 574 }
323 575
576 #if defined(USE_OZONE)
ncarter (slow) 2016/11/11 20:56:36 Add a comment explaining why this is disabled in o
Łukasz Anforowicz 2016/11/14 17:53:22 Done. I've opened https://crbug.com/665042. I th
577 #define MAYBE_DragStartInFrame DISABLED_DragStartInFrame
578 #else
579 #define MAYBE_DragStartInFrame DragStartInFrame
580 #endif
581 IN_PROC_BROWSER_TEST_F(DragAndDropBrowserTest, MAYBE_DragStartInFrame) {
582 std::string frame_site = "b.com";
583 ASSERT_TRUE(NavigateToTestPage("a.com"));
584 ASSERT_TRUE(NavigateLeftFrame(frame_site, "image_source.html"));
585
586 // Setup test expectations.
587 // (dragstart event handler in image_source.html is asking for "copy" only).
588 DOMDragEventVerifier expected_dom_event_data;
589 expected_dom_event_data.set_expected_drop_effect("none");
590 expected_dom_event_data.set_expected_effect_allowed("copy");
591 expected_dom_event_data.set_expected_mime_types(
592 "Files,text/html,text/uri-list");
593
594 // Start the drag in the left frame.
595 DragStartWaiter drag_start_waiter(web_contents());
596 DOMDragEventWaiter dragstart_event_waiter("dragstart", left_frame());
597 EXPECT_TRUE(SimulateMouseDownAndDragStartInLeftFrame());
598
599 // Verify Javascript event data.
600 {
601 std::string dragstart_event;
602 EXPECT_TRUE(
603 dragstart_event_waiter.WaitForNextMatchingEvent(&dragstart_event));
604 EXPECT_THAT(dragstart_event, expected_dom_event_data.Matches());
605 }
606
607 // Verify data being passed to the OS.
608 {
609 std::string text;
610 std::string html;
611 int operation = 0;
612 gfx::Point location_inside_web_contents;
613 EXPECT_TRUE(drag_start_waiter.WaitUntilDragStartIsIntercepted(
614 &text, &html, &operation, &location_inside_web_contents));
615 EXPECT_EQ(embedded_test_server()->GetURL(frame_site,
616 "/image_decoding/droids.jpg"),
617 text);
618 EXPECT_THAT(html,
619 testing::MatchesRegex("<img .*src=\""
620 "http://.*:.*/image_decoding/droids.jpg"
ncarter (slow) 2016/11/11 20:56:36 Technically this could fail if the testserver happ
Łukasz Anforowicz 2016/11/14 17:53:22 I guess I can get rid of the colon - this will sim
621 "\">"));
622 EXPECT_EQ(expected_location_of_drag_start_in_left_frame(),
623 location_inside_web_contents);
624 EXPECT_EQ(ui::DragDropTypes::DRAG_COPY, operation);
625 }
626
627 // Try to leave everything in a clean state.
628 SimulateMouseUp();
629 }
630
324 } // namespace chrome 631 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698