| 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 "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" |
| 11 #include "ui/gfx/render_text.h" |
| 11 #include "ui/views/border.h" | 12 #include "ui/views/border.h" |
| 12 #include "ui/views/controls/label.h" | 13 #include "ui/views/controls/label.h" |
| 13 | 14 |
| 14 namespace views { | 15 namespace views { |
| 15 | 16 |
| 16 // All text sizing measurements (width and height) should be greater than this. | 17 // All text sizing measurements (width and height) should be greater than this. |
| 17 const int kMinTextDimension = 4; | 18 const int kMinTextDimension = 4; |
| 18 | 19 |
| 19 TEST(LabelTest, FontPropertyCourier) { | 20 TEST(LabelTest, FontPropertyCourier) { |
| 20 Label label; | 21 Label label; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 reverse_alignment ? Label::ALIGN_LEFT : Label::ALIGN_RIGHT, | 73 reverse_alignment ? Label::ALIGN_LEFT : Label::ALIGN_RIGHT, |
| 73 label.horizontal_alignment()); | 74 label.horizontal_alignment()); |
| 74 label.SetHorizontalAlignment(Label::ALIGN_LEFT); | 75 label.SetHorizontalAlignment(Label::ALIGN_LEFT); |
| 75 EXPECT_EQ( | 76 EXPECT_EQ( |
| 76 reverse_alignment ? Label::ALIGN_RIGHT : Label::ALIGN_LEFT, | 77 reverse_alignment ? Label::ALIGN_RIGHT : Label::ALIGN_LEFT, |
| 77 label.horizontal_alignment()); | 78 label.horizontal_alignment()); |
| 78 label.SetHorizontalAlignment(Label::ALIGN_CENTER); | 79 label.SetHorizontalAlignment(Label::ALIGN_CENTER); |
| 79 EXPECT_EQ(Label::ALIGN_CENTER, label.horizontal_alignment()); | 80 EXPECT_EQ(Label::ALIGN_CENTER, label.horizontal_alignment()); |
| 80 | 81 |
| 81 // The label's alignment should not be flipped if the directionality mode is | 82 // The label's alignment should not be flipped if the directionality mode is |
| 82 // AUTO_DETECT_DIRECTIONALITY. | 83 // gfx::DERIVE_FROM_TEXT. |
| 83 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 84 label.set_directionality_mode(gfx::DERIVE_FROM_TEXT); |
| 84 label.SetHorizontalAlignment(Label::ALIGN_RIGHT); | 85 label.SetHorizontalAlignment(Label::ALIGN_RIGHT); |
| 85 EXPECT_EQ(Label::ALIGN_RIGHT, label.horizontal_alignment()); | 86 EXPECT_EQ(Label::ALIGN_RIGHT, label.horizontal_alignment()); |
| 86 label.SetHorizontalAlignment(Label::ALIGN_LEFT); | 87 label.SetHorizontalAlignment(Label::ALIGN_LEFT); |
| 87 EXPECT_EQ(Label::ALIGN_LEFT, label.horizontal_alignment()); | 88 EXPECT_EQ(Label::ALIGN_LEFT, label.horizontal_alignment()); |
| 88 label.SetHorizontalAlignment(Label::ALIGN_CENTER); | 89 label.SetHorizontalAlignment(Label::ALIGN_CENTER); |
| 89 EXPECT_EQ(Label::ALIGN_CENTER, label.horizontal_alignment()); | 90 EXPECT_EQ(Label::ALIGN_CENTER, label.horizontal_alignment()); |
| 90 } | 91 } |
| 91 | 92 |
| 92 TEST(LabelTest, DirectionalityModeProperty) { | 93 TEST(LabelTest, DirectionalityModeProperty) { |
| 93 Label label; | 94 Label label; |
| 94 EXPECT_EQ(Label::USE_UI_DIRECTIONALITY, label.directionality_mode()); | 95 EXPECT_EQ(gfx::DERIVE_FROM_UI, label.directionality_mode()); |
| 95 | 96 |
| 96 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 97 label.set_directionality_mode(gfx::DERIVE_FROM_TEXT); |
| 97 EXPECT_EQ(Label::AUTO_DETECT_DIRECTIONALITY, label.directionality_mode()); | 98 EXPECT_EQ(gfx::DERIVE_FROM_TEXT, label.directionality_mode()); |
| 98 | 99 |
| 99 label.set_directionality_mode(Label::USE_UI_DIRECTIONALITY); | 100 label.set_directionality_mode(gfx::DERIVE_FROM_UI); |
| 100 EXPECT_EQ(Label::USE_UI_DIRECTIONALITY, label.directionality_mode()); | 101 EXPECT_EQ(gfx::DERIVE_FROM_UI, label.directionality_mode()); |
| 101 } | 102 } |
| 102 | 103 |
| 103 TEST(LabelTest, MultiLineProperty) { | 104 TEST(LabelTest, MultiLineProperty) { |
| 104 Label label; | 105 Label label; |
| 105 EXPECT_FALSE(label.is_multi_line()); | 106 EXPECT_FALSE(label.is_multi_line()); |
| 106 label.SetMultiLine(true); | 107 label.SetMultiLine(true); |
| 107 EXPECT_TRUE(label.is_multi_line()); | 108 EXPECT_TRUE(label.is_multi_line()); |
| 108 label.SetMultiLine(false); | 109 label.SetMultiLine(false); |
| 109 EXPECT_FALSE(label.is_multi_line()); | 110 EXPECT_FALSE(label.is_multi_line()); |
| 110 } | 111 } |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 label.SetBounds(0, 0, 0, 0); | 274 label.SetBounds(0, 0, 0, 0); |
| 274 gfx::Size required_size_with_border = label.GetPreferredSize(); | 275 gfx::Size required_size_with_border = label.GetPreferredSize(); |
| 275 EXPECT_EQ(required_size_with_border.height(), | 276 EXPECT_EQ(required_size_with_border.height(), |
| 276 required_size.height() + border.height()); | 277 required_size.height() + border.height()); |
| 277 EXPECT_EQ(required_size_with_border.width(), | 278 EXPECT_EQ(required_size_with_border.width(), |
| 278 required_size.width() + border.width()); | 279 required_size.width() + border.width()); |
| 279 } | 280 } |
| 280 | 281 |
| 281 TEST(LabelTest, AutoDetectDirectionality) { | 282 TEST(LabelTest, AutoDetectDirectionality) { |
| 282 Label label; | 283 Label label; |
| 283 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 284 label.set_directionality_mode(gfx::DERIVE_FROM_TEXT); |
| 284 | |
| 285 // Test text starts with RTL character. | |
| 286 string16 test_text(WideToUTF16(L" \x5d0\x5d1\x5d2 abc")); | |
| 287 label.SetText(test_text); | |
| 288 gfx::Size required_size(label.GetPreferredSize()); | |
| 289 gfx::Size extra(22, 8); | |
| 290 label.SetBounds(0, | |
| 291 0, | |
| 292 required_size.width() + extra.width(), | |
| 293 required_size.height() + extra.height()); | |
| 294 | 285 |
| 295 string16 paint_text; | 286 string16 paint_text; |
| 296 gfx::Rect text_bounds; | 287 gfx::Rect text_bounds; |
| 297 int flags; | 288 int flags = 0; |
| 289 |
| 290 // Test text starting with an RTL character. |
| 291 label.SetText(WideToUTF16(L" \x5d0\x5d1\x5d2 abc ")); |
| 298 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 292 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 299 EXPECT_EQ(gfx::Canvas::FORCE_RTL_DIRECTIONALITY, flags); | 293 EXPECT_EQ(gfx::Canvas::FORCE_RTL_DIRECTIONALITY, flags); |
| 300 | 294 |
| 301 // Test text starts with LTR character. | 295 // Test text starting with an LTR character. |
| 302 test_text = (WideToUTF16(L"ltr \x5d0\x5d1\x5d2 abc")); | 296 label.SetText(WideToUTF16(L"ltr \x5d0\x5d1\x5d2 abc ")); |
| 303 label.SetText(test_text); | |
| 304 required_size = label.GetPreferredSize(); | |
| 305 label.SetBounds(0, | |
| 306 0, | |
| 307 required_size.width() + extra.width(), | |
| 308 required_size.height() + extra.height()); | |
| 309 | |
| 310 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 297 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 311 EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, flags); | 298 EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, flags); |
| 312 } | 299 } |
| 313 | 300 |
| 314 TEST(LabelTest, DrawSingleLineString) { | 301 TEST(LabelTest, DrawSingleLineString) { |
| 315 Label label; | 302 Label label; |
| 316 label.set_focusable(false); | 303 label.set_focusable(false); |
| 317 | 304 |
| 318 // Turn off mirroring so that we don't need to figure out if | 305 // Turn off mirroring so that we don't need to figure out if |
| 319 // align right really means align left. | 306 // align right really means align left. |
| 320 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 307 label.set_directionality_mode(gfx::DERIVE_FROM_TEXT); |
| 321 | 308 |
| 322 string16 test_text(ASCIIToUTF16("Here's a string with no returns.")); | 309 string16 test_text(ASCIIToUTF16("Here's a string with no returns.")); |
| 323 label.SetText(test_text); | 310 label.SetText(test_text); |
| 324 gfx::Size required_size(label.GetPreferredSize()); | 311 gfx::Size required_size(label.GetPreferredSize()); |
| 325 gfx::Size extra(22, 8); | 312 gfx::Size extra(22, 8); |
| 326 label.SetBounds(0, | 313 label.SetBounds(0, |
| 327 0, | 314 0, |
| 328 required_size.width() + extra.width(), | 315 required_size.width() + extra.width(), |
| 329 required_size.height() + extra.height()); | 316 required_size.height() + extra.height()); |
| 330 | 317 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 | 415 |
| 429 // On Linux the underlying pango routines require a max height in order to | 416 // On Linux the underlying pango routines require a max height in order to |
| 430 // ellide multiline text. So until that can be resolved, we set all | 417 // ellide multiline text. So until that can be resolved, we set all |
| 431 // multiline lables to not ellide in Linux only. | 418 // multiline lables to not ellide in Linux only. |
| 432 TEST(LabelTest, DrawMultiLineString) { | 419 TEST(LabelTest, DrawMultiLineString) { |
| 433 Label label; | 420 Label label; |
| 434 label.set_focusable(false); | 421 label.set_focusable(false); |
| 435 | 422 |
| 436 // Turn off mirroring so that we don't need to figure out if | 423 // Turn off mirroring so that we don't need to figure out if |
| 437 // align right really means align left. | 424 // align right really means align left. |
| 438 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 425 label.set_directionality_mode(gfx::DERIVE_FROM_TEXT); |
| 439 | 426 |
| 440 string16 test_text(ASCIIToUTF16("Another string\nwith returns\n\n!")); | 427 string16 test_text(ASCIIToUTF16("Another string\nwith returns\n\n!")); |
| 441 label.SetText(test_text); | 428 label.SetText(test_text); |
| 442 label.SetMultiLine(true); | 429 label.SetMultiLine(true); |
| 443 label.SizeToFit(0); | 430 label.SizeToFit(0); |
| 444 gfx::Size extra(50, 10); | 431 gfx::Size extra(50, 10); |
| 445 label.SetBounds(label.x(), | 432 label.SetBounds(label.x(), |
| 446 label.y(), | 433 label.y(), |
| 447 label.width() + extra.width(), | 434 label.width() + extra.width(), |
| 448 label.height() + extra.height()); | 435 label.height() + extra.height()); |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 EXPECT_EQ( | 836 EXPECT_EQ( |
| 850 0, label.ComputeDrawStringFlags() & gfx::Canvas::NO_SUBPIXEL_RENDERING); | 837 0, label.ComputeDrawStringFlags() & gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 851 | 838 |
| 852 label.SetBackgroundColor(SkColorSetARGB(64, 255, 255, 255)); | 839 label.SetBackgroundColor(SkColorSetARGB(64, 255, 255, 255)); |
| 853 EXPECT_EQ( | 840 EXPECT_EQ( |
| 854 gfx::Canvas::NO_SUBPIXEL_RENDERING, | 841 gfx::Canvas::NO_SUBPIXEL_RENDERING, |
| 855 label.ComputeDrawStringFlags() & gfx::Canvas::NO_SUBPIXEL_RENDERING); | 842 label.ComputeDrawStringFlags() & gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 856 } | 843 } |
| 857 | 844 |
| 858 } // namespace views | 845 } // namespace views |
| OLD | NEW |