• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/shelf/shelf_widget.h"
6 
7 #include "ash/launcher/launcher.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/shelf/shelf_button.h"
10 #include "ash/shelf/shelf_layout_manager.h"
11 #include "ash/shelf/shelf_model.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"
24 
25 namespace ash {
26 
27 namespace {
28 
GetShelfWidget()29 ShelfWidget* GetShelfWidget() {
30   return Launcher::ForPrimaryDisplay()->shelf_widget();
31 }
32 
GetShelfLayoutManager()33 internal::ShelfLayoutManager* GetShelfLayoutManager() {
34   return GetShelfWidget()->shelf_layout_manager();
35 }
36 
37 } // namespace
38 
39 typedef test::AshTestBase ShelfWidgetTest;
40 
41 // Shelf 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.
44 // crbug.com/285364.
TEST_F(ShelfWidgetTest,DISABLED_ActivateAsFallback)45 TEST_F(ShelfWidgetTest, DISABLED_ActivateAsFallback) {
46   Launcher* launcher = Launcher::ForPrimaryDisplay();
47   ShelfWidget* shelf_widget = launcher->shelf_widget();
48   EXPECT_FALSE(shelf_widget->CanActivate());
49 
50   shelf_widget->WillActivateAsFallback();
51   EXPECT_TRUE(shelf_widget->CanActivate());
52 
53   wm::ActivateWindow(shelf_widget->GetNativeWindow());
54   EXPECT_FALSE(shelf_widget->CanActivate());
55 }
56 
TestLauncherAlignment(aura::Window * root,ShelfAlignment alignment,const std::string & expected)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);
62   EXPECT_EQ(expected,
63             screen->GetDisplayNearestWindow(root).work_area().ToString());
64 }
65 
TEST_F(ShelfWidgetTest,TestAlignment)66 TEST_F(ShelfWidgetTest, TestAlignment) {
67   Launcher* launcher = Launcher::ForPrimaryDisplay();
68   UpdateDisplay("400x400");
69   ASSERT_TRUE(launcher);
70   {
71     SCOPED_TRACE("Single Bottom");
72     TestLauncherAlignment(Shell::GetPrimaryRootWindow(),
73                           SHELF_ALIGNMENT_BOTTOM,
74                           "0,0 400x353");
75   }
76   {
77     SCOPED_TRACE("Single Right");
78     TestLauncherAlignment(Shell::GetPrimaryRootWindow(),
79                           SHELF_ALIGNMENT_RIGHT,
80                           "0,0 353x400");
81   }
82   {
83     SCOPED_TRACE("Single Left");
84     TestLauncherAlignment(Shell::GetPrimaryRootWindow(),
85                           SHELF_ALIGNMENT_LEFT,
86                           "47,0 353x400");
87   }
88   if (!SupportsMultipleDisplays())
89     return;
90 
91   UpdateDisplay("300x300,500x500");
92   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
93   {
94     SCOPED_TRACE("Primary Bottom");
95     TestLauncherAlignment(root_windows[0],
96                           SHELF_ALIGNMENT_BOTTOM,
97                           "0,0 300x253");
98   }
99   {
100     SCOPED_TRACE("Primary Right");
101     TestLauncherAlignment(root_windows[0],
102                           SHELF_ALIGNMENT_RIGHT,
103                           "0,0 253x300");
104   }
105   {
106     SCOPED_TRACE("Primary Left");
107     TestLauncherAlignment(root_windows[0],
108                           SHELF_ALIGNMENT_LEFT,
109                           "47,0 253x300");
110   }
111   {
112     SCOPED_TRACE("Secondary Bottom");
113     TestLauncherAlignment(root_windows[1],
114                           SHELF_ALIGNMENT_BOTTOM,
115                           "300,0 500x453");
116   }
117   {
118     SCOPED_TRACE("Secondary Right");
119     TestLauncherAlignment(root_windows[1],
120                           SHELF_ALIGNMENT_RIGHT,
121                           "300,0 453x500");
122   }
123   {
124     SCOPED_TRACE("Secondary Left");
125     TestLauncherAlignment(root_windows[1],
126                           SHELF_ALIGNMENT_LEFT,
127                           "347,0 453x500");
128   }
129 }
130 
131 // Makes sure the launcher is initially sized correctly.
TEST_F(ShelfWidgetTest,LauncherInitiallySized)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());
145 }
146 
147 // Verifies when the shell is deleted with a full screen window we don't crash.
TEST_F(ShelfWidgetTest,DontReferenceLauncherAfterDeletion)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);
156 }
157 
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
TEST_F(ShelfWidgetTest,LauncherInitiallySizedAfterLogin)162 TEST_F(ShelfWidgetTest, LauncherInitiallySizedAfterLogin) {
163   SetUserLoggedIn(false);
164   UpdateDisplay("300x200,400x300");
165 
166   ShelfWidget* shelf = NULL;
167   Shell::RootWindowControllerList controllers(
168       Shell::GetAllRootWindowControllers());
169   for (Shell::RootWindowControllerList::const_iterator i = controllers.begin();
170        i != controllers.end();
171        ++i) {
172     if (!(*i)->shelf()->launcher()) {
173       shelf = (*i)->shelf();
174       break;
175     }
176   }
177   ASSERT_TRUE(shelf != NULL);
178 
179   SetUserLoggedIn(true);
180   Shell::GetInstance()->CreateLauncher();
181 
182   Launcher* launcher = shelf->launcher();
183   ASSERT_TRUE(launcher != NULL);
184 
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());
191 }
192 #endif
193 
194 }  // namespace ash
195