| 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/base/accessibility/accessible_view_state.h" | 10 #include "ui/base/accessibility/accessible_view_state.h" |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 base::string16 test_text(ASCIIToUTF16("A not so random string in one line.")); | 162 base::string16 test_text(ASCIIToUTF16("A not so random string in one line.")); |
| 163 label.SetText(test_text); | 163 label.SetText(test_text); |
| 164 | 164 |
| 165 // GetPreferredSize | 165 // GetPreferredSize |
| 166 gfx::Size required_size = label.GetPreferredSize(); | 166 gfx::Size required_size = label.GetPreferredSize(); |
| 167 EXPECT_GT(required_size.height(), kMinTextDimension); | 167 EXPECT_GT(required_size.height(), kMinTextDimension); |
| 168 EXPECT_GT(required_size.width(), kMinTextDimension); | 168 EXPECT_GT(required_size.width(), kMinTextDimension); |
| 169 | 169 |
| 170 // Test everything with borders. | 170 // Test everything with borders. |
| 171 gfx::Insets border(10, 20, 30, 40); | 171 gfx::Insets border(10, 20, 30, 40); |
| 172 label.set_border(Border::CreateEmptyBorder(border.top(), | 172 label.SetBorder(Border::CreateEmptyBorder( |
| 173 border.left(), | 173 border.top(), border.left(), border.bottom(), border.right())); |
| 174 border.bottom(), | |
| 175 border.right())); | |
| 176 | 174 |
| 177 // GetPreferredSize and borders. | 175 // GetPreferredSize and borders. |
| 178 label.SetBounds(0, 0, 0, 0); | 176 label.SetBounds(0, 0, 0, 0); |
| 179 gfx::Size required_size_with_border = label.GetPreferredSize(); | 177 gfx::Size required_size_with_border = label.GetPreferredSize(); |
| 180 EXPECT_EQ(required_size_with_border.height(), | 178 EXPECT_EQ(required_size_with_border.height(), |
| 181 required_size.height() + border.height()); | 179 required_size.height() + border.height()); |
| 182 EXPECT_EQ(required_size_with_border.width(), | 180 EXPECT_EQ(required_size_with_border.width(), |
| 183 required_size.width() + border.width()); | 181 required_size.width() + border.width()); |
| 184 } | 182 } |
| 185 | 183 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 EXPECT_GT(height_for_constrained_width, required_height); | 241 EXPECT_GT(height_for_constrained_width, required_height); |
| 244 #endif | 242 #endif |
| 245 // Using the constrained width or the required_width - 1 should give the | 243 // Using the constrained width or the required_width - 1 should give the |
| 246 // same result for the height because the constrainted width is the tight | 244 // same result for the height because the constrainted width is the tight |
| 247 // width when given "required_width - 1" as the max width. | 245 // width when given "required_width - 1" as the max width. |
| 248 EXPECT_EQ(height_for_constrained_width, | 246 EXPECT_EQ(height_for_constrained_width, |
| 249 label.GetHeightForWidth(required_width - 1)); | 247 label.GetHeightForWidth(required_width - 1)); |
| 250 | 248 |
| 251 // Test everything with borders. | 249 // Test everything with borders. |
| 252 gfx::Insets border(10, 20, 30, 40); | 250 gfx::Insets border(10, 20, 30, 40); |
| 253 label.set_border(Border::CreateEmptyBorder(border.top(), | 251 label.SetBorder(Border::CreateEmptyBorder( |
| 254 border.left(), | 252 border.top(), border.left(), border.bottom(), border.right())); |
| 255 border.bottom(), | |
| 256 border.right())); | |
| 257 | 253 |
| 258 // SizeToFit and borders. | 254 // SizeToFit and borders. |
| 259 label.SizeToFit(0); | 255 label.SizeToFit(0); |
| 260 int required_width_with_border = label.GetLocalBounds().width(); | 256 int required_width_with_border = label.GetLocalBounds().width(); |
| 261 EXPECT_EQ(required_width_with_border, required_width + border.width()); | 257 EXPECT_EQ(required_width_with_border, required_width + border.width()); |
| 262 | 258 |
| 263 // GetHeightForWidth and borders. | 259 // GetHeightForWidth and borders. |
| 264 int required_height_with_border = | 260 int required_height_with_border = |
| 265 label.GetHeightForWidth(required_width_with_border); | 261 label.GetHeightForWidth(required_width_with_border); |
| 266 EXPECT_EQ(required_height_with_border, required_height + border.height()); | 262 EXPECT_EQ(required_height_with_border, required_height + border.height()); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); | 380 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); |
| 385 EXPECT_EQ(required_size.width(), text_bounds.width()); | 381 EXPECT_EQ(required_size.width(), text_bounds.width()); |
| 386 EXPECT_EQ(required_size.height(), text_bounds.height()); | 382 EXPECT_EQ(required_size.height(), text_bounds.height()); |
| 387 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT, | 383 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT, |
| 388 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | 384 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | |
| 389 gfx::Canvas::TEXT_ALIGN_CENTER | | 385 gfx::Canvas::TEXT_ALIGN_CENTER | |
| 390 gfx::Canvas::TEXT_ALIGN_RIGHT)); | 386 gfx::Canvas::TEXT_ALIGN_RIGHT)); |
| 391 | 387 |
| 392 // Test single line drawing with a border. | 388 // Test single line drawing with a border. |
| 393 gfx::Insets border(39, 34, 8, 96); | 389 gfx::Insets border(39, 34, 8, 96); |
| 394 label.set_border(Border::CreateEmptyBorder(border.top(), | 390 label.SetBorder(Border::CreateEmptyBorder( |
| 395 border.left(), | 391 border.top(), border.left(), border.bottom(), border.right())); |
| 396 border.bottom(), | |
| 397 border.right())); | |
| 398 | 392 |
| 399 gfx::Size required_size_with_border(label.GetPreferredSize()); | 393 gfx::Size required_size_with_border(label.GetPreferredSize()); |
| 400 EXPECT_EQ(required_size.width() + border.width(), | 394 EXPECT_EQ(required_size.width() + border.width(), |
| 401 required_size_with_border.width()); | 395 required_size_with_border.width()); |
| 402 EXPECT_EQ(required_size.height() + border.height(), | 396 EXPECT_EQ(required_size.height() + border.height(), |
| 403 required_size_with_border.height()); | 397 required_size_with_border.height()); |
| 404 label.SetBounds(0, | 398 label.SetBounds(0, |
| 405 0, | 399 0, |
| 406 required_size_with_border.width() + extra.width(), | 400 required_size_with_border.width() + extra.width(), |
| 407 required_size_with_border.height() + extra.height()); | 401 required_size_with_border.height() + extra.height()); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 gfx::Canvas::TEXT_ALIGN_RIGHT | | 521 gfx::Canvas::TEXT_ALIGN_RIGHT | |
| 528 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; | 522 gfx::Canvas::FORCE_LTR_DIRECTIONALITY; |
| 529 #if defined(OS_WIN) | 523 #if defined(OS_WIN) |
| 530 EXPECT_EQ(expected_flags, flags); | 524 EXPECT_EQ(expected_flags, flags); |
| 531 #else | 525 #else |
| 532 EXPECT_EQ(expected_flags | gfx::Canvas::NO_ELLIPSIS, flags); | 526 EXPECT_EQ(expected_flags | gfx::Canvas::NO_ELLIPSIS, flags); |
| 533 #endif | 527 #endif |
| 534 | 528 |
| 535 // Test multiline drawing with a border. | 529 // Test multiline drawing with a border. |
| 536 gfx::Insets border(19, 92, 23, 2); | 530 gfx::Insets border(19, 92, 23, 2); |
| 537 label.set_border(Border::CreateEmptyBorder(border.top(), | 531 label.SetBorder(Border::CreateEmptyBorder( |
| 538 border.left(), | 532 border.top(), border.left(), border.bottom(), border.right())); |
| 539 border.bottom(), | |
| 540 border.right())); | |
| 541 label.SizeToFit(0); | 533 label.SizeToFit(0); |
| 542 label.SetBounds(label.x(), | 534 label.SetBounds(label.x(), |
| 543 label.y(), | 535 label.y(), |
| 544 label.width() + extra.width(), | 536 label.width() + extra.width(), |
| 545 label.height() + extra.height()); | 537 label.height() + extra.height()); |
| 546 | 538 |
| 547 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); | 539 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 548 paint_text.clear(); | 540 paint_text.clear(); |
| 549 text_bounds.SetRect(0, 0, 0, 0); | 541 text_bounds.SetRect(0, 0, 0, 0); |
| 550 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 542 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 EXPECT_EQ(required_size.width(), text_bounds.width()); | 653 EXPECT_EQ(required_size.width(), text_bounds.width()); |
| 662 EXPECT_EQ(required_size.height(), text_bounds.height()); | 654 EXPECT_EQ(required_size.height(), text_bounds.height()); |
| 663 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT, | 655 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT, |
| 664 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | | 656 flags & (gfx::Canvas::TEXT_ALIGN_LEFT | |
| 665 gfx::Canvas::TEXT_ALIGN_CENTER | | 657 gfx::Canvas::TEXT_ALIGN_CENTER | |
| 666 gfx::Canvas::TEXT_ALIGN_RIGHT)); | 658 gfx::Canvas::TEXT_ALIGN_RIGHT)); |
| 667 | 659 |
| 668 | 660 |
| 669 // Test single line drawing with a border. | 661 // Test single line drawing with a border. |
| 670 gfx::Insets border(39, 34, 8, 96); | 662 gfx::Insets border(39, 34, 8, 96); |
| 671 label.set_border(Border::CreateEmptyBorder(border.top(), | 663 label.SetBorder(Border::CreateEmptyBorder( |
| 672 border.left(), | 664 border.top(), border.left(), border.bottom(), border.right())); |
| 673 border.bottom(), | |
| 674 border.right())); | |
| 675 | 665 |
| 676 gfx::Size required_size_with_border(label.GetPreferredSize()); | 666 gfx::Size required_size_with_border(label.GetPreferredSize()); |
| 677 EXPECT_EQ(required_size.width() + border.width(), | 667 EXPECT_EQ(required_size.width() + border.width(), |
| 678 required_size_with_border.width()); | 668 required_size_with_border.width()); |
| 679 EXPECT_EQ(required_size.height() + border.height(), | 669 EXPECT_EQ(required_size.height() + border.height(), |
| 680 required_size_with_border.height()); | 670 required_size_with_border.height()); |
| 681 label.SetBounds(0, | 671 label.SetBounds(0, |
| 682 0, | 672 0, |
| 683 required_size_with_border.width() + extra.width(), | 673 required_size_with_border.width() + extra.width(), |
| 684 required_size_with_border.height() + extra.height()); | 674 required_size_with_border.height() + extra.height()); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 #else | 800 #else |
| 811 EXPECT_EQ( | 801 EXPECT_EQ( |
| 812 gfx::Canvas::MULTI_LINE | | 802 gfx::Canvas::MULTI_LINE | |
| 813 gfx::Canvas::TEXT_ALIGN_LEFT | | 803 gfx::Canvas::TEXT_ALIGN_LEFT | |
| 814 gfx::Canvas::NO_ELLIPSIS, | 804 gfx::Canvas::NO_ELLIPSIS, |
| 815 flags); | 805 flags); |
| 816 #endif | 806 #endif |
| 817 | 807 |
| 818 // Test multiline drawing with a border. | 808 // Test multiline drawing with a border. |
| 819 gfx::Insets border(19, 92, 23, 2); | 809 gfx::Insets border(19, 92, 23, 2); |
| 820 label.set_border(Border::CreateEmptyBorder(border.top(), | 810 label.SetBorder(Border::CreateEmptyBorder( |
| 821 border.left(), | 811 border.top(), border.left(), border.bottom(), border.right())); |
| 822 border.bottom(), | |
| 823 border.right())); | |
| 824 label.SizeToFit(0); | 812 label.SizeToFit(0); |
| 825 label.SetBounds(label.x(), | 813 label.SetBounds(label.x(), |
| 826 label.y(), | 814 label.y(), |
| 827 label.width() + extra.width(), | 815 label.width() + extra.width(), |
| 828 label.height() + extra.height()); | 816 label.height() + extra.height()); |
| 829 | 817 |
| 830 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); | 818 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 831 paint_text.clear(); | 819 paint_text.clear(); |
| 832 text_bounds.SetRect(0, 0, 0, 0); | 820 text_bounds.SetRect(0, 0, 0, 0); |
| 833 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 821 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 51))); | 913 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 51))); |
| 926 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); | 914 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); |
| 927 | 915 |
| 928 // GetTooltipHandlerForPoint works should work in child bounds. | 916 // GetTooltipHandlerForPoint works should work in child bounds. |
| 929 label.SetBounds(2, 2, 10, 10); | 917 label.SetBounds(2, 2, 10, 10); |
| 930 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(1, 5))); | 918 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(1, 5))); |
| 931 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(3, 11))); | 919 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(3, 11))); |
| 932 } | 920 } |
| 933 | 921 |
| 934 } // namespace views | 922 } // namespace views |
| OLD | NEW |