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/launcher/launcher_button.h" 9 #include "ash/launcher/launcher_model.h" 10 #include "ash/launcher/launcher_view.h" 11 #include "ash/root_window_controller.h" 12 #include "ash/shelf/shelf_layout_manager.h" 13 #include "ash/shell.h" 14 #include "ash/test/ash_test_base.h" 15 #include "ash/test/launcher_view_test_api.h" 16 #include "ash/wm/window_util.h" 17 #include "ui/aura/root_window.h" 18 #include "ui/gfx/display.h" 19 #include "ui/gfx/screen.h" 20 #include "ui/views/corewm/corewm_switches.h" 21 #include "ui/views/view.h" 22 #include "ui/views/widget/widget.h" 23 24 namespace ash { 25 26 namespace { 27 28 ShelfWidget* GetShelfWidget() { 29 return Launcher::ForPrimaryDisplay()->shelf_widget(); 30 } 31 32 internal::ShelfLayoutManager* GetShelfLayoutManager() { 33 return GetShelfWidget()->shelf_layout_manager(); 34 } 35 36 } // namespace 37 38 typedef test::AshTestBase ShelfWidgetTest; 39 40 // Launcher can't be activated on mouse click, but it is activable from 41 // the focus cycler or as fallback. 42 TEST_F(ShelfWidgetTest, ActivateAsFallback) { 43 // TODO(mtomasz): make this test work with the FocusController. 44 if (views::corewm::UseFocusController()) 45 return; 46 47 Launcher* launcher = Launcher::ForPrimaryDisplay(); 48 ShelfWidget* shelf_widget = launcher->shelf_widget(); 49 EXPECT_FALSE(shelf_widget->CanActivate()); 50 51 shelf_widget->WillActivateAsFallback(); 52 EXPECT_TRUE(shelf_widget->CanActivate()); 53 54 wm::ActivateWindow(shelf_widget->GetNativeWindow()); 55 EXPECT_FALSE(shelf_widget->CanActivate()); 56 } 57 58 void TestLauncherAlignment(aura::RootWindow* root, 59 ShelfAlignment alignment, 60 const std::string& expected) { 61 Shell::GetInstance()->SetShelfAlignment(alignment, root); 62 gfx::Screen* screen = gfx::Screen::GetScreenFor(root); 63 EXPECT_EQ(expected, 64 screen->GetDisplayNearestWindow(root).work_area().ToString()); 65 } 66 67 TEST_F(ShelfWidgetTest, TestAlignment) { 68 Launcher* launcher = Launcher::ForPrimaryDisplay(); 69 UpdateDisplay("400x400"); 70 ASSERT_TRUE(launcher); 71 { 72 SCOPED_TRACE("Single Bottom"); 73 TestLauncherAlignment(Shell::GetPrimaryRootWindow(), 74 SHELF_ALIGNMENT_BOTTOM, 75 "0,0 400x352"); 76 } 77 { 78 SCOPED_TRACE("Single Right"); 79 TestLauncherAlignment(Shell::GetPrimaryRootWindow(), 80 SHELF_ALIGNMENT_RIGHT, 81 "0,0 352x400"); 82 } 83 { 84 SCOPED_TRACE("Single Left"); 85 TestLauncherAlignment(Shell::GetPrimaryRootWindow(), 86 SHELF_ALIGNMENT_LEFT, 87 "48,0 352x400"); 88 } 89 if (!SupportsMultipleDisplays()) 90 return; 91 92 UpdateDisplay("300x300,500x500"); 93 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); 94 { 95 SCOPED_TRACE("Primary Bottom"); 96 TestLauncherAlignment(root_windows[0], 97 SHELF_ALIGNMENT_BOTTOM, 98 "0,0 300x252"); 99 } 100 { 101 SCOPED_TRACE("Primary Right"); 102 TestLauncherAlignment(root_windows[0], 103 SHELF_ALIGNMENT_RIGHT, 104 "0,0 252x300"); 105 } 106 { 107 SCOPED_TRACE("Primary Left"); 108 TestLauncherAlignment(root_windows[0], 109 SHELF_ALIGNMENT_LEFT, 110 "48,0 252x300"); 111 } 112 { 113 SCOPED_TRACE("Secondary Bottom"); 114 TestLauncherAlignment(root_windows[1], 115 SHELF_ALIGNMENT_BOTTOM, 116 "300,0 500x452"); 117 } 118 { 119 SCOPED_TRACE("Secondary Right"); 120 TestLauncherAlignment(root_windows[1], 121 SHELF_ALIGNMENT_RIGHT, 122 "300,0 452x500"); 123 } 124 { 125 SCOPED_TRACE("Secondary Left"); 126 TestLauncherAlignment(root_windows[1], 127 SHELF_ALIGNMENT_LEFT, 128 "348,0 452x500"); 129 } 130 } 131 132 // Makes sure the launcher is initially sized correctly. 133 TEST_F(ShelfWidgetTest, LauncherInitiallySized) { 134 ShelfWidget* shelf_widget = GetShelfWidget(); 135 Launcher* launcher = shelf_widget->launcher(); 136 ASSERT_TRUE(launcher); 137 internal::ShelfLayoutManager* shelf_layout_manager = GetShelfLayoutManager(); 138 ASSERT_TRUE(shelf_layout_manager); 139 ASSERT_TRUE(shelf_widget->status_area_widget()); 140 int status_width = shelf_widget->status_area_widget()-> 141 GetWindowBoundsInScreen().width(); 142 // Test only makes sense if the status is > 0, which it better be. 143 EXPECT_GT(status_width, 0); 144 EXPECT_EQ(status_width, shelf_widget->GetContentsView()->width() - 145 launcher->GetLauncherViewForTest()->width()); 146 } 147 148 // Verifies when the shell is deleted with a full screen window we don't crash. 149 TEST_F(ShelfWidgetTest, DontReferenceLauncherAfterDeletion) { 150 views::Widget* widget = new views::Widget; 151 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); 152 params.bounds = gfx::Rect(0, 0, 200, 200); 153 params.context = CurrentContext(); 154 // Widget is now owned by the parent window. 155 widget->Init(params); 156 widget->SetFullscreen(true); 157 } 158 159 #if defined(OS_CHROMEOS) 160 // Verifies launcher is created with correct size after user login and when its 161 // container and status widget has finished sizing. 162 // See http://crbug.com/252533 163 TEST_F(ShelfWidgetTest, LauncherInitiallySizedAfterLogin) { 164 SetUserLoggedIn(false); 165 UpdateDisplay("300x200,400x300"); 166 167 ShelfWidget* shelf = NULL; 168 Shell::RootWindowControllerList controllers( 169 Shell::GetAllRootWindowControllers()); 170 for (Shell::RootWindowControllerList::const_iterator i = controllers.begin(); 171 i != controllers.end(); 172 ++i) { 173 if (!(*i)->shelf()->launcher()) { 174 shelf = (*i)->shelf(); 175 break; 176 } 177 } 178 ASSERT_TRUE(shelf != NULL); 179 180 SetUserLoggedIn(true); 181 Shell::GetInstance()->CreateLauncher(); 182 183 Launcher* launcher = shelf->launcher(); 184 ASSERT_TRUE(launcher != NULL); 185 186 const int status_width = 187 shelf->status_area_widget()->GetWindowBoundsInScreen().width(); 188 EXPECT_GT(status_width, 0); 189 EXPECT_EQ(status_width, 190 shelf->GetContentsView()->width() - 191 launcher->GetLauncherViewForTest()->width()); 192 } 193 #endif 194 195 } // namespace ash 196