| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/i18n/rtl.h" | 5 #include "base/i18n/rtl.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/base/accessibility/accessible_view_state.h" | 8 #include "ui/base/accessibility/accessible_view_state.h" |
| 9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
| 10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 string16 font_name(ASCIIToUTF16("arial")); | 34 string16 font_name(ASCIIToUTF16("arial")); |
| 35 gfx::Font font(font_name, 30); | 35 gfx::Font font(font_name, 30); |
| 36 label.SetFont(font); | 36 label.SetFont(font); |
| 37 gfx::Font font_used = label.font(); | 37 gfx::Font font_used = label.font(); |
| 38 EXPECT_EQ(font_name, font_used.GetFontName()); | 38 EXPECT_EQ(font_name, font_used.GetFontName()); |
| 39 EXPECT_EQ(30, font_used.GetFontSize()); | 39 EXPECT_EQ(30, font_used.GetFontSize()); |
| 40 } | 40 } |
| 41 | 41 |
| 42 TEST(LabelTest, TextProperty) { | 42 TEST(LabelTest, TextProperty) { |
| 43 Label label; | 43 Label label; |
| 44 std::wstring test_text(L"A random string."); | 44 string16 test_text(ASCIIToUTF16("A random string.")); |
| 45 label.SetText(test_text); | 45 label.SetText(test_text); |
| 46 EXPECT_EQ(test_text, label.GetText()); | 46 EXPECT_EQ(test_text, label.GetText()); |
| 47 } | 47 } |
| 48 | 48 |
| 49 TEST(LabelTest, UrlProperty) { | 49 TEST(LabelTest, UrlProperty) { |
| 50 Label label; | 50 Label label; |
| 51 std::string my_url("http://www.orkut.com/some/Random/path"); | 51 std::string my_url("http://www.orkut.com/some/Random/path"); |
| 52 GURL url(my_url); | 52 GURL url(my_url); |
| 53 label.SetURL(url); | 53 label.SetURL(url); |
| 54 EXPECT_EQ(my_url, label.GetURL().spec()); | 54 EXPECT_EQ(my_url, label.GetURL().spec()); |
| 55 EXPECT_EQ(UTF8ToWide(my_url), label.GetText()); | 55 EXPECT_EQ(UTF8ToUTF16(my_url), label.GetText()); |
| 56 } | 56 } |
| 57 | 57 |
| 58 TEST(LabelTest, ColorProperty) { | 58 TEST(LabelTest, ColorProperty) { |
| 59 Label label; | 59 Label label; |
| 60 SkColor color = SkColorSetARGB(20, 40, 10, 5); | 60 SkColor color = SkColorSetARGB(20, 40, 10, 5); |
| 61 label.SetColor(color); | 61 label.SetColor(color); |
| 62 EXPECT_EQ(color, label.GetColor()); | 62 EXPECT_EQ(color, label.GetColor()); |
| 63 } | 63 } |
| 64 | 64 |
| 65 TEST(LabelTest, AlignmentProperty) { | 65 TEST(LabelTest, AlignmentProperty) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 EXPECT_FALSE(label.is_multi_line()); | 104 EXPECT_FALSE(label.is_multi_line()); |
| 105 label.SetMultiLine(true); | 105 label.SetMultiLine(true); |
| 106 EXPECT_TRUE(label.is_multi_line()); | 106 EXPECT_TRUE(label.is_multi_line()); |
| 107 label.SetMultiLine(false); | 107 label.SetMultiLine(false); |
| 108 EXPECT_FALSE(label.is_multi_line()); | 108 EXPECT_FALSE(label.is_multi_line()); |
| 109 } | 109 } |
| 110 | 110 |
| 111 TEST(LabelTest, TooltipProperty) { | 111 TEST(LabelTest, TooltipProperty) { |
| 112 Label label; | 112 Label label; |
| 113 string16 test_text(ASCIIToUTF16("My cool string.")); | 113 string16 test_text(ASCIIToUTF16("My cool string.")); |
| 114 label.SetText(UTF16ToWideHack(test_text)); | 114 label.SetText(test_text); |
| 115 | 115 |
| 116 string16 tooltip; | 116 string16 tooltip; |
| 117 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 117 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); |
| 118 EXPECT_EQ(test_text, tooltip); | 118 EXPECT_EQ(test_text, tooltip); |
| 119 | 119 |
| 120 string16 tooltip_text(ASCIIToUTF16("The tooltip!")); | 120 string16 tooltip_text(ASCIIToUTF16("The tooltip!")); |
| 121 label.SetTooltipText(tooltip_text); | 121 label.SetTooltipText(tooltip_text); |
| 122 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 122 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); |
| 123 EXPECT_EQ(tooltip_text, tooltip); | 123 EXPECT_EQ(tooltip_text, tooltip); |
| 124 | 124 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 151 label.SetTooltipText(tooltip_text); | 151 label.SetTooltipText(tooltip_text); |
| 152 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); | 152 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); |
| 153 EXPECT_EQ(tooltip_text, tooltip); | 153 EXPECT_EQ(tooltip_text, tooltip); |
| 154 // Clear out the tooltip. | 154 // Clear out the tooltip. |
| 155 label.SetTooltipText(empty_text); | 155 label.SetTooltipText(empty_text); |
| 156 } | 156 } |
| 157 | 157 |
| 158 TEST(LabelTest, Accessibility) { | 158 TEST(LabelTest, Accessibility) { |
| 159 Label label; | 159 Label label; |
| 160 string16 test_text(ASCIIToUTF16("My special text.")); | 160 string16 test_text(ASCIIToUTF16("My special text.")); |
| 161 label.SetText(UTF16ToWideHack(test_text)); | 161 label.SetText(test_text); |
| 162 | 162 |
| 163 ui::AccessibleViewState state; | 163 ui::AccessibleViewState state; |
| 164 label.GetAccessibleState(&state); | 164 label.GetAccessibleState(&state); |
| 165 EXPECT_EQ(ui::AccessibilityTypes::ROLE_STATICTEXT, state.role); | 165 EXPECT_EQ(ui::AccessibilityTypes::ROLE_STATICTEXT, state.role); |
| 166 EXPECT_EQ(test_text, state.name); | 166 EXPECT_EQ(test_text, state.name); |
| 167 EXPECT_TRUE(ui::AccessibilityTypes::STATE_READONLY & state.state); | 167 EXPECT_TRUE(ui::AccessibilityTypes::STATE_READONLY & state.state); |
| 168 } | 168 } |
| 169 | 169 |
| 170 TEST(LabelTest, SingleLineSizing) { | 170 TEST(LabelTest, SingleLineSizing) { |
| 171 Label label; | 171 Label label; |
| 172 std::wstring test_text(L"A not so random string in one line."); | 172 string16 test_text(ASCIIToUTF16("A not so random string in one line.")); |
| 173 label.SetText(test_text); | 173 label.SetText(test_text); |
| 174 | 174 |
| 175 // GetPreferredSize | 175 // GetPreferredSize |
| 176 gfx::Size required_size = label.GetPreferredSize(); | 176 gfx::Size required_size = label.GetPreferredSize(); |
| 177 EXPECT_GT(required_size.height(), kMinTextDimension); | 177 EXPECT_GT(required_size.height(), kMinTextDimension); |
| 178 EXPECT_GT(required_size.width(), kMinTextDimension); | 178 EXPECT_GT(required_size.width(), kMinTextDimension); |
| 179 | 179 |
| 180 // Test everything with borders. | 180 // Test everything with borders. |
| 181 gfx::Insets border(10, 20, 30, 40); | 181 gfx::Insets border(10, 20, 30, 40); |
| 182 label.set_border(Border::CreateEmptyBorder(border.top(), | 182 label.set_border(Border::CreateEmptyBorder(border.top(), |
| 183 border.left(), | 183 border.left(), |
| 184 border.bottom(), | 184 border.bottom(), |
| 185 border.right())); | 185 border.right())); |
| 186 | 186 |
| 187 // GetPreferredSize and borders. | 187 // GetPreferredSize and borders. |
| 188 label.SetBounds(0, 0, 0, 0); | 188 label.SetBounds(0, 0, 0, 0); |
| 189 gfx::Size required_size_with_border = label.GetPreferredSize(); | 189 gfx::Size required_size_with_border = label.GetPreferredSize(); |
| 190 EXPECT_EQ(required_size_with_border.height(), | 190 EXPECT_EQ(required_size_with_border.height(), |
| 191 required_size.height() + border.height()); | 191 required_size.height() + border.height()); |
| 192 EXPECT_EQ(required_size_with_border.width(), | 192 EXPECT_EQ(required_size_with_border.width(), |
| 193 required_size.width() + border.width()); | 193 required_size.width() + border.width()); |
| 194 } | 194 } |
| 195 | 195 |
| 196 TEST(LabelTest, MultiLineSizing) { | 196 TEST(LabelTest, MultiLineSizing) { |
| 197 Label label; | 197 Label label; |
| 198 label.set_focusable(false); | 198 label.set_focusable(false); |
| 199 std::wstring test_text(L"A random string\nwith multiple lines\nand returns!"); | 199 string16 test_text( |
| 200 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!")); |
| 200 label.SetText(test_text); | 201 label.SetText(test_text); |
| 201 label.SetMultiLine(true); | 202 label.SetMultiLine(true); |
| 202 | 203 |
| 203 // GetPreferredSize | 204 // GetPreferredSize |
| 204 gfx::Size required_size = label.GetPreferredSize(); | 205 gfx::Size required_size = label.GetPreferredSize(); |
| 205 EXPECT_GT(required_size.height(), kMinTextDimension); | 206 EXPECT_GT(required_size.height(), kMinTextDimension); |
| 206 EXPECT_GT(required_size.width(), kMinTextDimension); | 207 EXPECT_GT(required_size.width(), kMinTextDimension); |
| 207 | 208 |
| 208 // SizeToFit with unlimited width. | 209 // SizeToFit with unlimited width. |
| 209 label.SizeToFit(0); | 210 label.SizeToFit(0); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 } | 278 } |
| 278 | 279 |
| 279 TEST(LabelTest, DrawSingleLineString) { | 280 TEST(LabelTest, DrawSingleLineString) { |
| 280 Label label; | 281 Label label; |
| 281 label.set_focusable(false); | 282 label.set_focusable(false); |
| 282 | 283 |
| 283 // Turn off mirroring so that we don't need to figure out if | 284 // Turn off mirroring so that we don't need to figure out if |
| 284 // align right really means align left. | 285 // align right really means align left. |
| 285 label.set_rtl_alignment_mode(Label::AUTO_DETECT_ALIGNMENT); | 286 label.set_rtl_alignment_mode(Label::AUTO_DETECT_ALIGNMENT); |
| 286 | 287 |
| 287 std::wstring test_text(L"Here's a string with no returns."); | 288 string16 test_text(ASCIIToUTF16("Here's a string with no returns.")); |
| 288 label.SetText(test_text); | 289 label.SetText(test_text); |
| 289 gfx::Size required_size(label.GetPreferredSize()); | 290 gfx::Size required_size(label.GetPreferredSize()); |
| 290 gfx::Size extra(22, 8); | 291 gfx::Size extra(22, 8); |
| 291 label.SetBounds(0, | 292 label.SetBounds(0, |
| 292 0, | 293 0, |
| 293 required_size.width() + extra.width(), | 294 required_size.width() + extra.width(), |
| 294 required_size.height() + extra.height()); | 295 required_size.height() + extra.height()); |
| 295 | 296 |
| 296 // Do some basic verifications for all three alignments. | 297 // Do some basic verifications for all three alignments. |
| 297 std::wstring paint_text; | 298 string16 paint_text; |
| 298 gfx::Rect text_bounds; | 299 gfx::Rect text_bounds; |
| 299 int flags; | 300 int flags; |
| 300 | 301 |
| 301 // Centered text. | 302 // Centered text. |
| 302 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 303 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 303 EXPECT_EQ(test_text, paint_text); | 304 EXPECT_EQ(test_text, paint_text); |
| 304 // The text should be centered horizontally and vertically. | 305 // The text should be centered horizontally and vertically. |
| 305 EXPECT_EQ(extra.width() / 2, text_bounds.x()); | 306 EXPECT_EQ(extra.width() / 2, text_bounds.x()); |
| 306 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); | 307 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); |
| 307 EXPECT_EQ(required_size.width(), text_bounds.width()); | 308 EXPECT_EQ(required_size.width(), text_bounds.width()); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 // ellide multiline text. So until that can be resolved, we set all | 396 // ellide multiline text. So until that can be resolved, we set all |
| 396 // multiline lables to not ellide in Linux only. | 397 // multiline lables to not ellide in Linux only. |
| 397 TEST(LabelTest, DrawMultiLineString) { | 398 TEST(LabelTest, DrawMultiLineString) { |
| 398 Label label; | 399 Label label; |
| 399 label.set_focusable(false); | 400 label.set_focusable(false); |
| 400 | 401 |
| 401 // Turn off mirroring so that we don't need to figure out if | 402 // Turn off mirroring so that we don't need to figure out if |
| 402 // align right really means align left. | 403 // align right really means align left. |
| 403 label.set_rtl_alignment_mode(Label::AUTO_DETECT_ALIGNMENT); | 404 label.set_rtl_alignment_mode(Label::AUTO_DETECT_ALIGNMENT); |
| 404 | 405 |
| 405 std::wstring test_text(L"Another string\nwith returns\n\n!"); | 406 string16 test_text(ASCIIToUTF16("Another string\nwith returns\n\n!")); |
| 406 label.SetText(test_text); | 407 label.SetText(test_text); |
| 407 label.SetMultiLine(true); | 408 label.SetMultiLine(true); |
| 408 label.SizeToFit(0); | 409 label.SizeToFit(0); |
| 409 gfx::Size extra(50, 10); | 410 gfx::Size extra(50, 10); |
| 410 label.SetBounds(label.x(), | 411 label.SetBounds(label.x(), |
| 411 label.y(), | 412 label.y(), |
| 412 label.width() + extra.width(), | 413 label.width() + extra.width(), |
| 413 label.height() + extra.height()); | 414 label.height() + extra.height()); |
| 414 | 415 |
| 415 // Do some basic verifications for all three alignments. | 416 // Do some basic verifications for all three alignments. |
| 416 std::wstring paint_text; | 417 string16 paint_text; |
| 417 gfx::Rect text_bounds; | 418 gfx::Rect text_bounds; |
| 418 int flags; | 419 int flags; |
| 419 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 420 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 420 EXPECT_EQ(test_text, paint_text); | 421 EXPECT_EQ(test_text, paint_text); |
| 421 EXPECT_EQ(extra.width() / 2, text_bounds.x()); | 422 EXPECT_EQ(extra.width() / 2, text_bounds.x()); |
| 422 EXPECT_EQ(extra.height() / 2, text_bounds.y()); | 423 EXPECT_EQ(extra.height() / 2, text_bounds.y()); |
| 423 EXPECT_GT(text_bounds.width(), kMinTextDimension); | 424 EXPECT_GT(text_bounds.width(), kMinTextDimension); |
| 424 EXPECT_GT(text_bounds.height(), kMinTextDimension); | 425 EXPECT_GT(text_bounds.height(), kMinTextDimension); |
| 425 int expected_flags = gfx::Canvas::MULTI_LINE | | 426 int expected_flags = gfx::Canvas::MULTI_LINE | |
| 426 gfx::Canvas::TEXT_ALIGN_CENTER | | 427 gfx::Canvas::TEXT_ALIGN_CENTER | |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 #endif | 536 #endif |
| 536 } | 537 } |
| 537 | 538 |
| 538 TEST(LabelTest, DrawSingleLineStringInRTL) { | 539 TEST(LabelTest, DrawSingleLineStringInRTL) { |
| 539 Label label; | 540 Label label; |
| 540 label.set_focusable(false); | 541 label.set_focusable(false); |
| 541 | 542 |
| 542 std::string locale = l10n_util::GetApplicationLocale(""); | 543 std::string locale = l10n_util::GetApplicationLocale(""); |
| 543 base::i18n::SetICUDefaultLocale("he"); | 544 base::i18n::SetICUDefaultLocale("he"); |
| 544 | 545 |
| 545 std::wstring test_text(L"Here's a string with no returns."); | 546 string16 test_text(ASCIIToUTF16("Here's a string with no returns.")); |
| 546 label.SetText(test_text); | 547 label.SetText(test_text); |
| 547 gfx::Size required_size(label.GetPreferredSize()); | 548 gfx::Size required_size(label.GetPreferredSize()); |
| 548 gfx::Size extra(22, 8); | 549 gfx::Size extra(22, 8); |
| 549 label.SetBounds(0, | 550 label.SetBounds(0, |
| 550 0, | 551 0, |
| 551 required_size.width() + extra.width(), | 552 required_size.width() + extra.width(), |
| 552 required_size.height() + extra.height()); | 553 required_size.height() + extra.height()); |
| 553 | 554 |
| 554 // Do some basic verifications for all three alignments. | 555 // Do some basic verifications for all three alignments. |
| 555 std::wstring paint_text; | 556 string16 paint_text; |
| 556 gfx::Rect text_bounds; | 557 gfx::Rect text_bounds; |
| 557 int flags; | 558 int flags; |
| 558 | 559 |
| 559 // Centered text. | 560 // Centered text. |
| 560 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 561 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 561 EXPECT_EQ(test_text, paint_text); | 562 EXPECT_EQ(test_text, paint_text); |
| 562 // The text should be centered horizontally and vertically. | 563 // The text should be centered horizontally and vertically. |
| 563 EXPECT_EQ(extra.width() / 2, text_bounds.x()); | 564 EXPECT_EQ(extra.width() / 2, text_bounds.x()); |
| 564 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); | 565 EXPECT_EQ(extra.height() / 2 , text_bounds.y()); |
| 565 EXPECT_EQ(required_size.width(), text_bounds.width()); | 566 EXPECT_EQ(required_size.width(), text_bounds.width()); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 // ellide multiline text. So until that can be resolved, we set all | 658 // ellide multiline text. So until that can be resolved, we set all |
| 658 // multiline lables to not ellide in Linux only. | 659 // multiline lables to not ellide in Linux only. |
| 659 TEST(LabelTest, DrawMultiLineStringInRTL) { | 660 TEST(LabelTest, DrawMultiLineStringInRTL) { |
| 660 Label label; | 661 Label label; |
| 661 label.set_focusable(false); | 662 label.set_focusable(false); |
| 662 | 663 |
| 663 // Test for RTL. | 664 // Test for RTL. |
| 664 std::string locale = l10n_util::GetApplicationLocale(""); | 665 std::string locale = l10n_util::GetApplicationLocale(""); |
| 665 base::i18n::SetICUDefaultLocale("he"); | 666 base::i18n::SetICUDefaultLocale("he"); |
| 666 | 667 |
| 667 std::wstring test_text(L"Another string\nwith returns\n\n!"); | 668 string16 test_text(ASCIIToUTF16("Another string\nwith returns\n\n!")); |
| 668 label.SetText(test_text); | 669 label.SetText(test_text); |
| 669 label.SetMultiLine(true); | 670 label.SetMultiLine(true); |
| 670 label.SizeToFit(0); | 671 label.SizeToFit(0); |
| 671 gfx::Size extra(50, 10); | 672 gfx::Size extra(50, 10); |
| 672 label.SetBounds(label.x(), | 673 label.SetBounds(label.x(), |
| 673 label.y(), | 674 label.y(), |
| 674 label.width() + extra.width(), | 675 label.width() + extra.width(), |
| 675 label.height() + extra.height()); | 676 label.height() + extra.height()); |
| 676 | 677 |
| 677 // Do some basic verifications for all three alignments. | 678 // Do some basic verifications for all three alignments. |
| 678 std::wstring paint_text; | 679 string16 paint_text; |
| 679 gfx::Rect text_bounds; | 680 gfx::Rect text_bounds; |
| 680 int flags; | 681 int flags; |
| 681 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 682 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 682 EXPECT_EQ(test_text, paint_text); | 683 EXPECT_EQ(test_text, paint_text); |
| 683 EXPECT_EQ(extra.width() / 2, text_bounds.x()); | 684 EXPECT_EQ(extra.width() / 2, text_bounds.x()); |
| 684 EXPECT_EQ(extra.height() / 2, text_bounds.y()); | 685 EXPECT_EQ(extra.height() / 2, text_bounds.y()); |
| 685 EXPECT_GT(text_bounds.width(), kMinTextDimension); | 686 EXPECT_GT(text_bounds.width(), kMinTextDimension); |
| 686 EXPECT_GT(text_bounds.height(), kMinTextDimension); | 687 EXPECT_GT(text_bounds.height(), kMinTextDimension); |
| 687 #if defined(OS_WIN) | 688 #if defined(OS_WIN) |
| 688 EXPECT_EQ(gfx::Canvas::MULTI_LINE | gfx::Canvas::TEXT_ALIGN_CENTER, flags); | 689 EXPECT_EQ(gfx::Canvas::MULTI_LINE | gfx::Canvas::TEXT_ALIGN_CENTER, flags); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 gfx::Canvas::TEXT_ALIGN_LEFT | | 801 gfx::Canvas::TEXT_ALIGN_LEFT | |
| 801 gfx::Canvas::NO_ELLIPSIS, | 802 gfx::Canvas::NO_ELLIPSIS, |
| 802 flags); | 803 flags); |
| 803 #endif | 804 #endif |
| 804 | 805 |
| 805 // Reset Locale | 806 // Reset Locale |
| 806 base::i18n::SetICUDefaultLocale(locale); | 807 base::i18n::SetICUDefaultLocale(locale); |
| 807 } | 808 } |
| 808 | 809 |
| 809 } // namespace views | 810 } // namespace views |
| OLD | NEW |