Add screen space opacity to opacity tree
[chromium-blink-merge.git] / ash / test / ash_test_base.cc
blob31afda9f5148507dfb617a2ec2bfcc565b8cbf4b
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. These values
138 // prevent unexpected events from being generated during tests. Such as
139 // delayed events which create race conditions on slower tests.
140 ui::GestureConfiguration* gesture_config =
141 ui::GestureConfiguration::GetInstance();
142 gesture_config->set_max_touch_down_duration_for_click_in_ms(800);
143 gesture_config->set_long_press_time_in_ms(1000);
144 gesture_config->set_max_touch_move_in_pixels_for_click(5);
146 #if defined(OS_WIN)
147 if (!command_line->HasSwitch(ash::switches::kForceAshToDesktop)) {
148 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
149 ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread"));
150 base::Thread::Options options;
151 options.message_loop_type = base::MessageLoop::TYPE_IO;
152 ipc_thread_->StartWithOptions(options);
153 metro_viewer_host_.reset(
154 new TestMetroViewerProcessHost(ipc_thread_->task_runner()));
155 CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
156 win8::test::kDefaultTestAppUserModelId));
157 aura::RemoteWindowTreeHostWin* window_tree_host =
158 aura::RemoteWindowTreeHostWin::Instance();
159 CHECK(window_tree_host != NULL);
161 ash::WindowPositioner::SetMaximizeFirstWindow(true);
163 #endif
166 void AshTestBase::TearDown() {
167 teardown_called_ = true;
168 Shell::GetInstance()->OnAppTerminating();
169 // Flush the message loop to finish pending release tasks.
170 RunAllPendingInMessageLoop();
172 #if defined(OS_WIN)
173 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
174 !base::CommandLine::ForCurrentProcess()->HasSwitch(
175 ash::switches::kForceAshToDesktop)) {
176 // Check that our viewer connection is still established.
177 CHECK(!metro_viewer_host_->closed_unexpectedly());
179 #endif
181 ash_test_helper_->TearDown();
182 #if defined(OS_WIN)
183 ui::test::SetUsePopupAsRootWindowForTest(false);
184 // Kill the viewer process if we spun one up.
185 if (metro_viewer_host_) {
186 metro_viewer_host_->TerminateViewer();
187 metro_viewer_host_.reset();
189 #endif
191 event_generator_.reset();
192 // Some tests set an internal display id,
193 // reset it here, so other tests will continue in a clean environment.
194 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
197 ui::test::EventGenerator& AshTestBase::GetEventGenerator() {
198 if (!event_generator_) {
199 event_generator_.reset(
200 new ui::test::EventGenerator(new AshEventGeneratorDelegate()));
202 return *event_generator_.get();
205 gfx::Display::Rotation AshTestBase::GetActiveDisplayRotation(int64 id) {
206 return Shell::GetInstance()
207 ->display_manager()
208 ->GetDisplayInfo(id)
209 .GetActiveRotation();
212 gfx::Display::Rotation AshTestBase::GetCurrentInternalDisplayRotation() {
213 return GetActiveDisplayRotation(gfx::Display::InternalDisplayId());
216 bool AshTestBase::SupportsMultipleDisplays() {
217 return AshTestHelper::SupportsMultipleDisplays();
220 bool AshTestBase::SupportsHostWindowResize() {
221 return AshTestHelper::SupportsHostWindowResize();
224 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
225 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
226 DisplayManagerTestApi display_manager_test_api(display_manager);
227 display_manager_test_api.UpdateDisplay(display_specs);
228 display_manager->RunPendingTasksForTest();
231 aura::Window* AshTestBase::CurrentContext() {
232 return ash_test_helper_->CurrentContext();
235 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
236 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
239 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
240 const gfx::Rect& bounds) {
241 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
244 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
245 int id,
246 const gfx::Rect& bounds) {
247 return CreateTestWindowInShellWithDelegate(
248 new aura::test::ColorTestWindowDelegate(color), id, bounds);
251 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
252 aura::WindowDelegate* delegate,
253 int id,
254 const gfx::Rect& bounds) {
255 return CreateTestWindowInShellWithDelegateAndType(
256 delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds);
259 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
260 aura::WindowDelegate* delegate,
261 ui::wm::WindowType type,
262 int id,
263 const gfx::Rect& bounds) {
264 aura::Window* window = new aura::Window(delegate);
265 window->set_id(id);
266 window->SetType(type);
267 window->Init(ui::LAYER_TEXTURED);
268 window->Show();
270 if (bounds.IsEmpty()) {
271 ParentWindowInPrimaryRootWindow(window);
272 } else {
273 gfx::Display display =
274 Shell::GetScreen()->GetDisplayMatching(bounds);
275 aura::Window* root = ash::Shell::GetInstance()->display_controller()->
276 GetRootWindowForDisplayId(display.id());
277 gfx::Point origin = bounds.origin();
278 ::wm::ConvertPointFromScreen(root, &origin);
279 window->SetBounds(gfx::Rect(origin, bounds.size()));
280 aura::client::ParentWindowWithContext(window, root, bounds);
282 window->SetProperty(aura::client::kCanMaximizeKey, true);
283 window->SetProperty(aura::client::kCanMinimizeKey, true);
284 return window;
287 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
288 aura::client::ParentWindowWithContext(
289 window, Shell::GetPrimaryRootWindow(), gfx::Rect());
292 void AshTestBase::RunAllPendingInMessageLoop() {
293 ash_test_helper_->RunAllPendingInMessageLoop();
296 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
297 return ash_test_helper_->test_screenshot_delegate();
300 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() {
301 return static_cast<TestSystemTrayDelegate*>(
302 Shell::GetInstance()->system_tray_delegate());
305 void AshTestBase::SetSessionStarted(bool session_started) {
306 AshTestHelper::GetTestSessionStateDelegate()->SetActiveUserSessionStarted(
307 session_started);
310 void AshTestBase::SetSessionStarting() {
311 AshTestHelper::GetTestSessionStateDelegate()->set_session_state(
312 SessionStateDelegate::SESSION_STATE_ACTIVE);
315 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
316 AshTestHelper::GetTestSessionStateDelegate()->SetHasActiveUser(
317 user_logged_in);
320 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
321 AshTestHelper::GetTestSessionStateDelegate()->SetCanLockScreen(
322 can_lock_screen);
325 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
326 AshTestHelper::GetTestSessionStateDelegate()
327 ->SetShouldLockScreenBeforeSuspending(should_lock);
330 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
331 AshTestHelper::GetTestSessionStateDelegate()->SetUserAddingScreenRunning(
332 user_adding_screen_running);
335 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
336 switch (block_reason) {
337 case BLOCKED_BY_LOCK_SCREEN:
338 SetSessionStarted(true);
339 SetUserAddingScreenRunning(false);
340 Shell::GetInstance()->session_state_delegate()->LockScreen();
341 Shell::GetInstance()->OnLockStateChanged(true);
342 break;
343 case BLOCKED_BY_LOGIN_SCREEN:
344 SetUserAddingScreenRunning(false);
345 SetSessionStarted(false);
346 break;
347 case BLOCKED_BY_USER_ADDING_SCREEN:
348 SetUserAddingScreenRunning(true);
349 SetSessionStarted(true);
350 break;
351 default:
352 NOTREACHED();
353 break;
357 void AshTestBase::UnblockUserSession() {
358 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
359 SetSessionStarted(true);
360 SetUserAddingScreenRunning(false);
364 } // namespace test
365 } // namespace ash