ash_test_base.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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 // Must initialize |ash_test_helper_| here because some tests rely on 89 // AshTestBase methods before they call AshTestBase::SetUp(). 90 ash_test_helper_.reset(new AshTestHelper(&message_loop_)); 91} 92 93AshTestBase::~AshTestBase() { 94 CHECK(setup_called_) 95 << "You have overridden SetUp but never called AshTestBase::SetUp"; 96 CHECK(teardown_called_) 97 << "You have overridden TearDown but never called AshTestBase::TearDown"; 98} 99 100void AshTestBase::SetUp() { 101 setup_called_ = true; 102 // TODO(jamescook): Can we do this without changing command line? 103 // Use the origin (1,1) so that it doesn't over 104 // lap with the native mouse cursor. 105 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 106 switches::kAshHostWindowBounds, "1+1-800x600"); 107#if defined(OS_WIN) 108 aura::test::SetUsePopupAsRootWindowForTest(true); 109#endif 110 ui::InitializeInputMethodForTesting(); 111 112 ash_test_helper_->SetUp(); 113 114 Shell::GetPrimaryRootWindow()->Show(); 115 Shell::GetPrimaryRootWindow()->ShowRootWindow(); 116 // Move the mouse cursor to far away so that native events doesn't 117 // interfere test expectations. 118 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000)); 119 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents(); 120 121#if defined(OS_WIN) 122 if (base::win::GetVersion() >= base::win::VERSION_WIN8 && 123 !CommandLine::ForCurrentProcess()->HasSwitch( 124 ash::switches::kForceAshToDesktop)) { 125 ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread")); 126 base::Thread::Options options; 127 options.message_loop_type = base::MessageLoop::TYPE_IO; 128 ipc_thread_->StartWithOptions(options); 129 130 metro_viewer_host_.reset( 131 new TestMetroViewerProcessHost(ipc_thread_->message_loop_proxy())); 132 CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection( 133 win8::test::kDefaultTestAppUserModelId)); 134 aura::RemoteRootWindowHostWin* root_window_host = 135 aura::RemoteRootWindowHostWin::Instance(); 136 CHECK(root_window_host != NULL); 137 } 138#endif 139} 140 141void AshTestBase::TearDown() { 142 teardown_called_ = true; 143 // Flush the message loop to finish pending release tasks. 144 RunAllPendingInMessageLoop(); 145 146#if defined(OS_WIN) 147 if (base::win::GetVersion() >= base::win::VERSION_WIN8 && 148 !CommandLine::ForCurrentProcess()->HasSwitch( 149 ash::switches::kForceAshToDesktop)) { 150 // Check that our viewer connection is still established. 151 CHECK(!metro_viewer_host_->closed_unexpectedly()); 152 } 153#endif 154 155 ash_test_helper_->TearDown(); 156 157 ui::ShutdownInputMethodForTesting(); 158#if defined(OS_WIN) 159 aura::test::SetUsePopupAsRootWindowForTest(false); 160 // Kill the viewer process if we spun one up. 161 metro_viewer_host_.reset(); 162 163 // Clean up any dangling viewer processes as the metro APIs sometimes leave 164 // zombies behind. A default browser process in metro will have the 165 // following command line arg so use that to avoid killing all processes named 166 // win8::test::kDefaultTestExePath. 167 const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer"; 168 base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath, 169 kViewerProcessArgument); 170#endif 171 172 event_generator_.reset(); 173 // Some tests set an internal display id, 174 // reset it here, so other tests will continue in a clean environment. 175 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID); 176} 177 178aura::test::EventGenerator& AshTestBase::GetEventGenerator() { 179 if (!event_generator_) { 180 event_generator_.reset( 181 new aura::test::EventGenerator(new AshEventGeneratorDelegate())); 182 } 183 return *event_generator_.get(); 184} 185 186// static 187bool AshTestBase::SupportsMultipleDisplays() { 188#if defined(OS_WIN) 189 return base::win::GetVersion() < base::win::VERSION_WIN8; 190#else 191 return true; 192#endif 193} 194 195// static 196bool AshTestBase::SupportsHostWindowResize() { 197#if defined(OS_WIN) 198 return base::win::GetVersion() < base::win::VERSION_WIN8; 199#else 200 return true; 201#endif 202} 203 204void AshTestBase::UpdateDisplay(const std::string& display_specs) { 205 DisplayManagerTestApi display_manager_test_api( 206 Shell::GetInstance()->display_manager()); 207 display_manager_test_api.UpdateDisplay(display_specs); 208} 209 210aura::RootWindow* AshTestBase::CurrentContext() { 211 return ash_test_helper_->CurrentContext(); 212} 213 214aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) { 215 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect()); 216} 217 218aura::Window* AshTestBase::CreateTestWindowInShellWithBounds( 219 const gfx::Rect& bounds) { 220 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds); 221} 222 223aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color, 224 int id, 225 const gfx::Rect& bounds) { 226 return CreateTestWindowInShellWithDelegate( 227 new aura::test::ColorTestWindowDelegate(color), id, bounds); 228} 229 230aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate( 231 aura::WindowDelegate* delegate, 232 int id, 233 const gfx::Rect& bounds) { 234 return CreateTestWindowInShellWithDelegateAndType( 235 delegate, 236 aura::client::WINDOW_TYPE_NORMAL, 237 id, 238 bounds); 239} 240 241aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType( 242 aura::WindowDelegate* delegate, 243 aura::client::WindowType type, 244 int id, 245 const gfx::Rect& bounds) { 246 aura::Window* window = new aura::Window(delegate); 247 window->set_id(id); 248 window->SetType(type); 249 window->Init(ui::LAYER_TEXTURED); 250 window->Show(); 251 252 if (bounds.IsEmpty()) { 253 SetDefaultParentByPrimaryRootWindow(window); 254 } else { 255 gfx::Display display = 256 Shell::GetScreen()->GetDisplayMatching(bounds); 257 aura::RootWindow* root = ash::Shell::GetInstance()->display_controller()-> 258 GetRootWindowForDisplayId(display.id()); 259 gfx::Point origin = bounds.origin(); 260 wm::ConvertPointFromScreen(root, &origin); 261 window->SetBounds(gfx::Rect(origin, bounds.size())); 262 window->SetDefaultParentByRootWindow(root, bounds); 263 } 264 window->SetProperty(aura::client::kCanMaximizeKey, true); 265 return window; 266} 267 268void AshTestBase::SetDefaultParentByPrimaryRootWindow(aura::Window* window) { 269 window->SetDefaultParentByRootWindow( 270 Shell::GetPrimaryRootWindow(), gfx::Rect()); 271} 272 273void AshTestBase::RunAllPendingInMessageLoop() { 274 ash_test_helper_->RunAllPendingInMessageLoop(); 275} 276 277void AshTestBase::SetSessionStarted(bool session_started) { 278 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()-> 279 SetActiveUserSessionStarted(session_started); 280} 281 282void AshTestBase::SetUserLoggedIn(bool user_logged_in) { 283 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()-> 284 SetHasActiveUser(user_logged_in); 285} 286 287void AshTestBase::SetCanLockScreen(bool can_lock_screen) { 288 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()-> 289 SetCanLockScreen(can_lock_screen); 290} 291 292} // namespace test 293} // namespace ash 294