Utilize the window.speechSynthesis.onvoiceschanged event in ChromeVox.
[chromium-blink-merge.git] / ash / test / ash_test_base.cc
blob70db370c69f7d85f5da806f181ea030fa9ceaaa2
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/coordinate_conversion.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/gestures/gesture_configuration.h"
32 #include "ui/gfx/display.h"
33 #include "ui/gfx/point.h"
34 #include "ui/gfx/screen.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/test/test_process_killer_win.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 virtual ~AshEventGeneratorDelegate() {}
65 // aura::test::EventGeneratorDelegateAura overrides:
66 virtual 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 virtual 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 CommandLine* command_line = 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.
139 ui::GestureConfiguration::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 // Flush the message loop to finish pending release tasks.
164 RunAllPendingInMessageLoop();
166 #if defined(OS_WIN)
167 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
168 !CommandLine::ForCurrentProcess()->HasSwitch(
169 ash::switches::kForceAshToDesktop)) {
170 // Check that our viewer connection is still established.
171 CHECK(!metro_viewer_host_->closed_unexpectedly());
173 #endif
175 ash_test_helper_->TearDown();
176 #if defined(OS_WIN)
177 ui::test::SetUsePopupAsRootWindowForTest(false);
178 // Kill the viewer process if we spun one up.
179 metro_viewer_host_.reset();
181 // Clean up any dangling viewer processes as the metro APIs sometimes leave
182 // zombies behind. A default browser process in metro will have the
183 // following command line arg so use that to avoid killing all processes named
184 // win8::test::kDefaultTestExePath.
185 const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer";
186 base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath,
187 kViewerProcessArgument);
188 #endif
190 event_generator_.reset();
191 // Some tests set an internal display id,
192 // reset it here, so other tests will continue in a clean environment.
193 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
196 ui::test::EventGenerator& AshTestBase::GetEventGenerator() {
197 if (!event_generator_) {
198 event_generator_.reset(
199 new ui::test::EventGenerator(new AshEventGeneratorDelegate()));
201 return *event_generator_.get();
204 bool AshTestBase::SupportsMultipleDisplays() {
205 return AshTestHelper::SupportsMultipleDisplays();
208 bool AshTestBase::SupportsHostWindowResize() {
209 return AshTestHelper::SupportsHostWindowResize();
212 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
213 DisplayManagerTestApi display_manager_test_api(
214 Shell::GetInstance()->display_manager());
215 display_manager_test_api.UpdateDisplay(display_specs);
218 aura::Window* AshTestBase::CurrentContext() {
219 return ash_test_helper_->CurrentContext();
222 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
223 return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
226 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
227 const gfx::Rect& bounds) {
228 return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
231 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
232 int id,
233 const gfx::Rect& bounds) {
234 return CreateTestWindowInShellWithDelegate(
235 new aura::test::ColorTestWindowDelegate(color), id, bounds);
238 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
239 aura::WindowDelegate* delegate,
240 int id,
241 const gfx::Rect& bounds) {
242 return CreateTestWindowInShellWithDelegateAndType(
243 delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds);
246 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
247 aura::WindowDelegate* delegate,
248 ui::wm::WindowType type,
249 int id,
250 const gfx::Rect& bounds) {
251 aura::Window* window = new aura::Window(delegate);
252 window->set_id(id);
253 window->SetType(type);
254 window->Init(aura::WINDOW_LAYER_TEXTURED);
255 window->Show();
257 if (bounds.IsEmpty()) {
258 ParentWindowInPrimaryRootWindow(window);
259 } else {
260 gfx::Display display =
261 Shell::GetScreen()->GetDisplayMatching(bounds);
262 aura::Window* root = ash::Shell::GetInstance()->display_controller()->
263 GetRootWindowForDisplayId(display.id());
264 gfx::Point origin = bounds.origin();
265 wm::ConvertPointFromScreen(root, &origin);
266 window->SetBounds(gfx::Rect(origin, bounds.size()));
267 aura::client::ParentWindowWithContext(window, root, bounds);
269 window->SetProperty(aura::client::kCanMaximizeKey, true);
270 return window;
273 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
274 aura::client::ParentWindowWithContext(
275 window, Shell::GetPrimaryRootWindow(), gfx::Rect());
278 void AshTestBase::RunAllPendingInMessageLoop() {
279 ash_test_helper_->RunAllPendingInMessageLoop();
282 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
283 return ash_test_helper_->test_screenshot_delegate();
286 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() {
287 return static_cast<TestSystemTrayDelegate*>(
288 Shell::GetInstance()->system_tray_delegate());
291 void AshTestBase::SetSessionStarted(bool session_started) {
292 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
293 SetActiveUserSessionStarted(session_started);
296 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
297 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
298 SetHasActiveUser(user_logged_in);
301 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
302 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
303 SetCanLockScreen(can_lock_screen);
306 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
307 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
308 SetShouldLockScreenBeforeSuspending(should_lock);
311 void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
312 ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
313 SetUserAddingScreenRunning(user_adding_screen_running);
316 void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
317 switch (block_reason) {
318 case BLOCKED_BY_LOCK_SCREEN:
319 SetSessionStarted(true);
320 SetUserAddingScreenRunning(false);
321 Shell::GetInstance()->session_state_delegate()->LockScreen();
322 break;
323 case BLOCKED_BY_LOGIN_SCREEN:
324 SetUserAddingScreenRunning(false);
325 SetSessionStarted(false);
326 break;
327 case BLOCKED_BY_USER_ADDING_SCREEN:
328 SetUserAddingScreenRunning(true);
329 SetSessionStarted(true);
330 break;
331 default:
332 NOTREACHED();
333 break;
337 void AshTestBase::UnblockUserSession() {
338 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
339 SetSessionStarted(true);
340 SetUserAddingScreenRunning(false);
344 } // namespace test
345 } // namespace ash