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 #include "ui/views/test/widget_test.h"
7 #include "ui/gfx/native_widget_types.h"
8 #include "ui/views/widget/root_view.h"
14 // Helper to encapsulate common parts of the WidgetTest::Create* methods,
15 template <class NativeWidgetType
>
16 Widget
* CreateHelper(Widget::InitParams params
) {
17 Widget
* widget
= new Widget
;
18 params
.native_widget
= new NativeWidgetType(widget
);
23 Widget
* CreateHelper(Widget::InitParams params
) {
24 return CreateHelper
<NativeWidgetCapture
>(params
);
29 // A widget that assumes mouse capture always works. It won't in testing, so we
31 NativeWidgetCapture::NativeWidgetCapture(
32 internal::NativeWidgetDelegate
* delegate
)
33 : PlatformNativeWidget(delegate
),
34 mouse_capture_(false) {}
36 NativeWidgetCapture::~NativeWidgetCapture() {}
38 void NativeWidgetCapture::SetCapture() {
39 mouse_capture_
= true;
42 void NativeWidgetCapture::ReleaseCapture() {
44 delegate()->OnMouseCaptureLost();
45 mouse_capture_
= false;
48 bool NativeWidgetCapture::HasCapture() const {
49 return mouse_capture_
;
52 WidgetTest::WidgetTest() {}
53 WidgetTest::~WidgetTest() {}
55 Widget
* WidgetTest::CreateTopLevelPlatformWidget() {
56 return CreateHelper(CreateParams(Widget::InitParams::TYPE_WINDOW
));
59 Widget
* WidgetTest::CreateTopLevelFramelessPlatformWidget() {
60 return CreateHelper(CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS
));
63 Widget
* WidgetTest::CreateChildPlatformWidget(
64 gfx::NativeView parent_native_view
) {
65 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_CONTROL
);
66 params
.parent
= parent_native_view
;
67 Widget
* child
= CreateHelper(params
);
68 child
->SetContentsView(new View
);
72 Widget
* WidgetTest::CreateTopLevelNativeWidget() {
73 Widget
* toplevel
= new Widget
;
74 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
75 toplevel
->Init(params
);
79 Widget
* WidgetTest::CreateChildNativeWidgetWithParent(Widget
* parent
) {
80 Widget
* child
= new Widget
;
81 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_CONTROL
);
82 params
.parent
= parent
->GetNativeView();
84 child
->SetContentsView(new View
);
88 Widget
* WidgetTest::CreateChildNativeWidget() {
89 return CreateChildNativeWidgetWithParent(NULL
);
92 Widget
* WidgetTest::CreateNativeDesktopWidget() {
93 #if defined(OS_CHROMEOS)
94 return CreateHelper
<PlatformNativeWidget
>(
95 CreateParams(Widget::InitParams::TYPE_WINDOW
));
97 return CreateHelper
<PlatformDesktopNativeWidget
>(
98 CreateParams(Widget::InitParams::TYPE_WINDOW
));
102 View
* WidgetTest::GetMousePressedHandler(internal::RootView
* root_view
) {
103 return root_view
->mouse_pressed_handler_
;
106 View
* WidgetTest::GetMouseMoveHandler(internal::RootView
* root_view
) {
107 return root_view
->mouse_move_handler_
;
110 View
* WidgetTest::GetGestureHandler(internal::RootView
* root_view
) {
111 return root_view
->gesture_handler_
;
114 TestDesktopWidgetDelegate::TestDesktopWidgetDelegate() : widget_(new Widget
) {
117 TestDesktopWidgetDelegate::~TestDesktopWidgetDelegate() {
120 EXPECT_FALSE(widget_
);
123 void TestDesktopWidgetDelegate::InitWidget(Widget::InitParams init_params
) {
124 init_params
.delegate
= this;
125 #if !defined(OS_CHROMEOS)
126 init_params
.native_widget
= new PlatformDesktopNativeWidget(widget_
);
128 init_params
.bounds
= initial_bounds_
;
129 widget_
->Init(init_params
);
132 void TestDesktopWidgetDelegate::WindowClosing() {
133 window_closing_count_
++;
137 Widget
* TestDesktopWidgetDelegate::GetWidget() {
141 const Widget
* TestDesktopWidgetDelegate::GetWidget() const {
145 View
* TestDesktopWidgetDelegate::GetContentsView() {
146 return contents_view_
? contents_view_
: WidgetDelegate::GetContentsView();
149 bool TestDesktopWidgetDelegate::ShouldAdvanceFocusToTopLevelWidget() const {
150 return true; // Same default as DefaultWidgetDelegate in widget.cc.