1 // Copyright 2013 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 #ifndef UI_VIEWS_TEST_WIDGET_TEST_H_
6 #define UI_VIEWS_TEST_WIDGET_TEST_H_
8 #include "ui/gfx/native_widget_types.h"
9 #include "ui/views/test/views_test_base.h"
10 #include "ui/views/widget/widget_delegate.h"
13 #include "ui/views/widget/native_widget_aura.h"
14 #if !defined(OS_CHROMEOS)
15 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
17 #elif defined(OS_MACOSX)
18 #include "ui/views/widget/native_widget_mac.h"
23 class InputMethodDelegate
;
34 typedef NativeWidgetAura PlatformNativeWidget
;
35 #if !defined(OS_CHROMEOS)
36 typedef DesktopNativeWidgetAura PlatformDesktopNativeWidget
;
38 #elif defined(OS_MACOSX)
39 typedef NativeWidgetMac PlatformNativeWidget
;
40 typedef NativeWidgetMac PlatformDesktopNativeWidget
;
47 } // namespace internal
51 // A widget that assumes mouse capture always works. It won't on Aura in
52 // testing, so we mock it.
53 class NativeWidgetCapture
: public PlatformNativeWidget
{
55 explicit NativeWidgetCapture(internal::NativeWidgetDelegate
* delegate
);
56 ~NativeWidgetCapture() override
;
58 void SetCapture() override
;
59 void ReleaseCapture() override
;
60 bool HasCapture() const override
;
65 DISALLOW_COPY_AND_ASSIGN(NativeWidgetCapture
);
68 class WidgetTest
: public ViewsTestBase
{
70 // Scoped handle that fakes all widgets into claiming they are active. This
71 // allows a test to assume active status does not get stolen by a test that
72 // may be running in parallel. It shouldn't be used in tests that create
74 class FakeActivation
{
76 virtual ~FakeActivation() {}
82 DISALLOW_COPY_AND_ASSIGN(FakeActivation
);
86 ~WidgetTest() override
;
88 // Create Widgets with |native_widget| in InitParams set to an instance of
89 // test::NativeWidgetCapture.
90 Widget
* CreateTopLevelPlatformWidget();
91 Widget
* CreateTopLevelFramelessPlatformWidget();
92 Widget
* CreateChildPlatformWidget(gfx::NativeView parent_native_view
);
94 // Create Widgets initialized without a |native_widget| set in InitParams.
95 // Depending on the test environment, ViewsDelegate::OnBeforeWidgetInit() may
97 Widget
* CreateTopLevelNativeWidget();
98 Widget
* CreateChildNativeWidgetWithParent(Widget
* parent
);
99 Widget
* CreateChildNativeWidget();
101 // Create a top-level Widget with |native_widget| in InitParams set to an
102 // instance of the "native desktop" type. This is a PlatformNativeWidget on
103 // ChromeOS, and a PlatformDesktopNativeWidget everywhere else.
104 Widget
* CreateNativeDesktopWidget();
106 View
* GetMousePressedHandler(internal::RootView
* root_view
);
108 View
* GetMouseMoveHandler(internal::RootView
* root_view
);
110 View
* GetGestureHandler(internal::RootView
* root_view
);
112 // Simulate an OS-level destruction of the native window held by |widget|.
113 static void SimulateNativeDestroy(Widget
* widget
);
115 // Simulate an activation of the native window held by |widget|, as if it was
116 // clicked by the user. This is a synchronous method for use in
117 // non-interactive tests that do not spin a RunLoop in the test body (since
118 // that may cause real focus changes to flakily manifest).
119 static void SimulateNativeActivate(Widget
* widget
);
121 // Return true if |window| is visible according to the native platform.
122 static bool IsNativeWindowVisible(gfx::NativeWindow window
);
124 // Return true if |above| is higher than |below| in the native window Z-order.
125 // Both windows must be visible.
126 static bool IsWindowStackedAbove(Widget
* above
, Widget
* below
);
128 // Query the native window system for the minimum size configured for user
129 // initiated window resizes.
130 static gfx::Size
GetNativeWidgetMinimumContentSize(Widget
* widget
);
132 // Return the event processor for |widget|. On aura platforms, this is an
133 // aura::WindowEventDispatcher. Otherwise, it is a bridge to the OS event
135 static ui::EventProcessor
* GetEventProcessor(Widget
* widget
);
137 // Get the InputMethodDelegate, for setting on a Mock InputMethod in tests.
138 static ui::internal::InputMethodDelegate
* GetInputMethodDelegateForWidget(
141 #if defined(OS_MACOSX)
142 static scoped_ptr
<FakeActivation
> FakeWidgetIsActiveAlways();
146 DISALLOW_COPY_AND_ASSIGN(WidgetTest
);
149 // A helper WidgetDelegate for tests that require hooks into WidgetDelegate
150 // calls, and removes some of the boilerplate for initializing a Widget. Calls
151 // Widget::CloseNow() when destroyed if it hasn't already been done.
152 class TestDesktopWidgetDelegate
: public WidgetDelegate
{
154 TestDesktopWidgetDelegate();
155 ~TestDesktopWidgetDelegate() override
;
157 // Initialize the Widget, adding some meaningful default InitParams.
158 void InitWidget(Widget::InitParams init_params
);
160 // Set the contents view to be used during Widget initialization. For Widgets
161 // that use non-client views, this will be the contents_view used to
162 // initialize the ClientView in WidgetDelegate::CreateClientView(). Otherwise,
163 // it is the ContentsView of the Widget's RootView. Ownership passes to the
164 // view hierarchy during InitWidget().
165 void set_contents_view(View
* contents_view
) {
166 contents_view_
= contents_view
;
169 int window_closing_count() const { return window_closing_count_
; }
170 const gfx::Rect
& initial_bounds() { return initial_bounds_
; }
172 // WidgetDelegate overrides:
173 void WindowClosing() override
;
174 Widget
* GetWidget() override
;
175 const Widget
* GetWidget() const override
;
176 View
* GetContentsView() override
;
177 bool ShouldAdvanceFocusToTopLevelWidget() const override
;
181 View
* contents_view_
= nullptr;
182 int window_closing_count_
= 0;
183 gfx::Rect initial_bounds_
= gfx::Rect(100, 100, 200, 200);
185 DISALLOW_COPY_AND_ASSIGN(TestDesktopWidgetDelegate
);
191 #endif // UI_VIEWS_TEST_WIDGET_TEST_H_