| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/drag_and_drop_interactive_uitest.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/public/browser/web_contents.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/aura/client/drag_drop_delegate.h" |
| 13 #include "ui/aura/window.h" |
| 14 #include "ui/base/dragdrop/drag_drop_types.h" |
| 15 #include "ui/base/dragdrop/drop_target_event.h" |
| 16 #include "ui/base/dragdrop/os_exchange_data.h" |
| 17 #include "ui/gfx/geometry/point.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Test helper for simulating drag and drop happening in WebContents. |
| 22 class ExternalDragEnterSimulatorAura : public ExternalDragEnterSimulator { |
| 23 public: |
| 24 explicit ExternalDragEnterSimulatorAura(content::WebContents* web_contents) |
| 25 : web_contents_(web_contents) {} |
| 26 |
| 27 ~ExternalDragEnterSimulatorAura() override {} |
| 28 |
| 29 bool SimulateDragEnter(const gfx::Point& location, |
| 30 const std::string& text) override; |
| 31 bool SimulateDrop(const gfx::Point& location) override; |
| 32 |
| 33 private: |
| 34 bool SimulateDragEnter(const gfx::Point& location, |
| 35 const ui::OSExchangeData& data); |
| 36 aura::client::DragDropDelegate* GetDragDropDelegate(); |
| 37 void CalculateEventLocations(gfx::Point web_contents_relative_location, |
| 38 gfx::Point* out_event_location, |
| 39 gfx::Point* out_event_root_location); |
| 40 |
| 41 // These are ui::DropTargetEvent::source_operations_ being sent when manually |
| 42 // trying out drag&drop of an image file from Nemo (Ubuntu's file explorer) |
| 43 // into a content_shell. |
| 44 static constexpr int kDefaultSourceOperations = ui::DragDropTypes::DRAG_MOVE | |
| 45 ui::DragDropTypes::DRAG_COPY | |
| 46 ui::DragDropTypes::DRAG_LINK; |
| 47 |
| 48 content::WebContents* web_contents_; |
| 49 std::unique_ptr<ui::DropTargetEvent> active_drag_event_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(ExternalDragEnterSimulatorAura); |
| 52 }; |
| 53 |
| 54 bool ExternalDragEnterSimulatorAura::SimulateDragEnter( |
| 55 const gfx::Point& location, |
| 56 const std::string& text) { |
| 57 ui::OSExchangeData data; |
| 58 data.SetString(base::UTF8ToUTF16(text)); |
| 59 return SimulateDragEnter(location, data); |
| 60 } |
| 61 |
| 62 bool ExternalDragEnterSimulatorAura::SimulateDrop(const gfx::Point& location) { |
| 63 if (!active_drag_event_) { |
| 64 ADD_FAILURE() << "Cannot drop a drag that hasn't started yet."; |
| 65 return false; |
| 66 } |
| 67 |
| 68 aura::client::DragDropDelegate* delegate = GetDragDropDelegate(); |
| 69 if (!delegate) |
| 70 return false; |
| 71 |
| 72 gfx::Point event_location; |
| 73 gfx::Point event_root_location; |
| 74 CalculateEventLocations(location, &event_location, &event_root_location); |
| 75 active_drag_event_->set_location(event_location); |
| 76 active_drag_event_->set_root_location(event_root_location); |
| 77 |
| 78 delegate->OnDragUpdated(*active_drag_event_); |
| 79 delegate->OnPerformDrop(*active_drag_event_); |
| 80 return true; |
| 81 } |
| 82 |
| 83 bool ExternalDragEnterSimulatorAura::SimulateDragEnter( |
| 84 const gfx::Point& location, |
| 85 const ui::OSExchangeData& data) { |
| 86 if (active_drag_event_) { |
| 87 ADD_FAILURE() << "Cannot start a new drag when old one hasn't ended yet."; |
| 88 return false; |
| 89 } |
| 90 |
| 91 aura::client::DragDropDelegate* delegate = GetDragDropDelegate(); |
| 92 if (!delegate) |
| 93 return false; |
| 94 |
| 95 gfx::Point event_location; |
| 96 gfx::Point event_root_location; |
| 97 CalculateEventLocations(location, &event_location, &event_root_location); |
| 98 active_drag_event_.reset(new ui::DropTargetEvent( |
| 99 data, event_location, event_root_location, kDefaultSourceOperations)); |
| 100 |
| 101 delegate->OnDragEntered(*active_drag_event_); |
| 102 delegate->OnDragUpdated(*active_drag_event_); |
| 103 return true; |
| 104 } |
| 105 |
| 106 aura::client::DragDropDelegate* |
| 107 ExternalDragEnterSimulatorAura::GetDragDropDelegate() { |
| 108 gfx::NativeView view = web_contents_->GetContentNativeView(); |
| 109 aura::client::DragDropDelegate* delegate = |
| 110 aura::client::GetDragDropDelegate(view); |
| 111 EXPECT_TRUE(delegate) << "Expecting WebContents to have DragDropDelegate"; |
| 112 return delegate; |
| 113 } |
| 114 |
| 115 void ExternalDragEnterSimulatorAura::CalculateEventLocations( |
| 116 gfx::Point web_contents_relative_location, |
| 117 gfx::Point* out_event_location, |
| 118 gfx::Point* out_event_root_location) { |
| 119 gfx::NativeView view = web_contents_->GetNativeView(); |
| 120 |
| 121 *out_event_location = web_contents_relative_location; |
| 122 |
| 123 gfx::Point root_location = web_contents_relative_location; |
| 124 aura::Window::ConvertPointToTarget(view, view->GetRootWindow(), |
| 125 &root_location); |
| 126 *out_event_root_location = root_location; |
| 127 } |
| 128 |
| 129 } // namespace |
| 130 |
| 131 // static |
| 132 std::unique_ptr<ExternalDragEnterSimulator> ExternalDragEnterSimulator::Create( |
| 133 content::WebContents* web_contents) { |
| 134 return base::WrapUnique(new ExternalDragEnterSimulatorAura(web_contents)); |
| 135 } |
| OLD | NEW |