1 // Copyright (c) 2013 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.
5 #include "ash/drag_drop/drag_drop_controller.h"
8 #include "ash/test/ash_test_base.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "ui/aura/window_event_dispatcher.h"
14 #include "ui/base/dragdrop/drag_drop_types.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/base/test/ui_controls.h"
17 #include "ui/base/ui_base_paths.h"
18 #include "ui/gl/gl_surface.h"
19 #include "ui/views/view.h"
20 #include "ui/views/widget/widget.h"
25 class DraggableView
: public views::View
{
28 virtual ~DraggableView() {}
30 // views::View overrides:
31 virtual int GetDragOperations(const gfx::Point
& press_pt
) override
{
32 return ui::DragDropTypes::DRAG_MOVE
;
34 virtual void WriteDragData(const gfx::Point
& press_pt
,
35 OSExchangeData
* data
)override
{
36 data
->SetString(base::UTF8ToUTF16("test"));
40 DISALLOW_COPY_AND_ASSIGN(DraggableView
);
43 class TargetView
: public views::View
{
45 TargetView() : dropped_(false) {}
46 virtual ~TargetView() {}
48 // views::View overrides:
49 virtual bool GetDropFormats(
51 std::set
<OSExchangeData::CustomFormat
>* custom_formats
) override
{
52 *formats
= ui::OSExchangeData::STRING
;
55 virtual bool AreDropTypesRequired() override
{
58 virtual bool CanDrop(const OSExchangeData
& data
) override
{
61 virtual int OnDragUpdated(const ui::DropTargetEvent
& event
) override
{
62 return ui::DragDropTypes::DRAG_MOVE
;
64 virtual int OnPerformDrop(const ui::DropTargetEvent
& event
) override
{
66 return ui::DragDropTypes::DRAG_MOVE
;
69 bool dropped() const { return dropped_
; }
74 DISALLOW_COPY_AND_ASSIGN(TargetView
);
77 views::Widget
* CreateWidget(views::View
* contents_view
,
78 const gfx::Rect
& bounds
) {
79 views::Widget
* widget
= new views::Widget
;
80 views::Widget::InitParams params
;
81 params
.type
= views::Widget::InitParams::TYPE_WINDOW_FRAMELESS
;
82 params
.accept_events
= true;
83 params
.context
= Shell::GetPrimaryRootWindow();
84 params
.bounds
= bounds
;
87 widget
->SetContentsView(contents_view
);
93 base::MessageLoop::current()->Quit();
96 void DragDropAcrossMultiDisplay_Step4() {
97 ui_controls::SendMouseEventsNotifyWhenDone(
98 ui_controls::LEFT
, ui_controls::UP
,
99 base::Bind(&QuitLoop
));
102 void DragDropAcrossMultiDisplay_Step3() {
103 // Move to the edge of the 1st display so that the mouse
104 // is moved to 2nd display by ash.
105 ui_controls::SendMouseMoveNotifyWhenDone(
107 base::Bind(&DragDropAcrossMultiDisplay_Step4
));
110 void DragDropAcrossMultiDisplay_Step2() {
111 ui_controls::SendMouseMoveNotifyWhenDone(
113 base::Bind(&DragDropAcrossMultiDisplay_Step3
));
116 void DragDropAcrossMultiDisplay_Step1() {
117 ui_controls::SendMouseEventsNotifyWhenDone(
118 ui_controls::LEFT
, ui_controls::DOWN
,
119 base::Bind(&DragDropAcrossMultiDisplay_Step2
));
124 class DragDropTest
: public test::AshTestBase
{
127 virtual ~DragDropTest() {}
129 virtual void SetUp() override
{
130 gfx::GLSurface::InitializeOneOffForTests();
132 ui::RegisterPathProvider();
133 ui::ResourceBundle::InitSharedInstanceWithLocale(
134 "en-US", NULL
, ui::ResourceBundle::LOAD_COMMON_RESOURCES
);
135 base::FilePath resources_pack_path
;
136 PathService::Get(base::DIR_MODULE
, &resources_pack_path
);
137 resources_pack_path
=
138 resources_pack_path
.Append(FILE_PATH_LITERAL("resources.pak"));
139 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
140 resources_pack_path
, ui::SCALE_FACTOR_NONE
);
142 test::AshTestBase::SetUp();
146 #if !defined(OS_CHROMEOS)
147 #define MAYBE_DragDropAcrossMultiDisplay DISABLED_DragDropAcrossMultiDisplay
149 #define MAYBE_DragDropAcrossMultiDisplay DragDropAcrossMultiDisplay
152 // Test if the mouse gets moved properly to another display
153 // during drag & drop operation.
154 TEST_F(DragDropTest
, MAYBE_DragDropAcrossMultiDisplay
) {
155 if (!SupportsMultipleDisplays())
158 UpdateDisplay("400x400,400x400");
159 aura::Window::Windows root_windows
=
160 Shell::GetInstance()->GetAllRootWindows();
161 views::View
* draggable_view
= new DraggableView();
162 draggable_view
->set_drag_controller(NULL
);
163 draggable_view
->SetBounds(0, 0, 100, 100);
164 views::Widget
* source
=
165 CreateWidget(draggable_view
, gfx::Rect(0, 0, 100, 100));
167 TargetView
* target_view
= new TargetView();
168 target_view
->SetBounds(0, 0, 100, 100);
169 views::Widget
* target
=
170 CreateWidget(target_view
, gfx::Rect(400, 0, 100, 100));
172 // Make sure they're on the different root windows.
173 EXPECT_EQ(root_windows
[0], source
->GetNativeView()->GetRootWindow());
174 EXPECT_EQ(root_windows
[1], target
->GetNativeView()->GetRootWindow());
176 ui_controls::SendMouseMoveNotifyWhenDone(
177 10, 10, base::Bind(&DragDropAcrossMultiDisplay_Step1
));
179 base::MessageLoop::current()->Run();
181 EXPECT_TRUE(target_view
->dropped());