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.
5 #include "ash/shelf/shelf_widget.h"
7 #include "ash/launcher/launcher.h"
8 #include "ash/launcher/launcher_button.h"
9 #include "ash/launcher/launcher_model.h"
10 #include "ash/root_window_controller.h"
11 #include "ash/shelf/shelf_layout_manager.h"
12 #include "ash/shelf/shelf_view.h"
13 #include "ash/shell.h"
14 #include "ash/test/ash_test_base.h"
15 #include "ash/test/launcher_test_api.h"
16 #include "ash/test/shelf_view_test_api.h"
17 #include "ash/wm/window_util.h"
18 #include "ui/aura/root_window.h"
19 #include "ui/gfx/display.h"
20 #include "ui/gfx/screen.h"
21 #include "ui/views/corewm/corewm_switches.h"
22 #include "ui/views/view.h"
23 #include "ui/views/widget/widget.h"
29 ShelfWidget* GetShelfWidget() {
30 return Launcher::ForPrimaryDisplay()->shelf_widget();
33 internal::ShelfLayoutManager* GetShelfLayoutManager() {
34 return GetShelfWidget()->shelf_layout_manager();
39 typedef test::AshTestBase ShelfWidgetTest;
41 // Launcher can't be activated on mouse click, but it is activable from
42 // the focus cycler or as fallback.
43 // TODO(mtomasz): make this test work with the FocusController.
45 TEST_F(ShelfWidgetTest, DISABLED_ActivateAsFallback) {
46 Launcher* launcher = Launcher::ForPrimaryDisplay();
47 ShelfWidget* shelf_widget = launcher->shelf_widget();
48 EXPECT_FALSE(shelf_widget->CanActivate());
50 shelf_widget->WillActivateAsFallback();
51 EXPECT_TRUE(shelf_widget->CanActivate());
53 wm::ActivateWindow(shelf_widget->GetNativeWindow());
54 EXPECT_FALSE(shelf_widget->CanActivate());
57 void TestLauncherAlignment(aura::Window* root,
58 ShelfAlignment alignment,
59 const std::string& expected) {
60 Shell::GetInstance()->SetShelfAlignment(alignment, root);
61 gfx::Screen* screen = gfx::Screen::GetScreenFor(root);
63 screen->GetDisplayNearestWindow(root).work_area().ToString());
66 TEST_F(ShelfWidgetTest, TestAlignment) {
67 Launcher* launcher = Launcher::ForPrimaryDisplay();
68 UpdateDisplay("400x400");
69 ASSERT_TRUE(launcher);
71 SCOPED_TRACE("Single Bottom");
72 TestLauncherAlignment(Shell::GetPrimaryRootWindow(),
73 SHELF_ALIGNMENT_BOTTOM,
77 SCOPED_TRACE("Single Right");
78 TestLauncherAlignment(Shell::GetPrimaryRootWindow(),
79 SHELF_ALIGNMENT_RIGHT,
83 SCOPED_TRACE("Single Left");
84 TestLauncherAlignment(Shell::GetPrimaryRootWindow(),
88 if (!SupportsMultipleDisplays())
91 UpdateDisplay("300x300,500x500");
92 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
94 SCOPED_TRACE("Primary Bottom");
95 TestLauncherAlignment(root_windows[0],
96 SHELF_ALIGNMENT_BOTTOM,
100 SCOPED_TRACE("Primary Right");
101 TestLauncherAlignment(root_windows[0],
102 SHELF_ALIGNMENT_RIGHT,
106 SCOPED_TRACE("Primary Left");
107 TestLauncherAlignment(root_windows[0],
108 SHELF_ALIGNMENT_LEFT,
112 SCOPED_TRACE("Secondary Bottom");
113 TestLauncherAlignment(root_windows[1],
114 SHELF_ALIGNMENT_BOTTOM,
118 SCOPED_TRACE("Secondary Right");
119 TestLauncherAlignment(root_windows[1],
120 SHELF_ALIGNMENT_RIGHT,
124 SCOPED_TRACE("Secondary Left");
125 TestLauncherAlignment(root_windows[1],
126 SHELF_ALIGNMENT_LEFT,
131 // Makes sure the launcher is initially sized correctly.
132 TEST_F(ShelfWidgetTest, LauncherInitiallySized) {
133 ShelfWidget* shelf_widget = GetShelfWidget();
134 Launcher* launcher = shelf_widget->launcher();
135 ASSERT_TRUE(launcher);
136 internal::ShelfLayoutManager* shelf_layout_manager = GetShelfLayoutManager();
137 ASSERT_TRUE(shelf_layout_manager);
138 ASSERT_TRUE(shelf_widget->status_area_widget());
139 int status_width = shelf_widget->status_area_widget()->
140 GetWindowBoundsInScreen().width();
141 // Test only makes sense if the status is > 0, which it better be.
142 EXPECT_GT(status_width, 0);
143 EXPECT_EQ(status_width, shelf_widget->GetContentsView()->width() -
144 test::LauncherTestAPI(launcher).shelf_view()->width());
147 // Verifies when the shell is deleted with a full screen window we don't crash.
148 TEST_F(ShelfWidgetTest, DontReferenceLauncherAfterDeletion) {
149 views::Widget* widget = new views::Widget;
150 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
151 params.bounds = gfx::Rect(0, 0, 200, 200);
152 params.context = CurrentContext();
153 // Widget is now owned by the parent window.
154 widget->Init(params);
155 widget->SetFullscreen(true);
158 #if defined(OS_CHROMEOS)
159 // Verifies launcher is created with correct size after user login and when its
160 // container and status widget has finished sizing.
161 // See http://crbug.com/252533
162 TEST_F(ShelfWidgetTest, LauncherInitiallySizedAfterLogin) {
163 SetUserLoggedIn(false);
164 UpdateDisplay("300x200,400x300");
166 ShelfWidget* shelf = NULL;
167 Shell::RootWindowControllerList controllers(
168 Shell::GetAllRootWindowControllers());
169 for (Shell::RootWindowControllerList::const_iterator i = controllers.begin();
170 i != controllers.end();
172 if (!(*i)->shelf()->launcher()) {
173 shelf = (*i)->shelf();
177 ASSERT_TRUE(shelf != NULL);
179 SetUserLoggedIn(true);
180 Shell::GetInstance()->CreateLauncher();
182 Launcher* launcher = shelf->launcher();
183 ASSERT_TRUE(launcher != NULL);
185 const int status_width =
186 shelf->status_area_widget()->GetWindowBoundsInScreen().width();
187 EXPECT_GT(status_width, 0);
188 EXPECT_EQ(status_width,
189 shelf->GetContentsView()->width() -
190 test::LauncherTestAPI(launcher).shelf_view()->width());