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 explicit TestWidgetDelegate(bool can_maximize
) : can_maximize_(can_maximize
) {
24 virtual ~TestWidgetDelegate() {
27 virtual bool CanMaximize() const OVERRIDE
{
34 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate
);
39 class FrameCaptionButtonContainerViewTest
: public ash::test::AshTestBase
{
41 enum MaximizeAllowed
{
46 FrameCaptionButtonContainerViewTest() {
49 virtual ~FrameCaptionButtonContainerViewTest() {
52 // Creates a widget which allows maximizing based on |maximize_allowed|.
53 // The caller takes ownership of the returned widget.
54 views::Widget
* CreateTestWidget(
55 MaximizeAllowed maximize_allowed
) WARN_UNUSED_RESULT
{
56 views::Widget
* widget
= new views::Widget
;
57 views::Widget::InitParams params
;
58 params
.delegate
= new TestWidgetDelegate(
59 maximize_allowed
== MAXIMIZE_ALLOWED
);
60 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
61 params
.context
= CurrentContext();
66 // Sets |container| to use arbitrary images for the buttons. Setting the
67 // images causes the buttons to have non-empty sizes.
68 void SetMockImages(FrameCaptionButtonContainerView
* container
) {
69 for (int icon
= 0; icon
< CAPTION_BUTTON_ICON_COUNT
; ++icon
) {
70 container
->SetButtonImages(
71 static_cast<CaptionButtonIcon
>(icon
),
72 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE
,
73 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE_I
,
74 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H
,
75 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P
);
79 // Tests that |leftmost| and |rightmost| are at |container|'s edges.
80 bool CheckButtonsAtEdges(FrameCaptionButtonContainerView
* container
,
81 const ash::FrameCaptionButton
& leftmost
,
82 const ash::FrameCaptionButton
& rightmost
) {
83 gfx::Rect
expected(container
->GetPreferredSize());
85 gfx::Rect
container_size(container
->GetPreferredSize());
86 if (leftmost
.y() == rightmost
.y() &&
87 leftmost
.height() == rightmost
.height() &&
88 leftmost
.x() == expected
.x() &&
89 leftmost
.y() == expected
.y() &&
90 leftmost
.height() == expected
.height() &&
91 rightmost
.bounds().right() == expected
.right()) {
95 LOG(ERROR
) << "Buttons " << leftmost
.bounds().ToString() << " "
96 << rightmost
.bounds().ToString() << " not at edges of "
97 << expected
.ToString();
102 DISALLOW_COPY_AND_ASSIGN(FrameCaptionButtonContainerViewTest
);
105 // Test how the allowed actions affect which caption buttons are visible.
106 TEST_F(FrameCaptionButtonContainerViewTest
, ButtonVisibility
) {
107 // All the buttons should be visible when minimizing and maximizing are
109 scoped_ptr
<views::Widget
> widget_can_maximize(
110 CreateTestWidget(MAXIMIZE_ALLOWED
));
111 FrameCaptionButtonContainerView
container1(widget_can_maximize
.get(),
112 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED
);
113 SetMockImages(&container1
);
115 FrameCaptionButtonContainerView::TestApi
t1(&container1
);
116 EXPECT_TRUE(t1
.minimize_button()->visible());
117 EXPECT_TRUE(t1
.size_button()->visible());
118 EXPECT_TRUE(t1
.close_button()->visible());
119 EXPECT_TRUE(CheckButtonsAtEdges(
120 &container1
, *t1
.minimize_button(), *t1
.close_button()));
122 // The minimize button should be visible when minimizing is allowed but
123 // maximizing is disallowed.
124 scoped_ptr
<views::Widget
> widget_cannot_maximize(
125 CreateTestWidget(MAXIMIZE_DISALLOWED
));
126 FrameCaptionButtonContainerView
container2(widget_cannot_maximize
.get(),
127 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED
);
128 SetMockImages(&container2
);
130 FrameCaptionButtonContainerView::TestApi
t2(&container2
);
131 EXPECT_TRUE(t2
.minimize_button()->visible());
132 EXPECT_FALSE(t2
.size_button()->visible());
133 EXPECT_TRUE(t2
.close_button()->visible());
134 EXPECT_TRUE(CheckButtonsAtEdges(
135 &container2
, *t2
.minimize_button(), *t2
.close_button()));
137 // Neither the minimize button nor the size button should be visible when
138 // neither minimizing nor maximizing are allowed.
139 FrameCaptionButtonContainerView
container3(widget_cannot_maximize
.get(),
140 FrameCaptionButtonContainerView::MINIMIZE_DISALLOWED
);
141 SetMockImages(&container3
);
143 FrameCaptionButtonContainerView::TestApi
t3(&container3
);
144 EXPECT_FALSE(t3
.minimize_button()->visible());
145 EXPECT_FALSE(t3
.size_button()->visible());
146 EXPECT_TRUE(t3
.close_button()->visible());
147 EXPECT_TRUE(CheckButtonsAtEdges(
148 &container3
, *t3
.close_button(), *t3
.close_button()));
151 // Tests that the layout animations trigered by button visibility result in the
152 // correct placement of the buttons.
153 TEST_F(FrameCaptionButtonContainerViewTest
,
154 TestUpdateSizeButtonVisibilityAnimation
) {
155 scoped_ptr
<views::Widget
> widget_can_maximize(
156 CreateTestWidget(MAXIMIZE_ALLOWED
));
157 FrameCaptionButtonContainerView
container(widget_can_maximize
.get(),
158 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED
);
159 SetMockImages(&container
);
160 container
.SetBoundsRect(gfx::Rect(container
.GetPreferredSize()));
163 FrameCaptionButtonContainerView::TestApi
test(&container
);
164 gfx::Rect initial_minimize_button_bounds
= test
.minimize_button()->bounds();
165 gfx::Rect initial_size_button_bounds
= test
.size_button()->bounds();
166 gfx::Rect initial_close_button_bounds
= test
.close_button()->bounds();
167 gfx::Rect initial_container_bounds
= container
.bounds();
169 ASSERT_EQ(initial_size_button_bounds
.x(),
170 initial_minimize_button_bounds
.right());
171 ASSERT_EQ(initial_close_button_bounds
.x(),
172 initial_size_button_bounds
.right());
174 // Hidden size button should result in minimize button animating to the
175 // right. The size button should not be visible, but should not have moved.
176 Shell::GetInstance()->maximize_mode_controller()->
177 EnableMaximizeModeWindowManager(true);
178 container
.UpdateSizeButtonVisibility();
179 test
.EndAnimations();
180 // Parent needs to layout in response to size change.
183 EXPECT_TRUE(test
.minimize_button()->visible());
184 EXPECT_FALSE(test
.size_button()->visible());
185 EXPECT_TRUE(test
.close_button()->visible());
186 gfx::Rect minimize_button_bounds
= test
.minimize_button()->bounds();
187 gfx::Rect close_button_bounds
= test
.close_button()->bounds();
188 EXPECT_EQ(close_button_bounds
.x(), minimize_button_bounds
.right());
189 EXPECT_EQ(initial_size_button_bounds
, test
.size_button()->bounds());
190 EXPECT_EQ(initial_close_button_bounds
.size(), close_button_bounds
.size());
191 EXPECT_LT(container
.GetPreferredSize().width(),
192 initial_container_bounds
.width());
194 // Revealing the size button should cause the minimize button to return to its
195 // original position.
196 Shell::GetInstance()->maximize_mode_controller()->
197 EnableMaximizeModeWindowManager(false);
198 container
.UpdateSizeButtonVisibility();
199 // Calling code needs to layout in response to size change.
201 test
.EndAnimations();
202 EXPECT_TRUE(test
.minimize_button()->visible());
203 EXPECT_TRUE(test
.size_button()->visible());
204 EXPECT_TRUE(test
.close_button()->visible());
205 EXPECT_EQ(initial_minimize_button_bounds
, test
.minimize_button()->bounds());
206 EXPECT_EQ(initial_size_button_bounds
, test
.size_button()->bounds());
207 EXPECT_EQ(initial_close_button_bounds
, test
.close_button()->bounds());
208 EXPECT_EQ(container
.GetPreferredSize().width(),
209 initial_container_bounds
.width());