Chromium Code Reviews| 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 std::vector<views::FrameButton> GetLeadingButtons() { | |
| 65 return WindowButtonOrderProvider::GetInstance()->GetLeadingButtons(); | |
| 66 } | |
| 67 | |
| 68 std::vector<views::FrameButton> GetTrailingButtons() { | |
| 69 return WindowButtonOrderProvider::GetInstance()->GetTrailingButtons(); | |
| 70 } | |
| 71 | |
| 72 ImageButton* GetMinimizeButton() { | |
| 73 return custom_frame_view_->minimize_button_; | |
| 74 } | |
| 75 | |
| 76 ImageButton* GetMaximizeButton() { | |
| 77 return custom_frame_view_->maximize_button_; | |
| 78 } | |
| 79 | |
| 80 ImageButton* GetRestoreButton() { | |
| 81 return custom_frame_view_->restore_button_; | |
| 82 } | |
| 83 | |
| 84 ImageButton* GetCloseButton() { | |
| 85 return custom_frame_view_->close_button_; | |
| 86 } | |
| 87 | |
| 88 gfx::Rect GetTitleBounds() { | |
| 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 params.top_level = true; | |
| 118 widget_->Init(params); | |
| 119 | |
| 120 custom_frame_view_ = new CustomFrameView; | |
| 121 widget_->non_client_view()->SetFrameView(custom_frame_view_); | |
| 122 } | |
| 123 | |
| 124 void CustomFrameViewTest::TearDown() { | |
| 125 widget_->CloseNow(); | |
| 126 | |
| 127 ViewsTestBase::TearDown(); | |
| 128 } | |
| 129 | |
| 130 void CustomFrameViewTest::SetWindowButtonOrder( | |
| 131 const std::vector<views::FrameButton> leading_buttons, | |
| 132 const std::vector<views::FrameButton> trailing_buttons) { | |
| 133 WindowButtonOrderProvider::GetInstance()-> | |
| 134 SetWindowButtonOrder(leading_buttons, trailing_buttons); | |
| 135 } | |
| 136 | |
| 137 // Tests that there is a default button ordering before initialization causes | |
| 138 // a configuration file check. | |
| 139 TEST_F(CustomFrameViewTest, DefaultButtons) { | |
| 140 std::vector<views::FrameButton> trailing = GetTrailingButtons(); | |
| 141 EXPECT_EQ(trailing.size(), 3u); | |
| 142 EXPECT_TRUE(GetLeadingButtons().empty()); | |
| 143 EXPECT_EQ(trailing[0], FRAME_BUTTON_MINIMIZE); | |
| 144 EXPECT_EQ(trailing[1], FRAME_BUTTON_MAXIMIZE); | |
| 145 EXPECT_EQ(trailing[2], FRAME_BUTTON_CLOSE); | |
| 146 } | |
| 147 | |
| 148 // Tests that layout places the buttons in order, that the restore button is | |
| 149 // hidden and the buttons are placed after the title. | |
| 150 TEST_F(CustomFrameViewTest, DefaultButtonLayout) { | |
| 151 Widget* parent = widget(); | |
| 152 CustomFrameView* view = custom_frame_view(); | |
| 153 view->Init(parent); | |
| 154 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); | |
| 155 parent->Show(); | |
| 156 | |
| 157 EXPECT_LT(GetMinimizeButton()->x(), GetMaximizeButton()->x()); | |
| 158 EXPECT_LT(GetMaximizeButton()->x(), GetCloseButton()->x()); | |
| 159 EXPECT_FALSE(GetRestoreButton()->visible()); | |
| 160 | |
| 161 EXPECT_GT(GetMinimizeButton()->x(), | |
| 162 GetTitleBounds().x() + GetTitleBounds().width()); | |
| 163 } | |
| 164 | |
| 165 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | |
| 166 // Tests that setting the buttons to leading places them before the title. | |
| 167 TEST_F(CustomFrameViewTest, LeadingButtonLayout) { | |
| 168 Widget* parent = widget(); | |
| 169 CustomFrameView* view = custom_frame_view(); | |
| 170 | |
| 171 std::vector<views::FrameButton> leading; | |
| 172 leading.push_back(views::FRAME_BUTTON_CLOSE); | |
| 173 leading.push_back(views::FRAME_BUTTON_MINIMIZE); | |
| 174 leading.push_back(views::FRAME_BUTTON_MAXIMIZE); | |
| 175 | |
| 176 std::vector<views::FrameButton> trailing; | |
| 177 | |
| 178 SetWindowButtonOrder(leading, trailing); | |
| 179 | |
| 180 view->Init(parent); | |
| 181 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); | |
| 182 parent->Show(); | |
| 183 EXPECT_LT(GetCloseButton()->x(), GetMinimizeButton()->x()); | |
| 184 EXPECT_LT(GetMinimizeButton()->x(), GetMaximizeButton()->x()); | |
| 185 EXPECT_FALSE(GetRestoreButton()->visible()); | |
| 186 EXPECT_LT(GetMaximizeButton()->x() + GetMaximizeButton()->width(), | |
| 187 GetTitleBounds().x()); | |
| 188 } | |
| 189 #endif | |
| 190 | |
| 191 // Tests that layouts occuring while maximized swap the maximize button for the | |
| 192 // restore button | |
| 193 TEST_F(CustomFrameViewTest, MaximizeRevealsRestoreButton) { | |
| 194 Widget* parent = widget(); | |
| 195 CustomFrameView* view = custom_frame_view(); | |
| 196 view->Init(parent); | |
| 197 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); | |
| 198 parent->Show(); | |
| 199 | |
| 200 ASSERT_FALSE(GetRestoreButton()->visible()); | |
| 201 ASSERT_TRUE(GetMaximizeButton()->visible()); | |
| 202 | |
| 203 parent->Maximize(); | |
| 204 view->Layout(); | |
| 205 | |
| 206 EXPECT_TRUE(GetRestoreButton()->visible()); | |
| 207 EXPECT_FALSE(GetMaximizeButton()->visible()); | |
| 208 } | |
| 209 | |
| 210 // Tests that when the parent cannot maximize that the maximize button is not | |
| 211 // visible | |
| 212 TEST_F(CustomFrameViewTest, CannotMaximizeHidesButton) { | |
| 213 Widget* parent = widget(); | |
| 214 CustomFrameView* view = custom_frame_view(); | |
| 215 MaximizeStateControlDelegate* delegate = maximize_state_control_delegate(); | |
| 216 delegate->set_can_maximize(false); | |
| 217 | |
| 218 view->Init(parent); | |
| 219 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); | |
| 220 parent->Show(); | |
| 221 | |
| 222 EXPECT_FALSE(GetRestoreButton()->visible()); | |
| 223 EXPECT_FALSE(GetMaximizeButton()->visible()); | |
| 224 } | |
| 225 | |
|
flackr
2014/05/07 00:37:58
Perhaps it's worth a test to verify the extra widt
jonross
2014/05/07 17:27:08
Done.
| |
| 226 } // namespace views | |
| OLD | NEW |