Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: ui/views/controls/label_unittest.cc

Issue 1018463004: Fix focus rectangle for label. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: GetFocusBounds Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/label.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
13 #include "ui/views/border.h" 13 #include "ui/views/border.h"
14 #include "ui/views/test/focus_manager_test.h"
14 #include "ui/views/test/views_test_base.h" 15 #include "ui/views/test/views_test_base.h"
15 #include "ui/views/widget/widget.h" 16 #include "ui/views/widget/widget.h"
16 17
17 using base::ASCIIToUTF16; 18 using base::ASCIIToUTF16;
18 19
19 namespace views { 20 namespace views {
20 21
21 typedef ViewsTestBase LabelTest; 22 typedef ViewsTestBase LabelTest;
22 23
24 class LabelFocusTest : public FocusManagerTest {
25 public:
26 LabelFocusTest() {}
27 ~LabelFocusTest() override {}
28
29 protected:
30 views::Label* label() { return label_; }
31
32 private:
33 // FocusManagerTest:
34 void InitContentView() override {
35 label_ = new views::Label();
36 GetContentsView()->AddChildView(label_);
37 }
38
39 views::Label* label_;
40 };
41
23 // All text sizing measurements (width and height) should be greater than this. 42 // All text sizing measurements (width and height) should be greater than this.
24 const int kMinTextDimension = 4; 43 const int kMinTextDimension = 4;
25 44
26 // A test utility function to set the application default text direction. 45 // A test utility function to set the application default text direction.
27 void SetRTL(bool rtl) { 46 void SetRTL(bool rtl) {
28 // Override the current locale/direction. 47 // Override the current locale/direction.
29 base::i18n::SetICUDefaultLocale(rtl ? "he" : "en"); 48 base::i18n::SetICUDefaultLocale(rtl ? "he" : "en");
30 EXPECT_EQ(rtl, base::i18n::IsRTL()); 49 EXPECT_EQ(rtl, base::i18n::IsRTL());
31 } 50 }
32 51
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 label.SizeToPreferredSize(); 560 label.SizeToPreferredSize();
542 561
543 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); 562 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true);
544 label.Paint(&canvas, CullSet()); 563 label.Paint(&canvas, CullSet());
545 564
546 // There's only one 'line', RenderText itself supports multiple lines. 565 // There's only one 'line', RenderText itself supports multiple lines.
547 EXPECT_EQ(1u, label.lines_.size()); 566 EXPECT_EQ(1u, label.lines_.size());
548 } 567 }
549 #endif 568 #endif
550 569
570 TEST_F(LabelFocusTest, FocusBounds) {
571 label()->SetText(ASCIIToUTF16("Example"));
572 gfx::Size normal_size = label()->GetPreferredSize();
573
574 label()->SetFocusable(true);
575 label()->RequestFocus();
576 gfx::Size focusable_size = label()->GetPreferredSize();
577 // Focusable label requires larger size to paint the focus rectangle.
578 EXPECT_GT(focusable_size.width(), normal_size.width());
579 EXPECT_GT(focusable_size.height(), normal_size.height());
580
581 label()->SizeToPreferredSize();
582 gfx::Rect focus_bounds = label()->GetFocusBounds();
583 EXPECT_EQ(label()->GetLocalBounds().ToString(), focus_bounds.ToString());
584
585 label()->SetBounds(
586 0, 0, focusable_size.width() * 2, focusable_size.height() * 2);
587 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT);
588 focus_bounds = label()->GetFocusBounds();
589 EXPECT_EQ(0, focus_bounds.x());
590 EXPECT_LT(0, focus_bounds.y());
591 EXPECT_GT(label()->bounds().bottom(), focus_bounds.bottom());
592 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString());
593
594 label()->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
595 focus_bounds = label()->GetFocusBounds();
596 EXPECT_LT(0, focus_bounds.x());
597 EXPECT_EQ(label()->bounds().right(), focus_bounds.right());
598 EXPECT_LT(0, focus_bounds.y());
599 EXPECT_GT(label()->bounds().bottom(), focus_bounds.bottom());
600 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString());
601
602 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT);
603 label()->SetElideBehavior(gfx::FADE_TAIL);
604 label()->SetBounds(0, 0, focusable_size.width() / 2, focusable_size.height());
605 focus_bounds = label()->GetFocusBounds();
606 EXPECT_EQ(0, focus_bounds.x());
607 EXPECT_EQ(focusable_size.width() / 2, focus_bounds.width());
608 }
609
610 TEST_F(LabelFocusTest, EmptyLabel) {
611 label()->SetFocusable(true);
612 label()->RequestFocus();
613 label()->SizeToPreferredSize();
614
615 gfx::Rect focus_bounds = label()->GetFocusBounds();
616 EXPECT_FALSE(focus_bounds.IsEmpty());
617 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height());
618 }
619
551 } // namespace views 620 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/label.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698