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 virtual void SetUp() OVERRIDE
{
24 aura::Window
* CreateTestWindow(const gfx::Rect
& bounds
) {
25 static int window_id
= 0;
26 return CreateTestWindowInShellWithDelegate(
27 aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(),
32 static aura::Window
* GetTarget(const gfx::Point
& location
) {
33 scoped_ptr
<DragDropTracker
> tracker(
34 new DragDropTracker(Shell::GetPrimaryRootWindow(), NULL
));
35 ui::MouseEvent
e(ui::ET_MOUSE_DRAGGED
,
40 aura::Window
* target
= tracker
->GetTarget(e
);
44 static ui::LocatedEvent
* ConvertEvent(aura::Window
* target
,
45 const ui::MouseEvent
& event
) {
46 scoped_ptr
<DragDropTracker
> tracker(
47 new DragDropTracker(Shell::GetPrimaryRootWindow(), NULL
));
48 ui::LocatedEvent
* converted
= tracker
->ConvertEvent(target
, event
);
53 TEST_F(DragDropTrackerTest
, GetTarget
) {
54 if (!SupportsMultipleDisplays())
57 UpdateDisplay("200x200,300x300");
58 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
59 EXPECT_EQ(2U, root_windows
.size());
61 scoped_ptr
<aura::Window
> window0(
62 CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
65 scoped_ptr
<aura::Window
> window1(
66 CreateTestWindow(gfx::Rect(300, 100, 100, 100)));
68 EXPECT_EQ(root_windows
[0], window0
->GetRootWindow());
69 EXPECT_EQ(root_windows
[1], window1
->GetRootWindow());
70 EXPECT_EQ("0,0 100x100", window0
->GetBoundsInScreen().ToString());
71 EXPECT_EQ("300,100 100x100", window1
->GetBoundsInScreen().ToString());
73 // Make RootWindow0 active so that capture window is parented to it.
74 Shell::GetInstance()->set_target_root_window(root_windows
[0]);
76 // Start tracking from the RootWindow1 and check the point on RootWindow0 that
78 EXPECT_EQ(window0
.get(), GetTarget(gfx::Point(50, 50)));
80 // Start tracking from the RootWindow0 and check the point on RootWindow0 that
81 // neither |window0| nor |window1| covers.
82 EXPECT_NE(window0
.get(), GetTarget(gfx::Point(150, 150)));
83 EXPECT_NE(window1
.get(), GetTarget(gfx::Point(150, 150)));
85 // Start tracking from the RootWindow0 and check the point on RootWindow1 that
87 EXPECT_EQ(window1
.get(), GetTarget(gfx::Point(350, 150)));
89 // Start tracking from the RootWindow0 and check the point on RootWindow1 that
90 // neither |window0| nor |window1| covers.
91 EXPECT_NE(window0
.get(), GetTarget(gfx::Point(50, 250)));
92 EXPECT_NE(window1
.get(), GetTarget(gfx::Point(50, 250)));
94 // Make RootWindow1 active so that capture window is parented to it.
95 Shell::GetInstance()->set_target_root_window(root_windows
[1]);
97 // Start tracking from the RootWindow1 and check the point on RootWindow0 that
99 EXPECT_EQ(window0
.get(), GetTarget(gfx::Point(-150, 50)));
101 // Start tracking from the RootWindow1 and check the point on RootWindow0 that
102 // neither |window0| nor |window1| covers.
103 EXPECT_NE(window0
.get(), GetTarget(gfx::Point(150, -50)));
104 EXPECT_NE(window1
.get(), GetTarget(gfx::Point(150, -50)));
106 // Start tracking from the RootWindow1 and check the point on RootWindow1 that
108 EXPECT_EQ(window1
.get(), GetTarget(gfx::Point(150, 150)));
110 // Start tracking from the RootWindow1 and check the point on RootWindow1 that
111 // neither |window0| nor |window1| covers.
112 EXPECT_NE(window0
.get(), GetTarget(gfx::Point(50, 50)));
113 EXPECT_NE(window1
.get(), GetTarget(gfx::Point(50, 50)));
116 TEST_F(DragDropTrackerTest
, ConvertEvent
) {
117 if (!SupportsMultipleDisplays())
120 UpdateDisplay("200x200,300x300");
121 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
122 EXPECT_EQ(2U, root_windows
.size());
124 scoped_ptr
<aura::Window
> window0(
125 CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
128 scoped_ptr
<aura::Window
> window1(
129 CreateTestWindow(gfx::Rect(300, 100, 100, 100)));
132 // Make RootWindow0 active so that capture window is parented to it.
133 Shell::GetInstance()->set_target_root_window(root_windows
[0]);
135 // Start tracking from the RootWindow0 and converts the mouse event into
136 // |window0|'s coodinates.
137 ui::MouseEvent
original00(ui::ET_MOUSE_DRAGGED
,
142 scoped_ptr
<ui::LocatedEvent
> converted00(ConvertEvent(window0
.get(),
144 EXPECT_EQ(original00
.type(), converted00
->type());
145 EXPECT_EQ("50,50", converted00
->location().ToString());
146 EXPECT_EQ("50,50", converted00
->root_location().ToString());
147 EXPECT_EQ(original00
.flags(), converted00
->flags());
149 // Start tracking from the RootWindow0 and converts the mouse event into
150 // |window1|'s coodinates.
151 ui::MouseEvent
original01(ui::ET_MOUSE_DRAGGED
,
152 gfx::Point(350, 150),
153 gfx::Point(350, 150),
156 scoped_ptr
<ui::LocatedEvent
> converted01(ConvertEvent(window1
.get(),
158 EXPECT_EQ(original01
.type(), converted01
->type());
159 EXPECT_EQ("50,50", converted01
->location().ToString());
160 EXPECT_EQ("150,150", converted01
->root_location().ToString());
161 EXPECT_EQ(original01
.flags(), converted01
->flags());
163 // Make RootWindow1 active so that capture window is parented to it.
164 Shell::GetInstance()->set_target_root_window(root_windows
[1]);
166 // Start tracking from the RootWindow1 and converts the mouse event into
167 // |window0|'s coodinates.
168 ui::MouseEvent
original10(ui::ET_MOUSE_DRAGGED
,
169 gfx::Point(-150, 50),
170 gfx::Point(-150, 50),
173 scoped_ptr
<ui::LocatedEvent
> converted10(ConvertEvent(window0
.get(),
175 EXPECT_EQ(original10
.type(), converted10
->type());
176 EXPECT_EQ("50,50", converted10
->location().ToString());
177 EXPECT_EQ("50,50", converted10
->root_location().ToString());
178 EXPECT_EQ(original10
.flags(), converted10
->flags());
180 // Start tracking from the RootWindow1 and converts the mouse event into
181 // |window1|'s coodinates.
182 ui::MouseEvent
original11(ui::ET_MOUSE_DRAGGED
,
183 gfx::Point(150, 150),
184 gfx::Point(150, 150),
187 scoped_ptr
<ui::LocatedEvent
> converted11(ConvertEvent(window1
.get(),
189 EXPECT_EQ(original11
.type(), converted11
->type());
190 EXPECT_EQ("50,50", converted11
->location().ToString());
191 EXPECT_EQ("150,150", converted11
->root_location().ToString());
192 EXPECT_EQ(original11
.flags(), converted11
->flags());