Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ash / test / ash_test_base.cc
blobb4ccd36bd774113c316d3d63ff17f5eb7233deda
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/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()->display_controller()->
72 GetRootWindowForDisplayId(display.id())->GetHost();
75 aura::client::ScreenPositionClient* GetScreenPositionClient(
76 const aura::Window* window) const override {
77 return aura::client::GetScreenPositionClient(window->GetRootWindow());
80 private:
81 DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
84 } // namespace
86 /////////////////////////////////////////////////////////////////////////////
88 AshTestBase::AshTestBase()
89 : setup_called_(false),
90 teardown_called_(false),
91 start_session_(true) {
92 #if defined(USE_X11)
93 // This is needed for tests which use this base class but are run in browser
94 // test binaries so don't get the default initialization in the unit test
95 // suite.
96 gfx::InitializeThreadedX11();
97 #endif
99 thread_bundle_.reset(new content::TestBrowserThreadBundle);
100 // Must initialize |ash_test_helper_| here because some tests rely on
101 // AshTestBase methods before they call AshTestBase::SetUp().
102 ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current()));
105 AshTestBase::~AshTestBase() {
106 CHECK(setup_called_)
107 << "You have overridden SetUp but never called AshTestBase::SetUp";
108 CHECK(teardown_called_)
109 << "You have overridden TearDown but never called AshTestBase::TearDown";
112 void AshTestBase::SetUp() {
113 setup_called_ = true;
115 // Clears the saved state so that test doesn't use on the wrong
116 // default state.
117 shell::ToplevelWindow::ClearSavedStateForTest();
119 // TODO(jamescook): Can we do this without changing command line?
120 // Use the origin (1,1) so that it doesn't over
121 // lap with the native mouse cursor.
122 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
123 if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
124 command_line->AppendSwitchASCII(
125 switches::kAshHostWindowBounds, "1+1-800x600");
127 #if defined(OS_WIN)
128 ui::test::SetUsePopupAsRootWindowForTest(true);
129 #endif
130 ash_test_helper_->SetUp(start_session_);
132 Shell::GetPrimaryRootWindow()->Show();
133 Shell::GetPrimaryRootWindow()->GetHost()->Show();
134 // Move the mouse cursor to far away so that native events doesn't
135 // interfere test expectations.
136 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
137 ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
139 // Changing GestureConfiguration shouldn't make tests fail. These values
140 // prevent unexpected events from being generated during tests. Such as
141 // delayed events which create race conditions on slower tests.
142 ui::GestureConfiguration* gesture_config =
143 ui::GestureConfiguration::GetInstance();
144 gesture_config->set_max_touch_down_duration_for_click_in_ms(800);
145 gesture_config->set_long_press_time_in_ms(1000);
146 gesture_config->set_max_touch_move_in_pixels_for_click(5);
148 #if defined(OS_WIN)
149 if (!command_line->HasSwitch(ash::switches::kForceAshToDesktop)) {
150 if (gfx::win::ShouldUseMetroMode()) {
151 ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread"));
152 base::Thread::Options options;
153 options.message_loop_type = base::MessageLoop::TYPE_IO;
154 ipc_thread_->StartWithOptions(options);
155 metro_viewer_host_.reset(
156 new TestMetroViewerProcessHost(ipc_thread_->task_runner()));
157 CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
158 win8::test::kDefaultTestAppUserModelId));
159 aura::RemoteWindowTreeHostWin* window_tree_host =
160 aura::RemoteWindowTreeHostWin::Instance();
161 CHECK(window_tree_host != NULL);
163 ash::WindowPositioner::SetMaximizeFirstWindow(true);
165 #endif
168 void AshTestBase::TearDown() {
169 teardown_called_ = true;
170 Shell::GetInstance()->OnAppTerminating();
171 // Flush the message loop to finish pending release tasks.
172 RunAllPendingInMessageLoop();
174 #if defined(OS_WIN)
175 if (gfx::win::ShouldUseMetroMode() &&
176 !base::CommandLine::ForCurrentProcess()->HasSwitch(
177 ash::switches::kForceAshToDesktop)) {
178 // Check that our viewer connection is still established.
179 CHECK(!metro_viewer_host_->closed_unexpectedly());
181 #endif
183 ash_test_helper_->TearDown();
184 #if defined(OS_WIN)
185 ui::test::SetUsePopupAsRootWindowForTest(false);
186 // Kill the viewer process if we spun one up.
187 if (metro_viewer_host_) {
188 metro_viewer_host_->TerminateViewer();
189 metro_viewer_host_.reset();
191 #endif
193 event_generator_.reset();
194 // Some tests set an internal display id,
195 // reset it here, so other tests will continue in a clean environment.
196 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
199 ui::test::EventGenerator& AshTestBase::GetEventGenerator() {
200 if (!event_generator_) {
201 event_generator_.reset(
202 new ui::test::EventGenerator(new AshEventGeneratorDelegate()));
204 return *event_generator_.get();
207 gfx::Display::Rotation AshTestBase::GetActiveDisplayRotation(int64 id) {
208 return Shell::GetInstance()
209 ->display_manager()
210 ->GetDisplayInfo(id)
211 .GetActiveRotation();
214 gfx::Display::Rotation AshTestBase::GetCurrentInternalDisplayRotation() {
215 return GetActiveDisplayRotation(gfx::Display::InternalDisplayId());
218 bool AshTestBase::SupportsMultipleDisplays() {
219 return AshTestHelper::SupportsMultipleDisplays();
222 bool AshTestBase::SupportsHostWindowResize() {
223 return AshTestHelper::SupportsHostWindowResize();
226 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
227 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
228 DisplayManagerTestApi display_manager_test_api(display_manager);
229 display_manager_test_api.UpdateDisplay(display_specs);
230 display_manager->RunPendingTasksForTest();
233 aura::Window* AshTestBase::CurrentContext() {
234 return ash_test_helper_->CurrentContext();
237 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
238 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
241 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
242 const gfx::Rect& bounds) {
243 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
246 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
247 int id,
248 const gfx::Rect& bounds) {
249 return CreateTestWindowInShellWithDelegate(
250 new aura::test::ColorTestWindowDelegate(color), id, bounds);
253 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
254 aura::WindowDelegate* delegate,
255 int id,
256 const gfx::Rect& bounds) {
257 return CreateTestWindowInShellWithDelegateAndType(
258 delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds);
261 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
262 aura::WindowDelegate* delegate,
263 ui::wm::WindowType type,
264 int id,
265 const gfx::Rect& bounds) {
266 aura::Window* window = new aura::Window(delegate);
267 window->set_id(id);
268 window->SetType(type);
269 window->Init(ui::LAYER_TEXTURED);
270 window->Show();
272 if (bounds.IsEmpty()) {
273 ParentWindowInPrimaryRootWindow(window);
274 } else {
275 gfx::Display display =
276 Shell::GetScreen()->GetDisplayMatching(bounds);
277 aura::Window* root = ash::Shell::GetInstance()->display_controller()->
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()->display_controller()->input_method_event_handler());
370 } // namespace test
371 } // namespace ash