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/window_positioner.h"
20 #include "base/command_line.h"
21 #include "ui/aura/client/aura_constants.h"
22 #include "ui/aura/client/screen_position_client.h"
23 #include "ui/aura/client/window_tree_client.h"
24 #include "ui/aura/test/event_generator_delegate_aura.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/aura/window_tree_host.h"
29 #include "ui/base/ime/input_method_initializer.h"
30 #include "ui/events/gesture_detection/gesture_configuration.h"
31 #include "ui/gfx/display.h"
32 #include "ui/gfx/point.h"
33 #include "ui/gfx/screen.h"
34 #include "ui/wm/core/coordinate_conversion.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/win/metro.h"
43 #include "base/win/windows_version.h"
44 #include "ui/aura/remote_window_tree_host_win.h"
45 #include "ui/aura/window_tree_host_win.h"
46 #include "ui/platform_window/win/win_window.h"
47 #include "win8/test/test_registrar_constants.h"
51 #include "ui/gfx/x/x11_connection.h"
58 class AshEventGeneratorDelegate
59 : public aura::test::EventGeneratorDelegateAura
{
61 AshEventGeneratorDelegate() {}
62 ~AshEventGeneratorDelegate() override
{}
64 // aura::test::EventGeneratorDelegateAura overrides:
65 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 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 /////////////////////////////////////////////////////////////////////////////
86 AshTestBase::AshTestBase()
87 : setup_called_(false),
88 teardown_called_(false),
89 start_session_(true) {
91 // This is needed for tests which use this base class but are run in browser
92 // test binaries so don't get the default initialization in the unit test
94 gfx::InitializeThreadedX11();
97 thread_bundle_
.reset(new content::TestBrowserThreadBundle
);
98 // Must initialize |ash_test_helper_| here because some tests rely on
99 // AshTestBase methods before they call AshTestBase::SetUp().
100 ash_test_helper_
.reset(new AshTestHelper(base::MessageLoopForUI::current()));
103 AshTestBase::~AshTestBase() {
105 << "You have overridden SetUp but never called AshTestBase::SetUp";
106 CHECK(teardown_called_
)
107 << "You have overridden TearDown but never called AshTestBase::TearDown";
110 void AshTestBase::SetUp() {
111 setup_called_
= true;
113 // Clears the saved state so that test doesn't use on the wrong
115 shell::ToplevelWindow::ClearSavedStateForTest();
117 // TODO(jamescook): Can we do this without changing command line?
118 // Use the origin (1,1) so that it doesn't over
119 // lap with the native mouse cursor.
120 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
121 if (!command_line
->HasSwitch(switches::kAshHostWindowBounds
)) {
122 command_line
->AppendSwitchASCII(
123 switches::kAshHostWindowBounds
, "1+1-800x600");
126 ui::test::SetUsePopupAsRootWindowForTest(true);
128 ash_test_helper_
->SetUp(start_session_
);
130 Shell::GetPrimaryRootWindow()->Show();
131 Shell::GetPrimaryRootWindow()->GetHost()->Show();
132 // Move the mouse cursor to far away so that native events doesn't
133 // interfere test expectations.
134 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
135 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
137 // Changing GestureConfiguration shouldn't make tests fail.
138 ui::GestureConfiguration::GetInstance()
139 ->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 if (metro_viewer_host_
) {
180 metro_viewer_host_
->TerminateViewer();
181 metro_viewer_host_
.reset();
185 event_generator_
.reset();
186 // Some tests set an internal display id,
187 // reset it here, so other tests will continue in a clean environment.
188 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID
);
191 ui::test::EventGenerator
& AshTestBase::GetEventGenerator() {
192 if (!event_generator_
) {
193 event_generator_
.reset(
194 new ui::test::EventGenerator(new AshEventGeneratorDelegate()));
196 return *event_generator_
.get();
199 bool AshTestBase::SupportsMultipleDisplays() {
200 return AshTestHelper::SupportsMultipleDisplays();
203 bool AshTestBase::SupportsHostWindowResize() {
204 return AshTestHelper::SupportsHostWindowResize();
207 void AshTestBase::UpdateDisplay(const std::string
& display_specs
) {
208 DisplayManagerTestApi
display_manager_test_api(
209 Shell::GetInstance()->display_manager());
210 display_manager_test_api
.UpdateDisplay(display_specs
);
213 aura::Window
* AshTestBase::CurrentContext() {
214 return ash_test_helper_
->CurrentContext();
217 aura::Window
* AshTestBase::CreateTestWindowInShellWithId(int id
) {
218 return CreateTestWindowInShellWithDelegate(NULL
, id
, gfx::Rect());
221 aura::Window
* AshTestBase::CreateTestWindowInShellWithBounds(
222 const gfx::Rect
& bounds
) {
223 return CreateTestWindowInShellWithDelegate(NULL
, 0, bounds
);
226 aura::Window
* AshTestBase::CreateTestWindowInShell(SkColor color
,
228 const gfx::Rect
& bounds
) {
229 return CreateTestWindowInShellWithDelegate(
230 new aura::test::ColorTestWindowDelegate(color
), id
, bounds
);
233 aura::Window
* AshTestBase::CreateTestWindowInShellWithDelegate(
234 aura::WindowDelegate
* delegate
,
236 const gfx::Rect
& bounds
) {
237 return CreateTestWindowInShellWithDelegateAndType(
238 delegate
, ui::wm::WINDOW_TYPE_NORMAL
, id
, bounds
);
241 aura::Window
* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
242 aura::WindowDelegate
* delegate
,
243 ui::wm::WindowType type
,
245 const gfx::Rect
& bounds
) {
246 aura::Window
* window
= new aura::Window(delegate
);
248 window
->SetType(type
);
249 window
->Init(aura::WINDOW_LAYER_TEXTURED
);
252 if (bounds
.IsEmpty()) {
253 ParentWindowInPrimaryRootWindow(window
);
255 gfx::Display display
=
256 Shell::GetScreen()->GetDisplayMatching(bounds
);
257 aura::Window
* 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 aura::client::ParentWindowWithContext(window
, root
, bounds
);
264 window
->SetProperty(aura::client::kCanMaximizeKey
, true);
265 window
->SetProperty(aura::client::kCanMinimizeKey
, true);
269 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window
* window
) {
270 aura::client::ParentWindowWithContext(
271 window
, Shell::GetPrimaryRootWindow(), gfx::Rect());
274 void AshTestBase::RunAllPendingInMessageLoop() {
275 ash_test_helper_
->RunAllPendingInMessageLoop();
278 TestScreenshotDelegate
* AshTestBase::GetScreenshotDelegate() {
279 return ash_test_helper_
->test_screenshot_delegate();
282 TestSystemTrayDelegate
* AshTestBase::GetSystemTrayDelegate() {
283 return static_cast<TestSystemTrayDelegate
*>(
284 Shell::GetInstance()->system_tray_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);