Make the ChromeOS session name more robust w.r.t. the device type.
[chromium-blink-merge.git] / ash / test / ash_test_base.cc
blobeb573dbcea363d679409f61be0d84f5e3673a729
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"
7 #include <string>
8 #include <vector>
10 #include "ash/ash_switches.h"
11 #include "ash/display/display_controller.h"
12 #include "ash/ime/input_method_event_handler.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/test/test_system_tray_delegate.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/gesture_detection/gesture_configuration.h"
32 #include "ui/gfx/display.h"
33 #include "ui/gfx/geometry/point.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/wm/core/coordinate_conversion.h"
37 #if defined(OS_CHROMEOS)
38 #include "ash/system/chromeos/tray_display.h"
39 #endif
41 #if defined(OS_WIN)
42 #include "ash/test/test_metro_viewer_process_host.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"
49 #endif
51 #if defined(USE_X11)
52 #include "ui/gfx/x/x11_connection.h"
53 #endif
55 namespace ash {
56 namespace test {
57 namespace {
59 class AshEventGeneratorDelegate
60 : public aura::test::EventGeneratorDelegateAura {
61 public:
62 AshEventGeneratorDelegate() {}
63 ~AshEventGeneratorDelegate() override {}
65 // aura::test::EventGeneratorDelegateAura overrides:
66 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 aura::client::ScreenPositionClient* GetScreenPositionClient(
75 const aura::Window* window) const override {
76 return aura::client::GetScreenPositionClient(window->GetRootWindow());
79 private:
80 DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
83 } // namespace
85 /////////////////////////////////////////////////////////////////////////////
87 AshTestBase::AshTestBase()
88 : setup_called_(false),
89 teardown_called_(false),
90 start_session_(true) {
91 #if defined(USE_X11)
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
94 // suite.
95 gfx::InitializeThreadedX11();
96 #endif
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() {
105 CHECK(setup_called_)
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
115 // default state.
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 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
122 if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
123 command_line->AppendSwitchASCII(
124 switches::kAshHostWindowBounds, "1+1-800x600");
126 #if defined(OS_WIN)
127 ui::test::SetUsePopupAsRootWindowForTest(true);
128 #endif
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. These values
139 // prevent unexpected events from being generated during tests. Such as
140 // delayed events which create race conditions on slower tests.
141 ui::GestureConfiguration* gesture_config =
142 ui::GestureConfiguration::GetInstance();
143 gesture_config->set_max_touch_down_duration_for_click_in_ms(800);
144 gesture_config->set_long_press_time_in_ms(1000);
145 gesture_config->set_max_touch_move_in_pixels_for_click(5);
147 #if defined(OS_WIN)
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_->task_runner()));
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);
164 #endif
167 void AshTestBase::TearDown() {
168 teardown_called_ = true;
169 Shell::GetInstance()->OnAppTerminating();
170 // Flush the message loop to finish pending release tasks.
171 RunAllPendingInMessageLoop();
173 #if defined(OS_WIN)
174 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
175 !base::CommandLine::ForCurrentProcess()->HasSwitch(
176 ash::switches::kForceAshToDesktop)) {
177 // Check that our viewer connection is still established.
178 CHECK(!metro_viewer_host_->closed_unexpectedly());
180 #endif
182 ash_test_helper_->TearDown();
183 #if defined(OS_WIN)
184 ui::test::SetUsePopupAsRootWindowForTest(false);
185 // Kill the viewer process if we spun one up.
186 if (metro_viewer_host_) {
187 metro_viewer_host_->TerminateViewer();
188 metro_viewer_host_.reset();
190 #endif
192 event_generator_.reset();
193 // Some tests set an internal display id,
194 // reset it here, so other tests will continue in a clean environment.
195 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
198 ui::test::EventGenerator& AshTestBase::GetEventGenerator() {
199 if (!event_generator_) {
200 event_generator_.reset(
201 new ui::test::EventGenerator(new AshEventGeneratorDelegate()));
203 return *event_generator_.get();
206 gfx::Display::Rotation AshTestBase::GetActiveDisplayRotation(int64 id) {
207 return Shell::GetInstance()
208 ->display_manager()
209 ->GetDisplayInfo(id)
210 .GetActiveRotation();
213 gfx::Display::Rotation AshTestBase::GetCurrentInternalDisplayRotation() {
214 return GetActiveDisplayRotation(gfx::Display::InternalDisplayId());
217 bool AshTestBase::SupportsMultipleDisplays() {
218 return AshTestHelper::SupportsMultipleDisplays();
221 bool AshTestBase::SupportsHostWindowResize() {
222 return AshTestHelper::SupportsHostWindowResize();
225 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
226 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
227 DisplayManagerTestApi display_manager_test_api(display_manager);
228 display_manager_test_api.UpdateDisplay(display_specs);
229 display_manager->RunPendingTasksForTest();
232 aura::Window* AshTestBase::CurrentContext() {
233 return ash_test_helper_->CurrentContext();
236 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
237 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
240 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
241 const gfx::Rect& bounds) {
242 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
245 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
246 int id,
247 const gfx::Rect& bounds) {
248 return CreateTestWindowInShellWithDelegate(
249 new aura::test::ColorTestWindowDelegate(color), id, bounds);
252 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
253 aura::WindowDelegate* delegate,
254 int id,
255 const gfx::Rect& bounds) {
256 return CreateTestWindowInShellWithDelegateAndType(
257 delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds);
260 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
261 aura::WindowDelegate* delegate,
262 ui::wm::WindowType type,
263 int id,
264 const gfx::Rect& bounds) {
265 aura::Window* window = new aura::Window(delegate);
266 window->set_id(id);
267 window->SetType(type);
268 window->Init(ui::LAYER_TEXTURED);
269 window->Show();
271 if (bounds.IsEmpty()) {
272 ParentWindowInPrimaryRootWindow(window);
273 } else {
274 gfx::Display display =
275 Shell::GetScreen()->GetDisplayMatching(bounds);
276 aura::Window* root = ash::Shell::GetInstance()->display_controller()->
277 GetRootWindowForDisplayId(display.id());
278 gfx::Point origin = bounds.origin();
279 ::wm::ConvertPointFromScreen(root, &origin);
280 window->SetBounds(gfx::Rect(origin, bounds.size()));
281 aura::client::ParentWindowWithContext(window, root, bounds);
283 window->SetProperty(aura::client::kCanMaximizeKey, true);
284 window->SetProperty(aura::client::kCanMinimizeKey, true);
285 return window;
288 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
289 aura::client::ParentWindowWithContext(
290 window, Shell::GetPrimaryRootWindow(), gfx::Rect());
293 void AshTestBase::RunAllPendingInMessageLoop() {
294 ash_test_helper_->RunAllPendingInMessageLoop();
297 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
298 return ash_test_helper_->test_screenshot_delegate();
301 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() {
302 return static_cast<TestSystemTrayDelegate*>(
303 Shell::GetInstance()->system_tray_delegate());
306 void AshTestBase::SetSessionStarted(bool session_started) {
307 AshTestHelper::GetTestSessionStateDelegate()->SetActiveUserSessionStarted(
308 session_started);
311 void AshTestBase::SetSessionStarting() {
312 AshTestHelper::GetTestSessionStateDelegate()->set_session_state(
313 SessionStateDelegate::SESSION_STATE_ACTIVE);
316 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
317 AshTestHelper::GetTestSessionStateDelegate()->SetHasActiveUser(
318 user_logged_in);
321 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
322 AshTestHelper::GetTestSessionStateDelegate()->SetCanLockScreen(
323 can_lock_screen);
326 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
327 AshTestHelper::GetTestSessionStateDelegate()
328 ->SetShouldLockScreenBeforeSuspending(should_lock);
331 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
332 AshTestHelper::GetTestSessionStateDelegate()->SetUserAddingScreenRunning(
333 user_adding_screen_running);
336 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
337 switch (block_reason) {
338 case BLOCKED_BY_LOCK_SCREEN:
339 SetSessionStarted(true);
340 SetUserAddingScreenRunning(false);
341 Shell::GetInstance()->session_state_delegate()->LockScreen();
342 Shell::GetInstance()->OnLockStateChanged(true);
343 break;
344 case BLOCKED_BY_LOGIN_SCREEN:
345 SetUserAddingScreenRunning(false);
346 SetSessionStarted(false);
347 break;
348 case BLOCKED_BY_USER_ADDING_SCREEN:
349 SetUserAddingScreenRunning(true);
350 SetSessionStarted(true);
351 break;
352 default:
353 NOTREACHED();
354 break;
358 void AshTestBase::UnblockUserSession() {
359 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
360 SetSessionStarted(true);
361 SetUserAddingScreenRunning(false);
364 void AshTestBase::DisableIME() {
365 Shell::GetInstance()->RemovePreTargetHandler(
366 Shell::GetInstance()->display_controller()->input_method_event_handler());
369 } // namespace test
370 } // namespace ash