Upstream version 11.40.277.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/window_positioner.h"
20 #include "base/command_line.h"
21 #include "ui/aura/client/aura_constants.h"
22 #include "ui/aura/client/screen_position_client.h"
23 #include "ui/aura/client/window_tree_client.h"
24 #include "ui/aura/test/event_generator_delegate_aura.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/aura/window_tree_host.h"
29 #include "ui/base/ime/input_method_initializer.h"
30 #include "ui/events/gesture_detection/gesture_configuration.h"
31 #include "ui/gfx/display.h"
32 #include "ui/gfx/point.h"
33 #include "ui/gfx/screen.h"
34 #include "ui/wm/core/coordinate_conversion.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/win/metro.h"
43 #include "base/win/windows_version.h"
44 #include "ui/aura/remote_window_tree_host_win.h"
45 #include "ui/aura/window_tree_host_win.h"
46 #include "ui/platform_window/win/win_window.h"
47 #include "win8/test/test_registrar_constants.h"
48 #endif
49
50 #if defined(USE_X11)
51 #include "ui/gfx/x/x11_connection.h"
52 #endif
53
54 namespace ash {
55 namespace test {
56 namespace {
57
58 class AshEventGeneratorDelegate
59     : public aura::test::EventGeneratorDelegateAura {
60  public:
61   AshEventGeneratorDelegate() {}
62   ~AshEventGeneratorDelegate() override {}
63
64   // aura::test::EventGeneratorDelegateAura overrides:
65   aura::WindowTreeHost* GetHostAt(
66       const gfx::Point& point_in_screen) const override {
67     gfx::Screen* screen = Shell::GetScreen();
68     gfx::Display display = screen->GetDisplayNearestPoint(point_in_screen);
69     return Shell::GetInstance()->display_controller()->
70         GetRootWindowForDisplayId(display.id())->GetHost();
71   }
72
73   aura::client::ScreenPositionClient* GetScreenPositionClient(
74       const aura::Window* window) const override {
75     return aura::client::GetScreenPositionClient(window->GetRootWindow());
76   }
77
78  private:
79   DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate);
80 };
81
82 }  // namespace
83
84 /////////////////////////////////////////////////////////////////////////////
85
86 AshTestBase::AshTestBase()
87     : setup_called_(false),
88       teardown_called_(false),
89       start_session_(true) {
90 #if defined(USE_X11)
91   // This is needed for tests which use this base class but are run in browser
92   // test binaries so don't get the default initialization in the unit test
93   // suite.
94   gfx::InitializeThreadedX11();
95 #endif
96
97   thread_bundle_.reset(new content::TestBrowserThreadBundle);
98   // Must initialize |ash_test_helper_| here because some tests rely on
99   // AshTestBase methods before they call AshTestBase::SetUp().
100   ash_test_helper_.reset(new AshTestHelper(base::MessageLoopForUI::current()));
101 }
102
103 AshTestBase::~AshTestBase() {
104   CHECK(setup_called_)
105       << "You have overridden SetUp but never called AshTestBase::SetUp";
106   CHECK(teardown_called_)
107       << "You have overridden TearDown but never called AshTestBase::TearDown";
108 }
109
110 void AshTestBase::SetUp() {
111   setup_called_ = true;
112
113   // Clears the saved state so that test doesn't use on the wrong
114   // default state.
115   shell::ToplevelWindow::ClearSavedStateForTest();
116
117   // TODO(jamescook): Can we do this without changing command line?
118   // Use the origin (1,1) so that it doesn't over
119   // lap with the native mouse cursor.
120   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
121   if (!command_line->HasSwitch(switches::kAshHostWindowBounds)) {
122     command_line->AppendSwitchASCII(
123         switches::kAshHostWindowBounds, "1+1-800x600");
124   }
125 #if defined(OS_WIN)
126   ui::test::SetUsePopupAsRootWindowForTest(true);
127 #endif
128   ash_test_helper_->SetUp(start_session_);
129
130   Shell::GetPrimaryRootWindow()->Show();
131   Shell::GetPrimaryRootWindow()->GetHost()->Show();
132   // Move the mouse cursor to far away so that native events doesn't
133   // interfere test expectations.
134   Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
135   ash::Shell::GetInstance()->cursor_manager()->EnableMouseEvents();
136
137   // Changing GestureConfiguration shouldn't make tests fail.
138   ui::GestureConfiguration::GetInstance()
139       ->set_max_touch_move_in_pixels_for_click(5);
140
141 #if defined(OS_WIN)
142   if (!command_line->HasSwitch(ash::switches::kForceAshToDesktop)) {
143     if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
144       ipc_thread_.reset(new base::Thread("test_metro_viewer_ipc_thread"));
145       base::Thread::Options options;
146       options.message_loop_type = base::MessageLoop::TYPE_IO;
147       ipc_thread_->StartWithOptions(options);
148       metro_viewer_host_.reset(
149           new TestMetroViewerProcessHost(ipc_thread_->message_loop_proxy()));
150       CHECK(metro_viewer_host_->LaunchViewerAndWaitForConnection(
151           win8::test::kDefaultTestAppUserModelId));
152       aura::RemoteWindowTreeHostWin* window_tree_host =
153           aura::RemoteWindowTreeHostWin::Instance();
154       CHECK(window_tree_host != NULL);
155     }
156     ash::WindowPositioner::SetMaximizeFirstWindow(true);
157   }
158 #endif
159 }
160
161 void AshTestBase::TearDown() {
162   teardown_called_ = true;
163   // Flush the message loop to finish pending release tasks.
164   RunAllPendingInMessageLoop();
165
166 #if defined(OS_WIN)
167   if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
168       !base::CommandLine::ForCurrentProcess()->HasSwitch(
169           ash::switches::kForceAshToDesktop)) {
170     // Check that our viewer connection is still established.
171     CHECK(!metro_viewer_host_->closed_unexpectedly());
172   }
173 #endif
174
175   ash_test_helper_->TearDown();
176 #if defined(OS_WIN)
177   ui::test::SetUsePopupAsRootWindowForTest(false);
178   // Kill the viewer process if we spun one up.
179   if (metro_viewer_host_) {
180     metro_viewer_host_->TerminateViewer();
181     metro_viewer_host_.reset();
182   }
183 #endif
184
185   event_generator_.reset();
186   // Some tests set an internal display id,
187   // reset it here, so other tests will continue in a clean environment.
188   gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
189 }
190
191 ui::test::EventGenerator& AshTestBase::GetEventGenerator() {
192   if (!event_generator_) {
193     event_generator_.reset(
194         new ui::test::EventGenerator(new AshEventGeneratorDelegate()));
195   }
196   return *event_generator_.get();
197 }
198
199 bool AshTestBase::SupportsMultipleDisplays() {
200   return AshTestHelper::SupportsMultipleDisplays();
201 }
202
203 bool AshTestBase::SupportsHostWindowResize() {
204   return AshTestHelper::SupportsHostWindowResize();
205 }
206
207 void AshTestBase::UpdateDisplay(const std::string& display_specs) {
208   DisplayManagerTestApi display_manager_test_api(
209       Shell::GetInstance()->display_manager());
210   display_manager_test_api.UpdateDisplay(display_specs);
211 }
212
213 aura::Window* AshTestBase::CurrentContext() {
214   return ash_test_helper_->CurrentContext();
215 }
216
217 aura::Window* AshTestBase::CreateTestWindowInShellWithId(int id) {
218   return CreateTestWindowInShellWithDelegate(NULL, id, gfx::Rect());
219 }
220
221 aura::Window* AshTestBase::CreateTestWindowInShellWithBounds(
222     const gfx::Rect& bounds) {
223   return CreateTestWindowInShellWithDelegate(NULL, 0, bounds);
224 }
225
226 aura::Window* AshTestBase::CreateTestWindowInShell(SkColor color,
227                                                    int id,
228                                                    const gfx::Rect& bounds) {
229   return CreateTestWindowInShellWithDelegate(
230       new aura::test::ColorTestWindowDelegate(color), id, bounds);
231 }
232
233 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegate(
234     aura::WindowDelegate* delegate,
235     int id,
236     const gfx::Rect& bounds) {
237   return CreateTestWindowInShellWithDelegateAndType(
238       delegate, ui::wm::WINDOW_TYPE_NORMAL, id, bounds);
239 }
240
241 aura::Window* AshTestBase::CreateTestWindowInShellWithDelegateAndType(
242     aura::WindowDelegate* delegate,
243     ui::wm::WindowType type,
244     int id,
245     const gfx::Rect& bounds) {
246   aura::Window* window = new aura::Window(delegate);
247   window->set_id(id);
248   window->SetType(type);
249   window->Init(aura::WINDOW_LAYER_TEXTURED);
250   window->Show();
251
252   if (bounds.IsEmpty()) {
253     ParentWindowInPrimaryRootWindow(window);
254   } else {
255     gfx::Display display =
256         Shell::GetScreen()->GetDisplayMatching(bounds);
257     aura::Window* root = ash::Shell::GetInstance()->display_controller()->
258         GetRootWindowForDisplayId(display.id());
259     gfx::Point origin = bounds.origin();
260     ::wm::ConvertPointFromScreen(root, &origin);
261     window->SetBounds(gfx::Rect(origin, bounds.size()));
262     aura::client::ParentWindowWithContext(window, root, bounds);
263   }
264   window->SetProperty(aura::client::kCanMaximizeKey, true);
265   window->SetProperty(aura::client::kCanMinimizeKey, true);
266   return window;
267 }
268
269 void AshTestBase::ParentWindowInPrimaryRootWindow(aura::Window* window) {
270   aura::client::ParentWindowWithContext(
271       window, Shell::GetPrimaryRootWindow(), gfx::Rect());
272 }
273
274 void AshTestBase::RunAllPendingInMessageLoop() {
275   ash_test_helper_->RunAllPendingInMessageLoop();
276 }
277
278 TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() {
279   return ash_test_helper_->test_screenshot_delegate();
280 }
281
282 TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() {
283   return static_cast<TestSystemTrayDelegate*>(
284       Shell::GetInstance()->system_tray_delegate());
285 }
286
287 void AshTestBase::SetSessionStarted(bool session_started) {
288   ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
289       SetActiveUserSessionStarted(session_started);
290 }
291
292 void AshTestBase::SetUserLoggedIn(bool user_logged_in) {
293   ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
294       SetHasActiveUser(user_logged_in);
295 }
296
297 void AshTestBase::SetCanLockScreen(bool can_lock_screen) {
298   ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
299       SetCanLockScreen(can_lock_screen);
300 }
301
302 void AshTestBase::SetShouldLockScreenBeforeSuspending(bool should_lock) {
303   ash_test_helper_->test_shell_delegate()->test_session_state_delegate()->
304       SetShouldLockScreenBeforeSuspending(should_lock);
305 }
306
307 void 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
312 void 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
333 void AshTestBase::UnblockUserSession() {
334   Shell::GetInstance()->session_state_delegate()->UnlockScreen();
335   SetSessionStarted(true);
336   SetUserAddingScreenRunning(false);
337 }
338
339
340 }  // namespace test
341 }  // namespace ash