Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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/root_window.h" |
| 12 | #include "ui/aura/test/test_windows.h" |
| 13 | #include "ui/aura/window.h" |
| 14 | |
| 15 | namespace ash { |
| 16 | namespace test { |
| 17 | |
| 18 | class DragDropTrackerTest : public test::AshTestBase { |
| 19 | public: |
| 20 | virtual void SetUp() OVERRIDE { |
| 21 | AshTestBase::SetUp(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 22 | UpdateDisplay("200x200,300x300"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 25 | aura::Window* CreateTestWindow(const gfx::Rect& bounds) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 26 | static int window_id = 0; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 27 | return CreateTestWindowInShellWithDelegate( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 28 | aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(), |
| 29 | window_id++, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 30 | bounds); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static aura::Window* GetTarget(const gfx::Point& location) { |
| 34 | scoped_ptr<internal::DragDropTracker> tracker( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 35 | new internal::DragDropTracker(Shell::GetPrimaryRootWindow(), |
| 36 | NULL)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 37 | ui::MouseEvent e(ui::ET_MOUSE_DRAGGED, |
| 38 | location, |
| 39 | location, |
| 40 | ui::EF_NONE); |
| 41 | aura::Window* target = tracker->GetTarget(e); |
| 42 | return target; |
| 43 | } |
| 44 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 45 | static ui::LocatedEvent* ConvertEvent(aura::Window* target, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 46 | const ui::MouseEvent& event) { |
| 47 | scoped_ptr<internal::DragDropTracker> tracker( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 48 | new internal::DragDropTracker(Shell::GetPrimaryRootWindow(), |
| 49 | NULL)); |
| 50 | ui::LocatedEvent* converted = tracker->ConvertEvent(target, event); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 51 | return converted; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | // TODO(mazda): Remove this once ash/wm/coordinate_conversion.h supports |
| 56 | // non-X11 platforms. |
| 57 | #if defined(USE_X11) |
| 58 | #define MAYBE_GetTarget GetTarget |
| 59 | #else |
| 60 | #define MAYBE_GetTarget DISABLED_GetTarget |
| 61 | #endif |
| 62 | |
| 63 | TEST_F(DragDropTrackerTest, MAYBE_GetTarget) { |
| 64 | Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
| 65 | EXPECT_EQ(2U, root_windows.size()); |
| 66 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 67 | scoped_ptr<aura::Window> window0( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 68 | CreateTestWindow(gfx::Rect(0, 0, 100, 100))); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 69 | window0->Show(); |
| 70 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 71 | scoped_ptr<aura::Window> window1( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 72 | CreateTestWindow(gfx::Rect(300, 100, 100, 100))); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 73 | window1->Show(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 74 | EXPECT_EQ(root_windows[0], window0->GetRootWindow()); |
| 75 | EXPECT_EQ(root_windows[1], window1->GetRootWindow()); |
| 76 | EXPECT_EQ("0,0 100x100", window0->GetBoundsInScreen().ToString()); |
| 77 | EXPECT_EQ("300,100 100x100", window1->GetBoundsInScreen().ToString()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 78 | |
| 79 | // Make RootWindow0 active so that capture window is parented to it. |
| 80 | Shell::GetInstance()->set_active_root_window(root_windows[0]); |
| 81 | |
| 82 | // Start tracking from the RootWindow1 and check the point on RootWindow0 that |
| 83 | // |window0| covers. |
| 84 | EXPECT_EQ(window0.get(), GetTarget(gfx::Point(50, 50))); |
| 85 | |
| 86 | // Start tracking from the RootWindow0 and check the point on RootWindow0 that |
| 87 | // neither |window0| nor |window1| covers. |
| 88 | EXPECT_NE(window0.get(), GetTarget(gfx::Point(150, 150))); |
| 89 | EXPECT_NE(window1.get(), GetTarget(gfx::Point(150, 150))); |
| 90 | |
| 91 | // Start tracking from the RootWindow0 and check the point on RootWindow1 that |
| 92 | // |window1| covers. |
| 93 | EXPECT_EQ(window1.get(), GetTarget(gfx::Point(350, 150))); |
| 94 | |
| 95 | // Start tracking from the RootWindow0 and check the point on RootWindow1 that |
| 96 | // neither |window0| nor |window1| covers. |
| 97 | EXPECT_NE(window0.get(), GetTarget(gfx::Point(50, 250))); |
| 98 | EXPECT_NE(window1.get(), GetTarget(gfx::Point(50, 250))); |
| 99 | |
| 100 | // Make RootWindow1 active so that capture window is parented to it. |
| 101 | Shell::GetInstance()->set_active_root_window(root_windows[1]); |
| 102 | |
| 103 | // Start tracking from the RootWindow1 and check the point on RootWindow0 that |
| 104 | // |window0| covers. |
| 105 | EXPECT_EQ(window0.get(), GetTarget(gfx::Point(-150, 50))); |
| 106 | |
| 107 | // Start tracking from the RootWindow1 and check the point on RootWindow0 that |
| 108 | // neither |window0| nor |window1| covers. |
| 109 | EXPECT_NE(window0.get(), GetTarget(gfx::Point(150, -50))); |
| 110 | EXPECT_NE(window1.get(), GetTarget(gfx::Point(150, -50))); |
| 111 | |
| 112 | // Start tracking from the RootWindow1 and check the point on RootWindow1 that |
| 113 | // |window1| covers. |
| 114 | EXPECT_EQ(window1.get(), GetTarget(gfx::Point(150, 150))); |
| 115 | |
| 116 | // Start tracking from the RootWindow1 and check the point on RootWindow1 that |
| 117 | // neither |window0| nor |window1| covers. |
| 118 | EXPECT_NE(window0.get(), GetTarget(gfx::Point(50, 50))); |
| 119 | EXPECT_NE(window1.get(), GetTarget(gfx::Point(50, 50))); |
| 120 | } |
| 121 | |
| 122 | // TODO(mazda): Remove this once ash/wm/coordinate_conversion.h supports |
| 123 | // non-X11 platforms. |
| 124 | #if defined(USE_X11) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 125 | #define MAYBE_ConvertEvent ConvertEvent |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 126 | #else |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 127 | #define MAYBE_ConvertEvent DISABLED_ConvertEvent |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 128 | #endif |
| 129 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 130 | TEST_F(DragDropTrackerTest, MAYBE_ConvertEvent) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 131 | Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
| 132 | EXPECT_EQ(2U, root_windows.size()); |
| 133 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 134 | scoped_ptr<aura::Window> window0( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 135 | CreateTestWindow(gfx::Rect(0, 0, 100, 100))); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 136 | window0->Show(); |
| 137 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 138 | scoped_ptr<aura::Window> window1( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 139 | CreateTestWindow(gfx::Rect(300, 100, 100, 100))); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 140 | window1->Show(); |
| 141 | |
| 142 | // Make RootWindow0 active so that capture window is parented to it. |
| 143 | Shell::GetInstance()->set_active_root_window(root_windows[0]); |
| 144 | |
| 145 | // Start tracking from the RootWindow0 and converts the mouse event into |
| 146 | // |window0|'s coodinates. |
| 147 | ui::MouseEvent original00(ui::ET_MOUSE_DRAGGED, |
| 148 | gfx::Point(50, 50), |
| 149 | gfx::Point(50, 50), |
| 150 | ui::EF_NONE); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 151 | scoped_ptr<ui::LocatedEvent> converted00(ConvertEvent(window0.get(), |
| 152 | original00)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 153 | EXPECT_EQ(original00.type(), converted00->type()); |
| 154 | EXPECT_EQ("50,50", converted00->location().ToString()); |
| 155 | EXPECT_EQ("50,50", converted00->root_location().ToString()); |
| 156 | EXPECT_EQ(original00.flags(), converted00->flags()); |
| 157 | |
| 158 | // Start tracking from the RootWindow0 and converts the mouse event into |
| 159 | // |window1|'s coodinates. |
| 160 | ui::MouseEvent original01(ui::ET_MOUSE_DRAGGED, |
| 161 | gfx::Point(350, 150), |
| 162 | gfx::Point(350, 150), |
| 163 | ui::EF_NONE); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 164 | scoped_ptr<ui::LocatedEvent> converted01(ConvertEvent(window1.get(), |
| 165 | original01)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 166 | EXPECT_EQ(original01.type(), converted01->type()); |
| 167 | EXPECT_EQ("50,50", converted01->location().ToString()); |
| 168 | EXPECT_EQ("150,150", converted01->root_location().ToString()); |
| 169 | EXPECT_EQ(original01.flags(), converted01->flags()); |
| 170 | |
| 171 | // Make RootWindow1 active so that capture window is parented to it. |
| 172 | Shell::GetInstance()->set_active_root_window(root_windows[1]); |
| 173 | |
| 174 | // Start tracking from the RootWindow1 and converts the mouse event into |
| 175 | // |window0|'s coodinates. |
| 176 | ui::MouseEvent original10(ui::ET_MOUSE_DRAGGED, |
| 177 | gfx::Point(-150, 50), |
| 178 | gfx::Point(-150, 50), |
| 179 | ui::EF_NONE); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 180 | scoped_ptr<ui::LocatedEvent> converted10(ConvertEvent(window0.get(), |
| 181 | original10)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 182 | EXPECT_EQ(original10.type(), converted10->type()); |
| 183 | EXPECT_EQ("50,50", converted10->location().ToString()); |
| 184 | EXPECT_EQ("50,50", converted10->root_location().ToString()); |
| 185 | EXPECT_EQ(original10.flags(), converted10->flags()); |
| 186 | |
| 187 | // Start tracking from the RootWindow1 and converts the mouse event into |
| 188 | // |window1|'s coodinates. |
| 189 | ui::MouseEvent original11(ui::ET_MOUSE_DRAGGED, |
| 190 | gfx::Point(150, 150), |
| 191 | gfx::Point(150, 150), |
| 192 | ui::EF_NONE); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 193 | scoped_ptr<ui::LocatedEvent> converted11(ConvertEvent(window1.get(), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 194 | original11)); |
| 195 | EXPECT_EQ(original11.type(), converted11->type()); |
| 196 | EXPECT_EQ("50,50", converted11->location().ToString()); |
| 197 | EXPECT_EQ("150,150", converted11->root_location().ToString()); |
| 198 | EXPECT_EQ(original11.flags(), converted11->flags()); |
| 199 | } |
| 200 | |
| 201 | } // namespace test |
| 202 | } // namespace aura |