bluetooth: Initial JNI setup for device/bluetooth.
[chromium-blink-merge.git] / chrome / test / base / view_event_test_base.cc
blob0b007a3534712be547f9c5d329bfa1a2ffd02782
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.
5 #include "chrome/test/base/view_event_test_base.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "chrome/test/base/chrome_unit_test_suite.h"
10 #include "chrome/test/base/interactive_test_utils.h"
11 #include "chrome/test/base/testing_browser_process.h"
12 #include "chrome/test/base/view_event_test_platform_part.h"
13 #include "ui/base/ime/input_method_initializer.h"
14 #include "ui/base/test/ui_controls.h"
15 #include "ui/compositor/test/context_factories_for_test.h"
16 #include "ui/views/view.h"
17 #include "ui/views/widget/widget.h"
19 namespace {
21 // View subclass that allows you to specify the preferred size.
22 class TestView : public views::View {
23 public:
24 TestView() {}
26 void SetPreferredSize(const gfx::Size& size) {
27 preferred_size_ = size;
28 PreferredSizeChanged();
31 gfx::Size GetPreferredSize() const override {
32 if (!preferred_size_.IsEmpty())
33 return preferred_size_;
34 return View::GetPreferredSize();
37 void Layout() override {
38 View* child_view = child_at(0);
39 child_view->SetBounds(0, 0, width(), height());
42 private:
43 gfx::Size preferred_size_;
45 DISALLOW_COPY_AND_ASSIGN(TestView);
48 // Delay in background thread before posting mouse move.
49 const int kMouseMoveDelayMS = 200;
51 } // namespace
53 ViewEventTestBase::ViewEventTestBase()
54 : window_(NULL),
55 content_view_(NULL) {
56 // The TestingBrowserProcess must be created in the constructor because there
57 // are tests that require it before SetUp() is called.
58 TestingBrowserProcess::CreateInstance();
61 void ViewEventTestBase::Done() {
62 run_loop_.Quit();
65 void ViewEventTestBase::SetUpTestCase() {
66 ChromeUnitTestSuite::InitializeProviders();
67 ChromeUnitTestSuite::InitializeResourceBundle();
70 void ViewEventTestBase::SetUp() {
71 views::ViewsDelegate::views_delegate = &views_delegate_;
72 ui::InitializeInputMethodForTesting();
74 // The ContextFactory must exist before any Compositors are created.
75 bool enable_pixel_output = false;
76 ui::ContextFactory* context_factory =
77 ui::InitializeContextFactoryForTests(enable_pixel_output);
78 views_delegate_.set_context_factory(context_factory);
79 views_delegate_.set_use_desktop_native_widgets(true);
81 platform_part_.reset(ViewEventTestPlatformPart::Create(context_factory));
82 gfx::NativeWindow context = platform_part_->GetContext();
83 window_ = views::Widget::CreateWindowWithContext(this, context);
84 window_->Show();
87 void ViewEventTestBase::TearDown() {
88 if (window_) {
89 window_->Close();
90 content::RunAllPendingInMessageLoop();
91 window_ = NULL;
94 ui::Clipboard::DestroyClipboardForCurrentThread();
95 platform_part_.reset();
97 ui::TerminateContextFactoryForTests();
99 ui::ShutdownInputMethodForTesting();
100 views::ViewsDelegate::views_delegate = NULL;
103 bool ViewEventTestBase::CanResize() const {
104 return true;
107 views::View* ViewEventTestBase::GetContentsView() {
108 if (!content_view_) {
109 // Wrap the real view (as returned by CreateContentsView) in a View so
110 // that we can customize the preferred size.
111 TestView* test_view = new TestView();
112 test_view->SetPreferredSize(GetPreferredSize());
113 test_view->AddChildView(CreateContentsView());
114 content_view_ = test_view;
116 return content_view_;
119 const views::Widget* ViewEventTestBase::GetWidget() const {
120 return content_view_->GetWidget();
123 views::Widget* ViewEventTestBase::GetWidget() {
124 return content_view_->GetWidget();
127 ViewEventTestBase::~ViewEventTestBase() {
128 TestingBrowserProcess::DeleteInstance();
131 void ViewEventTestBase::StartMessageLoopAndRunTest() {
132 ASSERT_TRUE(
133 ui_test_utils::ShowAndFocusNativeWindow(window_->GetNativeWindow()));
135 // Flush any pending events to make sure we start with a clean slate.
136 content::RunAllPendingInMessageLoop();
138 // Schedule a task that starts the test. Need to do this as we're going to
139 // run the message loop.
140 base::MessageLoop::current()->PostTask(
141 FROM_HERE, base::Bind(&ViewEventTestBase::DoTestOnMessageLoop, this));
143 content::RunThisRunLoop(&run_loop_);
146 gfx::Size ViewEventTestBase::GetPreferredSize() const {
147 return gfx::Size();
150 void ViewEventTestBase::ScheduleMouseMoveInBackground(int x, int y) {
151 if (!dnd_thread_.get()) {
152 dnd_thread_.reset(new base::Thread("mouse-move-thread"));
153 dnd_thread_->Start();
155 dnd_thread_->message_loop()->PostDelayedTask(
156 FROM_HERE,
157 base::Bind(base::IgnoreResult(&ui_controls::SendMouseMove), x, y),
158 base::TimeDelta::FromMilliseconds(kMouseMoveDelayMS));
161 void ViewEventTestBase::StopBackgroundThread() {
162 dnd_thread_.reset(NULL);
165 void ViewEventTestBase::RunTestMethod(const base::Closure& task) {
166 StopBackgroundThread();
168 task.Run();
169 if (HasFatalFailure())
170 Done();