Mark //testing/perf target testonly.
[chromium-blink-merge.git] / ash / test / ash_test_base.cc
blobd202b1cb7fa1125ed6d2b30e19adf57c4ef77839
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/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/geometry/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"
38 #endif
40 #if defined(OS_WIN)
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"
48 #endif
50 #if defined(USE_X11)
51 #include "ui/gfx/x/x11_connection.h"
52 #endif
54 namespace ash {
55 namespace test {
56 namespace {
58 class AshEventGeneratorDelegate
59 : public aura::test::EventGeneratorDelegateAura {
60 public:
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());
78 private:
79 DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
82 } // namespace
84 /////////////////////////////////////////////////////////////////////////////
86 AshTestBase::AshTestBase()
87 : setup_called_(false),
88 teardown_called_(false),
89 start_session_(true) {
90 #if defined(USE_X11)
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
93 // suite.
94 gfx::InitializeThreadedX11();
95 #endif
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() {
104 CHECK(setup_called_)
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
114 // default state.
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 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
121 if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
122 command_line->AppendSwitchASCII(
123 switches::kAshHostWindowBounds, "1+1-800x600");
125 #if defined(OS_WIN)
126 ui::test::SetUsePopupAsRootWindowForTest(true);
127 #endif
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);
141 #if defined(OS_WIN)
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);
158 #endif
161 void AshTestBase::TearDown() {
162 teardown_called_ = true;
163 Shell::GetInstance()->OnAppTerminating();
164 // Flush the message loop to finish pending release tasks.
165 RunAllPendingInMessageLoop();
167 #if defined(OS_WIN)
168 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
169 !base::CommandLine::ForCurrentProcess()->HasSwitch(
170 ash::switches::kForceAshToDesktop)) {
171 // Check that our viewer connection is still established.
172 CHECK(!metro_viewer_host_->closed_unexpectedly());
174 #endif
176 ash_test_helper_->TearDown();
177 #if defined(OS_WIN)
178 ui::test::SetUsePopupAsRootWindowForTest(false);
179 // Kill the viewer process if we spun one up.
180 if (metro_viewer_host_) {
181 metro_viewer_host_->TerminateViewer();
182 metro_viewer_host_.reset();
184 #endif
186 event_generator_.reset();
187 // Some tests set an internal display id,
188 // reset it here, so other tests will continue in a clean environment.
189 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
192 ui::test::EventGenerator& AshTestBase::GetEventGenerator() {
193 if (!event_generator_) {
194 event_generator_.reset(
195 new ui::test::EventGenerator(new AshEventGeneratorDelegate()));
197 return *event_generator_.get();
200 bool AshTestBase::SupportsMultipleDisplays() {
201 return AshTestHelper::SupportsMultipleDisplays();
204 bool AshTestBase::SupportsHostWindowResize() {
205 return AshTestHelper::SupportsHostWindowResize();
208 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
209 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
210 DisplayManagerTestApi display_manager_test_api(display_manager);
211 display_manager_test_api.UpdateDisplay(display_specs);
212 if (display_manager->HasSoftwareMirroringDisplay())
213 RunAllPendingInMessageLoop();
216 aura::Window* AshTestBase::CurrentContext() {
217 return ash_test_helper_->CurrentContext();
220 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
221 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
224 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
225 const gfx::Rect& bounds) {
226 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
229 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
230 int id,
231 const gfx::Rect& bounds) {
232 return CreateTestWindowInShellWithDelegate(
233 new aura::test::ColorTestWindowDelegate(color), id, bounds);
236 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
237 aura::WindowDelegate* delegate,
238 int id,
239 const gfx::Rect& bounds) {
240 return CreateTestWindowInShellWithDelegateAndType(
241 delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds);
244 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
245 aura::WindowDelegate* delegate,
246 ui::wm::WindowType type,
247 int id,
248 const gfx::Rect& bounds) {
249 aura::Window* window = new aura::Window(delegate);
250 window->set_id(id);
251 window->SetType(type);
252 window->Init(aura::WINDOW_LAYER_TEXTURED);
253 window->Show();
255 if (bounds.IsEmpty()) {
256 ParentWindowInPrimaryRootWindow(window);
257 } else {
258 gfx::Display display =
259 Shell::GetScreen()->GetDisplayMatching(bounds);
260 aura::Window* root = ash::Shell::GetInstance()->display_controller()->
261 GetRootWindowForDisplayId(display.id());
262 gfx::Point origin = bounds.origin();
263 ::wm::ConvertPointFromScreen(root, &origin);
264 window->SetBounds(gfx::Rect(origin, bounds.size()));
265 aura::client::ParentWindowWithContext(window, root, bounds);
267 window->SetProperty(aura::client::kCanMaximizeKey, true);
268 window->SetProperty(aura::client::kCanMinimizeKey, true);
269 return window;
272 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
273 aura::client::ParentWindowWithContext(
274 window, Shell::GetPrimaryRootWindow(), gfx::Rect());
277 void AshTestBase::RunAllPendingInMessageLoop() {
278 ash_test_helper_->RunAllPendingInMessageLoop();
281 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
282 return ash_test_helper_->test_screenshot_delegate();
285 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() {
286 return static_cast<TestSystemTrayDelegate*>(
287 Shell::GetInstance()->system_tray_delegate());
290 void AshTestBase::SetSessionStarted(bool session_started) {
291 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
292 SetActiveUserSessionStarted(session_started);
295 void AshTestBase::SetSessionStarting() {
296 ash_test_helper_->test_shell_delegate()
297 ->test_session_state_delegate()
298 ->set_session_state(SessionStateDelegate::SESSION_STATE_ACTIVE);
301 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
302 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
303 SetHasActiveUser(user_logged_in);
306 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
307 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
308 SetCanLockScreen(can_lock_screen);
311 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
312 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
313 SetShouldLockScreenBeforeSuspending(should_lock);
316 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
317 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
318 SetUserAddingScreenRunning(user_adding_screen_running);
321 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
322 switch (block_reason) {
323 case BLOCKED_BY_LOCK_SCREEN:
324 SetSessionStarted(true);
325 SetUserAddingScreenRunning(false);
326 Shell::GetInstance()->session_state_delegate()->LockScreen();
327 Shell::GetInstance()->OnLockStateChanged(true);
328 break;
329 case BLOCKED_BY_LOGIN_SCREEN:
330 SetUserAddingScreenRunning(false);
331 SetSessionStarted(false);
332 break;
333 case BLOCKED_BY_USER_ADDING_SCREEN:
334 SetUserAddingScreenRunning(true);
335 SetSessionStarted(true);
336 break;
337 default:
338 NOTREACHED();
339 break;
343 void AshTestBase::UnblockUserSession() {
344 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
345 SetSessionStarted(true);
346 SetUserAddingScreenRunning(false);
350 } // namespace test
351 } // namespace ash