Make it clear that WeakPtrFactory is the last data member
[chromium-blink-merge.git] / ash / frame / custom_frame_view_ash_unittest.cc
blob3bcf91286a15a64d267701a45b89141a9eeae6df
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"
9 #include "ash/shell.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"
21 namespace ash {
23 // A views::WidgetDelegate which uses a CustomFrameViewAsh.
24 class TestWidgetDelegate : public views::WidgetDelegateView {
25 public:
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_;
39 private:
40 // Not owned.
41 CustomFrameViewAsh* custom_frame_view_;
43 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate);
46 class TestWidgetConstraintsDelegate : public TestWidgetDelegate {
47 public:
48 TestWidgetConstraintsDelegate() {}
49 virtual ~TestWidgetConstraintsDelegate() {}
51 // views::View:
52 virtual gfx::Size GetMinimumSize() const OVERRIDE {
53 return minimum_size_;
56 virtual gfx::Size GetMaximumSize() const OVERRIDE {
57 return maximum_size_;
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.
63 return this;
66 // views::WidgetDelegate:
67 virtual bool CanMaximize() const OVERRIDE {
68 return true;
71 void set_minimum_size(const gfx::Size& min_size) {
72 minimum_size_ = min_size;
75 void set_maximum_size(const gfx::Size& max_size) {
76 maximum_size_ = max_size;
79 const gfx::Rect& GetFrameCaptionButtonContainerViewBounds() {
80 return custom_frame_view()->GetFrameCaptionButtonContainerViewForTest()->
81 bounds();
84 void EndFrameCaptionButtonContainerViewAnimations() {
85 FrameCaptionButtonContainerView::TestApi test(custom_frame_view()->
86 GetFrameCaptionButtonContainerViewForTest());
87 test.EndAnimations();
90 int GetTitleBarHeight() const {
91 return custom_frame_view()->NonClientTopBorderHeight();
94 private:
95 gfx::Size minimum_size_;
96 gfx::Size maximum_size_;
98 DISALLOW_COPY_AND_ASSIGN(TestWidgetConstraintsDelegate);
101 class CustomFrameViewAshTest : public test::AshTestBase {
102 public:
103 CustomFrameViewAshTest() {}
104 virtual ~CustomFrameViewAshTest() {}
106 protected:
107 scoped_ptr<views::Widget> CreateWidget(TestWidgetDelegate* delegate) {
108 scoped_ptr<views::Widget> widget(new views::Widget);
109 views::Widget::InitParams params;
110 params.delegate = delegate;
111 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
112 params.bounds = gfx::Rect(0, 0, 100, 100);
113 params.context = CurrentContext();
114 widget->Init(params);
115 return widget.Pass();
118 test::TestSessionStateDelegate* GetTestSessionStateDelegate() {
119 return static_cast<ash::test::TestSessionStateDelegate*>(
120 Shell::GetInstance()->session_state_delegate());
123 private:
124 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewAshTest);
127 // Test that the height of the header is correct upon initially displaying
128 // the widget.
129 TEST_F(CustomFrameViewAshTest, HeaderHeight) {
130 TestWidgetDelegate* delegate = new TestWidgetDelegate;
132 scoped_ptr<views::Widget> widget(CreateWidget(delegate));
133 widget->Show();
135 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
136 gfx::ImageSkia* close_button =
137 rb.GetImageSkiaNamed(IDR_AURA_WINDOW_CONTROL_BACKGROUND_H);
139 // The header should have enough room for the window controls. The
140 // header/content separator line overlays the window controls.
141 EXPECT_EQ(close_button->height(),
142 delegate->custom_frame_view()->GetHeaderView()->height());
145 // Verify that CustomFrameViewAsh returns the correct minimum and maximum frame
146 // sizes when the client view does not specify any size constraints.
147 TEST_F(CustomFrameViewAshTest, NoSizeConstraints) {
148 TestWidgetConstraintsDelegate* delegate = new TestWidgetConstraintsDelegate;
149 scoped_ptr<views::Widget> widget(CreateWidget(delegate));
151 CustomFrameViewAsh* custom_frame_view = delegate->custom_frame_view();
152 gfx::Size min_frame_size = custom_frame_view->GetMinimumSize();
153 gfx::Size max_frame_size = custom_frame_view->GetMaximumSize();
155 EXPECT_EQ(delegate->GetTitleBarHeight(), min_frame_size.height());
157 // A width and height constraint of 0 denotes unbounded.
158 EXPECT_EQ(0, max_frame_size.width());
159 EXPECT_EQ(0, max_frame_size.height());
162 // Verify that CustomFrameViewAsh returns the correct minimum and maximum frame
163 // sizes when the client view specifies size constraints.
164 TEST_F(CustomFrameViewAshTest, MinimumAndMaximumSize) {
165 gfx::Size min_client_size(500, 500);
166 gfx::Size max_client_size(800, 800);
167 TestWidgetConstraintsDelegate* delegate = new TestWidgetConstraintsDelegate;
168 delegate->set_minimum_size(min_client_size);
169 delegate->set_maximum_size(max_client_size);
170 scoped_ptr<views::Widget> widget(CreateWidget(delegate));
172 CustomFrameViewAsh* custom_frame_view = delegate->custom_frame_view();
173 gfx::Size min_frame_size = custom_frame_view->GetMinimumSize();
174 gfx::Size max_frame_size = custom_frame_view->GetMaximumSize();
176 EXPECT_EQ(min_client_size.width(), min_frame_size.width());
177 EXPECT_EQ(max_client_size.width(), max_frame_size.width());
178 EXPECT_EQ(min_client_size.height() + delegate->GetTitleBarHeight(),
179 min_frame_size.height());
180 EXPECT_EQ(max_client_size.height() + delegate->GetTitleBarHeight(),
181 max_frame_size.height());
184 // Verify that CustomFrameViewAsh updates the avatar icon based on the
185 // state of the SessionStateDelegate after visibility change.
186 TEST_F(CustomFrameViewAshTest, AvatarIcon) {
187 TestWidgetConstraintsDelegate* delegate = new TestWidgetConstraintsDelegate;
188 scoped_ptr<views::Widget> widget(CreateWidget(delegate));
190 CustomFrameViewAsh* custom_frame_view = delegate->custom_frame_view();
191 EXPECT_FALSE(custom_frame_view->GetAvatarIconViewForTest());
193 // Avatar image becomes available.
194 const gfx::ImageSkia user_image =
195 *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
196 IDR_AURA_UBER_TRAY_GUEST_ICON);
197 GetTestSessionStateDelegate()->SetUserImage(user_image);
198 widget->Hide();
199 widget->Show();
200 EXPECT_TRUE(custom_frame_view->GetAvatarIconViewForTest());
202 // Avatar image is gone; the ImageView for the avatar icon should be
203 // removed.
204 GetTestSessionStateDelegate()->SetUserImage(gfx::ImageSkia());
205 widget->Hide();
206 widget->Show();
207 EXPECT_FALSE(custom_frame_view->GetAvatarIconViewForTest());
210 // The visibility of the size button is updated when maximize mode is toggled.
211 // Verify that the layout of the HeaderView is updated for the size button's
212 // new visibility.
213 TEST_F(CustomFrameViewAshTest, HeaderViewNotifiedOfChildSizeChange) {
214 TestWidgetConstraintsDelegate* delegate = new TestWidgetConstraintsDelegate;
215 scoped_ptr<views::Widget> widget(CreateWidget(delegate));
217 const gfx::Rect initial = delegate->
218 GetFrameCaptionButtonContainerViewBounds();
219 Shell::GetInstance()->maximize_mode_controller()->
220 EnableMaximizeModeWindowManager(true);
221 delegate->EndFrameCaptionButtonContainerViewAnimations();
222 const gfx::Rect maximize_mode_bounds = delegate->
223 GetFrameCaptionButtonContainerViewBounds();
224 EXPECT_GT(initial.width(), maximize_mode_bounds.width());
225 Shell::GetInstance()->maximize_mode_controller()->
226 EnableMaximizeModeWindowManager(false);
227 delegate->EndFrameCaptionButtonContainerViewAnimations();
228 const gfx::Rect after_restore = delegate->
229 GetFrameCaptionButtonContainerViewBounds();
230 EXPECT_EQ(initial, after_restore);
233 } // namespace ash