Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / test / ash_test_base.cc
blobdc0e36e44dbb9e6e87ad8c7f1fe548cd37c8de76
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/window_tree_host_manager.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/gfx/win/metro_mode.h"
48 #include "ui/platform_window/win/win_window.h"
49 #include "win8/test/test_registrar_constants.h"
50 #endif
52 #if defined(USE_X11)
53 #include "ui/gfx/x/x11_connection.h"
54 #endif
56 namespace ash {
57 namespace test {
58 namespace {
60 class AshEventGeneratorDelegate
61 : public aura::test::EventGeneratorDelegateAura {
62 public:
63 AshEventGeneratorDelegate() {}
64 ~AshEventGeneratorDelegate() override {}
66 // aura::test::EventGeneratorDelegateAura overrides:
67 aura::WindowTreeHost* GetHostAt(
68 const gfx::Point& point_in_screen) const override {
69 gfx::Screen* screen = Shell::GetScreen();
70 gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
71 return Shell::GetInstance()
72 ->window_tree_host_manager()
73 ->GetRootWindowForDisplayId(display.id())
74 ->GetHost();
77 aura::client::ScreenPositionClient* GetScreenPositionClient(
78 const aura::Window* window) const override {
79 return aura::client::GetScreenPositionClient(window->GetRootWindow());
82 private:
83 DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
86 } // namespace
88 /////////////////////////////////////////////////////////////////////////////
90 AshTestBase::AshTestBase()
91 : setup_called_(false),
92 teardown_called_(false),
93 start_session_(true) {
94 #if defined(USE_X11)
95 // This is needed for tests which use this base class but are run in browser
96 // test binaries so don't get the default initialization in the unit test
97 // suite.
98 gfx::InitializeThreadedX11();
99 #endif
101 thread_bundle_.reset(new content::TestBrowserThreadBundle);
102 // Must initialize |ash_test_helper_| here because some tests rely on
103 // AshTestBase methods before they call AshTestBase::SetUp().
104 ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current()));
107 AshTestBase::~AshTestBase() {
108 CHECK(setup_called_)
109 << "You have overridden SetUp but never called AshTestBase::SetUp";
110 CHECK(teardown_called_)
111 << "You have overridden TearDown but never called AshTestBase::TearDown";
114 void AshTestBase::SetUp() {
115 setup_called_ = true;
117 // Clears the saved state so that test doesn't use on the wrong
118 // default state.
119 shell::ToplevelWindow::ClearSavedStateForTest();
121 // TODO(jamescook): Can we do this without changing command line?
122 // Use the origin (1,1) so that it doesn't over
123 // lap with the native mouse cursor.
124 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
125 if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
126 command_line->AppendSwitchASCII(
127 switches::kAshHostWindowBounds, "1+1-800x600");
129 #if defined(OS_WIN)
130 ui::test::SetUsePopupAsRootWindowForTest(true);
131 #endif
132 ash_test_helper_->SetUp(start_session_);
134 Shell::GetPrimaryRootWindow()->Show();
135 Shell::GetPrimaryRootWindow()->GetHost()->Show();
136 // Move the mouse cursor to far away so that native events doesn't
137 // interfere test expectations.
138 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
139 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
141 // Changing GestureConfiguration shouldn't make tests fail. These values
142 // prevent unexpected events from being generated during tests. Such as
143 // delayed events which create race conditions on slower tests.
144 ui::GestureConfiguration* gesture_config =
145 ui::GestureConfiguration::GetInstance();
146 gesture_config->set_max_touch_down_duration_for_click_in_ms(800);
147 gesture_config->set_long_press_time_in_ms(1000);
148 gesture_config->set_max_touch_move_in_pixels_for_click(5);
150 #if defined(OS_WIN)
151 if (!command_line->HasSwitch(ash::switches::kForceAshToDesktop)) {
152 if (gfx::win::ShouldUseMetroMode()) {
153 ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread"));
154 base::Thread::Options options;
155 options.message_loop_type = base::MessageLoop::TYPE_IO;
156 ipc_thread_->StartWithOptions(options);
157 metro_viewer_host_.reset(
158 new TestMetroViewerProcessHost(ipc_thread_->task_runner()));
159 CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
160 win8::test::kDefaultTestAppUserModelId));
161 aura::RemoteWindowTreeHostWin* window_tree_host =
162 aura::RemoteWindowTreeHostWin::Instance();
163 CHECK(window_tree_host != NULL);
165 ash::WindowPositioner::SetMaximizeFirstWindow(true);
167 #endif
170 void AshTestBase::TearDown() {
171 teardown_called_ = true;
172 Shell::GetInstance()->OnAppTerminating();
173 // Flush the message loop to finish pending release tasks.
174 RunAllPendingInMessageLoop();
176 #if defined(OS_WIN)
177 if (gfx::win::ShouldUseMetroMode() &&
178 !base::CommandLine::ForCurrentProcess()->HasSwitch(
179 ash::switches::kForceAshToDesktop)) {
180 // Check that our viewer connection is still established.
181 CHECK(!metro_viewer_host_->closed_unexpectedly());
183 #endif
185 ash_test_helper_->TearDown();
186 #if defined(OS_WIN)
187 ui::test::SetUsePopupAsRootWindowForTest(false);
188 // Kill the viewer process if we spun one up.
189 if (metro_viewer_host_) {
190 metro_viewer_host_->TerminateViewer();
191 metro_viewer_host_.reset();
193 #endif
195 event_generator_.reset();
196 // Some tests set an internal display id,
197 // reset it here, so other tests will continue in a clean environment.
198 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
201 ui::test::EventGenerator& AshTestBase::GetEventGenerator() {
202 if (!event_generator_) {
203 event_generator_.reset(
204 new ui::test::EventGenerator(new AshEventGeneratorDelegate()));
206 return *event_generator_.get();
209 gfx::Display::Rotation AshTestBase::GetActiveDisplayRotation(int64 id) {
210 return Shell::GetInstance()
211 ->display_manager()
212 ->GetDisplayInfo(id)
213 .GetActiveRotation();
216 gfx::Display::Rotation AshTestBase::GetCurrentInternalDisplayRotation() {
217 return GetActiveDisplayRotation(gfx::Display::InternalDisplayId());
220 bool AshTestBase::SupportsMultipleDisplays() {
221 return AshTestHelper::SupportsMultipleDisplays();
224 bool AshTestBase::SupportsHostWindowResize() {
225 return AshTestHelper::SupportsHostWindowResize();
228 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
229 DisplayManagerTestApi().UpdateDisplay(display_specs);
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()
277 ->window_tree_host_manager()
278 ->GetRootWindowForDisplayId(display.id());
279 gfx::Point origin = bounds.origin();
280 ::wm::ConvertPointFromScreen(root, &origin);
281 window->SetBounds(gfx::Rect(origin, bounds.size()));
282 aura::client::ParentWindowWithContext(window, root, bounds);
284 window->SetProperty(aura::client::kCanMaximizeKey, true);
285 window->SetProperty(aura::client::kCanMinimizeKey, true);
286 return window;
289 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
290 aura::client::ParentWindowWithContext(
291 window, Shell::GetPrimaryRootWindow(), gfx::Rect());
294 void AshTestBase::RunAllPendingInMessageLoop() {
295 ash_test_helper_->RunAllPendingInMessageLoop();
298 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
299 return ash_test_helper_->test_screenshot_delegate();
302 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() {
303 return static_cast<TestSystemTrayDelegate*>(
304 Shell::GetInstance()->system_tray_delegate());
307 void AshTestBase::SetSessionStarted(bool session_started) {
308 AshTestHelper::GetTestSessionStateDelegate()->SetActiveUserSessionStarted(
309 session_started);
312 void AshTestBase::SetSessionStarting() {
313 AshTestHelper::GetTestSessionStateDelegate()->set_session_state(
314 SessionStateDelegate::SESSION_STATE_ACTIVE);
317 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
318 AshTestHelper::GetTestSessionStateDelegate()->SetHasActiveUser(
319 user_logged_in);
322 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
323 AshTestHelper::GetTestSessionStateDelegate()->SetCanLockScreen(
324 can_lock_screen);
327 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
328 AshTestHelper::GetTestSessionStateDelegate()
329 ->SetShouldLockScreenBeforeSuspending(should_lock);
332 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
333 AshTestHelper::GetTestSessionStateDelegate()->SetUserAddingScreenRunning(
334 user_adding_screen_running);
337 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
338 switch (block_reason) {
339 case BLOCKED_BY_LOCK_SCREEN:
340 SetSessionStarted(true);
341 SetUserAddingScreenRunning(false);
342 Shell::GetInstance()->session_state_delegate()->LockScreen();
343 Shell::GetInstance()->OnLockStateChanged(true);
344 break;
345 case BLOCKED_BY_LOGIN_SCREEN:
346 SetUserAddingScreenRunning(false);
347 SetSessionStarted(false);
348 break;
349 case BLOCKED_BY_USER_ADDING_SCREEN:
350 SetUserAddingScreenRunning(true);
351 SetSessionStarted(true);
352 break;
353 default:
354 NOTREACHED();
355 break;
359 void AshTestBase::UnblockUserSession() {
360 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
361 SetSessionStarted(true);
362 SetUserAddingScreenRunning(false);
365 void AshTestBase::DisableIME() {
366 Shell::GetInstance()->RemovePreTargetHandler(
367 Shell::GetInstance()
368 ->window_tree_host_manager()
369 ->input_method_event_handler());
372 } // namespace test
373 } // namespace ash