1 // Copyright 2014 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/window/custom_frame_view.h"
9 #include "ui/views/controls/button/image_button.h"
10 #include "ui/views/test/views_test_base.h"
11 #include "ui/views/widget/widget.h"
12 #include "ui/views/widget/widget_delegate.h"
13 #include "ui/views/window/window_button_order_provider.h"
19 // Allows for the control of whether or not the widget can minimize/maximize or
20 // not. This can be set after initial setup in order to allow testing of both
21 // forms of delegates. By default this can minimize and maximize.
22 class MinimizeAndMaximizeStateControlDelegate
: public WidgetDelegateView
{
24 MinimizeAndMaximizeStateControlDelegate()
25 : can_maximize_(true),
26 can_minimize_(true) {}
27 ~MinimizeAndMaximizeStateControlDelegate() override
{}
29 void set_can_maximize(bool can_maximize
) {
30 can_maximize_
= can_maximize
;
33 void set_can_minimize(bool can_minimize
) {
34 can_minimize_
= can_minimize
;
38 bool CanMaximize() const override
{ return can_maximize_
; }
39 bool CanMinimize() const override
{ return can_minimize_
; }
45 DISALLOW_COPY_AND_ASSIGN(MinimizeAndMaximizeStateControlDelegate
);
50 class CustomFrameViewTest
: public ViewsTestBase
{
52 CustomFrameViewTest() {}
53 ~CustomFrameViewTest() override
{}
55 CustomFrameView
* custom_frame_view() {
56 return custom_frame_view_
;
59 MinimizeAndMaximizeStateControlDelegate
*
60 minimize_and_maximize_state_control_delegate() {
61 return minimize_and_maximize_state_control_delegate_
;
69 void SetUp() override
;
70 void TearDown() override
;
73 const std::vector
<views::FrameButton
>& leading_buttons() {
74 return WindowButtonOrderProvider::GetInstance()->leading_buttons();
77 const std::vector
<views::FrameButton
>& trailing_buttons() {
78 return WindowButtonOrderProvider::GetInstance()->trailing_buttons();
81 ImageButton
* minimize_button() {
82 return custom_frame_view_
->minimize_button_
;
85 ImageButton
* maximize_button() {
86 return custom_frame_view_
->maximize_button_
;
89 ImageButton
* restore_button() {
90 return custom_frame_view_
->restore_button_
;
93 ImageButton
* close_button() {
94 return custom_frame_view_
->close_button_
;
97 gfx::Rect
title_bounds() {
98 return custom_frame_view_
->title_bounds_
;
101 void SetWindowButtonOrder(
102 const std::vector
<views::FrameButton
> leading_buttons
,
103 const std::vector
<views::FrameButton
> trailing_buttons
);
106 // Parent container for |custom_frame_view_|
109 // Owned by |widget_|
110 CustomFrameView
* custom_frame_view_
;
112 // Delegate of |widget_| which controls minimizing and maximizing
113 MinimizeAndMaximizeStateControlDelegate
*
114 minimize_and_maximize_state_control_delegate_
;
116 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewTest
);
119 void CustomFrameViewTest::SetUp() {
120 ViewsTestBase::SetUp();
122 minimize_and_maximize_state_control_delegate_
=
123 new MinimizeAndMaximizeStateControlDelegate
;
124 widget_
= new Widget
;
125 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
126 params
.delegate
= minimize_and_maximize_state_control_delegate_
;
127 params
.remove_standard_frame
= true;
128 widget_
->Init(params
);
130 custom_frame_view_
= new CustomFrameView
;
131 widget_
->non_client_view()->SetFrameView(custom_frame_view_
);
134 void CustomFrameViewTest::TearDown() {
137 ViewsTestBase::TearDown();
140 void CustomFrameViewTest::SetWindowButtonOrder(
141 const std::vector
<views::FrameButton
> leading_buttons
,
142 const std::vector
<views::FrameButton
> trailing_buttons
) {
143 WindowButtonOrderProvider::GetInstance()->
144 SetWindowButtonOrder(leading_buttons
, trailing_buttons
);
147 // Tests that there is a default button ordering before initialization causes
148 // a configuration file check.
149 TEST_F(CustomFrameViewTest
, DefaultButtons
) {
150 const std::vector
<views::FrameButton
>& trailing
= trailing_buttons();
151 EXPECT_EQ(trailing
.size(), 3u);
152 EXPECT_TRUE(leading_buttons().empty());
153 EXPECT_EQ(trailing
[0], FRAME_BUTTON_MINIMIZE
);
154 EXPECT_EQ(trailing
[1], FRAME_BUTTON_MAXIMIZE
);
155 EXPECT_EQ(trailing
[2], FRAME_BUTTON_CLOSE
);
158 // Tests that layout places the buttons in order, that the restore button is
159 // hidden and the buttons are placed after the title.
160 TEST_F(CustomFrameViewTest
, DefaultButtonLayout
) {
161 Widget
* parent
= widget();
162 CustomFrameView
* view
= custom_frame_view();
164 parent
->SetBounds(gfx::Rect(0, 0, 300, 100));
167 EXPECT_LT(minimize_button()->x(), maximize_button()->x());
168 EXPECT_LT(maximize_button()->x(), close_button()->x());
169 EXPECT_FALSE(restore_button()->visible());
171 EXPECT_GT(minimize_button()->x(),
172 title_bounds().x() + title_bounds().width());
175 // Tests that setting the buttons to leading places them before the title.
176 TEST_F(CustomFrameViewTest
, LeadingButtonLayout
) {
177 Widget
* parent
= widget();
178 CustomFrameView
* view
= custom_frame_view();
180 std::vector
<views::FrameButton
> leading
;
181 leading
.push_back(views::FRAME_BUTTON_CLOSE
);
182 leading
.push_back(views::FRAME_BUTTON_MINIMIZE
);
183 leading
.push_back(views::FRAME_BUTTON_MAXIMIZE
);
185 std::vector
<views::FrameButton
> trailing
;
187 SetWindowButtonOrder(leading
, trailing
);
190 parent
->SetBounds(gfx::Rect(0, 0, 300, 100));
192 EXPECT_LT(close_button()->x(), minimize_button()->x());
193 EXPECT_LT(minimize_button()->x(), maximize_button()->x());
194 EXPECT_FALSE(restore_button()->visible());
195 EXPECT_LT(maximize_button()->x() + maximize_button()->width(),
199 // Tests that layouts occurring while maximized swap the maximize button for the
201 TEST_F(CustomFrameViewTest
, MaximizeRevealsRestoreButton
) {
202 Widget
* parent
= widget();
203 CustomFrameView
* view
= custom_frame_view();
205 parent
->SetBounds(gfx::Rect(0, 0, 300, 100));
208 ASSERT_FALSE(restore_button()->visible());
209 ASSERT_TRUE(maximize_button()->visible());
214 #if defined(OS_MACOSX)
215 // Restore buttons do not exist on Mac. The maximize button is instead a kind
216 // of toggle, but has no effect on frame decorations.
217 EXPECT_FALSE(restore_button()->visible());
218 EXPECT_TRUE(maximize_button()->visible());
220 EXPECT_TRUE(restore_button()->visible());
221 EXPECT_FALSE(maximize_button()->visible());
225 // Tests that when the parent cannot maximize that the maximize button is not
227 TEST_F(CustomFrameViewTest
, CannotMaximizeHidesButton
) {
228 Widget
* parent
= widget();
229 CustomFrameView
* view
= custom_frame_view();
230 MinimizeAndMaximizeStateControlDelegate
* delegate
=
231 minimize_and_maximize_state_control_delegate();
232 delegate
->set_can_maximize(false);
235 parent
->SetBounds(gfx::Rect(0, 0, 300, 100));
238 EXPECT_FALSE(restore_button()->visible());
239 EXPECT_FALSE(maximize_button()->visible());
242 // Tests that when the parent cannot minimize that the minimize button is not
244 TEST_F(CustomFrameViewTest
, CannotMinimizeHidesButton
) {
245 Widget
* parent
= widget();
246 CustomFrameView
* view
= custom_frame_view();
247 MinimizeAndMaximizeStateControlDelegate
* delegate
=
248 minimize_and_maximize_state_control_delegate();
249 delegate
->set_can_minimize(false);
252 parent
->SetBounds(gfx::Rect(0, 0, 300, 100));
255 EXPECT_FALSE(minimize_button()->visible());
258 // Tests that when maximized that the edge button has an increased width.
259 TEST_F(CustomFrameViewTest
, LargerEdgeButtonsWhenMaximized
) {
260 Widget
* parent
= widget();
261 CustomFrameView
* view
= custom_frame_view();
263 // Custom ordering to have a button on each edge.
264 std::vector
<views::FrameButton
> leading
;
265 leading
.push_back(views::FRAME_BUTTON_CLOSE
);
266 leading
.push_back(views::FRAME_BUTTON_MAXIMIZE
);
267 std::vector
<views::FrameButton
> trailing
;
268 trailing
.push_back(views::FRAME_BUTTON_MINIMIZE
);
269 SetWindowButtonOrder(leading
, trailing
);
272 parent
->SetBounds(gfx::Rect(0, 0, 300, 100));
275 gfx::Rect close_button_initial_bounds
= close_button()->bounds();
276 gfx::Rect minimize_button_initial_bounds
= minimize_button()->bounds();
281 #if defined(OS_MACOSX)
282 // On Mac, "Maximize" should not alter the frame. Only fullscreen does that.
283 EXPECT_EQ(close_button()->bounds().width(),
284 close_button_initial_bounds
.width());
285 EXPECT_EQ(minimize_button()->bounds().width(),
286 minimize_button_initial_bounds
.width());
288 EXPECT_GT(close_button()->bounds().width(),
289 close_button_initial_bounds
.width());
290 EXPECT_GT(minimize_button()->bounds().width(),
291 minimize_button_initial_bounds
.width());