OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/common/system/tray/three_view_layout.h" | |
6 #include "base/memory/ptr_util.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 #include "ui/gfx/geometry/insets.h" | |
9 #include "ui/gfx/geometry/rect_conversions.h" | |
10 #include "ui/views/test/test_layout_manager.h" | |
11 #include "ui/views/view.h" | |
12 | |
13 namespace ash { | |
14 | |
15 namespace { | |
16 | |
17 // A View that allows clients to specify its preferred size. | |
18 class TestView : public views::View { | |
tdanderson
2016/10/19 18:31:00
should you specify a destructor for TestView and T
bruthig
2016/10/20 04:40:12
I don't think they are necessary, are they?
tdanderson
2016/10/20 18:16:49
My mistake, they're not.
| |
19 public: | |
20 TestView() : TestView(gfx::Size()) {} | |
21 | |
22 explicit TestView(const gfx::Size& preferred_size) | |
23 : preferred_size_(preferred_size) {} | |
24 | |
25 void set_preferred_size(const gfx::Size& size) { preferred_size_ = size; } | |
26 | |
27 // View: | |
28 gfx::Size GetPreferredSize() const override { return preferred_size_; } | |
29 | |
30 private: | |
31 gfx::Size preferred_size_; | |
32 | |
33 DISALLOW_COPY_AND_ASSIGN(TestView); | |
34 }; | |
35 | |
36 } // namespace | |
37 | |
38 class ThreeViewLayoutTest : public testing::Test { | |
39 public: | |
40 ThreeViewLayoutTest(); | |
41 | |
42 protected: | |
43 // Returns the bounds of |child| in the coordinate space of |host_|. | |
44 gfx::Rect GetBoundsInHost(const views::View* child) const; | |
45 | |
46 // Wrapper functions to access the internals of |layout_|. | |
47 views::View* GetContainer(ThreeViewLayout::Container container) const; | |
48 | |
49 // The test target. | |
50 ThreeViewLayout* layout_; | |
51 | |
52 // The View that the test target is installed on. | |
53 std::unique_ptr<views::View> host_; | |
54 | |
55 private: | |
56 DISALLOW_COPY_AND_ASSIGN(ThreeViewLayoutTest); | |
57 }; | |
58 | |
59 ThreeViewLayoutTest::ThreeViewLayoutTest() | |
60 : layout_(new ThreeViewLayout()), host_(new views::View) { | |
61 host_->SetLayoutManager(layout_); | |
62 } | |
63 | |
64 gfx::Rect ThreeViewLayoutTest::GetBoundsInHost(const views::View* child) const { | |
65 gfx::RectF rect_f(child->bounds()); | |
66 views::View::ConvertRectToTarget(child, host_.get(), &rect_f); | |
67 return ToNearestRect(rect_f); | |
68 } | |
69 | |
70 views::View* ThreeViewLayoutTest::GetContainer( | |
71 ThreeViewLayout::Container container) const { | |
72 return layout_->GetContainer(container); | |
73 } | |
74 | |
75 TEST_F(ThreeViewLayoutTest, PaddingBetweenContainers) { | |
76 const int kPaddingBetweenContainers = 3; | |
77 layout_ = new ThreeViewLayout(kPaddingBetweenContainers); | |
78 host_->SetLayoutManager(layout_); | |
79 | |
80 host_->SetBounds(0, 0, 100, 10); | |
81 views::View* start_child = new TestView(gfx::Size(10, 10)); | |
82 views::View* center_child = new TestView(gfx::Size(10, 10)); | |
83 views::View* end_child = new TestView(gfx::Size(10, 10)); | |
84 | |
85 layout_->AddView(ThreeViewLayout::START, start_child); | |
86 layout_->AddView(ThreeViewLayout::CENTER, center_child); | |
87 layout_->AddView(ThreeViewLayout::END, end_child); | |
88 | |
89 host_->Layout(); | |
90 | |
91 EXPECT_EQ(0, GetBoundsInHost(start_child).x()); | |
92 EXPECT_EQ(13, GetBoundsInHost(center_child).x()); | |
93 EXPECT_EQ(26, GetBoundsInHost(end_child).x()); | |
tdanderson
2016/10/19 18:31:00
Consider specifying these as constants derived fro
bruthig
2016/10/20 04:40:12
Updated along with some of the other tests. Let m
tdanderson
2016/10/20 18:16:49
lg, thanks
| |
94 } | |
95 | |
96 TEST_F(ThreeViewLayoutTest, VerticalOrientation) { | |
97 layout_ = new ThreeViewLayout(ThreeViewLayout::VERTICAL); | |
98 host_->SetLayoutManager(layout_); | |
99 | |
100 host_->SetBounds(0, 0, 10, 100); | |
101 views::View* start_child = new TestView(gfx::Size(10, 10)); | |
102 views::View* center_child = new TestView(gfx::Size(10, 10)); | |
103 views::View* end_child = new TestView(gfx::Size(10, 10)); | |
104 | |
105 layout_->AddView(ThreeViewLayout::START, start_child); | |
106 layout_->AddView(ThreeViewLayout::CENTER, center_child); | |
107 layout_->AddView(ThreeViewLayout::END, end_child); | |
108 | |
109 host_->Layout(); | |
110 | |
111 EXPECT_EQ(0, GetBoundsInHost(start_child).y()); | |
112 EXPECT_EQ(10, GetBoundsInHost(center_child).y()); | |
113 EXPECT_EQ(20, GetBoundsInHost(end_child).y()); | |
114 } | |
115 | |
116 TEST_F(ThreeViewLayoutTest, ContainerViewsRemovedFromHost) { | |
117 EXPECT_NE(0, host_->child_count()); | |
118 host_->SetLayoutManager(nullptr); | |
119 EXPECT_EQ(0, host_->child_count()); | |
120 } | |
121 | |
122 TEST_F(ThreeViewLayoutTest, MinCrossAxisSize) { | |
123 const int kMinCrossAxisSize = 15; | |
124 EXPECT_EQ(0, layout_->GetPreferredSize(host_.get()).height()); | |
125 layout_->SetMinCrossAxisSize(kMinCrossAxisSize); | |
126 EXPECT_EQ(kMinCrossAxisSize, layout_->GetPreferredSize(host_.get()).height()); | |
127 EXPECT_EQ(kMinCrossAxisSize, | |
128 layout_->GetPreferredHeightForWidth(host_.get(), 0)); | |
129 } | |
130 | |
131 TEST_F(ThreeViewLayoutTest, MainAxisMinSize) { | |
132 host_->SetBounds(0, 0, 100, 10); | |
133 const gfx::Size kMinSize(15, 10); | |
134 layout_->SetMinSize(ThreeViewLayout::START, kMinSize); | |
135 views::View* child = new TestView(gfx::Size(10, 10)); | |
136 layout_->AddView(ThreeViewLayout::CENTER, child); | |
137 host_->Layout(); | |
138 | |
139 EXPECT_EQ(kMinSize.width(), GetBoundsInHost(child).x()); | |
140 } | |
141 | |
142 TEST_F(ThreeViewLayoutTest, MainAxisMaxSize) { | |
143 host_->SetBounds(0, 0, 100, 10); | |
144 const gfx::Size kMaxSize(10, 10); | |
145 | |
146 layout_->SetMaxSize(ThreeViewLayout::START, kMaxSize); | |
147 views::View* start_child = new TestView(gfx::Size(20, 20)); | |
148 layout_->AddView(ThreeViewLayout::START, start_child); | |
149 | |
150 views::View* center_child = new TestView(gfx::Size(10, 10)); | |
151 layout_->AddView(ThreeViewLayout::CENTER, center_child); | |
152 | |
153 host_->Layout(); | |
154 | |
155 EXPECT_EQ(kMaxSize.width(), GetBoundsInHost(center_child).x()); | |
156 } | |
157 | |
158 TEST_F(ThreeViewLayoutTest, ViewsAddedToCorrectContainers) { | |
159 views::View* start_child = new TestView(); | |
160 views::View* center_child = new TestView(); | |
161 views::View* end_child = new TestView(); | |
162 | |
163 layout_->AddView(ThreeViewLayout::START, start_child); | |
164 layout_->AddView(ThreeViewLayout::CENTER, center_child); | |
165 layout_->AddView(ThreeViewLayout::END, end_child); | |
166 | |
167 EXPECT_TRUE(GetContainer(ThreeViewLayout::START)->Contains(start_child)); | |
168 EXPECT_EQ(1, GetContainer(ThreeViewLayout::START)->child_count()); | |
169 | |
170 EXPECT_TRUE(GetContainer(ThreeViewLayout::CENTER)->Contains(center_child)); | |
171 EXPECT_EQ(1, GetContainer(ThreeViewLayout::CENTER)->child_count()); | |
172 | |
173 EXPECT_TRUE(GetContainer(ThreeViewLayout::END)->Contains(end_child)); | |
174 EXPECT_EQ(1, GetContainer(ThreeViewLayout::END)->child_count()); | |
175 } | |
176 | |
177 TEST_F(ThreeViewLayoutTest, MultipleViewsAddedToTheSameContainer) { | |
178 views::View* child1 = new TestView(); | |
179 views::View* child2 = new TestView(); | |
180 | |
181 layout_->AddView(ThreeViewLayout::START, child1); | |
182 layout_->AddView(ThreeViewLayout::START, child2); | |
183 | |
184 EXPECT_TRUE(GetContainer(ThreeViewLayout::START)->Contains(child1)); | |
185 EXPECT_TRUE(GetContainer(ThreeViewLayout::START)->Contains(child2)); | |
186 } | |
187 | |
188 TEST_F(ThreeViewLayoutTest, ViewsRemovedOnSetViewToTheSameContainer) { | |
189 views::View* child1 = new TestView(); | |
190 views::View* child2 = new TestView(); | |
191 | |
192 layout_->AddView(ThreeViewLayout::START, child1); | |
193 EXPECT_TRUE(GetContainer(ThreeViewLayout::START)->Contains(child1)); | |
194 | |
195 layout_->SetView(ThreeViewLayout::START, child2); | |
196 EXPECT_TRUE(GetContainer(ThreeViewLayout::START)->Contains(child2)); | |
197 EXPECT_EQ(1, GetContainer(ThreeViewLayout::START)->child_count()); | |
198 } | |
199 | |
200 TEST_F(ThreeViewLayoutTest, Insets) { | |
201 host_->SetBounds(0, 0, 100, 10); | |
202 layout_->SetInsets(gfx::Insets(3)); | |
203 views::View* start_child = new TestView(gfx::Size(10, 10)); | |
204 views::View* center_child = new TestView(gfx::Size(100, 10)); | |
205 views::View* end_child = new TestView(gfx::Size(10, 10)); | |
206 | |
207 layout_->AddView(ThreeViewLayout::START, start_child); | |
208 layout_->AddView(ThreeViewLayout::CENTER, center_child); | |
209 layout_->AddView(ThreeViewLayout::END, end_child); | |
210 | |
211 layout_->SetFlexForContainer(ThreeViewLayout::CENTER, 1.f); | |
212 host_->Layout(); | |
213 | |
214 EXPECT_EQ(gfx::Rect(3, 3, 10, 4), GetBoundsInHost(start_child)); | |
215 EXPECT_EQ(gfx::Rect(13, 3, 74, 4), GetBoundsInHost(center_child)); | |
216 EXPECT_EQ(gfx::Rect(87, 3, 10, 4), GetBoundsInHost(end_child)); | |
217 } | |
218 | |
219 TEST_F(ThreeViewLayoutTest, InvisibleContainerDoesntTakeUpSpace) { | |
220 host_->SetBounds(0, 0, 30, 10); | |
221 views::View* start_child = new TestView(gfx::Size(10, 10)); | |
222 views::View* center_child = new TestView(gfx::Size(10, 10)); | |
223 views::View* end_child = new TestView(gfx::Size(10, 10)); | |
224 | |
225 layout_->AddView(ThreeViewLayout::START, start_child); | |
226 layout_->AddView(ThreeViewLayout::CENTER, center_child); | |
227 layout_->AddView(ThreeViewLayout::END, end_child); | |
228 | |
229 layout_->SetContainerVisible(ThreeViewLayout::START, false); | |
230 host_->Layout(); | |
231 | |
232 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), GetBoundsInHost(start_child)); | |
233 EXPECT_EQ(0, GetBoundsInHost(center_child).x()); | |
234 EXPECT_EQ(10, GetBoundsInHost(end_child).x()); | |
235 | |
236 layout_->SetContainerVisible(ThreeViewLayout::START, true); | |
237 host_->Layout(); | |
238 | |
239 EXPECT_EQ(0, GetBoundsInHost(start_child).x()); | |
240 EXPECT_EQ(10, GetBoundsInHost(center_child).x()); | |
241 EXPECT_EQ(20, GetBoundsInHost(end_child).x()); | |
242 } | |
243 | |
244 TEST_F(ThreeViewLayoutTest, NonZeroFlex) { | |
245 const gfx::Size kDefaultViewSize(10, 10); | |
246 const gfx::Size kCenterViewSize(100, 10); | |
247 const gfx::Size kExpectedCenterViewSize(80, 10); | |
248 host_->SetBounds(0, 0, 100, 10); | |
249 views::View* start_child = new TestView(kDefaultViewSize); | |
250 views::View* center_child = new TestView(kCenterViewSize); | |
251 views::View* end_child = new TestView(kDefaultViewSize); | |
252 | |
253 layout_->AddView(ThreeViewLayout::START, start_child); | |
254 layout_->AddView(ThreeViewLayout::CENTER, center_child); | |
255 layout_->AddView(ThreeViewLayout::END, end_child); | |
256 | |
257 layout_->SetFlexForContainer(ThreeViewLayout::CENTER, 1.f); | |
258 host_->Layout(); | |
259 | |
260 EXPECT_EQ(kDefaultViewSize, GetBoundsInHost(start_child).size()); | |
261 EXPECT_EQ(kExpectedCenterViewSize, GetBoundsInHost(center_child).size()); | |
262 EXPECT_EQ(kDefaultViewSize, GetBoundsInHost(end_child).size()); | |
263 } | |
264 | |
265 TEST_F(ThreeViewLayoutTest, NonZeroFlexTakesPrecedenceOverMaxSize) { | |
tdanderson
2016/10/19 18:30:59
Can you document this expected precedence behavior
bruthig
2016/10/20 04:40:12
Done. And added another test because this applies
tdanderson
2016/10/20 18:16:49
lg, thanks
| |
266 const gfx::Size kViewSize(10, 10); | |
267 const gfx::Size kMaxCenterSize(20, 10); | |
268 const gfx::Size kExpectedMaxCenterSize(80, 10); | |
269 host_->SetBounds(0, 0, 100, 10); | |
270 views::View* start_child = new TestView(kViewSize); | |
271 views::View* center_child = new TestView(kViewSize); | |
272 views::View* end_child = new TestView(kViewSize); | |
273 | |
274 layout_->AddView(ThreeViewLayout::START, start_child); | |
275 layout_->AddView(ThreeViewLayout::CENTER, center_child); | |
276 layout_->AddView(ThreeViewLayout::END, end_child); | |
277 | |
278 layout_->SetFlexForContainer(ThreeViewLayout::CENTER, 1.f); | |
279 layout_->SetMaxSize(ThreeViewLayout::CENTER, kMaxCenterSize); | |
280 host_->Layout(); | |
281 | |
282 EXPECT_EQ(kViewSize, GetBoundsInHost(start_child).size()); | |
283 EXPECT_EQ(kExpectedMaxCenterSize, | |
284 GetBoundsInHost(GetContainer(ThreeViewLayout::CENTER)).size()); | |
285 EXPECT_EQ(kViewSize, GetBoundsInHost(end_child).size()); | |
286 } | |
287 | |
288 } // namespace ash | |
OLD | NEW |