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

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

Powered by Google App Engine
This is Rietveld 408576698