Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / base / view_event_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 "chrome/test/base/view_event_test_base.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "chrome/test/base/chrome_unit_test_suite.h"
12 #include "chrome/test/base/interactive_test_utils.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "ui/aura/client/event_client.h"
16 #include "ui/aura/env.h"
17 #include "ui/aura/test/aura_test_helper.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/aura/window_tree_host.h"
20 #include "ui/base/ime/input_method_initializer.h"
21 #include "ui/base/test/ui_controls.h"
22 #include "ui/compositor/test/context_factories_for_test.h"
23 #include "ui/compositor/test/context_factories_for_test.h"
24 #include "ui/message_center/message_center.h"
25 #include "ui/views/view.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/wm/core/default_activation_client.h"
28 #include "ui/wm/core/wm_state.h"
29
30 #if defined(USE_ASH)
31 #include "ash/shell.h"
32 #include "ash/test/test_session_state_delegate.h"
33 #include "ash/test/test_shell_delegate.h"
34 #endif
35
36 #if defined(OS_CHROMEOS)
37 #include "chromeos/audio/cras_audio_handler.h"
38 #include "chromeos/dbus/dbus_thread_manager.h"
39 #include "chromeos/network/network_handler.h"
40 #else  // !defined(OS_CHROMEOS)
41 #include "ui/views/widget/desktop_aura/desktop_screen.h"
42 #endif
43
44 namespace {
45
46 // View subclass that allows you to specify the preferred size.
47 class TestView : public views::View {
48  public:
49   TestView() {}
50
51   void SetPreferredSize(const gfx::Size& size) {
52     preferred_size_ = size;
53     PreferredSizeChanged();
54   }
55
56   virtual gfx::Size GetPreferredSize() OVERRIDE {
57     if (!preferred_size_.IsEmpty())
58       return preferred_size_;
59     return View::GetPreferredSize();
60   }
61
62   virtual void Layout() OVERRIDE {
63     View* child_view = child_at(0);
64     child_view->SetBounds(0, 0, width(), height());
65   }
66
67  private:
68   gfx::Size preferred_size_;
69
70   DISALLOW_COPY_AND_ASSIGN(TestView);
71 };
72
73 // Delay in background thread before posting mouse move.
74 const int kMouseMoveDelayMS = 200;
75
76 }  // namespace
77
78 ViewEventTestBase::ViewEventTestBase()
79   : window_(NULL),
80     content_view_(NULL) {
81   // The TestingBrowserProcess must be created in the constructor because there
82   // are tests that require it before SetUp() is called.
83   TestingBrowserProcess::CreateInstance();
84 }
85
86 void ViewEventTestBase::Done() {
87   base::MessageLoop::current()->Quit();
88
89   // If we're in a nested message loop, as is the case with menus, we
90   // need to quit twice. The second quit does that for us. Finish all
91   // pending UI events before posting closure because events it may be
92   // executed before UI events are executed.
93   ui_controls::RunClosureAfterAllPendingUIEvents(
94       base::MessageLoop::QuitClosure());
95 }
96
97 void ViewEventTestBase::SetUpTestCase() {
98   ChromeUnitTestSuite::InitializeProviders();
99   ChromeUnitTestSuite::InitializeResourceBundle();
100 }
101
102 void ViewEventTestBase::SetUp() {
103   wm_state_.reset(new wm::WMState);
104
105   views::ViewsDelegate::views_delegate = &views_delegate_;
106   ui::InitializeInputMethodForTesting();
107   gfx::NativeView context = NULL;
108
109   // The ContextFactory must exist before any Compositors are created.
110   bool enable_pixel_output = false;
111   ui::InitializeContextFactoryForTests(enable_pixel_output);
112
113 #if defined(USE_ASH)
114 #if defined(OS_WIN)
115   // http://crbug.com/154081 use ash::Shell code path below on win_ash bots when
116   // interactive_ui_tests is brought up on that platform.
117   gfx::Screen::SetScreenInstance(
118       gfx::SCREEN_TYPE_NATIVE, views::CreateDesktopScreen());
119
120 #else  // !OS_WIN
121   // Ash Shell can't just live on its own without a browser process, we need to
122   // also create the message center.
123   message_center::MessageCenter::Initialize();
124 #if defined(OS_CHROMEOS)
125   chromeos::DBusThreadManager::InitializeWithStub();
126   chromeos::CrasAudioHandler::InitializeForTesting();
127   chromeos::NetworkHandler::Initialize();
128 #endif  // OS_CHROMEOS
129   ash::test::TestShellDelegate* shell_delegate =
130       new ash::test::TestShellDelegate();
131   ash::Shell::CreateInstance(shell_delegate);
132   shell_delegate->test_session_state_delegate()
133       ->SetActiveUserSessionStarted(true);
134   context = ash::Shell::GetPrimaryRootWindow();
135   context->GetHost()->Show();
136 #endif  // !OS_WIN
137   aura::Env::CreateInstance(true);
138 #elif defined(USE_AURA)
139   // Instead of using the ash shell, use an AuraTestHelper to create and manage
140   // the test screen.
141   aura_test_helper_.reset(
142       new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
143   aura_test_helper_->SetUp();
144   new wm::DefaultActivationClient(aura_test_helper_->root_window());
145   context = aura_test_helper_->root_window();
146 #endif  // !USE_ASH && USE_AURA
147
148   window_ = views::Widget::CreateWindowWithContext(this, context);
149 }
150
151 void ViewEventTestBase::TearDown() {
152   if (window_) {
153     window_->Close();
154     content::RunAllPendingInMessageLoop();
155     window_ = NULL;
156   }
157
158   ui::Clipboard::DestroyClipboardForCurrentThread();
159
160 #if defined(USE_ASH)
161 #if !defined(OS_WIN)
162   ash::Shell::DeleteInstance();
163 #if defined(OS_CHROMEOS)
164   chromeos::NetworkHandler::Shutdown();
165   chromeos::CrasAudioHandler::Shutdown();
166   chromeos::DBusThreadManager::Shutdown();
167 #endif
168   // Ash Shell can't just live on its own without a browser process, we need to
169   // also shut down the message center.
170   message_center::MessageCenter::Shutdown();
171 #endif  // !OS_WIN
172   aura::Env::DeleteInstance();
173 #elif defined(USE_AURA)
174   aura_test_helper_->TearDown();
175 #endif  // !USE_ASH && USE_AURA
176
177   ui::TerminateContextFactoryForTests();
178
179   ui::ShutdownInputMethodForTesting();
180   views::ViewsDelegate::views_delegate = NULL;
181
182   wm_state_.reset();
183 }
184
185 bool ViewEventTestBase::CanResize() const {
186   return true;
187 }
188
189 views::View* ViewEventTestBase::GetContentsView() {
190   if (!content_view_) {
191     // Wrap the real view (as returned by CreateContentsView) in a View so
192     // that we can customize the preferred size.
193     TestView* test_view = new TestView();
194     test_view->SetPreferredSize(GetPreferredSize());
195     test_view->AddChildView(CreateContentsView());
196     content_view_ = test_view;
197   }
198   return content_view_;
199 }
200
201 const views::Widget* ViewEventTestBase::GetWidget() const {
202   return content_view_->GetWidget();
203 }
204
205 views::Widget* ViewEventTestBase::GetWidget() {
206   return content_view_->GetWidget();
207 }
208
209 ViewEventTestBase::~ViewEventTestBase() {
210   TestingBrowserProcess::DeleteInstance();
211 }
212
213 void ViewEventTestBase::StartMessageLoopAndRunTest() {
214   ASSERT_TRUE(
215       ui_test_utils::ShowAndFocusNativeWindow(window_->GetNativeWindow()));
216
217   // Flush any pending events to make sure we start with a clean slate.
218   content::RunAllPendingInMessageLoop();
219
220   // Schedule a task that starts the test. Need to do this as we're going to
221   // run the message loop.
222   base::MessageLoop::current()->PostTask(
223       FROM_HERE, base::Bind(&ViewEventTestBase::DoTestOnMessageLoop, this));
224
225   content::RunMessageLoop();
226 }
227
228 gfx::Size ViewEventTestBase::GetPreferredSize() {
229   return gfx::Size();
230 }
231
232 void ViewEventTestBase::ScheduleMouseMoveInBackground(int x, int y) {
233   if (!dnd_thread_.get()) {
234     dnd_thread_.reset(new base::Thread("mouse-move-thread"));
235     dnd_thread_->Start();
236   }
237   dnd_thread_->message_loop()->PostDelayedTask(
238       FROM_HERE,
239       base::Bind(base::IgnoreResult(&ui_controls::SendMouseMove), x, y),
240       base::TimeDelta::FromMilliseconds(kMouseMoveDelayMS));
241 }
242
243 void ViewEventTestBase::StopBackgroundThread() {
244   dnd_thread_.reset(NULL);
245 }
246
247 void ViewEventTestBase::RunTestMethod(const base::Closure& task) {
248   StopBackgroundThread();
249
250   task.Run();
251   if (HasFatalFailure())
252     Done();
253 }