Fix vectorized icon for PPAPI broker website setting icon
[chromium-blink-merge.git] / chrome / test / base / view_event_test_base.cc
blob8978f12ab95bb5e7621369103c4fbfdab0fff591
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/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.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/view_event_test_platform_part.h"
15 #include "ui/base/ime/input_method_initializer.h"
16 #include "ui/base/test/ui_controls.h"
17 #include "ui/compositor/test/context_factories_for_test.h"
18 #include "ui/views/view.h"
19 #include "ui/views/widget/widget.h"
21 namespace {
23 // View subclass that allows you to specify the preferred size.
24 class TestView : public views::View {
25 public:
26 explicit TestView(ViewEventTestBase* harness) : harness_(harness) {}
28 gfx::Size GetPreferredSize() const override {
29 return harness_->GetPreferredSize();
32 void Layout() override {
33 View* child_view = child_at(0);
34 child_view->SetBounds(0, 0, width(), height());
37 private:
38 ViewEventTestBase* harness_;
40 DISALLOW_COPY_AND_ASSIGN(TestView);
43 // Delay in background thread before posting mouse move.
44 const int kMouseMoveDelayMS = 200;
46 } // namespace
48 ViewEventTestBase::ViewEventTestBase()
49 : window_(NULL),
50 content_view_(NULL) {
51 // The TestingBrowserProcess must be created in the constructor because there
52 // are tests that require it before SetUp() is called.
53 TestingBrowserProcess::CreateInstance();
56 void ViewEventTestBase::Done() {
57 run_loop_.Quit();
60 void ViewEventTestBase::SetUpTestCase() {
61 ChromeUnitTestSuite::InitializeProviders();
62 ChromeUnitTestSuite::InitializeResourceBundle();
65 void ViewEventTestBase::SetUp() {
66 ui::InitializeInputMethodForTesting();
68 // The ContextFactory must exist before any Compositors are created.
69 bool enable_pixel_output = false;
70 ui::ContextFactory* context_factory =
71 ui::InitializeContextFactoryForTests(enable_pixel_output);
72 views_delegate_.set_context_factory(context_factory);
73 views_delegate_.set_use_desktop_native_widgets(true);
75 platform_part_.reset(ViewEventTestPlatformPart::Create(context_factory));
76 gfx::NativeWindow context = platform_part_->GetContext();
77 window_ = views::Widget::CreateWindowWithContext(this, context);
78 window_->Show();
81 void ViewEventTestBase::TearDown() {
82 if (window_) {
83 window_->Close();
84 content::RunAllPendingInMessageLoop();
85 window_ = NULL;
88 ui::Clipboard::DestroyClipboardForCurrentThread();
89 platform_part_.reset();
91 ui::TerminateContextFactoryForTests();
93 ui::ShutdownInputMethodForTesting();
96 gfx::Size ViewEventTestBase::GetPreferredSize() const {
97 return gfx::Size();
100 bool ViewEventTestBase::CanResize() const {
101 return true;
104 views::View* ViewEventTestBase::GetContentsView() {
105 if (!content_view_) {
106 // Wrap the real view (as returned by CreateContentsView) in a View so
107 // that we can customize the preferred size.
108 TestView* test_view = new TestView(this);
109 test_view->AddChildView(CreateContentsView());
110 content_view_ = test_view;
112 return content_view_;
115 const views::Widget* ViewEventTestBase::GetWidget() const {
116 return content_view_->GetWidget();
119 views::Widget* ViewEventTestBase::GetWidget() {
120 return content_view_->GetWidget();
123 ViewEventTestBase::~ViewEventTestBase() {
124 TestingBrowserProcess::DeleteInstance();
127 void ViewEventTestBase::StartMessageLoopAndRunTest() {
128 ASSERT_TRUE(
129 ui_test_utils::ShowAndFocusNativeWindow(window_->GetNativeWindow()));
131 // Flush any pending events to make sure we start with a clean slate.
132 content::RunAllPendingInMessageLoop();
134 // Schedule a task that starts the test. Need to do this as we're going to
135 // run the message loop.
136 base::ThreadTaskRunnerHandle::Get()->PostTask(
137 FROM_HERE, base::Bind(&ViewEventTestBase::DoTestOnMessageLoop, this));
139 content::RunThisRunLoop(&run_loop_);
142 void ViewEventTestBase::ScheduleMouseMoveInBackground(int x, int y) {
143 if (!dnd_thread_.get()) {
144 dnd_thread_.reset(new base::Thread("mouse-move-thread"));
145 dnd_thread_->Start();
147 dnd_thread_->task_runner()->PostDelayedTask(
148 FROM_HERE,
149 base::Bind(base::IgnoreResult(&ui_controls::SendMouseMove), x, y),
150 base::TimeDelta::FromMilliseconds(kMouseMoveDelayMS));
153 void ViewEventTestBase::StopBackgroundThread() {
154 dnd_thread_.reset(NULL);
157 void ViewEventTestBase::RunTestMethod(const base::Closure& task) {
158 StopBackgroundThread();
160 task.Run();
161 if (HasFatalFailure())
162 Done();