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.
5 #include "ash/test/ash_test_base.h"
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 "content/public/test/web_contents_tester.h"
23 #include "ui/aura/client/aura_constants.h"
24 #include "ui/aura/client/screen_position_client.h"
25 #include "ui/aura/client/window_tree_client.h"
26 #include "ui/aura/test/event_generator.h"
27 #include "ui/aura/test/test_window_delegate.h"
28 #include "ui/aura/window.h"
29 #include "ui/aura/window_delegate.h"
30 #include "ui/aura/window_tree_host.h"
31 #include "ui/base/ime/input_method_initializer.h"
32 #include "ui/events/gestures/gesture_configuration.h"
33 #include "ui/gfx/display.h"
34 #include "ui/gfx/point.h"
35 #include "ui/gfx/screen.h"
37 #if defined(OS_CHROMEOS)
38 #include "ash/system/chromeos/tray_display.h"
42 #include "ash/test/test_metro_viewer_process_host.h"
43 #include "base/test/test_process_killer_win.h"
44 #include "base/win/metro.h"
45 #include "base/win/windows_version.h"
46 #include "ui/aura/remote_window_tree_host_win.h"
47 #include "ui/aura/window_tree_host_win.h"
48 #include "win8/test/test_registrar_constants.h"
52 #include "ui/gfx/x/x11_connection.h"
59 class AshEventGeneratorDelegate : public aura::test::EventGeneratorDelegate {
61 AshEventGeneratorDelegate() {}
62 virtual ~AshEventGeneratorDelegate() {}
64 // aura::test::EventGeneratorDelegate overrides:
65 virtual aura::WindowTreeHost* GetHostAt(
66 const gfx::Point& point_in_screen) const OVERRIDE {
67 gfx::Screen* screen = Shell::GetScreen();
68 gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
69 return Shell::GetInstance()->display_controller()->
70 GetRootWindowForDisplayId(display.id())->GetHost();
73 virtual aura::client::ScreenPositionClient* GetScreenPositionClient(
74 const aura::Window* window) const OVERRIDE {
75 return aura::client::GetScreenPositionClient(window->GetRootWindow());
79 DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
84 content::WebContents* AshTestViewsDelegate::CreateWebContents(
85 content::BrowserContext* browser_context,
86 content::SiteInstance* site_instance) {
87 return content::WebContentsTester::CreateTestWebContents(browser_context,
91 /////////////////////////////////////////////////////////////////////////////
93 AshTestBase::AshTestBase()
94 : setup_called_(false),
95 teardown_called_(false),
96 start_session_(true) {
98 // This is needed for tests which use this base class but are run in browser
99 // test binaries so don't get the default initialization in the unit test
101 gfx::InitializeThreadedX11();
104 thread_bundle_.reset(new content::TestBrowserThreadBundle);
105 // Must initialize |ash_test_helper_| here because some tests rely on
106 // AshTestBase methods before they call AshTestBase::SetUp().
107 ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current()));
110 AshTestBase::~AshTestBase() {
112 << "You have overridden SetUp but never called AshTestBase::SetUp";
113 CHECK(teardown_called_)
114 << "You have overridden TearDown but never called AshTestBase::TearDown";
117 void AshTestBase::SetUp() {
118 setup_called_ = true;
120 // Clears the saved state so that test doesn't use on the wrong
122 shell::ToplevelWindow::ClearSavedStateForTest();
124 // TODO(jamescook): Can we do this without changing command line?
125 // Use the origin (1,1) so that it doesn't over
126 // lap with the native mouse cursor.
127 CommandLine* command_line = CommandLine::ForCurrentProcess();
128 if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
129 command_line->AppendSwitchASCII(
130 switches::kAshHostWindowBounds, "1+1-800x600");
133 aura::test::SetUsePopupAsRootWindowForTest(true);
135 ash_test_helper_->SetUp(start_session_);
137 Shell::GetPrimaryRootWindow()->Show();
138 Shell::GetPrimaryRootWindow()->GetHost()->Show();
139 // Move the mouse cursor to far away so that native events doesn't
140 // interfere test expectations.
141 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
142 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
144 // Changing GestureConfiguration shouldn't make tests fail.
145 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(5);
148 if (!command_line->HasSwitch(ash::switches::kForceAshToDesktop)) {
149 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
150 ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread"));
151 base::Thread::Options options;
152 options.message_loop_type = base::MessageLoop::TYPE_IO;
153 ipc_thread_->StartWithOptions(options);
154 metro_viewer_host_.reset(
155 new TestMetroViewerProcessHost(ipc_thread_->message_loop_proxy()));
156 CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
157 win8::test::kDefaultTestAppUserModelId));
158 aura::RemoteWindowTreeHostWin* window_tree_host =
159 aura::RemoteWindowTreeHostWin::Instance();
160 CHECK(window_tree_host != NULL);
162 ash::WindowPositioner::SetMaximizeFirstWindow(true);
167 void AshTestBase::TearDown() {
168 teardown_called_ = true;
169 // Flush the message loop to finish pending release tasks.
170 RunAllPendingInMessageLoop();
173 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
174 !CommandLine::ForCurrentProcess()->HasSwitch(
175 ash::switches::kForceAshToDesktop)) {
176 // Check that our viewer connection is still established.
177 CHECK(!metro_viewer_host_->closed_unexpectedly());
181 ash_test_helper_->TearDown();
183 aura::test::SetUsePopupAsRootWindowForTest(false);
184 // Kill the viewer process if we spun one up.
185 metro_viewer_host_.reset();
187 // Clean up any dangling viewer processes as the metro APIs sometimes leave
188 // zombies behind. A default browser process in metro will have the
189 // following command line arg so use that to avoid killing all processes named
190 // win8::test::kDefaultTestExePath.
191 const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer";
192 base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath,
193 kViewerProcessArgument);
196 event_generator_.reset();
197 // Some tests set an internal display id,
198 // reset it here, so other tests will continue in a clean environment.
199 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
202 aura::test::EventGenerator& AshTestBase::GetEventGenerator() {
203 if (!event_generator_) {
204 event_generator_.reset(
205 new aura::test::EventGenerator(new AshEventGeneratorDelegate()));
207 return *event_generator_.get();
210 bool AshTestBase::SupportsMultipleDisplays() {
211 return AshTestHelper::SupportsMultipleDisplays();
214 bool AshTestBase::SupportsHostWindowResize() {
215 return AshTestHelper::SupportsHostWindowResize();
218 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
219 DisplayManagerTestApi display_manager_test_api(
220 Shell::GetInstance()->display_manager());
221 display_manager_test_api.UpdateDisplay(display_specs);
224 aura::Window* AshTestBase::CurrentContext() {
225 return ash_test_helper_->CurrentContext();
228 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
229 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
232 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
233 const gfx::Rect& bounds) {
234 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
237 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
239 const gfx::Rect& bounds) {
240 return CreateTestWindowInShellWithDelegate(
241 new aura::test::ColorTestWindowDelegate(color), id, bounds);
244 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
245 aura::WindowDelegate* delegate,
247 const gfx::Rect& bounds) {
248 return CreateTestWindowInShellWithDelegateAndType(
249 delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds);
252 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
253 aura::WindowDelegate* delegate,
254 ui::wm::WindowType type,
256 const gfx::Rect& bounds) {
257 aura::Window* window = new aura::Window(delegate);
259 window->SetType(type);
260 window->Init(aura::WINDOW_LAYER_TEXTURED);
263 if (bounds.IsEmpty()) {
264 ParentWindowInPrimaryRootWindow(window);
266 gfx::Display display =
267 Shell::GetScreen()->GetDisplayMatching(bounds);
268 aura::Window* root = ash::Shell::GetInstance()->display_controller()->
269 GetRootWindowForDisplayId(display.id());
270 gfx::Point origin = bounds.origin();
271 wm::ConvertPointFromScreen(root, &origin);
272 window->SetBounds(gfx::Rect(origin, bounds.size()));
273 aura::client::ParentWindowWithContext(window, root, bounds);
275 window->SetProperty(aura::client::kCanMaximizeKey, true);
279 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
280 aura::client::ParentWindowWithContext(
281 window, Shell::GetPrimaryRootWindow(), gfx::Rect());
284 void AshTestBase::RunAllPendingInMessageLoop() {
285 ash_test_helper_->RunAllPendingInMessageLoop();
288 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
289 return ash_test_helper_->test_screenshot_delegate();
292 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() {
293 return static_cast<TestSystemTrayDelegate*>(
294 Shell::GetInstance()->system_tray_delegate());
297 void AshTestBase::SetSessionStarted(bool session_started) {
298 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
299 SetActiveUserSessionStarted(session_started);
302 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
303 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
304 SetHasActiveUser(user_logged_in);
307 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
308 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
309 SetCanLockScreen(can_lock_screen);
312 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
313 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
314 SetShouldLockScreenBeforeSuspending(should_lock);
317 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
318 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
319 SetUserAddingScreenRunning(user_adding_screen_running);
322 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
323 switch (block_reason) {
324 case BLOCKED_BY_LOCK_SCREEN:
325 SetSessionStarted(true);
326 SetUserAddingScreenRunning(false);
327 Shell::GetInstance()->session_state_delegate()->LockScreen();
329 case BLOCKED_BY_LOGIN_SCREEN:
330 SetUserAddingScreenRunning(false);
331 SetSessionStarted(false);
333 case BLOCKED_BY_USER_ADDING_SCREEN:
334 SetUserAddingScreenRunning(true);
335 SetSessionStarted(true);
343 void AshTestBase::UnblockUserSession() {
344 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
345 SetSessionStarted(true);
346 SetUserAddingScreenRunning(false);