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.
5 #include "ash/drag_drop/drag_drop_tracker.h"
8 #include "ash/shell_window_ids.h"
9 #include "ash/test/ash_test_base.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "ui/aura/test/test_windows.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_event_dispatcher.h"
18 class DragDropTrackerTest
: public test::AshTestBase
{
20 void SetUp() override
{ AshTestBase::SetUp(); }
22 aura::Window
* CreateTestWindow(const gfx::Rect
& bounds
) {
23 static int window_id
= 0;
24 return CreateTestWindowInShellWithDelegate(
25 aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(),
30 static aura::Window
* GetTarget(const gfx::Point
& location
) {
31 scoped_ptr
<DragDropTracker
> tracker(
32 new DragDropTracker(Shell::GetPrimaryRootWindow(), NULL
));
33 ui::MouseEvent
e(ui::ET_MOUSE_DRAGGED
,
38 aura::Window
* target
= tracker
->GetTarget(e
);
42 static ui::LocatedEvent
* ConvertEvent(aura::Window
* target
,
43 const ui::MouseEvent
& event
) {
44 scoped_ptr
<DragDropTracker
> tracker(
45 new DragDropTracker(Shell::GetPrimaryRootWindow(), NULL
));
46 ui::LocatedEvent
* converted
= tracker
->ConvertEvent(target
, event
);
51 TEST_F(DragDropTrackerTest
, GetTarget
) {
52 if (!SupportsMultipleDisplays())
55 UpdateDisplay("200x200,300x300");
56 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
57 EXPECT_EQ(2U, root_windows
.size());
59 scoped_ptr
<aura::Window
> window0(
60 CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
63 scoped_ptr
<aura::Window
> window1(
64 CreateTestWindow(gfx::Rect(300, 100, 100, 100)));
66 EXPECT_EQ(root_windows
[0], window0
->GetRootWindow());
67 EXPECT_EQ(root_windows
[1], window1
->GetRootWindow());
68 EXPECT_EQ("0,0 100x100", window0
->GetBoundsInScreen().ToString());
69 EXPECT_EQ("300,100 100x100", window1
->GetBoundsInScreen().ToString());
71 // Make RootWindow0 active so that capture window is parented to it.
72 Shell::GetInstance()->set_target_root_window(root_windows
[0]);
74 // Start tracking from the RootWindow1 and check the point on RootWindow0 that
76 EXPECT_EQ(window0
.get(), GetTarget(gfx::Point(50, 50)));
78 // Start tracking from the RootWindow0 and check the point on RootWindow0 that
79 // neither |window0| nor |window1| covers.
80 EXPECT_NE(window0
.get(), GetTarget(gfx::Point(150, 150)));
81 EXPECT_NE(window1
.get(), GetTarget(gfx::Point(150, 150)));
83 // Start tracking from the RootWindow0 and check the point on RootWindow1 that
85 EXPECT_EQ(window1
.get(), GetTarget(gfx::Point(350, 150)));
87 // Start tracking from the RootWindow0 and check the point on RootWindow1 that
88 // neither |window0| nor |window1| covers.
89 EXPECT_NE(window0
.get(), GetTarget(gfx::Point(50, 250)));
90 EXPECT_NE(window1
.get(), GetTarget(gfx::Point(50, 250)));
92 // Make RootWindow1 active so that capture window is parented to it.
93 Shell::GetInstance()->set_target_root_window(root_windows
[1]);
95 // Start tracking from the RootWindow1 and check the point on RootWindow0 that
97 EXPECT_EQ(window0
.get(), GetTarget(gfx::Point(-150, 50)));
99 // Start tracking from the RootWindow1 and check the point on RootWindow0 that
100 // neither |window0| nor |window1| covers.
101 EXPECT_NE(window0
.get(), GetTarget(gfx::Point(150, -50)));
102 EXPECT_NE(window1
.get(), GetTarget(gfx::Point(150, -50)));
104 // Start tracking from the RootWindow1 and check the point on RootWindow1 that
106 EXPECT_EQ(window1
.get(), GetTarget(gfx::Point(150, 150)));
108 // Start tracking from the RootWindow1 and check the point on RootWindow1 that
109 // neither |window0| nor |window1| covers.
110 EXPECT_NE(window0
.get(), GetTarget(gfx::Point(50, 50)));
111 EXPECT_NE(window1
.get(), GetTarget(gfx::Point(50, 50)));
114 TEST_F(DragDropTrackerTest
, ConvertEvent
) {
115 if (!SupportsMultipleDisplays())
118 UpdateDisplay("200x200,300x300");
119 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
120 EXPECT_EQ(2U, root_windows
.size());
122 scoped_ptr
<aura::Window
> window0(
123 CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
126 scoped_ptr
<aura::Window
> window1(
127 CreateTestWindow(gfx::Rect(300, 100, 100, 100)));
130 // Make RootWindow0 active so that capture window is parented to it.
131 Shell::GetInstance()->set_target_root_window(root_windows
[0]);
133 // Start tracking from the RootWindow0 and converts the mouse event into
134 // |window0|'s coodinates.
135 ui::MouseEvent
original00(ui::ET_MOUSE_DRAGGED
,
140 scoped_ptr
<ui::LocatedEvent
> converted00(ConvertEvent(window0
.get(),
142 EXPECT_EQ(original00
.type(), converted00
->type());
143 EXPECT_EQ("50,50", converted00
->location().ToString());
144 EXPECT_EQ("50,50", converted00
->root_location().ToString());
145 EXPECT_EQ(original00
.flags(), converted00
->flags());
147 // Start tracking from the RootWindow0 and converts the mouse event into
148 // |window1|'s coodinates.
149 ui::MouseEvent
original01(ui::ET_MOUSE_DRAGGED
,
150 gfx::Point(350, 150),
151 gfx::Point(350, 150),
154 scoped_ptr
<ui::LocatedEvent
> converted01(ConvertEvent(window1
.get(),
156 EXPECT_EQ(original01
.type(), converted01
->type());
157 EXPECT_EQ("50,50", converted01
->location().ToString());
158 EXPECT_EQ("150,150", converted01
->root_location().ToString());
159 EXPECT_EQ(original01
.flags(), converted01
->flags());
161 // Make RootWindow1 active so that capture window is parented to it.
162 Shell::GetInstance()->set_target_root_window(root_windows
[1]);
164 // Start tracking from the RootWindow1 and converts the mouse event into
165 // |window0|'s coodinates.
166 ui::MouseEvent
original10(ui::ET_MOUSE_DRAGGED
,
167 gfx::Point(-150, 50),
168 gfx::Point(-150, 50),
171 scoped_ptr
<ui::LocatedEvent
> converted10(ConvertEvent(window0
.get(),
173 EXPECT_EQ(original10
.type(), converted10
->type());
174 EXPECT_EQ("50,50", converted10
->location().ToString());
175 EXPECT_EQ("50,50", converted10
->root_location().ToString());
176 EXPECT_EQ(original10
.flags(), converted10
->flags());
178 // Start tracking from the RootWindow1 and converts the mouse event into
179 // |window1|'s coodinates.
180 ui::MouseEvent
original11(ui::ET_MOUSE_DRAGGED
,
181 gfx::Point(150, 150),
182 gfx::Point(150, 150),
185 scoped_ptr
<ui::LocatedEvent
> converted11(ConvertEvent(window1
.get(),
187 EXPECT_EQ(original11
.type(), converted11
->type());
188 EXPECT_EQ("50,50", converted11
->location().ToString());
189 EXPECT_EQ("150,150", converted11
->root_location().ToString());
190 EXPECT_EQ(original11
.flags(), converted11
->flags());