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