ash_test_base.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/display/display_manager.h"
13#include "ash/shell.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/wm/coordinate_conversion.h"
19#include "base/command_line.h"
20#include "content/public/test/web_contents_tester.h"
21#include "ui/aura/client/aura_constants.h"
22#include "ui/aura/client/screen_position_client.h"
23#include "ui/aura/root_window.h"
24#include "ui/aura/test/event_generator.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/gfx/display.h"
29#include "ui/gfx/point.h"
30#include "ui/gfx/screen.h"
31
32#if defined(OS_WIN)
33#include "ash/test/test_metro_viewer_process_host.h"
34#include "base/test/test_process_killer_win.h"
35#include "base/win/metro.h"
36#include "base/win/windows_version.h"
37#include "ui/aura/remote_root_window_host_win.h"
38#include "ui/aura/root_window_host_win.h"
39#include "ui/base/ime/win/tsf_bridge.h"
40#include "win8/test/test_registrar_constants.h"
41#endif
42
43namespace ash {
44namespace test {
45namespace {
46
47class AshEventGeneratorDelegate : public aura::test::EventGeneratorDelegate {
48 public:
49  AshEventGeneratorDelegate() {}
50  virtual ~AshEventGeneratorDelegate() {}
51
52  // aura::test::EventGeneratorDelegate overrides:
53  virtual aura::RootWindow* GetRootWindowAt(
54      const gfx::Point& point_in_screen) const OVERRIDE {
55    gfx::Screen* screen = Shell::GetScreen();
56    gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
57    return Shell::GetInstance()->display_controller()->
58        GetRootWindowForDisplayId(display.id());
59  }
60
61  virtual aura::client::ScreenPositionClient* GetScreenPositionClient(
62      const aura::Window* window) const OVERRIDE {
63    return aura::client::GetScreenPositionClient(window->GetRootWindow());
64  }
65
66 private:
67  DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
68};
69
70}  // namespace
71
72content::WebContents* AshTestViewsDelegate::CreateWebContents(
73    content::BrowserContext* browser_context,
74    content::SiteInstance* site_instance) {
75  return content::WebContentsTester::CreateTestWebContents(browser_context,
76                                                           site_instance);
77}
78
79/////////////////////////////////////////////////////////////////////////////
80
81AshTestBase::AshTestBase()
82    : setup_called_(false),
83      teardown_called_(false) {
84  // Must initialize |ash_test_helper_| here because some tests rely on
85  // AshTestBase methods before they call AshTestBase::SetUp().
86  ash_test_helper_.reset(new AshTestHelper(&message_loop_));
87}
88
89AshTestBase::~AshTestBase() {
90  CHECK(setup_called_)
91      << "You have overridden SetUp but never called AshTestBase::SetUp";
92  CHECK(teardown_called_)
93      << "You have overridden TearDown but never called AshTestBase::TearDown";
94}
95
96void AshTestBase::SetUp() {
97  setup_called_ = true;
98  // TODO(jamescook): Can we do this without changing command line?
99  // Use the origin (1,1) so that it doesn't over
100  // lap with the native mouse cursor.
101  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
102      switches::kAshHostWindowBounds, "1+1-800x600");
103#if defined(OS_WIN)
104  aura::test::SetUsePopupAsRootWindowForTest(true);
105  if (base::win::IsTSFAwareRequired())
106    ui::TSFBridge::Initialize();
107#endif
108
109  ash_test_helper_->SetUp();
110
111  Shell::GetPrimaryRootWindow()->Show();
112  Shell::GetPrimaryRootWindow()->ShowRootWindow();
113  // Move the mouse cursor to far away so that native events doesn't
114  // interfere test expectations.
115  Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
116  ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
117
118#if defined(OS_WIN)
119  if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
120      !CommandLine::ForCurrentProcess()->HasSwitch(
121          ash::switches::kForceAshToDesktop)) {
122    metro_viewer_host_.reset(new TestMetroViewerProcessHost("viewer"));
123    CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
124        win8::test::kDefaultTestAppUserModelId));
125    aura::RemoteRootWindowHostWin* root_window_host =
126        aura::RemoteRootWindowHostWin::Instance();
127    CHECK(root_window_host != NULL);
128  }
129#endif
130}
131
132void AshTestBase::TearDown() {
133  teardown_called_ = true;
134  // Flush the message loop to finish pending release tasks.
135  RunAllPendingInMessageLoop();
136
137#if defined(OS_WIN)
138  if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
139      !CommandLine::ForCurrentProcess()->HasSwitch(
140          ash::switches::kForceAshToDesktop)) {
141    // Check that our viewer connection is still established.
142    CHECK(!metro_viewer_host_->closed_unexpectedly());
143  }
144#endif
145
146  ash_test_helper_->TearDown();
147
148#if defined(OS_WIN)
149  aura::test::SetUsePopupAsRootWindowForTest(false);
150  // Kill the viewer process if we spun one up.
151  metro_viewer_host_.reset();
152
153  // Clean up any dangling viewer processes as the metro APIs sometimes leave
154  // zombies behind. A default browser process in metro will have the
155  // following command line arg so use that to avoid killing all processes named
156  // win8::test::kDefaultTestExePath.
157  const wchar_t kViewerProcessArgument[] = L"DefaultBrowserServer";
158  base::KillAllNamedProcessesWithArgument(win8::test::kDefaultTestExePath,
159                                          kViewerProcessArgument);
160#endif
161
162  event_generator_.reset();
163  // Some tests set an internal display id,
164  // reset it here, so other tests will continue in a clean environment.
165  gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
166}
167
168aura::test::EventGenerator& AshTestBase::GetEventGenerator() {
169  if (!event_generator_) {
170    event_generator_.reset(
171        new aura::test::EventGenerator(new AshEventGeneratorDelegate()));
172  }
173  return *event_generator_.get();
174}
175
176void AshTestBase::UpdateDisplay(const std::string& display_specs) {
177  DisplayManagerTestApi display_manager_test_api(
178      Shell::GetInstance()->display_manager());
179  display_manager_test_api.UpdateDisplay(display_specs);
180}
181
182aura::RootWindow* AshTestBase::CurrentContext() {
183  return ash_test_helper_->CurrentContext();
184}
185
186aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
187  return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
188}
189
190aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
191    const gfx::Rect& bounds) {
192  return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
193}
194
195aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
196                                                   int id,
197                                                   const gfx::Rect& bounds) {
198  return CreateTestWindowInShellWithDelegate(
199      new aura::test::ColorTestWindowDelegate(color), id, bounds);
200}
201
202aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
203    aura::WindowDelegate* delegate,
204    int id,
205    const gfx::Rect& bounds) {
206  return CreateTestWindowInShellWithDelegateAndType(
207      delegate,
208      aura::client::WINDOW_TYPE_NORMAL,
209      id,
210      bounds);
211}
212
213aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
214    aura::WindowDelegate* delegate,
215    aura::client::WindowType type,
216    int id,
217    const gfx::Rect& bounds) {
218  aura::Window* window = new aura::Window(delegate);
219  window->set_id(id);
220  window->SetType(type);
221  window->Init(ui::LAYER_TEXTURED);
222  window->Show();
223
224  if (bounds.IsEmpty()) {
225    SetDefaultParentByPrimaryRootWindow(window);
226  } else {
227    gfx::Display display =
228      ash::Shell::GetInstance()->display_manager()->GetDisplayMatching(bounds);
229    aura::RootWindow* root = ash::Shell::GetInstance()->display_controller()->
230        GetRootWindowForDisplayId(display.id());
231    gfx::Point origin = bounds.origin();
232    wm::ConvertPointFromScreen(root, &origin);
233    window->SetBounds(gfx::Rect(origin, bounds.size()));
234    window->SetDefaultParentByRootWindow(root, bounds);
235  }
236  window->SetProperty(aura::client::kCanMaximizeKey, true);
237  return window;
238}
239
240void AshTestBase::SetDefaultParentByPrimaryRootWindow(aura::Window* window) {
241  window->SetDefaultParentByRootWindow(
242      Shell::GetPrimaryRootWindow(), gfx::Rect());
243}
244
245void AshTestBase::RunAllPendingInMessageLoop() {
246  ash_test_helper_->RunAllPendingInMessageLoop();
247}
248
249void AshTestBase::SetSessionStarted(bool session_started) {
250  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
251      SetActiveUserSessionStarted(session_started);
252}
253
254void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
255  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
256      SetHasActiveUser(user_logged_in);
257}
258
259void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
260  ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
261      SetCanLockScreen(can_lock_screen);
262}
263
264}  // namespace test
265}  // namespace ash
266