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.
4
5#include "ash/drag_drop/drag_drop_tracker.h"
6
7#include "ash/shell.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"
14
15namespace ash {
16namespace test {
17
18class DragDropTrackerTest : public test::AshTestBase {
19 public:
20  virtual void SetUp() OVERRIDE {
21    AshTestBase::SetUp();
22  }
23
24  aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
25    static int window_id = 0;
26    return CreateTestWindowInShellWithDelegate(
27        aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(),
28        window_id++,
29        bounds);
30  }
31
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,
36                     location,
37                     location,
38                     ui::EF_NONE,
39                     ui::EF_NONE);
40    aura::Window* target = tracker->GetTarget(e);
41    return target;
42  }
43
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);
49    return converted;
50  }
51};
52
53TEST_F(DragDropTrackerTest, GetTarget) {
54  if (!SupportsMultipleDisplays())
55    return;
56
57  UpdateDisplay("200x200,300x300");
58  aura::Window::Windows root_windows = Shell::GetAllRootWindows();
59  EXPECT_EQ(2U, root_windows.size());
60
61  scoped_ptr<aura::Window> window0(
62      CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
63  window0->Show();
64
65  scoped_ptr<aura::Window> window1(
66      CreateTestWindow(gfx::Rect(300, 100, 100, 100)));
67  window1->Show();
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());
72
73  // Make RootWindow0 active so that capture window is parented to it.
74  Shell::GetInstance()->set_target_root_window(root_windows[0]);
75
76  // Start tracking from the RootWindow1 and check the point on RootWindow0 that
77  // |window0| covers.
78  EXPECT_EQ(window0.get(), GetTarget(gfx::Point(50, 50)));
79
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)));
84
85  // Start tracking from the RootWindow0 and check the point on RootWindow1 that
86  // |window1| covers.
87  EXPECT_EQ(window1.get(), GetTarget(gfx::Point(350, 150)));
88
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)));
93
94  // Make RootWindow1 active so that capture window is parented to it.
95  Shell::GetInstance()->set_target_root_window(root_windows[1]);
96
97  // Start tracking from the RootWindow1 and check the point on RootWindow0 that
98  // |window0| covers.
99  EXPECT_EQ(window0.get(), GetTarget(gfx::Point(-150, 50)));
100
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)));
105
106  // Start tracking from the RootWindow1 and check the point on RootWindow1 that
107  // |window1| covers.
108  EXPECT_EQ(window1.get(), GetTarget(gfx::Point(150, 150)));
109
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)));
114}
115
116TEST_F(DragDropTrackerTest, ConvertEvent) {
117  if (!SupportsMultipleDisplays())
118    return;
119
120  UpdateDisplay("200x200,300x300");
121  aura::Window::Windows root_windows = Shell::GetAllRootWindows();
122  EXPECT_EQ(2U, root_windows.size());
123
124  scoped_ptr<aura::Window> window0(
125      CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
126  window0->Show();
127
128  scoped_ptr<aura::Window> window1(
129      CreateTestWindow(gfx::Rect(300, 100, 100, 100)));
130  window1->Show();
131
132  // Make RootWindow0 active so that capture window is parented to it.
133  Shell::GetInstance()->set_target_root_window(root_windows[0]);
134
135  // Start tracking from the RootWindow0 and converts the mouse event into
136  // |window0|'s coodinates.
137  ui::MouseEvent original00(ui::ET_MOUSE_DRAGGED,
138                            gfx::Point(50, 50),
139                            gfx::Point(50, 50),
140                            ui::EF_NONE,
141                            ui::EF_NONE);
142  scoped_ptr<ui::LocatedEvent> converted00(ConvertEvent(window0.get(),
143                                                        original00));
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());
148
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),
154                            ui::EF_NONE,
155                            ui::EF_NONE);
156  scoped_ptr<ui::LocatedEvent> converted01(ConvertEvent(window1.get(),
157                                                        original01));
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());
162
163  // Make RootWindow1 active so that capture window is parented to it.
164  Shell::GetInstance()->set_target_root_window(root_windows[1]);
165
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),
171                            ui::EF_NONE,
172                            ui::EF_NONE);
173  scoped_ptr<ui::LocatedEvent> converted10(ConvertEvent(window0.get(),
174                                                        original10));
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());
179
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),
185                            ui::EF_NONE,
186                            ui::EF_NONE);
187  scoped_ptr<ui::LocatedEvent> converted11(ConvertEvent(window1.get(),
188                                                           original11));
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());
193}
194
195}  // namespace test
196}  // namespace aura
197