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. 4 5 #include "ash/frame/custom_frame_view_ash.h" 6 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" 20 21 namespace ash { 22 23 // A views::WidgetDelegate which uses a CustomFrameViewAsh. 24 class TestWidgetDelegate : public views::WidgetDelegateView { 25 public: 26 TestWidgetDelegate() {} 27 virtual ~TestWidgetDelegate() {} 28 29 virtual views::NonClientFrameView* CreateNonClientFrameView( 30 views::Widget* widget) OVERRIDE { 31 custom_frame_view_ = new CustomFrameViewAsh(widget); 32 return custom_frame_view_; 33 } 34 35 CustomFrameViewAsh* custom_frame_view() const { 36 return custom_frame_view_; 37 } 38 39 private: 40 // Not owned. 41 CustomFrameViewAsh* custom_frame_view_; 42 43 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); 44 }; 45 46 class TestWidgetConstraintsDelegate : public TestWidgetDelegate { 47 public: 48 TestWidgetConstraintsDelegate() {} 49 virtual ~TestWidgetConstraintsDelegate() {} 50 51 // views::View: 52 virtual gfx::Size GetMinimumSize() const OVERRIDE { 53 return minimum_size_; 54 } 55 56 virtual gfx::Size GetMaximumSize() const OVERRIDE { 57 return maximum_size_; 58 } 59 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; 64 } 65 66 // views::WidgetDelegate: 67 virtual bool CanMaximize() const OVERRIDE { 68 return true; 69 } 70 71 void set_minimum_size(const gfx::Size& min_size) { 72 minimum_size_ = min_size; 73 } 74 75 void set_maximum_size(const gfx::Size& max_size) { 76 maximum_size_ = max_size; 77 } 78 79 const gfx::Rect& GetFrameCaptionButtonContainerViewBounds() { 80 return custom_frame_view()->GetFrameCaptionButtonContainerViewForTest()-> 81 bounds(); 82 } 83 84 int GetTitleBarHeight() const { 85 return custom_frame_view()->NonClientTopBorderHeight(); 86 } 87 88 private: 89 gfx::Size minimum_size_; 90 gfx::Size maximum_size_; 91 92 DISALLOW_COPY_AND_ASSIGN(TestWidgetConstraintsDelegate); 93 }; 94 95 class CustomFrameViewAshTest : public test::AshTestBase { 96 public: 97 CustomFrameViewAshTest() {} 98 virtual ~CustomFrameViewAshTest() {} 99 100 protected: 101 scoped_ptr<views::Widget> CreateWidget(TestWidgetDelegate* delegate) { 102 scoped_ptr<views::Widget> widget(new views::Widget); 103 views::Widget::InitParams params; 104 params.delegate = delegate; 105 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 106 params.bounds = gfx::Rect(0, 0, 100, 100); 107 params.context = CurrentContext(); 108 widget->Init(params); 109 return widget.Pass(); 110 } 111 112 test::TestSessionStateDelegate* GetTestSessionStateDelegate() { 113 return static_cast<ash::test::TestSessionStateDelegate*>( 114 Shell::GetInstance()->session_state_delegate()); 115 } 116 117 private: 118 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewAshTest); 119 }; 120 121 // Test that the height of the header is correct upon initially displaying 122 // the widget. 123 TEST_F(CustomFrameViewAshTest, HeaderHeight) { 124 TestWidgetDelegate* delegate = new TestWidgetDelegate; 125 126 scoped_ptr<views::Widget> widget(CreateWidget(delegate)); 127 widget->Show(); 128 129 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 130 gfx::ImageSkia* close_button = 131 rb.GetImageSkiaNamed(IDR_AURA_WINDOW_CONTROL_BACKGROUND_H); 132 133 // The header should have enough room for the window controls. The 134 // header/content separator line overlays the window controls. 135 EXPECT_EQ(close_button->height(), 136 delegate->custom_frame_view()->GetHeaderView()->height()); 137 } 138 139 // Verify that CustomFrameViewAsh returns the correct minimum and maximum frame 140 // sizes when the client view does not specify any size constraints. 141 TEST_F(CustomFrameViewAshTest, NoSizeConstraints) { 142 TestWidgetConstraintsDelegate* delegate = new TestWidgetConstraintsDelegate; 143 scoped_ptr<views::Widget> widget(CreateWidget(delegate)); 144 145 CustomFrameViewAsh* custom_frame_view = delegate->custom_frame_view(); 146 gfx::Size min_frame_size = custom_frame_view->GetMinimumSize(); 147 gfx::Size max_frame_size = custom_frame_view->GetMaximumSize(); 148 149 EXPECT_EQ(delegate->GetTitleBarHeight(), min_frame_size.height()); 150 151 // A width and height constraint of 0 denotes unbounded. 152 EXPECT_EQ(0, max_frame_size.width()); 153 EXPECT_EQ(0, max_frame_size.height()); 154 } 155 156 // Verify that CustomFrameViewAsh returns the correct minimum and maximum frame 157 // sizes when the client view specifies size constraints. 158 TEST_F(CustomFrameViewAshTest, MinimumAndMaximumSize) { 159 gfx::Size min_client_size(500, 500); 160 gfx::Size max_client_size(800, 800); 161 TestWidgetConstraintsDelegate* delegate = new TestWidgetConstraintsDelegate; 162 delegate->set_minimum_size(min_client_size); 163 delegate->set_maximum_size(max_client_size); 164 scoped_ptr<views::Widget> widget(CreateWidget(delegate)); 165 166 CustomFrameViewAsh* custom_frame_view = delegate->custom_frame_view(); 167 gfx::Size min_frame_size = custom_frame_view->GetMinimumSize(); 168 gfx::Size max_frame_size = custom_frame_view->GetMaximumSize(); 169 170 EXPECT_EQ(min_client_size.width(), min_frame_size.width()); 171 EXPECT_EQ(max_client_size.width(), max_frame_size.width()); 172 EXPECT_EQ(min_client_size.height() + delegate->GetTitleBarHeight(), 173 min_frame_size.height()); 174 EXPECT_EQ(max_client_size.height() + delegate->GetTitleBarHeight(), 175 max_frame_size.height()); 176 } 177 178 // Verify that CustomFrameViewAsh updates the avatar icon based on the 179 // state of the SessionStateDelegate after visibility change. 180 TEST_F(CustomFrameViewAshTest, AvatarIcon) { 181 TestWidgetConstraintsDelegate* delegate = new TestWidgetConstraintsDelegate; 182 scoped_ptr<views::Widget> widget(CreateWidget(delegate)); 183 184 CustomFrameViewAsh* custom_frame_view = delegate->custom_frame_view(); 185 EXPECT_FALSE(custom_frame_view->GetAvatarIconViewForTest()); 186 187 // Avatar image becomes available. 188 const gfx::ImageSkia user_image = 189 *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 190 IDR_AURA_UBER_TRAY_GUEST_ICON); 191 GetTestSessionStateDelegate()->SetUserImage(user_image); 192 widget->Hide(); 193 widget->Show(); 194 EXPECT_TRUE(custom_frame_view->GetAvatarIconViewForTest()); 195 196 // Avatar image is gone; the ImageView for the avatar icon should be 197 // removed. 198 GetTestSessionStateDelegate()->SetUserImage(gfx::ImageSkia()); 199 widget->Hide(); 200 widget->Show(); 201 EXPECT_FALSE(custom_frame_view->GetAvatarIconViewForTest()); 202 } 203 204 // The visibility of the size button is updated when maximize mode is toggled. 205 // Verify that the layout of the HeaderView is updated for the size button's 206 // new visibility. 207 TEST_F(CustomFrameViewAshTest, HeaderViewNotifiedOfChildSizeChange) { 208 TestWidgetConstraintsDelegate* delegate = new TestWidgetConstraintsDelegate; 209 scoped_ptr<views::Widget> widget(CreateWidget(delegate)); 210 211 const gfx::Rect initial = delegate-> 212 GetFrameCaptionButtonContainerViewBounds(); 213 Shell::GetInstance()->maximize_mode_controller()-> 214 EnableMaximizeModeWindowManager(true); 215 const gfx::Rect maximize_mode_bounds = delegate-> 216 GetFrameCaptionButtonContainerViewBounds(); 217 EXPECT_GT(initial.width(), maximize_mode_bounds.width()); 218 Shell::GetInstance()->maximize_mode_controller()-> 219 EnableMaximizeModeWindowManager(false); 220 const gfx::Rect after_restore = delegate-> 221 GetFrameCaptionButtonContainerViewBounds(); 222 EXPECT_EQ(initial, after_restore); 223 } 224 225 } // namespace ash 226