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()); |
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 border.set_arrow(BubbleBorder::TOP_LEFT); | |
327 const gfx::Size kTotalSizeWithHorizArrow = | |
328 border.GetSizeForContentsSize(kContentSize); | |
329 | |
330 border.set_arrow(BubbleBorder::RIGHT_BOTTOM); | |
331 const gfx::Size kTotalSizeWithVertArrow = | |
332 border.GetSizeForContentsSize(kContentSize); | |
333 | |
334 border.set_arrow(BubbleBorder::NONE); | |
335 const gfx::Size kTotalSizeWithNoArrow = | |
336 border.GetSizeForContentsSize(kContentSize); | |
337 | |
338 const int kBorderThickness = border.GetBorderThickness(); | |
339 | |
340 const int kMidAnchorWidth = kAnchor.width() / 2; | |
msw
2014/08/25 18:01:45
nit: make this kMidAnchorX for simplicity, ditto f
bruthig
2014/08/25 18:22:21
Done.
msw
2014/08/25 18:48:17
Actually, remove these and do these replacements b
bruthig
2014/08/25 20:15:03
Done.
| |
341 const int kMidAnchorHeight = kAnchor.height() / 2; | |
342 | |
343 const int kArrowOffsetForHorizCenter = kTotalSizeWithHorizArrow.width() / 2; | |
344 const int kArrowOffsetForVertCenter = kTotalSizeWithVertArrow.height() / 2; | |
345 const int kArrowOffsetForNotCenter = | |
346 kImages->border_thickness + (kImages->top_arrow.width() / 2); | |
347 | |
348 const int kArrowSize = | |
349 kImages->arrow_interior_thickness + BubbleBorder::kStroke - | |
350 kImages->arrow_thickness; | |
351 | |
352 const int kTopHorizArrowY = kAnchor.y() + kAnchor.height() + kArrowSize; | |
353 const int kBottomHorizArrowY = | |
354 kAnchor.y() - kArrowSize - kTotalSizeWithHorizArrow.height(); | |
355 | |
356 const int kLeftVertArrowX = kAnchor.x() + kAnchor.width() + kArrowSize; | |
357 const int kRightVertArrowX = | |
358 kAnchor.x() - kArrowSize - kTotalSizeWithVertArrow.width(); | |
359 | |
360 struct TestCase { | |
361 BubbleBorder::Arrow arrow; | |
362 BubbleBorder::BubbleAlignment alignment; | |
363 int expectedX; | |
364 int expectedY; | |
msw
2014/08/25 18:01:45
nit: |expected_x|, ditto for y.
bruthig
2014/08/25 18:22:21
Done.
| |
365 }; | |
366 | |
367 TestCase cases[] = { | |
368 // Horizontal arrow tests. | |
369 { BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
370 kAnchor.x() + kMidAnchorWidth - kArrowOffsetForNotCenter, | |
msw
2014/08/25 18:01:45
nit: indent to align with "Bubble" above; ditto el
bruthig
2014/08/25 18:22:21
Done.
| |
371 kTopHorizArrowY }, | |
372 { BubbleBorder::TOP_LEFT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | |
373 kAnchor.x() + BubbleBorder::kStroke - kBorderThickness, | |
374 kTopHorizArrowY }, | |
375 { BubbleBorder::TOP_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
376 kAnchor.x() + kMidAnchorWidth - kArrowOffsetForHorizCenter, | |
377 kTopHorizArrowY }, | |
378 { BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
379 kAnchor.x() + kMidAnchorWidth + kArrowOffsetForNotCenter - | |
380 kTotalSizeWithHorizArrow.width(), | |
381 kBottomHorizArrowY }, | |
382 { BubbleBorder::BOTTOM_RIGHT, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | |
383 kAnchor.x() + kAnchor.width() - kTotalSizeWithHorizArrow.width() + | |
384 kBorderThickness - BubbleBorder::kStroke, | |
385 kBottomHorizArrowY }, | |
386 | |
387 // Vertical arrow tests. | |
388 { BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
389 kLeftVertArrowX, | |
390 kAnchor.y() + kMidAnchorHeight - kArrowOffsetForNotCenter }, | |
391 { BubbleBorder::LEFT_TOP, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | |
392 kLeftVertArrowX, | |
393 kAnchor.y() + BubbleBorder::kStroke - kBorderThickness }, | |
394 { BubbleBorder::LEFT_CENTER, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
395 kLeftVertArrowX, | |
396 kAnchor.y() + kMidAnchorHeight - kArrowOffsetForVertCenter }, | |
397 { BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
398 kRightVertArrowX, | |
399 kAnchor.y() + kMidAnchorHeight + kArrowOffsetForNotCenter - | |
400 kTotalSizeWithVertArrow.height() }, | |
401 { BubbleBorder::RIGHT_BOTTOM, BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, | |
402 kRightVertArrowX, | |
403 kAnchor.y() + kAnchor.height() - kTotalSizeWithVertArrow.height() + | |
404 kBorderThickness - BubbleBorder::kStroke }, | |
405 | |
406 // No arrow tests. | |
407 { BubbleBorder::NONE, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
408 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, | |
409 kAnchor.y() + kAnchor.height() }, | |
410 { BubbleBorder::FLOAT, BubbleBorder::ALIGN_ARROW_TO_MID_ANCHOR, | |
411 kAnchor.x() + (kAnchor.width() - kTotalSizeWithNoArrow.width()) / 2, | |
412 kAnchor.y() + (kAnchor.height() - kTotalSizeWithNoArrow.height()) / 2 }, | |
413 }; | |
414 | |
415 for (size_t i = 0; i < arraysize(cases); ++i) { | |
416 SCOPED_TRACE(base::StringPrintf("i=%d arrow=%d alignment=%d", | |
417 static_cast<int>(i), cases[i].arrow, cases[i].alignment)); | |
418 border.set_arrow(cases[i].arrow); | |
419 border.set_alignment(cases[i].alignment); | |
420 | |
421 gfx::Point origin = border.GetBounds(kAnchor, kContentSize).origin(); | |
422 EXPECT_EQ(cases[i].expectedX, origin.x()); | |
423 EXPECT_EQ(cases[i].expectedY, origin.y()); | |
424 } | |
425 } | |
239 | 426 |
240 } // namespace views | 427 } // namespace views |
OLD | NEW |