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

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

Issue 12543032: Add views::RichLabel, a class which creates multi-line text with mixed (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add tests Created 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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.
4
5 #include <string>
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/views/controls/link.h"
11 #include "ui/views/controls/styled_label.h"
12 #include "ui/views/controls/styled_label_listener.h"
13
14 namespace views {
15
16 class StyledLabelTest : public testing::Test, public StyledLabelListener {
17 public:
18 StyledLabelTest() {}
19 virtual ~StyledLabelTest() {}
20
21 // StyledLabelListener implementation.
22 virtual void StyledLabelLinkClicked(const ui::Range& range,
23 int event_flags) OVERRIDE {}
24
25 protected:
26 StyledLabel* styled() { return styled_.get(); }
27
28 void InitStyledLabel(const std::string& ascii_text) {
29 styled_.reset(new StyledLabel(ASCIIToUTF16(ascii_text), this));
30 }
31
32 int StyledLabelContentHeightForWidth(int w) {
33 return styled_->GetHeightForWidth(w) - styled_->GetInsets().height();
34 }
35
36 private:
37 scoped_ptr<StyledLabel> styled_;
38 };
sky 2013/03/14 23:08:01 DISALLOW...
Evan Stade 2013/03/15 02:42:27 I could swear that didn't work last time I tried t
39
40 TEST_F(StyledLabelTest, NoWrapping) {
41 std::string text("This is a test block of text");
msw 2013/03/14 23:24:30 nit: const
Evan Stade 2013/03/15 02:42:27 Done.
42 InitStyledLabel(text);
43 Label label(ASCIIToUTF16(text));
44 gfx::Size label_preferred_size = label.GetPreferredSize();
msw 2013/03/14 23:24:30 nit: const
Evan Stade 2013/03/15 02:42:27 Done.
45 EXPECT_EQ(label_preferred_size.height(),
46 StyledLabelContentHeightForWidth(label_preferred_size.width() * 2));
47 }
48
49 TEST_F(StyledLabelTest, BasicWrapping) {
50 std::string text("This is a test block of text");
msw 2013/03/14 23:24:30 nit: const
Evan Stade 2013/03/15 02:42:27 Done.
51 InitStyledLabel(text);
52 Label label(ASCIIToUTF16(text.substr(0, text.size() * 2 / 3)));
53 gfx::Size label_preferred_size = label.GetPreferredSize();
54 EXPECT_EQ(label_preferred_size.height() * 2,
55 StyledLabelContentHeightForWidth(label_preferred_size.width()));
56
57 // Also respect the border.
58 styled()->set_border(Border::CreateEmptyBorder(3, 3, 3, 3));
59 styled()->SetBounds(
60 0,
61 0,
62 styled()->GetInsets().width() + label_preferred_size.width(),
63 styled()->GetInsets().height() + 2 * label_preferred_size.height());
64 styled()->Layout();
65 ASSERT_EQ(2, styled()->child_count());
66 EXPECT_EQ(3, styled()->child_at(0)->bounds().x());
msw 2013/03/14 23:24:30 optional nit: compare to styled()->GetInsets()->wi
Evan Stade 2013/03/15 02:42:27 don't want to do that because it might pass if Sty
67 EXPECT_EQ(3, styled()->child_at(0)->bounds().y());
68 EXPECT_EQ(styled()->bounds().height() - 3,
69 styled()->child_at(1)->bounds().bottom());
70 }
71
72 TEST_F(StyledLabelTest, CreateLinks) {
73 std::string text("This is a test block of text.");
msw 2013/03/14 23:24:30 nit: const
Evan Stade 2013/03/15 02:42:27 Done.
74 InitStyledLabel(text);
75 styled()->AddLink(ui::Range(0, 1));
76 styled()->AddLink(ui::Range(1, 2));
77 styled()->AddLink(ui::Range(10, 11));
78 styled()->AddLink(ui::Range(12, 13));
79
80 styled()->SetBounds(0, 0, 1000, 1000);
81 styled()->Layout();
82 ASSERT_EQ(7, styled()->child_count());
83 }
84
85 TEST_F(StyledLabelTest, DontBreakLinks) {
86 std::string text("This is a test block of text, ");
msw 2013/03/14 23:24:30 nit: const
Evan Stade 2013/03/15 02:42:27 Done.
87 std::string link_text("and this should be a link");
msw 2013/03/14 23:24:30 nit: const
Evan Stade 2013/03/15 02:42:27 Done.
88 InitStyledLabel(text + link_text);
89 styled()->AddLink(ui::Range(text.size(), text.size() + link_text.size()));
90
91 Label label(ASCIIToUTF16(text + link_text.substr(0, link_text.size() / 2)));
92 gfx::Size label_preferred_size = label.GetPreferredSize();
93 int pref_height = styled()->GetHeightForWidth(label_preferred_size.width());
94 EXPECT_EQ(label_preferred_size.height() * 2,
95 pref_height - styled()->GetInsets().height());
96
97 styled()->SetBounds(0, 0, label_preferred_size.width(), pref_height);
98 styled()->Layout();
99 ASSERT_EQ(2, styled()->child_count());
100 EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
101 EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
102 }
103
104 TEST_F(StyledLabelTest, HandleEmptyLayout) {
105 std::string text("This is a test block of text, ");
msw 2013/03/14 23:24:30 nit: const
Evan Stade 2013/03/15 02:42:27 Done.
106 InitStyledLabel(text);
107 styled()->Layout();
108 ASSERT_EQ(0, styled()->child_count());
109 }
110
111 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698