| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013 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/wm/dock/docked_window_layout_manager.h" |
| 6 |
| 7 #include "ash/ash_switches.h" |
| 8 #include "ash/launcher/launcher.h" |
| 9 #include "ash/launcher/launcher_model.h" |
| 10 #include "ash/root_window_controller.h" |
| 11 #include "ash/shelf/shelf_layout_manager.h" |
| 12 #include "ash/shelf/shelf_types.h" |
| 13 #include "ash/shelf/shelf_widget.h" |
| 14 #include "ash/shell.h" |
| 15 #include "ash/shell_window_ids.h" |
| 16 #include "ash/test/ash_test_base.h" |
| 17 #include "ash/test/launcher_view_test_api.h" |
| 18 #include "ash/test/shell_test_api.h" |
| 19 #include "ash/test/test_launcher_delegate.h" |
| 20 #include "ash/wm/panels/panel_layout_manager.h" |
| 21 #include "ash/wm/window_properties.h" |
| 22 #include "ash/wm/window_resizer.h" |
| 23 #include "base/basictypes.h" |
| 24 #include "base/command_line.h" |
| 25 #include "ui/aura/client/aura_constants.h" |
| 26 #include "ui/aura/root_window.h" |
| 27 #include "ui/aura/window.h" |
| 28 #include "ui/base/hit_test.h" |
| 29 #include "ui/views/widget/widget.h" |
| 30 |
| 31 namespace ash { |
| 32 namespace internal { |
| 33 |
| 34 class DockedWindowLayoutManagerTest |
| 35 : public test::AshTestBase, |
| 36 public testing::WithParamInterface<aura::client::WindowType> { |
| 37 public: |
| 38 DockedWindowLayoutManagerTest() : window_type_(GetParam()) {} |
| 39 virtual ~DockedWindowLayoutManagerTest() {} |
| 40 |
| 41 virtual void SetUp() OVERRIDE { |
| 42 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 43 ash::switches::kAshEnableStickyEdges); |
| 44 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 45 ash::switches::kAshEnableDockedWindows); |
| 46 AshTestBase::SetUp(); |
| 47 UpdateDisplay("600x400"); |
| 48 ASSERT_TRUE(test::TestLauncherDelegate::instance()); |
| 49 |
| 50 launcher_view_test_.reset(new test::LauncherViewTestAPI( |
| 51 Launcher::ForPrimaryDisplay()->GetLauncherViewForTest())); |
| 52 launcher_view_test_->SetAnimationDuration(1); |
| 53 } |
| 54 |
| 55 protected: |
| 56 enum DockedEdge { |
| 57 DOCKED_EDGE_NONE, |
| 58 DOCKED_EDGE_LEFT, |
| 59 DOCKED_EDGE_RIGHT, |
| 60 }; |
| 61 |
| 62 enum DockedState { |
| 63 UNDOCKED, |
| 64 DOCKED, |
| 65 }; |
| 66 |
| 67 aura::Window* CreateTestWindow(const gfx::Rect& bounds) { |
| 68 aura::Window* window = CreateTestWindowInShellWithDelegateAndType( |
| 69 NULL, |
| 70 window_type_, |
| 71 0, |
| 72 bounds); |
| 73 if (window_type_ == aura::client::WINDOW_TYPE_PANEL) { |
| 74 test::TestLauncherDelegate* launcher_delegate = |
| 75 test::TestLauncherDelegate::instance(); |
| 76 launcher_delegate->AddLauncherItem(window); |
| 77 PanelLayoutManager* manager = |
| 78 static_cast<PanelLayoutManager*>(GetPanelContainer(window)-> |
| 79 layout_manager()); |
| 80 manager->Relayout(); |
| 81 } |
| 82 return window; |
| 83 } |
| 84 |
| 85 aura::Window* GetPanelContainer(aura::Window* panel) { |
| 86 return Shell::GetContainer(panel->GetRootWindow(), |
| 87 internal::kShellWindowId_PanelContainer); |
| 88 } |
| 89 |
| 90 static WindowResizer* CreateSomeWindowResizer( |
| 91 aura::Window* window, |
| 92 const gfx::Point& point_in_parent, |
| 93 int window_component) { |
| 94 return static_cast<WindowResizer*>(CreateWindowResizer( |
| 95 window, point_in_parent, window_component).release()); |
| 96 } |
| 97 |
| 98 void DragStart(aura::Window* window) { |
| 99 initial_location_in_parent_ = window->bounds().origin(); |
| 100 resizer_.reset(CreateSomeWindowResizer(window, |
| 101 initial_location_in_parent_, |
| 102 HTCAPTION)); |
| 103 ASSERT_TRUE(resizer_.get()); |
| 104 } |
| 105 |
| 106 void DragStartAtOffsetFromwindowOrigin(aura::Window* window, |
| 107 int dx, |
| 108 int dy) { |
| 109 initial_location_in_parent_ = |
| 110 window->bounds().origin() + gfx::Vector2d(dx, dy); |
| 111 resizer_.reset(CreateSomeWindowResizer(window, |
| 112 initial_location_in_parent_, |
| 113 HTCAPTION)); |
| 114 ASSERT_TRUE(resizer_.get()); |
| 115 } |
| 116 |
| 117 void DragMove(int dx, int dy) { |
| 118 resizer_->Drag(initial_location_in_parent_ + gfx::Vector2d(dx, dy), 0); |
| 119 } |
| 120 |
| 121 void DragEnd() { |
| 122 resizer_->CompleteDrag(0); |
| 123 resizer_.reset(); |
| 124 } |
| 125 |
| 126 void DragRevert() { |
| 127 resizer_->RevertDrag(); |
| 128 resizer_.reset(); |
| 129 } |
| 130 |
| 131 // Panels are parented by panel container during drags. |
| 132 // Docked windows are parented by dock container during drags. |
| 133 // All other windows that we are testing here have workspace as a parent. |
| 134 int CorrectContainerIdDuringDrag(DockedState is_docked) { |
| 135 if (window_type_ == aura::client::WINDOW_TYPE_PANEL) |
| 136 return internal::kShellWindowId_PanelContainer; |
| 137 if (is_docked == DOCKED) |
| 138 return internal::kShellWindowId_DockContainer; |
| 139 return internal::kShellWindowId_WorkspaceContainer; |
| 140 } |
| 141 |
| 142 // Test dragging the window vertically (to detach if it is a panel) and then |
| 143 // horizontally to the edge with an added offset from the edge of |dx|. |
| 144 void DragRelativeToEdge(DockedEdge edge, |
| 145 aura::Window* window, |
| 146 int dx) { |
| 147 DragVerticallyAndRelativeToEdge( |
| 148 edge, |
| 149 window, |
| 150 dx, |
| 151 window_type_ == aura::client::WINDOW_TYPE_PANEL ? -100 : 20); |
| 152 } |
| 153 |
| 154 // Detach if our window is a panel, then drag it vertically by |dy| and |
| 155 // horizontally to the edge with an added offset from the edge of |dx|. |
| 156 void DragVerticallyAndRelativeToEdge(DockedEdge edge, |
| 157 aura::Window* window, |
| 158 int dx, |
| 159 int dy) { |
| 160 aura::RootWindow* root_window = window->GetRootWindow(); |
| 161 gfx::Rect initial_bounds = window->GetBoundsInScreen(); |
| 162 |
| 163 if (window_type_ == aura::client::WINDOW_TYPE_PANEL) { |
| 164 DragStart(window); |
| 165 EXPECT_TRUE(window->GetProperty(kPanelAttachedKey)); |
| 166 |
| 167 // Drag enough to detach since our tests assume panels to be initially |
| 168 // detached. |
| 169 DragMove(0, dy); |
| 170 EXPECT_EQ(CorrectContainerIdDuringDrag(UNDOCKED), window->parent()->id()); |
| 171 EXPECT_EQ(initial_bounds.x(), window->GetBoundsInScreen().x()); |
| 172 EXPECT_EQ(initial_bounds.y() + dy, window->GetBoundsInScreen().y()); |
| 173 |
| 174 // The panel should be detached when the drag completes. |
| 175 DragEnd(); |
| 176 |
| 177 EXPECT_FALSE(window->GetProperty(kPanelAttachedKey)); |
| 178 EXPECT_EQ(internal::kShellWindowId_WorkspaceContainer, |
| 179 window->parent()->id()); |
| 180 EXPECT_EQ(root_window, window->GetRootWindow()); |
| 181 } |
| 182 |
| 183 // avoid snap by clicking away from the border |
| 184 DragStartAtOffsetFromwindowOrigin(window, 5, 5); |
| 185 |
| 186 // Drag the window left or right to the edge (or almost to it). |
| 187 if (edge == DOCKED_EDGE_LEFT) |
| 188 dx += window->GetRootWindow()->bounds().x() - initial_bounds.x(); |
| 189 else if (edge == DOCKED_EDGE_RIGHT) |
| 190 dx += window->GetRootWindow()->bounds().right() - initial_bounds.right(); |
| 191 DragMove(dx, window_type_ == aura::client::WINDOW_TYPE_PANEL ? 0 : dy); |
| 192 EXPECT_EQ(CorrectContainerIdDuringDrag(UNDOCKED), window->parent()->id()); |
| 193 // Release the mouse and the panel should be attached to the dock. |
| 194 DragEnd(); |
| 195 |
| 196 // x-coordinate can get adjusted by snapping or sticking. |
| 197 // y-coordinate should not change by possible docking. |
| 198 EXPECT_EQ(initial_bounds.y() + dy, window->GetBoundsInScreen().y()); |
| 199 } |
| 200 |
| 201 private: |
| 202 scoped_ptr<WindowResizer> resizer_; |
| 203 scoped_ptr<test::LauncherViewTestAPI> launcher_view_test_; |
| 204 aura::client::WindowType window_type_; |
| 205 |
| 206 // Location at start of the drag in |window->parent()|'s coordinates. |
| 207 gfx::Point initial_location_in_parent_; |
| 208 |
| 209 DISALLOW_COPY_AND_ASSIGN(DockedWindowLayoutManagerTest); |
| 210 }; |
| 211 |
| 212 // Tests that a created window is successfully added to the dock |
| 213 // layout manager. |
| 214 TEST_P(DockedWindowLayoutManagerTest, AddOneWindow) { |
| 215 gfx::Rect bounds(0, 0, 201, 201); |
| 216 scoped_ptr<aura::Window> window(CreateTestWindow(bounds)); |
| 217 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0); |
| 218 |
| 219 // The window should be attached and snapped to the right side of the screen. |
| 220 EXPECT_EQ(window->GetRootWindow()->bounds().right(), |
| 221 window->GetBoundsInScreen().right()); |
| 222 EXPECT_EQ(internal::kShellWindowId_DockContainer, window->parent()->id()); |
| 223 } |
| 224 |
| 225 //TODO(varkha): Add more tests for fanning windows in the dock. |
| 226 // See http://crbug.com/233334. |
| 227 |
| 228 // Tests run twice - on both panels and normal windows |
| 229 INSTANTIATE_TEST_CASE_P(NormalOrPanel, |
| 230 DockedWindowLayoutManagerTest, |
| 231 testing::Values(aura::client::WINDOW_TYPE_NORMAL, |
| 232 aura::client::WINDOW_TYPE_PANEL)); |
| 233 } // namespace internal |
| 234 } // namespace ash |
| OLD | NEW |