ash_test_base.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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.
4
5#include "ash/test/ash_test_base.h"
6
7#include <string>
8#include <vector>
9
10#include "ash/ash_switches.h"
11#include "ash/display/display_controller.h"
12#include "ash/screen_ash.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/wm/coordinate_conversion.h"
20#include "ash/wm/window_positioner.h"
21#include "base/command_line.h"
22#include "content/public/test/web_contents_tester.h"
23#include "ui/aura/client/aura_constants.h"
24#include "ui/aura/client/screen_position_client.h"
25#include "ui/aura/client/window_tree_client.h"
26#include "ui/aura/root_window.h"
27#include "ui/aura/test/event_generator.h"
28#include "ui/aura/test/test_window_delegate.h"
29#include "ui/aura/window.h"
30#include "ui/aura/window_delegate.h"
31#include "ui/base/ime/input_method_initializer.h"
32#include "ui/gfx/display.h"
33#include "ui/gfx/point.h"
34#include "ui/gfx/screen.h"
35
36#if defined(OS_CHROMEOS)
37#include "ash/system/chromeos/tray_display.h"
38#endif
39
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_root_window_host_win.h"
46#include "ui/aura/root_window_host_win.h"
47#include "win8/test/test_registrar_constants.h"
48#endif
49
50namespace ash {
51namespace test {
52namespace {
53
54class AshEventGeneratorDelegate : public aura::test::EventGeneratorDelegate {
55 public:
56  AshEventGeneratorDelegate() {}
57  virtual ~AshEventGeneratorDelegate() {}
58
59  // aura::test::EventGeneratorDelegate overrides:
60  virtual aura::RootWindow* GetRootWindowAt(
61      const gfx::Point& point_in_screen) const OVERRIDE {
62    gfx::Screen* screen = Shell::GetScreen();
63    gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
64    return Shell::GetInstance()->display_controller()->
65        GetRootWindowForDisplayId(display.id())->GetDispatcher();
66  }
67
68  virtual aura::client::ScreenPositionClient* GetScreenPositionClient(
69      const aura::Window* window) const OVERRIDE {
70    return aura::client::GetScreenPositionClient(window->GetRootWindow());
71  }
72
73 private:
74  DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
75};
76
77}  // namespace
78
79content::WebContents* AshTestViewsDelegate::CreateWebContents(
80    content::BrowserContext* browser_context,
81    content::SiteInstance* site_instance) {
82  return content::WebContentsTester::CreateTestWebContents(browser_context,
83                                                           site_instance);
84}
85
86/////////////////////////////////////////////////////////////////////////////
87
88AshTestBase::AshTestBase()
89    : setup_called_(false),
90      teardown_called_(false),
91      start_session_(true) {
92  // Must initialize |ash_test_helper_| here because some tests rely on
93  // AshTestBase methods before they call AshTestBase::SetUp().
94  ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current()));
95}
96
97AshTestBase::~AshTestBase() {
98  CHECK(setup_called_)
99      << "You have overridden SetUp but never called AshTestBase::SetUp";
100  CHECK(teardown_called_)
101      << "You have overridden TearDown but never called AshTestBase::TearDown";
102}
103
104void AshTestBase::SetUp() {
105  setup_called_ = true;
106
107  // Clears the saved state so that test doesn't use on the wrong
108  // default state.
109  shell::ToplevelWindow::ClearSavedStateForTest();
110
111  // TODO(jamescook): Can we do this without changing command line?
112  // Use the origin (1,1) so that it doesn't over
113  // lap with the native mouse cursor.
114  CommandLine* command_line = CommandLine::ForCurrentProcess();
115  if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
116    command_line->AppendSwitchASCII(
117        switches::kAshHostWindowBounds, "1+1-800x600");
118  }
119#if defined(OS_WIN)
120  aura::test::SetUsePopupAsRootWindowForTest(true);
121#endif
122  ash_test_helper_->SetUp(start_session_);
123
124  Shell::GetPrimaryRootWindow()->Show();
125  Shell::GetPrimaryRootWindow()->GetDispatcher()->host()->Show();
126  // Move the mouse cursor to far away so that native events doesn't
127  // interfere test expectations.
128  Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
129  ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
130
131#if defined(OS_WIN)
132  if (!command_line->HasSwitch(ash::switches::kForceAshToDesktop)) {
133    if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
134      ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread"));
135      base::Thread::Options options;
136      options.message_loop_type = base::MessageLoop::TYPE_IO;
137      ipc_thread_->StartWithOptions(options);
138      metro_viewer_host_.reset(
139          new TestMetroViewerProcessHost(ipc_thread_->message_loop_proxy()));
140      CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
141          win8::test::kDefaultTestAppUserModelId));
142      aura::RemoteRootWindowHostWin* root_window_host =
143          aura::RemoteRootWindowHostWin::Instance();
144      CHECK(root_window_host != NULL);
145    }
146    ash::WindowPositioner::SetMaximizeFirstWindow(true);
147  }
148#endif
149}
150
151void AshTestBase::TearDown() {
152  teardown_called_ = true;
153  // Flush the message loop to finish pending release tasks.
154  RunAllPendingInMessageLoop();
155
156#if defined(OS_WIN)
157  if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
158      !CommandLine::ForCurrentProcess()->HasSwitch(
159          ash::switches::kForceAshToDesktop)) {
160    // Check that our viewer connection is still established.
161    CHECK(!metro_viewer_host_->closed_unexpectedly());
162  }
163#endif
164
165  ash_test_helper_->TearDown();
166#if defined(OS_WIN)
167  aura::test::SetUsePopupAsRootWindowForTest(false);
168  // Kill the viewer process if we spun one up.
169  metro_viewer_host_.reset();
170
171  // Clean up any dangling viewer processes as the metro APIs sometimes leave
172  // zombies behind. A default browser process in metro will have the
173  // following command line arg so use that to avoid killing all processes named
174  // win8::test::kDefaultTestExePath.
175  const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer";
176  base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath,
177                                          kViewerProcessArgument);
178#endif
179
180  event_generator_.reset();
181  // Some tests set an internal display id,
182  // reset it here, so other tests will continue in a clean environment.
183  gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
184}
185
186aura::test::EventGenerator& AshTestBase::GetEventGenerator() {
187  if (!event_generator_) {
188    event_generator_.reset(
189        new aura::test::EventGenerator(new AshEventGeneratorDelegate()));
190  }
191  return *event_generator_.get();
192}
193
194// static
195bool AshTestBase::SupportsMultipleDisplays() {
196#if defined(OS_WIN)
197  return base::win::GetVersion() < base::win::VERSION_WIN8;
198#else
199  return true;
200#endif
201}
202
203// static
204bool AshTestBase::SupportsHostWindowResize() {
205#if defined(OS_WIN)
206  return base::win::GetVersion() < base::win::VERSION_WIN8;
207#else
208  return true;
209#endif
210}
211
212void 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);
216}
217
218aura::Window* AshTestBase::CurrentContext() {
219  return ash_test_helper_->CurrentContext();
220}
221
222aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
223  return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
224}
225
226aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
227    const gfx::Rect& bounds) {
228  return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
229}
230
231aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
232                                                   int id,
233                                                   const gfx::Rect& bounds) {
234  return CreateTestWindowInShellWithDelegate(
235      new aura::test::ColorTestWindowDelegate(color), id, bounds);
236}
237
238aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
239    aura::WindowDelegate* delegate,
240    int id,
241    const gfx::Rect& bounds) {
242  return CreateTestWindowInShellWithDelegateAndType(
243      delegate,
244      aura::client::WINDOW_TYPE_NORMAL,
245      id,
246      bounds);
247}
248
249aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
250    aura::WindowDelegate* delegate,
251    aura::client::WindowType type,
252    int id,
253    const gfx::Rect& bounds) {
254  aura::Window* window = new aura::Window(delegate);
255  window->set_id(id);
256  window->SetType(type);
257  window->Init(ui::LAYER_TEXTURED);
258  window->Show();
259
260  if (bounds.IsEmpty()) {
261    ParentWindowInPrimaryRootWindow(window);
262  } else {
263    gfx::Display display =
264        Shell::GetScreen()->GetDisplayMatching(bounds);
265    aura::Window* root = ash::Shell::GetInstance()->display_controller()->
266        GetRootWindowForDisplayId(display.id());
267    gfx::Point origin = bounds.origin();
268    wm::ConvertPointFromScreen(root, &origin);
269    window->SetBounds(gfx::Rect(origin, bounds.size()));
270    aura::client::ParentWindowWithContext(window, root, bounds);
271  }
272  window->SetProperty(aura::client::kCanMaximizeKey, true);
273  return window;
274}
275
276void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
277  aura::client::ParentWindowWithContext(
278      window, Shell::GetPrimaryRootWindow(), gfx::Rect());
279}
280
281void AshTestBase::RunAllPendingInMessageLoop() {
282  ash_test_helper_->RunAllPendingInMessageLoop();
283}
284
285TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
286  return ash_test_helper_->test_screenshot_delegate();
287}
288
289void AshTestBase::SetSessionStarted(bool session_started) {
290  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
291      SetActiveUserSessionStarted(session_started);
292}
293
294void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
295  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
296      SetHasActiveUser(user_logged_in);
297}
298
299void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
300  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
301      SetCanLockScreen(can_lock_screen);
302}
303
304void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
305  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
306      SetShouldLockScreenBeforeSuspending(should_lock);
307}
308
309void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
310  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
311      SetUserAddingScreenRunning(user_adding_screen_running);
312}
313
314void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
315  switch (block_reason) {
316    case BLOCKED_BY_LOCK_SCREEN:
317      SetSessionStarted(true);
318      SetUserAddingScreenRunning(false);
319      Shell::GetInstance()->session_state_delegate()->LockScreen();
320      break;
321    case BLOCKED_BY_LOGIN_SCREEN:
322      SetUserAddingScreenRunning(false);
323      SetSessionStarted(false);
324      break;
325    case BLOCKED_BY_USER_ADDING_SCREEN:
326      SetUserAddingScreenRunning(true);
327      SetSessionStarted(true);
328      break;
329    default:
330      NOTREACHED();
331      break;
332  }
333}
334
335void AshTestBase::UnblockUserSession() {
336  Shell::GetInstance()->session_state_delegate()->UnlockScreen();
337  SetSessionStarted(true);
338  SetUserAddingScreenRunning(false);
339}
340
341
342}  // namespace test
343}  // namespace ash
344