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 "ash/frame/caption_buttons/frame_caption_button_container_view.h"
7 #include "ash/frame/caption_buttons/frame_caption_button.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
11 #include "grit/ash_resources.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/widget/widget_delegate.h"
20 class TestWidgetDelegate
: public views::WidgetDelegateView
{
22 TestWidgetDelegate(bool can_maximize
, bool can_minimize
)
23 : can_maximize_(can_maximize
), can_minimize_(can_minimize
) {}
24 ~TestWidgetDelegate() override
{}
26 bool CanMaximize() const override
{ return can_maximize_
; }
28 bool CanMinimize() const override
{ return can_minimize_
; }
34 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate
);
39 class FrameCaptionButtonContainerViewTest
: public ash::test::AshTestBase
{
41 enum MaximizeAllowed
{
46 enum MinimizeAllowed
{
51 FrameCaptionButtonContainerViewTest() {
54 ~FrameCaptionButtonContainerViewTest() override
{}
56 // Creates a widget which allows maximizing based on |maximize_allowed|.
57 // The caller takes ownership of the returned widget.
58 views::Widget
* CreateTestWidget(
59 MaximizeAllowed maximize_allowed
,
60 MinimizeAllowed minimize_allowed
) WARN_UNUSED_RESULT
{
61 views::Widget
* widget
= new views::Widget
;
62 views::Widget::InitParams params
;
63 params
.delegate
= new TestWidgetDelegate(
64 maximize_allowed
== MAXIMIZE_ALLOWED
,
65 minimize_allowed
== MINIMIZE_ALLOWED
);
66 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
67 params
.context
= CurrentContext();
72 // Sets |container| to use arbitrary images for the buttons. Setting the
73 // images causes the buttons to have non-empty sizes.
74 void SetMockImages(FrameCaptionButtonContainerView
* container
) {
75 for (int icon
= 0; icon
< CAPTION_BUTTON_ICON_COUNT
; ++icon
) {
76 container
->SetButtonImages(
77 static_cast<CaptionButtonIcon
>(icon
),
78 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE
,
79 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H
,
80 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P
);
84 // Tests that |leftmost| and |rightmost| are at |container|'s edges.
85 bool CheckButtonsAtEdges(FrameCaptionButtonContainerView
* container
,
86 const ash::FrameCaptionButton
& leftmost
,
87 const ash::FrameCaptionButton
& rightmost
) {
88 gfx::Rect
expected(container
->GetPreferredSize());
90 gfx::Rect
container_size(container
->GetPreferredSize());
91 if (leftmost
.y() == rightmost
.y() &&
92 leftmost
.height() == rightmost
.height() &&
93 leftmost
.x() == expected
.x() &&
94 leftmost
.y() == expected
.y() &&
95 leftmost
.height() == expected
.height() &&
96 rightmost
.bounds().right() == expected
.right()) {
100 LOG(ERROR
) << "Buttons " << leftmost
.bounds().ToString() << " "
101 << rightmost
.bounds().ToString() << " not at edges of "
102 << expected
.ToString();
107 DISALLOW_COPY_AND_ASSIGN(FrameCaptionButtonContainerViewTest
);
110 // Test how the allowed actions affect which caption buttons are visible.
111 TEST_F(FrameCaptionButtonContainerViewTest
, ButtonVisibility
) {
112 // All the buttons should be visible when minimizing and maximizing are
114 FrameCaptionButtonContainerView
container1(
115 CreateTestWidget(MAXIMIZE_ALLOWED
, MINIMIZE_ALLOWED
));
116 SetMockImages(&container1
);
118 FrameCaptionButtonContainerView::TestApi
t1(&container1
);
119 EXPECT_TRUE(t1
.minimize_button()->visible());
120 EXPECT_TRUE(t1
.size_button()->visible());
121 EXPECT_TRUE(t1
.close_button()->visible());
122 EXPECT_TRUE(CheckButtonsAtEdges(
123 &container1
, *t1
.minimize_button(), *t1
.close_button()));
125 // The minimize button should be visible when minimizing is allowed but
126 // maximizing is disallowed.
127 FrameCaptionButtonContainerView
container2(
128 CreateTestWidget(MAXIMIZE_DISALLOWED
, MINIMIZE_ALLOWED
));
129 SetMockImages(&container2
);
131 FrameCaptionButtonContainerView::TestApi
t2(&container2
);
132 EXPECT_TRUE(t2
.minimize_button()->visible());
133 EXPECT_FALSE(t2
.size_button()->visible());
134 EXPECT_TRUE(t2
.close_button()->visible());
135 EXPECT_TRUE(CheckButtonsAtEdges(
136 &container2
, *t2
.minimize_button(), *t2
.close_button()));
138 // Neither the minimize button nor the size button should be visible when
139 // neither minimizing nor maximizing are allowed.
140 FrameCaptionButtonContainerView
container3(
141 CreateTestWidget(MAXIMIZE_DISALLOWED
, MINIMIZE_DISALLOWED
));
142 SetMockImages(&container3
);
144 FrameCaptionButtonContainerView::TestApi
t3(&container3
);
145 EXPECT_FALSE(t3
.minimize_button()->visible());
146 EXPECT_FALSE(t3
.size_button()->visible());
147 EXPECT_TRUE(t3
.close_button()->visible());
148 EXPECT_TRUE(CheckButtonsAtEdges(
149 &container3
, *t3
.close_button(), *t3
.close_button()));
152 // Tests that the layout animations trigered by button visibility result in the
153 // correct placement of the buttons.
154 TEST_F(FrameCaptionButtonContainerViewTest
,
155 TestUpdateSizeButtonVisibilityAnimation
) {
156 FrameCaptionButtonContainerView
container(
157 CreateTestWidget(MAXIMIZE_ALLOWED
, MINIMIZE_ALLOWED
));
158 SetMockImages(&container
);
159 container
.SetBoundsRect(gfx::Rect(container
.GetPreferredSize()));
162 FrameCaptionButtonContainerView::TestApi
test(&container
);
163 gfx::Rect initial_minimize_button_bounds
= test
.minimize_button()->bounds();
164 gfx::Rect initial_size_button_bounds
= test
.size_button()->bounds();
165 gfx::Rect initial_close_button_bounds
= test
.close_button()->bounds();
166 gfx::Rect initial_container_bounds
= container
.bounds();
168 ASSERT_EQ(initial_size_button_bounds
.x(),
169 initial_minimize_button_bounds
.right());
170 ASSERT_EQ(initial_close_button_bounds
.x(),
171 initial_size_button_bounds
.right());
173 // Hidden size button should result in minimize button animating to the
174 // right. The size button should not be visible, but should not have moved.
175 Shell::GetInstance()->maximize_mode_controller()->
176 EnableMaximizeModeWindowManager(true);
177 container
.UpdateSizeButtonVisibility();
178 test
.EndAnimations();
179 // Parent needs to layout in response to size change.
182 EXPECT_TRUE(test
.minimize_button()->visible());
183 EXPECT_FALSE(test
.size_button()->visible());
184 EXPECT_TRUE(test
.close_button()->visible());
185 gfx::Rect minimize_button_bounds
= test
.minimize_button()->bounds();
186 gfx::Rect close_button_bounds
= test
.close_button()->bounds();
187 EXPECT_EQ(close_button_bounds
.x(), minimize_button_bounds
.right());
188 EXPECT_EQ(initial_size_button_bounds
, test
.size_button()->bounds());
189 EXPECT_EQ(initial_close_button_bounds
.size(), close_button_bounds
.size());
190 EXPECT_LT(container
.GetPreferredSize().width(),
191 initial_container_bounds
.width());
193 // Revealing the size button should cause the minimize button to return to its
194 // original position.
195 Shell::GetInstance()->maximize_mode_controller()->
196 EnableMaximizeModeWindowManager(false);
197 container
.UpdateSizeButtonVisibility();
198 // Calling code needs to layout in response to size change.
200 test
.EndAnimations();
201 EXPECT_TRUE(test
.minimize_button()->visible());
202 EXPECT_TRUE(test
.size_button()->visible());
203 EXPECT_TRUE(test
.close_button()->visible());
204 EXPECT_EQ(initial_minimize_button_bounds
, test
.minimize_button()->bounds());
205 EXPECT_EQ(initial_size_button_bounds
, test
.size_button()->bounds());
206 EXPECT_EQ(initial_close_button_bounds
, test
.close_button()->bounds());
207 EXPECT_EQ(container
.GetPreferredSize().width(),
208 initial_container_bounds
.width());