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 "ash/frame/custom_frame_view_ash.h"
7 #include "ash/frame/caption_buttons/frame_caption_button.h"
8 #include "ash/frame/caption_buttons/frame_caption_button_container_view.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ash/test/test_session_state_delegate.h"
12 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "grit/ash_resources.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/image/image_skia.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/views/widget/widget_delegate.h"
23 // A views::WidgetDelegate which uses a CustomFrameViewAsh.
24 class TestWidgetDelegate
: public views::WidgetDelegateView
{
26 TestWidgetDelegate() {}
27 virtual ~TestWidgetDelegate() {}
29 virtual views::NonClientFrameView
* CreateNonClientFrameView(
30 views::Widget
* widget
) override
{
31 custom_frame_view_
= new CustomFrameViewAsh(widget
);
32 return custom_frame_view_
;
35 CustomFrameViewAsh
* custom_frame_view() const {
36 return custom_frame_view_
;
41 CustomFrameViewAsh
* custom_frame_view_
;
43 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate
);
46 class TestWidgetConstraintsDelegate
: public TestWidgetDelegate
{
48 TestWidgetConstraintsDelegate() {}
49 virtual ~TestWidgetConstraintsDelegate() {}
52 virtual gfx::Size
GetMinimumSize() const override
{
56 virtual gfx::Size
GetMaximumSize() const override
{
60 virtual views::View
* GetContentsView() override
{
61 // Set this instance as the contents view so that the maximum and minimum
62 // size constraints will be used.
66 // views::WidgetDelegate:
67 virtual bool CanMaximize() const override
{
71 virtual bool CanMinimize() const override
{
75 void set_minimum_size(const gfx::Size
& min_size
) {
76 minimum_size_
= min_size
;
79 void set_maximum_size(const gfx::Size
& max_size
) {
80 maximum_size_
= max_size
;
83 const gfx::Rect
& GetFrameCaptionButtonContainerViewBounds() {
84 return custom_frame_view()->GetFrameCaptionButtonContainerViewForTest()->
88 void EndFrameCaptionButtonContainerViewAnimations() {
89 FrameCaptionButtonContainerView::TestApi
test(custom_frame_view()->
90 GetFrameCaptionButtonContainerViewForTest());
94 int GetTitleBarHeight() const {
95 return custom_frame_view()->NonClientTopBorderHeight();
99 gfx::Size minimum_size_
;
100 gfx::Size maximum_size_
;
102 DISALLOW_COPY_AND_ASSIGN(TestWidgetConstraintsDelegate
);
105 class CustomFrameViewAshTest
: public test::AshTestBase
{
107 CustomFrameViewAshTest() {}
108 virtual ~CustomFrameViewAshTest() {}
111 scoped_ptr
<views::Widget
> CreateWidget(TestWidgetDelegate
* delegate
) {
112 scoped_ptr
<views::Widget
> widget(new views::Widget
);
113 views::Widget::InitParams params
;
114 params
.delegate
= delegate
;
115 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
116 params
.bounds
= gfx::Rect(0, 0, 100, 100);
117 params
.context
= CurrentContext();
118 widget
->Init(params
);
119 return widget
.Pass();
122 test::TestSessionStateDelegate
* GetTestSessionStateDelegate() {
123 return static_cast<ash::test::TestSessionStateDelegate
*>(
124 Shell::GetInstance()->session_state_delegate());
128 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewAshTest
);
131 // Test that the height of the header is correct upon initially displaying
133 TEST_F(CustomFrameViewAshTest
, HeaderHeight
) {
134 TestWidgetDelegate
* delegate
= new TestWidgetDelegate
;
136 scoped_ptr
<views::Widget
> widget(CreateWidget(delegate
));
139 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
140 gfx::ImageSkia
* close_button
=
141 rb
.GetImageSkiaNamed(IDR_AURA_WINDOW_CONTROL_BACKGROUND_H
);
143 // The header should have enough room for the window controls. The
144 // header/content separator line overlays the window controls.
145 EXPECT_EQ(close_button
->height(),
146 delegate
->custom_frame_view()->GetHeaderView()->height());
149 // Verify that CustomFrameViewAsh returns the correct minimum and maximum frame
150 // sizes when the client view does not specify any size constraints.
151 TEST_F(CustomFrameViewAshTest
, NoSizeConstraints
) {
152 TestWidgetConstraintsDelegate
* delegate
= new TestWidgetConstraintsDelegate
;
153 scoped_ptr
<views::Widget
> widget(CreateWidget(delegate
));
155 CustomFrameViewAsh
* custom_frame_view
= delegate
->custom_frame_view();
156 gfx::Size min_frame_size
= custom_frame_view
->GetMinimumSize();
157 gfx::Size max_frame_size
= custom_frame_view
->GetMaximumSize();
159 EXPECT_EQ(delegate
->GetTitleBarHeight(), min_frame_size
.height());
161 // A width and height constraint of 0 denotes unbounded.
162 EXPECT_EQ(0, max_frame_size
.width());
163 EXPECT_EQ(0, max_frame_size
.height());
166 // Verify that CustomFrameViewAsh returns the correct minimum and maximum frame
167 // sizes when the client view specifies size constraints.
168 TEST_F(CustomFrameViewAshTest
, MinimumAndMaximumSize
) {
169 gfx::Size
min_client_size(500, 500);
170 gfx::Size
max_client_size(800, 800);
171 TestWidgetConstraintsDelegate
* delegate
= new TestWidgetConstraintsDelegate
;
172 delegate
->set_minimum_size(min_client_size
);
173 delegate
->set_maximum_size(max_client_size
);
174 scoped_ptr
<views::Widget
> widget(CreateWidget(delegate
));
176 CustomFrameViewAsh
* custom_frame_view
= delegate
->custom_frame_view();
177 gfx::Size min_frame_size
= custom_frame_view
->GetMinimumSize();
178 gfx::Size max_frame_size
= custom_frame_view
->GetMaximumSize();
180 EXPECT_EQ(min_client_size
.width(), min_frame_size
.width());
181 EXPECT_EQ(max_client_size
.width(), max_frame_size
.width());
182 EXPECT_EQ(min_client_size
.height() + delegate
->GetTitleBarHeight(),
183 min_frame_size
.height());
184 EXPECT_EQ(max_client_size
.height() + delegate
->GetTitleBarHeight(),
185 max_frame_size
.height());
188 // Verify that CustomFrameViewAsh updates the avatar icon based on the
189 // state of the SessionStateDelegate after visibility change.
190 TEST_F(CustomFrameViewAshTest
, AvatarIcon
) {
191 TestWidgetConstraintsDelegate
* delegate
= new TestWidgetConstraintsDelegate
;
192 scoped_ptr
<views::Widget
> widget(CreateWidget(delegate
));
194 CustomFrameViewAsh
* custom_frame_view
= delegate
->custom_frame_view();
195 EXPECT_FALSE(custom_frame_view
->GetAvatarIconViewForTest());
197 // Avatar image becomes available.
198 const gfx::ImageSkia user_image
=
199 *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
200 IDR_AURA_UBER_TRAY_GUEST_ICON
);
201 GetTestSessionStateDelegate()->SetUserImage(user_image
);
204 EXPECT_TRUE(custom_frame_view
->GetAvatarIconViewForTest());
206 // Avatar image is gone; the ImageView for the avatar icon should be
208 GetTestSessionStateDelegate()->SetUserImage(gfx::ImageSkia());
211 EXPECT_FALSE(custom_frame_view
->GetAvatarIconViewForTest());
214 // The visibility of the size button is updated when maximize mode is toggled.
215 // Verify that the layout of the HeaderView is updated for the size button's
217 TEST_F(CustomFrameViewAshTest
, HeaderViewNotifiedOfChildSizeChange
) {
218 TestWidgetConstraintsDelegate
* delegate
= new TestWidgetConstraintsDelegate
;
219 scoped_ptr
<views::Widget
> widget(CreateWidget(delegate
));
221 const gfx::Rect initial
= delegate
->
222 GetFrameCaptionButtonContainerViewBounds();
223 Shell::GetInstance()->maximize_mode_controller()->
224 EnableMaximizeModeWindowManager(true);
225 delegate
->EndFrameCaptionButtonContainerViewAnimations();
226 const gfx::Rect maximize_mode_bounds
= delegate
->
227 GetFrameCaptionButtonContainerViewBounds();
228 EXPECT_GT(initial
.width(), maximize_mode_bounds
.width());
229 Shell::GetInstance()->maximize_mode_controller()->
230 EnableMaximizeModeWindowManager(false);
231 delegate
->EndFrameCaptionButtonContainerViewAnimations();
232 const gfx::Rect after_restore
= delegate
->
233 GetFrameCaptionButtonContainerViewBounds();
234 EXPECT_EQ(initial
, after_restore
);