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 "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/test/event_generator_delegate_aura.h"
26 #include "ui/aura/test/test_window_delegate.h"
27 #include "ui/aura/window.h"
28 #include "ui/aura/window_delegate.h"
29 #include "ui/aura/window_tree_host.h"
30 #include "ui/base/ime/input_method_initializer.h"
31 #include "ui/events/gestures/gesture_configuration.h"
32 #include "ui/gfx/display.h"
33 #include "ui/gfx/point.h"
34 #include "ui/gfx/screen.h"
36 #if defined(OS_CHROMEOS)
37 #include "ash/system/chromeos/tray_display.h"
41 #include "ash/test/test_metro_viewer_process_host.h"
42 #include "base/test/test_process_killer_win.h"
43 #include "base/win/metro.h"
44 #include "base/win/windows_version.h"
45 #include "ui/aura/remote_window_tree_host_win.h"
46 #include "ui/aura/window_tree_host_win.h"
47 #include "ui/platform_window/win/win_window.h"
48 #include "win8/test/test_registrar_constants.h"
52 #include "ui/gfx/x/x11_connection.h"
59 class AshEventGeneratorDelegate
60 : public aura::test::EventGeneratorDelegateAura
{
62 AshEventGeneratorDelegate() {}
63 virtual ~AshEventGeneratorDelegate() {}
65 // aura::test::EventGeneratorDelegateAura overrides:
66 virtual aura::WindowTreeHost
* GetHostAt(
67 const gfx::Point
& point_in_screen
) const OVERRIDE
{
68 gfx::Screen
* screen
= Shell::GetScreen();
69 gfx::Display display
= screen
->GetDisplayNearestPoint(point_in_screen
);
70 return Shell::GetInstance()->display_controller()->
71 GetRootWindowForDisplayId(display
.id())->GetHost();
74 virtual aura::client::ScreenPositionClient
* GetScreenPositionClient(
75 const aura::Window
* window
) const OVERRIDE
{
76 return aura::client::GetScreenPositionClient(window
->GetRootWindow());
80 DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate
);
85 /////////////////////////////////////////////////////////////////////////////
87 AshTestBase::AshTestBase()
88 : setup_called_(false),
89 teardown_called_(false),
90 start_session_(true) {
92 // This is needed for tests which use this base class but are run in browser
93 // test binaries so don't get the default initialization in the unit test
95 gfx::InitializeThreadedX11();
98 thread_bundle_
.reset(new content::TestBrowserThreadBundle
);
99 // Must initialize |ash_test_helper_| here because some tests rely on
100 // AshTestBase methods before they call AshTestBase::SetUp().
101 ash_test_helper_
.reset(new AshTestHelper(base::MessageLoopForUI::current()));
104 AshTestBase::~AshTestBase() {
106 << "You have overridden SetUp but never called AshTestBase::SetUp";
107 CHECK(teardown_called_
)
108 << "You have overridden TearDown but never called AshTestBase::TearDown";
111 void AshTestBase::SetUp() {
112 setup_called_
= true;
114 // Clears the saved state so that test doesn't use on the wrong
116 shell::ToplevelWindow::ClearSavedStateForTest();
118 // TODO(jamescook): Can we do this without changing command line?
119 // Use the origin (1,1) so that it doesn't over
120 // lap with the native mouse cursor.
121 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
122 if (!command_line
->HasSwitch(switches::kAshHostWindowBounds
)) {
123 command_line
->AppendSwitchASCII(
124 switches::kAshHostWindowBounds
, "1+1-800x600");
127 ui::test::SetUsePopupAsRootWindowForTest(true);
129 ash_test_helper_
->SetUp(start_session_
);
131 Shell::GetPrimaryRootWindow()->Show();
132 Shell::GetPrimaryRootWindow()->GetHost()->Show();
133 // Move the mouse cursor to far away so that native events doesn't
134 // interfere test expectations.
135 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
136 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
138 // Changing GestureConfiguration shouldn't make tests fail.
139 ui::GestureConfiguration::set_max_touch_move_in_pixels_for_click(5);
142 if (!command_line
->HasSwitch(ash::switches::kForceAshToDesktop
)) {
143 if (base::win::GetVersion() >= base::win::VERSION_WIN8
) {
144 ipc_thread_
.reset(new base::Thread("test_metro_viewer_ipc_thread"));
145 base::Thread::Options options
;
146 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
147 ipc_thread_
->StartWithOptions(options
);
148 metro_viewer_host_
.reset(
149 new TestMetroViewerProcessHost(ipc_thread_
->message_loop_proxy()));
150 CHECK(metro_viewer_host_
->LaunchViewerAndWaitForConnection(
151 win8::test::kDefaultTestAppUserModelId
));
152 aura::RemoteWindowTreeHostWin
* window_tree_host
=
153 aura::RemoteWindowTreeHostWin::Instance();
154 CHECK(window_tree_host
!= NULL
);
156 ash::WindowPositioner::SetMaximizeFirstWindow(true);
161 void AshTestBase::TearDown() {
162 teardown_called_
= true;
163 // Flush the message loop to finish pending release tasks.
164 RunAllPendingInMessageLoop();
167 if (base::win::GetVersion() >= base::win::VERSION_WIN8
&&
168 !CommandLine::ForCurrentProcess()->HasSwitch(
169 ash::switches::kForceAshToDesktop
)) {
170 // Check that our viewer connection is still established.
171 CHECK(!metro_viewer_host_
->closed_unexpectedly());
175 ash_test_helper_
->TearDown();
177 ui::test::SetUsePopupAsRootWindowForTest(false);
178 // Kill the viewer process if we spun one up.
179 metro_viewer_host_
.reset();
181 // Clean up any dangling viewer processes as the metro APIs sometimes leave
182 // zombies behind. A default browser process in metro will have the
183 // following command line arg so use that to avoid killing all processes named
184 // win8::test::kDefaultTestExePath.
185 const wchar_t kViewerProcessArgument
[] = L
"DefaultBrowserServer";
186 base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath
,
187 kViewerProcessArgument
);
190 event_generator_
.reset();
191 // Some tests set an internal display id,
192 // reset it here, so other tests will continue in a clean environment.
193 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID
);
196 ui::test::EventGenerator
& AshTestBase::GetEventGenerator() {
197 if (!event_generator_
) {
198 event_generator_
.reset(
199 new ui::test::EventGenerator(new AshEventGeneratorDelegate()));
201 return *event_generator_
.get();
204 bool AshTestBase::SupportsMultipleDisplays() {
205 return AshTestHelper::SupportsMultipleDisplays();
208 bool AshTestBase::SupportsHostWindowResize() {
209 return AshTestHelper::SupportsHostWindowResize();
212 void AshTestBase::UpdateDisplay(const std::string
& display_specs
) {
213 DisplayManagerTestApi
display_manager_test_api(
214 Shell::GetInstance()->display_manager());
215 display_manager_test_api
.UpdateDisplay(display_specs
);
218 aura::Window
* AshTestBase::CurrentContext() {
219 return ash_test_helper_
->CurrentContext();
222 aura::Window
* AshTestBase::CreateTestWindowInShellWithId(int id
) {
223 return CreateTestWindowInShellWithDelegate(NULL
, id
, gfx::Rect());
226 aura::Window
* AshTestBase::CreateTestWindowInShellWithBounds(
227 const gfx::Rect
& bounds
) {
228 return CreateTestWindowInShellWithDelegate(NULL
, 0, bounds
);
231 aura::Window
* AshTestBase::CreateTestWindowInShell(SkColor color
,
233 const gfx::Rect
& bounds
) {
234 return CreateTestWindowInShellWithDelegate(
235 new aura::test::ColorTestWindowDelegate(color
), id
, bounds
);
238 aura::Window
* AshTestBase::CreateTestWindowInShellWithDelegate(
239 aura::WindowDelegate
* delegate
,
241 const gfx::Rect
& bounds
) {
242 return CreateTestWindowInShellWithDelegateAndType(
243 delegate
, ui::wm::WINDOW_TYPE_NORMAL
, id
, bounds
);
246 aura::Window
* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
247 aura::WindowDelegate
* delegate
,
248 ui::wm::WindowType type
,
250 const gfx::Rect
& bounds
) {
251 aura::Window
* window
= new aura::Window(delegate
);
253 window
->SetType(type
);
254 window
->Init(aura::WINDOW_LAYER_TEXTURED
);
257 if (bounds
.IsEmpty()) {
258 ParentWindowInPrimaryRootWindow(window
);
260 gfx::Display display
=
261 Shell::GetScreen()->GetDisplayMatching(bounds
);
262 aura::Window
* root
= ash::Shell::GetInstance()->display_controller()->
263 GetRootWindowForDisplayId(display
.id());
264 gfx::Point origin
= bounds
.origin();
265 wm::ConvertPointFromScreen(root
, &origin
);
266 window
->SetBounds(gfx::Rect(origin
, bounds
.size()));
267 aura::client::ParentWindowWithContext(window
, root
, bounds
);
269 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
273 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window
* window
) {
274 aura::client::ParentWindowWithContext(
275 window
, Shell::GetPrimaryRootWindow(), gfx::Rect());
278 void AshTestBase::RunAllPendingInMessageLoop() {
279 ash_test_helper_
->RunAllPendingInMessageLoop();
282 TestScreenshotDelegate
* AshTestBase::GetScreenshotDelegate() {
283 return ash_test_helper_
->test_screenshot_delegate();
286 TestSystemTrayDelegate
* AshTestBase::GetSystemTrayDelegate() {
287 return static_cast<TestSystemTrayDelegate
*>(
288 Shell::GetInstance()->system_tray_delegate());
291 void AshTestBase::SetSessionStarted(bool session_started
) {
292 ash_test_helper_
->test_shell_delegate()->test_session_state_delegate()->
293 SetActiveUserSessionStarted(session_started
);
296 void AshTestBase::SetUserLoggedIn(bool user_logged_in
) {
297 ash_test_helper_
->test_shell_delegate()->test_session_state_delegate()->
298 SetHasActiveUser(user_logged_in
);
301 void AshTestBase::SetCanLockScreen(bool can_lock_screen
) {
302 ash_test_helper_
->test_shell_delegate()->test_session_state_delegate()->
303 SetCanLockScreen(can_lock_screen
);
306 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock
) {
307 ash_test_helper_
->test_shell_delegate()->test_session_state_delegate()->
308 SetShouldLockScreenBeforeSuspending(should_lock
);
311 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running
) {
312 ash_test_helper_
->test_shell_delegate()->test_session_state_delegate()->
313 SetUserAddingScreenRunning(user_adding_screen_running
);
316 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason
) {
317 switch (block_reason
) {
318 case BLOCKED_BY_LOCK_SCREEN
:
319 SetSessionStarted(true);
320 SetUserAddingScreenRunning(false);
321 Shell::GetInstance()->session_state_delegate()->LockScreen();
323 case BLOCKED_BY_LOGIN_SCREEN
:
324 SetUserAddingScreenRunning(false);
325 SetSessionStarted(false);
327 case BLOCKED_BY_USER_ADDING_SCREEN
:
328 SetUserAddingScreenRunning(true);
329 SetSessionStarted(true);
337 void AshTestBase::UnblockUserSession() {
338 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
339 SetSessionStarted(true);
340 SetUserAddingScreenRunning(false);