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. 4 5 #include "ash/test/ash_test_base.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "ash/ash_switches.h" 11 #include "ash/display/display_controller.h" 12 #include "ash/shell.h" 13 #include "ash/shell/toplevel_window.h" 14 #include "ash/test/ash_test_helper.h" 15 #include "ash/test/display_manager_test_api.h" 16 #include "ash/test/test_session_state_delegate.h" 17 #include "ash/test/test_shell_delegate.h" 18 #include "ash/test/test_system_tray_delegate.h" 19 #include "ash/wm/coordinate_conversion.h" 20 #include "ash/wm/window_positioner.h" 21 #include "base/command_line.h" 22 #include "ui/aura/client/aura_constants.h" 23 #include "ui/aura/client/screen_position_client.h" 24 #include "ui/aura/client/window_tree_client.h" 25 #include "ui/aura/test/event_generator.h" 26 #include "ui/aura/test/test_window_delegate.h" 27 #include "ui/aura/window.h" 28 #include "ui/aura/window_delegate.h" 29 #include "ui/aura/window_tree_host.h" 30 #include "ui/base/ime/input_method_initializer.h" 31 #include "ui/events/gestures/gesture_configuration.h" 32 #include "ui/gfx/display.h" 33 #include "ui/gfx/point.h" 34 #include "ui/gfx/screen.h" 35 36 #if defined(OS_CHROMEOS) 37 #include "ash/system/chromeos/tray_display.h" 38 #endif 39 40 #if defined(OS_WIN) 41 #include "ash/test/test_metro_viewer_process_host.h" 42 #include "base/test/test_process_killer_win.h" 43 #include "base/win/metro.h" 44 #include "base/win/windows_version.h" 45 #include "ui/aura/remote_window_tree_host_win.h" 46 #include "ui/aura/window_tree_host_win.h" 47 #include "win8/test/test_registrar_constants.h" 48 #endif 49 50 #if defined(USE_X11) 51 #include "ui/gfx/x/x11_connection.h" 52 #endif 53 54 namespace ash { 55 namespace test { 56 namespace { 57 58 class AshEventGeneratorDelegate : public aura::test::EventGeneratorDelegate { 59 public: 60 AshEventGeneratorDelegate() {} 61 virtual ~AshEventGeneratorDelegate() {} 62 63 // aura::test::EventGeneratorDelegate overrides: 64 virtual aura::WindowTreeHost* GetHostAt( 65 const gfx::Point& point_in_screen) const OVERRIDE { 66 gfx::Screen* screen = Shell::GetScreen(); 67 gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen); 68 return Shell::GetInstance()->display_controller()-> 69 GetRootWindowForDisplayId(display.id())->GetHost(); 70 } 71 72 virtual aura::client::ScreenPositionClient* GetScreenPositionClient( 73 const aura::Window* window) const OVERRIDE { 74 return aura::client::GetScreenPositionClient(window->GetRootWindow()); 75 } 76 77 private: 78 DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate); 79 }; 80 81 } // namespace 82 83 ///////////////////////////////////////////////////////////////////////////// 84 85 AshTestBase::AshTestBase() 86 : setup_called_(false), 87 teardown_called_(false), 88 start_session_(true) { 89 #if defined(USE_X11) 90 // This is needed for tests which use this base class but are run in browser 91 // test binaries so don't get the default initialization in the unit test 92 // suite. 93 gfx::InitializeThreadedX11(); 94 #endif 95 96 thread_bundle_.reset(new content::TestBrowserThreadBundle); 97 // Must initialize |ash_test_helper_| here because some tests rely on 98 // AshTestBase methods before they call AshTestBase::SetUp(). 99 ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current())); 100 } 101 102 AshTestBase::~AshTestBase() { 103 CHECK(setup_called_) 104 << "You have overridden SetUp but never called AshTestBase::SetUp"; 105 CHECK(teardown_called_) 106 << "You have overridden TearDown but never called AshTestBase::TearDown"; 107 } 108 109 void AshTestBase::SetUp() { 110 setup_called_ = true; 111 112 // Clears the saved state so that test doesn't use on the wrong 113 // default state. 114 shell::ToplevelWindow::ClearSavedStateForTest(); 115 116 // TODO(jamescook): Can we do this without changing command line? 117 // Use the origin (1,1) so that it doesn't over 118 // lap with the native mouse cursor. 119 CommandLine* command_line = CommandLine::ForCurrentProcess(); 120 if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) { 121 command_line->AppendSwitchASCII( 122 switches::kAshHostWindowBounds, "1+1-800x600"); 123 } 124 #if defined(OS_WIN) 125 aura::test::SetUsePopupAsRootWindowForTest(true); 126 #endif 127 ash_test_helper_->SetUp(start_session_); 128 129 Shell::GetPrimaryRootWindow()->Show(); 130 Shell::GetPrimaryRootWindow()->GetHost()->Show(); 131 // Move the mouse cursor to far away so that native events doesn't 132 // interfere test expectations. 133 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000)); 134 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents(); 135 136 // Changing GestureConfiguration shouldn't make tests fail. 137 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(5); 138 139 #if defined(OS_WIN) 140 if (!command_line->HasSwitch(ash::switches::kForceAshToDesktop)) { 141 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { 142 ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread")); 143 base::Thread::Options options; 144 options.message_loop_type = base::MessageLoop::TYPE_IO; 145 ipc_thread_->StartWithOptions(options); 146 metro_viewer_host_.reset( 147 new TestMetroViewerProcessHost(ipc_thread_->message_loop_proxy())); 148 CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection( 149 win8::test::kDefaultTestAppUserModelId)); 150 aura::RemoteWindowTreeHostWin* window_tree_host = 151 aura::RemoteWindowTreeHostWin::Instance(); 152 CHECK(window_tree_host != NULL); 153 } 154 ash::WindowPositioner::SetMaximizeFirstWindow(true); 155 } 156 #endif 157 } 158 159 void AshTestBase::TearDown() { 160 teardown_called_ = true; 161 // Flush the message loop to finish pending release tasks. 162 RunAllPendingInMessageLoop(); 163 164 #if defined(OS_WIN) 165 if (base::win::GetVersion() >= base::win::VERSION_WIN8 && 166 !CommandLine::ForCurrentProcess()->HasSwitch( 167 ash::switches::kForceAshToDesktop)) { 168 // Check that our viewer connection is still established. 169 CHECK(!metro_viewer_host_->closed_unexpectedly()); 170 } 171 #endif 172 173 ash_test_helper_->TearDown(); 174 #if defined(OS_WIN) 175 aura::test::SetUsePopupAsRootWindowForTest(false); 176 // Kill the viewer process if we spun one up. 177 metro_viewer_host_.reset(); 178 179 // Clean up any dangling viewer processes as the metro APIs sometimes leave 180 // zombies behind. A default browser process in metro will have the 181 // following command line arg so use that to avoid killing all processes named 182 // win8::test::kDefaultTestExePath. 183 const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer"; 184 base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath, 185 kViewerProcessArgument); 186 #endif 187 188 event_generator_.reset(); 189 // Some tests set an internal display id, 190 // reset it here, so other tests will continue in a clean environment. 191 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID); 192 } 193 194 aura::test::EventGenerator& AshTestBase::GetEventGenerator() { 195 if (!event_generator_) { 196 event_generator_.reset( 197 new aura::test::EventGenerator(new AshEventGeneratorDelegate())); 198 } 199 return *event_generator_.get(); 200 } 201 202 bool AshTestBase::SupportsMultipleDisplays() { 203 return AshTestHelper::SupportsMultipleDisplays(); 204 } 205 206 bool AshTestBase::SupportsHostWindowResize() { 207 return AshTestHelper::SupportsHostWindowResize(); 208 } 209 210 void AshTestBase::UpdateDisplay(const std::string& display_specs) { 211 DisplayManagerTestApi display_manager_test_api( 212 Shell::GetInstance()->display_manager()); 213 display_manager_test_api.UpdateDisplay(display_specs); 214 } 215 216 aura::Window* AshTestBase::CurrentContext() { 217 return ash_test_helper_->CurrentContext(); 218 } 219 220 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) { 221 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect()); 222 } 223 224 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds( 225 const gfx::Rect& bounds) { 226 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds); 227 } 228 229 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color, 230 int id, 231 const gfx::Rect& bounds) { 232 return CreateTestWindowInShellWithDelegate( 233 new aura::test::ColorTestWindowDelegate(color), id, bounds); 234 } 235 236 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate( 237 aura::WindowDelegate* delegate, 238 int id, 239 const gfx::Rect& bounds) { 240 return CreateTestWindowInShellWithDelegateAndType( 241 delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds); 242 } 243 244 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType( 245 aura::WindowDelegate* delegate, 246 ui::wm::WindowType type, 247 int id, 248 const gfx::Rect& bounds) { 249 aura::Window* window = new aura::Window(delegate); 250 window->set_id(id); 251 window->SetType(type); 252 window->Init(aura::WINDOW_LAYER_TEXTURED); 253 window->Show(); 254 255 if (bounds.IsEmpty()) { 256 ParentWindowInPrimaryRootWindow(window); 257 } else { 258 gfx::Display display = 259 Shell::GetScreen()->GetDisplayMatching(bounds); 260 aura::Window* root = ash::Shell::GetInstance()->display_controller()-> 261 GetRootWindowForDisplayId(display.id()); 262 gfx::Point origin = bounds.origin(); 263 wm::ConvertPointFromScreen(root, &origin); 264 window->SetBounds(gfx::Rect(origin, bounds.size())); 265 aura::client::ParentWindowWithContext(window, root, bounds); 266 } 267 window->SetProperty(aura::client::kCanMaximizeKey, true); 268 return window; 269 } 270 271 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) { 272 aura::client::ParentWindowWithContext( 273 window, Shell::GetPrimaryRootWindow(), gfx::Rect()); 274 } 275 276 void AshTestBase::RunAllPendingInMessageLoop() { 277 ash_test_helper_->RunAllPendingInMessageLoop(); 278 } 279 280 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() { 281 return ash_test_helper_->test_screenshot_delegate(); 282 } 283 284 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() { 285 return static_cast<TestSystemTrayDelegate*>( 286 Shell::GetInstance()->system_tray_delegate()); 287 } 288 289 void AshTestBase::SetSessionStarted(bool session_started) { 290 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()-> 291 SetActiveUserSessionStarted(session_started); 292 } 293 294 void AshTestBase::SetUserLoggedIn(bool user_logged_in) { 295 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()-> 296 SetHasActiveUser(user_logged_in); 297 } 298 299 void AshTestBase::SetCanLockScreen(bool can_lock_screen) { 300 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()-> 301 SetCanLockScreen(can_lock_screen); 302 } 303 304 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) { 305 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()-> 306 SetShouldLockScreenBeforeSuspending(should_lock); 307 } 308 309 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) { 310 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()-> 311 SetUserAddingScreenRunning(user_adding_screen_running); 312 } 313 314 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) { 315 switch (block_reason) { 316 case BLOCKED_BY_LOCK_SCREEN: 317 SetSessionStarted(true); 318 SetUserAddingScreenRunning(false); 319 Shell::GetInstance()->session_state_delegate()->LockScreen(); 320 break; 321 case BLOCKED_BY_LOGIN_SCREEN: 322 SetUserAddingScreenRunning(false); 323 SetSessionStarted(false); 324 break; 325 case BLOCKED_BY_USER_ADDING_SCREEN: 326 SetUserAddingScreenRunning(true); 327 SetSessionStarted(true); 328 break; 329 default: 330 NOTREACHED(); 331 break; 332 } 333 } 334 335 void AshTestBase::UnblockUserSession() { 336 Shell::GetInstance()->session_state_delegate()->UnlockScreen(); 337 SetSessionStarted(true); 338 SetUserAddingScreenRunning(false); 339 } 340 341 342 } // namespace test 343 } // namespace ash 344