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