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

Side by Side Diff: chrome/browser/ui/views/drag_and_drop_interactive_uitest.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
« no previous file with comments | « chrome/browser/ui/views/OWNERS ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 void 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 // message_loop_runner_->Quit is only called from StartDragAndDrop.
211 DCHECK(drag_started_);
212
213 *text = text_;
214 *html = html_;
215 *operation = operation_;
216 *location_inside_web_contents = location_inside_web_contents_;
217 }
218
219 // aura::client::DragDropClient overrides:
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 DCHECK(!drag_started_);
227 if (!drag_started_) {
228 drag_started_ = true;
229 message_loop_runner_->Quit();
230
231 base::string16 text;
232 if (data.GetString(&text))
233 text_ = base::UTF16ToUTF8(text);
234 else
235 text_ = "<no text>";
236
237 GURL base_url;
238 base::string16 html;
239 if (data.GetHtml(&html, &base_url))
240 html_ = base::UTF16ToUTF8(html);
241 else
242 html_ = "<no html>";
243
244 gfx::Rect bounds =
245 web_contents_->GetContentNativeView()->GetBoundsInScreen();
246 location_inside_web_contents_ =
247 screen_location - gfx::Vector2d(bounds.x(), bounds.y());
248
249 operation_ = operation;
250 }
251
252 // Forwarding to |old_client_| is undesirable, because test cannot control
253 // next steps after a nested drag-and-drop loop is entered at the OS level
254 // (as is the case in Windows, via DoDragDrop). Instead, in the test we
255 // kind of ignore renderer's request to start drag and drop and never
256 // forward this request to the OS-specific layers.
257 return 0;
258 }
259
260 void DragCancel() override {
261 ADD_FAILURE() << "Unexpected call to DragCancel";
262 }
263
264 bool IsDragDropInProgress() override { return drag_started_; }
265
266 private:
267 content::WebContents* web_contents_;
268 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
269 aura::client::DragDropClient* old_client_;
270
271 // Data captured during the first intercepted StartDragAndDrop call.
272 bool drag_started_;
273 std::string text_;
274 std::string html_;
275 int operation_;
276 gfx::Point location_inside_web_contents_;
277
278 DISALLOW_COPY_AND_ASSIGN(DragStartWaiter);
279 };
280
166 // Helper for waiting for notifications from 281 // Helper for waiting for notifications from
167 // content/test/data/drag_and_drop/event_monitoring.js 282 // content/test/data/drag_and_drop/event_monitoring.js
168 class DOMDragEventWaiter { 283 class DOMDragEventWaiter {
169 public: 284 public:
170 explicit DOMDragEventWaiter(const std::string& event_type_to_wait_for, 285 explicit DOMDragEventWaiter(const std::string& event_type_to_wait_for,
171 const ToRenderFrameHost& target) 286 const content::ToRenderFrameHost& target)
172 : target_frame_name_(target.render_frame_host()->GetFrameName()), 287 : target_frame_name_(target.render_frame_host()->GetFrameName()),
173 event_type_to_wait_for_(event_type_to_wait_for), 288 event_type_to_wait_for_(event_type_to_wait_for),
174 dom_message_queue_( 289 dom_message_queue_(content::WebContents::FromRenderFrameHost(
175 WebContents::FromRenderFrameHost(target.render_frame_host())) {} 290 target.render_frame_host())) {}
176 291
177 // Waits until |target| calls reportDragEvent in 292 // Waits until |target| calls reportDragEvent in
178 // content/test/data/drag_and_drop/event_monitoring.js with event_type 293 // chrome/test/data/drag_and_drop/event_monitoring.js with event_type
179 // property set to |event_type_to_wait_for|. (|target| and 294 // property set to |event_type_to_wait_for|. (|target| and
180 // |event_type_to_wait_for| are passed to the constructor). 295 // |event_type_to_wait_for| are passed to the constructor).
181 // 296 //
182 // Returns the event details via |found_event| (in form of a JSON-encoded 297 // 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 298 // object). See chrome/test/data/drag_and_drop/event_monitoring.js for keys
184 // and properties that |found_event| is expected to have. 299 // and properties that |found_event| is expected to have.
185 // 300 //
186 // Returns true upon success. It is okay if |response| is null. 301 // Returns true upon success. It is okay if |response| is null.
187 bool WaitForNextMatchingEvent(std::string* found_event) WARN_UNUSED_RESULT { 302 bool WaitForNextMatchingEvent(std::string* found_event) WARN_UNUSED_RESULT {
188 std::string candidate_event; 303 std::string candidate_event;
189 bool got_right_event_type = false; 304 bool got_right_event_type = false;
190 bool got_right_window_name = false; 305 bool got_right_window_name = false;
191 do { 306 do {
192 if (!dom_message_queue_.WaitForMessage(&candidate_event)) 307 if (!dom_message_queue_.WaitForMessage(&candidate_event))
193 return false; 308 return false;
194 309
195 got_right_event_type = base::MatchPattern( 310 got_right_event_type = base::MatchPattern(
196 candidate_event, base::StringPrintf("*\"event_type\":\"%s\"*", 311 candidate_event, base::StringPrintf("*\"event_type\":\"%s\"*",
197 event_type_to_wait_for_.c_str())); 312 event_type_to_wait_for_.c_str()));
198 313
199 got_right_window_name = base::MatchPattern( 314 got_right_window_name = base::MatchPattern(
200 candidate_event, base::StringPrintf("*\"window_name\":\"%s\"*", 315 candidate_event, base::StringPrintf("*\"window_name\":\"%s\"*",
201 target_frame_name_.c_str())); 316 target_frame_name_.c_str()));
202 } while (!got_right_event_type || !got_right_window_name); 317 } while (!got_right_event_type || !got_right_window_name);
203 318
204 if (found_event) 319 if (found_event)
205 *found_event = candidate_event; 320 *found_event = candidate_event;
206 321
207 return true; 322 return true;
208 } 323 }
209 324
210 private: 325 private:
211 std::string target_frame_name_; 326 std::string target_frame_name_;
212 std::string event_type_to_wait_for_; 327 std::string event_type_to_wait_for_;
213 DOMMessageQueue dom_message_queue_; 328 content::DOMMessageQueue dom_message_queue_;
329
330 DISALLOW_COPY_AND_ASSIGN(DOMDragEventWaiter);
331 };
332
333 // Helper for verifying contents of DOM events associated with drag-and-drop.
334 class DOMDragEventVerifier {
335 public:
336 DOMDragEventVerifier() {}
337
338 void set_expected_drop_effect(const std::string& value) {
339 expected_drop_effect_ = value;
340 }
341
342 void set_expected_effect_allowed(const std::string& value) {
343 expected_effect_allowed_ = value;
344 }
345
346 void set_expected_mime_types(const std::string& value) {
347 expected_mime_types_ = value;
348 }
349
350 // Returns a matcher that will match a std::string (drag event data - e.g.
351 // one returned by DOMDragEventWaiter::WaitForNextMatchingEvent) if it matches
352 // the expectations of this DOMDragEventVerifier.
353 testing::Matcher<std::string> Matches() const {
354 return testing::AllOf(
355 FieldMatches("drop_effect", expected_drop_effect_),
356 FieldMatches("effect_allowed", expected_effect_allowed_),
357 FieldMatches("mime_types", expected_mime_types_));
358 }
359
360 private:
361 static testing::Matcher<std::string> FieldMatches(
362 const std::string& field_name,
363 const std::string& expected_value) {
364 if (expected_value == "<no expectation>")
365 return testing::A<std::string>();
366
367 return testing::HasSubstr(base::StringPrintf(
368 "\"%s\":\"%s\"", field_name.c_str(), expected_value.c_str()));
369 }
370
371 std::string expected_drop_effect_ = "<no expectation>";
372 std::string expected_effect_allowed_ = "<no expectation>";
373 std::string expected_mime_types_ = "<no expectation>";
374
375 DISALLOW_COPY_AND_ASSIGN(DOMDragEventVerifier);
214 }; 376 };
215 377
216 const char kTestPagePath[] = "/drag_and_drop/page.html"; 378 const char kTestPagePath[] = "/drag_and_drop/page.html";
217 379
218 } // namespace 380 } // namespace
219 381
220 class DragAndDropBrowserTest : public ContentBrowserTest { 382 class DragAndDropBrowserTest : public InProcessBrowserTest {
221 public: 383 public:
222 DragAndDropBrowserTest(){}; 384 DragAndDropBrowserTest(){};
223 385
224 protected: 386 protected:
225 void SetUpOnMainThread() override { 387 void SetUpOnMainThread() override {
226 host_resolver()->AddRule("*", "127.0.0.1"); 388 host_resolver()->AddRule("*", "127.0.0.1");
227 ASSERT_TRUE(embedded_test_server()->Start()); 389 ASSERT_TRUE(embedded_test_server()->Start());
228 content::SetupCrossSiteRedirector(embedded_test_server()); 390 content::SetupCrossSiteRedirector(embedded_test_server());
229 drag_simulator_.reset(new DragAndDropSimulator(shell()->web_contents())); 391 drag_simulator_.reset(new DragAndDropSimulator(web_contents()));
230 } 392 }
231 393
232 // Navigates to content/test/data/drag_and_drop/page.html, with page origin 394 content::RenderFrameHost* left_frame() {
233 // and frame contents being controlled by the parameters. 395 AssertTestPageIsLoaded();
234 bool NavigateToTestPage(const std::string& main_origin, 396 return GetFrameByName("left");
235 const std::string& left_frame, 397 }
236 const std::string& right_frame) {
237 GURL base_url = embedded_test_server()->GetURL(main_origin, kTestPagePath);
238 398
239 std::string left_arg; 399 content::RenderFrameHost* right_frame() {
240 if (!left_frame.empty()) { 400 AssertTestPageIsLoaded();
241 left_arg = net::EscapeQueryParamValue( 401 return GetFrameByName("right");
242 std::string("/cross-site/") + left_frame, true); 402 }
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 403
255 return NavigateToURL(shell(), target_url); 404 content::WebContents* web_contents() {
405 return browser()->tab_strip_model()->GetActiveWebContents();
256 } 406 }
257 407
408 //////////////////////
409 // Navigation helpers.
410
411 bool NavigateToTestPage(const std::string& origin_of_main_frame) {
412 GURL url =
413 embedded_test_server()->GetURL(origin_of_main_frame, kTestPagePath);
414 ui_test_utils::NavigateToURL(browser(), url);
415 return web_contents()->GetLastCommittedURL() == url;
416 }
417
418 // Navigates the left frame to |filename| (found under
419 // chrome/test/data/drag_and_drop directory).
420 bool NavigateLeftFrame(const std::string& origin,
421 const std::string& filename) {
422 AssertTestPageIsLoaded();
423 return NavigateNamedFrame("left", origin, filename);
424 }
425
426 // Navigates the right frame to |filename| (found under
427 // chrome/test/data/drag_and_drop directory).
428 bool NavigateRightFrame(const std::string& origin,
429 const std::string& filename) {
430 AssertTestPageIsLoaded();
431 return NavigateNamedFrame("right", origin, filename);
432 }
433
434 ////////////////////////////////////////////////////////////
435 // Simulation of starting a drag-and-drop (using the mouse).
436
437 bool SimulateMouseDownAndDragStartInLeftFrame() {
438 AssertTestPageIsLoaded();
439 if (!SimulateMouseMove(kMiddleOfLeftFrame) || !SimulateMouseDown() ||
440 !SimulateMouseMove(expected_location_of_drag_start_in_left_frame()))
441 return false;
442
443 return true;
444 }
445
446 gfx::Point expected_location_of_drag_start_in_left_frame() {
447 // TODO(crbug.com/653490): The delta below should exceed kDragThresholdX and
448 // kDragThresholdY from MouseEventManager.cpp in blink. Ideally, it would
449 // come from the OS instead.
450 return kMiddleOfLeftFrame + gfx::Vector2d(10, 10);
451 }
452
453 bool SimulateMouseUp() {
454 return ui_test_utils::SendMouseEventsSync(ui_controls::LEFT,
455 ui_controls::UP);
456 }
457
458 ////////////////////////////////////////////////////////////////////
459 // Simulation of dragging from outside the browser into web contents
460 // (using DragAndDropSimulator, not simulating mouse events).
461
258 bool SimulateDragEnterToRightFrame(const std::string& text) { 462 bool SimulateDragEnterToRightFrame(const std::string& text) {
259 AssertTestPageIsLoaded(); 463 AssertTestPageIsLoaded();
260 return drag_simulator_->SimulateDragEnter(kMiddleOfRightFrame, text); 464 return drag_simulator_->SimulateDragEnter(kMiddleOfRightFrame, text);
261 } 465 }
262 466
263 bool SimulateDropInRightFrame() { 467 bool SimulateDropInRightFrame() {
264 AssertTestPageIsLoaded(); 468 AssertTestPageIsLoaded();
265 return drag_simulator_->SimulateDrop(kMiddleOfRightFrame); 469 return drag_simulator_->SimulateDrop(kMiddleOfRightFrame);
266 } 470 }
267 471
268 RenderFrameHost* right_frame() { 472 private:
269 AssertTestPageIsLoaded(); 473 bool SimulateMouseDown() {
270 return GetFrameByName("right"); 474 return ui_test_utils::SendMouseEventsSync(ui_controls::LEFT,
475 ui_controls::DOWN);
271 } 476 }
272 477
273 private: 478 bool SimulateMouseMove(const gfx::Point& location_inside_web_contents) {
274 RenderFrameHost* GetFrameByName(const std::string& name_to_find) { 479 gfx::Rect bounds = web_contents()->GetContainerBounds();
275 WebContentsImpl* web_contents = 480 return ui_test_utils::SendMouseMoveSync(
276 static_cast<WebContentsImpl*>(shell()->web_contents()); 481 gfx::Point(bounds.x() + location_inside_web_contents.x(),
277 FrameTree* frame_tree = web_contents->GetFrameTree(); 482 bounds.y() + location_inside_web_contents.y()));
278 return frame_tree->FindByName(name_to_find)->current_frame_host(); 483 }
484
485 bool NavigateNamedFrame(const std::string& frame_name,
486 const std::string& origin,
487 const std::string& filename) {
488 content::RenderFrameHost* frame = GetFrameByName(frame_name);
489 if (!frame)
490 return false;
491
492 std::string script;
493 int response = 0;
494
495 // Navigate the frame and wait for the load event.
496 script = base::StringPrintf(
497 "location.href = '/cross-site/%s/drag_and_drop/%s';\n"
498 "setTimeout(function() { domAutomationController.send(42); }, 0);",
499 origin.c_str(), filename.c_str());
500 content::TestFrameNavigationObserver observer(frame);
501 if (!content::ExecuteScriptAndExtractInt(frame, script, &response))
502 return false;
503 if (response != 42)
504 return false;
505 observer.Wait();
506
507 // |frame| might have been swapped-out during a cross-site navigation,
508 // therefore we need to get the current RenderFrameHost to work against
509 // going forward.
510 frame = GetFrameByName(frame_name);
511 DCHECK(frame);
512
513 // Wait until frame contents (e.g. images) have painted (which should happen
514 // in the animation frame that *starts* after the onload event - therefore
515 // we need to wait for 2 animation frames).
516 script = std::string(
517 "requestAnimationFrame(function() {\n"
518 " requestAnimationFrame(function() {\n"
519 " domAutomationController.send(43);\n"
520 " });\n"
521 "});\n");
522 if (!content::ExecuteScriptAndExtractInt(frame, script, &response))
523 return false;
524 if (response != 43)
525 return false;
526
527 return true;
528 }
529
530 content::RenderFrameHost* GetFrameByName(const std::string& name_to_find) {
531 content::RenderFrameHost* result = nullptr;
532 for (content::RenderFrameHost* rfh : web_contents()->GetAllFrames()) {
533 if (rfh->GetFrameName() == name_to_find) {
534 if (result) {
535 ADD_FAILURE() << "More than one frame named "
536 << "'" << name_to_find << "'";
537 return nullptr;
538 }
539 result = rfh;
540 }
541 }
542
543 EXPECT_TRUE(result) << "Couldn't find a frame named "
544 << "'" << name_to_find << "'";
545 return result;
279 } 546 }
280 547
281 void AssertTestPageIsLoaded() { 548 void AssertTestPageIsLoaded() {
282 ASSERT_EQ(kTestPagePath, 549 ASSERT_EQ(kTestPagePath, web_contents()->GetLastCommittedURL().path());
283 shell()->web_contents()->GetLastCommittedURL().path());
284 } 550 }
285 551
286 std::unique_ptr<DragAndDropSimulator> drag_simulator_; 552 std::unique_ptr<DragAndDropSimulator> drag_simulator_;
287 553
288 // Constants with coordinates within content/test/data/drag_and_drop/page.html 554 // Constants with coordinates within content/test/data/drag_and_drop/page.html
289 const gfx::Point kMiddleOfLeftFrame = gfx::Point(200, 200); 555 const gfx::Point kMiddleOfLeftFrame = gfx::Point(200, 200);
290 const gfx::Point kMiddleOfRightFrame = gfx::Point(400, 200); 556 const gfx::Point kMiddleOfRightFrame = gfx::Point(400, 200);
291 557
292 DISALLOW_COPY_AND_ASSIGN(DragAndDropBrowserTest); 558 DISALLOW_COPY_AND_ASSIGN(DragAndDropBrowserTest);
293 }; 559 };
294 560
295 IN_PROC_BROWSER_TEST_F(DragAndDropBrowserTest, DropTextFromOutside) { 561 IN_PROC_BROWSER_TEST_F(DragAndDropBrowserTest, DropTextFromOutside) {
296 ASSERT_TRUE(NavigateToTestPage("a.com", 562 ASSERT_TRUE(NavigateToTestPage("a.com"));
297 "", // Left frame is unused by this test. 563 ASSERT_TRUE(NavigateRightFrame("b.com", "drop_target.html"));
298 "c.com/drag_and_drop/drop_target.html"));
299 564
565 // Setup test expectations.
566 DOMDragEventVerifier expected_dom_event_data;
567 expected_dom_event_data.set_expected_drop_effect("none");
568 expected_dom_event_data.set_expected_effect_allowed("all");
569 expected_dom_event_data.set_expected_mime_types("text/plain");
570
571 // Drag text from outside the browser into/over the right frame.
300 { 572 {
301 std::string dragover_event;
302 DOMDragEventWaiter dragover_waiter("dragover", right_frame()); 573 DOMDragEventWaiter dragover_waiter("dragover", right_frame());
303 ASSERT_TRUE(SimulateDragEnterToRightFrame("Dragged test text")); 574 ASSERT_TRUE(SimulateDragEnterToRightFrame("Dragged test text"));
575
576 std::string dragover_event;
304 ASSERT_TRUE(dragover_waiter.WaitForNextMatchingEvent(&dragover_event)); 577 ASSERT_TRUE(dragover_waiter.WaitForNextMatchingEvent(&dragover_event));
305 EXPECT_THAT(dragover_event, testing::HasSubstr("\"drop_effect\":\"none\"")); 578 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 } 579 }
311 580
581 // Drop into the right frame.
312 { 582 {
313 std::string drop_event;
314 DOMDragEventWaiter drop_waiter("drop", right_frame()); 583 DOMDragEventWaiter drop_waiter("drop", right_frame());
315 ASSERT_TRUE(SimulateDropInRightFrame()); 584 ASSERT_TRUE(SimulateDropInRightFrame());
585
586 std::string drop_event;
316 ASSERT_TRUE(drop_waiter.WaitForNextMatchingEvent(&drop_event)); 587 ASSERT_TRUE(drop_waiter.WaitForNextMatchingEvent(&drop_event));
317 EXPECT_THAT(drop_event, testing::HasSubstr("\"drop_effect\":\"none\"")); 588 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 } 589 }
322 } 590 }
323 591
592 // The test is disabled on Ozone because of trouble with mouse simulation
593 // during the test - see https://crbug.com/665042.
594 #if defined(USE_OZONE)
595 #define MAYBE_DragStartInFrame DISABLED_DragStartInFrame
596 #else
597 #define MAYBE_DragStartInFrame DragStartInFrame
598 #endif
599 IN_PROC_BROWSER_TEST_F(DragAndDropBrowserTest, MAYBE_DragStartInFrame) {
600 std::string frame_site = "b.com";
601 ASSERT_TRUE(NavigateToTestPage("a.com"));
602 ASSERT_TRUE(NavigateLeftFrame(frame_site, "image_source.html"));
603
604 // Setup test expectations.
605 // (dragstart event handler in image_source.html is asking for "copy" only).
606 DOMDragEventVerifier expected_dom_event_data;
607 expected_dom_event_data.set_expected_drop_effect("none");
608 expected_dom_event_data.set_expected_effect_allowed("copy");
609 expected_dom_event_data.set_expected_mime_types(
610 "Files,text/html,text/uri-list");
611
612 // Start the drag in the left frame.
613 DragStartWaiter drag_start_waiter(web_contents());
614 DOMDragEventWaiter dragstart_event_waiter("dragstart", left_frame());
615 EXPECT_TRUE(SimulateMouseDownAndDragStartInLeftFrame());
616
617 // Verify Javascript event data.
618 {
619 std::string dragstart_event;
620 EXPECT_TRUE(
621 dragstart_event_waiter.WaitForNextMatchingEvent(&dragstart_event));
622 EXPECT_THAT(dragstart_event, expected_dom_event_data.Matches());
623 }
624
625 // Verify data being passed to the OS.
626 {
627 std::string text;
628 std::string html;
629 int operation = 0;
630 gfx::Point location_inside_web_contents;
631 drag_start_waiter.WaitUntilDragStartIsIntercepted(
632 &text, &html, &operation, &location_inside_web_contents);
633 EXPECT_EQ(embedded_test_server()->GetURL(frame_site,
634 "/image_decoding/droids.jpg"),
635 text);
636 EXPECT_THAT(html,
637 testing::MatchesRegex("<img .*src=\""
638 "http://.*/image_decoding/droids.jpg"
639 "\">"));
640 EXPECT_EQ(expected_location_of_drag_start_in_left_frame(),
641 location_inside_web_contents);
642 EXPECT_EQ(ui::DragDropTypes::DRAG_COPY, operation);
643 }
644
645 // Try to leave everything in a clean state.
646 SimulateMouseUp();
647 }
648
324 } // namespace chrome 649 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/OWNERS ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698