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