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/controls/label.h" | 5 #include "ui/views/controls/label.h" |
6 | 6 |
7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "ui/accessibility/ax_view_state.h" | 10 #include "ui/accessibility/ax_view_state.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 70 |
71 Label label; | 71 Label label; |
72 for (size_t i = 0; i < 2; ++i) { | 72 for (size_t i = 0; i < 2; ++i) { |
73 // Toggle the application default text direction (to try each direction). | 73 // Toggle the application default text direction (to try each direction). |
74 SetRTL(!base::i18n::IsRTL()); | 74 SetRTL(!base::i18n::IsRTL()); |
75 bool reverse_alignment = base::i18n::IsRTL(); | 75 bool reverse_alignment = base::i18n::IsRTL(); |
76 | 76 |
77 // The alignment should be flipped in RTL UI. | 77 // The alignment should be flipped in RTL UI. |
78 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | 78 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); |
79 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_LEFT : gfx::ALIGN_RIGHT, | 79 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_LEFT : gfx::ALIGN_RIGHT, |
80 label.GetHorizontalAlignment()); | 80 label.horizontal_alignment()); |
81 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | 81 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); |
82 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT, | 82 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT, |
83 label.GetHorizontalAlignment()); | 83 label.horizontal_alignment()); |
84 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); | 84 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); |
85 EXPECT_EQ(gfx::ALIGN_CENTER, label.GetHorizontalAlignment()); | 85 EXPECT_EQ(gfx::ALIGN_CENTER, label.horizontal_alignment()); |
86 | 86 |
87 for (size_t j = 0; j < 2; ++j) { | 87 for (size_t j = 0; j < 2; ++j) { |
88 label.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); | 88 label.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); |
89 const bool rtl = j == 0; | 89 const bool rtl = j == 0; |
90 label.SetText(rtl ? base::WideToUTF16(L"\x5d0") : ASCIIToUTF16("A")); | 90 label.SetText(rtl ? base::WideToUTF16(L"\x5d0") : ASCIIToUTF16("A")); |
91 EXPECT_EQ(rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT, | 91 EXPECT_EQ(gfx::ALIGN_TO_HEAD, label.horizontal_alignment()); |
92 label.GetHorizontalAlignment()); | |
93 } | 92 } |
94 } | 93 } |
95 | 94 |
96 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); | 95 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); |
97 } | 96 } |
98 | 97 |
99 TEST_F(LabelTest, MultiLineProperty) { | 98 TEST_F(LabelTest, MultiLineProperty) { |
100 Label label; | 99 Label label; |
101 EXPECT_FALSE(label.multi_line()); | 100 EXPECT_FALSE(label.multi_line()); |
102 label.SetMultiLine(true); | 101 label.SetMultiLine(true); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 label.SetObscured(true); | 179 label.SetObscured(true); |
181 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); | 180 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); |
182 | 181 |
183 // Obscuring the text shouldn't permanently clobber the tooltip. | 182 // Obscuring the text shouldn't permanently clobber the tooltip. |
184 label.SetObscured(false); | 183 label.SetObscured(false); |
185 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 184 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); |
186 | 185 |
187 // Making the label multiline shouldn't eliminate the tooltip. | 186 // Making the label multiline shouldn't eliminate the tooltip. |
188 label.SetMultiLine(true); | 187 label.SetMultiLine(true); |
189 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 188 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); |
| 189 |
190 // Expanding the multiline label bounds should eliminate the tooltip. | 190 // Expanding the multiline label bounds should eliminate the tooltip. |
191 label.SetBounds(0, 0, 1000, 1000); | 191 label.SetBounds(0, 0, 1000, 1000); |
192 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); | 192 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); |
193 | 193 |
194 // Verify that setting the tooltip still shows it. | 194 // Verify that setting the tooltip still shows it. |
195 label.SetTooltipText(tooltip_text); | 195 label.SetTooltipText(tooltip_text); |
196 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 196 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); |
197 EXPECT_EQ(tooltip_text, tooltip); | 197 EXPECT_EQ(tooltip_text, tooltip); |
198 // Clear out the tooltip. | 198 // Clear out the tooltip. |
199 label.SetTooltipText(base::string16()); | 199 label.SetTooltipText(base::string16()); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 | 325 |
326 // GetPreferredSize and borders. | 326 // GetPreferredSize and borders. |
327 label.SetBounds(0, 0, 0, 0); | 327 label.SetBounds(0, 0, 0, 0); |
328 gfx::Size required_size_with_border = label.GetPreferredSize(); | 328 gfx::Size required_size_with_border = label.GetPreferredSize(); |
329 EXPECT_EQ(required_size_with_border.height(), | 329 EXPECT_EQ(required_size_with_border.height(), |
330 required_size.height() + border.height()); | 330 required_size.height() + border.height()); |
331 EXPECT_EQ(required_size_with_border.width(), | 331 EXPECT_EQ(required_size_with_border.width(), |
332 required_size.width() + border.width()); | 332 required_size.width() + border.width()); |
333 } | 333 } |
334 | 334 |
335 TEST_F(LabelTest, DirectionalityFromText) { | |
336 Label label; | |
337 label.SetBounds(0, 0, 1000, 1000); | |
338 base::string16 paint_text; | |
339 gfx::Rect text_bounds; | |
340 int flags = -1; | |
341 | |
342 // Test text starts with RTL character. | |
343 label.SetText(base::WideToUTF16(L" \x5d0\x5d1\x5d2 abc")); | |
344 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
345 EXPECT_EQ(gfx::Canvas::FORCE_RTL_DIRECTIONALITY, | |
346 flags & (gfx::Canvas::FORCE_RTL_DIRECTIONALITY | | |
347 gfx::Canvas::FORCE_LTR_DIRECTIONALITY)); | |
348 | |
349 // Test text starts with LTR character. | |
350 label.SetText(base::WideToUTF16(L"ltr \x5d0\x5d1\x5d2 abc")); | |
351 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
352 EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, | |
353 flags & (gfx::Canvas::FORCE_RTL_DIRECTIONALITY | | |
354 gfx::Canvas::FORCE_LTR_DIRECTIONALITY)); | |
355 } | |
356 | |
357 TEST_F(LabelTest, DrawSingleLineString) { | |
358 Label label; | |
359 label.SetFocusable(false); | |
360 | |
361 label.SetText(ASCIIToUTF16("Here's a string with no returns.")); | |
362 gfx::Size required_size(label.GetPreferredSize()); | |
363 gfx::Size extra(22, 8); | |
364 label.SetBounds(0, 0, required_size.width() + extra.width(), | |
365 required_size.height() + extra.height()); | |
366 | |
367 // Do some basic verifications for all three alignments. | |
368 base::string16 paint_text; | |
369 gfx::Rect text_bounds; | |
370 int flags = -1; | |
371 | |
372 // Centered text. | |
373 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
374 EXPECT_EQ(label.text(), paint_text); | |
375 // The text should be centered horizontally and vertically. | |
376 EXPECT_EQ(extra.width() / 2, text_bounds.x()); | |
377 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); | |
378 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
379 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
380 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_CENTER, | |
381 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
382 gfx::Canvas::TEXT_ALIGN_CENTER | | |
383 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
384 | |
385 // Left aligned text. | |
386 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
387 paint_text.clear(); | |
388 text_bounds.SetRect(0, 0, 0, 0); | |
389 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
390 EXPECT_EQ(label.text(), paint_text); | |
391 // The text should be left aligned horizontally and centered vertically. | |
392 EXPECT_EQ(0, text_bounds.x()); | |
393 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); | |
394 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
395 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
396 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT, | |
397 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
398 gfx::Canvas::TEXT_ALIGN_CENTER | | |
399 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
400 | |
401 // Right aligned text. | |
402 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
403 paint_text.clear(); | |
404 text_bounds.SetRect(0, 0, 0, 0); | |
405 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
406 EXPECT_EQ(label.text(), paint_text); | |
407 // The text should be right aligned horizontally and centered vertically. | |
408 EXPECT_EQ(extra.width(), text_bounds.x()); | |
409 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); | |
410 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
411 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
412 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT, | |
413 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
414 gfx::Canvas::TEXT_ALIGN_CENTER | | |
415 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
416 | |
417 // Test single line drawing with a border. | |
418 gfx::Insets border(39, 34, 8, 96); | |
419 label.SetBorder(Border::CreateEmptyBorder( | |
420 border.top(), border.left(), border.bottom(), border.right())); | |
421 | |
422 gfx::Size required_size_with_border(label.GetPreferredSize()); | |
423 EXPECT_EQ(required_size.width() + border.width(), | |
424 required_size_with_border.width()); | |
425 EXPECT_EQ(required_size.height() + border.height(), | |
426 required_size_with_border.height()); | |
427 label.SetBounds(0, 0, required_size_with_border.width() + extra.width(), | |
428 required_size_with_border.height() + extra.height()); | |
429 | |
430 // Centered text with border. | |
431 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
432 paint_text.clear(); | |
433 text_bounds.SetRect(0, 0, 0, 0); | |
434 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
435 EXPECT_EQ(label.text(), paint_text); | |
436 // The text should be centered horizontally and vertically within the border. | |
437 EXPECT_EQ(border.left() + extra.width() / 2, text_bounds.x()); | |
438 EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y()); | |
439 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
440 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
441 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_CENTER, | |
442 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
443 gfx::Canvas::TEXT_ALIGN_CENTER | | |
444 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
445 | |
446 // Left aligned text with border. | |
447 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
448 paint_text.clear(); | |
449 text_bounds.SetRect(0, 0, 0, 0); | |
450 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
451 EXPECT_EQ(label.text(), paint_text); | |
452 // The text should be left aligned horizontally and centered vertically. | |
453 EXPECT_EQ(border.left(), text_bounds.x()); | |
454 EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y()); | |
455 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
456 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
457 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT, | |
458 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
459 gfx::Canvas::TEXT_ALIGN_CENTER | | |
460 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
461 | |
462 // Right aligned text. | |
463 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
464 paint_text.clear(); | |
465 text_bounds.SetRect(0, 0, 0, 0); | |
466 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
467 EXPECT_EQ(label.text(), paint_text); | |
468 // The text should be right aligned horizontally and centered vertically. | |
469 EXPECT_EQ(border.left() + extra.width(), text_bounds.x()); | |
470 EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y()); | |
471 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
472 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
473 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT, | |
474 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
475 gfx::Canvas::TEXT_ALIGN_CENTER | | |
476 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
477 } | |
478 | |
479 // Pango needs a max height to elide multiline text; that is not supported here. | |
480 TEST_F(LabelTest, DrawMultiLineString) { | |
481 Label label; | |
482 label.SetFocusable(false); | |
483 // Set a background color to prevent gfx::Canvas::NO_SUBPIXEL_RENDERING flags. | |
484 label.SetBackgroundColor(SK_ColorWHITE); | |
485 | |
486 label.SetText(ASCIIToUTF16("Another string\nwith returns\n\n!")); | |
487 label.SetMultiLine(true); | |
488 label.SizeToFit(0); | |
489 gfx::Size extra(50, 10); | |
490 label.SetBounds(label.x(), label.y(), | |
491 label.width() + extra.width(), | |
492 label.height() + extra.height()); | |
493 | |
494 // Do some basic verifications for all three alignments. | |
495 base::string16 paint_text; | |
496 gfx::Rect text_bounds; | |
497 int flags = -1; | |
498 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
499 EXPECT_EQ(label.text(), paint_text); | |
500 EXPECT_EQ(extra.width() / 2, text_bounds.x()); | |
501 EXPECT_EQ(extra.height() / 2, text_bounds.y()); | |
502 EXPECT_GT(text_bounds.width(), kMinTextDimension); | |
503 EXPECT_GT(text_bounds.height(), kMinTextDimension); | |
504 int expected_flags = gfx::Canvas::MULTI_LINE | | |
505 gfx::Canvas::TEXT_ALIGN_CENTER | | |
506 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
507 #if !defined(OS_WIN) | |
508 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | |
509 #endif | |
510 EXPECT_EQ(expected_flags, expected_flags); | |
511 gfx::Rect center_bounds(text_bounds); | |
512 | |
513 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
514 paint_text.clear(); | |
515 text_bounds.SetRect(0, 0, 0, 0); | |
516 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
517 EXPECT_EQ(label.text(), paint_text); | |
518 EXPECT_EQ(0, text_bounds.x()); | |
519 EXPECT_EQ(extra.height() / 2, text_bounds.y()); | |
520 EXPECT_GT(text_bounds.width(), kMinTextDimension); | |
521 EXPECT_GT(text_bounds.height(), kMinTextDimension); | |
522 expected_flags = gfx::Canvas::MULTI_LINE | | |
523 gfx::Canvas::TEXT_ALIGN_LEFT | | |
524 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
525 #if !defined(OS_WIN) | |
526 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | |
527 #endif | |
528 EXPECT_EQ(expected_flags, expected_flags); | |
529 | |
530 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
531 paint_text.clear(); | |
532 text_bounds.SetRect(0, 0, 0, 0); | |
533 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
534 EXPECT_EQ(label.text(), paint_text); | |
535 EXPECT_EQ(extra.width(), text_bounds.x()); | |
536 EXPECT_EQ(extra.height() / 2, text_bounds.y()); | |
537 EXPECT_GT(text_bounds.width(), kMinTextDimension); | |
538 EXPECT_GT(text_bounds.height(), kMinTextDimension); | |
539 expected_flags = gfx::Canvas::MULTI_LINE | | |
540 gfx::Canvas::TEXT_ALIGN_RIGHT | | |
541 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
542 #if !defined(OS_WIN) | |
543 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | |
544 #endif | |
545 EXPECT_EQ(expected_flags, expected_flags); | |
546 | |
547 // Test multiline drawing with a border. | |
548 gfx::Insets border(19, 92, 23, 2); | |
549 label.SetBorder(Border::CreateEmptyBorder( | |
550 border.top(), border.left(), border.bottom(), border.right())); | |
551 label.SizeToFit(0); | |
552 label.SetBounds(label.x(), label.y(), | |
553 label.width() + extra.width(), | |
554 label.height() + extra.height()); | |
555 | |
556 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
557 paint_text.clear(); | |
558 text_bounds.SetRect(0, 0, 0, 0); | |
559 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
560 EXPECT_EQ(label.text(), paint_text); | |
561 EXPECT_EQ(border.left() + extra.width() / 2, text_bounds.x()); | |
562 EXPECT_EQ(border.top() + extra.height() / 2, text_bounds.y()); | |
563 EXPECT_EQ(center_bounds.width(), text_bounds.width()); | |
564 EXPECT_EQ(center_bounds.height(), text_bounds.height()); | |
565 expected_flags = gfx::Canvas::MULTI_LINE | | |
566 gfx::Canvas::TEXT_ALIGN_CENTER | | |
567 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
568 #if !defined(OS_WIN) | |
569 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | |
570 #endif | |
571 EXPECT_EQ(expected_flags, expected_flags); | |
572 | |
573 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
574 paint_text.clear(); | |
575 text_bounds.SetRect(0, 0, 0, 0); | |
576 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
577 EXPECT_EQ(label.text(), paint_text); | |
578 EXPECT_EQ(border.left(), text_bounds.x()); | |
579 EXPECT_EQ(border.top() + extra.height() / 2, text_bounds.y()); | |
580 EXPECT_EQ(center_bounds.width(), text_bounds.width()); | |
581 EXPECT_EQ(center_bounds.height(), text_bounds.height()); | |
582 expected_flags = gfx::Canvas::MULTI_LINE | | |
583 gfx::Canvas::TEXT_ALIGN_LEFT | | |
584 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
585 #if !defined(OS_WIN) | |
586 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | |
587 #endif | |
588 EXPECT_EQ(expected_flags, expected_flags); | |
589 | |
590 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
591 paint_text.clear(); | |
592 text_bounds.SetRect(0, 0, 0, 0); | |
593 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
594 EXPECT_EQ(label.text(), paint_text); | |
595 EXPECT_EQ(extra.width() + border.left(), text_bounds.x()); | |
596 EXPECT_EQ(border.top() + extra.height() / 2, text_bounds.y()); | |
597 EXPECT_EQ(center_bounds.width(), text_bounds.width()); | |
598 EXPECT_EQ(center_bounds.height(), text_bounds.height()); | |
599 expected_flags = gfx::Canvas::MULTI_LINE | | |
600 gfx::Canvas::TEXT_ALIGN_RIGHT | | |
601 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
602 #if !defined(OS_WIN) | |
603 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | |
604 #endif | |
605 EXPECT_EQ(expected_flags, expected_flags); | |
606 } | |
607 | |
608 TEST_F(LabelTest, DrawSingleLineStringInRTL) { | |
609 Label label; | |
610 label.SetFocusable(false); | |
611 | |
612 std::string locale = l10n_util::GetApplicationLocale(""); | |
613 base::i18n::SetICUDefaultLocale("he"); | |
614 | |
615 label.SetText(ASCIIToUTF16("Here's a string with no returns.")); | |
616 gfx::Size required_size(label.GetPreferredSize()); | |
617 gfx::Size extra(22, 8); | |
618 label.SetBounds(0, 0, required_size.width() + extra.width(), | |
619 required_size.height() + extra.height()); | |
620 | |
621 // Do some basic verifications for all three alignments. | |
622 base::string16 paint_text; | |
623 gfx::Rect text_bounds; | |
624 int flags = -1; | |
625 | |
626 // Centered text. | |
627 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
628 EXPECT_EQ(label.text(), paint_text); | |
629 // The text should be centered horizontally and vertically. | |
630 EXPECT_EQ(extra.width() / 2, text_bounds.x()); | |
631 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); | |
632 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
633 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
634 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_CENTER, | |
635 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
636 gfx::Canvas::TEXT_ALIGN_CENTER | | |
637 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
638 | |
639 // ALIGN_LEFT label. | |
640 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
641 paint_text.clear(); | |
642 text_bounds.SetRect(0, 0, 0, 0); | |
643 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
644 EXPECT_EQ(label.text(), paint_text); | |
645 // The text should be right aligned horizontally and centered vertically. | |
646 EXPECT_EQ(extra.width(), text_bounds.x()); | |
647 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); | |
648 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
649 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
650 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT, | |
651 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
652 gfx::Canvas::TEXT_ALIGN_CENTER | | |
653 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
654 | |
655 // ALIGN_RIGHT label. | |
656 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
657 paint_text.clear(); | |
658 text_bounds.SetRect(0, 0, 0, 0); | |
659 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
660 EXPECT_EQ(label.text(), paint_text); | |
661 // The text should be left aligned horizontally and centered vertically. | |
662 EXPECT_EQ(0, text_bounds.x()); | |
663 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); | |
664 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
665 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
666 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT, | |
667 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
668 gfx::Canvas::TEXT_ALIGN_CENTER | | |
669 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
670 | |
671 | |
672 // Test single line drawing with a border. | |
673 gfx::Insets border(39, 34, 8, 96); | |
674 label.SetBorder(Border::CreateEmptyBorder( | |
675 border.top(), border.left(), border.bottom(), border.right())); | |
676 | |
677 gfx::Size required_size_with_border(label.GetPreferredSize()); | |
678 EXPECT_EQ(required_size.width() + border.width(), | |
679 required_size_with_border.width()); | |
680 EXPECT_EQ(required_size.height() + border.height(), | |
681 required_size_with_border.height()); | |
682 label.SetBounds(0, 0, required_size_with_border.width() + extra.width(), | |
683 required_size_with_border.height() + extra.height()); | |
684 | |
685 // Centered text with border. | |
686 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
687 paint_text.clear(); | |
688 text_bounds.SetRect(0, 0, 0, 0); | |
689 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
690 EXPECT_EQ(label.text(), paint_text); | |
691 // The text should be centered horizontally and vertically within the border. | |
692 EXPECT_EQ(border.left() + extra.width() / 2, text_bounds.x()); | |
693 EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y()); | |
694 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
695 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
696 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_CENTER, | |
697 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
698 gfx::Canvas::TEXT_ALIGN_CENTER | | |
699 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
700 | |
701 // ALIGN_LEFT text with border. | |
702 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
703 paint_text.clear(); | |
704 text_bounds.SetRect(0, 0, 0, 0); | |
705 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
706 EXPECT_EQ(label.text(), paint_text); | |
707 // The text should be right aligned horizontally and centered vertically. | |
708 EXPECT_EQ(border.left() + extra.width(), text_bounds.x()); | |
709 EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y()); | |
710 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
711 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
712 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT, | |
713 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
714 gfx::Canvas::TEXT_ALIGN_CENTER | | |
715 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
716 | |
717 // ALIGN_RIGHT text. | |
718 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
719 paint_text.clear(); | |
720 text_bounds.SetRect(0, 0, 0, 0); | |
721 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
722 EXPECT_EQ(label.text(), paint_text); | |
723 // The text should be left aligned horizontally and centered vertically. | |
724 EXPECT_EQ(border.left(), text_bounds.x()); | |
725 EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y()); | |
726 EXPECT_EQ(required_size.width(), text_bounds.width()); | |
727 EXPECT_EQ(required_size.height(), text_bounds.height()); | |
728 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT, | |
729 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | |
730 gfx::Canvas::TEXT_ALIGN_CENTER | | |
731 gfx::Canvas::TEXT_ALIGN_RIGHT)); | |
732 | |
733 // Reset locale. | |
734 base::i18n::SetICUDefaultLocale(locale); | |
735 } | |
736 | |
737 // On Linux the underlying pango routines require a max height in order to | |
738 // ellide multiline text. So until that can be resolved, we set all | |
739 // multiline lables to not ellide in Linux only. | |
740 TEST_F(LabelTest, DrawMultiLineStringInRTL) { | |
741 Label label; | |
742 label.SetFocusable(false); | |
743 | |
744 // Test for RTL. | |
745 std::string locale = l10n_util::GetApplicationLocale(""); | |
746 base::i18n::SetICUDefaultLocale("he"); | |
747 | |
748 label.SetText(ASCIIToUTF16("Another string\nwith returns\n\n!")); | |
749 label.SetMultiLine(true); | |
750 label.SizeToFit(0); | |
751 gfx::Size extra(50, 10); | |
752 label.SetBounds(label.x(), label.y(), | |
753 label.width() + extra.width(), | |
754 label.height() + extra.height()); | |
755 | |
756 // Do some basic verifications for all three alignments. | |
757 base::string16 paint_text; | |
758 gfx::Rect text_bounds; | |
759 int flags = -1; | |
760 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
761 EXPECT_EQ(label.text(), paint_text); | |
762 EXPECT_EQ(extra.width() / 2, text_bounds.x()); | |
763 EXPECT_EQ(extra.height() / 2, text_bounds.y()); | |
764 EXPECT_GT(text_bounds.width(), kMinTextDimension); | |
765 EXPECT_GT(text_bounds.height(), kMinTextDimension); | |
766 EXPECT_TRUE(gfx::Canvas::MULTI_LINE & flags); | |
767 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_CENTER & flags); | |
768 #if !defined(OS_WIN) | |
769 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS & flags); | |
770 #endif | |
771 gfx::Rect center_bounds(text_bounds); | |
772 | |
773 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
774 paint_text.clear(); | |
775 text_bounds.SetRect(0, 0, 0, 0); | |
776 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
777 EXPECT_EQ(label.text(), paint_text); | |
778 EXPECT_EQ(extra.width(), text_bounds.x()); | |
779 EXPECT_EQ(extra.height() / 2, text_bounds.y()); | |
780 EXPECT_GT(text_bounds.width(), kMinTextDimension); | |
781 EXPECT_GT(text_bounds.height(), kMinTextDimension); | |
782 EXPECT_TRUE(gfx::Canvas::MULTI_LINE & flags); | |
783 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_RIGHT & flags); | |
784 #if !defined(OS_WIN) | |
785 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS & flags); | |
786 #endif | |
787 | |
788 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
789 paint_text.clear(); | |
790 text_bounds.SetRect(0, 0, 0, 0); | |
791 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
792 EXPECT_EQ(label.text(), paint_text); | |
793 EXPECT_EQ(0, text_bounds.x()); | |
794 EXPECT_EQ(extra.height() / 2, text_bounds.y()); | |
795 EXPECT_GT(text_bounds.width(), kMinTextDimension); | |
796 EXPECT_GT(text_bounds.height(), kMinTextDimension); | |
797 EXPECT_TRUE(gfx::Canvas::MULTI_LINE & flags); | |
798 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_LEFT & flags); | |
799 #if !defined(OS_WIN) | |
800 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS & flags); | |
801 #endif | |
802 | |
803 // Test multiline drawing with a border. | |
804 gfx::Insets border(19, 92, 23, 2); | |
805 label.SetBorder(Border::CreateEmptyBorder( | |
806 border.top(), border.left(), border.bottom(), border.right())); | |
807 label.SizeToFit(0); | |
808 label.SetBounds(label.x(), label.y(), | |
809 label.width() + extra.width(), | |
810 label.height() + extra.height()); | |
811 | |
812 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
813 paint_text.clear(); | |
814 text_bounds.SetRect(0, 0, 0, 0); | |
815 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
816 EXPECT_EQ(label.text(), paint_text); | |
817 EXPECT_EQ(border.left() + extra.width() / 2, text_bounds.x()); | |
818 EXPECT_EQ(border.top() + extra.height() / 2, text_bounds.y()); | |
819 EXPECT_EQ(center_bounds.width(), text_bounds.width()); | |
820 EXPECT_EQ(center_bounds.height(), text_bounds.height()); | |
821 EXPECT_TRUE(gfx::Canvas::MULTI_LINE & flags); | |
822 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_CENTER & flags); | |
823 #if !defined(OS_WIN) | |
824 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS & flags); | |
825 #endif | |
826 | |
827 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
828 paint_text.clear(); | |
829 text_bounds.SetRect(0, 0, 0, 0); | |
830 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
831 EXPECT_EQ(label.text(), paint_text); | |
832 EXPECT_EQ(border.left() + extra.width(), text_bounds.x()); | |
833 EXPECT_EQ(border.top() + extra.height() / 2, text_bounds.y()); | |
834 EXPECT_EQ(center_bounds.width(), text_bounds.width()); | |
835 EXPECT_EQ(center_bounds.height(), text_bounds.height()); | |
836 EXPECT_TRUE(gfx::Canvas::MULTI_LINE & flags); | |
837 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_RIGHT & flags); | |
838 #if !defined(OS_WIN) | |
839 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS & flags); | |
840 #endif | |
841 | |
842 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
843 paint_text.clear(); | |
844 text_bounds.SetRect(0, 0, 0, 0); | |
845 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | |
846 EXPECT_EQ(label.text(), paint_text); | |
847 EXPECT_EQ(border.left(), text_bounds.x()); | |
848 EXPECT_EQ(border.top() + extra.height() / 2, text_bounds.y()); | |
849 EXPECT_EQ(center_bounds.width(), text_bounds.width()); | |
850 EXPECT_EQ(center_bounds.height(), text_bounds.height()); | |
851 EXPECT_TRUE(gfx::Canvas::MULTI_LINE & flags); | |
852 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_LEFT & flags); | |
853 #if !defined(OS_WIN) | |
854 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS & flags); | |
855 #endif | |
856 | |
857 // Reset Locale | |
858 base::i18n::SetICUDefaultLocale(locale); | |
859 } | |
860 | |
861 // Ensure the subpixel rendering flag and background color alpha are respected. | |
862 TEST_F(LabelTest, DisableSubpixelRendering) { | |
863 Label label; | |
864 label.SetBackgroundColor(SK_ColorWHITE); | |
865 const int flag = gfx::Canvas::NO_SUBPIXEL_RENDERING; | |
866 EXPECT_EQ(0, label.ComputeDrawStringFlags() & flag); | |
867 label.SetSubpixelRenderingEnabled(false); | |
868 EXPECT_EQ(flag, label.ComputeDrawStringFlags() & flag); | |
869 label.SetSubpixelRenderingEnabled(true); | |
870 EXPECT_EQ(0, label.ComputeDrawStringFlags() & flag); | |
871 // Text cannot be drawn with subpixel rendering on transparent backgrounds. | |
872 label.SetBackgroundColor(SkColorSetARGB(64, 255, 255, 255)); | |
873 EXPECT_EQ(flag, label.ComputeDrawStringFlags() & flag); | |
874 } | |
875 | |
876 // Check that labels support GetTooltipHandlerForPoint. | 335 // Check that labels support GetTooltipHandlerForPoint. |
877 TEST_F(LabelTest, GetTooltipHandlerForPoint) { | 336 TEST_F(LabelTest, GetTooltipHandlerForPoint) { |
878 // A root view must be defined for this test because the hit-testing | 337 // A root view must be defined for this test because the hit-testing |
879 // behaviour used by GetTooltipHandlerForPoint() is defined by | 338 // behaviour used by GetTooltipHandlerForPoint() is defined by |
880 // the ViewTargeter installed on the root view. | 339 // the ViewTargeter installed on the root view. |
881 Widget widget; | 340 Widget widget; |
882 Widget::InitParams init_params = | 341 Widget::InitParams init_params = |
883 CreateParams(Widget::InitParams::TYPE_POPUP); | 342 CreateParams(Widget::InitParams::TYPE_POPUP); |
884 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 343 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
885 init_params.bounds = gfx::Rect(0, 0, 200, 200); | 344 init_params.bounds = gfx::Rect(0, 0, 200, 200); |
(...skipping 22 matching lines...) Expand all Loading... |
908 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 51))); | 367 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 51))); |
909 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); | 368 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); |
910 | 369 |
911 // GetTooltipHandlerForPoint works should work in child bounds. | 370 // GetTooltipHandlerForPoint works should work in child bounds. |
912 label.SetBounds(2, 2, 10, 10); | 371 label.SetBounds(2, 2, 10, 10); |
913 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(1, 5))); | 372 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(1, 5))); |
914 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(3, 11))); | 373 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(3, 11))); |
915 } | 374 } |
916 | 375 |
917 } // namespace views | 376 } // namespace views |
OLD | NEW |