blob: 6a169ceadcf62741149ea956d962c8da102179f7 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "base/macros.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_task_environment.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "ui/base/ui_base_features.h"
#include "ui/views/test/scoped_views_test_helper.h"
#include "ui/views/test/test_views_delegate.h"
#include "ui/views/widget/widget.h"
#if defined(OS_WIN)
#include "ui/base/win/scoped_ole_initializer.h"
namespace aura {
class Env;
namespace base {
class ShadowingAtExitManager;
namespace views {
// A base class for views unit test. It creates a message loop necessary
// to drive UI events and takes care of OLE initialization for windows.
class ViewsTestBase : public PlatformTest {
using ScopedTaskEnvironment = base::test::ScopedTaskEnvironment;
enum class NativeWidgetType {
kDefault, // On Aura, corresponds to NativeWidgetAura.
kDesktop, // On Aura, corresponds to DesktopNativeWidgetAura.
// For Mus/ChromeOS, passing this to the ViewsTestBase
// constructor will also do necessary Mus setup.
~ViewsTestBase() override;
// testing::Test:
void SetUp() override;
void TearDown() override;
// This copies some of the setup done in ViewsTestSuite, so it's only
// necessary for a ViewsTestBase that runs out of that test suite, such as in
// interactive ui tests.
void SetUpForInteractiveTests();
void RunPendingMessages();
// Creates a widget of |type| with any platform specific data for use in
// cross-platform tests.
virtual Widget::InitParams CreateParams(Widget::InitParams::Type type);
bool HasCompositingManager() const;
// Simulate an OS-level destruction of the native window held by |widget|.
void SimulateNativeDestroy(Widget* widget);
TestViewsDelegate* test_views_delegate() const {
return test_helper_->test_views_delegate();
void set_native_widget_type(NativeWidgetType native_widget_type) {
native_widget_type_ = native_widget_type;
void set_scoped_task_environment(
std::unique_ptr<ScopedTaskEnvironment> scoped_task_environment) {
scoped_task_environment_ = std::move(scoped_task_environment);
void set_views_delegate(std::unique_ptr<TestViewsDelegate> views_delegate) {
// Returns a context view. In aura builds, this will be the
// RootWindow. Everywhere else, NULL.
gfx::NativeWindow GetContext();
bool is_mus() const {
return native_widget_type_ == NativeWidgetType::kDesktop;
// Factory for creating the native widget when |native_widget_type_| is set to
// kDesktop.
NativeWidget* CreateNativeWidgetForTest(
const Widget::InitParams& init_params,
internal::NativeWidgetDelegate* delegate);
// Controls what type of widget will be created by default for a test (i.e.
// when creating a Widget and leaving InitParams::native_widget unspecified).
// kDefault will allow the system default to be used (typically
// NativeWidgetAura on Aura). kDesktop forces DesktopNativeWidgetAura on Aura.
// There are exceptions, such as for modal dialog widgets, for which this
// value is ignored.
NativeWidgetType native_widget_type_ = NativeWidgetType::kDefault;
// Needed to make sure the InputDeviceManager is cleaned up between test runs.
std::unique_ptr<base::ShadowingAtExitManager> at_exit_manager_;
// Unlike on other platforms, |aura::Env| has to be created for each test and
// not as part of the test suite, because the type (LOCAL or MUS) depends on
// the individual test.
std::unique_ptr<aura::Env> env_;
std::unique_ptr<base::Thread> ipc_thread_;
std::unique_ptr<mojo::core::ScopedIPCSupport> ipc_support_;
base::test::ScopedFeatureList feature_list_;
std::unique_ptr<ScopedTaskEnvironment> scoped_task_environment_;
std::unique_ptr<TestViewsDelegate> views_delegate_for_setup_;
std::unique_ptr<ScopedViewsTestHelper> test_helper_;
bool interactive_setup_called_ = false;
bool setup_called_ = false;
bool teardown_called_ = false;
bool has_compositing_manager_ = false;
#if defined(OS_WIN)
ui::ScopedOleInitializer ole_initializer_;
// A helper that makes it easier to declare basic views tests that want to test
// desktop native widgets. See |ViewsTestBase::native_wiget_type_| and
// |ViewsTestBase::CreateNativeWidgetForTest|. In short, for Aura, this will
// result in most Widgets automatically being backed by a
// DesktopNativeWidgetAura. For Mac, it has no impact as a NativeWidgetMac is
// used either way.
class ViewsTestWithDesktopNativeWidget : public ViewsTestBase {
ViewsTestWithDesktopNativeWidget() = default;
~ViewsTestWithDesktopNativeWidget() override = default;
// ViewsTestBase:
void SetUp() override;
} // namespace views