| 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/text_constants.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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 reverse_alignment ? Label::ALIGN_LEFT : Label::ALIGN_RIGHT, | 65 reverse_alignment ? Label::ALIGN_LEFT : Label::ALIGN_RIGHT, |
| 65 label.horizontal_alignment()); | 66 label.horizontal_alignment()); |
| 66 label.SetHorizontalAlignment(Label::ALIGN_LEFT); | 67 label.SetHorizontalAlignment(Label::ALIGN_LEFT); |
| 67 EXPECT_EQ( | 68 EXPECT_EQ( |
| 68 reverse_alignment ? Label::ALIGN_RIGHT : Label::ALIGN_LEFT, | 69 reverse_alignment ? Label::ALIGN_RIGHT : Label::ALIGN_LEFT, |
| 69 label.horizontal_alignment()); | 70 label.horizontal_alignment()); |
| 70 label.SetHorizontalAlignment(Label::ALIGN_CENTER); | 71 label.SetHorizontalAlignment(Label::ALIGN_CENTER); |
| 71 EXPECT_EQ(Label::ALIGN_CENTER, label.horizontal_alignment()); | 72 EXPECT_EQ(Label::ALIGN_CENTER, label.horizontal_alignment()); |
| 72 | 73 |
| 73 // The label's alignment should not be flipped if the directionality mode is | 74 // The label's alignment should not be flipped if the directionality mode is |
| 74 // AUTO_DETECT_DIRECTIONALITY. | 75 // gfx::DIRECTIONALITY_FROM_TEXT. |
| 75 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 76 label.set_directionality_mode(gfx::DIRECTIONALITY_FROM_TEXT); |
| 76 label.SetHorizontalAlignment(Label::ALIGN_RIGHT); | 77 label.SetHorizontalAlignment(Label::ALIGN_RIGHT); |
| 77 EXPECT_EQ(Label::ALIGN_RIGHT, label.horizontal_alignment()); | 78 EXPECT_EQ(Label::ALIGN_RIGHT, label.horizontal_alignment()); |
| 78 label.SetHorizontalAlignment(Label::ALIGN_LEFT); | 79 label.SetHorizontalAlignment(Label::ALIGN_LEFT); |
| 79 EXPECT_EQ(Label::ALIGN_LEFT, label.horizontal_alignment()); | 80 EXPECT_EQ(Label::ALIGN_LEFT, label.horizontal_alignment()); |
| 80 label.SetHorizontalAlignment(Label::ALIGN_CENTER); | 81 label.SetHorizontalAlignment(Label::ALIGN_CENTER); |
| 81 EXPECT_EQ(Label::ALIGN_CENTER, label.horizontal_alignment()); | 82 EXPECT_EQ(Label::ALIGN_CENTER, label.horizontal_alignment()); |
| 82 } | 83 } |
| 83 | 84 |
| 84 TEST(LabelTest, DirectionalityModeProperty) { | 85 TEST(LabelTest, DirectionalityModeProperty) { |
| 85 Label label; | 86 Label label; |
| 86 EXPECT_EQ(Label::USE_UI_DIRECTIONALITY, label.directionality_mode()); | 87 EXPECT_EQ(gfx::DIRECTIONALITY_FROM_UI, label.directionality_mode()); |
| 87 | 88 |
| 88 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 89 label.set_directionality_mode(gfx::DIRECTIONALITY_FROM_TEXT); |
| 89 EXPECT_EQ(Label::AUTO_DETECT_DIRECTIONALITY, label.directionality_mode()); | 90 EXPECT_EQ(gfx::DIRECTIONALITY_FROM_TEXT, label.directionality_mode()); |
| 90 | 91 |
| 91 label.set_directionality_mode(Label::USE_UI_DIRECTIONALITY); | 92 label.set_directionality_mode(gfx::DIRECTIONALITY_FROM_UI); |
| 92 EXPECT_EQ(Label::USE_UI_DIRECTIONALITY, label.directionality_mode()); | 93 EXPECT_EQ(gfx::DIRECTIONALITY_FROM_UI, label.directionality_mode()); |
| 93 } | 94 } |
| 94 | 95 |
| 95 TEST(LabelTest, MultiLineProperty) { | 96 TEST(LabelTest, MultiLineProperty) { |
| 96 Label label; | 97 Label label; |
| 97 EXPECT_FALSE(label.is_multi_line()); | 98 EXPECT_FALSE(label.is_multi_line()); |
| 98 label.SetMultiLine(true); | 99 label.SetMultiLine(true); |
| 99 EXPECT_TRUE(label.is_multi_line()); | 100 EXPECT_TRUE(label.is_multi_line()); |
| 100 label.SetMultiLine(false); | 101 label.SetMultiLine(false); |
| 101 EXPECT_FALSE(label.is_multi_line()); | 102 EXPECT_FALSE(label.is_multi_line()); |
| 102 } | 103 } |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 label.SetBounds(0, 0, 0, 0); | 266 label.SetBounds(0, 0, 0, 0); |
| 266 gfx::Size required_size_with_border = label.GetPreferredSize(); | 267 gfx::Size required_size_with_border = label.GetPreferredSize(); |
| 267 EXPECT_EQ(required_size_with_border.height(), | 268 EXPECT_EQ(required_size_with_border.height(), |
| 268 required_size.height() + border.height()); | 269 required_size.height() + border.height()); |
| 269 EXPECT_EQ(required_size_with_border.width(), | 270 EXPECT_EQ(required_size_with_border.width(), |
| 270 required_size.width() + border.width()); | 271 required_size.width() + border.width()); |
| 271 } | 272 } |
| 272 | 273 |
| 273 TEST(LabelTest, AutoDetectDirectionality) { | 274 TEST(LabelTest, AutoDetectDirectionality) { |
| 274 Label label; | 275 Label label; |
| 275 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 276 label.set_directionality_mode(gfx::DIRECTIONALITY_FROM_TEXT); |
| 276 | |
| 277 // Test text starts with RTL character. | |
| 278 string16 test_text(WideToUTF16(L" \x5d0\x5d1\x5d2 abc")); | |
| 279 label.SetText(test_text); | |
| 280 gfx::Size required_size(label.GetPreferredSize()); | |
| 281 gfx::Size extra(22, 8); | |
| 282 label.SetBounds(0, | |
| 283 0, | |
| 284 required_size.width() + extra.width(), | |
| 285 required_size.height() + extra.height()); | |
| 286 | 277 |
| 287 string16 paint_text; | 278 string16 paint_text; |
| 288 gfx::Rect text_bounds; | 279 gfx::Rect text_bounds; |
| 289 int flags; | 280 int flags = 0; |
| 281 |
| 282 // Test text starting with an RTL character. |
| 283 label.SetText(WideToUTF16(L" \x5d0\x5d1\x5d2 abc ")); |
| 290 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 284 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 291 EXPECT_EQ(gfx::Canvas::FORCE_RTL_DIRECTIONALITY, flags); | 285 EXPECT_EQ(gfx::Canvas::FORCE_RTL_DIRECTIONALITY, flags); |
| 292 | 286 |
| 293 // Test text starts with LTR character. | 287 // Test text starting with an LTR character. |
| 294 test_text = (WideToUTF16(L"ltr \x5d0\x5d1\x5d2 abc")); | 288 label.SetText(WideToUTF16(L"ltr \x5d0\x5d1\x5d2 abc ")); |
| 295 label.SetText(test_text); | |
| 296 required_size = label.GetPreferredSize(); | |
| 297 label.SetBounds(0, | |
| 298 0, | |
| 299 required_size.width() + extra.width(), | |
| 300 required_size.height() + extra.height()); | |
| 301 | |
| 302 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); | 289 label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags); |
| 303 EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, flags); | 290 EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY, flags); |
| 304 } | 291 } |
| 305 | 292 |
| 306 TEST(LabelTest, DrawSingleLineString) { | 293 TEST(LabelTest, DrawSingleLineString) { |
| 307 Label label; | 294 Label label; |
| 308 label.set_focusable(false); | 295 label.set_focusable(false); |
| 309 | 296 |
| 310 // Turn off mirroring so that we don't need to figure out if | 297 // Turn off mirroring so that we don't need to figure out if |
| 311 // align right really means align left. | 298 // align right really means align left. |
| 312 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 299 label.set_directionality_mode(gfx::DIRECTIONALITY_FROM_TEXT); |
| 313 | 300 |
| 314 string16 test_text(ASCIIToUTF16("Here's a string with no returns.")); | 301 string16 test_text(ASCIIToUTF16("Here's a string with no returns.")); |
| 315 label.SetText(test_text); | 302 label.SetText(test_text); |
| 316 gfx::Size required_size(label.GetPreferredSize()); | 303 gfx::Size required_size(label.GetPreferredSize()); |
| 317 gfx::Size extra(22, 8); | 304 gfx::Size extra(22, 8); |
| 318 label.SetBounds(0, | 305 label.SetBounds(0, |
| 319 0, | 306 0, |
| 320 required_size.width() + extra.width(), | 307 required_size.width() + extra.width(), |
| 321 required_size.height() + extra.height()); | 308 required_size.height() + extra.height()); |
| 322 | 309 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 407 |
| 421 // On Linux the underlying pango routines require a max height in order to | 408 // On Linux the underlying pango routines require a max height in order to |
| 422 // ellide multiline text. So until that can be resolved, we set all | 409 // ellide multiline text. So until that can be resolved, we set all |
| 423 // multiline lables to not ellide in Linux only. | 410 // multiline lables to not ellide in Linux only. |
| 424 TEST(LabelTest, DrawMultiLineString) { | 411 TEST(LabelTest, DrawMultiLineString) { |
| 425 Label label; | 412 Label label; |
| 426 label.set_focusable(false); | 413 label.set_focusable(false); |
| 427 | 414 |
| 428 // Turn off mirroring so that we don't need to figure out if | 415 // Turn off mirroring so that we don't need to figure out if |
| 429 // align right really means align left. | 416 // align right really means align left. |
| 430 label.set_directionality_mode(Label::AUTO_DETECT_DIRECTIONALITY); | 417 label.set_directionality_mode(gfx::DIRECTIONALITY_FROM_TEXT); |
| 431 | 418 |
| 432 string16 test_text(ASCIIToUTF16("Another string\nwith returns\n\n!")); | 419 string16 test_text(ASCIIToUTF16("Another string\nwith returns\n\n!")); |
| 433 label.SetText(test_text); | 420 label.SetText(test_text); |
| 434 label.SetMultiLine(true); | 421 label.SetMultiLine(true); |
| 435 label.SizeToFit(0); | 422 label.SizeToFit(0); |
| 436 gfx::Size extra(50, 10); | 423 gfx::Size extra(50, 10); |
| 437 label.SetBounds(label.x(), | 424 label.SetBounds(label.x(), |
| 438 label.y(), | 425 label.y(), |
| 439 label.width() + extra.width(), | 426 label.width() + extra.width(), |
| 440 label.height() + extra.height()); | 427 label.height() + extra.height()); |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 841 EXPECT_EQ( | 828 EXPECT_EQ( |
| 842 0, label.ComputeDrawStringFlags() & gfx::Canvas::NO_SUBPIXEL_RENDERING); | 829 0, label.ComputeDrawStringFlags() & gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 843 | 830 |
| 844 label.SetBackgroundColor(SkColorSetARGB(64, 255, 255, 255)); | 831 label.SetBackgroundColor(SkColorSetARGB(64, 255, 255, 255)); |
| 845 EXPECT_EQ( | 832 EXPECT_EQ( |
| 846 gfx::Canvas::NO_SUBPIXEL_RENDERING, | 833 gfx::Canvas::NO_SUBPIXEL_RENDERING, |
| 847 label.ComputeDrawStringFlags() & gfx::Canvas::NO_SUBPIXEL_RENDERING); | 834 label.ComputeDrawStringFlags() & gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 848 } | 835 } |
| 849 | 836 |
| 850 } // namespace views | 837 } // namespace views |
| OLD | NEW |