| 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 CreateWindowResizer( |
| 95 window, |
| 96 point_in_parent, |
| 97 window_component, |
| 98 aura::client::WINDOW_MOVE_SOURCE_MOUSE).release(); |
| 99 } |
| 100 |
| 101 void DragStart(aura::Window* window) { |
| 102 initial_location_in_parent_ = window->bounds().origin(); |
| 103 resizer_.reset(CreateSomeWindowResizer(window, |
| 104 initial_location_in_parent_, |
| 105 HTCAPTION)); |
| 106 ASSERT_TRUE(resizer_.get()); |
| 107 } |
| 108 |
| 109 void DragStartAtOffsetFromwindowOrigin(aura::Window* window, |
| 110 int dx, |
| 111 int dy) { |
| 112 initial_location_in_parent_ = |
| 113 window->bounds().origin() + gfx::Vector2d(dx, dy); |
| 114 resizer_.reset(CreateSomeWindowResizer(window, |
| 115 initial_location_in_parent_, |
| 116 HTCAPTION)); |
| 117 ASSERT_TRUE(resizer_.get()); |
| 118 } |
| 119 |
| 120 void DragMove(int dx, int dy) { |
| 121 resizer_->Drag(initial_location_in_parent_ + gfx::Vector2d(dx, dy), 0); |
| 122 } |
| 123 |
| 124 void DragEnd() { |
| 125 resizer_->CompleteDrag(0); |
| 126 resizer_.reset(); |
| 127 } |
| 128 |
| 129 void DragRevert() { |
| 130 resizer_->RevertDrag(); |
| 131 resizer_.reset(); |
| 132 } |
| 133 |
| 134 // Panels are parented by panel container during drags. |
| 135 // Docked windows are parented by dock container during drags. |
| 136 // All other windows that we are testing here have workspace as a parent. |
| 137 int CorrectContainerIdDuringDrag(DockedState is_docked) { |
| 138 if (window_type_ == aura::client::WINDOW_TYPE_PANEL) |
| 139 return internal::kShellWindowId_PanelContainer; |
| 140 if (is_docked == DOCKED) |
| 141 return internal::kShellWindowId_DockedContainer; |
| 142 return internal::kShellWindowId_WorkspaceContainer; |
| 143 } |
| 144 |
| 145 // Test dragging the window vertically (to detach if it is a panel) and then |
| 146 // horizontally to the edge with an added offset from the edge of |dx|. |
| 147 void DragRelativeToEdge(DockedEdge edge, |
| 148 aura::Window* window, |
| 149 int dx) { |
| 150 DragVerticallyAndRelativeToEdge( |
| 151 edge, |
| 152 window, |
| 153 dx, |
| 154 window_type_ == aura::client::WINDOW_TYPE_PANEL ? -100 : 20); |
| 155 } |
| 156 |
| 157 // Detach if our window is a panel, then drag it vertically by |dy| and |
| 158 // horizontally to the edge with an added offset from the edge of |dx|. |
| 159 void DragVerticallyAndRelativeToEdge(DockedEdge edge, |
| 160 aura::Window* window, |
| 161 int dx, |
| 162 int dy) { |
| 163 aura::RootWindow* root_window = window->GetRootWindow(); |
| 164 gfx::Rect initial_bounds = window->GetBoundsInScreen(); |
| 165 |
| 166 if (window_type_ == aura::client::WINDOW_TYPE_PANEL) { |
| 167 ASSERT_NO_FATAL_FAILURE(DragStart(window)); |
| 168 EXPECT_TRUE(window->GetProperty(kPanelAttachedKey)); |
| 169 |
| 170 // Drag enough to detach since our tests assume panels to be initially |
| 171 // detached. |
| 172 DragMove(0, dy); |
| 173 EXPECT_EQ(CorrectContainerIdDuringDrag(UNDOCKED), window->parent()->id()); |
| 174 EXPECT_EQ(initial_bounds.x(), window->GetBoundsInScreen().x()); |
| 175 EXPECT_EQ(initial_bounds.y() + dy, window->GetBoundsInScreen().y()); |
| 176 |
| 177 // The panel should be detached when the drag completes. |
| 178 DragEnd(); |
| 179 |
| 180 EXPECT_FALSE(window->GetProperty(kPanelAttachedKey)); |
| 181 EXPECT_EQ(internal::kShellWindowId_WorkspaceContainer, |
| 182 window->parent()->id()); |
| 183 EXPECT_EQ(root_window, window->GetRootWindow()); |
| 184 } |
| 185 |
| 186 // avoid snap by clicking away from the border |
| 187 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromwindowOrigin(window, 5, 5)); |
| 188 |
| 189 // Drag the window left or right to the edge (or almost to it). |
| 190 if (edge == DOCKED_EDGE_LEFT) |
| 191 dx += window->GetRootWindow()->bounds().x() - initial_bounds.x(); |
| 192 else if (edge == DOCKED_EDGE_RIGHT) |
| 193 dx += window->GetRootWindow()->bounds().right() - initial_bounds.right(); |
| 194 DragMove(dx, window_type_ == aura::client::WINDOW_TYPE_PANEL ? 0 : dy); |
| 195 EXPECT_EQ(CorrectContainerIdDuringDrag(UNDOCKED), window->parent()->id()); |
| 196 // Release the mouse and the panel should be attached to the dock. |
| 197 DragEnd(); |
| 198 |
| 199 // x-coordinate can get adjusted by snapping or sticking. |
| 200 // y-coordinate should not change by possible docking. |
| 201 EXPECT_EQ(initial_bounds.y() + dy, window->GetBoundsInScreen().y()); |
| 202 } |
| 203 |
| 204 private: |
| 205 scoped_ptr<WindowResizer> resizer_; |
| 206 scoped_ptr<test::LauncherViewTestAPI> launcher_view_test_; |
| 207 aura::client::WindowType window_type_; |
| 208 |
| 209 // Location at start of the drag in |window->parent()|'s coordinates. |
| 210 gfx::Point initial_location_in_parent_; |
| 211 |
| 212 DISALLOW_COPY_AND_ASSIGN(DockedWindowLayoutManagerTest); |
| 213 }; |
| 214 |
| 215 // Tests that a created window is successfully added to the dock |
| 216 // layout manager. |
| 217 TEST_P(DockedWindowLayoutManagerTest, AddOneWindow) { |
| 218 gfx::Rect bounds(0, 0, 201, 201); |
| 219 scoped_ptr<aura::Window> window(CreateTestWindow(bounds)); |
| 220 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0); |
| 221 |
| 222 // The window should be attached and snapped to the right side of the screen. |
| 223 EXPECT_EQ(window->GetRootWindow()->bounds().right(), |
| 224 window->GetBoundsInScreen().right()); |
| 225 EXPECT_EQ(internal::kShellWindowId_DockedContainer, window->parent()->id()); |
| 226 } |
| 227 |
| 228 //TODO(varkha): Add more tests for fanning windows in the dock. |
| 229 // See http://crbug.com/233334. |
| 230 |
| 231 // Tests run twice - on both panels and normal windows |
| 232 INSTANTIATE_TEST_CASE_P(NormalOrPanel, |
| 233 DockedWindowLayoutManagerTest, |
| 234 testing::Values(aura::client::WINDOW_TYPE_NORMAL, |
| 235 aura::client::WINDOW_TYPE_PANEL)); |
| 236 } // namespace internal |
| 237 } // namespace ash |
| OLD | NEW |