ash_test_base.cc revision f2477e01787aa58f445919b809d89e252beef54f
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          L"test_open"));
143      aura::RemoteRootWindowHostWin* root_window_host =
144          aura::RemoteRootWindowHostWin::Instance();
145      CHECK(root_window_host != NULL);
146    }
147    ash::WindowPositioner::SetMaximizeFirstWindow(true);
148  }
149#endif
150}
151
152void AshTestBase::TearDown() {
153  teardown_called_ = true;
154  // Flush the message loop to finish pending release tasks.
155  RunAllPendingInMessageLoop();
156
157#if defined(OS_WIN)
158  if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
159      !CommandLine::ForCurrentProcess()->HasSwitch(
160          ash::switches::kForceAshToDesktop)) {
161    // Check that our viewer connection is still established.
162    CHECK(!metro_viewer_host_->closed_unexpectedly());
163  }
164#endif
165
166  ash_test_helper_->TearDown();
167#if defined(OS_WIN)
168  aura::test::SetUsePopupAsRootWindowForTest(false);
169  // Kill the viewer process if we spun one up.
170  metro_viewer_host_.reset();
171
172  // Clean up any dangling viewer processes as the metro APIs sometimes leave
173  // zombies behind. A default browser process in metro will have the
174  // following command line arg so use that to avoid killing all processes named
175  // win8::test::kDefaultTestExePath.
176  const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer";
177  base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath,
178                                          kViewerProcessArgument);
179#endif
180
181  event_generator_.reset();
182  // Some tests set an internal display id,
183  // reset it here, so other tests will continue in a clean environment.
184  gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
185}
186
187aura::test::EventGenerator& AshTestBase::GetEventGenerator() {
188  if (!event_generator_) {
189    event_generator_.reset(
190        new aura::test::EventGenerator(new AshEventGeneratorDelegate()));
191  }
192  return *event_generator_.get();
193}
194
195// static
196bool AshTestBase::SupportsMultipleDisplays() {
197#if defined(OS_WIN)
198  return base::win::GetVersion() < base::win::VERSION_WIN8;
199#else
200  return true;
201#endif
202}
203
204// static
205bool AshTestBase::SupportsHostWindowResize() {
206#if defined(OS_WIN)
207  return base::win::GetVersion() < base::win::VERSION_WIN8;
208#else
209  return true;
210#endif
211}
212
213void AshTestBase::UpdateDisplay(const std::string& display_specs) {
214  DisplayManagerTestApi display_manager_test_api(
215      Shell::GetInstance()->display_manager());
216  display_manager_test_api.UpdateDisplay(display_specs);
217}
218
219aura::Window* AshTestBase::CurrentContext() {
220  return ash_test_helper_->CurrentContext();
221}
222
223aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
224  return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
225}
226
227aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
228    const gfx::Rect& bounds) {
229  return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
230}
231
232aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
233                                                   int id,
234                                                   const gfx::Rect& bounds) {
235  return CreateTestWindowInShellWithDelegate(
236      new aura::test::ColorTestWindowDelegate(color), id, bounds);
237}
238
239aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
240    aura::WindowDelegate* delegate,
241    int id,
242    const gfx::Rect& bounds) {
243  return CreateTestWindowInShellWithDelegateAndType(
244      delegate,
245      aura::client::WINDOW_TYPE_NORMAL,
246      id,
247      bounds);
248}
249
250aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
251    aura::WindowDelegate* delegate,
252    aura::client::WindowType type,
253    int id,
254    const gfx::Rect& bounds) {
255  aura::Window* window = new aura::Window(delegate);
256  window->set_id(id);
257  window->SetType(type);
258  window->Init(ui::LAYER_TEXTURED);
259  window->Show();
260
261  if (bounds.IsEmpty()) {
262    ParentWindowInPrimaryRootWindow(window);
263  } else {
264    gfx::Display display =
265        Shell::GetScreen()->GetDisplayMatching(bounds);
266    aura::Window* root = ash::Shell::GetInstance()->display_controller()->
267        GetRootWindowForDisplayId(display.id());
268    gfx::Point origin = bounds.origin();
269    wm::ConvertPointFromScreen(root, &origin);
270    window->SetBounds(gfx::Rect(origin, bounds.size()));
271    aura::client::ParentWindowWithContext(window, root, bounds);
272  }
273  window->SetProperty(aura::client::kCanMaximizeKey, true);
274  return window;
275}
276
277void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
278  aura::client::ParentWindowWithContext(
279      window, Shell::GetPrimaryRootWindow(), gfx::Rect());
280}
281
282void AshTestBase::RunAllPendingInMessageLoop() {
283  ash_test_helper_->RunAllPendingInMessageLoop();
284}
285
286TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
287  return ash_test_helper_->test_screenshot_delegate();
288}
289
290void AshTestBase::SetSessionStarted(bool session_started) {
291  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
292      SetActiveUserSessionStarted(session_started);
293}
294
295void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
296  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
297      SetHasActiveUser(user_logged_in);
298}
299
300void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
301  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
302      SetCanLockScreen(can_lock_screen);
303}
304
305void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
306  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
307      SetShouldLockScreenBeforeSuspending(should_lock);
308}
309
310void AshTestBase::SetUserAddingScreenRunning(bool user_adding_screen_running) {
311  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
312      SetUserAddingScreenRunning(user_adding_screen_running);
313}
314
315void AshTestBase::BlockUserSession(UserSessionBlockReason block_reason) {
316  switch (block_reason) {
317    case BLOCKED_BY_LOCK_SCREEN:
318      SetSessionStarted(true);
319      SetUserAddingScreenRunning(false);
320      Shell::GetInstance()->session_state_delegate()->LockScreen();
321      break;
322    case BLOCKED_BY_LOGIN_SCREEN:
323      SetUserAddingScreenRunning(false);
324      SetSessionStarted(false);
325      break;
326    case BLOCKED_BY_USER_ADDING_SCREEN:
327      SetUserAddingScreenRunning(true);
328      SetSessionStarted(true);
329      break;
330    default:
331      NOTREACHED();
332      break;
333  }
334}
335
336void AshTestBase::UnblockUserSession() {
337  Shell::GetInstance()->session_state_delegate()->UnlockScreen();
338  SetSessionStarted(true);
339  SetUserAddingScreenRunning(false);
340}
341
342
343}  // namespace test
344}  // namespace ash
345