OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/bubble/bubble_border.h" | 5 #include "ui/views/bubble/bubble_border.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/strings/stringprintf.h" | |
7 #include "ui/views/test/views_test_base.h" | 9 #include "ui/views/test/views_test_base.h" |
8 | 10 |
9 namespace views { | 11 namespace views { |
10 | 12 |
11 typedef ViewsTestBase BubbleBorderTest; | 13 typedef views::ViewsTestBase BubbleBorderTest; |
12 | 14 |
13 TEST_F(BubbleBorderTest, GetMirroredArrow) { | 15 TEST_F(BubbleBorderTest, GetMirroredArrow) { |
14 // Horizontal mirroring. | 16 // Horizontal mirroring. |
15 EXPECT_EQ(BubbleBorder::TOP_RIGHT, | 17 EXPECT_EQ(BubbleBorder::TOP_RIGHT, |
16 BubbleBorder::horizontal_mirror(BubbleBorder::TOP_LEFT)); | 18 BubbleBorder::horizontal_mirror(BubbleBorder::TOP_LEFT)); |
17 EXPECT_EQ(BubbleBorder::TOP_LEFT, | 19 EXPECT_EQ(BubbleBorder::TOP_LEFT, |
18 BubbleBorder::horizontal_mirror(BubbleBorder::TOP_RIGHT)); | 20 BubbleBorder::horizontal_mirror(BubbleBorder::TOP_RIGHT)); |
19 | 21 |
20 EXPECT_EQ(BubbleBorder::BOTTOM_RIGHT, | 22 EXPECT_EQ(BubbleBorder::BOTTOM_RIGHT, |
21 BubbleBorder::horizontal_mirror(BubbleBorder::BOTTOM_LEFT)); | 23 BubbleBorder::horizontal_mirror(BubbleBorder::BOTTOM_LEFT)); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::TOP_CENTER)); | 197 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::TOP_CENTER)); |
196 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::BOTTOM_CENTER)); | 198 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::BOTTOM_CENTER)); |
197 | 199 |
198 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::LEFT_CENTER)); | 200 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::LEFT_CENTER)); |
199 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::RIGHT_CENTER)); | 201 EXPECT_TRUE(BubbleBorder::is_arrow_at_center(BubbleBorder::RIGHT_CENTER)); |
200 | 202 |
201 EXPECT_FALSE(BubbleBorder::is_arrow_at_center(BubbleBorder::NONE)); | 203 EXPECT_FALSE(BubbleBorder::is_arrow_at_center(BubbleBorder::NONE)); |
202 EXPECT_FALSE(BubbleBorder::is_arrow_at_center(BubbleBorder::FLOAT)); | 204 EXPECT_FALSE(BubbleBorder::is_arrow_at_center(BubbleBorder::FLOAT)); |
203 } | 205 } |
204 | 206 |
205 TEST_F(BubbleBorderTest, TestMinimalSize) { | 207 TEST_F(BubbleBorderTest, GetSizeForContentsSizeTest) { |
206 gfx::Rect anchor = gfx::Rect(100, 100, 20, 20); | 208 views::BubbleBorder border(BubbleBorder::NONE, |
207 gfx::Size contents = gfx::Size(10, 10); | 209 BubbleBorder::NO_SHADOW, |
208 BubbleBorder b1(BubbleBorder::RIGHT_TOP, BubbleBorder::NO_SHADOW, 0); | 210 SK_ColorWHITE); |
209 | 211 |
210 // The height should be much bigger then the requested size + border and | 212 const views::internal::BorderImages* kImages = border.GetImagesForTest(); |
211 // padding since it needs to be able to include the tip bitmap. | 213 |
212 gfx::Rect visible_tip_1 = b1.GetBounds(anchor, contents); | 214 // kSmallSize is smaller than the minimum allowable size and does not |
213 EXPECT_GE(visible_tip_1.height(), 30); | 215 // contribute to the resulting size. |
214 EXPECT_LE(visible_tip_1.width(), 30); | 216 const gfx::Size kSmallSize = gfx::Size(1, 2); |
215 | 217 // kMediumSize is larger than the minimum allowable size and contributes to |
216 // With the tip being invisible the height should now be much smaller. | 218 // the resulting size. |
217 b1.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); | 219 const gfx::Size kMediumSize = gfx::Size(50, 60); |
218 gfx::Rect invisible_tip_1 = b1.GetBounds(anchor, contents); | 220 |
219 EXPECT_LE(invisible_tip_1.height(), 30); | 221 const gfx::Size kSmallHorizArrow( |
220 EXPECT_LE(invisible_tip_1.width(), 30); | 222 2 * kImages->border_thickness + kImages->top_arrow.width(), |
221 | 223 kImages->border_thickness + kImages->arrow_thickness + |
222 // When the orientation of the tip changes, the above mentioned tests need to | 224 kImages->border_interior_thickness); |
223 // be reverse for width and height. | 225 |
224 BubbleBorder b2(BubbleBorder::TOP_RIGHT, BubbleBorder::NO_SHADOW, 0); | 226 const gfx::Size kSmallVertArrow(kSmallHorizArrow.height(), |
225 | 227 kSmallHorizArrow.width()); |
msw
2014/08/23 00:29:12
nit: indent to match open paren.
bruthig
2014/08/25 14:47:28
Done.
| |
226 // The width should be much bigger then the requested size + border and | 228 |
227 // padding since it needs to be able to include the tip bitmap. | 229 const gfx::Size kSmallNoArrow(2 * kImages->border_thickness, |
228 gfx::Rect visible_tip_2 = b2.GetBounds(anchor, contents); | 230 2 * kImages->border_thickness); |
229 EXPECT_GE(visible_tip_2.width(), 30); | 231 |
230 EXPECT_LE(visible_tip_2.height(), 30); | 232 const gfx::Size kMediumHorizArrow( |
231 | 233 kMediumSize.width() + 2 * border.GetBorderThickness(), |
232 // With the tip being invisible the width should now be much smaller. | 234 kMediumSize.height() + border.GetBorderThickness() + |
233 b2.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); | 235 kImages->arrow_thickness); |
234 gfx::Rect invisible_tip_2 = b2.GetBounds(anchor, contents); | 236 |
235 EXPECT_LE(invisible_tip_2.width(), 30); | 237 const gfx::Size kMediumVertArrow( |
236 EXPECT_LE(invisible_tip_2.height(), 30); | 238 kMediumSize.width() + border.GetBorderThickness() + |
239 kImages->arrow_thickness, | |
240 kMediumSize.height() + 2 * border.GetBorderThickness()); | |
241 | |
242 const gfx::Size kMediumNoArrow( | |
243 kMediumSize.width() + 2 * border.GetBorderThickness(), | |
244 kMediumSize.height() + 2 * border.GetBorderThickness()); | |
245 | |
246 struct TestCase { | |
247 BubbleBorder::Arrow arrow; | |
248 gfx::Size content; | |
249 gfx::Size expected_with_arrow; | |
250 gfx::Size expected_without_arrow; | |
251 }; | |
252 | |
253 TestCase cases[] = { | |
254 // Content size: kSmallSize | |
255 { BubbleBorder::TOP_LEFT, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, | |
256 { BubbleBorder::TOP_CENTER, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, | |
257 { BubbleBorder::TOP_RIGHT, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, | |
258 { BubbleBorder::BOTTOM_LEFT, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, | |
259 { BubbleBorder::BOTTOM_CENTER, kSmallSize, kSmallHorizArrow, | |
260 kSmallNoArrow }, | |
261 { BubbleBorder::BOTTOM_RIGHT, kSmallSize, kSmallHorizArrow, kSmallNoArrow }, | |
262 { BubbleBorder::LEFT_TOP, kSmallSize, kSmallVertArrow, kSmallNoArrow }, | |
263 { BubbleBorder::LEFT_CENTER, kSmallSize, kSmallVertArrow, kSmallNoArrow }, | |
264 { BubbleBorder::LEFT_BOTTOM, kSmallSize, kSmallVertArrow, kSmallNoArrow }, | |
265 { BubbleBorder::RIGHT_TOP, kSmallSize, kSmallVertArrow, kSmallNoArrow }, | |
266 { BubbleBorder::RIGHT_CENTER, kSmallSize, kSmallVertArrow, kSmallNoArrow }, | |
267 { BubbleBorder::RIGHT_BOTTOM, kSmallSize, kSmallVertArrow, kSmallNoArrow }, | |
268 { BubbleBorder::NONE, kSmallSize, kSmallNoArrow, kSmallNoArrow }, | |
269 { BubbleBorder::FLOAT, kSmallSize, kSmallNoArrow, kSmallNoArrow }, | |
270 | |
271 // Content size: kMediumSize | |
272 { BubbleBorder::TOP_LEFT, kMediumSize, kMediumHorizArrow, kMediumNoArrow }, | |
273 { BubbleBorder::TOP_CENTER, kMediumSize, kMediumHorizArrow, | |
274 kMediumNoArrow }, | |
275 { BubbleBorder::TOP_RIGHT, kMediumSize, kMediumHorizArrow, kMediumNoArrow }, | |
276 { BubbleBorder::BOTTOM_LEFT, kMediumSize, kMediumHorizArrow, | |
277 kMediumNoArrow }, | |
278 { BubbleBorder::BOTTOM_CENTER, kMediumSize, kMediumHorizArrow, | |
279 kMediumNoArrow }, | |
280 { BubbleBorder::BOTTOM_RIGHT, kMediumSize, kMediumHorizArrow, | |
281 kMediumNoArrow }, | |
282 { BubbleBorder::LEFT_TOP, kMediumSize, kMediumVertArrow, kMediumNoArrow }, | |
283 { BubbleBorder::LEFT_CENTER, kMediumSize, kMediumVertArrow, | |
284 kMediumNoArrow }, | |
285 { BubbleBorder::LEFT_BOTTOM, kMediumSize, kMediumVertArrow, | |
286 kMediumNoArrow }, | |
287 { BubbleBorder::RIGHT_TOP, kMediumSize, kMediumVertArrow, kMediumNoArrow }, | |
288 { BubbleBorder::RIGHT_CENTER, kMediumSize, kMediumVertArrow, | |
289 kMediumNoArrow }, | |
290 { BubbleBorder::RIGHT_BOTTOM, kMediumSize, kMediumVertArrow, | |
291 kMediumNoArrow }, | |
292 { BubbleBorder::NONE, kMediumSize, kMediumNoArrow, kMediumNoArrow }, | |
293 { BubbleBorder::FLOAT, kMediumSize, kMediumNoArrow, kMediumNoArrow } | |
294 }; | |
295 | |
296 for (size_t i = 0; i < arraysize(cases); ++i) { | |
297 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d", | |
298 static_cast<int>(i), cases[i].arrow)); | |
299 | |
300 border.set_arrow(cases[i].arrow); | |
301 | |
302 border.set_paint_arrow(BubbleBorder::PAINT_NORMAL); | |
303 EXPECT_EQ(cases[i].expected_with_arrow, | |
304 border.GetSizeForContentsSize(cases[i].content)); | |
305 | |
306 border.set_paint_arrow(BubbleBorder::PAINT_TRANSPARENT); | |
307 EXPECT_EQ(cases[i].expected_with_arrow, | |
308 border.GetSizeForContentsSize(cases[i].content)); | |
309 | |
310 border.set_paint_arrow(BubbleBorder::PAINT_NONE); | |
311 EXPECT_EQ(cases[i].expected_without_arrow, | |
312 border.GetSizeForContentsSize(cases[i].content)); | |
313 } | |
237 } | 314 } |
238 | 315 |
316 TEST_F(BubbleBorderTest, GetBoundsOriginTest) { | |
317 views::BubbleBorder border(BubbleBorder::TOP_LEFT, | |
318 BubbleBorder::NO_SHADOW, | |
319 SK_ColorWHITE); | |
320 | |
321 const gfx::Rect kAnchor(100, 100, 20, 30); | |
322 const gfx::Size kContentSize(50, 60); | |
323 | |
324 const views::internal::BorderImages* kImages = border.GetImagesForTest(); | |
325 | |
326 const int kStroke = BubbleBorder::kStroke; | |
327 | |
328 border.set_arrow(BubbleBorder::TOP_LEFT); | |
329 const gfx::Size kTotalSizeWithHorizArrow = | |
330 border.GetSizeForContentsSize(kContentSize); | |
331 | |
332 border.set_arrow(BubbleBorder::RIGHT_BOTTOM); | |
333 const gfx::Size kTotalSizeWithVertArrow = | |
334 border.GetSizeForContentsSize(kContentSize); | |
335 | |
336 border.set_arrow(BubbleBorder::NONE); | |
337 const gfx::Size kTotalSizeWithNoArrow = | |
338 border.GetSizeForContentsSize(kContentSize); | |
339 | |
340 const int kBorderThickness = border.GetBorderThickness(); | |
341 | |
342 const int kMidAnchorWidth = kAnchor.width() / 2; | |
343 const int kMidAnchorHeight = kAnchor.height() / 2; | |
344 | |
345 const int kArrowOffsetForHorizCenter = kTotalSizeWithHorizArrow.width() / 2; | |
346 const int kArrowOffsetForVertCenter = kTotalSizeWithVertArrow.height() / 2; | |
347 const int kArrowOffsetForNotCenter = | |
348 kImages->border_thickness + (kImages->top_arrow.width() / 2); | |
349 | |
350 const int kArrowSize = | |
351 kImages->arrow_interior_thickness + kStroke - kImages->arrow_thickness; | |
352 | |
353 const int kTopHorizArrowY = kAnchor.y() + kAnchor.height() + kArrowSize; | |
354 const int kBottomHorizArrowY = | |
355 kAnchor.y() - kArrowSize - kTotalSizeWithHorizArrow.height(); | |
356 | |
357 const int kLeftVertArrowX = kAnchor.x() + kAnchor.width() + kArrowSize; | |
358 const int kRightVertArrowX = | |
359 kAnchor.x() - kArrowSize - kTotalSizeWithVertArrow.width(); | |
360 | |
361 struct TestCase { | |
362 BubbleBorder::Arrow arrow; | |
363 int expected_arrow_to_mid_anchor_x; | |
364 int expected_arrow_to_mid_anchor_y; | |
365 int expected_edge_to_anchor_edge_anchor_x; | |
366 int expected_edge_to_anchor_edge_anchor_y; | |
367 }; | |
368 | |
369 const int kTodo = 0; | |
bruthig
2014/08/22 22:55:04
Let me know if you like this simplification and I
msw
2014/08/23 00:29:12
This doesn't seem like a big win, I'll leave it up
| |
370 | |
371 TestCase cases[] = { | |
372 // Horizontal arrow tests. | |
373 { BubbleBorder::TOP_LEFT, kAnchor.x() + kMidAnchorWidth - | |
374 kArrowOffsetForNotCenter, kTopHorizArrowY, kAnchor.x() + kStroke - | |
375 kBorderThickness, kTopHorizArrowY }, | |
376 { BubbleBorder::TOP_CENTER, kAnchor.x() + kMidAnchorWidth - | |
377 kArrowOffsetForHorizCenter, kTopHorizArrowY, kTodo, kTodo }, | |
378 { BubbleBorder::TOP_RIGHT, kTodo, kTodo, kTodo, kTodo }, | |
379 { BubbleBorder::BOTTOM_LEFT, kTodo, kTodo, kTodo, kTodo }, | |
380 { BubbleBorder::BOTTOM_CENTER, kTodo, kTodo, kTodo, kTodo }, | |
381 { BubbleBorder::BOTTOM_RIGHT, kAnchor.x() + kMidAnchorWidth + | |
382 kArrowOffsetForNotCenter - kTotalSizeWithHorizArrow.width(), | |
383 kBottomHorizArrowY, kAnchor.x() + kAnchor.width() - | |
384 kTotalSizeWithHorizArrow.width() + kBorderThickness - kStroke, | |
385 kBottomHorizArrowY }, | |
386 | |
387 // Vertical arrow tests. | |
388 { BubbleBorder::LEFT_TOP, kLeftVertArrowX, kAnchor.y() + kMidAnchorHeight - | |
389 kArrowOffsetForNotCenter, kLeftVertArrowX, kAnchor.y() + kStroke - | |
390 kBorderThickness }, | |
391 { BubbleBorder::LEFT_CENTER, kLeftVertArrowX, kAnchor.y() + | |
392 kMidAnchorHeight - kArrowOffsetForVertCenter, kTodo, kTodo }, | |
393 { BubbleBorder::LEFT_BOTTOM, kTodo, kTodo, kTodo, kTodo }, | |
394 { BubbleBorder::RIGHT_TOP, kTodo, kTodo, kTodo, kTodo }, | |
395 { BubbleBorder::RIGHT_CENTER, kTodo, kTodo, kTodo, kTodo }, | |
396 { BubbleBorder::RIGHT_BOTTOM, kRightVertArrowX, kAnchor.y() + | |
397 kMidAnchorHeight + kArrowOffsetForNotCenter - | |
398 kTotalSizeWithVertArrow.height(), kRightVertArrowX, kAnchor.y() + | |
399 kAnchor.height() - kTotalSizeWithVertArrow.height() + | |
400 kBorderThickness - kStroke }, | |
401 | |
402 // No arrow tests. | |
403 { BubbleBorder::NONE, kAnchor.x() + (kAnchor.width() - | |
404 kTotalSizeWithNoArrow.width()) / 2, kAnchor.y() + kAnchor.height(), | |
405 kTodo, kTodo }, | |
406 { BubbleBorder::FLOAT, kAnchor.x() + (kAnchor.width() - | |
407 kTotalSizeWithNoArrow.width()) / 2, kAnchor.y() + (kAnchor.height() - | |
408 kTotalSizeWithNoArrow.height()) / 2, kTodo, kTodo } | |
409 }; | |
410 | |
411 gfx::Point origin; | |
412 | |
413 for (size_t i = 0; i < arraysize(cases); ++i) { | |
414 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d", | |
415 static_cast<int>(i), cases[i].arrow)); | |
416 border.set_arrow(cases[i].arrow); | |
417 | |
418 border.set_alignment(BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR); | |
419 origin = border.GetBounds(kAnchor, kContentSize).origin(); | |
420 EXPECT_EQ(cases[i].expected_arrow_to_mid_anchor_x, origin.x()); | |
421 EXPECT_EQ(cases[i].expected_arrow_to_mid_anchor_y, origin.y()); | |
422 | |
423 border.set_alignment(BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); | |
424 origin = border.GetBounds(kAnchor, kContentSize).origin(); | |
425 EXPECT_EQ(cases[i].expected_edge_to_anchor_edge_anchor_x, origin.x()); | |
426 EXPECT_EQ(cases[i].expected_edge_to_anchor_edge_anchor_y, origin.y()); | |
427 } | |
428 } | |
239 | 429 |
240 } // namespace views | 430 } // namespace views |
OLD | NEW |