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/accessibility/ax_view_state.h" | 10 #include "ui/accessibility/ax_view_state.h" |
11 #include "ui/base/l10n/l10n_util.h" | 11 #include "ui/base/l10n/l10n_util.h" |
12 #include "ui/compositor/paint_context.h" | 12 #include "ui/compositor/canvas_painter.h" |
13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
14 #include "ui/views/border.h" | 14 #include "ui/views/border.h" |
15 #include "ui/views/test/focus_manager_test.h" | 15 #include "ui/views/test/focus_manager_test.h" |
16 #include "ui/views/test/views_test_base.h" | 16 #include "ui/views/test/views_test_base.h" |
17 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
18 | 18 |
19 using base::ASCIIToUTF16; | 19 using base::ASCIIToUTF16; |
20 | 20 |
21 namespace views { | 21 namespace views { |
22 | 22 |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 EXPECT_EQ(label.text(), state.name); | 298 EXPECT_EQ(label.text(), state.name); |
299 EXPECT_TRUE(state.HasStateFlag(ui::AX_STATE_READ_ONLY)); | 299 EXPECT_TRUE(state.HasStateFlag(ui::AX_STATE_READ_ONLY)); |
300 } | 300 } |
301 | 301 |
302 TEST_F(LabelTest, TextChangeWithoutLayout) { | 302 TEST_F(LabelTest, TextChangeWithoutLayout) { |
303 Label label; | 303 Label label; |
304 label.SetText(ASCIIToUTF16("Example")); | 304 label.SetText(ASCIIToUTF16("Example")); |
305 label.SetBounds(0, 0, 200, 200); | 305 label.SetBounds(0, 0, 200, 200); |
306 | 306 |
307 gfx::Canvas canvas(gfx::Size(200, 200), 1.0f, true); | 307 gfx::Canvas canvas(gfx::Size(200, 200), 1.0f, true); |
308 label.Paint(ui::PaintContext(&canvas)); | 308 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
309 EXPECT_EQ(1u, label.lines_.size()); | 309 EXPECT_EQ(1u, label.lines_.size()); |
310 EXPECT_EQ(ASCIIToUTF16("Example"), label.lines_[0]->GetDisplayText()); | 310 EXPECT_EQ(ASCIIToUTF16("Example"), label.lines_[0]->GetDisplayText()); |
311 | 311 |
312 label.SetText(ASCIIToUTF16("Altered")); | 312 label.SetText(ASCIIToUTF16("Altered")); |
313 // The altered text should be painted even though Layout() or SetBounds() are | 313 // The altered text should be painted even though Layout() or SetBounds() are |
314 // not called. | 314 // not called. |
315 label.Paint(ui::PaintContext(&canvas)); | 315 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
316 EXPECT_EQ(1u, label.lines_.size()); | 316 EXPECT_EQ(1u, label.lines_.size()); |
317 EXPECT_EQ(ASCIIToUTF16("Altered"), label.lines_[0]->GetDisplayText()); | 317 EXPECT_EQ(ASCIIToUTF16("Altered"), label.lines_[0]->GetDisplayText()); |
318 } | 318 } |
319 | 319 |
320 TEST_F(LabelTest, EmptyLabelSizing) { | 320 TEST_F(LabelTest, EmptyLabelSizing) { |
321 Label label; | 321 Label label; |
322 const gfx::Size expected_size(0, gfx::FontList().GetHeight()); | 322 const gfx::Size expected_size(0, gfx::FontList().GetHeight()); |
323 EXPECT_EQ(expected_size, label.GetPreferredSize()); | 323 EXPECT_EQ(expected_size, label.GetPreferredSize()); |
324 label.SetMultiLine(!label.multi_line()); | 324 label.SetMultiLine(!label.multi_line()); |
325 EXPECT_EQ(expected_size, label.GetPreferredSize()); | 325 EXPECT_EQ(expected_size, label.GetPreferredSize()); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 gfx::Size narrow_size = label.GetPreferredSize(); | 475 gfx::Size narrow_size = label.GetPreferredSize(); |
476 EXPECT_GT(required_size.width(), narrow_size.width()); | 476 EXPECT_GT(required_size.width(), narrow_size.width()); |
477 EXPECT_LT(required_size.height(), narrow_size.height()); | 477 EXPECT_LT(required_size.height(), narrow_size.height()); |
478 | 478 |
479 // SetBounds() doesn't change the preferred size. | 479 // SetBounds() doesn't change the preferred size. |
480 label.SetBounds(0, 0, narrow_size.width() - 1, narrow_size.height()); | 480 label.SetBounds(0, 0, narrow_size.width() - 1, narrow_size.height()); |
481 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); | 481 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); |
482 | 482 |
483 // Paint() doesn't change the preferred size. | 483 // Paint() doesn't change the preferred size. |
484 gfx::Canvas canvas; | 484 gfx::Canvas canvas; |
485 label.Paint(ui::PaintContext(&canvas)); | 485 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
486 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); | 486 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); |
487 } | 487 } |
488 | 488 |
489 // Check that labels support GetTooltipHandlerForPoint. | 489 // Check that labels support GetTooltipHandlerForPoint. |
490 TEST_F(LabelTest, GetTooltipHandlerForPoint) { | 490 TEST_F(LabelTest, GetTooltipHandlerForPoint) { |
491 // A root view must be defined for this test because the hit-testing | 491 // A root view must be defined for this test because the hit-testing |
492 // behaviour used by GetTooltipHandlerForPoint() is defined by | 492 // behaviour used by GetTooltipHandlerForPoint() is defined by |
493 // the ViewTargeter installed on the root view. | 493 // the ViewTargeter installed on the root view. |
494 Widget widget; | 494 Widget widget; |
495 Widget::InitParams init_params = | 495 Widget::InitParams init_params = |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 TEST_F(LabelTest, ResetRenderTextData) { | 548 TEST_F(LabelTest, ResetRenderTextData) { |
549 Label label; | 549 Label label; |
550 label.SetText(ASCIIToUTF16("Example")); | 550 label.SetText(ASCIIToUTF16("Example")); |
551 label.SizeToPreferredSize(); | 551 label.SizeToPreferredSize(); |
552 gfx::Size preferred_size = label.GetPreferredSize(); | 552 gfx::Size preferred_size = label.GetPreferredSize(); |
553 | 553 |
554 EXPECT_NE(gfx::Size().ToString(), preferred_size.ToString()); | 554 EXPECT_NE(gfx::Size().ToString(), preferred_size.ToString()); |
555 EXPECT_EQ(0u, label.lines_.size()); | 555 EXPECT_EQ(0u, label.lines_.size()); |
556 | 556 |
557 gfx::Canvas canvas(preferred_size, 1.0f, true); | 557 gfx::Canvas canvas(preferred_size, 1.0f, true); |
558 label.Paint(ui::PaintContext(&canvas)); | 558 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
559 EXPECT_EQ(1u, label.lines_.size()); | 559 EXPECT_EQ(1u, label.lines_.size()); |
560 | 560 |
561 // Label should recreate its RenderText object when it's invisible, to release | 561 // Label should recreate its RenderText object when it's invisible, to release |
562 // the layout structures and data. | 562 // the layout structures and data. |
563 label.SetVisible(false); | 563 label.SetVisible(false); |
564 EXPECT_EQ(0u, label.lines_.size()); | 564 EXPECT_EQ(0u, label.lines_.size()); |
565 | 565 |
566 // Querying fields or size information should not recompute the layout | 566 // Querying fields or size information should not recompute the layout |
567 // unnecessarily. | 567 // unnecessarily. |
568 EXPECT_EQ(ASCIIToUTF16("Example"), label.text()); | 568 EXPECT_EQ(ASCIIToUTF16("Example"), label.text()); |
569 EXPECT_EQ(0u, label.lines_.size()); | 569 EXPECT_EQ(0u, label.lines_.size()); |
570 | 570 |
571 EXPECT_EQ(preferred_size.ToString(), label.GetPreferredSize().ToString()); | 571 EXPECT_EQ(preferred_size.ToString(), label.GetPreferredSize().ToString()); |
572 EXPECT_EQ(0u, label.lines_.size()); | 572 EXPECT_EQ(0u, label.lines_.size()); |
573 | 573 |
574 // RenderText data should be back when it's necessary. | 574 // RenderText data should be back when it's necessary. |
575 label.SetVisible(true); | 575 label.SetVisible(true); |
576 EXPECT_EQ(0u, label.lines_.size()); | 576 EXPECT_EQ(0u, label.lines_.size()); |
577 | 577 |
578 label.Paint(ui::PaintContext(&canvas)); | 578 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
579 EXPECT_EQ(1u, label.lines_.size()); | 579 EXPECT_EQ(1u, label.lines_.size()); |
580 | 580 |
581 // Changing layout just resets |lines_|. It'll recover next time it's drawn. | 581 // Changing layout just resets |lines_|. It'll recover next time it's drawn. |
582 label.SetBounds(0, 0, 10, 10); | 582 label.SetBounds(0, 0, 10, 10); |
583 EXPECT_EQ(0u, label.lines_.size()); | 583 EXPECT_EQ(0u, label.lines_.size()); |
584 | 584 |
585 label.Paint(ui::PaintContext(&canvas)); | 585 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
586 EXPECT_EQ(1u, label.lines_.size()); | 586 EXPECT_EQ(1u, label.lines_.size()); |
587 } | 587 } |
588 | 588 |
589 #if !defined(OS_MACOSX) | 589 #if !defined(OS_MACOSX) |
590 TEST_F(LabelTest, MultilineSupportedRenderText) { | 590 TEST_F(LabelTest, MultilineSupportedRenderText) { |
591 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance()); | 591 scoped_ptr<gfx::RenderText> render_text(gfx::RenderText::CreateInstance()); |
592 ASSERT_TRUE(render_text->MultilineSupported()); | 592 ASSERT_TRUE(render_text->MultilineSupported()); |
593 | 593 |
594 Label label; | 594 Label label; |
595 label.SetText(ASCIIToUTF16("Example of\nmultilined label")); | 595 label.SetText(ASCIIToUTF16("Example of\nmultilined label")); |
596 label.SetMultiLine(true); | 596 label.SetMultiLine(true); |
597 label.SizeToPreferredSize(); | 597 label.SizeToPreferredSize(); |
598 | 598 |
599 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); | 599 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); |
600 label.Paint(ui::PaintContext(&canvas)); | 600 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
601 | 601 |
602 // There's only one 'line', RenderText itself supports multiple lines. | 602 // There's only one 'line', RenderText itself supports multiple lines. |
603 EXPECT_EQ(1u, label.lines_.size()); | 603 EXPECT_EQ(1u, label.lines_.size()); |
604 } | 604 } |
605 #endif | 605 #endif |
606 | 606 |
607 TEST_F(LabelFocusTest, FocusBounds) { | 607 TEST_F(LabelFocusTest, FocusBounds) { |
608 label()->SetText(ASCIIToUTF16("Example")); | 608 label()->SetText(ASCIIToUTF16("Example")); |
609 gfx::Size normal_size = label()->GetPreferredSize(); | 609 gfx::Size normal_size = label()->GetPreferredSize(); |
610 | 610 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
648 label()->SetFocusable(true); | 648 label()->SetFocusable(true); |
649 label()->RequestFocus(); | 649 label()->RequestFocus(); |
650 label()->SizeToPreferredSize(); | 650 label()->SizeToPreferredSize(); |
651 | 651 |
652 gfx::Rect focus_bounds = label()->GetFocusBounds(); | 652 gfx::Rect focus_bounds = label()->GetFocusBounds(); |
653 EXPECT_FALSE(focus_bounds.IsEmpty()); | 653 EXPECT_FALSE(focus_bounds.IsEmpty()); |
654 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); | 654 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); |
655 } | 655 } |
656 | 656 |
657 } // namespace views | 657 } // namespace views |
OLD | NEW |