blob: 0e4bfa1ec421990d71f94fd3aa71ca0d1133c4f5 [file] [log] [blame]
[email protected]0518d0d2012-08-26 11:59:041// 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"
[email protected]0518d0d2012-08-26 11:59:0411#include "ui/aura/test/test_windows.h"
12#include "ui/aura/window.h"
[email protected]fcc51c952014-02-21 21:31:2613#include "ui/aura/window_event_dispatcher.h"
robert.bradfordb3d800b2015-02-18 18:56:1014#include "ui/events/event_utils.h"
[email protected]0518d0d2012-08-26 11:59:0415
16namespace ash {
17namespace test {
18
19class DragDropTrackerTest : public test::AshTestBase {
20 public:
dcheng34dfcbe2014-10-29 18:58:1921 void SetUp() override { AshTestBase::SetUp(); }
[email protected]0518d0d2012-08-26 11:59:0422
[email protected]5ebe6102012-11-28 21:00:0323 aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
[email protected]0518d0d2012-08-26 11:59:0424 static int window_id = 0;
[email protected]5ebe6102012-11-28 21:00:0325 return CreateTestWindowInShellWithDelegate(
[email protected]b31dbaf52012-09-10 20:47:1026 aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(),
[email protected]0518d0d2012-08-26 11:59:0427 window_id++,
[email protected]5ebe6102012-11-28 21:00:0328 bounds);
[email protected]0518d0d2012-08-26 11:59:0429 }
30
[email protected]978a4a32012-10-05 19:51:0631 static aura::Window* GetTarget(const gfx::Point& location) {
[email protected]093b8d642014-04-03 20:59:2832 scoped_ptr<DragDropTracker> tracker(
33 new DragDropTracker(Shell::GetPrimaryRootWindow(), NULL));
robert.bradfordb3d800b2015-02-18 18:56:1034 ui::MouseEvent e(ui::ET_MOUSE_DRAGGED, location, location,
35 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
[email protected]0518d0d2012-08-26 11:59:0436 aura::Window* target = tracker->GetTarget(e);
37 return target;
38 }
39
[email protected]ffa4e4482012-11-28 12:11:4940 static ui::LocatedEvent* ConvertEvent(aura::Window* target,
[email protected]0518d0d2012-08-26 11:59:0441 const ui::MouseEvent& event) {
[email protected]093b8d642014-04-03 20:59:2842 scoped_ptr<DragDropTracker> tracker(
43 new DragDropTracker(Shell::GetPrimaryRootWindow(), NULL));
[email protected]ffa4e4482012-11-28 12:11:4944 ui::LocatedEvent* converted = tracker->ConvertEvent(target, event);
[email protected]0518d0d2012-08-26 11:59:0445 return converted;
46 }
47};
48
dnicoara54f98652014-09-18 17:58:2249TEST_F(DragDropTrackerTest, GetTarget) {
50 if (!SupportsMultipleDisplays())
51 return;
[email protected]0518d0d2012-08-26 11:59:0452
dnicoara54f98652014-09-18 17:58:2253 UpdateDisplay("200x200,300x300");
[email protected]c9390bd2013-11-08 20:33:1354 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
[email protected]0518d0d2012-08-26 11:59:0455 EXPECT_EQ(2U, root_windows.size());
56
[email protected]0518d0d2012-08-26 11:59:0457 scoped_ptr<aura::Window> window0(
[email protected]5ebe6102012-11-28 21:00:0358 CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
[email protected]0518d0d2012-08-26 11:59:0459 window0->Show();
60
[email protected]0518d0d2012-08-26 11:59:0461 scoped_ptr<aura::Window> window1(
[email protected]96ea2282012-12-27 00:37:5562 CreateTestWindow(gfx::Rect(300, 100, 100, 100)));
[email protected]0518d0d2012-08-26 11:59:0463 window1->Show();
[email protected]96ea2282012-12-27 00:37:5564 EXPECT_EQ(root_windows[0], window0->GetRootWindow());
65 EXPECT_EQ(root_windows[1], window1->GetRootWindow());
66 EXPECT_EQ("0,0 100x100", window0->GetBoundsInScreen().ToString());
67 EXPECT_EQ("300,100 100x100", window1->GetBoundsInScreen().ToString());
[email protected]0518d0d2012-08-26 11:59:0468
[email protected]978a4a32012-10-05 19:51:0669 // Make RootWindow0 active so that capture window is parented to it.
[email protected]d17642d2013-09-12 23:44:3870 Shell::GetInstance()->set_target_root_window(root_windows[0]);
[email protected]0518d0d2012-08-26 11:59:0471
72 // Start tracking from the RootWindow1 and check the point on RootWindow0 that
73 // |window0| covers.
[email protected]978a4a32012-10-05 19:51:0674 EXPECT_EQ(window0.get(), GetTarget(gfx::Point(50, 50)));
75
76 // Start tracking from the RootWindow0 and check the point on RootWindow0 that
77 // neither |window0| nor |window1| covers.
78 EXPECT_NE(window0.get(), GetTarget(gfx::Point(150, 150)));
79 EXPECT_NE(window1.get(), GetTarget(gfx::Point(150, 150)));
80
81 // Start tracking from the RootWindow0 and check the point on RootWindow1 that
82 // |window1| covers.
83 EXPECT_EQ(window1.get(), GetTarget(gfx::Point(350, 150)));
84
85 // Start tracking from the RootWindow0 and check the point on RootWindow1 that
86 // neither |window0| nor |window1| covers.
87 EXPECT_NE(window0.get(), GetTarget(gfx::Point(50, 250)));
88 EXPECT_NE(window1.get(), GetTarget(gfx::Point(50, 250)));
89
90 // Make RootWindow1 active so that capture window is parented to it.
[email protected]d17642d2013-09-12 23:44:3891 Shell::GetInstance()->set_target_root_window(root_windows[1]);
[email protected]978a4a32012-10-05 19:51:0692
93 // Start tracking from the RootWindow1 and check the point on RootWindow0 that
94 // |window0| covers.
95 EXPECT_EQ(window0.get(), GetTarget(gfx::Point(-150, 50)));
[email protected]0518d0d2012-08-26 11:59:0496
97 // Start tracking from the RootWindow1 and check the point on RootWindow0 that
98 // neither |window0| nor |window1| covers.
[email protected]978a4a32012-10-05 19:51:0699 EXPECT_NE(window0.get(), GetTarget(gfx::Point(150, -50)));
100 EXPECT_NE(window1.get(), GetTarget(gfx::Point(150, -50)));
[email protected]0518d0d2012-08-26 11:59:04101
102 // Start tracking from the RootWindow1 and check the point on RootWindow1 that
103 // |window1| covers.
[email protected]978a4a32012-10-05 19:51:06104 EXPECT_EQ(window1.get(), GetTarget(gfx::Point(150, 150)));
[email protected]0518d0d2012-08-26 11:59:04105
106 // Start tracking from the RootWindow1 and check the point on RootWindow1 that
107 // neither |window0| nor |window1| covers.
[email protected]978a4a32012-10-05 19:51:06108 EXPECT_NE(window0.get(), GetTarget(gfx::Point(50, 50)));
109 EXPECT_NE(window1.get(), GetTarget(gfx::Point(50, 50)));
[email protected]0518d0d2012-08-26 11:59:04110}
111
dnicoara54f98652014-09-18 17:58:22112TEST_F(DragDropTrackerTest, ConvertEvent) {
113 if (!SupportsMultipleDisplays())
114 return;
[email protected]0518d0d2012-08-26 11:59:04115
dnicoara54f98652014-09-18 17:58:22116 UpdateDisplay("200x200,300x300");
[email protected]c9390bd2013-11-08 20:33:13117 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
[email protected]0518d0d2012-08-26 11:59:04118 EXPECT_EQ(2U, root_windows.size());
119
[email protected]0518d0d2012-08-26 11:59:04120 scoped_ptr<aura::Window> window0(
[email protected]5ebe6102012-11-28 21:00:03121 CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
[email protected]0518d0d2012-08-26 11:59:04122 window0->Show();
123
[email protected]0518d0d2012-08-26 11:59:04124 scoped_ptr<aura::Window> window1(
[email protected]96ea2282012-12-27 00:37:55125 CreateTestWindow(gfx::Rect(300, 100, 100, 100)));
[email protected]0518d0d2012-08-26 11:59:04126 window1->Show();
127
[email protected]978a4a32012-10-05 19:51:06128 // Make RootWindow0 active so that capture window is parented to it.
[email protected]d17642d2013-09-12 23:44:38129 Shell::GetInstance()->set_target_root_window(root_windows[0]);
[email protected]978a4a32012-10-05 19:51:06130
[email protected]0518d0d2012-08-26 11:59:04131 // Start tracking from the RootWindow0 and converts the mouse event into
132 // |window0|'s coodinates.
robert.bradfordb3d800b2015-02-18 18:56:10133 ui::MouseEvent original00(ui::ET_MOUSE_DRAGGED, gfx::Point(50, 50),
134 gfx::Point(50, 50), ui::EventTimeForNow(),
135 ui::EF_NONE, ui::EF_NONE);
[email protected]ffa4e4482012-11-28 12:11:49136 scoped_ptr<ui::LocatedEvent> converted00(ConvertEvent(window0.get(),
137 original00));
[email protected]0518d0d2012-08-26 11:59:04138 EXPECT_EQ(original00.type(), converted00->type());
[email protected]ae3dfe9c2012-09-20 00:53:43139 EXPECT_EQ("50,50", converted00->location().ToString());
140 EXPECT_EQ("50,50", converted00->root_location().ToString());
[email protected]0518d0d2012-08-26 11:59:04141 EXPECT_EQ(original00.flags(), converted00->flags());
142
143 // Start tracking from the RootWindow0 and converts the mouse event into
144 // |window1|'s coodinates.
robert.bradfordb3d800b2015-02-18 18:56:10145 ui::MouseEvent original01(ui::ET_MOUSE_DRAGGED, gfx::Point(350, 150),
146 gfx::Point(350, 150), ui::EventTimeForNow(),
147 ui::EF_NONE, ui::EF_NONE);
[email protected]ffa4e4482012-11-28 12:11:49148 scoped_ptr<ui::LocatedEvent> converted01(ConvertEvent(window1.get(),
149 original01));
[email protected]0518d0d2012-08-26 11:59:04150 EXPECT_EQ(original01.type(), converted01->type());
[email protected]ae3dfe9c2012-09-20 00:53:43151 EXPECT_EQ("50,50", converted01->location().ToString());
152 EXPECT_EQ("150,150", converted01->root_location().ToString());
[email protected]0518d0d2012-08-26 11:59:04153 EXPECT_EQ(original01.flags(), converted01->flags());
154
[email protected]978a4a32012-10-05 19:51:06155 // Make RootWindow1 active so that capture window is parented to it.
[email protected]d17642d2013-09-12 23:44:38156 Shell::GetInstance()->set_target_root_window(root_windows[1]);
[email protected]978a4a32012-10-05 19:51:06157
[email protected]0518d0d2012-08-26 11:59:04158 // Start tracking from the RootWindow1 and converts the mouse event into
159 // |window0|'s coodinates.
robert.bradfordb3d800b2015-02-18 18:56:10160 ui::MouseEvent original10(ui::ET_MOUSE_DRAGGED, gfx::Point(-150, 50),
161 gfx::Point(-150, 50), ui::EventTimeForNow(),
162 ui::EF_NONE, ui::EF_NONE);
[email protected]ffa4e4482012-11-28 12:11:49163 scoped_ptr<ui::LocatedEvent> converted10(ConvertEvent(window0.get(),
164 original10));
[email protected]0518d0d2012-08-26 11:59:04165 EXPECT_EQ(original10.type(), converted10->type());
[email protected]ae3dfe9c2012-09-20 00:53:43166 EXPECT_EQ("50,50", converted10->location().ToString());
167 EXPECT_EQ("50,50", converted10->root_location().ToString());
[email protected]0518d0d2012-08-26 11:59:04168 EXPECT_EQ(original10.flags(), converted10->flags());
169
170 // Start tracking from the RootWindow1 and converts the mouse event into
171 // |window1|'s coodinates.
robert.bradfordb3d800b2015-02-18 18:56:10172 ui::MouseEvent original11(ui::ET_MOUSE_DRAGGED, gfx::Point(150, 150),
173 gfx::Point(150, 150), ui::EventTimeForNow(),
174 ui::EF_NONE, ui::EF_NONE);
[email protected]ffa4e4482012-11-28 12:11:49175 scoped_ptr<ui::LocatedEvent> converted11(ConvertEvent(window1.get(),
[email protected]0518d0d2012-08-26 11:59:04176 original11));
177 EXPECT_EQ(original11.type(), converted11->type());
[email protected]ae3dfe9c2012-09-20 00:53:43178 EXPECT_EQ("50,50", converted11->location().ToString());
179 EXPECT_EQ("150,150", converted11->root_location().ToString());
[email protected]0518d0d2012-08-26 11:59:04180 EXPECT_EQ(original11.flags(), converted11->flags());
181}
182
183} // namespace test
184} // namespace aura