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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "ui/accessibility/ax_view_state.h" | 13 #include "ui/accessibility/ax_view_state.h" |
14 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
15 #include "ui/compositor/canvas_painter.h" | 15 #include "ui/compositor/canvas_painter.h" |
16 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
17 #include "ui/views/border.h" | 17 #include "ui/views/border.h" |
18 #include "ui/views/controls/link.h" | |
18 #include "ui/views/test/focus_manager_test.h" | 19 #include "ui/views/test/focus_manager_test.h" |
19 #include "ui/views/test/views_test_base.h" | 20 #include "ui/views/test/views_test_base.h" |
20 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
21 | 22 |
22 using base::ASCIIToUTF16; | 23 using base::ASCIIToUTF16; |
23 | 24 |
24 namespace views { | 25 namespace views { |
25 | 26 |
26 typedef ViewsTestBase LabelTest; | 27 typedef ViewsTestBase LabelTest; |
27 | 28 |
28 class LabelFocusTest : public FocusManagerTest { | 29 class LabelFocusTest : public FocusManagerTest { |
29 public: | 30 public: |
30 LabelFocusTest() {} | 31 LabelFocusTest() {} |
31 ~LabelFocusTest() override {} | 32 ~LabelFocusTest() override {} |
32 | 33 |
33 protected: | 34 protected: |
34 views::Label* label() { return label_; } | 35 views::Label* label() { return label_; } |
35 | 36 |
36 private: | 37 private: |
37 // FocusManagerTest: | 38 // FocusManagerTest: |
38 void InitContentView() override { | 39 void InitContentView() override { |
39 label_ = new views::Label(); | 40 // A link is a focusable label. |
41 label_ = new views::Link(); | |
40 GetContentsView()->AddChildView(label_); | 42 GetContentsView()->AddChildView(label_); |
41 } | 43 } |
42 | 44 |
43 views::Label* label_; | 45 views::Label* label_; |
44 }; | 46 }; |
45 | 47 |
46 // All text sizing measurements (width and height) should be greater than this. | 48 // All text sizing measurements (width and height) should be greater than this. |
47 const int kMinTextDimension = 4; | 49 const int kMinTextDimension = 4; |
48 | 50 |
49 // A test utility function to set the application default text direction. | 51 // A test utility function to set the application default text direction. |
(...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
601 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); | 603 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); |
602 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); | 604 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); |
603 | 605 |
604 // There's only one 'line', RenderText itself supports multiple lines. | 606 // There's only one 'line', RenderText itself supports multiple lines. |
605 EXPECT_EQ(1u, label.lines_.size()); | 607 EXPECT_EQ(1u, label.lines_.size()); |
606 } | 608 } |
607 #endif | 609 #endif |
608 | 610 |
609 TEST_F(LabelFocusTest, FocusBounds) { | 611 TEST_F(LabelFocusTest, FocusBounds) { |
610 label()->SetText(ASCIIToUTF16("Example")); | 612 label()->SetText(ASCIIToUTF16("Example")); |
611 gfx::Size normal_size = label()->GetPreferredSize(); | |
612 | |
613 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); | |
614 label()->RequestFocus(); | 613 label()->RequestFocus(); |
615 gfx::Size focusable_size = label()->GetPreferredSize(); | 614 gfx::Size focusable_size = label()->GetPreferredSize(); |
616 // Focusable label requires larger size to paint the focus rectangle. | |
617 EXPECT_GT(focusable_size.width(), normal_size.width()); | |
618 EXPECT_GT(focusable_size.height(), normal_size.height()); | |
619 | 615 |
620 label()->SizeToPreferredSize(); | 616 label()->SizeToPreferredSize(); |
621 gfx::Rect focus_bounds = label()->GetFocusBounds(); | 617 gfx::Rect focus_bounds = label()->GetFocusBounds(); |
622 EXPECT_EQ(label()->GetLocalBounds().ToString(), focus_bounds.ToString()); | 618 EXPECT_EQ(label()->GetLocalBounds().ToString(), focus_bounds.ToString()); |
623 | 619 |
624 label()->SetBounds( | 620 label()->SetBounds( |
625 0, 0, focusable_size.width() * 2, focusable_size.height() * 2); | 621 0, 0, focusable_size.width() * 2, focusable_size.height() * 2); |
626 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 622 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
627 focus_bounds = label()->GetFocusBounds(); | 623 focus_bounds = label()->GetFocusBounds(); |
628 EXPECT_EQ(0, focus_bounds.x()); | 624 EXPECT_EQ(0, focus_bounds.x()); |
(...skipping 10 matching lines...) Expand all Loading... | |
639 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString()); | 635 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString()); |
640 | 636 |
641 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 637 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
642 label()->SetElideBehavior(gfx::FADE_TAIL); | 638 label()->SetElideBehavior(gfx::FADE_TAIL); |
643 label()->SetBounds(0, 0, focusable_size.width() / 2, focusable_size.height()); | 639 label()->SetBounds(0, 0, focusable_size.width() / 2, focusable_size.height()); |
644 focus_bounds = label()->GetFocusBounds(); | 640 focus_bounds = label()->GetFocusBounds(); |
645 EXPECT_EQ(0, focus_bounds.x()); | 641 EXPECT_EQ(0, focus_bounds.x()); |
646 EXPECT_EQ(focusable_size.width() / 2, focus_bounds.width()); | 642 EXPECT_EQ(focusable_size.width() / 2, focus_bounds.width()); |
647 } | 643 } |
648 | 644 |
649 TEST_F(LabelFocusTest, EmptyLabel) { | |
karandeepb
2016/04/27 08:04:40
An empty link is not focusable. Hence this test ca
| |
650 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); | |
651 label()->RequestFocus(); | |
652 label()->SizeToPreferredSize(); | |
653 | |
654 gfx::Rect focus_bounds = label()->GetFocusBounds(); | |
655 EXPECT_FALSE(focus_bounds.IsEmpty()); | |
656 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); | |
657 } | |
658 | |
659 } // namespace views | 645 } // namespace views |
OLD | NEW |