Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(672)

Side by Side Diff: ash/wm/dock/docked_window_resizer_unittest.cc

Issue 2700523004: Remove docked windows entirely in M59. (Closed)
Patch Set: Rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/common/wm/dock/docked_window_resizer.h"
6
7 #include "ash/common/ash_switches.h"
8 #include "ash/common/shelf/shelf_widget.h"
9 #include "ash/common/shelf/wm_shelf.h"
10 #include "ash/common/test/test_shelf_delegate.h"
11 #include "ash/common/wm/dock/docked_window_layout_manager.h"
12 #include "ash/common/wm/panels/panel_layout_manager.h"
13 #include "ash/common/wm/window_state.h"
14 #include "ash/common/wm/wm_event.h"
15 #include "ash/common/wm_shell.h"
16 #include "ash/common/wm_window.h"
17 #include "ash/display/window_tree_host_manager.h"
18 #include "ash/public/cpp/shelf_types.h"
19 #include "ash/public/cpp/shell_window_ids.h"
20 #include "ash/public/cpp/window_properties.h"
21 #include "ash/root_window_controller.h"
22 #include "ash/screen_util.h"
23 #include "ash/shell.h"
24 #include "ash/test/ash_test_base.h"
25 #include "ash/test/cursor_manager_test_api.h"
26 #include "ash/wm/drag_window_resizer.h"
27 #include "ash/wm/window_state_aura.h"
28 #include "ash/wm/window_util.h"
29 #include "base/command_line.h"
30 #include "ui/aura/client/aura_constants.h"
31 #include "ui/aura/client/window_parenting_client.h"
32 #include "ui/aura/test/test_window_delegate.h"
33 #include "ui/aura/window_event_dispatcher.h"
34 #include "ui/base/hit_test.h"
35 #include "ui/base/ui_base_types.h"
36 #include "ui/events/test/event_generator.h"
37 #include "ui/views/widget/widget.h"
38 #include "ui/wm/core/coordinate_conversion.h"
39 #include "ui/wm/core/window_util.h"
40
41 namespace ash {
42
43 class DockedWindowResizerTest
44 : public test::AshTestBase,
45 public testing::WithParamInterface<ui::wm::WindowType> {
46 public:
47 DockedWindowResizerTest() : window_type_(GetParam()) {}
48 virtual ~DockedWindowResizerTest() {}
49
50 void SetUp() override {
51 base::CommandLine::ForCurrentProcess()->AppendSwitch(
52 ash::switches::kAshEnableDockedWindows);
53 AshTestBase::SetUp();
54 UpdateDisplay("600x400");
55 }
56
57 void TearDown() override { AshTestBase::TearDown(); }
58
59 protected:
60 enum DockedEdge {
61 DOCKED_EDGE_NONE,
62 DOCKED_EDGE_LEFT,
63 DOCKED_EDGE_RIGHT,
64 };
65
66 int ideal_width() const { return DockedWindowLayoutManager::kIdealWidth; }
67 int min_dock_gap() const { return DockedWindowLayoutManager::kMinDockGap; }
68 int max_width() const { return DockedWindowLayoutManager::kMaxDockWidth; }
69 int docked_width(const DockedWindowLayoutManager* layout_manager) const {
70 return layout_manager->docked_width_;
71 }
72 int docked_alignment(const DockedWindowLayoutManager* layout_manager) const {
73 return layout_manager->alignment_;
74 }
75 aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
76 aura::Window* window = CreateTestWindowInShellWithDelegateAndType(
77 &delegate_, window_type_, 0, bounds);
78 if (window_type_ == ui::wm::WINDOW_TYPE_PANEL) {
79 WmWindow* wm_window = WmWindow::Get(window);
80 test::TestShelfDelegate::instance()->AddShelfItem(wm_window);
81 PanelLayoutManager::Get(wm_window)->Relayout();
82 }
83 return window;
84 }
85
86 aura::Window* CreateModalWindow(const gfx::Rect& bounds) {
87 aura::Window* window = new aura::Window(&delegate_);
88 window->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_SYSTEM);
89 window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
90 window->Init(ui::LAYER_TEXTURED);
91 window->Show();
92
93 if (bounds.IsEmpty()) {
94 ParentWindowInPrimaryRootWindow(window);
95 } else {
96 display::Display display =
97 display::Screen::GetScreen()->GetDisplayMatching(bounds);
98 aura::Window* root = ash::Shell::GetInstance()
99 ->window_tree_host_manager()
100 ->GetRootWindowForDisplayId(display.id());
101 gfx::Point origin = bounds.origin();
102 ::wm::ConvertPointFromScreen(root, &origin);
103 window->SetBounds(gfx::Rect(origin, bounds.size()));
104 aura::client::ParentWindowWithContext(window, root, bounds);
105 }
106 return window;
107 }
108
109 static WindowResizer* CreateSomeWindowResizer(
110 aura::Window* window,
111 const gfx::Point& point_in_parent,
112 int window_component) {
113 return CreateWindowResizer(WmWindow::Get(window), point_in_parent,
114 window_component,
115 aura::client::WINDOW_MOVE_SOURCE_MOUSE)
116 .release();
117 }
118
119 void DragStart(aura::Window* window) {
120 DragStartAtOffsetFromWindowOrigin(window, 0, 0);
121 }
122
123 void DragStartAtOffsetFromWindowOrigin(aura::Window* window, int dx, int dy) {
124 initial_location_in_parent_ =
125 window->bounds().origin() + gfx::Vector2d(dx, dy);
126 resizer_.reset(CreateSomeWindowResizer(window, initial_location_in_parent_,
127 HTCAPTION));
128 ASSERT_TRUE(resizer_.get());
129 }
130
131 void ResizeStartAtOffsetFromWindowOrigin(aura::Window* window,
132 int dx,
133 int dy,
134 int window_component) {
135 initial_location_in_parent_ =
136 window->bounds().origin() + gfx::Vector2d(dx, dy);
137 resizer_.reset(CreateSomeWindowResizer(window, initial_location_in_parent_,
138 window_component));
139 ASSERT_TRUE(resizer_.get());
140 }
141
142 void DragMove(int dx, int dy) {
143 resizer_->Drag(initial_location_in_parent_ + gfx::Vector2d(dx, dy), 0);
144 }
145
146 void DragEnd() {
147 resizer_->CompleteDrag();
148 resizer_.reset();
149 }
150
151 void DragRevert() {
152 resizer_->RevertDrag();
153 resizer_.reset();
154 }
155
156 // Panels are parented by panel container during drags.
157 // All other windows that are tested here are parented by dock container
158 // during drags.
159 int CorrectContainerIdDuringDrag() {
160 if (window_type_ == ui::wm::WINDOW_TYPE_PANEL)
161 return kShellWindowId_PanelContainer;
162 return kShellWindowId_DockedContainer;
163 }
164
165 // Test dragging the window vertically (to detach if it is a panel) and then
166 // horizontally to the edge with an added offset from the edge of |dx|.
167 void DragRelativeToEdge(DockedEdge edge, aura::Window* window, int dx) {
168 DragVerticallyAndRelativeToEdge(
169 edge, window, dx, window_type_ == ui::wm::WINDOW_TYPE_PANEL ? -100 : 20,
170 25, 5);
171 }
172
173 void DragToVerticalPositionAndToEdge(DockedEdge edge,
174 aura::Window* window,
175 int y) {
176 DragToVerticalPositionRelativeToEdge(edge, window, 0, y);
177 }
178
179 void DragToVerticalPositionRelativeToEdge(DockedEdge edge,
180 aura::Window* window,
181 int dx,
182 int y) {
183 gfx::Rect initial_bounds = window->GetBoundsInScreen();
184 DragVerticallyAndRelativeToEdge(edge, window, dx, y - initial_bounds.y(),
185 25, 5);
186 }
187
188 // Detach if our window is a panel, then drag it vertically by |dy| and
189 // horizontally to the edge with an added offset from the edge of |dx|.
190 void DragVerticallyAndRelativeToEdge(DockedEdge edge,
191 aura::Window* window,
192 int dx,
193 int dy,
194 int grab_x,
195 int grab_y) {
196 gfx::Rect initial_bounds = window->GetBoundsInScreen();
197 // avoid snap by clicking away from the border
198 ASSERT_NO_FATAL_FAILURE(
199 DragStartAtOffsetFromWindowOrigin(window, grab_x, grab_y));
200
201 gfx::Rect work_area = display::Screen::GetScreen()
202 ->GetDisplayNearestWindow(window)
203 .work_area();
204 gfx::Point initial_location_in_screen = initial_location_in_parent_;
205 ::wm::ConvertPointToScreen(window->parent(), &initial_location_in_screen);
206 // Drag the window left or right to the edge (or almost to it).
207 if (edge == DOCKED_EDGE_LEFT)
208 dx += work_area.x() - initial_location_in_screen.x();
209 else if (edge == DOCKED_EDGE_RIGHT)
210 dx += work_area.right() - 1 - initial_location_in_screen.x();
211 DragMove(dx, dy);
212 EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
213 // Release the mouse and the panel should be attached to the dock.
214 DragEnd();
215
216 // x-coordinate can get adjusted by snapping or sticking.
217 // y-coordinate could be changed by possible automatic layout if docked.
218 if (window->parent()->id() != kShellWindowId_DockedContainer &&
219 !wm::GetWindowState(window)->HasRestoreBounds()) {
220 EXPECT_EQ(initial_bounds.y() + dy, window->GetBoundsInScreen().y());
221 }
222 }
223
224 bool test_panels() const { return window_type_ == ui::wm::WINDOW_TYPE_PANEL; }
225
226 aura::test::TestWindowDelegate* delegate() { return &delegate_; }
227
228 const gfx::Point& initial_location_in_parent() const {
229 return initial_location_in_parent_;
230 }
231
232 private:
233 std::unique_ptr<WindowResizer> resizer_;
234 ui::wm::WindowType window_type_;
235 aura::test::TestWindowDelegate delegate_;
236
237 // Location at start of the drag in |window->parent()|'s coordinates.
238 gfx::Point initial_location_in_parent_;
239
240 DISALLOW_COPY_AND_ASSIGN(DockedWindowResizerTest);
241 };
242
243 // Verifies a window can be dragged and attached to the dock.
244 TEST_P(DockedWindowResizerTest, AttachRightPrecise) {
245 std::unique_ptr<aura::Window> window(
246 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
247 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);
248
249 // The window should be docked at the right edge.
250 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
251 window->GetBoundsInScreen().right());
252 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
253 }
254
255 // Verifies a window can be dragged and attached to the dock
256 // even if pointer overshoots the screen edge by a few pixels (sticky edge)
257 TEST_P(DockedWindowResizerTest, AttachRightOvershoot) {
258 std::unique_ptr<aura::Window> window(
259 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
260 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), +4);
261
262 // The window should be docked at the right edge.
263 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
264 window->GetBoundsInScreen().right());
265 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
266 }
267
268 // Verifies a window can be dragged and then if a pointer is not quite reaching
269 // the screen edge the window does not get docked and stays in the desktop.
270 TEST_P(DockedWindowResizerTest, AttachRightUndershoot) {
271 std::unique_ptr<aura::Window> window(
272 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
273 // Grabbing at 70px ensures that at least 30% of the window is in screen,
274 // otherwise the window would be adjusted in
275 // WorkspaceLayoutManager::AdjustWindowBoundsWhenAdded.
276 const int kGrabOffsetX = 70;
277 const int kUndershootBy = 1;
278 DragVerticallyAndRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(),
279 -kUndershootBy, test_panels() ? -100 : 20,
280 kGrabOffsetX, 5);
281
282 // The window right should be past the screen edge but not docked.
283 // Initial touch point is 70px to the right which helps to find where the edge
284 // should be.
285 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right() +
286 window->bounds().width() - kGrabOffsetX - kUndershootBy - 1,
287 window->GetBoundsInScreen().right());
288 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
289 }
290
291 // Verifies a window can be dragged and attached to the dock.
292 TEST_P(DockedWindowResizerTest, AttachLeftPrecise) {
293 std::unique_ptr<aura::Window> window(
294 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
295 DragRelativeToEdge(DOCKED_EDGE_LEFT, window.get(), 0);
296
297 // The window should be docked at the left edge.
298 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().x(),
299 window->GetBoundsInScreen().x());
300 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
301 }
302
303 // Verifies a window can be dragged and attached to the dock
304 // even if pointer overshoots the screen edge by a few pixels (sticky edge)
305 TEST_P(DockedWindowResizerTest, AttachLeftOvershoot) {
306 std::unique_ptr<aura::Window> window(
307 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
308 DragRelativeToEdge(DOCKED_EDGE_LEFT, window.get(), -4);
309
310 // The window should be docked at the left edge.
311 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().x(),
312 window->GetBoundsInScreen().x());
313 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
314 }
315
316 // Verifies a window can be dragged and then if a pointer is not quite reaching
317 // the screen edge the window does not get docked and stays in the desktop.
318 TEST_P(DockedWindowResizerTest, AttachLeftUndershoot) {
319 std::unique_ptr<aura::Window> window(
320 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
321 gfx::Rect initial_bounds(window->bounds());
322 DragRelativeToEdge(DOCKED_EDGE_LEFT, window.get(), 1);
323
324 // The window should be crossing the screen edge but not docked.
325 int expected_x = initial_bounds.x() - initial_location_in_parent().x() + 1;
326 EXPECT_EQ(expected_x, window->GetBoundsInScreen().x());
327 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
328 }
329
330 // Dock on the right side, change shelf alignment, check that windows move to
331 // the opposite side.
332 TEST_P(DockedWindowResizerTest, AttachRightChangeShelf) {
333 std::unique_ptr<aura::Window> window(
334 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
335 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);
336
337 // The window should be docked at the right edge.
338 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
339 window->GetBoundsInScreen().right());
340 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
341
342 // set launcher shelf to be aligned on the right
343 GetPrimaryShelf()->SetAlignment(SHELF_ALIGNMENT_RIGHT);
344 // The window should have moved and get attached to the left dock.
345 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().x(),
346 window->GetBoundsInScreen().x());
347 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
348
349 // set launcher shelf to be aligned on the left
350 GetPrimaryShelf()->SetAlignment(SHELF_ALIGNMENT_LEFT);
351 // The window should have moved and get attached to the right edge.
352 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
353 window->GetBoundsInScreen().right());
354 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
355
356 // set launcher shelf to be aligned at the bottom
357 GetPrimaryShelf()->SetAlignment(SHELF_ALIGNMENT_BOTTOM);
358 // The window should stay in the right edge.
359 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
360 window->GetBoundsInScreen().right());
361 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
362 }
363
364 // Dock on the right side, try to undock, then drag more to really undock
365 TEST_P(DockedWindowResizerTest, AttachTryDetach) {
366 std::unique_ptr<aura::Window> window(
367 CreateTestWindow(gfx::Rect(0, 0, ideal_width() + 10, 201)));
368 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);
369
370 // The window should be docked at the right edge.
371 // Its width should shrink to ideal width.
372 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
373 window->GetBoundsInScreen().right());
374 EXPECT_EQ(ideal_width(), window->GetBoundsInScreen().width());
375 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
376
377 // Try to detach by dragging left less than kSnapToDockDistance.
378 // The window should stay docked.
379 ASSERT_NO_FATAL_FAILURE(
380 DragStartAtOffsetFromWindowOrigin(window.get(), 10, 0));
381 DragMove(-4, -10);
382 // Release the mouse and the window should be still attached to the dock.
383 DragEnd();
384
385 // The window should be still attached to the right edge.
386 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
387 window->GetBoundsInScreen().right());
388 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
389
390 // Try to detach by dragging left by kSnapToDockDistance or more.
391 // The window should get undocked.
392 const int left_edge = window->bounds().x();
393 ASSERT_NO_FATAL_FAILURE(
394 DragStartAtOffsetFromWindowOrigin(window.get(), 10, 0));
395 DragMove(-32, -10);
396 // Release the mouse and the window should be no longer attached to the dock.
397 DragEnd();
398
399 // The window should be floating on the desktop again and moved to the left.
400 EXPECT_EQ(left_edge - 32, window->GetBoundsInScreen().x());
401 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
402 }
403
404 // Dock on the right side, and undock by dragging the right edge of the window
405 // header. This test is useful because both the position of the dragged window
406 // and the position of the mouse are used in determining whether a window should
407 // be undocked.
408 TEST_P(DockedWindowResizerTest, AttachTryDetachDragRightEdgeOfHeader) {
409 std::unique_ptr<aura::Window> window(
410 CreateTestWindow(gfx::Rect(0, 0, ideal_width() + 10, 201)));
411 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);
412
413 // The window should be docked at the right edge.
414 // Its width should shrink to ideal width.
415 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
416 window->GetBoundsInScreen().right());
417 EXPECT_EQ(ideal_width(), window->GetBoundsInScreen().width());
418 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
419
420 // Try to detach by dragging left less than kSnapToDockDistance.
421 // The window should stay docked.
422 ASSERT_NO_FATAL_FAILURE(
423 DragStartAtOffsetFromWindowOrigin(window.get(), ideal_width() - 10, 0));
424 DragMove(-4, -10);
425 // Release the mouse and the window should be still attached to the dock.
426 DragEnd();
427
428 // The window should be still attached to the right edge.
429 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
430 window->GetBoundsInScreen().right());
431 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
432
433 // Try to detach by dragging left by kSnapToDockDistance or more.
434 // The window should get undocked.
435 const int left_edge = window->bounds().x();
436 ASSERT_NO_FATAL_FAILURE(
437 DragStartAtOffsetFromWindowOrigin(window.get(), ideal_width() - 10, 0));
438 DragMove(-32, -10);
439 // Release the mouse and the window should be no longer attached to the dock.
440 DragEnd();
441
442 // The window should be floating on the desktop again and moved to the left.
443 EXPECT_EQ(left_edge - 32, window->GetBoundsInScreen().x());
444 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
445 }
446
447 // Minimize a docked window, then restore it and check that it is still docked.
448 TEST_P(DockedWindowResizerTest, AttachMinimizeRestore) {
449 std::unique_ptr<aura::Window> window(
450 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
451 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);
452
453 // The window should be docked at the right edge.
454 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
455 window->GetBoundsInScreen().right());
456 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
457
458 wm::WindowState* window_state = wm::GetWindowState(window.get());
459 // Minimize the window, it should be hidden.
460 window_state->Minimize();
461 RunAllPendingInMessageLoop();
462 EXPECT_FALSE(window->IsVisible());
463 EXPECT_TRUE(window_state->IsMinimized());
464 // Restore the window; window should be visible.
465 window_state->Restore();
466 RunAllPendingInMessageLoop();
467 EXPECT_TRUE(window->IsVisible());
468 EXPECT_TRUE(window_state->IsDocked());
469 }
470
471 // Maximize a docked window and check that it is maximized and no longer docked.
472 TEST_P(DockedWindowResizerTest, AttachMaximize) {
473 std::unique_ptr<aura::Window> window(
474 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
475 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);
476
477 // The window should be docked at the right edge.
478 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
479 window->GetBoundsInScreen().right());
480 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
481
482 wm::WindowState* window_state = wm::GetWindowState(window.get());
483 // Maximize the window, it should get undocked and maximized in a desktop.
484 window_state->Maximize();
485 RunAllPendingInMessageLoop();
486 EXPECT_TRUE(window->IsVisible());
487 EXPECT_TRUE(window_state->IsMaximized());
488 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
489 }
490
491 // Dock two windows, undock one, check that the other one is still docked.
492 TEST_P(DockedWindowResizerTest, AttachTwoWindows) {
493 UpdateDisplay("600x600");
494
495 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
496 std::unique_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
497 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
498 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w2.get(), 50);
499
500 // Docking second window should not minimize the first.
501 wm::WindowState* window_state1 = wm::GetWindowState(w1.get());
502 EXPECT_FALSE(window_state1->IsMinimized());
503
504 // Both windows should be docked at the right edge.
505 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
506 w1->GetBoundsInScreen().right());
507 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
508
509 EXPECT_EQ(w2->GetRootWindow()->GetBoundsInScreen().right(),
510 w2->GetBoundsInScreen().right());
511 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
512
513 // Detach by dragging left (should get undocked).
514 const int left_edge = w2->bounds().x();
515 ASSERT_NO_FATAL_FAILURE(DragStart(w2.get()));
516 // Drag up as well to avoid attaching panels to launcher shelf.
517 DragMove(-32, -100);
518 // Release the mouse and the window should be no longer attached to the edge.
519 DragEnd();
520
521 // The first window should be still docked.
522 EXPECT_FALSE(window_state1->IsMinimized());
523 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
524 w1->GetBoundsInScreen().right());
525 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
526
527 // The window should be floating on the desktop again and moved to the left.
528 EXPECT_EQ(left_edge - 32, w2->GetBoundsInScreen().x());
529 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
530 }
531
532 // Create two windows, dock one and change shelf to auto-hide.
533 TEST_P(DockedWindowResizerTest, AttachOneAutoHideShelf) {
534 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
535 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
536
537 // w1 should be docked at the right edge.
538 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
539 w1->GetBoundsInScreen().right());
540 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
541
542 std::unique_ptr<aura::Window> w2(CreateTestWindowInShellWithDelegateAndType(
543 NULL, ui::wm::WINDOW_TYPE_NORMAL, 0, gfx::Rect(20, 20, 150, 20)));
544 wm::GetWindowState(w2.get())->Maximize();
545 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
546 EXPECT_TRUE(wm::GetWindowState(w2.get())->IsMaximized());
547
548 gfx::Rect work_area = display::Screen::GetScreen()
549 ->GetDisplayNearestWindow(w1.get())
550 .work_area();
551 DockedWindowLayoutManager* manager =
552 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
553
554 // Docked window should be centered vertically in the work area.
555 EXPECT_EQ(work_area.CenterPoint().y(), w1->bounds().CenterPoint().y());
556 // Docked background should extend to the bottom of work area.
557 EXPECT_EQ(work_area.bottom(), manager->docked_bounds().bottom());
558
559 // Turn on shelf auto-hide.
560 GetPrimaryShelf()->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
561 work_area = display::Screen::GetScreen()
562 ->GetDisplayNearestWindow(w1.get())
563 .work_area();
564 // Docked window should be centered vertically in the work area.
565 EXPECT_EQ(work_area.CenterPoint().y(), w1->bounds().CenterPoint().y());
566 // Docked background should extend to the bottom of work area.
567 EXPECT_EQ(work_area.bottom(), manager->docked_bounds().bottom());
568 }
569
570 // Dock one window, try to dock another window on the opposite side (should not
571 // dock).
572 TEST_P(DockedWindowResizerTest, AttachOnTwoSides) {
573 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
574 std::unique_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
575 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
576 gfx::Rect initial_bounds(w2->bounds());
577 DragToVerticalPositionAndToEdge(DOCKED_EDGE_LEFT, w2.get(), 50);
578
579 // The first window should be docked at the right edge.
580 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
581 w1->GetBoundsInScreen().right());
582 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
583
584 // The second window should be near the left edge but not snapped.
585 // Normal window will get side-maximized while panels will not.
586 int expected_x = test_panels()
587 ? (initial_bounds.x() - initial_location_in_parent().x())
588 : 0;
589 EXPECT_EQ(expected_x, w2->GetBoundsInScreen().x());
590 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
591 }
592
593 // Tests that reverting a drag restores docked state if a window was docked.
594 TEST_P(DockedWindowResizerTest, RevertDragRestoresAttachment) {
595 std::unique_ptr<aura::Window> window(
596 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
597 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);
598
599 // The window should be docked at the right edge.
600 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
601 window->GetBoundsInScreen().right());
602 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
603
604 // Drag the window out but revert the drag
605 ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
606 DragMove(-50, 0);
607 DragRevert();
608 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
609
610 // Detach window.
611 ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
612 DragMove(-50, 0);
613 DragEnd();
614 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
615 }
616
617 // Tests that reverting drag restores undocked state if a window was not docked.
618 TEST_P(DockedWindowResizerTest, RevertDockedDragRevertsAttachment) {
619 std::unique_ptr<aura::Window> window(
620 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
621 DockedWindowLayoutManager* manager =
622 DockedWindowLayoutManager::Get(WmWindow::Get(window.get()));
623 int previous_container_id = window->parent()->id();
624 // Drag the window out but revert the drag
625 ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
626 DragMove(-50 - window->bounds().x(), 50 - window->bounds().y());
627 EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
628 DragRevert();
629 EXPECT_EQ(previous_container_id, window->parent()->id());
630 EXPECT_EQ(DOCKED_ALIGNMENT_NONE, docked_alignment(manager));
631
632 // Drag a window to the left so that it overlaps the screen edge.
633 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(
634 window.get(), window->bounds().width() / 2 + 10, 0));
635 DragMove(-50 - window->bounds().x(), 50 - window->bounds().y());
636 DragEnd();
637 // The window now overlaps the left screen edge but is not docked.
638 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
639 EXPECT_EQ(DOCKED_ALIGNMENT_NONE, docked_alignment(manager));
640 EXPECT_LT(window->bounds().x(), 0);
641 EXPECT_GT(window->bounds().right(), 0);
642
643 // Drag the window further left and revert the drag.
644 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(
645 window.get(), window->bounds().width() / 2 + 10, 0));
646 DragMove(-10, 10);
647 DragRevert();
648 // The window should be in default container and not docked.
649 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
650 // Docked area alignment should be cleared.
651 EXPECT_EQ(DOCKED_ALIGNMENT_NONE, docked_alignment(manager));
652 }
653
654 // Move a docked window to the second display
655 TEST_P(DockedWindowResizerTest, DragAcrossDisplays) {
656 UpdateDisplay("800x800,800x800");
657 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
658 EXPECT_EQ(2, static_cast<int>(root_windows.size()));
659 std::unique_ptr<aura::Window> window(
660 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
661 EXPECT_EQ(root_windows[0], window->GetRootWindow());
662
663 DragRelativeToEdge(DOCKED_EDGE_RIGHT, window.get(), 0);
664 // The window should be docked at the right edge.
665 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
666 window->GetBoundsInScreen().right());
667 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
668
669 // Try dragging to the right - enough to get it peeking at the other screen
670 // but not enough to land in the other screen.
671 // The window should stay on the left screen.
672 ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
673 DragMove(100, 0);
674 EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
675 DragEnd();
676 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
677 window->GetBoundsInScreen().right());
678 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
679 EXPECT_EQ(root_windows[0], window->GetRootWindow());
680
681 // Undock and move to the right - enough to get the mouse pointer past the
682 // edge of the screen and into the second screen. The window should now be
683 // in the second screen and not docked.
684 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(
685 window.get(), window->bounds().width() / 2 + 10, 0));
686 DragMove(window->bounds().width() / 2 - 5, 0);
687 EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
688 DragEnd();
689 EXPECT_NE(window->GetRootWindow()->GetBoundsInScreen().right(),
690 window->GetBoundsInScreen().right());
691 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
692 EXPECT_EQ(root_windows[1], window->GetRootWindow());
693
694 // Keep dragging it to the right until its left edge touches the screen edge.
695 // The window should now be in the second screen and not docked.
696 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(
697 window.get(), window->bounds().width() / 2 + 10, 0));
698 DragMove(window->GetRootWindow()->GetBoundsInScreen().x() -
699 window->GetBoundsInScreen().x(),
700 0);
701 EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
702 DragEnd();
703 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().x(),
704 window->GetBoundsInScreen().x());
705 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
706 EXPECT_EQ(root_windows[1], window->GetRootWindow());
707 }
708
709 // Dock two windows, undock one.
710 // Test the docked windows area size and default container resizing.
711 TEST_P(DockedWindowResizerTest, AttachTwoWindowsDetachOne) {
712 UpdateDisplay("600x600");
713
714 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
715 std::unique_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 210, 201)));
716 // Work area should cover the whole screen.
717 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width(),
718 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
719
720 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
721 // A window should be docked at the right edge.
722 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
723 w1->GetBoundsInScreen().right());
724 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
725 DockedWindowLayoutManager* manager =
726 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
727 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
728 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
729
730 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w2.get(), 100);
731 // Both windows should now be docked at the right edge.
732 EXPECT_EQ(w2->GetRootWindow()->GetBoundsInScreen().right(),
733 w2->GetBoundsInScreen().right());
734 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
735 // Dock width should be set to a wider window.
736 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
737 EXPECT_EQ(std::max(w1->bounds().width(), w2->bounds().width()),
738 docked_width(manager));
739
740 // Try to detach by dragging left a bit (should not get undocked).
741 // This would normally detach a single docked window but since we have another
742 // window and the mouse pointer does not leave the dock area the window
743 // should stay docked.
744 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(w2.get(), 60, 0));
745 // Drag up as well as left to avoid attaching panels to launcher shelf.
746 DragMove(-40, -40);
747 // Release the mouse and the window should be still attached to the edge.
748 DragEnd();
749
750 // The first window should be still docked.
751 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
752 w1->GetBoundsInScreen().right());
753 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
754
755 // The second window should be still docked.
756 EXPECT_EQ(w2->GetRootWindow()->GetBoundsInScreen().right(),
757 w2->GetBoundsInScreen().right());
758 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
759
760 // Detach by dragging left more (should get undocked).
761 const int left_edge = w2->bounds().x();
762 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(
763 w2.get(), w2->bounds().width() / 2 + 10, 0));
764 // Drag up as well to avoid attaching panels to launcher shelf.
765 const int drag_x = -(w2->bounds().width() / 2 + 20);
766 DragMove(drag_x, -100);
767 // Release the mouse and the window should be no longer attached to the edge.
768 DragEnd();
769
770 // The second window should be floating on the desktop again.
771 EXPECT_EQ(left_edge + drag_x, w2->bounds().x());
772 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
773 // Dock width should be set to remaining single docked window.
774 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
775 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
776 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
777 }
778
779 // Dock one of the windows. Maximize other testing desktop resizing.
780 TEST_P(DockedWindowResizerTest, AttachWindowMaximizeOther) {
781 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
782 std::unique_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 210, 201)));
783 // Work area should cover the whole screen.
784 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width(),
785 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
786
787 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
788 // A window should be docked at the right edge.
789 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
790 w1->GetBoundsInScreen().right());
791 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
792 DockedWindowLayoutManager* manager =
793 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
794 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
795 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
796
797 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(w2.get(), 25, 5));
798 DragMove(w2->GetRootWindow()->bounds().width() - w2->bounds().width() -
799 (w2->bounds().width() / 2 + 20) - w2->bounds().x(),
800 50 - w2->bounds().y());
801 DragEnd();
802 // The first window should be still docked.
803 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
804 w1->GetBoundsInScreen().right());
805 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
806
807 // The second window should be floating on the desktop.
808 EXPECT_EQ(w2->GetRootWindow()->GetBoundsInScreen().right() -
809 (w2->bounds().width() / 2 + 20),
810 w2->GetBoundsInScreen().right());
811 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
812 // Dock width should be set to remaining single docked window.
813 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
814 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
815 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
816 // Desktop work area should now shrink.
817 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width() -
818 docked_width(manager) - min_dock_gap(),
819 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
820
821 // Maximize the second window - Maximized area should be shrunk.
822 const gfx::Rect restored_bounds = w2->bounds();
823 wm::WindowState* w2_state = wm::GetWindowState(w2.get());
824 w2_state->Maximize();
825 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width() -
826 docked_width(manager) - min_dock_gap(),
827 w2->bounds().width());
828
829 // Detach the first window (this should require very little drag).
830 ASSERT_NO_FATAL_FAILURE(DragStart(w1.get()));
831 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
832 DragMove(-35, 10);
833 // Alignment is set to "NONE" when drag starts.
834 EXPECT_EQ(DOCKED_ALIGNMENT_NONE, docked_alignment(manager));
835 // Release the mouse and the window should be no longer attached to the edge.
836 DragEnd();
837 EXPECT_EQ(DOCKED_ALIGNMENT_NONE, docked_alignment(manager));
838 // Dock should get shrunk and desktop should get expanded.
839 EXPECT_EQ(kShellWindowId_DefaultContainer, w1->parent()->id());
840 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
841 EXPECT_EQ(DOCKED_ALIGNMENT_NONE, docked_alignment(manager));
842 EXPECT_EQ(0, docked_width(manager));
843 // The second window should now get resized and take up the whole screen.
844 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width(),
845 w2->bounds().width());
846
847 // Dock the first window to the left edge.
848 // Click at an offset from origin to prevent snapping.
849 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(w1.get(), 10, 0));
850 // Drag left to get pointer touching the screen edge.
851 DragMove(-w1->bounds().x() - 10, 0);
852 // Alignment set to "NONE" during the drag of the window when none are docked.
853 EXPECT_EQ(DOCKED_ALIGNMENT_NONE, docked_alignment(manager));
854 // Release the mouse and the window should be now attached to the edge.
855 DragEnd();
856 // Dock should get expanded and desktop should get shrunk.
857 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
858 EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, docked_alignment(manager));
859 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
860 // Second window should still be in the desktop.
861 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
862 // Maximized window should be shrunk.
863 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width() -
864 docked_width(manager) - min_dock_gap(),
865 w2->bounds().width());
866
867 // Unmaximize the second window.
868 w2_state->Restore();
869 // Its bounds should get restored.
870 EXPECT_EQ(restored_bounds, w2->bounds());
871 }
872
873 // Dock one window. Test the sticky behavior near screen or desktop edge.
874 TEST_P(DockedWindowResizerTest, AttachOneTestSticky) {
875 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
876 std::unique_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 210, 201)));
877 // Work area should cover the whole screen.
878 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width(),
879 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
880
881 DragToVerticalPositionAndToEdge(DOCKED_EDGE_LEFT, w1.get(), 20);
882 // A window should be docked at the left edge.
883 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().x(),
884 w1->GetBoundsInScreen().x());
885 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
886 DockedWindowLayoutManager* manager =
887 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
888 // The first window should be docked.
889 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().x(),
890 w1->GetBoundsInScreen().x());
891 // Dock width should be set to that of a single docked window.
892 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
893 EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, docked_alignment(manager));
894 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
895
896 // Position second window in the desktop 20px to the right of the docked w1.
897 DragToVerticalPositionRelativeToEdge(DOCKED_EDGE_LEFT, w2.get(),
898 20 + 25 - min_dock_gap(), 50);
899 // The second window should be floating on the desktop.
900 EXPECT_EQ(w2->GetRootWindow()->GetBoundsInScreen().x() +
901 (w1->bounds().right() + 20),
902 w2->GetBoundsInScreen().x());
903 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
904 // Dock width should be set to that of a single docked window.
905 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
906 EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, docked_alignment(manager));
907 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
908
909 // Drag w2 almost to the dock, the mouse pointer not quite reaching the dock.
910 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(w2.get(), 10, 0));
911 DragMove(1 + docked_width(manager) - w2->bounds().x(), 0);
912 // Alignment set to "LEFT" during the drag because dock has a window in it.
913 EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, docked_alignment(manager));
914 // Release the mouse and the window should not be attached to the edge.
915 DragEnd();
916 // Dock should still have only one window in it.
917 EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, docked_alignment(manager));
918 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
919 // The second window should still be in the desktop.
920 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
921 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
922
923 // Drag w2 by a bit more - it should resist the drag (stuck edges)
924 int start_x = w2->bounds().x();
925 ASSERT_NO_FATAL_FAILURE(DragStartAtOffsetFromWindowOrigin(w2.get(), 100, 5));
926 DragMove(-2, 0);
927 // Window should not actually move.
928 EXPECT_EQ(start_x, w2->bounds().x());
929 // Alignment set to "LEFT" during the drag because dock has a window in it.
930 EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, docked_alignment(manager));
931 // Release the mouse and the window should not be attached to the edge.
932 DragEnd();
933 // Window should be still where it was before the last drag started.
934 EXPECT_EQ(start_x, w2->bounds().x());
935 // Dock should still have only one window in it
936 EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, docked_alignment(manager));
937 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
938 // The second window should still be in the desktop
939 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
940 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
941
942 // Drag w2 by more than the stuck threshold and drop it into the dock.
943 ASSERT_NO_FATAL_FAILURE(DragStart(w2.get()));
944 DragMove(-100, 0);
945 // Window should actually move.
946 EXPECT_NE(start_x, w2->bounds().x());
947 // Alignment set to "LEFT" during the drag because dock has a window in it.
948 EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, docked_alignment(manager));
949 // Release the mouse and the window should be attached to the edge.
950 DragEnd();
951 // Both windows are docked now.
952 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
953 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
954 // Dock should get expanded and desktop should get shrunk.
955 EXPECT_EQ(DOCKED_ALIGNMENT_LEFT, docked_alignment(manager));
956 EXPECT_EQ(std::max(w1->bounds().width(), w2->bounds().width()),
957 docked_width(manager));
958 // Desktop work area should now shrink by dock width.
959 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width() -
960 docked_width(manager) - min_dock_gap(),
961 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
962 }
963
964 // Dock two windows, resize one.
965 // Test the docked windows area size and remaining desktop resizing.
966 TEST_P(DockedWindowResizerTest, ResizeOneOfTwoWindows) {
967 // Wider display to start since panels are limited to half the display width.
968 UpdateDisplay("1000x600");
969 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
970 std::unique_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 210, 201)));
971 // Work area should cover the whole screen.
972 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width(),
973 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
974
975 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
976 // A window should be docked at the right edge.
977 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
978 w1->GetBoundsInScreen().right());
979 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
980 DockedWindowLayoutManager* manager =
981 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
982 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
983 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
984
985 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w2.get(), 100);
986 // Both windows should now be docked at the right edge.
987 EXPECT_EQ(w2->GetRootWindow()->GetBoundsInScreen().right(),
988 w2->GetBoundsInScreen().right());
989 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
990 // Dock width should be set to a wider window.
991 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
992 EXPECT_EQ(std::max(w1->bounds().width(), w2->bounds().width()),
993 docked_width(manager));
994
995 // Resize the first window left by a bit and test that the dock expands.
996 int previous_width = w1->bounds().width();
997 const int kResizeSpan1 = 30;
998 ASSERT_NO_FATAL_FAILURE(
999 ResizeStartAtOffsetFromWindowOrigin(w1.get(), 0, 20, HTLEFT));
1000 DragMove(-kResizeSpan1, 0);
1001 // Alignment set to "RIGHT" during the drag because dock has a window in it.
1002 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1003 // Release the mouse and the window should be attached to the edge.
1004 DragEnd();
1005 // Dock should still have both windows in it.
1006 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1007 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
1008 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1009 // w1 is now wider than before. The dock should expand and be as wide as w1.
1010 EXPECT_EQ(previous_width + kResizeSpan1, w1->bounds().width());
1011 // Both windows should get resized since they both don't have min/max size.
1012 EXPECT_EQ(w1->bounds().width(), w2->bounds().width());
1013 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1014 // Desktop work area should shrink.
1015 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width() -
1016 docked_width(manager) - min_dock_gap(),
1017 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
1018
1019 // Resize the first window left by more than the dock maximum width.
1020 // This should cause the window width to be restricted by maximum dock width.
1021 previous_width = w1->bounds().width();
1022 const int kResizeSpan2 = 250;
1023 ASSERT_NO_FATAL_FAILURE(
1024 ResizeStartAtOffsetFromWindowOrigin(w1.get(), 0, 20, HTLEFT));
1025 DragMove(-kResizeSpan2, 0);
1026 // Alignment set to "RIGHT" during the drag because dock has a window in it.
1027 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1028 // Release the mouse and the window should be attached to the edge.
1029 DragEnd();
1030 // Dock should still have both windows in it.
1031 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1032 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
1033 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1034 // w1 is now as wide as the maximum dock width and the dock should get
1035 // resized to the maximum width.
1036 EXPECT_EQ(max_width(), w1->bounds().width());
1037 // Both windows should get resized since they both don't have min/max size.
1038 EXPECT_EQ(w1->bounds().width(), w2->bounds().width());
1039 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1040 // Desktop work area should shrink.
1041 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width() -
1042 docked_width(manager) - min_dock_gap(),
1043 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
1044
1045 // Resize the first window right to get it completely inside the docked area.
1046 previous_width = w1->bounds().width();
1047 const int kResizeSpan3 = 100;
1048 ASSERT_NO_FATAL_FAILURE(
1049 ResizeStartAtOffsetFromWindowOrigin(w1.get(), 0, 20, HTLEFT));
1050 DragMove(kResizeSpan3, 0);
1051 // Alignment set to "RIGHT" during the drag because dock has a window in it.
1052 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1053 // Release the mouse and the window should be docked.
1054 DragEnd();
1055 // Dock should still have both windows in it.
1056 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1057 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
1058 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1059 // w1 should be narrower than before by the length of the drag.
1060 EXPECT_EQ(previous_width - kResizeSpan3, w1->bounds().width());
1061 // Both windows should get resized since they both don't have min/max size.
1062 EXPECT_EQ(w1->bounds().width(), w2->bounds().width());
1063 // The dock should be as wide as w1 or w2.
1064 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1065 // Desktop work area should shrink.
1066 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w2.get()).width() -
1067 docked_width(manager) - min_dock_gap(),
1068 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w2.get()).width());
1069
1070 // Resize the first window left to be overhang again.
1071 previous_width = w1->bounds().width();
1072 ASSERT_NO_FATAL_FAILURE(
1073 ResizeStartAtOffsetFromWindowOrigin(w1.get(), 0, 20, HTLEFT));
1074 DragMove(-kResizeSpan3, 0);
1075 DragEnd();
1076 EXPECT_EQ(previous_width + kResizeSpan3, w1->bounds().width());
1077 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1078 // Docked area should be as wide as possible (maximum) and same as w1.
1079 EXPECT_EQ(max_width(), docked_width(manager));
1080 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1081
1082 // Undock the first window. Docked area should shrink to its ideal size.
1083 ASSERT_NO_FATAL_FAILURE(DragStart(w1.get()));
1084 // Drag up as well to avoid attaching panels to launcher shelf.
1085 DragMove(-(400 - 210), -100);
1086 // Alignment set to "RIGHT" since we have another window docked.
1087 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1088 // Release the mouse and the window should be no longer attached to the edge.
1089 DragEnd();
1090 EXPECT_EQ(kShellWindowId_DefaultContainer, w1->parent()->id());
1091 // Dock should be as wide as w2 (and same as ideal width).
1092 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1093 EXPECT_EQ(ideal_width(), docked_width(manager));
1094 EXPECT_EQ(w2->bounds().width(), docked_width(manager));
1095 // The second window should be still docked.
1096 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
1097 // Desktop work area should be inset.
1098 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w1.get()).width() -
1099 docked_width(manager) - min_dock_gap(),
1100 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w1.get()).width());
1101 }
1102
1103 // Dock a window, resize it and test that undocking it restores the pre-docked
1104 // size.
1105 TEST_P(DockedWindowResizerTest, ResizingKeepsSize) {
1106 // Wider display to start since panels are limited to half the display width.
1107 UpdateDisplay("1000x600");
1108 const gfx::Size original_size(201, 201);
1109 std::unique_ptr<aura::Window> w1(
1110 CreateTestWindow(gfx::Rect(gfx::Point(), original_size)));
1111
1112 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
1113 // Window should be docked at the right edge.
1114 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
1115 w1->GetBoundsInScreen().right());
1116 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1117 DockedWindowLayoutManager* manager =
1118 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
1119 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1120 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1121
1122 // Resize the window left by a bit and test that the dock expands.
1123 int previous_width = w1->bounds().width();
1124 const int kResizeSpan1 = 30;
1125 ASSERT_NO_FATAL_FAILURE(
1126 ResizeStartAtOffsetFromWindowOrigin(w1.get(), 0, 20, HTLEFT));
1127 DragMove(-kResizeSpan1, 0);
1128 // Alignment stays "RIGHT" during the drag because the only docked window
1129 // is being resized.
1130 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1131 // Release the mouse and the window should be attached to the edge.
1132 DragEnd();
1133 // The window should get docked.
1134 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1135 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1136 // w1 is now wider and the dock should expand to be as wide as w1.
1137 EXPECT_EQ(previous_width + kResizeSpan1, w1->bounds().width());
1138 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1139
1140 // Undock by dragging almost to the left edge.
1141 DragToVerticalPositionRelativeToEdge(DOCKED_EDGE_LEFT, w1.get(), 100, 20);
1142 // Size should be restored to what it was originally.
1143 EXPECT_EQ(original_size.ToString(), w1->bounds().size().ToString());
1144
1145 // Dock again.
1146 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
1147 // Width should be reset to initial ideal width (25px).
1148 EXPECT_EQ(ideal_width(), w1->bounds().width());
1149
1150 // Undock again by dragging left.
1151 DragToVerticalPositionRelativeToEdge(DOCKED_EDGE_LEFT, w1.get(), 100, 20);
1152 // Size should be restored to what it was originally.
1153 EXPECT_EQ(original_size.ToString(), w1->bounds().size().ToString());
1154 }
1155
1156 // Dock a window, resize it and test that it stays docked.
1157 TEST_P(DockedWindowResizerTest, ResizingKeepsDockedState) {
1158 // Wider display to start since panels are limited to half the display width.
1159 UpdateDisplay("1000x600");
1160 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
1161
1162 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
1163 // Window should be docked at the right edge.
1164 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
1165 w1->GetBoundsInScreen().right());
1166 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1167 DockedWindowLayoutManager* manager =
1168 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
1169 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1170 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1171
1172 // Resize the window left by a bit and test that the dock expands.
1173 int previous_width = w1->bounds().width();
1174 const int kResizeSpan1 = 30;
1175 ASSERT_NO_FATAL_FAILURE(
1176 ResizeStartAtOffsetFromWindowOrigin(w1.get(), 0, 20, HTLEFT));
1177 DragMove(-kResizeSpan1, 0);
1178 // Normally alignment would be reset to "NONE" during the drag when there is
1179 // only a single window docked and it is being dragged. However because that
1180 // window is being resized rather than moved the alignment is not changed.
1181 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1182 // Release the mouse and the window should be attached to the edge.
1183 DragEnd();
1184 // The window should stay docked.
1185 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1186 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1187 // w1 is now wider and the dock should expand to be as wide as w1.
1188 EXPECT_EQ(previous_width + kResizeSpan1, w1->bounds().width());
1189 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1190
1191 // Resize the window by dragging its right edge left a bit and test that the
1192 // window stays docked.
1193 previous_width = w1->bounds().width();
1194 const int kResizeSpan2 = 15;
1195 ASSERT_NO_FATAL_FAILURE(ResizeStartAtOffsetFromWindowOrigin(
1196 w1.get(), w1->bounds().width(), 20, HTRIGHT));
1197 DragMove(-kResizeSpan2, 0);
1198 // Alignment stays "RIGHT" during the drag because the window is being
1199 // resized rather than dragged.
1200 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1201 // Release the mouse and the window should be attached to the edge.
1202 DragEnd();
1203 // The window should stay docked.
1204 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1205 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1206 // The dock should stay as wide as w1 is now (a bit less than before).
1207 EXPECT_EQ(previous_width - kResizeSpan2, w1->bounds().width());
1208 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1209 }
1210
1211 // Dock two windows, resize one. Test the docked windows area size.
1212 TEST_P(DockedWindowResizerTest, ResizeTwoWindows) {
1213 // Wider display to start since panels are limited to half the display width.
1214 UpdateDisplay("1000x600");
1215 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
1216 std::unique_ptr<aura::Window> w2(CreateTestWindow(gfx::Rect(0, 0, 210, 201)));
1217
1218 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
1219 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w2.get(), 100);
1220 // Both windows should now be docked at the right edge.
1221 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1222 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
1223 // Dock width should be set to ideal width.
1224 DockedWindowLayoutManager* manager =
1225 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
1226 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1227 EXPECT_EQ(ideal_width(), docked_width(manager));
1228
1229 // Resize the first window left by a bit and test that the dock expands.
1230 int previous_width = w1->bounds().width();
1231 const int kResizeSpan1 = 30;
1232 ASSERT_NO_FATAL_FAILURE(
1233 ResizeStartAtOffsetFromWindowOrigin(w1.get(), 0, 20, HTLEFT));
1234 DragMove(-kResizeSpan1, 0);
1235 DragEnd();
1236 // w1 is now wider than before.
1237 EXPECT_EQ(previous_width + kResizeSpan1, w1->bounds().width());
1238 // Both windows should get resized since they both don't have min/max size.
1239 EXPECT_EQ(w1->bounds().width(), w2->bounds().width());
1240 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1241
1242 // Resize the second window left by a bit more and test that the dock expands.
1243 previous_width = w2->bounds().width();
1244 ASSERT_NO_FATAL_FAILURE(
1245 ResizeStartAtOffsetFromWindowOrigin(w2.get(), 0, 20, HTLEFT));
1246 DragMove(-kResizeSpan1, 0);
1247 DragEnd();
1248 // w2 should get wider since it was resized by a user.
1249 EXPECT_EQ(previous_width + kResizeSpan1, w2->bounds().width());
1250 // w1 should stay as wide as w2 since both were flush with the dock edge.
1251 EXPECT_EQ(w2->bounds().width(), w1->bounds().width());
1252 EXPECT_EQ(w2->bounds().width(), docked_width(manager));
1253
1254 // Undock w2 and then dock it back.
1255 DragToVerticalPositionRelativeToEdge(DOCKED_EDGE_RIGHT, w2.get(), -400, 100);
1256 EXPECT_EQ(kShellWindowId_DefaultContainer, w2->parent()->id());
1257 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w2.get(), 100);
1258 EXPECT_EQ(kShellWindowId_DockedContainer, w2->parent()->id());
1259 // w2 should become same width as w1.
1260 EXPECT_EQ(w1->bounds().width(), w2->bounds().width());
1261 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1262
1263 // Make w1 even wider.
1264 ASSERT_NO_FATAL_FAILURE(
1265 ResizeStartAtOffsetFromWindowOrigin(w1.get(), 0, 20, HTLEFT));
1266 DragMove(-kResizeSpan1, 0);
1267 DragEnd();
1268 // Making w1 wider should make both windows wider since w2 no longer remembers
1269 // user width.
1270 EXPECT_EQ(w1->bounds().width(), w2->bounds().width());
1271 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1272 }
1273
1274 // Tests that dragging a window down to shelf attaches a panel but does not
1275 // attach a regular window.
1276 TEST_P(DockedWindowResizerTest, DragToShelf) {
1277 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
1278 // Work area should cover the whole screen.
1279 EXPECT_EQ(ScreenUtil::GetDisplayBoundsInParent(w1.get()).width(),
1280 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w1.get()).width());
1281
1282 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
1283 // A window should be docked at the right edge.
1284 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
1285 w1->GetBoundsInScreen().right());
1286 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1287 DockedWindowLayoutManager* manager =
1288 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
1289 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1290 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1291
1292 // Detach and drag down to shelf.
1293 ASSERT_NO_FATAL_FAILURE(DragStart(w1.get()));
1294 DragMove(-40, 0);
1295 // Alignment is set to "NONE" when drag starts.
1296 EXPECT_EQ(DOCKED_ALIGNMENT_NONE, docked_alignment(manager));
1297 // Release the mouse and the window should be no longer attached to the edge.
1298 DragEnd();
1299 EXPECT_EQ(DOCKED_ALIGNMENT_NONE, docked_alignment(manager));
1300
1301 // Drag down almost to shelf. A panel will snap, a regular window won't.
1302 const int shelf_y =
1303 GetPrimaryShelf()->shelf_widget()->GetWindowBoundsInScreen().y();
1304 const int kDistanceFromShelf = 10;
1305 ASSERT_NO_FATAL_FAILURE(DragStart(w1.get()));
1306 DragMove(0, -kDistanceFromShelf + shelf_y - w1->bounds().bottom());
1307 DragEnd();
1308 if (test_panels()) {
1309 // The panel should be touching the shelf and attached.
1310 EXPECT_EQ(shelf_y, w1->bounds().bottom());
1311 EXPECT_TRUE(w1->GetProperty(kPanelAttachedKey));
1312 } else {
1313 // The window should not be touching the shelf.
1314 EXPECT_EQ(shelf_y - kDistanceFromShelf, w1->bounds().bottom());
1315 }
1316 }
1317
1318 // Tests that docking and undocking a |window| with a transient child properly
1319 // maintains the parent of that transient child to be the same as the |window|.
1320 TEST_P(DockedWindowResizerTest, DragWindowWithTransientChild) {
1321 // Create a window with a transient child.
1322 std::unique_ptr<aura::Window> window(
1323 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
1324 std::unique_ptr<aura::Window> child(
1325 CreateTestWindowInShellWithDelegateAndType(
1326 NULL, ui::wm::WINDOW_TYPE_NORMAL, 0, gfx::Rect(20, 20, 150, 20)));
1327 ::wm::AddTransientChild(window.get(), child.get());
1328 if (window->parent() != child->parent())
1329 window->parent()->AddChild(child.get());
1330 EXPECT_EQ(window.get(), ::wm::GetTransientParent(child.get()));
1331
1332 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, window.get(), 20);
1333
1334 // A window should be docked at the right edge.
1335 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
1336 EXPECT_EQ(kShellWindowId_DockedContainer, child->parent()->id());
1337
1338 // Drag the child - it should move freely and stay where it is dragged.
1339 ASSERT_NO_FATAL_FAILURE(DragStart(child.get()));
1340 DragMove(500, 20);
1341 DragEnd();
1342 EXPECT_EQ(gfx::Point(20 + 500, 20 + 20).ToString(),
1343 child->GetBoundsInScreen().origin().ToString());
1344
1345 // Undock the window by dragging left.
1346 ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
1347 DragMove(-32, -10);
1348 DragEnd();
1349
1350 // The window should be undocked and the transient child should be reparented.
1351 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
1352 EXPECT_EQ(kShellWindowId_DefaultContainer, child->parent()->id());
1353 // The child should not have moved.
1354 EXPECT_EQ(gfx::Point(20 + 500, 20 + 20).ToString(),
1355 child->GetBoundsInScreen().origin().ToString());
1356 }
1357
1358 // Tests that reparenting windows during the drag does not affect system modal
1359 // windows that are transient children of the dragged windows.
1360 TEST_P(DockedWindowResizerTest, DragWindowWithModalTransientChild) {
1361 // Create a window.
1362 std::unique_ptr<aura::Window> window(
1363 CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
1364 gfx::Rect bounds(window->bounds());
1365
1366 // Start dragging the window.
1367 ASSERT_NO_FATAL_FAILURE(DragStart(window.get()));
1368 gfx::Vector2d move_vector(40, test_panels() ? -60 : 60);
1369 DragMove(move_vector.x(), move_vector.y());
1370 EXPECT_EQ(CorrectContainerIdDuringDrag(), window->parent()->id());
1371
1372 // While still dragging create a modal window and make it a transient child of
1373 // the |window|.
1374 std::unique_ptr<aura::Window> child(
1375 CreateModalWindow(gfx::Rect(20, 20, 150, 20)));
1376 ::wm::AddTransientChild(window.get(), child.get());
1377 EXPECT_EQ(window.get(), ::wm::GetTransientParent(child.get()));
1378 EXPECT_EQ(kShellWindowId_SystemModalContainer, child->parent()->id());
1379
1380 // End the drag, the |window| should have moved (if it is a panel it will
1381 // no longer be attached to the shelf since we dragged it above).
1382 DragEnd();
1383 bounds.Offset(move_vector);
1384 EXPECT_EQ(bounds.ToString(), window->GetBoundsInScreen().ToString());
1385
1386 // The original |window| should be in the default container (not docked or
1387 // attached).
1388 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
1389 // The transient |child| should still be in system modal container.
1390 EXPECT_EQ(kShellWindowId_SystemModalContainer, child->parent()->id());
1391 // The |child| should not have moved.
1392 EXPECT_EQ(gfx::Point(20, 20).ToString(),
1393 child->GetBoundsInScreen().origin().ToString());
1394 // The |child| should still be a transient child of |window|.
1395 EXPECT_EQ(window.get(), ::wm::GetTransientParent(child.get()));
1396 }
1397
1398 // Tests that side snapping a window undocks it, closes the dock and then snaps.
1399 TEST_P(DockedWindowResizerTest, SideSnapDocked) {
1400 if (test_panels())
1401 return;
1402
1403 std::unique_ptr<aura::Window> w1(CreateTestWindow(gfx::Rect(0, 0, 201, 201)));
1404 wm::WindowState* window_state = wm::GetWindowState(w1.get());
1405 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
1406 // A window should be docked at the right edge.
1407 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
1408 w1->GetBoundsInScreen().right());
1409 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1410 DockedWindowLayoutManager* manager =
1411 DockedWindowLayoutManager::Get(WmWindow::Get(w1.get()));
1412 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1413 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1414 EXPECT_TRUE(window_state->IsDocked());
1415 EXPECT_FALSE(window_state->IsSnapped());
1416
1417 // Side snap at right edge.
1418 const wm::WMEvent snap_right(wm::WM_EVENT_SNAP_RIGHT);
1419 window_state->OnWMEvent(&snap_right);
1420 // The window should be snapped at the right edge and the dock should close.
1421 gfx::Rect work_area(ScreenUtil::GetDisplayWorkAreaBoundsInParent(w1.get()));
1422 EXPECT_EQ(0, docked_width(manager));
1423 EXPECT_EQ(work_area.height(), w1->bounds().height());
1424 EXPECT_EQ(work_area.right(), w1->bounds().right());
1425 EXPECT_EQ(kShellWindowId_DefaultContainer, w1->parent()->id());
1426 EXPECT_FALSE(window_state->IsDocked());
1427 EXPECT_TRUE(window_state->IsSnapped());
1428
1429 // Dock again.
1430 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, w1.get(), 20);
1431 // A window should be docked at the right edge.
1432 EXPECT_EQ(w1->GetRootWindow()->GetBoundsInScreen().right(),
1433 w1->GetBoundsInScreen().right());
1434 EXPECT_EQ(kShellWindowId_DockedContainer, w1->parent()->id());
1435 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1436 EXPECT_EQ(w1->bounds().width(), docked_width(manager));
1437 EXPECT_TRUE(window_state->IsDocked());
1438 EXPECT_FALSE(window_state->IsSnapped());
1439
1440 // Side snap at left edge.
1441 const wm::WMEvent snap_left(wm::WM_EVENT_SNAP_LEFT);
1442 window_state->OnWMEvent(&snap_left);
1443 // The window should be snapped at the right edge and the dock should close.
1444 EXPECT_EQ(work_area.ToString(),
1445 ScreenUtil::GetDisplayWorkAreaBoundsInParent(w1.get()).ToString());
1446 EXPECT_EQ(0, docked_width(manager));
1447 EXPECT_EQ(work_area.height(), w1->bounds().height());
1448 EXPECT_EQ(work_area.x(), w1->bounds().x());
1449 EXPECT_EQ(kShellWindowId_DefaultContainer, w1->parent()->id());
1450 EXPECT_FALSE(window_state->IsDocked());
1451 EXPECT_TRUE(window_state->IsSnapped());
1452 }
1453
1454 // Tests that a window is undocked if the window is maximized via a keyboard
1455 // accelerator during a drag.
1456 TEST_P(DockedWindowResizerTest, MaximizedDuringDrag) {
1457 if (test_panels())
1458 return;
1459
1460 std::unique_ptr<aura::Window> window(
1461 CreateTestWindow(gfx::Rect(0, 0, ideal_width(), 201)));
1462 wm::WindowState* window_state = wm::GetWindowState(window.get());
1463
1464 // Dock the window to the right edge.
1465 DragToVerticalPositionAndToEdge(DOCKED_EDGE_RIGHT, window.get(), 20);
1466 EXPECT_EQ(window->GetRootWindow()->GetBoundsInScreen().right(),
1467 window->GetBoundsInScreen().right());
1468 EXPECT_EQ(kShellWindowId_DockedContainer, window->parent()->id());
1469 DockedWindowLayoutManager* manager =
1470 DockedWindowLayoutManager::Get(WmWindow::Get(window.get()));
1471 EXPECT_EQ(DOCKED_ALIGNMENT_RIGHT, docked_alignment(manager));
1472 EXPECT_EQ(window->bounds().width(), docked_width(manager));
1473 EXPECT_TRUE(window_state->IsDocked());
1474
1475 // Maximize the window while in a real drag. In particular,
1476 // ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowStateTypeChanged()
1477 // must be called in order for the maximized window's size to be correct.
1478 delegate()->set_window_component(HTCAPTION);
1479 ui::test::EventGenerator& generator = GetEventGenerator();
1480 generator.MoveMouseTo(window->GetBoundsInScreen().origin());
1481 generator.PressLeftButton();
1482 generator.MoveMouseBy(10, 10);
1483 window_state->Maximize();
1484 generator.ReleaseLeftButton();
1485
1486 // |window| should get undocked.
1487 EXPECT_EQ(kShellWindowId_DefaultContainer, window->parent()->id());
1488 EXPECT_EQ(0, docked_width(manager));
1489 EXPECT_EQ(
1490 ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
1491 window->bounds().ToString());
1492 EXPECT_TRUE(window_state->IsMaximized());
1493 }
1494
1495 // Tests run twice - on both panels and normal windows
1496 INSTANTIATE_TEST_CASE_P(NormalOrPanel,
1497 DockedWindowResizerTest,
1498 testing::Values(ui::wm::WINDOW_TYPE_NORMAL,
1499 ui::wm::WINDOW_TYPE_PANEL));
1500
1501 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698