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