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
3 // found in the LICENSE file.
5 #include "ui/views/controls/label.h"
7 #include "base/i18n/rtl.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/accessibility/ax_view_state.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/views/border.h"
15 using base::ASCIIToUTF16
;
19 // All text sizing measurements (width and height) should be greater than this.
20 const int kMinTextDimension
= 4;
22 // A test utility function to set the application default text direction.
23 void SetRTL(bool rtl
) {
24 // Override the current locale/direction.
25 base::i18n::SetICUDefaultLocale(rtl
? "he" : "en");
26 EXPECT_EQ(rtl
, base::i18n::IsRTL());
29 TEST(LabelTest
, FontPropertySymbol
) {
31 std::string
font_name("symbol");
32 gfx::Font
font(font_name
, 26);
33 label
.SetFontList(gfx::FontList(font
));
34 gfx::Font font_used
= label
.font_list().GetPrimaryFont();
35 EXPECT_EQ(font_name
, font_used
.GetFontName());
36 EXPECT_EQ(26, font_used
.GetFontSize());
39 TEST(LabelTest
, FontPropertyArial
) {
41 std::string
font_name("arial");
42 gfx::Font
font(font_name
, 30);
43 label
.SetFontList(gfx::FontList(font
));
44 gfx::Font font_used
= label
.font_list().GetPrimaryFont();
45 EXPECT_EQ(font_name
, font_used
.GetFontName());
46 EXPECT_EQ(30, font_used
.GetFontSize());
49 TEST(LabelTest
, TextProperty
) {
51 base::string16
test_text(ASCIIToUTF16("A random string."));
52 label
.SetText(test_text
);
53 EXPECT_EQ(test_text
, label
.text());
56 TEST(LabelTest
, ColorProperty
) {
58 SkColor color
= SkColorSetARGB(20, 40, 10, 5);
59 label
.SetAutoColorReadabilityEnabled(false);
60 label
.SetEnabledColor(color
);
61 EXPECT_EQ(color
, label
.enabled_color());
64 TEST(LabelTest
, AlignmentProperty
) {
65 const bool was_rtl
= base::i18n::IsRTL();
68 for (size_t i
= 0; i
< 2; ++i
) {
69 // Toggle the application default text direction (to try each direction).
70 SetRTL(!base::i18n::IsRTL());
71 bool reverse_alignment
= base::i18n::IsRTL();
73 // The alignment should be flipped in RTL UI.
74 label
.SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
75 EXPECT_EQ(reverse_alignment
? gfx::ALIGN_LEFT
: gfx::ALIGN_RIGHT
,
76 label
.GetHorizontalAlignment());
77 label
.SetHorizontalAlignment(gfx::ALIGN_LEFT
);
78 EXPECT_EQ(reverse_alignment
? gfx::ALIGN_RIGHT
: gfx::ALIGN_LEFT
,
79 label
.GetHorizontalAlignment());
80 label
.SetHorizontalAlignment(gfx::ALIGN_CENTER
);
81 EXPECT_EQ(gfx::ALIGN_CENTER
, label
.GetHorizontalAlignment());
83 for (size_t j
= 0; j
< 2; ++j
) {
84 label
.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD
);
85 const bool rtl
= j
== 0;
86 label
.SetText(rtl
? base::WideToUTF16(L
"\x5d0") : ASCIIToUTF16("A"));
87 EXPECT_EQ(rtl
? gfx::ALIGN_RIGHT
: gfx::ALIGN_LEFT
,
88 label
.GetHorizontalAlignment());
92 EXPECT_EQ(was_rtl
, base::i18n::IsRTL());
95 TEST(LabelTest
, DirectionalityModeProperty
) {
97 EXPECT_EQ(gfx::DIRECTIONALITY_FROM_UI
, label
.directionality_mode());
99 label
.set_directionality_mode(gfx::DIRECTIONALITY_FROM_TEXT
);
100 EXPECT_EQ(gfx::DIRECTIONALITY_FROM_TEXT
, label
.directionality_mode());
102 label
.set_directionality_mode(gfx::DIRECTIONALITY_FROM_UI
);
103 EXPECT_EQ(gfx::DIRECTIONALITY_FROM_UI
, label
.directionality_mode());
106 TEST(LabelTest
, MultiLineProperty
) {
108 EXPECT_FALSE(label
.is_multi_line());
109 label
.SetMultiLine(true);
110 EXPECT_TRUE(label
.is_multi_line());
111 label
.SetMultiLine(false);
112 EXPECT_FALSE(label
.is_multi_line());
115 TEST(LabelTest
, ObscuredProperty
) {
117 base::string16
test_text(ASCIIToUTF16("Password!"));
118 label
.SetText(test_text
);
120 // Should be false by default...
121 EXPECT_FALSE(label
.is_obscured());
122 EXPECT_EQ(test_text
, label
.layout_text());
123 EXPECT_EQ(test_text
, label
.text());
125 label
.SetObscured(true);
126 EXPECT_TRUE(label
.is_obscured());
127 EXPECT_EQ(ASCIIToUTF16("*********"), label
.layout_text());
128 EXPECT_EQ(test_text
, label
.text());
130 label
.SetText(test_text
+ test_text
);
131 EXPECT_EQ(ASCIIToUTF16("******************"), label
.layout_text());
132 EXPECT_EQ(test_text
+ test_text
, label
.text());
134 label
.SetObscured(false);
135 EXPECT_FALSE(label
.is_obscured());
136 EXPECT_EQ(test_text
+ test_text
, label
.layout_text());
137 EXPECT_EQ(test_text
+ test_text
, label
.text());
140 TEST(LabelTest
, ObscuredSurrogatePair
) {
141 // 'MUSICAL SYMBOL G CLEF': represented in UTF-16 as two characters
142 // forming the surrogate pair 0x0001D11E.
144 base::string16 test_text
= base::UTF8ToUTF16("\xF0\x9D\x84\x9E");
145 label
.SetText(test_text
);
147 label
.SetObscured(true);
148 EXPECT_EQ(ASCIIToUTF16("*"), label
.layout_text());
149 EXPECT_EQ(test_text
, label
.text());
152 TEST(LabelTest
, TooltipProperty
) {
154 label
.SetText(ASCIIToUTF16("My cool string."));
156 base::string16 tooltip
;
157 EXPECT_TRUE(label
.GetTooltipText(gfx::Point(), &tooltip
));
158 EXPECT_EQ(label
.text(), tooltip
);
160 base::string16
tooltip_text(ASCIIToUTF16("The tooltip!"));
161 label
.SetTooltipText(tooltip_text
);
162 EXPECT_TRUE(label
.GetTooltipText(gfx::Point(), &tooltip
));
163 EXPECT_EQ(tooltip_text
, tooltip
);
165 label
.SetTooltipText(base::string16());
166 EXPECT_TRUE(label
.GetTooltipText(gfx::Point(), &tooltip
));
167 EXPECT_EQ(label
.text(), tooltip
);
169 // Make the label big enough to hold the text
170 // and expect there to be no tooltip.
171 label
.SetBounds(0, 0, 1000, 40);
172 EXPECT_FALSE(label
.GetTooltipText(gfx::Point(), &tooltip
));
174 // Verify that setting the tooltip still shows it.
175 label
.SetTooltipText(tooltip_text
);
176 EXPECT_TRUE(label
.GetTooltipText(gfx::Point(), &tooltip
));
177 EXPECT_EQ(tooltip_text
, tooltip
);
178 // Clear out the tooltip.
179 label
.SetTooltipText(base::string16());
181 // Shrink the bounds and the tooltip should come back.
182 label
.SetBounds(0, 0, 1, 1);
183 EXPECT_TRUE(label
.GetTooltipText(gfx::Point(), &tooltip
));
185 // Make the label obscured and there is no tooltip.
186 label
.SetObscured(true);
187 EXPECT_FALSE(label
.GetTooltipText(gfx::Point(), &tooltip
));
189 // Obscuring the text shouldn't permanently clobber the tooltip.
190 label
.SetObscured(false);
191 EXPECT_TRUE(label
.GetTooltipText(gfx::Point(), &tooltip
));
193 // Make the label multiline and there is no tooltip.
194 label
.SetMultiLine(true);
195 EXPECT_FALSE(label
.GetTooltipText(gfx::Point(), &tooltip
));
197 // Verify that setting the tooltip still shows it.
198 label
.SetTooltipText(tooltip_text
);
199 EXPECT_TRUE(label
.GetTooltipText(gfx::Point(), &tooltip
));
200 EXPECT_EQ(tooltip_text
, tooltip
);
201 // Clear out the tooltip.
202 label
.SetTooltipText(base::string16());
205 TEST(LabelTest
, Accessibility
) {
207 label
.SetText(ASCIIToUTF16("My special text."));
209 ui::AXViewState state
;
210 label
.GetAccessibleState(&state
);
211 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT
, state
.role
);
212 EXPECT_EQ(label
.text(), state
.name
);
213 EXPECT_TRUE(state
.HasStateFlag(ui::AX_STATE_READ_ONLY
));
216 TEST(LabelTest
, SingleLineSizing
) {
218 label
.SetText(ASCIIToUTF16("A not so random string in one line."));
221 gfx::Size required_size
= label
.GetPreferredSize();
222 EXPECT_GT(required_size
.height(), kMinTextDimension
);
223 EXPECT_GT(required_size
.width(), kMinTextDimension
);
225 // Test everything with borders.
226 gfx::Insets
border(10, 20, 30, 40);
227 label
.SetBorder(Border::CreateEmptyBorder(
228 border
.top(), border
.left(), border
.bottom(), border
.right()));
230 // GetPreferredSize and borders.
231 label
.SetBounds(0, 0, 0, 0);
232 gfx::Size required_size_with_border
= label
.GetPreferredSize();
233 EXPECT_EQ(required_size_with_border
.height(),
234 required_size
.height() + border
.height());
235 EXPECT_EQ(required_size_with_border
.width(),
236 required_size
.width() + border
.width());
239 TEST(LabelTest
, MultilineSmallAvailableWidthSizing
) {
241 label
.SetMultiLine(true);
242 label
.SetAllowCharacterBreak(true);
243 label
.SetText(ASCIIToUTF16("Too Wide."));
245 // Check that Label can be laid out at a variety of small sizes,
246 // splitting the words into up to one character per line if necessary.
247 // Incorrect word splitting may cause infinite loops in text layout.
248 gfx::Size required_size
= label
.GetPreferredSize();
249 for (int i
= 1; i
< required_size
.width(); ++i
)
250 EXPECT_GT(label
.GetHeightForWidth(i
), 0);
253 TEST(LabelTest
, MultiLineSizing
) {
255 label
.SetFocusable(false);
257 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!"));
258 label
.SetMultiLine(true);
261 gfx::Size required_size
= label
.GetPreferredSize();
262 EXPECT_GT(required_size
.height(), kMinTextDimension
);
263 EXPECT_GT(required_size
.width(), kMinTextDimension
);
265 // SizeToFit with unlimited width.
267 int required_width
= label
.GetLocalBounds().width();
268 EXPECT_GT(required_width
, kMinTextDimension
);
270 // SizeToFit with limited width.
271 label
.SizeToFit(required_width
- 1);
272 int constrained_width
= label
.GetLocalBounds().width();
274 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc)
275 // has to be fixed to return the size that fits to given width/height.
276 EXPECT_LT(constrained_width
, required_width
);
278 EXPECT_GT(constrained_width
, kMinTextDimension
);
280 // Change the width back to the desire width.
281 label
.SizeToFit(required_width
);
282 EXPECT_EQ(required_width
, label
.GetLocalBounds().width());
284 // General tests for GetHeightForWidth.
285 int required_height
= label
.GetHeightForWidth(required_width
);
286 EXPECT_GT(required_height
, kMinTextDimension
);
287 int height_for_constrained_width
= label
.GetHeightForWidth(constrained_width
);
289 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc)
290 // has to be fixed to return the size that fits to given width/height.
291 EXPECT_GT(height_for_constrained_width
, required_height
);
293 // Using the constrained width or the required_width - 1 should give the
294 // same result for the height because the constrainted width is the tight
295 // width when given "required_width - 1" as the max width.
296 EXPECT_EQ(height_for_constrained_width
,
297 label
.GetHeightForWidth(required_width
- 1));
299 // Test everything with borders.
300 gfx::Insets
border(10, 20, 30, 40);
301 label
.SetBorder(Border::CreateEmptyBorder(
302 border
.top(), border
.left(), border
.bottom(), border
.right()));
304 // SizeToFit and borders.
306 int required_width_with_border
= label
.GetLocalBounds().width();
307 EXPECT_EQ(required_width_with_border
, required_width
+ border
.width());
309 // GetHeightForWidth and borders.
310 int required_height_with_border
=
311 label
.GetHeightForWidth(required_width_with_border
);
312 EXPECT_EQ(required_height_with_border
, required_height
+ border
.height());
314 // Test that the border width is subtracted before doing the height
315 // calculation. If it is, then the height will grow when width
317 int height1
= label
.GetHeightForWidth(required_width_with_border
- 1);
319 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc)
320 // has to be fixed to return the size that fits to given width/height.
321 EXPECT_GT(height1
, required_height_with_border
);
323 EXPECT_EQ(height1
, height_for_constrained_width
+ border
.height());
325 // GetPreferredSize and borders.
326 label
.SetBounds(0, 0, 0, 0);
327 gfx::Size required_size_with_border
= label
.GetPreferredSize();
328 EXPECT_EQ(required_size_with_border
.height(),
329 required_size
.height() + border
.height());
330 EXPECT_EQ(required_size_with_border
.width(),
331 required_size
.width() + border
.width());
334 TEST(LabelTest
, DirectionalityFromText
) {
336 label
.set_directionality_mode(gfx::DIRECTIONALITY_FROM_TEXT
);
337 label
.SetBounds(0, 0, 1000, 1000);
338 base::string16 paint_text
;
339 gfx::Rect text_bounds
;
342 // Test text starts with RTL character.
343 label
.SetText(base::WideToUTF16(L
" \x5d0\x5d1\x5d2 abc"));
344 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
345 EXPECT_EQ(gfx::Canvas::FORCE_RTL_DIRECTIONALITY
,
346 flags
& (gfx::Canvas::FORCE_RTL_DIRECTIONALITY
|
347 gfx::Canvas::FORCE_LTR_DIRECTIONALITY
));
349 // Test text starts with LTR character.
350 label
.SetText(base::WideToUTF16(L
"ltr \x5d0\x5d1\x5d2 abc"));
351 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
352 EXPECT_EQ(gfx::Canvas::FORCE_LTR_DIRECTIONALITY
,
353 flags
& (gfx::Canvas::FORCE_RTL_DIRECTIONALITY
|
354 gfx::Canvas::FORCE_LTR_DIRECTIONALITY
));
357 TEST(LabelTest
, DrawSingleLineString
) {
359 label
.SetFocusable(false);
360 // Force a directionality to simplify alignment value testing.
361 label
.set_directionality_mode(gfx::DIRECTIONALITY_FORCE_LTR
);
363 label
.SetText(ASCIIToUTF16("Here's a string with no returns."));
364 gfx::Size
required_size(label
.GetPreferredSize());
365 gfx::Size
extra(22, 8);
366 label
.SetBounds(0, 0, required_size
.width() + extra
.width(),
367 required_size
.height() + extra
.height());
369 // Do some basic verifications for all three alignments.
370 base::string16 paint_text
;
371 gfx::Rect text_bounds
;
375 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
376 EXPECT_EQ(label
.text(), paint_text
);
377 // The text should be centered horizontally and vertically.
378 EXPECT_EQ(extra
.width() / 2, text_bounds
.x());
379 EXPECT_EQ(extra
.height() / 2 , text_bounds
.y());
380 EXPECT_EQ(required_size
.width(), text_bounds
.width());
381 EXPECT_EQ(required_size
.height(), text_bounds
.height());
382 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_CENTER
,
383 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
384 gfx::Canvas::TEXT_ALIGN_CENTER
|
385 gfx::Canvas::TEXT_ALIGN_RIGHT
));
387 // Left aligned text.
388 label
.SetHorizontalAlignment(gfx::ALIGN_LEFT
);
390 text_bounds
.SetRect(0, 0, 0, 0);
391 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
392 EXPECT_EQ(label
.text(), paint_text
);
393 // The text should be left aligned horizontally and centered vertically.
394 EXPECT_EQ(0, text_bounds
.x());
395 EXPECT_EQ(extra
.height() / 2 , text_bounds
.y());
396 EXPECT_EQ(required_size
.width(), text_bounds
.width());
397 EXPECT_EQ(required_size
.height(), text_bounds
.height());
398 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT
,
399 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
400 gfx::Canvas::TEXT_ALIGN_CENTER
|
401 gfx::Canvas::TEXT_ALIGN_RIGHT
));
403 // Right aligned text.
404 label
.SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
406 text_bounds
.SetRect(0, 0, 0, 0);
407 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
408 EXPECT_EQ(label
.text(), paint_text
);
409 // The text should be right aligned horizontally and centered vertically.
410 EXPECT_EQ(extra
.width(), text_bounds
.x());
411 EXPECT_EQ(extra
.height() / 2 , text_bounds
.y());
412 EXPECT_EQ(required_size
.width(), text_bounds
.width());
413 EXPECT_EQ(required_size
.height(), text_bounds
.height());
414 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT
,
415 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
416 gfx::Canvas::TEXT_ALIGN_CENTER
|
417 gfx::Canvas::TEXT_ALIGN_RIGHT
));
419 // Test single line drawing with a border.
420 gfx::Insets
border(39, 34, 8, 96);
421 label
.SetBorder(Border::CreateEmptyBorder(
422 border
.top(), border
.left(), border
.bottom(), border
.right()));
424 gfx::Size
required_size_with_border(label
.GetPreferredSize());
425 EXPECT_EQ(required_size
.width() + border
.width(),
426 required_size_with_border
.width());
427 EXPECT_EQ(required_size
.height() + border
.height(),
428 required_size_with_border
.height());
429 label
.SetBounds(0, 0, required_size_with_border
.width() + extra
.width(),
430 required_size_with_border
.height() + extra
.height());
432 // Centered text with border.
433 label
.SetHorizontalAlignment(gfx::ALIGN_CENTER
);
435 text_bounds
.SetRect(0, 0, 0, 0);
436 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
437 EXPECT_EQ(label
.text(), paint_text
);
438 // The text should be centered horizontally and vertically within the border.
439 EXPECT_EQ(border
.left() + extra
.width() / 2, text_bounds
.x());
440 EXPECT_EQ(border
.top() + extra
.height() / 2 , text_bounds
.y());
441 EXPECT_EQ(required_size
.width(), text_bounds
.width());
442 EXPECT_EQ(required_size
.height(), text_bounds
.height());
443 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_CENTER
,
444 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
445 gfx::Canvas::TEXT_ALIGN_CENTER
|
446 gfx::Canvas::TEXT_ALIGN_RIGHT
));
448 // Left aligned text with border.
449 label
.SetHorizontalAlignment(gfx::ALIGN_LEFT
);
451 text_bounds
.SetRect(0, 0, 0, 0);
452 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
453 EXPECT_EQ(label
.text(), paint_text
);
454 // The text should be left aligned horizontally and centered vertically.
455 EXPECT_EQ(border
.left(), text_bounds
.x());
456 EXPECT_EQ(border
.top() + extra
.height() / 2 , text_bounds
.y());
457 EXPECT_EQ(required_size
.width(), text_bounds
.width());
458 EXPECT_EQ(required_size
.height(), text_bounds
.height());
459 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT
,
460 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
461 gfx::Canvas::TEXT_ALIGN_CENTER
|
462 gfx::Canvas::TEXT_ALIGN_RIGHT
));
464 // Right aligned text.
465 label
.SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
467 text_bounds
.SetRect(0, 0, 0, 0);
468 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
469 EXPECT_EQ(label
.text(), paint_text
);
470 // The text should be right aligned horizontally and centered vertically.
471 EXPECT_EQ(border
.left() + extra
.width(), text_bounds
.x());
472 EXPECT_EQ(border
.top() + extra
.height() / 2 , text_bounds
.y());
473 EXPECT_EQ(required_size
.width(), text_bounds
.width());
474 EXPECT_EQ(required_size
.height(), text_bounds
.height());
475 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT
,
476 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
477 gfx::Canvas::TEXT_ALIGN_CENTER
|
478 gfx::Canvas::TEXT_ALIGN_RIGHT
));
481 // Pango needs a max height to elide multiline text; that is not supported here.
482 TEST(LabelTest
, DrawMultiLineString
) {
484 label
.SetFocusable(false);
485 // Force a directionality to simplify alignment value testing.
486 label
.set_directionality_mode(gfx::DIRECTIONALITY_FORCE_LTR
);
487 // Set a background color to prevent gfx::Canvas::NO_SUBPIXEL_RENDERING flags.
488 label
.SetBackgroundColor(SK_ColorWHITE
);
490 label
.SetText(ASCIIToUTF16("Another string\nwith returns\n\n!"));
491 label
.SetMultiLine(true);
493 gfx::Size
extra(50, 10);
494 label
.SetBounds(label
.x(), label
.y(),
495 label
.width() + extra
.width(),
496 label
.height() + extra
.height());
498 // Do some basic verifications for all three alignments.
499 base::string16 paint_text
;
500 gfx::Rect text_bounds
;
502 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
503 EXPECT_EQ(label
.text(), paint_text
);
504 EXPECT_EQ(extra
.width() / 2, text_bounds
.x());
505 EXPECT_EQ(extra
.height() / 2, text_bounds
.y());
506 EXPECT_GT(text_bounds
.width(), kMinTextDimension
);
507 EXPECT_GT(text_bounds
.height(), kMinTextDimension
);
508 int expected_flags
= gfx::Canvas::MULTI_LINE
|
509 gfx::Canvas::TEXT_ALIGN_CENTER
|
510 gfx::Canvas::FORCE_LTR_DIRECTIONALITY
;
512 expected_flags
|= gfx::Canvas::NO_ELLIPSIS
;
514 EXPECT_EQ(expected_flags
, expected_flags
);
515 gfx::Rect
center_bounds(text_bounds
);
517 label
.SetHorizontalAlignment(gfx::ALIGN_LEFT
);
519 text_bounds
.SetRect(0, 0, 0, 0);
520 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
521 EXPECT_EQ(label
.text(), paint_text
);
522 EXPECT_EQ(0, text_bounds
.x());
523 EXPECT_EQ(extra
.height() / 2, text_bounds
.y());
524 EXPECT_GT(text_bounds
.width(), kMinTextDimension
);
525 EXPECT_GT(text_bounds
.height(), kMinTextDimension
);
526 expected_flags
= gfx::Canvas::MULTI_LINE
|
527 gfx::Canvas::TEXT_ALIGN_LEFT
|
528 gfx::Canvas::FORCE_LTR_DIRECTIONALITY
;
530 expected_flags
|= gfx::Canvas::NO_ELLIPSIS
;
532 EXPECT_EQ(expected_flags
, expected_flags
);
534 label
.SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
536 text_bounds
.SetRect(0, 0, 0, 0);
537 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
538 EXPECT_EQ(label
.text(), paint_text
);
539 EXPECT_EQ(extra
.width(), text_bounds
.x());
540 EXPECT_EQ(extra
.height() / 2, text_bounds
.y());
541 EXPECT_GT(text_bounds
.width(), kMinTextDimension
);
542 EXPECT_GT(text_bounds
.height(), kMinTextDimension
);
543 expected_flags
= gfx::Canvas::MULTI_LINE
|
544 gfx::Canvas::TEXT_ALIGN_RIGHT
|
545 gfx::Canvas::FORCE_LTR_DIRECTIONALITY
;
547 expected_flags
|= gfx::Canvas::NO_ELLIPSIS
;
549 EXPECT_EQ(expected_flags
, expected_flags
);
551 // Test multiline drawing with a border.
552 gfx::Insets
border(19, 92, 23, 2);
553 label
.SetBorder(Border::CreateEmptyBorder(
554 border
.top(), border
.left(), border
.bottom(), border
.right()));
556 label
.SetBounds(label
.x(), label
.y(),
557 label
.width() + extra
.width(),
558 label
.height() + extra
.height());
560 label
.SetHorizontalAlignment(gfx::ALIGN_CENTER
);
562 text_bounds
.SetRect(0, 0, 0, 0);
563 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
564 EXPECT_EQ(label
.text(), paint_text
);
565 EXPECT_EQ(border
.left() + extra
.width() / 2, text_bounds
.x());
566 EXPECT_EQ(border
.top() + extra
.height() / 2, text_bounds
.y());
567 EXPECT_EQ(center_bounds
.width(), text_bounds
.width());
568 EXPECT_EQ(center_bounds
.height(), text_bounds
.height());
569 expected_flags
= gfx::Canvas::MULTI_LINE
|
570 gfx::Canvas::TEXT_ALIGN_CENTER
|
571 gfx::Canvas::FORCE_LTR_DIRECTIONALITY
;
573 expected_flags
|= gfx::Canvas::NO_ELLIPSIS
;
575 EXPECT_EQ(expected_flags
, expected_flags
);
577 label
.SetHorizontalAlignment(gfx::ALIGN_LEFT
);
579 text_bounds
.SetRect(0, 0, 0, 0);
580 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
581 EXPECT_EQ(label
.text(), paint_text
);
582 EXPECT_EQ(border
.left(), text_bounds
.x());
583 EXPECT_EQ(border
.top() + extra
.height() / 2, text_bounds
.y());
584 EXPECT_EQ(center_bounds
.width(), text_bounds
.width());
585 EXPECT_EQ(center_bounds
.height(), text_bounds
.height());
586 expected_flags
= gfx::Canvas::MULTI_LINE
|
587 gfx::Canvas::TEXT_ALIGN_LEFT
|
588 gfx::Canvas::FORCE_LTR_DIRECTIONALITY
;
590 expected_flags
|= gfx::Canvas::NO_ELLIPSIS
;
592 EXPECT_EQ(expected_flags
, expected_flags
);
594 label
.SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
596 text_bounds
.SetRect(0, 0, 0, 0);
597 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
598 EXPECT_EQ(label
.text(), paint_text
);
599 EXPECT_EQ(extra
.width() + border
.left(), text_bounds
.x());
600 EXPECT_EQ(border
.top() + extra
.height() / 2, text_bounds
.y());
601 EXPECT_EQ(center_bounds
.width(), text_bounds
.width());
602 EXPECT_EQ(center_bounds
.height(), text_bounds
.height());
603 expected_flags
= gfx::Canvas::MULTI_LINE
|
604 gfx::Canvas::TEXT_ALIGN_RIGHT
|
605 gfx::Canvas::FORCE_LTR_DIRECTIONALITY
;
607 expected_flags
|= gfx::Canvas::NO_ELLIPSIS
;
609 EXPECT_EQ(expected_flags
, expected_flags
);
612 TEST(LabelTest
, DrawSingleLineStringInRTL
) {
614 label
.SetFocusable(false);
616 std::string locale
= l10n_util::GetApplicationLocale("");
617 base::i18n::SetICUDefaultLocale("he");
619 label
.SetText(ASCIIToUTF16("Here's a string with no returns."));
620 gfx::Size
required_size(label
.GetPreferredSize());
621 gfx::Size
extra(22, 8);
622 label
.SetBounds(0, 0, required_size
.width() + extra
.width(),
623 required_size
.height() + extra
.height());
625 // Do some basic verifications for all three alignments.
626 base::string16 paint_text
;
627 gfx::Rect text_bounds
;
631 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
632 EXPECT_EQ(label
.text(), paint_text
);
633 // The text should be centered horizontally and vertically.
634 EXPECT_EQ(extra
.width() / 2, text_bounds
.x());
635 EXPECT_EQ(extra
.height() / 2 , text_bounds
.y());
636 EXPECT_EQ(required_size
.width(), text_bounds
.width());
637 EXPECT_EQ(required_size
.height(), text_bounds
.height());
638 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_CENTER
,
639 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
640 gfx::Canvas::TEXT_ALIGN_CENTER
|
641 gfx::Canvas::TEXT_ALIGN_RIGHT
));
644 label
.SetHorizontalAlignment(gfx::ALIGN_LEFT
);
646 text_bounds
.SetRect(0, 0, 0, 0);
647 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
648 EXPECT_EQ(label
.text(), paint_text
);
649 // The text should be right aligned horizontally and centered vertically.
650 EXPECT_EQ(extra
.width(), text_bounds
.x());
651 EXPECT_EQ(extra
.height() / 2 , text_bounds
.y());
652 EXPECT_EQ(required_size
.width(), text_bounds
.width());
653 EXPECT_EQ(required_size
.height(), text_bounds
.height());
654 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT
,
655 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
656 gfx::Canvas::TEXT_ALIGN_CENTER
|
657 gfx::Canvas::TEXT_ALIGN_RIGHT
));
659 // ALIGN_RIGHT label.
660 label
.SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
662 text_bounds
.SetRect(0, 0, 0, 0);
663 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
664 EXPECT_EQ(label
.text(), paint_text
);
665 // The text should be left aligned horizontally and centered vertically.
666 EXPECT_EQ(0, text_bounds
.x());
667 EXPECT_EQ(extra
.height() / 2 , text_bounds
.y());
668 EXPECT_EQ(required_size
.width(), text_bounds
.width());
669 EXPECT_EQ(required_size
.height(), text_bounds
.height());
670 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT
,
671 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
672 gfx::Canvas::TEXT_ALIGN_CENTER
|
673 gfx::Canvas::TEXT_ALIGN_RIGHT
));
676 // Test single line drawing with a border.
677 gfx::Insets
border(39, 34, 8, 96);
678 label
.SetBorder(Border::CreateEmptyBorder(
679 border
.top(), border
.left(), border
.bottom(), border
.right()));
681 gfx::Size
required_size_with_border(label
.GetPreferredSize());
682 EXPECT_EQ(required_size
.width() + border
.width(),
683 required_size_with_border
.width());
684 EXPECT_EQ(required_size
.height() + border
.height(),
685 required_size_with_border
.height());
686 label
.SetBounds(0, 0, required_size_with_border
.width() + extra
.width(),
687 required_size_with_border
.height() + extra
.height());
689 // Centered text with border.
690 label
.SetHorizontalAlignment(gfx::ALIGN_CENTER
);
692 text_bounds
.SetRect(0, 0, 0, 0);
693 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
694 EXPECT_EQ(label
.text(), paint_text
);
695 // The text should be centered horizontally and vertically within the border.
696 EXPECT_EQ(border
.left() + extra
.width() / 2, text_bounds
.x());
697 EXPECT_EQ(border
.top() + extra
.height() / 2 , text_bounds
.y());
698 EXPECT_EQ(required_size
.width(), text_bounds
.width());
699 EXPECT_EQ(required_size
.height(), text_bounds
.height());
700 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_CENTER
,
701 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
702 gfx::Canvas::TEXT_ALIGN_CENTER
|
703 gfx::Canvas::TEXT_ALIGN_RIGHT
));
705 // ALIGN_LEFT text with border.
706 label
.SetHorizontalAlignment(gfx::ALIGN_LEFT
);
708 text_bounds
.SetRect(0, 0, 0, 0);
709 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
710 EXPECT_EQ(label
.text(), paint_text
);
711 // The text should be right aligned horizontally and centered vertically.
712 EXPECT_EQ(border
.left() + extra
.width(), text_bounds
.x());
713 EXPECT_EQ(border
.top() + extra
.height() / 2 , text_bounds
.y());
714 EXPECT_EQ(required_size
.width(), text_bounds
.width());
715 EXPECT_EQ(required_size
.height(), text_bounds
.height());
716 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_RIGHT
,
717 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
718 gfx::Canvas::TEXT_ALIGN_CENTER
|
719 gfx::Canvas::TEXT_ALIGN_RIGHT
));
722 label
.SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
724 text_bounds
.SetRect(0, 0, 0, 0);
725 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
726 EXPECT_EQ(label
.text(), paint_text
);
727 // The text should be left aligned horizontally and centered vertically.
728 EXPECT_EQ(border
.left(), text_bounds
.x());
729 EXPECT_EQ(border
.top() + extra
.height() / 2 , text_bounds
.y());
730 EXPECT_EQ(required_size
.width(), text_bounds
.width());
731 EXPECT_EQ(required_size
.height(), text_bounds
.height());
732 EXPECT_EQ(gfx::Canvas::TEXT_ALIGN_LEFT
,
733 flags
& (gfx::Canvas::TEXT_ALIGN_LEFT
|
734 gfx::Canvas::TEXT_ALIGN_CENTER
|
735 gfx::Canvas::TEXT_ALIGN_RIGHT
));
738 base::i18n::SetICUDefaultLocale(locale
);
741 // On Linux the underlying pango routines require a max height in order to
742 // ellide multiline text. So until that can be resolved, we set all
743 // multiline lables to not ellide in Linux only.
744 TEST(LabelTest
, DrawMultiLineStringInRTL
) {
746 label
.SetFocusable(false);
749 std::string locale
= l10n_util::GetApplicationLocale("");
750 base::i18n::SetICUDefaultLocale("he");
752 label
.SetText(ASCIIToUTF16("Another string\nwith returns\n\n!"));
753 label
.SetMultiLine(true);
755 gfx::Size
extra(50, 10);
756 label
.SetBounds(label
.x(), label
.y(),
757 label
.width() + extra
.width(),
758 label
.height() + extra
.height());
760 // Do some basic verifications for all three alignments.
761 base::string16 paint_text
;
762 gfx::Rect text_bounds
;
764 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
765 EXPECT_EQ(label
.text(), paint_text
);
766 EXPECT_EQ(extra
.width() / 2, text_bounds
.x());
767 EXPECT_EQ(extra
.height() / 2, text_bounds
.y());
768 EXPECT_GT(text_bounds
.width(), kMinTextDimension
);
769 EXPECT_GT(text_bounds
.height(), kMinTextDimension
);
770 EXPECT_TRUE(gfx::Canvas::MULTI_LINE
& flags
);
771 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_CENTER
& flags
);
773 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS
& flags
);
775 gfx::Rect
center_bounds(text_bounds
);
777 label
.SetHorizontalAlignment(gfx::ALIGN_LEFT
);
779 text_bounds
.SetRect(0, 0, 0, 0);
780 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
781 EXPECT_EQ(label
.text(), paint_text
);
782 EXPECT_EQ(extra
.width(), text_bounds
.x());
783 EXPECT_EQ(extra
.height() / 2, text_bounds
.y());
784 EXPECT_GT(text_bounds
.width(), kMinTextDimension
);
785 EXPECT_GT(text_bounds
.height(), kMinTextDimension
);
786 EXPECT_TRUE(gfx::Canvas::MULTI_LINE
& flags
);
787 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_RIGHT
& flags
);
789 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS
& flags
);
792 label
.SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
794 text_bounds
.SetRect(0, 0, 0, 0);
795 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
796 EXPECT_EQ(label
.text(), paint_text
);
797 EXPECT_EQ(0, text_bounds
.x());
798 EXPECT_EQ(extra
.height() / 2, text_bounds
.y());
799 EXPECT_GT(text_bounds
.width(), kMinTextDimension
);
800 EXPECT_GT(text_bounds
.height(), kMinTextDimension
);
801 EXPECT_TRUE(gfx::Canvas::MULTI_LINE
& flags
);
802 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_LEFT
& flags
);
804 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS
& flags
);
807 // Test multiline drawing with a border.
808 gfx::Insets
border(19, 92, 23, 2);
809 label
.SetBorder(Border::CreateEmptyBorder(
810 border
.top(), border
.left(), border
.bottom(), border
.right()));
812 label
.SetBounds(label
.x(), label
.y(),
813 label
.width() + extra
.width(),
814 label
.height() + extra
.height());
816 label
.SetHorizontalAlignment(gfx::ALIGN_CENTER
);
818 text_bounds
.SetRect(0, 0, 0, 0);
819 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
820 EXPECT_EQ(label
.text(), paint_text
);
821 EXPECT_EQ(border
.left() + extra
.width() / 2, text_bounds
.x());
822 EXPECT_EQ(border
.top() + extra
.height() / 2, text_bounds
.y());
823 EXPECT_EQ(center_bounds
.width(), text_bounds
.width());
824 EXPECT_EQ(center_bounds
.height(), text_bounds
.height());
825 EXPECT_TRUE(gfx::Canvas::MULTI_LINE
& flags
);
826 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_CENTER
& flags
);
828 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS
& flags
);
831 label
.SetHorizontalAlignment(gfx::ALIGN_LEFT
);
833 text_bounds
.SetRect(0, 0, 0, 0);
834 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
835 EXPECT_EQ(label
.text(), paint_text
);
836 EXPECT_EQ(border
.left() + extra
.width(), text_bounds
.x());
837 EXPECT_EQ(border
.top() + extra
.height() / 2, text_bounds
.y());
838 EXPECT_EQ(center_bounds
.width(), text_bounds
.width());
839 EXPECT_EQ(center_bounds
.height(), text_bounds
.height());
840 EXPECT_TRUE(gfx::Canvas::MULTI_LINE
& flags
);
841 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_RIGHT
& flags
);
843 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS
& flags
);
846 label
.SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
848 text_bounds
.SetRect(0, 0, 0, 0);
849 label
.CalculateDrawStringParams(&paint_text
, &text_bounds
, &flags
);
850 EXPECT_EQ(label
.text(), paint_text
);
851 EXPECT_EQ(border
.left(), text_bounds
.x());
852 EXPECT_EQ(border
.top() + extra
.height() / 2, text_bounds
.y());
853 EXPECT_EQ(center_bounds
.width(), text_bounds
.width());
854 EXPECT_EQ(center_bounds
.height(), text_bounds
.height());
855 EXPECT_TRUE(gfx::Canvas::MULTI_LINE
& flags
);
856 EXPECT_TRUE(gfx::Canvas::TEXT_ALIGN_LEFT
& flags
);
858 EXPECT_TRUE(gfx::Canvas::NO_ELLIPSIS
& flags
);
862 base::i18n::SetICUDefaultLocale(locale
);
865 // Ensure the subpixel rendering flag and background color alpha are respected.
866 TEST(LabelTest
, DisableSubpixelRendering
) {
868 label
.SetBackgroundColor(SK_ColorWHITE
);
869 const int flag
= gfx::Canvas::NO_SUBPIXEL_RENDERING
;
870 EXPECT_EQ(0, label
.ComputeDrawStringFlags() & flag
);
871 label
.set_subpixel_rendering_enabled(false);
872 EXPECT_EQ(flag
, label
.ComputeDrawStringFlags() & flag
);
873 label
.set_subpixel_rendering_enabled(true);
874 EXPECT_EQ(0, label
.ComputeDrawStringFlags() & flag
);
875 // Text cannot be drawn with subpixel rendering on transparent backgrounds.
876 label
.SetBackgroundColor(SkColorSetARGB(64, 255, 255, 255));
877 EXPECT_EQ(flag
, label
.ComputeDrawStringFlags() & flag
);
880 // Check that labels support GetTooltipHandlerForPoint.
881 TEST(LabelTest
, GetTooltipHandlerForPoint
) {
884 ASCIIToUTF16("A string that's long enough to exceed the bounds"));
885 label
.SetBounds(0, 0, 10, 10);
886 // There's a default tooltip if the text is too big to fit.
887 EXPECT_EQ(&label
, label
.GetTooltipHandlerForPoint(gfx::Point(2, 2)));
889 // If there's no default tooltip, this should return NULL.
890 label
.SetBounds(0, 0, 500, 50);
891 EXPECT_FALSE(label
.GetTooltipHandlerForPoint(gfx::Point(2, 2)));
893 label
.SetTooltipText(ASCIIToUTF16("a tooltip"));
894 // If the point hits the label, and tooltip is set, the label should be
895 // returned as its tooltip handler.
896 EXPECT_EQ(&label
, label
.GetTooltipHandlerForPoint(gfx::Point(2, 2)));
898 // Additionally, GetTooltipHandlerForPoint should verify that the label
899 // actually contains the point.
900 EXPECT_FALSE(label
.GetTooltipHandlerForPoint(gfx::Point(2, 51)));
901 EXPECT_FALSE(label
.GetTooltipHandlerForPoint(gfx::Point(-1, 20)));
903 // GetTooltipHandlerForPoint works should work in child bounds.
904 label
.SetBounds(2, 2, 10, 10);
905 EXPECT_EQ(&label
, label
.GetTooltipHandlerForPoint(gfx::Point(1, 5)));
906 EXPECT_FALSE(label
.GetTooltipHandlerForPoint(gfx::Point(3, 11)));