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 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 const std::vector<views::FrameButton>& trailing = trailing_buttons(); | |
141 EXPECT_EQ(trailing.size(), 3u); | |
142 EXPECT_TRUE(leading_buttons().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(minimize_button()->x(), maximize_button()->x()); | |
158 EXPECT_LT(maximize_button()->x(), close_button()->x()); | |
159 EXPECT_FALSE(restore_button()->visible()); | |
160 | |
161 EXPECT_GT(minimize_button()->x(), | |
162 title_bounds().x() + title_bounds().width()); | |
163 } | |
164 | |
165 // Tests that setting the buttons to leading places them before the title. | |
166 TEST_F(CustomFrameViewTest, LeadingButtonLayout) { | |
167 Widget* parent = widget(); | |
168 CustomFrameView* view = custom_frame_view(); | |
169 | |
170 std::vector<views::FrameButton> leading; | |
171 leading.push_back(views::FRAME_BUTTON_CLOSE); | |
172 leading.push_back(views::FRAME_BUTTON_MINIMIZE); | |
173 leading.push_back(views::FRAME_BUTTON_MAXIMIZE); | |
174 | |
175 std::vector<views::FrameButton> trailing; | |
176 | |
177 SetWindowButtonOrder(leading, trailing); | |
178 | |
179 view->Init(parent); | |
180 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); | |
181 parent->Show(); | |
182 EXPECT_LT(close_button()->x(), minimize_button()->x()); | |
183 EXPECT_LT(minimize_button()->x(), maximize_button()->x()); | |
184 EXPECT_FALSE(restore_button()->visible()); | |
185 EXPECT_LT(maximize_button()->x() + maximize_button()->width(), | |
186 title_bounds().x()); | |
187 } | |
188 | |
189 // Tests that layouts occuring while maximized swap the maximize button for the | |
190 // restore button | |
191 TEST_F(CustomFrameViewTest, MaximizeRevealsRestoreButton) { | |
192 Widget* parent = widget(); | |
193 CustomFrameView* view = custom_frame_view(); | |
194 view->Init(parent); | |
195 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); | |
196 parent->Show(); | |
197 | |
198 ASSERT_FALSE(restore_button()->visible()); | |
199 ASSERT_TRUE(maximize_button()->visible()); | |
200 | |
201 parent->Maximize(); | |
202 view->Layout(); | |
203 | |
204 EXPECT_TRUE(restore_button()->visible()); | |
205 EXPECT_FALSE(maximize_button()->visible()); | |
206 } | |
207 | |
208 // Tests that when the parent cannot maximize that the maximize button is not | |
209 // visible | |
210 TEST_F(CustomFrameViewTest, CannotMaximizeHidesButton) { | |
211 Widget* parent = widget(); | |
212 CustomFrameView* view = custom_frame_view(); | |
213 MaximizeStateControlDelegate* delegate = maximize_state_control_delegate(); | |
214 delegate->set_can_maximize(false); | |
215 | |
216 view->Init(parent); | |
217 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); | |
218 parent->Show(); | |
219 | |
220 EXPECT_FALSE(restore_button()->visible()); | |
221 EXPECT_FALSE(maximize_button()->visible()); | |
222 } | |
223 | |
224 // Tests that when maximized that the edge button has an increased width. | |
225 TEST_F(CustomFrameViewTest, LargerEdgeButtonsWhenMaximized) { | |
226 Widget* parent = widget(); | |
227 CustomFrameView* view = custom_frame_view(); | |
228 | |
229 // Custom ordering to have a button on each edge. | |
230 std::vector<views::FrameButton> leading; | |
231 leading.push_back(views::FRAME_BUTTON_CLOSE); | |
232 leading.push_back(views::FRAME_BUTTON_MAXIMIZE); | |
233 std::vector<views::FrameButton> trailing; | |
234 trailing.push_back(views::FRAME_BUTTON_MINIMIZE); | |
235 SetWindowButtonOrder(leading, trailing); | |
236 | |
237 view->Init(parent); | |
238 parent->SetBounds(gfx::Rect(0, 0, 300, 100)); | |
239 parent->Show(); | |
240 | |
241 gfx::Rect close_button_initial_bounds = close_button()->bounds(); | |
242 gfx::Rect minimize_button_initial_bounds = minimize_button()->bounds(); | |
243 | |
244 parent->Maximize(); | |
245 view->Layout(); | |
246 | |
247 EXPECT_GT(close_button()->bounds().width(), | |
248 close_button_initial_bounds.width()); | |
249 EXPECT_GT(minimize_button()->bounds().width(), | |
250 minimize_button_initial_bounds.width()); | |
251 } | |
252 | |
253 } // namespace views | |
OLD | NEW |