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/root_window.h"
27 #include "ui/aura/test/event_generator.h"
28 #include "ui/aura/test/test_window_delegate.h"
29 #include "ui/aura/window.h"
30 #include "ui/aura/window_delegate.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"
55 class AshEventGeneratorDelegate : public aura::test::EventGeneratorDelegate {
57 AshEventGeneratorDelegate() {}
58 virtual ~AshEventGeneratorDelegate() {}
60 // aura::test::EventGeneratorDelegate overrides:
61 virtual aura::RootWindow* GetRootWindowAt(
62 const gfx::Point& point_in_screen) const OVERRIDE {
63 gfx::Screen* screen = Shell::GetScreen();
64 gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
65 return Shell::GetInstance()->display_controller()->
66 GetRootWindowForDisplayId(display.id())->GetDispatcher();
69 virtual aura::client::ScreenPositionClient* GetScreenPositionClient(
70 const aura::Window* window) const OVERRIDE {
71 return aura::client::GetScreenPositionClient(window->GetRootWindow());
75 DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
80 content::WebContents* AshTestViewsDelegate::CreateWebContents(
81 content::BrowserContext* browser_context,
82 content::SiteInstance* site_instance) {
83 return content::WebContentsTester::CreateTestWebContents(browser_context,
87 /////////////////////////////////////////////////////////////////////////////
89 AshTestBase::AshTestBase()
90 : setup_called_(false),
91 teardown_called_(false),
92 start_session_(true) {
93 // Must initialize |ash_test_helper_| here because some tests rely on
94 // AshTestBase methods before they call AshTestBase::SetUp().
95 ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current()));
98 AshTestBase::~AshTestBase() {
100 << "You have overridden SetUp but never called AshTestBase::SetUp";
101 CHECK(teardown_called_)
102 << "You have overridden TearDown but never called AshTestBase::TearDown";
105 void AshTestBase::SetUp() {
106 setup_called_ = true;
108 // Clears the saved state so that test doesn't use on the wrong
110 shell::ToplevelWindow::ClearSavedStateForTest();
112 // TODO(jamescook): Can we do this without changing command line?
113 // Use the origin (1,1) so that it doesn't over
114 // lap with the native mouse cursor.
115 CommandLine* command_line = CommandLine::ForCurrentProcess();
116 if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
117 command_line->AppendSwitchASCII(
118 switches::kAshHostWindowBounds, "1+1-800x600");
121 aura::test::SetUsePopupAsRootWindowForTest(true);
123 ash_test_helper_->SetUp(start_session_);
125 Shell::GetPrimaryRootWindow()->Show();
126 Shell::GetPrimaryRootWindow()->GetDispatcher()->host()->Show();
127 // Move the mouse cursor to far away so that native events doesn't
128 // interfere test expectations.
129 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
130 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
132 // Changing GestureConfiguration shouldn't make tests fail.
133 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(5);
136 if (!command_line->HasSwitch(ash::switches::kForceAshToDesktop)) {
137 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
138 ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread"));
139 base::Thread::Options options;
140 options.message_loop_type = base::MessageLoop::TYPE_IO;
141 ipc_thread_->StartWithOptions(options);
142 metro_viewer_host_.reset(
143 new TestMetroViewerProcessHost(ipc_thread_->message_loop_proxy()));
144 CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
145 win8::test::kDefaultTestAppUserModelId));
146 aura::RemoteWindowTreeHostWin* window_tree_host =
147 aura::RemoteWindowTreeHostWin::Instance();
148 CHECK(window_tree_host != NULL);
150 ash::WindowPositioner::SetMaximizeFirstWindow(true);
155 void AshTestBase::TearDown() {
156 teardown_called_ = true;
157 // Flush the message loop to finish pending release tasks.
158 RunAllPendingInMessageLoop();
161 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
162 !CommandLine::ForCurrentProcess()->HasSwitch(
163 ash::switches::kForceAshToDesktop)) {
164 // Check that our viewer connection is still established.
165 CHECK(!metro_viewer_host_->closed_unexpectedly());
169 ash_test_helper_->TearDown();
171 aura::test::SetUsePopupAsRootWindowForTest(false);
172 // Kill the viewer process if we spun one up.
173 metro_viewer_host_.reset();
175 // Clean up any dangling viewer processes as the metro APIs sometimes leave
176 // zombies behind. A default browser process in metro will have the
177 // following command line arg so use that to avoid killing all processes named
178 // win8::test::kDefaultTestExePath.
179 const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer";
180 base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath,
181 kViewerProcessArgument);
184 event_generator_.reset();
185 // Some tests set an internal display id,
186 // reset it here, so other tests will continue in a clean environment.
187 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
190 aura::test::EventGenerator& AshTestBase::GetEventGenerator() {
191 if (!event_generator_) {
192 event_generator_.reset(
193 new aura::test::EventGenerator(new AshEventGeneratorDelegate()));
195 return *event_generator_.get();
199 bool AshTestBase::SupportsMultipleDisplays() {
201 return base::win::GetVersion() < base::win::VERSION_WIN8;
208 bool AshTestBase::SupportsHostWindowResize() {
210 return base::win::GetVersion() < base::win::VERSION_WIN8;
216 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
217 DisplayManagerTestApi display_manager_test_api(
218 Shell::GetInstance()->display_manager());
219 display_manager_test_api.UpdateDisplay(display_specs);
222 aura::Window* AshTestBase::CurrentContext() {
223 return ash_test_helper_->CurrentContext();
226 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
227 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
230 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
231 const gfx::Rect& bounds) {
232 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
235 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
237 const gfx::Rect& bounds) {
238 return CreateTestWindowInShellWithDelegate(
239 new aura::test::ColorTestWindowDelegate(color), id, bounds);
242 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
243 aura::WindowDelegate* delegate,
245 const gfx::Rect& bounds) {
246 return CreateTestWindowInShellWithDelegateAndType(
247 delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds);
250 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
251 aura::WindowDelegate* delegate,
252 ui::wm::WindowType type,
254 const gfx::Rect& bounds) {
255 aura::Window* window = new aura::Window(delegate);
257 window->SetType(type);
258 window->Init(aura::WINDOW_LAYER_TEXTURED);
261 if (bounds.IsEmpty()) {
262 ParentWindowInPrimaryRootWindow(window);
264 gfx::Display display =
265 Shell::GetScreen()->GetDisplayMatching(bounds);
266 aura::Window* root = ash::Shell::GetInstance()->display_controller()->
267 GetRootWindowForDisplayId(display.id());
268 gfx::Point origin = bounds.origin();
269 wm::ConvertPointFromScreen(root, &origin);
270 window->SetBounds(gfx::Rect(origin, bounds.size()));
271 aura::client::ParentWindowWithContext(window, root, bounds);
273 window->SetProperty(aura::client::kCanMaximizeKey, true);
277 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
278 aura::client::ParentWindowWithContext(
279 window, Shell::GetPrimaryRootWindow(), gfx::Rect());
282 void AshTestBase::RunAllPendingInMessageLoop() {
283 ash_test_helper_->RunAllPendingInMessageLoop();
286 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
287 return ash_test_helper_->test_screenshot_delegate();
290 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() {
291 return static_cast<TestSystemTrayDelegate*>(
292 Shell::GetInstance()->system_tray_delegate());
295 void AshTestBase::SetSessionStarted(bool session_started) {
296 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
297 SetActiveUserSessionStarted(session_started);
300 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
301 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
302 SetHasActiveUser(user_logged_in);
305 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
306 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
307 SetCanLockScreen(can_lock_screen);
310 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
311 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
312 SetShouldLockScreenBeforeSuspending(should_lock);
315 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
316 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
317 SetUserAddingScreenRunning(user_adding_screen_running);
320 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
321 switch (block_reason) {
322 case BLOCKED_BY_LOCK_SCREEN:
323 SetSessionStarted(true);
324 SetUserAddingScreenRunning(false);
325 Shell::GetInstance()->session_state_delegate()->LockScreen();
327 case BLOCKED_BY_LOGIN_SCREEN:
328 SetUserAddingScreenRunning(false);
329 SetSessionStarted(false);
331 case BLOCKED_BY_USER_ADDING_SCREEN:
332 SetUserAddingScreenRunning(true);
333 SetSessionStarted(true);
341 void AshTestBase::UnblockUserSession() {
342 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
343 SetSessionStarted(true);
344 SetUserAddingScreenRunning(false);