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/screen_ash.h"
13 #include "ash/shell.h"
14 #include "ash/shell/toplevel_window.h"
15 #include "ash/test/ash_test_helper.h"
16 #include "ash/test/display_manager_test_api.h"
17 #include "ash/test/test_session_state_delegate.h"
18 #include "ash/test/test_shell_delegate.h"
19 #include "ash/wm/coordinate_conversion.h"
20 #include "base/command_line.h"
21 #include "content/public/test/web_contents_tester.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/root_window.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/base/ime/input_method_initializer.h"
31 #include "ui/gfx/display.h"
32 #include "ui/gfx/point.h"
33 #include "ui/gfx/screen.h"
35 #if defined(OS_CHROMEOS)
36 #include "ash/system/chromeos/tray_display.h"
40 #include "ash/test/test_metro_viewer_process_host.h"
41 #include "base/test/test_process_killer_win.h"
42 #include "base/win/metro.h"
43 #include "base/win/windows_version.h"
44 #include "ui/aura/remote_root_window_host_win.h"
45 #include "ui/aura/root_window_host_win.h"
46 #include "win8/test/test_registrar_constants.h"
53 class AshEventGeneratorDelegate : public aura::test::EventGeneratorDelegate {
55 AshEventGeneratorDelegate() {}
56 virtual ~AshEventGeneratorDelegate() {}
58 // aura::test::EventGeneratorDelegate overrides:
59 virtual aura::RootWindow* GetRootWindowAt(
60 const gfx::Point& point_in_screen) const OVERRIDE {
61 gfx::Screen* screen = Shell::GetScreen();
62 gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
63 return Shell::GetInstance()->display_controller()->
64 GetRootWindowForDisplayId(display.id())->GetDispatcher();
67 virtual aura::client::ScreenPositionClient* GetScreenPositionClient(
68 const aura::Window* window) const OVERRIDE {
69 return aura::client::GetScreenPositionClient(window->GetRootWindow());
73 DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
78 content::WebContents* AshTestViewsDelegate::CreateWebContents(
79 content::BrowserContext* browser_context,
80 content::SiteInstance* site_instance) {
81 return content::WebContentsTester::CreateTestWebContents(browser_context,
85 /////////////////////////////////////////////////////////////////////////////
87 AshTestBase::AshTestBase()
88 : setup_called_(false),
89 teardown_called_(false),
90 start_session_(true) {
91 // Must initialize |ash_test_helper_| here because some tests rely on
92 // AshTestBase methods before they call AshTestBase::SetUp().
93 ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current()));
96 AshTestBase::~AshTestBase() {
98 << "You have overridden SetUp but never called AshTestBase::SetUp";
99 CHECK(teardown_called_)
100 << "You have overridden TearDown but never called AshTestBase::TearDown";
103 void AshTestBase::SetUp() {
104 setup_called_ = true;
106 // Clears the saved state so that test doesn't use on the wrong
108 shell::ToplevelWindow::ClearSavedStateForTest();
110 // TODO(jamescook): Can we do this without changing command line?
111 // Use the origin (1,1) so that it doesn't over
112 // lap with the native mouse cursor.
113 CommandLine* command_line = CommandLine::ForCurrentProcess();
114 if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
115 command_line->AppendSwitchASCII(
116 switches::kAshHostWindowBounds, "1+1-800x600");
119 aura::test::SetUsePopupAsRootWindowForTest(true);
121 ash_test_helper_->SetUp(start_session_);
123 Shell::GetPrimaryRootWindow()->Show();
124 Shell::GetPrimaryRootWindow()->GetDispatcher()->ShowRootWindow();
125 // Move the mouse cursor to far away so that native events doesn't
126 // interfere test expectations.
127 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
128 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
131 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
132 !command_line->HasSwitch(ash::switches::kForceAshToDesktop)) {
133 ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread"));
134 base::Thread::Options options;
135 options.message_loop_type = base::MessageLoop::TYPE_IO;
136 ipc_thread_->StartWithOptions(options);
138 metro_viewer_host_.reset(
139 new TestMetroViewerProcessHost(ipc_thread_->message_loop_proxy()));
140 CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
141 win8::test::kDefaultTestAppUserModelId));
142 aura::RemoteRootWindowHostWin* root_window_host =
143 aura::RemoteRootWindowHostWin::Instance();
144 CHECK(root_window_host != NULL);
149 void AshTestBase::TearDown() {
150 teardown_called_ = true;
151 // Flush the message loop to finish pending release tasks.
152 RunAllPendingInMessageLoop();
155 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
156 !CommandLine::ForCurrentProcess()->HasSwitch(
157 ash::switches::kForceAshToDesktop)) {
158 // Check that our viewer connection is still established.
159 CHECK(!metro_viewer_host_->closed_unexpectedly());
163 ash_test_helper_->TearDown();
165 aura::test::SetUsePopupAsRootWindowForTest(false);
166 // Kill the viewer process if we spun one up.
167 metro_viewer_host_.reset();
169 // Clean up any dangling viewer processes as the metro APIs sometimes leave
170 // zombies behind. A default browser process in metro will have the
171 // following command line arg so use that to avoid killing all processes named
172 // win8::test::kDefaultTestExePath.
173 const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer";
174 base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath,
175 kViewerProcessArgument);
178 event_generator_.reset();
179 // Some tests set an internal display id,
180 // reset it here, so other tests will continue in a clean environment.
181 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
184 aura::test::EventGenerator& AshTestBase::GetEventGenerator() {
185 if (!event_generator_) {
186 event_generator_.reset(
187 new aura::test::EventGenerator(new AshEventGeneratorDelegate()));
189 return *event_generator_.get();
193 bool AshTestBase::SupportsMultipleDisplays() {
195 return base::win::GetVersion() < base::win::VERSION_WIN8;
202 bool AshTestBase::SupportsHostWindowResize() {
204 return base::win::GetVersion() < base::win::VERSION_WIN8;
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);
216 aura::Window* AshTestBase::CurrentContext() {
217 return ash_test_helper_->CurrentContext();
220 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
221 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
224 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
225 const gfx::Rect& bounds) {
226 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
229 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
231 const gfx::Rect& bounds) {
232 return CreateTestWindowInShellWithDelegate(
233 new aura::test::ColorTestWindowDelegate(color), id, bounds);
236 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
237 aura::WindowDelegate* delegate,
239 const gfx::Rect& bounds) {
240 return CreateTestWindowInShellWithDelegateAndType(
242 aura::client::WINDOW_TYPE_NORMAL,
247 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
248 aura::WindowDelegate* delegate,
249 aura::client::WindowType type,
251 const gfx::Rect& bounds) {
252 aura::Window* window = new aura::Window(delegate);
254 window->SetType(type);
255 window->Init(ui::LAYER_TEXTURED);
258 if (bounds.IsEmpty()) {
259 ParentWindowInPrimaryRootWindow(window);
261 gfx::Display display =
262 Shell::GetScreen()->GetDisplayMatching(bounds);
263 aura::Window* root = ash::Shell::GetInstance()->display_controller()->
264 GetRootWindowForDisplayId(display.id());
265 gfx::Point origin = bounds.origin();
266 wm::ConvertPointFromScreen(root, &origin);
267 window->SetBounds(gfx::Rect(origin, bounds.size()));
268 aura::client::ParentWindowWithContext(window, root, bounds);
270 window->SetProperty(aura::client::kCanMaximizeKey, true);
274 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
275 aura::client::ParentWindowWithContext(
276 window, Shell::GetPrimaryRootWindow(), gfx::Rect());
279 void AshTestBase::RunAllPendingInMessageLoop() {
280 ash_test_helper_->RunAllPendingInMessageLoop();
283 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
284 return ash_test_helper_->test_screenshot_delegate();
287 void AshTestBase::SetSessionStarted(bool session_started) {
288 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
289 SetActiveUserSessionStarted(session_started);
292 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
293 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
294 SetHasActiveUser(user_logged_in);
297 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
298 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
299 SetCanLockScreen(can_lock_screen);
302 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
303 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
304 SetShouldLockScreenBeforeSuspending(should_lock);
307 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
308 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
309 SetUserAddingScreenRunning(user_adding_screen_running);
312 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
313 switch (block_reason) {
314 case BLOCKED_BY_LOCK_SCREEN:
315 SetSessionStarted(true);
316 SetUserAddingScreenRunning(false);
317 Shell::GetInstance()->session_state_delegate()->LockScreen();
319 case BLOCKED_BY_LOGIN_SCREEN:
320 SetUserAddingScreenRunning(false);
321 SetSessionStarted(false);
323 case BLOCKED_BY_USER_ADDING_SCREEN:
324 SetUserAddingScreenRunning(true);
325 SetSessionStarted(true);
333 void AshTestBase::UnblockUserSession() {
334 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
335 SetSessionStarted(true);
336 SetUserAddingScreenRunning(false);