Automated Commit: Committing new LKGM version 7479.0.0 for chromeos.
[chromium-blink-merge.git] / ui / views / test / widget_test.cc
blob66cf6e55777e428fe09b0139e7f951fbf1fcdb1a
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"
10 namespace views {
11 namespace test {
12 namespace {
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);
19 widget->Init(params);
20 return widget;
23 Widget* CreateHelper(Widget::InitParams params) {
24 return CreateHelper<NativeWidgetCapture>(params);
27 } // namespace
29 // A widget that assumes mouse capture always works. It won't in testing, so we
30 // mock it.
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() {
43 if (mouse_capture_)
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);
69 return child;
72 Widget* WidgetTest::CreateTopLevelNativeWidget() {
73 Widget* toplevel = new Widget;
74 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
75 toplevel->Init(params);
76 return toplevel;
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();
83 child->Init(params);
84 child->SetContentsView(new View);
85 return child;
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));
96 #else
97 return CreateHelper<PlatformDesktopNativeWidget>(
98 CreateParams(Widget::InitParams::TYPE_WINDOW));
99 #endif
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() {
118 if (widget_)
119 widget_->CloseNow();
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_);
127 #endif
128 init_params.bounds = initial_bounds_;
129 widget_->Init(init_params);
132 void TestDesktopWidgetDelegate::WindowClosing() {
133 window_closing_count_++;
134 widget_ = nullptr;
137 Widget* TestDesktopWidgetDelegate::GetWidget() {
138 return widget_;
141 const Widget* TestDesktopWidgetDelegate::GetWidget() const {
142 return widget_;
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.
153 } // namespace test
154 } // namespace views