Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ash / wm / workspace / workspace_manager_unittest.cc
blob26072513b1e09687a1ffe07525e3db79b5acfe47
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/wm/workspace/workspace_manager.h"
7 #include <map>
9 #include "ash/ash_switches.h"
10 #include "ash/root_window_controller.h"
11 #include "ash/screen_ash.h"
12 #include "ash/shelf/shelf_layout_manager.h"
13 #include "ash/shelf/shelf_widget.h"
14 #include "ash/shell.h"
15 #include "ash/shell_window_ids.h"
16 #include "ash/system/status_area_widget.h"
17 #include "ash/test/ash_test_base.h"
18 #include "ash/test/shell_test_api.h"
19 #include "ash/wm/activation_controller.h"
20 #include "ash/wm/property_util.h"
21 #include "ash/wm/window_properties.h"
22 #include "ash/wm/window_util.h"
23 #include "ash/wm/workspace/workspace.h"
24 #include "ash/wm/workspace_controller_test_helper.h"
25 #include "base/command_line.h"
26 #include "base/string_number_conversions.h"
27 #include "ui/aura/client/aura_constants.h"
28 #include "ui/aura/root_window.h"
29 #include "ui/aura/test/event_generator.h"
30 #include "ui/aura/test/test_window_delegate.h"
31 #include "ui/aura/test/test_windows.h"
32 #include "ui/aura/window.h"
33 #include "ui/base/hit_test.h"
34 #include "ui/base/ui_base_types.h"
35 #include "ui/compositor/layer.h"
36 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
37 #include "ui/gfx/screen.h"
38 #include "ui/views/widget/widget.h"
40 using aura::Window;
42 namespace ash {
43 namespace internal {
45 // Returns a string containing the names of all the children of |window| (in
46 // order). Each entry is separated by a space.
47 std::string GetWindowNames(const aura::Window* window) {
48 std::string result;
49 for (size_t i = 0; i < window->children().size(); ++i) {
50 if (i != 0)
51 result += " ";
52 result += window->children()[i]->name();
54 return result;
57 // Returns a string containing the names of windows corresponding to each of the
58 // child layers of |window|'s layer. Any layers that don't correspond to a child
59 // Window of |window| are ignored. The result is ordered based on the layer
60 // ordering.
61 std::string GetLayerNames(const aura::Window* window) {
62 typedef std::map<const ui::Layer*, std::string> LayerToWindowNameMap;
63 LayerToWindowNameMap window_names;
64 for (size_t i = 0; i < window->children().size(); ++i) {
65 window_names[window->children()[i]->layer()] =
66 window->children()[i]->name();
69 std::string result;
70 const std::vector<ui::Layer*>& layers(window->layer()->children());
71 for (size_t i = 0; i < layers.size(); ++i) {
72 LayerToWindowNameMap::iterator layer_i =
73 window_names.find(layers[i]);
74 if (layer_i != window_names.end()) {
75 if (!result.empty())
76 result += " ";
77 result += layer_i->second;
80 return result;
83 class WorkspaceManagerTest : public test::AshTestBase {
84 public:
85 WorkspaceManagerTest() : manager_(NULL) {}
86 virtual ~WorkspaceManagerTest() {}
88 aura::Window* CreateTestWindowUnparented() {
89 aura::Window* window = new aura::Window(NULL);
90 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
91 window->SetType(aura::client::WINDOW_TYPE_NORMAL);
92 window->Init(ui::LAYER_TEXTURED);
93 return window;
96 aura::Window* CreateTestWindow() {
97 aura::Window* window = new aura::Window(NULL);
98 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
99 window->SetType(aura::client::WINDOW_TYPE_NORMAL);
100 window->Init(ui::LAYER_TEXTURED);
101 SetDefaultParentByPrimaryRootWindow(window);
102 return window;
105 aura::Window* GetViewport() {
106 return Shell::GetContainer(Shell::GetPrimaryRootWindow(),
107 kShellWindowId_DefaultContainer);
110 const std::vector<Workspace*>& workspaces() const {
111 return manager_->workspaces_;
114 gfx::Rect GetFullscreenBounds(aura::Window* window) {
115 return Shell::GetScreen()->GetDisplayNearestWindow(window).bounds();
118 Workspace* active_workspace() {
119 return manager_->active_workspace_;
122 ShelfWidget* shelf_widget() {
123 return Shell::GetPrimaryRootWindowController()->shelf();
126 ShelfLayoutManager* shelf_layout_manager() {
127 return Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager();
130 bool GetWindowOverlapsShelf() {
131 return shelf_layout_manager()->window_overlaps_shelf();
134 Workspace* FindBy(aura::Window* window) const {
135 return manager_->FindBy(window);
138 std::string WorkspaceStateString(Workspace* workspace) {
139 return (workspace->is_maximized() ? "M" : "") +
140 base::IntToString(static_cast<int>(
141 workspace->window()->children().size()));
144 int active_index() {
145 return static_cast<int>(
146 manager_->FindWorkspace(manager_->active_workspace_) -
147 manager_->workspaces_.begin());
150 // Returns a string description of the current state. The string has the
151 // following format:
152 // W* P=W* active=N
153 // Each W corresponds to a workspace. Each workspace is prefixed with an 'M'
154 // if the workspace is maximized and is followed by the number of windows in
155 // the workspace.
156 // 'P=' is used for the pending workspaces (see
157 // WorkspaceManager::pending_workspaces_ for details on pending workspaces).
158 // N is the index of the active workspace (index into
159 // WorkspaceManager::workspaces_).
160 // For example, '2 M1 P=M1 active=1' means the first workspace (the desktop)
161 // has 2 windows, the second workspace is a maximized workspace with 1 window,
162 // there is a pending maximized workspace with 1 window and the second
163 // workspace is active.
164 std::string StateString() {
165 std::string result;
166 for (size_t i = 0; i < manager_->workspaces_.size(); ++i) {
167 if (i > 0)
168 result += " ";
169 result += WorkspaceStateString(manager_->workspaces_[i]);
172 if (!manager_->pending_workspaces_.empty()) {
173 result += " P=";
174 for (std::set<Workspace*>::const_iterator i =
175 manager_->pending_workspaces_.begin();
176 i != manager_->pending_workspaces_.end(); ++i) {
177 if (i != manager_->pending_workspaces_.begin())
178 result += " ";
179 result += WorkspaceStateString(*i);
183 result += " active=" + base::IntToString(active_index());
184 return result;
187 // Overridden from AshTestBase:
188 virtual void SetUp() OVERRIDE {
189 test::AshTestBase::SetUp();
190 WorkspaceControllerTestHelper workspace_helper(
191 test::ShellTestApi(Shell::GetInstance()).workspace_controller());
192 manager_ = workspace_helper.workspace_manager();
195 virtual void TearDown() OVERRIDE {
196 manager_ = NULL;
197 test::AshTestBase::TearDown();
200 protected:
201 WorkspaceManager* manager_;
203 private:
204 scoped_ptr<ActivationController> activation_controller_;
206 DISALLOW_COPY_AND_ASSIGN(WorkspaceManagerTest);
209 // Assertions around adding a normal window.
210 TEST_F(WorkspaceManagerTest, AddNormalWindowWhenEmpty) {
211 scoped_ptr<Window> w1(CreateTestWindow());
212 w1->SetBounds(gfx::Rect(0, 0, 250, 251));
214 EXPECT_TRUE(GetRestoreBoundsInScreen(w1.get()) == NULL);
216 w1->Show();
218 EXPECT_TRUE(GetRestoreBoundsInScreen(w1.get()) == NULL);
220 ASSERT_TRUE(w1->layer() != NULL);
221 EXPECT_TRUE(w1->layer()->visible());
223 EXPECT_EQ("0,0 250x251", w1->bounds().ToString());
225 // Should be 1 workspace for the desktop, not maximized.
226 ASSERT_EQ("1 active=0", StateString());
227 EXPECT_EQ(w1.get(), workspaces()[0]->window()->children()[0]);
230 // Assertions around maximizing/unmaximizing.
231 TEST_F(WorkspaceManagerTest, SingleMaximizeWindow) {
232 scoped_ptr<Window> w1(CreateTestWindow());
233 w1->SetBounds(gfx::Rect(0, 0, 250, 251));
235 w1->Show();
236 wm::ActivateWindow(w1.get());
238 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
240 ASSERT_TRUE(w1->layer() != NULL);
241 EXPECT_TRUE(w1->layer()->visible());
243 EXPECT_EQ("0,0 250x251", w1->bounds().ToString());
245 // Maximize the window.
246 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
248 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
250 // Should be 2 workspaces, the second maximized with w1.
251 ASSERT_EQ("0 M1 active=1", StateString());
252 EXPECT_EQ(w1.get(), workspaces()[1]->window()->children()[0]);
253 EXPECT_EQ(ScreenAsh::GetMaximizedWindowBoundsInParent(w1.get()).width(),
254 w1->bounds().width());
255 EXPECT_EQ(ScreenAsh::GetMaximizedWindowBoundsInParent(w1.get()).height(),
256 w1->bounds().height());
258 // Restore the window.
259 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
261 // Should be 1 workspace for the desktop.
262 ASSERT_EQ("1 active=0", StateString());
263 EXPECT_EQ(w1.get(), workspaces()[0]->window()->children()[0]);
264 EXPECT_EQ("0,0 250x251", w1->bounds().ToString());
267 // Assertions around closing the last window in a workspace.
268 TEST_F(WorkspaceManagerTest, CloseLastWindowInWorkspace) {
269 scoped_ptr<Window> w1(CreateTestWindow());
270 scoped_ptr<Window> w2(CreateTestWindow());
271 w1->SetBounds(gfx::Rect(0, 0, 250, 251));
272 w1->Show();
273 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
274 w2->Show();
275 wm::ActivateWindow(w1.get());
277 // Should be 1 workspace and 1 pending, !maximized and maximized. The second
278 // workspace is pending since the window wasn't active.
279 ASSERT_EQ("1 P=M1 active=0", StateString());
280 EXPECT_EQ(w1.get(), workspaces()[0]->window()->children()[0]);
282 // Close w2.
283 w2.reset();
285 // Should have one workspace.
286 ASSERT_EQ("1 active=0", StateString());
287 EXPECT_EQ(w1.get(), workspaces()[0]->window()->children()[0]);
288 EXPECT_TRUE(w1->IsVisible());
291 // Assertions around adding a maximized window when empty.
292 TEST_F(WorkspaceManagerTest, AddMaximizedWindowWhenEmpty) {
293 scoped_ptr<Window> w1(CreateTestWindow());
294 w1->SetBounds(gfx::Rect(0, 0, 250, 251));
295 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
296 w1->Show();
297 wm::ActivateWindow(w1.get());
299 ASSERT_TRUE(w1->layer() != NULL);
300 EXPECT_TRUE(w1->layer()->visible());
301 gfx::Rect work_area(
302 ScreenAsh::GetMaximizedWindowBoundsInParent(w1.get()));
303 EXPECT_EQ(work_area.width(), w1->bounds().width());
304 EXPECT_EQ(work_area.height(), w1->bounds().height());
306 // Should be 2 workspaces (since we always keep the desktop).
307 ASSERT_EQ("0 M1 active=1", StateString());
308 EXPECT_EQ(w1.get(), workspaces()[1]->window()->children()[0]);
311 // Assertions around two windows and toggling one to be maximized.
312 TEST_F(WorkspaceManagerTest, MaximizeWithNormalWindow) {
313 scoped_ptr<Window> w1(CreateTestWindow());
314 scoped_ptr<Window> w2(CreateTestWindow());
315 w1->SetBounds(gfx::Rect(0, 0, 250, 251));
316 w1->Show();
318 ASSERT_TRUE(w1->layer() != NULL);
319 EXPECT_TRUE(w1->layer()->visible());
321 w2->SetBounds(gfx::Rect(0, 0, 50, 51));
322 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
323 w2->Show();
324 wm::ActivateWindow(w2.get());
326 // Should now be two workspaces.
327 ASSERT_EQ("1 M1 active=1", StateString());
328 EXPECT_EQ(w1.get(), workspaces()[0]->window()->children()[0]);
329 EXPECT_EQ(w2.get(), workspaces()[1]->window()->children()[0]);
331 gfx::Rect work_area(ScreenAsh::GetMaximizedWindowBoundsInParent(w1.get()));
332 EXPECT_EQ(work_area.width(), w2->bounds().width());
333 EXPECT_EQ(work_area.height(), w2->bounds().height());
335 // Restore w2, which should then go back to one workspace.
336 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
337 ASSERT_EQ("2 active=0", StateString());
338 EXPECT_EQ(w1.get(), workspaces()[0]->window()->children()[0]);
339 EXPECT_EQ(w2.get(), workspaces()[0]->window()->children()[1]);
340 EXPECT_EQ(50, w2->bounds().width());
341 EXPECT_EQ(51, w2->bounds().height());
342 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
345 // Assertions around two maximized windows.
346 TEST_F(WorkspaceManagerTest, TwoMaximized) {
347 scoped_ptr<Window> w1(CreateTestWindow());
348 scoped_ptr<Window> w2(CreateTestWindow());
349 w1->SetBounds(gfx::Rect(0, 0, 250, 251));
350 w1->Show();
351 wm::ActivateWindow(w1.get());
352 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
353 ASSERT_EQ("1 M1 active=1", StateString());
355 w2->SetBounds(gfx::Rect(0, 0, 50, 51));
356 w2->Show();
357 wm::ActivateWindow(w2.get());
358 ASSERT_EQ("1 M1 active=0", StateString());
360 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
361 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
362 ASSERT_EQ("0 M1 M1 active=2", StateString());
364 // The last stacked window (|w2|) should be last since it was maximized last.
365 EXPECT_EQ(w1.get(), workspaces()[1]->window()->children()[0]);
366 EXPECT_EQ(w2.get(), workspaces()[2]->window()->children()[0]);
369 // Makes sure requests to change the bounds of a normal window go through.
370 TEST_F(WorkspaceManagerTest, ChangeBoundsOfNormalWindow) {
371 scoped_ptr<Window> w1(CreateTestWindow());
372 w1->Show();
374 // Setting the bounds should go through since the window is in the normal
375 // workspace.
376 w1->SetBounds(gfx::Rect(0, 0, 200, 500));
377 EXPECT_EQ(200, w1->bounds().width());
378 EXPECT_EQ(500, w1->bounds().height());
381 // Verifies the bounds is not altered when showing and grid is enabled.
382 TEST_F(WorkspaceManagerTest, SnapToGrid) {
383 scoped_ptr<Window> w1(CreateTestWindowUnparented());
384 w1->SetBounds(gfx::Rect(1, 6, 25, 30));
385 SetDefaultParentByPrimaryRootWindow(w1.get());
386 // We are not aligning this anymore this way. When the window gets shown
387 // the window is expected to be handled differently, but this cannot be
388 // tested with this test. So the result of this test should be that the
389 // bounds are exactly as passed in.
390 EXPECT_EQ("1,6 25x30", w1->bounds().ToString());
393 // Assertions around a fullscreen window.
394 TEST_F(WorkspaceManagerTest, SingleFullscreenWindow) {
395 scoped_ptr<Window> w1(CreateTestWindow());
396 w1->SetBounds(gfx::Rect(0, 0, 250, 251));
397 // Make the window fullscreen.
398 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
399 w1->Show();
400 wm::ActivateWindow(w1.get());
402 // Should be 2 workspaces, normal and maximized.
403 ASSERT_EQ("0 M1 active=1", StateString());
404 EXPECT_EQ(w1.get(), workspaces()[1]->window()->children()[0]);
405 EXPECT_EQ(GetFullscreenBounds(w1.get()).width(), w1->bounds().width());
406 EXPECT_EQ(GetFullscreenBounds(w1.get()).height(), w1->bounds().height());
408 // Restore the window. Use SHOW_STATE_DEFAULT as that is what we'll end up
409 // with when using views::Widget.
410 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_DEFAULT);
411 EXPECT_EQ("0,0 250x251", w1->bounds().ToString());
413 // Should be 1 workspace for the desktop.
414 ASSERT_EQ("1 active=0", StateString());
415 EXPECT_EQ(w1.get(), workspaces()[0]->window()->children()[0]);
416 EXPECT_EQ(250, w1->bounds().width());
417 EXPECT_EQ(251, w1->bounds().height());
419 // Back to fullscreen.
420 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
421 ASSERT_EQ("0 M1 active=1", StateString());
422 EXPECT_EQ(w1.get(), workspaces()[1]->window()->children()[0]);
423 EXPECT_EQ(GetFullscreenBounds(w1.get()).width(), w1->bounds().width());
424 EXPECT_EQ(GetFullscreenBounds(w1.get()).height(), w1->bounds().height());
425 ASSERT_TRUE(GetRestoreBoundsInScreen(w1.get()));
426 EXPECT_EQ("0,0 250x251", GetRestoreBoundsInScreen(w1.get())->ToString());
429 // Makes sure switching workspaces doesn't show transient windows.
430 TEST_F(WorkspaceManagerTest, DontShowTransientsOnSwitch) {
431 scoped_ptr<Window> w1(CreateTestWindow());
432 scoped_ptr<Window> w2(CreateTestWindow());
434 w1->SetBounds(gfx::Rect(0, 0, 250, 251));
435 w2->SetBounds(gfx::Rect(0, 0, 250, 251));
436 w1->AddTransientChild(w2.get());
438 w1->Show();
440 scoped_ptr<Window> w3(CreateTestWindow());
441 w3->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
442 w3->Show();
443 wm::ActivateWindow(w3.get());
445 EXPECT_FALSE(w1->layer()->IsDrawn());
446 EXPECT_FALSE(w2->layer()->IsDrawn());
447 EXPECT_TRUE(w3->layer()->IsDrawn());
449 wm::ActivateWindow(w1.get());
450 EXPECT_TRUE(w1->layer()->IsDrawn());
451 EXPECT_FALSE(w2->layer()->IsDrawn());
452 EXPECT_FALSE(w3->layer()->IsDrawn());
455 // Assertions around minimizing a single window.
456 TEST_F(WorkspaceManagerTest, MinimizeSingleWindow) {
457 scoped_ptr<Window> w1(CreateTestWindow());
459 w1->Show();
460 ASSERT_EQ("1 active=0", StateString());
462 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
463 ASSERT_EQ("1 active=0", StateString());
464 EXPECT_FALSE(w1->layer()->IsDrawn());
466 // Show the window.
467 w1->Show();
468 EXPECT_TRUE(wm::IsWindowNormal(w1.get()));
469 ASSERT_EQ("1 active=0", StateString());
470 EXPECT_TRUE(w1->layer()->IsDrawn());
473 // Assertions around minimizing a maximized window.
474 TEST_F(WorkspaceManagerTest, MinimizeMaximizedWindow) {
475 // Two windows, w1 normal, w2 maximized.
476 scoped_ptr<Window> w1(CreateTestWindow());
477 scoped_ptr<Window> w2(CreateTestWindow());
478 w1->Show();
479 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
480 w2->Show();
481 wm::ActivateWindow(w2.get());
482 ASSERT_EQ("1 M1 active=1", StateString());
484 // Minimize w2.
485 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
486 ASSERT_EQ("1 P=M1 active=0", StateString());
487 EXPECT_TRUE(w1->layer()->IsDrawn());
488 EXPECT_FALSE(w2->layer()->IsDrawn());
490 // Show the window, which should trigger unminimizing.
491 w2->Show();
492 ASSERT_EQ("1 P=M1 active=0", StateString());
494 wm::ActivateWindow(w2.get());
495 ASSERT_EQ("1 M1 active=1", StateString());
497 EXPECT_TRUE(wm::IsWindowMaximized(w2.get()));
498 EXPECT_FALSE(w1->layer()->IsDrawn());
499 EXPECT_TRUE(w2->layer()->IsDrawn());
501 // Minimize the window, which should hide the window and activate another.
502 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
503 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
504 EXPECT_FALSE(wm::IsActiveWindow(w2.get()));
505 EXPECT_FALSE(w2->layer()->IsDrawn());
506 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
508 // Make the window normal.
509 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
510 ASSERT_EQ("2 active=0", StateString());
511 EXPECT_EQ(w1.get(), workspaces()[0]->window()->children()[0]);
512 EXPECT_EQ(w2.get(), workspaces()[0]->window()->children()[1]);
513 EXPECT_TRUE(w2->layer()->IsDrawn());
516 // Verifies ShelfLayoutManager's visibility/auto-hide state is correctly
517 // updated.
518 TEST_F(WorkspaceManagerTest, ShelfStateUpdated) {
519 // Since ShelfLayoutManager queries for mouse location, move the mouse so
520 // it isn't over the shelf.
521 aura::test::EventGenerator generator(
522 Shell::GetPrimaryRootWindow(), gfx::Point());
523 generator.MoveMouseTo(0, 0);
525 scoped_ptr<Window> w1(CreateTestWindow());
526 const gfx::Rect w1_bounds(0, 1, 101, 102);
527 ShelfLayoutManager* shelf = shelf_layout_manager();
528 shelf->SetAutoHideBehavior(ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
529 const gfx::Rect touches_shelf_bounds(
530 0, shelf->GetIdealBounds().y() - 10, 101, 102);
531 // Move |w1| to overlap the shelf.
532 w1->SetBounds(touches_shelf_bounds);
533 EXPECT_FALSE(GetWindowOverlapsShelf());
535 // A visible ignored window should not trigger the overlap.
536 scoped_ptr<Window> w_ignored(CreateTestWindow());
537 w_ignored->SetBounds(touches_shelf_bounds);
538 SetIgnoredByShelf(&(*w_ignored), true);
539 w_ignored->Show();
540 EXPECT_FALSE(GetWindowOverlapsShelf());
542 // Make it visible, since visible shelf overlaps should be true.
543 w1->Show();
544 EXPECT_TRUE(GetWindowOverlapsShelf());
546 wm::ActivateWindow(w1.get());
547 w1->SetBounds(w1_bounds);
548 w1->Show();
549 wm::ActivateWindow(w1.get());
551 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
553 // Maximize the window.
554 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
555 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
556 EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state());
558 // Restore.
559 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
560 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
561 EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
563 // Fullscreen.
564 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
565 EXPECT_EQ(SHELF_HIDDEN, shelf->visibility_state());
567 // Normal.
568 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
569 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
570 EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
571 EXPECT_FALSE(GetWindowOverlapsShelf());
573 // Move window so it obscures shelf.
574 w1->SetBounds(touches_shelf_bounds);
575 EXPECT_TRUE(GetWindowOverlapsShelf());
577 // Move it back.
578 w1->SetBounds(w1_bounds);
579 EXPECT_FALSE(GetWindowOverlapsShelf());
581 // Maximize again.
582 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
583 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
584 EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state());
586 // Minimize.
587 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
588 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
590 // Since the restore from minimize will restore to the pre-minimize
591 // state (tested elsewhere), we abandon the current size and restore
592 // rect and set them to the window.
593 gfx::Rect restore = *GetRestoreBoundsInScreen(w1.get());
594 EXPECT_EQ("0,0 800x597", w1->bounds().ToString());
595 EXPECT_EQ("0,1 101x102", restore.ToString());
596 ClearRestoreBounds(w1.get());
597 w1->SetBounds(restore);
599 // Restore.
600 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
601 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
602 EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
604 // Create another window, maximized.
605 scoped_ptr<Window> w2(CreateTestWindow());
606 w2->SetBounds(gfx::Rect(10, 11, 250, 251));
607 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
608 w2->Show();
609 wm::ActivateWindow(w2.get());
610 EXPECT_EQ(1, active_index());
611 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
612 EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state());
613 EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
615 // Switch to w1.
616 wm::ActivateWindow(w1.get());
617 EXPECT_EQ(0, active_index());
618 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
619 EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
620 EXPECT_EQ(ScreenAsh::GetMaximizedWindowBoundsInParent(
621 w2->parent()).ToString(),
622 w2->bounds().ToString());
624 // Switch to w2.
625 wm::ActivateWindow(w2.get());
626 EXPECT_EQ(1, active_index());
627 EXPECT_EQ(SHELF_AUTO_HIDE, shelf->visibility_state());
628 EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state());
629 EXPECT_EQ("0,1 101x102", w1->bounds().ToString());
630 EXPECT_EQ(ScreenAsh::GetMaximizedWindowBoundsInParent(w2.get()).ToString(),
631 w2->bounds().ToString());
633 // Turn off auto-hide, switch back to w2 (maximized) and verify overlap.
634 shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
635 wm::ActivateWindow(w2.get());
636 EXPECT_FALSE(GetWindowOverlapsShelf());
638 // Move w1 to overlap shelf, it shouldn't change window overlaps shelf since
639 // the window isn't in the visible workspace.
640 w1->SetBounds(touches_shelf_bounds);
641 EXPECT_FALSE(GetWindowOverlapsShelf());
643 // Activate w1. Since w1 is visible the overlap state should be true.
644 wm::ActivateWindow(w1.get());
645 EXPECT_TRUE(GetWindowOverlapsShelf());
648 // Verifies persist across all workspaces.
649 TEST_F(WorkspaceManagerTest, PersistAcrossAllWorkspaces) {
650 // Create a maximized window.
651 scoped_ptr<Window> w1(CreateTestWindow());
652 w1->Show();
653 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
654 wm::ActivateWindow(w1.get());
655 ASSERT_EQ("0 M1 active=1", StateString());
657 // Create a window that persists across all workspaces. It should be placed in
658 // the current maximized workspace.
659 scoped_ptr<Window> w2(CreateTestWindow());
660 SetPersistsAcrossAllWorkspaces(
661 w2.get(),
662 WINDOW_PERSISTS_ACROSS_ALL_WORKSPACES_VALUE_YES);
663 w2->Show();
664 ASSERT_EQ("1 M1 active=1", StateString());
666 // Activate w2, which should move it to the 2nd workspace.
667 wm::ActivateWindow(w2.get());
668 ASSERT_EQ("0 M2 active=1", StateString());
670 // Restoring w2 should drop the persists window back to the desktop, and drop
671 // it to the bottom of the stack.
672 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
673 ASSERT_EQ("2 active=0", StateString());
674 EXPECT_EQ(w2.get(), workspaces()[0]->window()->children()[0]);
675 EXPECT_EQ(w1.get(), workspaces()[0]->window()->children()[1]);
677 // Repeat, but this time minimize. The minimized window should end up in
678 // pending.
679 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
680 ASSERT_EQ("1 P=M1 active=0", StateString());
681 w2.reset(CreateTestWindow());
682 SetPersistsAcrossAllWorkspaces(
683 w2.get(),
684 WINDOW_PERSISTS_ACROSS_ALL_WORKSPACES_VALUE_YES);
685 w2->Show();
686 ASSERT_EQ("1 P=M1 active=0", StateString());
687 wm::ActivateWindow(w2.get());
688 ASSERT_EQ("1 P=M1 active=0", StateString());
689 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
690 ASSERT_EQ("1 P=M1 active=0", StateString());
691 EXPECT_EQ(w2.get(), workspaces()[0]->window()->children()[0]);
694 // Verifies that when a window persists across all workpaces is activated that
695 // it moves to the current workspace.
696 TEST_F(WorkspaceManagerTest, ActivatePersistAcrossAllWorkspacesWhenNotActive) {
697 // Create a window that persists across all workspaces.
698 scoped_ptr<Window> w2(CreateTestWindow());
699 SetPersistsAcrossAllWorkspaces(
700 w2.get(),
701 WINDOW_PERSISTS_ACROSS_ALL_WORKSPACES_VALUE_YES);
702 w2->Show();
703 ASSERT_EQ("1 active=0", StateString());
705 // Create a maximized window.
706 scoped_ptr<Window> w1(CreateTestWindow());
707 w1->Show();
708 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
709 wm::ActivateWindow(w1.get());
710 ASSERT_EQ("1 M1 active=1", StateString());
712 // Activate the persists across all workspace window. It should move to the
713 // current workspace.
714 wm::ActivateWindow(w2.get());
715 ASSERT_EQ("0 M2 active=1", StateString());
716 // The window that persists across all workspaces should be moved to the top
717 // of the stacking order.
718 EXPECT_EQ(w1.get(), workspaces()[1]->window()->children()[0]);
719 EXPECT_EQ(w2.get(), workspaces()[1]->window()->children()[1]);
720 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
723 // Verifies Show()ing a minimized window that persists across all workspaces
724 // unminimizes the window.
725 TEST_F(WorkspaceManagerTest, ShowMinimizedPersistWindow) {
726 // Create a window that persists across all workspaces.
727 scoped_ptr<Window> w1(CreateTestWindow());
728 SetPersistsAcrossAllWorkspaces(
729 w1.get(),
730 WINDOW_PERSISTS_ACROSS_ALL_WORKSPACES_VALUE_YES);
731 w1->Show();
732 wm::ActivateWindow(w1.get());
733 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
734 EXPECT_FALSE(w1->IsVisible());
735 w1->Show();
736 EXPECT_TRUE(w1->IsVisible());
739 // Test that a persistent window across all workspaces which was first
740 // maximized, then got minimized and finally got restored does not crash the
741 // system (see http://crbug.com/151698) and restores its maximized workspace
742 // instead.
743 TEST_F(WorkspaceManagerTest, MaximizeMinimizeRestoreDoesNotCrash) {
744 // We need to create a regular window first so there's an active workspace.
745 scoped_ptr<Window> w1(CreateTestWindow());
746 w1->Show();
748 // Create a window that persists across all workspaces.
749 scoped_ptr<Window> w2(CreateTestWindow());
750 SetPersistsAcrossAllWorkspaces(
751 w2.get(),
752 WINDOW_PERSISTS_ACROSS_ALL_WORKSPACES_VALUE_YES);
753 w2->Show();
754 wm::ActivateWindow(w2.get());
755 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
756 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
757 EXPECT_FALSE(w2->IsVisible());
758 // This is the critical call which should switch to the maximized workspace
759 // of that window instead of reparenting it to the other workspace (and
760 // crashing while trying to do so).
761 wm::ActivateWindow(w2.get());
762 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED,
763 w2->GetProperty(aura::client::kShowStateKey));
764 EXPECT_TRUE(w2->IsVisible());
767 // Test that we report we're in the fullscreen state even if the fullscreen
768 // window isn't being managed by us (http://crbug.com/123931).
769 TEST_F(WorkspaceManagerTest, GetWindowStateWithUnmanagedFullscreenWindow) {
770 ShelfLayoutManager* shelf = shelf_layout_manager();
772 // We need to create a regular window first so there's an active workspace.
773 scoped_ptr<Window> w1(CreateTestWindow());
774 w1->Show();
776 scoped_ptr<Window> w2(CreateTestWindow());
777 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
778 SetPersistsAcrossAllWorkspaces(
779 w2.get(),
780 WINDOW_PERSISTS_ACROSS_ALL_WORKSPACES_VALUE_YES);
781 w2->Show();
782 wm::ActivateWindow(w2.get());
784 ASSERT_EQ("1 M1 active=1", StateString());
786 EXPECT_EQ(SHELF_HIDDEN, shelf->visibility_state());
787 EXPECT_EQ(WORKSPACE_WINDOW_STATE_FULL_SCREEN, manager_->GetWindowState());
789 w2->Hide();
790 ASSERT_EQ("1 P=M1 active=0", StateString());
791 EXPECT_EQ(SHELF_VISIBLE, shelf->visibility_state());
793 w2->Show();
794 ASSERT_EQ("1 P=M1 active=0", StateString());
795 EXPECT_EQ(SHELF_VISIBLE, shelf->visibility_state());
796 EXPECT_EQ(WORKSPACE_WINDOW_STATE_DEFAULT, manager_->GetWindowState());
798 wm::ActivateWindow(w2.get());
799 ASSERT_EQ("1 M1 active=1", StateString());
800 EXPECT_EQ(SHELF_HIDDEN, shelf->visibility_state());
801 EXPECT_EQ(WORKSPACE_WINDOW_STATE_FULL_SCREEN, manager_->GetWindowState());
803 w2.reset();
804 ASSERT_EQ("1 active=0", StateString());
805 EXPECT_EQ(SHELF_VISIBLE, shelf->visibility_state());
806 EXPECT_EQ(WORKSPACE_WINDOW_STATE_DEFAULT, manager_->GetWindowState());
809 // Variant of GetWindowStateWithUnmanagedFullscreenWindow that uses a maximized
810 // window rather than a normal window.
811 TEST_F(WorkspaceManagerTest,
812 GetWindowStateWithUnmanagedFullscreenWindowWithMaximized) {
813 ShelfLayoutManager* shelf = shelf_layout_manager();
814 shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
816 // Make the first window maximized.
817 scoped_ptr<Window> w1(CreateTestWindow());
818 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
819 w1->Show();
821 scoped_ptr<Window> w2(CreateTestWindow());
822 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
823 SetPersistsAcrossAllWorkspaces(
824 w2.get(),
825 WINDOW_PERSISTS_ACROSS_ALL_WORKSPACES_VALUE_YES);
826 w2->Show();
827 wm::ActivateWindow(w2.get());
829 // Even though auto-hide behavior is NEVER full-screen windows cause the shelf
830 // to hide.
831 EXPECT_EQ(SHELF_HIDDEN, shelf->visibility_state());
832 EXPECT_EQ(WORKSPACE_WINDOW_STATE_FULL_SCREEN,
833 manager_->GetWindowState());
835 w2->Hide();
836 EXPECT_EQ(SHELF_VISIBLE, shelf->visibility_state());
838 w2->Show();
839 wm::ActivateWindow(w2.get());
840 EXPECT_EQ(SHELF_HIDDEN, shelf->visibility_state());
841 EXPECT_EQ(WORKSPACE_WINDOW_STATE_FULL_SCREEN,
842 manager_->GetWindowState());
844 w2.reset();
845 EXPECT_EQ(SHELF_VISIBLE, shelf->visibility_state());
848 // Verifies a window marked as persisting across all workspaces ends up in its
849 // own workspace when maximized.
850 TEST_F(WorkspaceManagerTest, MaximizeDontPersistEndsUpInOwnWorkspace) {
851 scoped_ptr<Window> w1(CreateTestWindow());
853 SetPersistsAcrossAllWorkspaces(
854 w1.get(),
855 WINDOW_PERSISTS_ACROSS_ALL_WORKSPACES_VALUE_YES);
856 w1->Show();
858 ASSERT_EQ("1 active=0", StateString());
860 // Maximize should trigger containing the window.
861 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
862 ASSERT_EQ("0 P=M1 active=0", StateString());
864 // And resetting to normal should remove it.
865 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
866 ASSERT_EQ("1 active=0", StateString());
869 // Verifies going from maximized to minimized sets the right state for painting
870 // the background of the launcher.
871 TEST_F(WorkspaceManagerTest, MinimizeResetsVisibility) {
872 scoped_ptr<Window> w1(CreateTestWindow());
873 w1->Show();
874 wm::ActivateWindow(w1.get());
875 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
876 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
877 EXPECT_EQ(SHELF_VISIBLE,
878 shelf_layout_manager()->visibility_state());
879 EXPECT_FALSE(shelf_widget()->paints_background());
882 // Verifies transients are moved when maximizing.
883 TEST_F(WorkspaceManagerTest, MoveTransientOnMaximize) {
884 scoped_ptr<Window> w1(CreateTestWindow());
885 w1->Show();
886 scoped_ptr<Window> w2(CreateTestWindow());
887 w1->AddTransientChild(w2.get());
888 w2->Show();
889 wm::ActivateWindow(w1.get());
890 ASSERT_EQ("2 active=0", StateString());
892 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
893 ASSERT_EQ("0 M2 active=1", StateString());
894 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
896 // Create another transient child of |w1|. We do this unparented, set up the
897 // transient parent then set parent. This is how NativeWidgetAura does things
898 // too.
899 scoped_ptr<Window> w3(CreateTestWindowUnparented());
900 w1->AddTransientChild(w3.get());
901 SetDefaultParentByPrimaryRootWindow(w3.get());
902 w3->Show();
903 ASSERT_EQ("0 M3 active=1", StateString());
905 // Minimize the window. All the transients are hidden as a result, so it ends
906 // up in pending.
907 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
908 ASSERT_EQ("0 P=M3 active=0", StateString());
910 // Restore and everything should go back to the first workspace.
911 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
912 ASSERT_EQ("3 active=0", StateString());
915 // Verifies window visibility during various workspace changes.
916 TEST_F(WorkspaceManagerTest, VisibilityTests) {
917 scoped_ptr<Window> w1(CreateTestWindow());
918 w1->Show();
919 EXPECT_TRUE(w1->IsVisible());
920 EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
922 // Create another window, activate it and maximized it.
923 scoped_ptr<Window> w2(CreateTestWindow());
924 w2->Show();
925 wm::ActivateWindow(w2.get());
926 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
927 EXPECT_TRUE(w2->IsVisible());
928 EXPECT_EQ(1.0f, w2->layer()->GetCombinedOpacity());
929 EXPECT_FALSE(w1->IsVisible());
931 // Switch to w1. |w1| should be visible and |w2| hidden.
932 wm::ActivateWindow(w1.get());
933 EXPECT_TRUE(w1->IsVisible());
934 EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
935 EXPECT_FALSE(w2->IsVisible());
937 // Switch back to |w2|.
938 wm::ActivateWindow(w2.get());
939 EXPECT_TRUE(w2->IsVisible());
940 EXPECT_EQ(1.0f, w2->layer()->GetCombinedOpacity());
941 EXPECT_FALSE(w1->IsVisible());
943 // Restore |w2|, both windows should be visible.
944 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
945 EXPECT_TRUE(w1->IsVisible());
946 EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
947 EXPECT_TRUE(w2->IsVisible());
948 EXPECT_EQ(1.0f, w2->layer()->GetCombinedOpacity());
950 // Maximize |w2| again, then close it.
951 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
952 w2->Hide();
953 EXPECT_FALSE(w2->IsVisible());
954 EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
955 EXPECT_TRUE(w1->IsVisible());
957 // Create |w2| and make it fullscreen.
958 w2.reset(CreateTestWindow());
959 w2->Show();
960 wm::ActivateWindow(w2.get());
961 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
962 EXPECT_TRUE(w2->IsVisible());
963 EXPECT_EQ(1.0f, w2->layer()->GetCombinedOpacity());
964 EXPECT_FALSE(w1->IsVisible());
966 // Close |w2|.
967 w2.reset();
968 EXPECT_EQ(1.0f, w1->layer()->GetCombinedOpacity());
969 EXPECT_TRUE(w1->IsVisible());
972 // Verifies windows that are offscreen don't move when switching workspaces.
973 TEST_F(WorkspaceManagerTest, DontMoveOnSwitch) {
974 aura::test::EventGenerator generator(
975 Shell::GetPrimaryRootWindow(), gfx::Point());
976 generator.MoveMouseTo(0, 0);
978 scoped_ptr<Window> w1(CreateTestWindow());
979 ShelfLayoutManager* shelf = shelf_layout_manager();
980 const gfx::Rect touches_shelf_bounds(
981 0, shelf->GetIdealBounds().y() - 10, 101, 102);
982 // Move |w1| to overlap the shelf.
983 w1->SetBounds(touches_shelf_bounds);
984 w1->Show();
985 wm::ActivateWindow(w1.get());
987 // Create another window and maximize it.
988 scoped_ptr<Window> w2(CreateTestWindow());
989 w2->SetBounds(gfx::Rect(10, 11, 250, 251));
990 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
991 w2->Show();
992 wm::ActivateWindow(w2.get());
994 // Switch to w1.
995 wm::ActivateWindow(w1.get());
996 EXPECT_EQ(touches_shelf_bounds.ToString(), w1->bounds().ToString());
999 // Verifies that windows that are completely offscreen move when switching
1000 // workspaces.
1001 TEST_F(WorkspaceManagerTest, MoveOnSwitch) {
1002 aura::test::EventGenerator generator(
1003 Shell::GetPrimaryRootWindow(), gfx::Point());
1004 generator.MoveMouseTo(0, 0);
1006 scoped_ptr<Window> w1(CreateTestWindow());
1007 ShelfLayoutManager* shelf = shelf_layout_manager();
1008 const gfx::Rect w1_bounds(0, shelf->GetIdealBounds().y(), 100, 200);
1009 // Move |w1| so that the top edge is the same as the top edge of the shelf.
1010 w1->SetBounds(w1_bounds);
1011 w1->Show();
1012 wm::ActivateWindow(w1.get());
1013 EXPECT_EQ(w1_bounds.ToString(), w1->bounds().ToString());
1015 // Create another window and maximize it.
1016 scoped_ptr<Window> w2(CreateTestWindow());
1017 w2->SetBounds(gfx::Rect(10, 11, 250, 251));
1018 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
1019 w2->Show();
1020 wm::ActivateWindow(w2.get());
1022 // Increase the size of the shelf. This would make |w1| fall completely out of
1023 // the display work area.
1024 gfx::Size size(shelf_widget()->status_area_widget()->
1025 GetWindowBoundsInScreen().size());
1026 size.Enlarge(0, 30);
1027 shelf_widget()->status_area_widget()->SetSize(size);
1029 // Switch to w1. The window should have moved.
1030 wm::ActivateWindow(w1.get());
1031 EXPECT_NE(w1_bounds.ToString(), w1->bounds().ToString());
1034 // Verifies Focus() works in a window that isn't in the active workspace.
1035 TEST_F(WorkspaceManagerTest, FocusOnFullscreenInSeparateWorkspace) {
1036 scoped_ptr<Window> w1(CreateTestWindow());
1037 w1->SetBounds(gfx::Rect(10, 11, 250, 251));
1038 w1->Show();
1039 wm::ActivateWindow(w1.get());
1041 scoped_ptr<Window> w2(CreateTestWindow());
1042 w2->SetBounds(gfx::Rect(10, 11, 250, 251));
1043 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
1044 w2->Show();
1045 EXPECT_FALSE(w2->IsVisible());
1046 EXPECT_FALSE(wm::IsActiveWindow(w2.get()));
1048 w2->Focus();
1049 EXPECT_TRUE(w2->IsVisible());
1050 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
1051 EXPECT_FALSE(w1->IsVisible());
1054 namespace {
1056 // WindowDelegate used by DontCrashOnChangeAndActivate.
1057 class DontCrashOnChangeAndActivateDelegate
1058 : public aura::test::TestWindowDelegate {
1059 public:
1060 DontCrashOnChangeAndActivateDelegate() : window_(NULL) {}
1062 void set_window(aura::Window* window) { window_ = window; }
1064 // WindowDelegate overrides:
1065 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
1066 const gfx::Rect& new_bounds) OVERRIDE {
1067 if (window_) {
1068 wm::ActivateWindow(window_);
1069 window_ = NULL;
1073 private:
1074 aura::Window* window_;
1076 DISALLOW_COPY_AND_ASSIGN(DontCrashOnChangeAndActivateDelegate);
1079 } // namespace
1081 // Exercises possible crash in W2. Here's the sequence:
1082 // . minimize a maximized window.
1083 // . remove the window (which happens when switching displays).
1084 // . add the window back.
1085 // . show the window and during the bounds change activate it.
1086 TEST_F(WorkspaceManagerTest, DontCrashOnChangeAndActivate) {
1087 // Force the shelf
1088 ShelfLayoutManager* shelf = shelf_layout_manager();
1089 shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
1091 DontCrashOnChangeAndActivateDelegate delegate;
1092 scoped_ptr<Window> w1(CreateTestWindowInShellWithDelegate(
1093 &delegate, 1000, gfx::Rect(10, 11, 250, 251)));
1095 w1->Show();
1096 wm::ActivateWindow(w1.get());
1097 wm::MaximizeWindow(w1.get());
1098 wm::MinimizeWindow(w1.get());
1100 w1->parent()->RemoveChild(w1.get());
1102 // Do this so that when we Show() the window a resize occurs and we make the
1103 // window active.
1104 shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
1106 SetDefaultParentByPrimaryRootWindow(w1.get());
1107 delegate.set_window(w1.get());
1108 w1->Show();
1111 // Verifies a window with a transient parent not managed by workspace works.
1112 TEST_F(WorkspaceManagerTest, TransientParent) {
1113 // Normal window with no transient parent.
1114 scoped_ptr<Window> w2(CreateTestWindow());
1115 w2->SetBounds(gfx::Rect(10, 11, 250, 251));
1116 w2->Show();
1117 wm::ActivateWindow(w2.get());
1119 // Window with a transient parent. We set the transient parent to the root,
1120 // which would never happen but is enough to exercise the bug.
1121 scoped_ptr<Window> w1(CreateTestWindowUnparented());
1122 Shell::GetInstance()->GetPrimaryRootWindow()->AddTransientChild(w1.get());
1123 w1->SetBounds(gfx::Rect(10, 11, 250, 251));
1124 SetDefaultParentByPrimaryRootWindow(w1.get());
1125 w1->Show();
1126 wm::ActivateWindow(w1.get());
1128 // The window with the transient parent should get added to the same parent as
1129 // the normal window.
1130 EXPECT_EQ(w2->parent(), w1->parent());
1133 // Verifies changing TrackedByWorkspace works.
1134 TEST_F(WorkspaceManagerTest, TrackedByWorkspace) {
1135 // Create a window maximized.
1136 scoped_ptr<Window> w1(CreateTestWindow());
1137 w1->Show();
1138 wm::ActivateWindow(w1.get());
1139 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
1140 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
1141 EXPECT_TRUE(w1->IsVisible());
1143 // Create a second window maximized and mark it not tracked by workspace
1144 // manager.
1145 scoped_ptr<Window> w2(CreateTestWindowUnparented());
1146 w2->SetBounds(gfx::Rect(1, 6, 25, 30));
1147 w2->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
1148 SetDefaultParentByPrimaryRootWindow(w2.get());
1149 w2->Show();
1150 SetTrackedByWorkspace(w2.get(), false);
1151 wm::ActivateWindow(w2.get());
1153 // Activating |w2| should force it to have the same parent as |w1|.
1154 EXPECT_EQ(w1->parent(), w2->parent());
1155 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
1156 EXPECT_TRUE(w1->IsVisible());
1157 EXPECT_TRUE(w2->IsVisible());
1159 // Because |w2| isn't tracked we should be able to set the bounds of it.
1160 gfx::Rect bounds(w2->bounds());
1161 bounds.Offset(4, 5);
1162 w2->SetBounds(bounds);
1163 EXPECT_EQ(bounds.ToString(), w2->bounds().ToString());
1165 // Transition it to tracked by worskpace. It should end up in its own
1166 // workspace.
1167 SetTrackedByWorkspace(w2.get(), true);
1168 EXPECT_TRUE(wm::IsActiveWindow(w2.get()));
1169 EXPECT_FALSE(w1->IsVisible());
1170 EXPECT_TRUE(w2->IsVisible());
1171 EXPECT_NE(w1->parent(), w2->parent());
1174 // Verifies a window marked as persisting across all workspaces ends up in its
1175 // own workspace when maximized.
1176 TEST_F(WorkspaceManagerTest, DeactivateDropsToDesktop) {
1177 // Create a window maximized.
1178 scoped_ptr<Window> w1(CreateTestWindow());
1179 w1->Show();
1180 wm::ActivateWindow(w1.get());
1181 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
1182 EXPECT_TRUE(wm::IsActiveWindow(w1.get()));
1183 EXPECT_TRUE(w1->IsVisible());
1185 // Create another window that persists across all workspaces. It should end
1186 // up with the same parent as |w1|.
1187 scoped_ptr<Window> w2(CreateTestWindow());
1188 SetPersistsAcrossAllWorkspaces(
1189 w2.get(),
1190 WINDOW_PERSISTS_ACROSS_ALL_WORKSPACES_VALUE_YES);
1191 w2->Show();
1192 wm::ActivateWindow(w2.get());
1193 EXPECT_EQ(w1->parent(), w2->parent());
1194 ASSERT_EQ("0 M2 active=1", StateString());
1196 // Activate |w1|, should result in dropping |w2| to the desktop.
1197 wm::ActivateWindow(w1.get());
1198 ASSERT_EQ("1 M1 active=1", StateString());
1201 // Test the basic auto placement of one and or two windows in a "simulated
1202 // session" of sequential window operations.
1203 TEST_F(WorkspaceManagerTest, BasicAutoPlacing) {
1204 // Test 1: In case there is no manageable window, no window should shift.
1206 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
1207 window1->SetBounds(gfx::Rect(16, 32, 640, 320));
1208 gfx::Rect desktop_area = window1->parent()->bounds();
1210 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
1211 // Trigger the auto window placement function by making it visible.
1212 // Note that the bounds are getting changed while it is invisible.
1213 window2->Hide();
1214 window2->SetBounds(gfx::Rect(32, 48, 256, 512));
1215 window2->Show();
1217 // Check the initial position of the windows is unchanged.
1218 EXPECT_EQ("16,32 640x320", window1->bounds().ToString());
1219 EXPECT_EQ("32,48 256x512", window2->bounds().ToString());
1221 // Remove the second window and make sure that the first window
1222 // does NOT get centered.
1223 window2.reset();
1224 EXPECT_EQ("16,32 640x320", window1->bounds().ToString());
1226 // Test 2: Set up two managed windows and check their auto positioning.
1227 ash::wm::SetWindowPositionManaged(window1.get(), true);
1228 scoped_ptr<aura::Window> window3(CreateTestWindowInShellWithId(2));
1229 ash::wm::SetWindowPositionManaged(window3.get(), true);
1230 // To avoid any auto window manager changes due to SetBounds, the window
1231 // gets first hidden and then shown again.
1232 window3->Hide();
1233 window3->SetBounds(gfx::Rect(32, 48, 256, 512));
1234 window3->Show();
1235 // |window1| should be flush right and |window3| flush left.
1236 EXPECT_EQ("0,32 640x320", window1->bounds().ToString());
1237 EXPECT_EQ(base::IntToString(
1238 desktop_area.width() - window3->bounds().width()) +
1239 ",48 256x512", window3->bounds().ToString());
1241 // After removing |window3|, |window1| should be centered again.
1242 window3.reset();
1243 EXPECT_EQ(
1244 base::IntToString(
1245 (desktop_area.width() - window1->bounds().width()) / 2) +
1246 ",32 640x320", window1->bounds().ToString());
1248 // Test 3: Set up a manageable and a non manageable window and check
1249 // positioning.
1250 scoped_ptr<aura::Window> window4(CreateTestWindowInShellWithId(3));
1251 // To avoid any auto window manager changes due to SetBounds, the window
1252 // gets first hidden and then shown again.
1253 window1->Hide();
1254 window1->SetBounds(gfx::Rect(16, 32, 640, 320));
1255 window4->SetBounds(gfx::Rect(32, 48, 256, 512));
1256 window1->Show();
1257 // |window1| should be centered and |window4| untouched.
1258 EXPECT_EQ(
1259 base::IntToString(
1260 (desktop_area.width() - window1->bounds().width()) / 2) +
1261 ",32 640x320", window1->bounds().ToString());
1262 EXPECT_EQ("32,48 256x512", window4->bounds().ToString());
1264 // Test4: A single manageable window should get centered.
1265 window4.reset();
1266 ash::wm::SetUserHasChangedWindowPositionOrSize(window1.get(), false);
1267 // Trigger the auto window placement function by showing (and hiding) it.
1268 window1->Hide();
1269 window1->Show();
1270 // |window1| should be centered.
1271 EXPECT_EQ(
1272 base::IntToString(
1273 (desktop_area.width() - window1->bounds().width()) / 2) +
1274 ",32 640x320", window1->bounds().ToString());
1277 // Test the proper usage of user window movement interaction.
1278 TEST_F(WorkspaceManagerTest, TestUserMovedWindowRepositioning) {
1279 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
1280 window1->SetBounds(gfx::Rect(16, 32, 640, 320));
1281 gfx::Rect desktop_area = window1->parent()->bounds();
1282 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
1283 window2->SetBounds(gfx::Rect(32, 48, 256, 512));
1284 window1->Hide();
1285 window2->Hide();
1286 ash::wm::SetWindowPositionManaged(window1.get(), true);
1287 ash::wm::SetWindowPositionManaged(window2.get(), true);
1288 EXPECT_FALSE(ash::wm::HasUserChangedWindowPositionOrSize(window1.get()));
1289 EXPECT_FALSE(ash::wm::HasUserChangedWindowPositionOrSize(window2.get()));
1291 // Check that the current location gets preserved if the user has
1292 // positioned it previously.
1293 ash::wm::SetUserHasChangedWindowPositionOrSize(window1.get(), true);
1294 window1->Show();
1295 EXPECT_EQ("16,32 640x320", window1->bounds().ToString());
1296 // Flag should be still set.
1297 EXPECT_TRUE(ash::wm::HasUserChangedWindowPositionOrSize(window1.get()));
1298 EXPECT_FALSE(ash::wm::HasUserChangedWindowPositionOrSize(window2.get()));
1300 // Turn on the second window and make sure that both windows are now
1301 // positionable again (user movement cleared).
1302 window2->Show();
1304 // |window1| should be flush left and |window3| flush right.
1305 EXPECT_EQ("0,32 640x320", window1->bounds().ToString());
1306 EXPECT_EQ(
1307 base::IntToString(desktop_area.width() - window2->bounds().width()) +
1308 ",48 256x512", window2->bounds().ToString());
1309 // FLag should now be reset.
1310 EXPECT_FALSE(ash::wm::HasUserChangedWindowPositionOrSize(window1.get()));
1311 EXPECT_FALSE(ash::wm::HasUserChangedWindowPositionOrSize(window1.get()));
1313 // Going back to one shown window should keep the state.
1314 ash::wm::SetUserHasChangedWindowPositionOrSize(window1.get(), true);
1315 window2->Hide();
1316 EXPECT_EQ("0,32 640x320", window1->bounds().ToString());
1317 EXPECT_TRUE(ash::wm::HasUserChangedWindowPositionOrSize(window1.get()));
1320 // Test that user placed windows go back to their user placement after the user
1321 // closes all other windows.
1322 TEST_F(WorkspaceManagerTest, TestUserHandledWindowRestore) {
1323 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
1324 gfx::Rect user_pos = gfx::Rect(16, 42, 640, 320);
1325 window1->SetBounds(user_pos);
1326 ash::wm::SetPreAutoManageWindowBounds(window1.get(), user_pos);
1327 gfx::Rect desktop_area = window1->parent()->bounds();
1329 // Create a second window to let the auto manager kick in.
1330 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
1331 window2->SetBounds(gfx::Rect(32, 48, 256, 512));
1332 window1->Hide();
1333 window2->Hide();
1334 ash::wm::SetWindowPositionManaged(window1.get(), true);
1335 ash::wm::SetWindowPositionManaged(window2.get(), true);
1336 window1->Show();
1337 EXPECT_EQ(user_pos.ToString(), window1->bounds().ToString());
1338 window2->Show();
1340 // |window1| should be flush left and |window2| flush right.
1341 EXPECT_EQ("0," + base::IntToString(user_pos.y()) +
1342 " 640x320", window1->bounds().ToString());
1343 EXPECT_EQ(
1344 base::IntToString(desktop_area.width() - window2->bounds().width()) +
1345 ",48 256x512", window2->bounds().ToString());
1346 window2->Hide();
1348 // After the other window get hidden the window has to move back to the
1349 // previous position and the bounds should still be set and unchanged.
1350 EXPECT_EQ(user_pos.ToString(), window1->bounds().ToString());
1351 ASSERT_TRUE(ash::wm::GetPreAutoManageWindowBounds(window1.get()));
1352 EXPECT_EQ(user_pos.ToString(),
1353 ash::wm::GetPreAutoManageWindowBounds(window1.get())->ToString());
1356 // Test that a window from normal to minimize will repos the remaining.
1357 TEST_F(WorkspaceManagerTest, ToMinimizeRepositionsRemaining) {
1358 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
1359 ash::wm::SetWindowPositionManaged(window1.get(), true);
1360 window1->SetBounds(gfx::Rect(16, 32, 640, 320));
1361 gfx::Rect desktop_area = window1->parent()->bounds();
1363 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
1364 ash::wm::SetWindowPositionManaged(window2.get(), true);
1365 window2->SetBounds(gfx::Rect(32, 48, 256, 512));
1367 ash::wm::MinimizeWindow(window1.get());
1369 // |window2| should be centered now.
1370 EXPECT_TRUE(window2->IsVisible());
1371 EXPECT_TRUE(ash::wm::IsWindowNormal(window2.get()));
1372 EXPECT_EQ(base::IntToString(
1373 (desktop_area.width() - window2->bounds().width()) / 2) +
1374 ",48 256x512", window2->bounds().ToString());
1376 ash::wm::RestoreWindow(window1.get());
1377 // |window1| should be flush right and |window3| flush left.
1378 EXPECT_EQ(base::IntToString(
1379 desktop_area.width() - window1->bounds().width()) +
1380 ",32 640x320", window1->bounds().ToString());
1381 EXPECT_EQ("0,48 256x512", window2->bounds().ToString());
1384 // Test that minimizing an initially maximized window will repos the remaining.
1385 TEST_F(WorkspaceManagerTest, MaxToMinRepositionsRemaining) {
1386 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
1387 ash::wm::SetWindowPositionManaged(window1.get(), true);
1388 gfx::Rect desktop_area = window1->parent()->bounds();
1390 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
1391 ash::wm::SetWindowPositionManaged(window2.get(), true);
1392 window2->SetBounds(gfx::Rect(32, 48, 256, 512));
1394 ash::wm::MaximizeWindow(window1.get());
1395 ash::wm::MinimizeWindow(window1.get());
1397 // |window2| should be centered now.
1398 EXPECT_TRUE(window2->IsVisible());
1399 EXPECT_TRUE(ash::wm::IsWindowNormal(window2.get()));
1400 EXPECT_EQ(base::IntToString(
1401 (desktop_area.width() - window2->bounds().width()) / 2) +
1402 ",48 256x512", window2->bounds().ToString());
1405 // Test that nomral, maximize, minimizing will repos the remaining.
1406 TEST_F(WorkspaceManagerTest, NormToMaxToMinRepositionsRemaining) {
1407 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
1408 window1->SetBounds(gfx::Rect(16, 32, 640, 320));
1409 ash::wm::SetWindowPositionManaged(window1.get(), true);
1410 gfx::Rect desktop_area = window1->parent()->bounds();
1412 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
1413 ash::wm::SetWindowPositionManaged(window2.get(), true);
1414 window2->SetBounds(gfx::Rect(32, 40, 256, 512));
1416 // Trigger the auto window placement function by showing (and hiding) it.
1417 window1->Hide();
1418 window1->Show();
1420 // |window1| should be flush right and |window3| flush left.
1421 EXPECT_EQ(base::IntToString(
1422 desktop_area.width() - window1->bounds().width()) +
1423 ",32 640x320", window1->bounds().ToString());
1424 EXPECT_EQ("0,40 256x512", window2->bounds().ToString());
1426 ash::wm::MaximizeWindow(window1.get());
1427 ash::wm::MinimizeWindow(window1.get());
1429 // |window2| should be centered now.
1430 EXPECT_TRUE(window2->IsVisible());
1431 EXPECT_TRUE(ash::wm::IsWindowNormal(window2.get()));
1432 EXPECT_EQ(base::IntToString(
1433 (desktop_area.width() - window2->bounds().width()) / 2) +
1434 ",40 256x512", window2->bounds().ToString());
1437 // Test that nomral, maximize, normal will repos the remaining.
1438 TEST_F(WorkspaceManagerTest, NormToMaxToNormRepositionsRemaining) {
1439 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
1440 window1->SetBounds(gfx::Rect(16, 32, 640, 320));
1441 ash::wm::SetWindowPositionManaged(window1.get(), true);
1442 gfx::Rect desktop_area = window1->parent()->bounds();
1444 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
1445 ash::wm::SetWindowPositionManaged(window2.get(), true);
1446 window2->SetBounds(gfx::Rect(32, 40, 256, 512));
1448 // Trigger the auto window placement function by showing (and hiding) it.
1449 window1->Hide();
1450 window1->Show();
1452 // |window1| should be flush right and |window3| flush left.
1453 EXPECT_EQ(base::IntToString(
1454 desktop_area.width() - window1->bounds().width()) +
1455 ",32 640x320", window1->bounds().ToString());
1456 EXPECT_EQ("0,40 256x512", window2->bounds().ToString());
1458 ash::wm::MaximizeWindow(window1.get());
1459 ash::wm::RestoreWindow(window1.get());
1461 // |window1| should be flush right and |window2| flush left.
1462 EXPECT_EQ(base::IntToString(
1463 desktop_area.width() - window1->bounds().width()) +
1464 ",32 640x320", window1->bounds().ToString());
1465 EXPECT_EQ("0,40 256x512", window2->bounds().ToString());
1468 // Test that animations are triggered.
1469 TEST_F(WorkspaceManagerTest, AnimatedNormToMaxToNormRepositionsRemaining) {
1470 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
1471 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
1472 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(0));
1473 window1->Hide();
1474 window1->SetBounds(gfx::Rect(16, 32, 640, 320));
1475 gfx::Rect desktop_area = window1->parent()->bounds();
1476 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(1));
1477 window2->Hide();
1478 window2->SetBounds(gfx::Rect(32, 48, 256, 512));
1480 ash::wm::SetWindowPositionManaged(window1.get(), true);
1481 ash::wm::SetWindowPositionManaged(window2.get(), true);
1482 // Make sure nothing is animating.
1483 window1->layer()->GetAnimator()->StopAnimating();
1484 window2->layer()->GetAnimator()->StopAnimating();
1485 window2->Show();
1487 // The second window should now animate.
1488 EXPECT_FALSE(window1->layer()->GetAnimator()->is_animating());
1489 EXPECT_TRUE(window2->layer()->GetAnimator()->is_animating());
1490 window2->layer()->GetAnimator()->StopAnimating();
1492 window1->Show();
1493 EXPECT_TRUE(window1->layer()->GetAnimator()->is_animating());
1494 EXPECT_TRUE(window2->layer()->GetAnimator()->is_animating());
1496 window1->layer()->GetAnimator()->StopAnimating();
1497 window2->layer()->GetAnimator()->StopAnimating();
1498 // |window1| should be flush right and |window2| flush left.
1499 EXPECT_EQ(base::IntToString(
1500 desktop_area.width() - window1->bounds().width()) +
1501 ",32 640x320", window1->bounds().ToString());
1502 EXPECT_EQ("0,48 256x512", window2->bounds().ToString());
1505 // This tests simulates a browser and an app and verifies the ordering of the
1506 // windows and layers doesn't get out of sync as various operations occur. Its
1507 // really testing code in FocusController, but easier to simulate here. Just as
1508 // with a real browser the browser here has a transient child window
1509 // (corresponds to the status bubble).
1510 TEST_F(WorkspaceManagerTest, VerifyLayerOrdering) {
1511 scoped_ptr<Window> browser(
1512 aura::test::CreateTestWindowWithDelegate(
1513 NULL,
1514 aura::client::WINDOW_TYPE_NORMAL,
1515 gfx::Rect(5, 6, 7, 8),
1516 NULL));
1517 browser->SetName("browser");
1518 SetDefaultParentByPrimaryRootWindow(browser.get());
1519 browser->Show();
1520 wm::ActivateWindow(browser.get());
1522 // |status_bubble| is made a transient child of |browser| and as a result
1523 // owned by |browser|.
1524 aura::test::TestWindowDelegate* status_bubble_delegate =
1525 aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate();
1526 status_bubble_delegate->set_can_focus(false);
1527 Window* status_bubble =
1528 aura::test::CreateTestWindowWithDelegate(
1529 status_bubble_delegate,
1530 aura::client::WINDOW_TYPE_POPUP,
1531 gfx::Rect(5, 6, 7, 8),
1532 NULL);
1533 browser->AddTransientChild(status_bubble);
1534 SetDefaultParentByPrimaryRootWindow(status_bubble);
1535 status_bubble->SetName("status_bubble");
1537 scoped_ptr<Window> app(
1538 aura::test::CreateTestWindowWithDelegate(
1539 NULL,
1540 aura::client::WINDOW_TYPE_NORMAL,
1541 gfx::Rect(5, 6, 7, 8),
1542 NULL));
1543 app->SetName("app");
1544 SetDefaultParentByPrimaryRootWindow(app.get());
1546 aura::Window* parent = browser->parent();
1548 app->Show();
1549 wm::ActivateWindow(app.get());
1550 EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
1552 // Minimize the app, focus should go the browser.
1553 app->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
1554 EXPECT_TRUE(wm::IsActiveWindow(browser.get()));
1555 EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
1557 // Minimize the browser (neither windows are focused).
1558 browser->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
1559 EXPECT_FALSE(wm::IsActiveWindow(browser.get()));
1560 EXPECT_FALSE(wm::IsActiveWindow(app.get()));
1561 EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
1563 // Show the browser (which should restore it).
1564 browser->Show();
1565 EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
1567 // Activate the browser.
1568 ash::wm::ActivateWindow(browser.get());
1569 EXPECT_TRUE(wm::IsActiveWindow(browser.get()));
1570 EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
1572 // Restore the app. This differs from above code for |browser| as internally
1573 // the app code does this. Restoring this way or using Show() should not make
1574 // a difference.
1575 app->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
1576 EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
1578 // Activate the app.
1579 ash::wm::ActivateWindow(app.get());
1580 EXPECT_TRUE(wm::IsActiveWindow(app.get()));
1581 EXPECT_EQ(GetWindowNames(parent), GetLayerNames(parent));
1584 namespace {
1586 // Used by DragMaximizedNonTrackedWindow to track how many times the window
1587 // hierarchy changes.
1588 class DragMaximizedNonTrackedWindowObserver
1589 : public aura::WindowObserver {
1590 public:
1591 DragMaximizedNonTrackedWindowObserver() : change_count_(0) {
1594 // Number of times OnWindowHierarchyChanged() has been received.
1595 void clear_change_count() { change_count_ = 0; }
1596 int change_count() const {
1597 return change_count_;
1600 // aura::WindowObserver overrides:
1601 virtual void OnWindowHierarchyChanged(
1602 const HierarchyChangeParams& params) OVERRIDE {
1603 change_count_++;
1606 private:
1607 int change_count_;
1609 DISALLOW_COPY_AND_ASSIGN(DragMaximizedNonTrackedWindowObserver);
1612 } // namespace
1614 // Verifies setting tracked by workspace to false and then dragging a maximized
1615 // window doesn't result in changing the window hierarchy (which typically
1616 // indicates new workspaces have been created).
1617 TEST_F(WorkspaceManagerTest, DragMaximizedNonTrackedWindow) {
1618 aura::test::EventGenerator generator(
1619 Shell::GetPrimaryRootWindow(), gfx::Point());
1620 generator.MoveMouseTo(5, 5);
1622 aura::test::TestWindowDelegate delegate;
1623 delegate.set_window_component(HTCAPTION);
1624 scoped_ptr<Window> w1(
1625 aura::test::CreateTestWindowWithDelegate(&delegate,
1626 aura::client::WINDOW_TYPE_NORMAL,
1627 gfx::Rect(5, 6, 7, 8),
1628 NULL));
1629 SetDefaultParentByPrimaryRootWindow(w1.get());
1630 w1->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
1631 w1->Show();
1632 wm::ActivateWindow(w1.get());
1633 DragMaximizedNonTrackedWindowObserver observer;
1634 w1->parent()->parent()->AddObserver(&observer);
1635 const gfx::Rect max_bounds(w1->bounds());
1637 // There should be two workspace, one for the desktop and one for the
1638 // maximized window with the maximized active.
1639 EXPECT_EQ("0 M1 active=1", StateString());
1641 generator.PressLeftButton();
1642 generator.MoveMouseTo(100, 100);
1643 // The bounds shouldn't change (drag should result in nothing happening
1644 // now.
1645 EXPECT_EQ(max_bounds.ToString(), w1->bounds().ToString());
1646 EXPECT_EQ("0 M1 active=1", StateString());
1648 generator.ReleaseLeftButton();
1649 EXPECT_EQ(0, observer.change_count());
1651 // Set tracked to false and repeat, now the window should move.
1652 SetTrackedByWorkspace(w1.get(), false);
1653 generator.MoveMouseTo(5, 5);
1654 generator.PressLeftButton();
1655 generator.MoveMouseBy(100, 100);
1656 EXPECT_EQ(gfx::Rect(max_bounds.x() + 100, max_bounds.y() + 100,
1657 max_bounds.width(), max_bounds.height()).ToString(),
1658 w1->bounds().ToString());
1659 EXPECT_EQ("0 M1 active=1", StateString());
1661 generator.ReleaseLeftButton();
1662 SetTrackedByWorkspace(w1.get(), true);
1663 // Marking the window tracked again should snap back to origin.
1664 EXPECT_EQ("0 M1 active=1", StateString());
1665 EXPECT_EQ(max_bounds.ToString(), w1->bounds().ToString());
1666 EXPECT_EQ(0, observer.change_count());
1668 w1->parent()->parent()->RemoveObserver(&observer);
1671 } // namespace internal
1672 } // namespace ash