OLD | NEW |
(Empty) | |
| 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 "ui/views/window/custom_frame_view.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "ui/views/controls/button/image_button.h" |
| 10 #include "ui/views/test/views_test_base.h" |
| 11 #include "ui/views/widget/widget.h" |
| 12 #include "ui/views/widget/widget_delegate.h" |
| 13 #include "ui/views/window/window_button_order_provider.h" |
| 14 |
| 15 namespace views { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Allows for the control of whether or not the widget can maximize or not. |
| 20 // This can be set after initial setup in order to allow testing of both forms |
| 21 // of delegates. By default this can maximize. |
| 22 class MaximizeStateControlDelegate : public WidgetDelegateView { |
| 23 public: |
| 24 MaximizeStateControlDelegate() : can_maximize_(true) {} |
| 25 virtual ~MaximizeStateControlDelegate() {} |
| 26 |
| 27 void set_can_maximize(bool can_maximize) { |
| 28 can_maximize_ = can_maximize; |
| 29 } |
| 30 |
| 31 // WidgetDelegate: |
| 32 virtual bool CanMaximize() const OVERRIDE { return can_maximize_; } |
| 33 |
| 34 private: |
| 35 bool can_maximize_; |
| 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(MaximizeStateControlDelegate); |
| 38 }; |
| 39 |
| 40 } // namespace |
| 41 |
| 42 class CustomFrameViewTest : public ViewsTestBase { |
| 43 public: |
| 44 CustomFrameViewTest() {} |
| 45 virtual ~CustomFrameViewTest() {} |
| 46 |
| 47 CustomFrameView* custom_frame_view() { |
| 48 return custom_frame_view_; |
| 49 } |
| 50 |
| 51 MaximizeStateControlDelegate* maximize_state_control_delegate() { |
| 52 return maximize_state_control_delegate_; |
| 53 } |
| 54 |
| 55 Widget* widget() { |
| 56 return widget_; |
| 57 } |
| 58 |
| 59 // ViewsTestBase: |
| 60 virtual void SetUp() OVERRIDE; |
| 61 virtual void TearDown() OVERRIDE; |
| 62 |
| 63 protected: |
| 64 const std::vector<views::FrameButton>& leading_buttons() { |
| 65 return WindowButtonOrderProvider::GetInstance()->leading_buttons(); |
| 66 } |
| 67 |
| 68 const std::vector<views::FrameButton>& trailing_buttons() { |
| 69 return WindowButtonOrderProvider::GetInstance()->trailing_buttons(); |
| 70 } |
| 71 |
| 72 ImageButton* minimize_button() { |
| 73 return custom_frame_view_->minimize_button_; |
| 74 } |
| 75 |
| 76 ImageButton* maximize_button() { |
| 77 return custom_frame_view_->maximize_button_; |
| 78 } |
| 79 |
| 80 ImageButton* restore_button() { |
| 81 return custom_frame_view_->restore_button_; |
| 82 } |
| 83 |
| 84 ImageButton* close_button() { |
| 85 return custom_frame_view_->close_button_; |
| 86 } |
| 87 |
| 88 gfx::Rect title_bounds() { |
| 89 return custom_frame_view_->title_bounds_; |
| 90 } |
| 91 |
| 92 void SetWindowButtonOrder( |
| 93 const std::vector<views::FrameButton> leading_buttons, |
| 94 const std::vector<views::FrameButton> trailing_buttons); |
| 95 |
| 96 private: |
| 97 // Parent container for |custom_frame_view_| |
| 98 Widget* widget_; |
| 99 |
| 100 // Owned by |widget_| |
| 101 CustomFrameView* custom_frame_view_; |
| 102 |
| 103 // Delegate of |widget_| which controls maximizing |
| 104 MaximizeStateControlDelegate* maximize_state_control_delegate_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewTest); |
| 107 }; |
| 108 |
| 109 void CustomFrameViewTest::SetUp() { |
| 110 ViewsTestBase::SetUp(); |
| 111 |
| 112 maximize_state_control_delegate_ = new MaximizeStateControlDelegate; |
| 113 widget_ = new Widget; |
| 114 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 115 params.delegate = maximize_state_control_delegate_; |
| 116 params.remove_standard_frame = true; |
| 117 widget_->Init(params); |
| 118 |
| 119 custom_frame_view_ = new CustomFrameView; |
| 120 widget_->non_client_view()->SetFrameView(custom_frame_view_); |
| 121 } |
| 122 |
| 123 void CustomFrameViewTest::TearDown() { |
| 124 widget_->CloseNow(); |
| 125 |
| 126 ViewsTestBase::TearDown(); |
| 127 } |
| 128 |
| 129 void CustomFrameViewTest::SetWindowButtonOrder( |
| 130 const std::vector<views::FrameButton> leading_buttons, |
| 131 const std::vector<views::FrameButton> trailing_buttons) { |
| 132 WindowButtonOrderProvider::GetInstance()-> |
| 133 SetWindowButtonOrder(leading_buttons, trailing_buttons); |
| 134 } |
| 135 |
| 136 // Tests that there is a default button ordering before initialization causes |
| 137 // a configuration file check. |
| 138 TEST_F(CustomFrameViewTest, DefaultButtons) { |
| 139 const std::vector<views::FrameButton>& trailing = trailing_buttons(); |
| 140 EXPECT_EQ(trailing.size(), 3u); |
| 141 EXPECT_TRUE(leading_buttons().empty()); |
| 142 EXPECT_EQ(trailing[0], FRAME_BUTTON_MINIMIZE); |
| 143 EXPECT_EQ(trailing[1], FRAME_BUTTON_MAXIMIZE); |
| 144 EXPECT_EQ(trailing[2], FRAME_BUTTON_CLOSE); |
| 145 } |
| 146 |
| 147 // Tests that layout places the buttons in order, that the restore button is |
| 148 // hidden and the buttons are placed after the title. |
| 149 TEST_F(CustomFrameViewTest, DefaultButtonLayout) { |
| 150 Widget* parent = widget(); |
| 151 CustomFrameView* view = custom_frame_view(); |
| 152 view->Init(parent); |
| 153 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); |
| 154 parent->Show(); |
| 155 |
| 156 EXPECT_LT(minimize_button()->x(), maximize_button()->x()); |
| 157 EXPECT_LT(maximize_button()->x(), close_button()->x()); |
| 158 EXPECT_FALSE(restore_button()->visible()); |
| 159 |
| 160 EXPECT_GT(minimize_button()->x(), |
| 161 title_bounds().x() + title_bounds().width()); |
| 162 } |
| 163 |
| 164 // Tests that setting the buttons to leading places them before the title. |
| 165 TEST_F(CustomFrameViewTest, LeadingButtonLayout) { |
| 166 Widget* parent = widget(); |
| 167 CustomFrameView* view = custom_frame_view(); |
| 168 |
| 169 std::vector<views::FrameButton> leading; |
| 170 leading.push_back(views::FRAME_BUTTON_CLOSE); |
| 171 leading.push_back(views::FRAME_BUTTON_MINIMIZE); |
| 172 leading.push_back(views::FRAME_BUTTON_MAXIMIZE); |
| 173 |
| 174 std::vector<views::FrameButton> trailing; |
| 175 |
| 176 SetWindowButtonOrder(leading, trailing); |
| 177 |
| 178 view->Init(parent); |
| 179 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); |
| 180 parent->Show(); |
| 181 EXPECT_LT(close_button()->x(), minimize_button()->x()); |
| 182 EXPECT_LT(minimize_button()->x(), maximize_button()->x()); |
| 183 EXPECT_FALSE(restore_button()->visible()); |
| 184 EXPECT_LT(maximize_button()->x() + maximize_button()->width(), |
| 185 title_bounds().x()); |
| 186 } |
| 187 |
| 188 // Tests that layouts occuring while maximized swap the maximize button for the |
| 189 // restore button |
| 190 TEST_F(CustomFrameViewTest, MaximizeRevealsRestoreButton) { |
| 191 Widget* parent = widget(); |
| 192 CustomFrameView* view = custom_frame_view(); |
| 193 view->Init(parent); |
| 194 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); |
| 195 parent->Show(); |
| 196 |
| 197 ASSERT_FALSE(restore_button()->visible()); |
| 198 ASSERT_TRUE(maximize_button()->visible()); |
| 199 |
| 200 parent->Maximize(); |
| 201 view->Layout(); |
| 202 |
| 203 EXPECT_TRUE(restore_button()->visible()); |
| 204 EXPECT_FALSE(maximize_button()->visible()); |
| 205 } |
| 206 |
| 207 // Tests that when the parent cannot maximize that the maximize button is not |
| 208 // visible |
| 209 TEST_F(CustomFrameViewTest, CannotMaximizeHidesButton) { |
| 210 Widget* parent = widget(); |
| 211 CustomFrameView* view = custom_frame_view(); |
| 212 MaximizeStateControlDelegate* delegate = maximize_state_control_delegate(); |
| 213 delegate->set_can_maximize(false); |
| 214 |
| 215 view->Init(parent); |
| 216 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); |
| 217 parent->Show(); |
| 218 |
| 219 EXPECT_FALSE(restore_button()->visible()); |
| 220 EXPECT_FALSE(maximize_button()->visible()); |
| 221 } |
| 222 |
| 223 // Tests that when maximized that the edge button has an increased width. |
| 224 TEST_F(CustomFrameViewTest, LargerEdgeButtonsWhenMaximized) { |
| 225 Widget* parent = widget(); |
| 226 CustomFrameView* view = custom_frame_view(); |
| 227 |
| 228 // Custom ordering to have a button on each edge. |
| 229 std::vector<views::FrameButton> leading; |
| 230 leading.push_back(views::FRAME_BUTTON_CLOSE); |
| 231 leading.push_back(views::FRAME_BUTTON_MAXIMIZE); |
| 232 std::vector<views::FrameButton> trailing; |
| 233 trailing.push_back(views::FRAME_BUTTON_MINIMIZE); |
| 234 SetWindowButtonOrder(leading, trailing); |
| 235 |
| 236 view->Init(parent); |
| 237 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); |
| 238 parent->Show(); |
| 239 |
| 240 gfx::Rect close_button_initial_bounds = close_button()->bounds(); |
| 241 gfx::Rect minimize_button_initial_bounds = minimize_button()->bounds(); |
| 242 |
| 243 parent->Maximize(); |
| 244 view->Layout(); |
| 245 |
| 246 EXPECT_GT(close_button()->bounds().width(), |
| 247 close_button_initial_bounds.width()); |
| 248 EXPECT_GT(minimize_button()->bounds().width(), |
| 249 minimize_button_initial_bounds.width()); |
| 250 } |
| 251 |
| 252 } // namespace views |
OLD | NEW |