| 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 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 | 350 |
| 351 // GetPreferredSize and borders. | 351 // GetPreferredSize and borders. |
| 352 label.SetBounds(0, 0, 0, 0); | 352 label.SetBounds(0, 0, 0, 0); |
| 353 gfx::Size required_size_with_border = label.GetPreferredSize(); | 353 gfx::Size required_size_with_border = label.GetPreferredSize(); |
| 354 EXPECT_EQ(required_size_with_border.height(), | 354 EXPECT_EQ(required_size_with_border.height(), |
| 355 required_size.height() + border.height()); | 355 required_size.height() + border.height()); |
| 356 EXPECT_EQ(required_size_with_border.width(), | 356 EXPECT_EQ(required_size_with_border.width(), |
| 357 required_size.width() + border.width()); | 357 required_size.width() + border.width()); |
| 358 } | 358 } |
| 359 | 359 |
| 360 TEST_F(LabelTest, DirectionalityFromText) { | |
| 361 Label label; | |
| 362 label.SetBounds(0, 0, 1000, 1000); | |
| 363 | |
| 364 // Test text starts with RTL character. | |
| 365 label.SetText(base::WideToUTF16(L" \x5d0\x5d1\x5d2 abc")); | |
| 366 const Label::DrawStringParams* params = label.CalculateDrawStringParams(); | |
| 367 EXPECT_EQ(gfx::Canvas::FORCE_RTL_DIRECTIONALITY, | |
| 368 params->flags & (gfx::Canvas::FORCE_RTL_DIRECTIONALITY | | |
| 369 gfx::Canvas::FORCE_LTR_DIRECTIONALITY)); | |
| 370 | |
| 371 // Test text starts with LTR character. | |
| 372 label.SetText(base::WideToUTF16(L"ltr \x5d0\x5d1\x5d2 abc")); | |
| 373 params = label.CalculateDrawStringParams(); | |
| 374 EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, | |
| 375 params->flags & (gfx::Canvas::FORCE_RTL_DIRECTIONALITY | | |
| 376 gfx::Canvas::FORCE_LTR_DIRECTIONALITY)); | |
| 377 } | |
| 378 | |
| 379 TEST_F(LabelTest, DrawSingleLineString) { | 360 TEST_F(LabelTest, DrawSingleLineString) { |
| 380 Label label; | 361 Label label; |
| 381 label.SetFocusable(false); | 362 label.SetFocusable(false); |
| 382 | 363 |
| 383 label.SetText(ASCIIToUTF16("Here's a string with no returns.")); | 364 label.SetText(ASCIIToUTF16("Here's a string with no returns.")); |
| 384 gfx::Size required_size(label.GetPreferredSize()); | 365 gfx::Size required_size(label.GetPreferredSize()); |
| 385 gfx::Size extra(22, 8); | 366 gfx::Size extra(22, 8); |
| 386 label.SetBounds(0, 0, required_size.width() + extra.width(), | 367 label.SetBounds(0, 0, required_size.width() + extra.width(), |
| 387 required_size.height() + extra.height()); | 368 required_size.height() + extra.height()); |
| 388 | 369 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 label.height() + extra.height()); | 486 label.height() + extra.height()); |
| 506 | 487 |
| 507 // Do some basic verifications for all three alignments. | 488 // Do some basic verifications for all three alignments. |
| 508 const Label::DrawStringParams* params = label.CalculateDrawStringParams(); | 489 const Label::DrawStringParams* params = label.CalculateDrawStringParams(); |
| 509 EXPECT_EQ(label.text(), params->text); | 490 EXPECT_EQ(label.text(), params->text); |
| 510 EXPECT_EQ(extra.width() / 2, params->bounds.x()); | 491 EXPECT_EQ(extra.width() / 2, params->bounds.x()); |
| 511 EXPECT_EQ(extra.height() / 2, params->bounds.y()); | 492 EXPECT_EQ(extra.height() / 2, params->bounds.y()); |
| 512 EXPECT_GT(params->bounds.width(), kMinTextDimension); | 493 EXPECT_GT(params->bounds.width(), kMinTextDimension); |
| 513 EXPECT_GT(params->bounds.height(), kMinTextDimension); | 494 EXPECT_GT(params->bounds.height(), kMinTextDimension); |
| 514 int expected_flags = gfx::Canvas::MULTI_LINE | | 495 int expected_flags = gfx::Canvas::MULTI_LINE | |
| 515 gfx::Canvas::TEXT_ALIGN_CENTER | | 496 gfx::Canvas::TEXT_ALIGN_CENTER; |
| 516 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
| 517 #if !defined(OS_WIN) | 497 #if !defined(OS_WIN) |
| 518 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | 498 expected_flags |= gfx::Canvas::NO_ELLIPSIS; |
| 519 #endif | 499 #endif |
| 520 EXPECT_EQ(expected_flags, expected_flags); | 500 EXPECT_EQ(expected_flags, expected_flags); |
| 521 gfx::Rect center_bounds(params->bounds); | 501 gfx::Rect center_bounds(params->bounds); |
| 522 | 502 |
| 523 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | 503 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 524 label.ResetLayoutCache(); | 504 label.ResetLayoutCache(); |
| 525 params = label.CalculateDrawStringParams(); | 505 params = label.CalculateDrawStringParams(); |
| 526 EXPECT_EQ(label.text(), params->text); | 506 EXPECT_EQ(label.text(), params->text); |
| 527 EXPECT_EQ(0, params->bounds.x()); | 507 EXPECT_EQ(0, params->bounds.x()); |
| 528 EXPECT_EQ(extra.height() / 2, params->bounds.y()); | 508 EXPECT_EQ(extra.height() / 2, params->bounds.y()); |
| 529 EXPECT_GT(params->bounds.width(), kMinTextDimension); | 509 EXPECT_GT(params->bounds.width(), kMinTextDimension); |
| 530 EXPECT_GT(params->bounds.height(), kMinTextDimension); | 510 EXPECT_GT(params->bounds.height(), kMinTextDimension); |
| 531 expected_flags = gfx::Canvas::MULTI_LINE | | 511 expected_flags = gfx::Canvas::MULTI_LINE | |
| 532 gfx::Canvas::TEXT_ALIGN_LEFT | | 512 gfx::Canvas::TEXT_ALIGN_LEFT; |
| 533 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
| 534 #if !defined(OS_WIN) | 513 #if !defined(OS_WIN) |
| 535 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | 514 expected_flags |= gfx::Canvas::NO_ELLIPSIS; |
| 536 #endif | 515 #endif |
| 537 EXPECT_EQ(expected_flags, expected_flags); | 516 EXPECT_EQ(expected_flags, expected_flags); |
| 538 | 517 |
| 539 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | 518 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); |
| 540 label.ResetLayoutCache(); | 519 label.ResetLayoutCache(); |
| 541 params = label.CalculateDrawStringParams(); | 520 params = label.CalculateDrawStringParams(); |
| 542 EXPECT_EQ(label.text(), params->text); | 521 EXPECT_EQ(label.text(), params->text); |
| 543 EXPECT_EQ(extra.width(), params->bounds.x()); | 522 EXPECT_EQ(extra.width(), params->bounds.x()); |
| 544 EXPECT_EQ(extra.height() / 2, params->bounds.y()); | 523 EXPECT_EQ(extra.height() / 2, params->bounds.y()); |
| 545 EXPECT_GT(params->bounds.width(), kMinTextDimension); | 524 EXPECT_GT(params->bounds.width(), kMinTextDimension); |
| 546 EXPECT_GT(params->bounds.height(), kMinTextDimension); | 525 EXPECT_GT(params->bounds.height(), kMinTextDimension); |
| 547 expected_flags = gfx::Canvas::MULTI_LINE | | 526 expected_flags = gfx::Canvas::MULTI_LINE | |
| 548 gfx::Canvas::TEXT_ALIGN_RIGHT | | 527 gfx::Canvas::TEXT_ALIGN_RIGHT; |
| 549 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
| 550 #if !defined(OS_WIN) | 528 #if !defined(OS_WIN) |
| 551 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | 529 expected_flags |= gfx::Canvas::NO_ELLIPSIS; |
| 552 #endif | 530 #endif |
| 553 EXPECT_EQ(expected_flags, expected_flags); | 531 EXPECT_EQ(expected_flags, expected_flags); |
| 554 | 532 |
| 555 // Test multiline drawing with a border. | 533 // Test multiline drawing with a border. |
| 556 gfx::Insets border(19, 92, 23, 2); | 534 gfx::Insets border(19, 92, 23, 2); |
| 557 label.SetBorder(Border::CreateEmptyBorder( | 535 label.SetBorder(Border::CreateEmptyBorder( |
| 558 border.top(), border.left(), border.bottom(), border.right())); | 536 border.top(), border.left(), border.bottom(), border.right())); |
| 559 label.SizeToFit(0); | 537 label.SizeToFit(0); |
| 560 label.SetBounds(label.x(), label.y(), | 538 label.SetBounds(label.x(), label.y(), |
| 561 label.width() + extra.width(), | 539 label.width() + extra.width(), |
| 562 label.height() + extra.height()); | 540 label.height() + extra.height()); |
| 563 | 541 |
| 564 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); | 542 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 565 label.ResetLayoutCache(); | 543 label.ResetLayoutCache(); |
| 566 params = label.CalculateDrawStringParams(); | 544 params = label.CalculateDrawStringParams(); |
| 567 EXPECT_EQ(label.text(), params->text); | 545 EXPECT_EQ(label.text(), params->text); |
| 568 EXPECT_EQ(border.left() + extra.width() / 2, params->bounds.x()); | 546 EXPECT_EQ(border.left() + extra.width() / 2, params->bounds.x()); |
| 569 EXPECT_EQ(border.top() + extra.height() / 2, params->bounds.y()); | 547 EXPECT_EQ(border.top() + extra.height() / 2, params->bounds.y()); |
| 570 EXPECT_EQ(center_bounds.width(), params->bounds.width()); | 548 EXPECT_EQ(center_bounds.width(), params->bounds.width()); |
| 571 EXPECT_EQ(center_bounds.height(), params->bounds.height()); | 549 EXPECT_EQ(center_bounds.height(), params->bounds.height()); |
| 572 expected_flags = gfx::Canvas::MULTI_LINE | | 550 expected_flags = gfx::Canvas::MULTI_LINE | |
| 573 gfx::Canvas::TEXT_ALIGN_CENTER | | 551 gfx::Canvas::TEXT_ALIGN_CENTER; |
| 574 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
| 575 #if !defined(OS_WIN) | 552 #if !defined(OS_WIN) |
| 576 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | 553 expected_flags |= gfx::Canvas::NO_ELLIPSIS; |
| 577 #endif | 554 #endif |
| 578 EXPECT_EQ(expected_flags, expected_flags); | 555 EXPECT_EQ(expected_flags, expected_flags); |
| 579 | 556 |
| 580 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); | 557 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 581 label.ResetLayoutCache(); | 558 label.ResetLayoutCache(); |
| 582 params = label.CalculateDrawStringParams(); | 559 params = label.CalculateDrawStringParams(); |
| 583 EXPECT_EQ(label.text(), params->text); | 560 EXPECT_EQ(label.text(), params->text); |
| 584 EXPECT_EQ(border.left(), params->bounds.x()); | 561 EXPECT_EQ(border.left(), params->bounds.x()); |
| 585 EXPECT_EQ(border.top() + extra.height() / 2, params->bounds.y()); | 562 EXPECT_EQ(border.top() + extra.height() / 2, params->bounds.y()); |
| 586 EXPECT_EQ(center_bounds.width(), params->bounds.width()); | 563 EXPECT_EQ(center_bounds.width(), params->bounds.width()); |
| 587 EXPECT_EQ(center_bounds.height(), params->bounds.height()); | 564 EXPECT_EQ(center_bounds.height(), params->bounds.height()); |
| 588 expected_flags = gfx::Canvas::MULTI_LINE | | 565 expected_flags = gfx::Canvas::MULTI_LINE | |
| 589 gfx::Canvas::TEXT_ALIGN_LEFT | | 566 gfx::Canvas::TEXT_ALIGN_LEFT; |
| 590 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
| 591 #if !defined(OS_WIN) | 567 #if !defined(OS_WIN) |
| 592 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | 568 expected_flags |= gfx::Canvas::NO_ELLIPSIS; |
| 593 #endif | 569 #endif |
| 594 EXPECT_EQ(expected_flags, expected_flags); | 570 EXPECT_EQ(expected_flags, expected_flags); |
| 595 | 571 |
| 596 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); | 572 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); |
| 597 label.ResetLayoutCache(); | 573 label.ResetLayoutCache(); |
| 598 params = label.CalculateDrawStringParams(); | 574 params = label.CalculateDrawStringParams(); |
| 599 EXPECT_EQ(label.text(), params->text); | 575 EXPECT_EQ(label.text(), params->text); |
| 600 EXPECT_EQ(extra.width() + border.left(), params->bounds.x()); | 576 EXPECT_EQ(extra.width() + border.left(), params->bounds.x()); |
| 601 EXPECT_EQ(border.top() + extra.height() / 2, params->bounds.y()); | 577 EXPECT_EQ(border.top() + extra.height() / 2, params->bounds.y()); |
| 602 EXPECT_EQ(center_bounds.width(), params->bounds.width()); | 578 EXPECT_EQ(center_bounds.width(), params->bounds.width()); |
| 603 EXPECT_EQ(center_bounds.height(), params->bounds.height()); | 579 EXPECT_EQ(center_bounds.height(), params->bounds.height()); |
| 604 expected_flags = gfx::Canvas::MULTI_LINE | | 580 expected_flags = gfx::Canvas::MULTI_LINE | |
| 605 gfx::Canvas::TEXT_ALIGN_RIGHT | | 581 gfx::Canvas::TEXT_ALIGN_RIGHT; |
| 606 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | |
| 607 #if !defined(OS_WIN) | 582 #if !defined(OS_WIN) |
| 608 expected_flags |= gfx::Canvas::NO_ELLIPSIS; | 583 expected_flags |= gfx::Canvas::NO_ELLIPSIS; |
| 609 #endif | 584 #endif |
| 610 EXPECT_EQ(expected_flags, expected_flags); | 585 EXPECT_EQ(expected_flags, expected_flags); |
| 611 } | 586 } |
| 612 | 587 |
| 613 TEST_F(LabelTest, DrawSingleLineStringInRTL) { | 588 TEST_F(LabelTest, DrawSingleLineStringInRTL) { |
| 614 Label label; | 589 Label label; |
| 615 label.SetFocusable(false); | 590 label.SetFocusable(false); |
| 616 | 591 |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); | 888 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); |
| 914 label.SetHandlesTooltips(true); | 889 label.SetHandlesTooltips(true); |
| 915 | 890 |
| 916 // GetTooltipHandlerForPoint works should work in child bounds. | 891 // GetTooltipHandlerForPoint works should work in child bounds. |
| 917 label.SetBounds(2, 2, 10, 10); | 892 label.SetBounds(2, 2, 10, 10); |
| 918 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(1, 5))); | 893 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(1, 5))); |
| 919 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(3, 11))); | 894 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(3, 11))); |
| 920 } | 895 } |
| 921 | 896 |
| 922 } // namespace views | 897 } // namespace views |
| OLD | NEW |