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

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

Issue 2413223003: Views:: Make Labels support text selection. (Closed)
Patch Set: Cleanup. More tests. Created 4 years, 1 month 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
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 <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/clipboard/clipboard.h"
14 #include "ui/base/l10n/l10n_util.h" 15 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/compositor/canvas_painter.h" 16 #include "ui/compositor/canvas_painter.h"
17 #include "ui/events/base_event_utils.h"
16 #include "ui/gfx/canvas.h" 18 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/render_text.h" 19 #include "ui/gfx/render_text.h"
18 #include "ui/views/border.h" 20 #include "ui/views/border.h"
19 #include "ui/views/test/focus_manager_test.h" 21 #include "ui/views/test/focus_manager_test.h"
20 #include "ui/views/test/views_test_base.h" 22 #include "ui/views/test/views_test_base.h"
21 #include "ui/views/widget/widget.h" 23 #include "ui/views/widget/widget.h"
22 24
23 using base::ASCIIToUTF16; 25 using base::ASCIIToUTF16;
24 26
27 #define EXPECT_STR_EQ(ascii, utf16) EXPECT_EQ(ASCIIToUTF16(ascii), utf16)
28
25 namespace views { 29 namespace views {
30
31 class LabelTest : public ViewsTestBase {
32 public:
33 LabelTest() : label_(nullptr), widget_(nullptr) {}
34
35 // ::testing::Test:
msw 2016/11/01 23:42:22 nit: cite the more direct base class (ViewsTestBas
karandeepb 2016/11/02 06:29:38 Done.
36 void SetUp() override {
37 ViewsTestBase::SetUp();
38
39 widget_ = new Widget;
msw 2016/11/01 23:42:22 Avoid new and a raw pointer; use a plain member |W
karandeepb 2016/11/02 06:29:38 Done.
40 Widget::InitParams params =
41 CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
42 params.bounds = gfx::Rect(200, 200, 200, 200);
msw 2016/11/01 23:42:22 q: are the bounds important or should they just be
karandeepb 2016/11/02 06:29:38 Done.
43 widget_->Init(params);
44 View* container = new View();
45 widget_->SetContentsView(container);
46
47 label_ = new Label();
48 container->AddChildView(label_);
49
50 widget_->Show();
51 }
52
53 void TearDown() override {
54 if (widget_)
msw 2016/11/01 23:42:22 I don't see when widget_ would be null here; remov
karandeepb 2016/11/02 06:29:38 Done.
55 widget_->Close();
msw 2016/11/01 23:42:22 nit: Use WIDGET_OWNS_NATIVE_WIDGET and skip this?
karandeepb 2016/11/02 06:29:38 Using WIDGET_OWNS_NATIVE_WIDGET since we are manag
56
57 ViewsTestBase::TearDown();
58 }
59
60 protected:
61 Label* label() { return label_; }
62
63 Widget* widget() { return widget_; }
64
65 View* GetFocusedView() {
66 return widget()->GetFocusManager()->GetFocusedView();
msw 2016/11/01 23:42:22 optional nit: inline this.
karandeepb 2016/11/02 06:29:38 Keeping this. This is used multiple times.
67 }
68
69 void PerformMousePress(const gfx::Point& point, int extra_flags = 0) {
msw 2016/11/01 23:42:22 I wonder if it makes sense to use EventGenerator h
karandeepb 2016/11/02 06:29:38 This is easier to set up (Don't have to think abou
70 ui::MouseEvent pressed_event = ui::MouseEvent(
71 ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(),
72 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON | extra_flags);
73 label()->OnMousePressed(pressed_event);
74 }
75
76 void PerformMouseRelease(const gfx::Point& point) {
77 ui::MouseEvent released_event = ui::MouseEvent(
78 ui::ET_MOUSE_RELEASED, point, point, ui::EventTimeForNow(),
79 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
80 label()->OnMouseReleased(released_event);
81 }
82
83 void PerformClick(const gfx::Point& point, int extra_flags = 0) {
84 PerformMousePress(point, extra_flags);
85 PerformMouseRelease(point);
86 }
87
88 void DragMouseTo(const gfx::Point& point) {
msw 2016/11/01 23:42:22 nit: name PerformMouseDragTo or similar for consis
karandeepb 2016/11/02 06:29:38 Done.
89 ui::MouseEvent drag(ui::ET_MOUSE_DRAGGED, point, point,
90 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0);
91 label()->OnMouseDragged(drag);
92 }
93
94 gfx::Point GetCursorPoint(int cursor_pos) {
95 return label()
96 ->GetRenderTextForSelectionController()
97 ->GetCursorBounds(gfx::SelectionModel(cursor_pos, gfx::CURSOR_FORWARD),
98 false)
99 .origin();
100 }
101
102 private:
103 Label* label_;
msw 2016/11/01 23:42:22 nit: |= nullptr| here to avoid initializer list.
karandeepb 2016/11/02 06:29:38 Done.
104 Widget* widget_;
105
106 DISALLOW_COPY_AND_ASSIGN(LabelTest);
107 };
108
26 namespace { 109 namespace {
27 110
28 // All text sizing measurements (width and height) should be greater than this. 111 // All text sizing measurements (width and height) should be greater than this.
29 const int kMinTextDimension = 4; 112 const int kMinTextDimension = 4;
30 113
31 using LabelTest = ViewsTestBase;
32
33 class LabelFocusTest : public FocusManagerTest {
34 public:
35 LabelFocusTest() {}
36 ~LabelFocusTest() override {}
37
38 protected:
39 Label* label() { return label_; }
40
41 private:
42 // FocusManagerTest:
43 void InitContentView() override {
44 label_ = new views::Label();
45 GetContentsView()->AddChildView(label_);
46 }
47
48 Label* label_;
49 };
50
51 class TestLabel : public Label { 114 class TestLabel : public Label {
52 public: 115 public:
53 TestLabel() : Label(ASCIIToUTF16("TestLabel")) { SizeToPreferredSize(); } 116 TestLabel() : Label(ASCIIToUTF16("TestLabel")) { SizeToPreferredSize(); }
54 117
55 int schedule_paint_count() const { return schedule_paint_count_; } 118 int schedule_paint_count() const { return schedule_paint_count_; }
56 119
57 void SimulatePaint() { 120 void SimulatePaint() {
58 gfx::Canvas canvas(bounds().size(), 1.0, false /* is_opaque */); 121 gfx::Canvas canvas(bounds().size(), 1.0, false /* is_opaque */);
59 Paint(ui::CanvasPainter(&canvas, 1.f).context()); 122 Paint(ui::CanvasPainter(&canvas, 1.f).context());
60 } 123 }
(...skipping 17 matching lines...) Expand all
78 EXPECT_EQ(rtl, base::i18n::IsRTL()); 141 EXPECT_EQ(rtl, base::i18n::IsRTL());
79 } 142 }
80 143
81 // Returns true if |current| is bigger than |last|. Sets |last| to |current|. 144 // Returns true if |current| is bigger than |last|. Sets |last| to |current|.
82 bool Increased(int current, int* last) { 145 bool Increased(int current, int* last) {
83 bool increased = current > *last; 146 bool increased = current > *last;
84 *last = current; 147 *last = current;
85 return increased; 148 return increased;
86 } 149 }
87 150
151 base::string16 GetSelectionClipboardText() {
152 base::string16 selection_clipboard_text;
153 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_SELECTION,
154 &selection_clipboard_text);
155 return selection_clipboard_text;
156 }
157
88 } // namespace 158 } // namespace
89 159
90 // Crashes on Linux only. http://crbug.com/612406 160 // Crashes on Linux only. http://crbug.com/612406
91 #if defined(OS_LINUX) 161 #if defined(OS_LINUX)
92 #define MAYBE_FontPropertySymbol DISABLED_FontPropertySymbol 162 #define MAYBE_FontPropertySymbol DISABLED_FontPropertySymbol
93 #else 163 #else
94 #define MAYBE_FontPropertySymbol FontPropertySymbol 164 #define MAYBE_FontPropertySymbol FontPropertySymbol
95 #endif 165 #endif
96 TEST_F(LabelTest, MAYBE_FontPropertySymbol) { 166 TEST_F(LabelTest, MAYBE_FontPropertySymbol) {
97 Label label;
98 std::string font_name("symbol"); 167 std::string font_name("symbol");
99 gfx::Font font(font_name, 26); 168 gfx::Font font(font_name, 26);
100 label.SetFontList(gfx::FontList(font)); 169 label()->SetFontList(gfx::FontList(font));
101 gfx::Font font_used = label.font_list().GetPrimaryFont(); 170 gfx::Font font_used = label()->font_list().GetPrimaryFont();
102 EXPECT_EQ(font_name, font_used.GetFontName()); 171 EXPECT_EQ(font_name, font_used.GetFontName());
103 EXPECT_EQ(26, font_used.GetFontSize()); 172 EXPECT_EQ(26, font_used.GetFontSize());
104 } 173 }
105 174
106 TEST_F(LabelTest, FontPropertyArial) { 175 TEST_F(LabelTest, FontPropertyArial) {
107 Label label;
108 std::string font_name("arial"); 176 std::string font_name("arial");
109 gfx::Font font(font_name, 30); 177 gfx::Font font(font_name, 30);
110 label.SetFontList(gfx::FontList(font)); 178 label()->SetFontList(gfx::FontList(font));
111 gfx::Font font_used = label.font_list().GetPrimaryFont(); 179 gfx::Font font_used = label()->font_list().GetPrimaryFont();
112 EXPECT_EQ(font_name, font_used.GetFontName()); 180 EXPECT_EQ(font_name, font_used.GetFontName());
113 EXPECT_EQ(30, font_used.GetFontSize()); 181 EXPECT_EQ(30, font_used.GetFontSize());
114 } 182 }
115 183
116 TEST_F(LabelTest, TextProperty) { 184 TEST_F(LabelTest, TextProperty) {
117 Label label;
118 base::string16 test_text(ASCIIToUTF16("A random string.")); 185 base::string16 test_text(ASCIIToUTF16("A random string."));
119 label.SetText(test_text); 186 label()->SetText(test_text);
120 EXPECT_EQ(test_text, label.text()); 187 EXPECT_EQ(test_text, label()->text());
121 } 188 }
122 189
123 TEST_F(LabelTest, ColorProperty) { 190 TEST_F(LabelTest, ColorProperty) {
124 Label label;
125 SkColor color = SkColorSetARGB(20, 40, 10, 5); 191 SkColor color = SkColorSetARGB(20, 40, 10, 5);
126 label.SetAutoColorReadabilityEnabled(false); 192 label()->SetAutoColorReadabilityEnabled(false);
127 label.SetEnabledColor(color); 193 label()->SetEnabledColor(color);
128 EXPECT_EQ(color, label.enabled_color()); 194 EXPECT_EQ(color, label()->enabled_color());
129 } 195 }
130 196
131 TEST_F(LabelTest, AlignmentProperty) { 197 TEST_F(LabelTest, AlignmentProperty) {
132 const bool was_rtl = base::i18n::IsRTL(); 198 const bool was_rtl = base::i18n::IsRTL();
133 199
134 Label label;
135 for (size_t i = 0; i < 2; ++i) { 200 for (size_t i = 0; i < 2; ++i) {
136 // Toggle the application default text direction (to try each direction). 201 // Toggle the application default text direction (to try each direction).
137 SetRTL(!base::i18n::IsRTL()); 202 SetRTL(!base::i18n::IsRTL());
138 bool reverse_alignment = base::i18n::IsRTL(); 203 bool reverse_alignment = base::i18n::IsRTL();
139 204
140 // The alignment should be flipped in RTL UI. 205 // The alignment should be flipped in RTL UI.
141 label.SetHorizontalAlignment(gfx::ALIGN_RIGHT); 206 label()->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
142 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_LEFT : gfx::ALIGN_RIGHT, 207 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_LEFT : gfx::ALIGN_RIGHT,
143 label.horizontal_alignment()); 208 label()->horizontal_alignment());
144 label.SetHorizontalAlignment(gfx::ALIGN_LEFT); 209 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT);
145 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT, 210 EXPECT_EQ(reverse_alignment ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT,
146 label.horizontal_alignment()); 211 label()->horizontal_alignment());
147 label.SetHorizontalAlignment(gfx::ALIGN_CENTER); 212 label()->SetHorizontalAlignment(gfx::ALIGN_CENTER);
148 EXPECT_EQ(gfx::ALIGN_CENTER, label.horizontal_alignment()); 213 EXPECT_EQ(gfx::ALIGN_CENTER, label()->horizontal_alignment());
149 214
150 for (size_t j = 0; j < 2; ++j) { 215 for (size_t j = 0; j < 2; ++j) {
151 label.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); 216 label()->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
152 const bool rtl = j == 0; 217 const bool rtl = j == 0;
153 label.SetText(rtl ? base::WideToUTF16(L"\x5d0") : ASCIIToUTF16("A")); 218 label()->SetText(rtl ? base::WideToUTF16(L"\x5d0") : ASCIIToUTF16("A"));
154 EXPECT_EQ(gfx::ALIGN_TO_HEAD, label.horizontal_alignment()); 219 EXPECT_EQ(gfx::ALIGN_TO_HEAD, label()->horizontal_alignment());
155 } 220 }
156 } 221 }
157 222
158 EXPECT_EQ(was_rtl, base::i18n::IsRTL()); 223 EXPECT_EQ(was_rtl, base::i18n::IsRTL());
159 } 224 }
160 225
161 TEST_F(LabelTest, ElideBehavior) { 226 TEST_F(LabelTest, ElideBehavior) {
162 Label label;
163 base::string16 text(ASCIIToUTF16("This is example text.")); 227 base::string16 text(ASCIIToUTF16("This is example text."));
164 label.SetText(text); 228 label()->SetText(text);
165 EXPECT_EQ(gfx::ELIDE_TAIL, label.elide_behavior()); 229 EXPECT_EQ(gfx::ELIDE_TAIL, label()->elide_behavior());
166 gfx::Size size = label.GetPreferredSize(); 230 gfx::Size size = label()->GetPreferredSize();
167 label.SetBoundsRect(gfx::Rect(size)); 231 label()->SetBoundsRect(gfx::Rect(size));
168 EXPECT_EQ(text, label.GetDisplayTextForTesting()); 232 EXPECT_EQ(text, label()->GetDisplayTextForTesting());
169 233
170 size.set_width(size.width() / 2); 234 size.set_width(size.width() / 2);
171 label.SetBoundsRect(gfx::Rect(size)); 235 label()->SetBoundsRect(gfx::Rect(size));
172 EXPECT_GT(text.size(), label.GetDisplayTextForTesting().size()); 236 EXPECT_GT(text.size(), label()->GetDisplayTextForTesting().size());
173 237
174 label.SetElideBehavior(gfx::NO_ELIDE); 238 label()->SetElideBehavior(gfx::NO_ELIDE);
175 EXPECT_EQ(text, label.GetDisplayTextForTesting()); 239 EXPECT_EQ(text, label()->GetDisplayTextForTesting());
176 } 240 }
177 241
178 TEST_F(LabelTest, MultiLineProperty) { 242 TEST_F(LabelTest, MultiLineProperty) {
179 Label label; 243 EXPECT_FALSE(label()->multi_line());
180 EXPECT_FALSE(label.multi_line()); 244 label()->SetMultiLine(true);
181 label.SetMultiLine(true); 245 EXPECT_TRUE(label()->multi_line());
182 EXPECT_TRUE(label.multi_line()); 246 label()->SetMultiLine(false);
183 label.SetMultiLine(false); 247 EXPECT_FALSE(label()->multi_line());
184 EXPECT_FALSE(label.multi_line());
185 } 248 }
186 249
187 TEST_F(LabelTest, ObscuredProperty) { 250 TEST_F(LabelTest, ObscuredProperty) {
188 Label label;
189 base::string16 test_text(ASCIIToUTF16("Password!")); 251 base::string16 test_text(ASCIIToUTF16("Password!"));
190 label.SetText(test_text); 252 label()->SetText(test_text);
191 label.SizeToPreferredSize(); 253 label()->SizeToPreferredSize();
192 254
193 // The text should be unobscured by default. 255 // The text should be unobscured by default.
194 EXPECT_FALSE(label.obscured()); 256 EXPECT_FALSE(label()->obscured());
195 EXPECT_EQ(test_text, label.GetDisplayTextForTesting()); 257 EXPECT_EQ(test_text, label()->GetDisplayTextForTesting());
196 EXPECT_EQ(test_text, label.text()); 258 EXPECT_EQ(test_text, label()->text());
197 259
198 label.SetObscured(true); 260 label()->SetObscured(true);
199 label.SizeToPreferredSize(); 261 label()->SizeToPreferredSize();
200 EXPECT_TRUE(label.obscured()); 262 EXPECT_TRUE(label()->obscured());
201 EXPECT_EQ(base::string16(test_text.size(), 263 EXPECT_EQ(base::string16(test_text.size(),
202 gfx::RenderText::kPasswordReplacementChar), 264 gfx::RenderText::kPasswordReplacementChar),
203 label.GetDisplayTextForTesting()); 265 label()->GetDisplayTextForTesting());
204 EXPECT_EQ(test_text, label.text()); 266 EXPECT_EQ(test_text, label()->text());
205 267
206 label.SetText(test_text + test_text); 268 label()->SetText(test_text + test_text);
207 label.SizeToPreferredSize(); 269 label()->SizeToPreferredSize();
208 EXPECT_EQ(base::string16(test_text.size() * 2, 270 EXPECT_EQ(base::string16(test_text.size() * 2,
209 gfx::RenderText::kPasswordReplacementChar), 271 gfx::RenderText::kPasswordReplacementChar),
210 label.GetDisplayTextForTesting()); 272 label()->GetDisplayTextForTesting());
211 EXPECT_EQ(test_text + test_text, label.text()); 273 EXPECT_EQ(test_text + test_text, label()->text());
212 274
213 label.SetObscured(false); 275 label()->SetObscured(false);
214 label.SizeToPreferredSize(); 276 label()->SizeToPreferredSize();
215 EXPECT_FALSE(label.obscured()); 277 EXPECT_FALSE(label()->obscured());
216 EXPECT_EQ(test_text + test_text, label.GetDisplayTextForTesting()); 278 EXPECT_EQ(test_text + test_text, label()->GetDisplayTextForTesting());
217 EXPECT_EQ(test_text + test_text, label.text()); 279 EXPECT_EQ(test_text + test_text, label()->text());
218 } 280 }
219 281
220 TEST_F(LabelTest, ObscuredSurrogatePair) { 282 TEST_F(LabelTest, ObscuredSurrogatePair) {
221 // 'MUSICAL SYMBOL G CLEF': represented in UTF-16 as two characters 283 // 'MUSICAL SYMBOL G CLEF': represented in UTF-16 as two characters
222 // forming the surrogate pair 0x0001D11E. 284 // forming the surrogate pair 0x0001D11E.
223 Label label;
224 base::string16 test_text = base::UTF8ToUTF16("\xF0\x9D\x84\x9E"); 285 base::string16 test_text = base::UTF8ToUTF16("\xF0\x9D\x84\x9E");
225 label.SetText(test_text); 286 label()->SetText(test_text);
226 label.SetObscured(true); 287 label()->SetObscured(true);
227 label.SizeToPreferredSize(); 288 label()->SizeToPreferredSize();
228 EXPECT_EQ(base::string16(1, gfx::RenderText::kPasswordReplacementChar), 289 EXPECT_EQ(base::string16(1, gfx::RenderText::kPasswordReplacementChar),
229 label.GetDisplayTextForTesting()); 290 label()->GetDisplayTextForTesting());
230 EXPECT_EQ(test_text, label.text()); 291 EXPECT_EQ(test_text, label()->text());
231 } 292 }
232 293
233 // This test case verifies the label preferred size will change based on the 294 // This test case verifies the label preferred size will change based on the
234 // current layout, which may seem wrong. However many of our code base assumes 295 // current layout, which may seem wrong. However many of our code base assumes
235 // this behavior, therefore this behavior will have to be kept until the code 296 // this behavior, therefore this behavior will have to be kept until the code
236 // with this assumption is fixed. See http://crbug.com/468494 and 297 // with this assumption is fixed. See http://crbug.com/468494 and
237 // http://crbug.com/467526. 298 // http://crbug.com/467526.
238 // TODO(mukai): fix the code assuming this behavior and then fix Label 299 // TODO(mukai): fix the code assuming this behavior and then fix Label
239 // implementation, and remove this test case. 300 // implementation, and remove this test case.
240 TEST_F(LabelTest, MultilinePreferredSizeTest) { 301 TEST_F(LabelTest, MultilinePreferredSizeTest) {
241 Label label; 302 label()->SetText(ASCIIToUTF16("This is an example."));
242 label.SetText(ASCIIToUTF16("This is an example."));
243 303
244 gfx::Size single_line_size = label.GetPreferredSize(); 304 gfx::Size single_line_size = label()->GetPreferredSize();
245 305
246 label.SetMultiLine(true); 306 label()->SetMultiLine(true);
247 gfx::Size multi_line_size = label.GetPreferredSize(); 307 gfx::Size multi_line_size = label()->GetPreferredSize();
248 EXPECT_EQ(single_line_size, multi_line_size); 308 EXPECT_EQ(single_line_size, multi_line_size);
249 309
250 int new_width = multi_line_size.width() / 2; 310 int new_width = multi_line_size.width() / 2;
251 label.SetBounds(0, 0, new_width, label.GetHeightForWidth(new_width)); 311 label()->SetBounds(0, 0, new_width, label()->GetHeightForWidth(new_width));
252 gfx::Size new_size = label.GetPreferredSize(); 312 gfx::Size new_size = label()->GetPreferredSize();
253 EXPECT_GT(multi_line_size.width(), new_size.width()); 313 EXPECT_GT(multi_line_size.width(), new_size.width());
254 EXPECT_LT(multi_line_size.height(), new_size.height()); 314 EXPECT_LT(multi_line_size.height(), new_size.height());
255 } 315 }
256 316
257 TEST_F(LabelTest, TooltipProperty) { 317 TEST_F(LabelTest, TooltipProperty) {
258 Label label; 318 label()->SetText(ASCIIToUTF16("My cool string."));
259 label.SetText(ASCIIToUTF16("My cool string."));
260 319
261 // Initially, label has no bounds, its text does not fit, and therefore its 320 // Initially, label has no bounds, its text does not fit, and therefore its
262 // text should be returned as the tooltip text. 321 // text should be returned as the tooltip text.
263 base::string16 tooltip; 322 base::string16 tooltip;
264 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); 323 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip));
265 EXPECT_EQ(label.text(), tooltip); 324 EXPECT_EQ(label()->text(), tooltip);
266 325
267 // While tooltip handling is disabled, GetTooltipText() should fail. 326 // While tooltip handling is disabled, GetTooltipText() should fail.
268 label.SetHandlesTooltips(false); 327 label()->SetHandlesTooltips(false);
269 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); 328 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip));
270 label.SetHandlesTooltips(true); 329 label()->SetHandlesTooltips(true);
271 330
272 // When set, custom tooltip text should be returned instead of the label's 331 // When set, custom tooltip text should be returned instead of the label's
273 // text. 332 // text.
274 base::string16 tooltip_text(ASCIIToUTF16("The tooltip!")); 333 base::string16 tooltip_text(ASCIIToUTF16("The tooltip!"));
275 label.SetTooltipText(tooltip_text); 334 label()->SetTooltipText(tooltip_text);
276 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); 335 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip));
277 EXPECT_EQ(tooltip_text, tooltip); 336 EXPECT_EQ(tooltip_text, tooltip);
278 337
279 // While tooltip handling is disabled, GetTooltipText() should fail. 338 // While tooltip handling is disabled, GetTooltipText() should fail.
280 label.SetHandlesTooltips(false); 339 label()->SetHandlesTooltips(false);
281 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); 340 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip));
282 label.SetHandlesTooltips(true); 341 label()->SetHandlesTooltips(true);
283 342
284 // When the tooltip text is set to an empty string, the original behavior is 343 // When the tooltip text is set to an empty string, the original behavior is
285 // restored. 344 // restored.
286 label.SetTooltipText(base::string16()); 345 label()->SetTooltipText(base::string16());
287 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); 346 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip));
288 EXPECT_EQ(label.text(), tooltip); 347 EXPECT_EQ(label()->text(), tooltip);
289 348
290 // While tooltip handling is disabled, GetTooltipText() should fail. 349 // While tooltip handling is disabled, GetTooltipText() should fail.
291 label.SetHandlesTooltips(false); 350 label()->SetHandlesTooltips(false);
292 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); 351 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip));
293 label.SetHandlesTooltips(true); 352 label()->SetHandlesTooltips(true);
294 353
295 // Make the label big enough to hold the text 354 // Make the label big enough to hold the text
296 // and expect there to be no tooltip. 355 // and expect there to be no tooltip.
297 label.SetBounds(0, 0, 1000, 40); 356 label()->SetBounds(0, 0, 1000, 40);
298 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); 357 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip));
299 358
300 // Shrinking the single-line label's height shouldn't trigger a tooltip. 359 // Shrinking the single-line label's height shouldn't trigger a tooltip.
301 label.SetBounds(0, 0, 1000, label.GetPreferredSize().height() / 2); 360 label()->SetBounds(0, 0, 1000, label()->GetPreferredSize().height() / 2);
302 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); 361 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip));
303 362
304 // Verify that explicitly set tooltip text is shown, regardless of size. 363 // Verify that explicitly set tooltip text is shown, regardless of size.
305 label.SetTooltipText(tooltip_text); 364 label()->SetTooltipText(tooltip_text);
306 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); 365 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip));
307 EXPECT_EQ(tooltip_text, tooltip); 366 EXPECT_EQ(tooltip_text, tooltip);
308 // Clear out the explicitly set tooltip text. 367 // Clear out the explicitly set tooltip text.
309 label.SetTooltipText(base::string16()); 368 label()->SetTooltipText(base::string16());
310 369
311 // Shrink the bounds and the tooltip should come back. 370 // Shrink the bounds and the tooltip should come back.
312 label.SetBounds(0, 0, 10, 10); 371 label()->SetBounds(0, 0, 10, 10);
313 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); 372 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip));
314 373
315 // Make the label obscured and there is no tooltip. 374 // Make the label obscured and there is no tooltip.
316 label.SetObscured(true); 375 label()->SetObscured(true);
317 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); 376 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip));
318 377
319 // Obscuring the text shouldn't permanently clobber the tooltip. 378 // Obscuring the text shouldn't permanently clobber the tooltip.
320 label.SetObscured(false); 379 label()->SetObscured(false);
321 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); 380 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip));
322 381
323 // Making the label multiline shouldn't eliminate the tooltip. 382 // Making the label multiline shouldn't eliminate the tooltip.
324 label.SetMultiLine(true); 383 label()->SetMultiLine(true);
325 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); 384 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip));
326 // Expanding the multiline label bounds should eliminate the tooltip. 385 // Expanding the multiline label bounds should eliminate the tooltip.
327 label.SetBounds(0, 0, 1000, 1000); 386 label()->SetBounds(0, 0, 1000, 1000);
328 EXPECT_FALSE(label.GetTooltipText(gfx::Point(), &tooltip)); 387 EXPECT_FALSE(label()->GetTooltipText(gfx::Point(), &tooltip));
329 388
330 // Verify that setting the tooltip still shows it. 389 // Verify that setting the tooltip still shows it.
331 label.SetTooltipText(tooltip_text); 390 label()->SetTooltipText(tooltip_text);
332 EXPECT_TRUE(label.GetTooltipText(gfx::Point(), &tooltip)); 391 EXPECT_TRUE(label()->GetTooltipText(gfx::Point(), &tooltip));
333 EXPECT_EQ(tooltip_text, tooltip); 392 EXPECT_EQ(tooltip_text, tooltip);
334 // Clear out the tooltip. 393 // Clear out the tooltip.
335 label.SetTooltipText(base::string16()); 394 label()->SetTooltipText(base::string16());
336 } 395 }
337 396
338 TEST_F(LabelTest, Accessibility) { 397 TEST_F(LabelTest, Accessibility) {
339 Label label; 398 label()->SetText(ASCIIToUTF16("My special text."));
340 label.SetText(ASCIIToUTF16("My special text."));
341 399
342 ui::AXViewState state; 400 ui::AXViewState state;
343 label.GetAccessibleState(&state); 401 label()->GetAccessibleState(&state);
344 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT, state.role); 402 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT, state.role);
345 EXPECT_EQ(label.text(), state.name); 403 EXPECT_EQ(label()->text(), state.name);
346 EXPECT_TRUE(state.HasStateFlag(ui::AX_STATE_READ_ONLY)); 404 EXPECT_TRUE(state.HasStateFlag(ui::AX_STATE_READ_ONLY));
347 } 405 }
348 406
349 TEST_F(LabelTest, TextChangeWithoutLayout) { 407 TEST_F(LabelTest, TextChangeWithoutLayout) {
350 Label label; 408 label()->SetText(ASCIIToUTF16("Example"));
351 label.SetText(ASCIIToUTF16("Example")); 409 label()->SetBounds(0, 0, 200, 200);
352 label.SetBounds(0, 0, 200, 200);
353 410
354 gfx::Canvas canvas(gfx::Size(200, 200), 1.0f, true); 411 gfx::Canvas canvas(gfx::Size(200, 200), 1.0f, true);
355 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); 412 label()->OnPaint(&canvas);
356 EXPECT_EQ(1u, label.lines_.size()); 413 EXPECT_EQ(1u, label()->lines_.size());
357 EXPECT_EQ(ASCIIToUTF16("Example"), label.lines_[0]->GetDisplayText()); 414 EXPECT_EQ(ASCIIToUTF16("Example"), label()->lines_[0]->GetDisplayText());
358 415
359 label.SetText(ASCIIToUTF16("Altered")); 416 label()->SetText(ASCIIToUTF16("Altered"));
360 // The altered text should be painted even though Layout() or SetBounds() are 417 // The altered text should be painted even though Layout() or SetBounds() are
361 // not called. 418 // not called.
362 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); 419 label()->OnPaint(&canvas);
363 EXPECT_EQ(1u, label.lines_.size()); 420 EXPECT_EQ(1u, label()->lines_.size());
364 EXPECT_EQ(ASCIIToUTF16("Altered"), label.lines_[0]->GetDisplayText()); 421 EXPECT_EQ(ASCIIToUTF16("Altered"), label()->lines_[0]->GetDisplayText());
365 } 422 }
366 423
367 TEST_F(LabelTest, EmptyLabelSizing) { 424 TEST_F(LabelTest, EmptyLabelSizing) {
368 Label label; 425 const gfx::Size expected_size(0, label()->font_list().GetHeight());
369 const gfx::Size expected_size(0, label.font_list().GetHeight()); 426 EXPECT_EQ(expected_size, label()->GetPreferredSize());
370 EXPECT_EQ(expected_size, label.GetPreferredSize()); 427 label()->SetMultiLine(!label()->multi_line());
371 label.SetMultiLine(!label.multi_line()); 428 EXPECT_EQ(expected_size, label()->GetPreferredSize());
372 EXPECT_EQ(expected_size, label.GetPreferredSize());
373 } 429 }
374 430
375 TEST_F(LabelTest, SingleLineSizing) { 431 TEST_F(LabelTest, SingleLineSizing) {
376 Label label; 432 label()->SetText(ASCIIToUTF16("A not so random string in one line."));
377 label.SetText(ASCIIToUTF16("A not so random string in one line.")); 433 const gfx::Size size = label()->GetPreferredSize();
378 const gfx::Size size = label.GetPreferredSize();
379 EXPECT_GT(size.height(), kMinTextDimension); 434 EXPECT_GT(size.height(), kMinTextDimension);
380 EXPECT_GT(size.width(), kMinTextDimension); 435 EXPECT_GT(size.width(), kMinTextDimension);
381 436
382 // Setting a size smaller than preferred should not change the preferred size. 437 // Setting a size smaller than preferred should not change the preferred size.
383 label.SetSize(gfx::Size(size.width() / 2, size.height() / 2)); 438 label()->SetSize(gfx::Size(size.width() / 2, size.height() / 2));
384 EXPECT_EQ(size, label.GetPreferredSize()); 439 EXPECT_EQ(size, label()->GetPreferredSize());
385 440
386 const gfx::Insets border(10, 20, 30, 40); 441 const gfx::Insets border(10, 20, 30, 40);
387 label.SetBorder(Border::CreateEmptyBorder( 442 label()->SetBorder(Border::CreateEmptyBorder(
388 border.top(), border.left(), border.bottom(), border.right())); 443 border.top(), border.left(), border.bottom(), border.right()));
389 const gfx::Size size_with_border = label.GetPreferredSize(); 444 const gfx::Size size_with_border = label()->GetPreferredSize();
390 EXPECT_EQ(size_with_border.height(), size.height() + border.height()); 445 EXPECT_EQ(size_with_border.height(), size.height() + border.height());
391 EXPECT_EQ(size_with_border.width(), size.width() + border.width()); 446 EXPECT_EQ(size_with_border.width(), size.width() + border.width());
392 EXPECT_EQ(size.height() + border.height(), 447 EXPECT_EQ(size.height() + border.height(),
393 label.GetHeightForWidth(size_with_border.width())); 448 label()->GetHeightForWidth(size_with_border.width()));
394 } 449 }
395 450
396 TEST_F(LabelTest, MultilineSmallAvailableWidthSizing) { 451 TEST_F(LabelTest, MultilineSmallAvailableWidthSizing) {
397 Label label; 452 label()->SetMultiLine(true);
398 label.SetMultiLine(true); 453 label()->SetAllowCharacterBreak(true);
399 label.SetAllowCharacterBreak(true); 454 label()->SetText(ASCIIToUTF16("Too Wide."));
400 label.SetText(ASCIIToUTF16("Too Wide."));
401 455
402 // Check that Label can be laid out at a variety of small sizes, 456 // Check that Label can be laid out at a variety of small sizes,
403 // splitting the words into up to one character per line if necessary. 457 // splitting the words into up to one character per line if necessary.
404 // Incorrect word splitting may cause infinite loops in text layout. 458 // Incorrect word splitting may cause infinite loops in text layout.
405 gfx::Size required_size = label.GetPreferredSize(); 459 gfx::Size required_size = label()->GetPreferredSize();
406 for (int i = 1; i < required_size.width(); ++i) 460 for (int i = 1; i < required_size.width(); ++i)
407 EXPECT_GT(label.GetHeightForWidth(i), 0); 461 EXPECT_GT(label()->GetHeightForWidth(i), 0);
408 } 462 }
409 463
410 // Verifies if SetAllowCharacterBreak(true) doesn't change the preferred size. 464 // Verifies if SetAllowCharacterBreak(true) doesn't change the preferred size.
411 // See crbug.com/469559 465 // See crbug.com/469559
412 TEST_F(LabelTest, PreferredSizeForAllowCharacterBreak) { 466 TEST_F(LabelTest, PreferredSizeForAllowCharacterBreak) {
413 Label label(base::ASCIIToUTF16("Example")); 467 label()->SetText(base::ASCIIToUTF16("Example"));
414 gfx::Size preferred_size = label.GetPreferredSize(); 468 gfx::Size preferred_size = label()->GetPreferredSize();
415 469
416 label.SetMultiLine(true); 470 label()->SetMultiLine(true);
417 label.SetAllowCharacterBreak(true); 471 label()->SetAllowCharacterBreak(true);
418 EXPECT_EQ(preferred_size, label.GetPreferredSize()); 472 EXPECT_EQ(preferred_size, label()->GetPreferredSize());
419 } 473 }
420 474
421 TEST_F(LabelTest, MultiLineSizing) { 475 TEST_F(LabelTest, MultiLineSizing) {
422 Label label; 476 label()->SetText(
423 label.SetText(
424 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!")); 477 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!"));
425 label.SetMultiLine(true); 478 label()->SetMultiLine(true);
426 479
427 // GetPreferredSize 480 // GetPreferredSize
428 gfx::Size required_size = label.GetPreferredSize(); 481 gfx::Size required_size = label()->GetPreferredSize();
429 EXPECT_GT(required_size.height(), kMinTextDimension); 482 EXPECT_GT(required_size.height(), kMinTextDimension);
430 EXPECT_GT(required_size.width(), kMinTextDimension); 483 EXPECT_GT(required_size.width(), kMinTextDimension);
431 484
432 // SizeToFit with unlimited width. 485 // SizeToFit with unlimited width.
433 label.SizeToFit(0); 486 label()->SizeToFit(0);
434 int required_width = label.GetLocalBounds().width(); 487 int required_width = label()->GetLocalBounds().width();
435 EXPECT_GT(required_width, kMinTextDimension); 488 EXPECT_GT(required_width, kMinTextDimension);
436 489
437 // SizeToFit with limited width. 490 // SizeToFit with limited width.
438 label.SizeToFit(required_width - 1); 491 label()->SizeToFit(required_width - 1);
439 int constrained_width = label.GetLocalBounds().width(); 492 int constrained_width = label()->GetLocalBounds().width();
440 #if defined(OS_WIN) 493 #if defined(OS_WIN)
441 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc) 494 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc)
442 // has to be fixed to return the size that fits to given width/height. 495 // has to be fixed to return the size that fits to given width/height.
443 EXPECT_LT(constrained_width, required_width); 496 EXPECT_LT(constrained_width, required_width);
444 #endif 497 #endif
445 EXPECT_GT(constrained_width, kMinTextDimension); 498 EXPECT_GT(constrained_width, kMinTextDimension);
446 499
447 // Change the width back to the desire width. 500 // Change the width back to the desire width.
448 label.SizeToFit(required_width); 501 label()->SizeToFit(required_width);
449 EXPECT_EQ(required_width, label.GetLocalBounds().width()); 502 EXPECT_EQ(required_width, label()->GetLocalBounds().width());
450 503
451 // General tests for GetHeightForWidth. 504 // General tests for GetHeightForWidth.
452 int required_height = label.GetHeightForWidth(required_width); 505 int required_height = label()->GetHeightForWidth(required_width);
453 EXPECT_GT(required_height, kMinTextDimension); 506 EXPECT_GT(required_height, kMinTextDimension);
454 int height_for_constrained_width = label.GetHeightForWidth(constrained_width); 507 int height_for_constrained_width =
508 label()->GetHeightForWidth(constrained_width);
455 #if defined(OS_WIN) 509 #if defined(OS_WIN)
456 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc) 510 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc)
457 // has to be fixed to return the size that fits to given width/height. 511 // has to be fixed to return the size that fits to given width/height.
458 EXPECT_GT(height_for_constrained_width, required_height); 512 EXPECT_GT(height_for_constrained_width, required_height);
459 #endif 513 #endif
460 // Using the constrained width or the required_width - 1 should give the 514 // Using the constrained width or the required_width - 1 should give the
461 // same result for the height because the constrainted width is the tight 515 // same result for the height because the constrainted width is the tight
462 // width when given "required_width - 1" as the max width. 516 // width when given "required_width - 1" as the max width.
463 EXPECT_EQ(height_for_constrained_width, 517 EXPECT_EQ(height_for_constrained_width,
464 label.GetHeightForWidth(required_width - 1)); 518 label()->GetHeightForWidth(required_width - 1));
465 519
466 // Test everything with borders. 520 // Test everything with borders.
467 gfx::Insets border(10, 20, 30, 40); 521 gfx::Insets border(10, 20, 30, 40);
468 label.SetBorder(Border::CreateEmptyBorder( 522 label()->SetBorder(Border::CreateEmptyBorder(
469 border.top(), border.left(), border.bottom(), border.right())); 523 border.top(), border.left(), border.bottom(), border.right()));
470 524
471 // SizeToFit and borders. 525 // SizeToFit and borders.
472 label.SizeToFit(0); 526 label()->SizeToFit(0);
473 int required_width_with_border = label.GetLocalBounds().width(); 527 int required_width_with_border = label()->GetLocalBounds().width();
474 EXPECT_EQ(required_width_with_border, required_width + border.width()); 528 EXPECT_EQ(required_width_with_border, required_width + border.width());
475 529
476 // GetHeightForWidth and borders. 530 // GetHeightForWidth and borders.
477 int required_height_with_border = 531 int required_height_with_border =
478 label.GetHeightForWidth(required_width_with_border); 532 label()->GetHeightForWidth(required_width_with_border);
479 EXPECT_EQ(required_height_with_border, required_height + border.height()); 533 EXPECT_EQ(required_height_with_border, required_height + border.height());
480 534
481 // Test that the border width is subtracted before doing the height 535 // Test that the border width is subtracted before doing the height
482 // calculation. If it is, then the height will grow when width 536 // calculation. If it is, then the height will grow when width
483 // is shrunk. 537 // is shrunk.
484 int height1 = label.GetHeightForWidth(required_width_with_border - 1); 538 int height1 = label()->GetHeightForWidth(required_width_with_border - 1);
485 #if defined(OS_WIN) 539 #if defined(OS_WIN)
486 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc) 540 // Canvas::SizeStringInt (in ui/gfx/canvas_linux.cc)
487 // has to be fixed to return the size that fits to given width/height. 541 // has to be fixed to return the size that fits to given width/height.
488 EXPECT_GT(height1, required_height_with_border); 542 EXPECT_GT(height1, required_height_with_border);
489 #endif 543 #endif
490 EXPECT_EQ(height1, height_for_constrained_width + border.height()); 544 EXPECT_EQ(height1, height_for_constrained_width + border.height());
491 545
492 // GetPreferredSize and borders. 546 // GetPreferredSize and borders.
493 label.SetBounds(0, 0, 0, 0); 547 label()->SetBounds(0, 0, 0, 0);
494 gfx::Size required_size_with_border = label.GetPreferredSize(); 548 gfx::Size required_size_with_border = label()->GetPreferredSize();
495 EXPECT_EQ(required_size_with_border.height(), 549 EXPECT_EQ(required_size_with_border.height(),
496 required_size.height() + border.height()); 550 required_size.height() + border.height());
497 EXPECT_EQ(required_size_with_border.width(), 551 EXPECT_EQ(required_size_with_border.width(),
498 required_size.width() + border.width()); 552 required_size.width() + border.width());
499 } 553 }
500 554
501 // Verifies if the combination of text eliding and multiline doesn't cause 555 // Verifies if the combination of text eliding and multiline doesn't cause
502 // any side effects of size / layout calculation. 556 // any side effects of size / layout calculation.
503 TEST_F(LabelTest, MultiLineSizingWithElide) { 557 TEST_F(LabelTest, MultiLineSizingWithElide) {
504 const base::string16 text = 558 const base::string16 text =
505 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!"); 559 ASCIIToUTF16("A random string\nwith multiple lines\nand returns!");
506 Label label; 560 label()->SetText(text);
507 label.SetText(text); 561 label()->SetMultiLine(true);
508 label.SetMultiLine(true);
509 562
510 gfx::Size required_size = label.GetPreferredSize(); 563 gfx::Size required_size = label()->GetPreferredSize();
511 EXPECT_GT(required_size.height(), kMinTextDimension); 564 EXPECT_GT(required_size.height(), kMinTextDimension);
512 EXPECT_GT(required_size.width(), kMinTextDimension); 565 EXPECT_GT(required_size.width(), kMinTextDimension);
513 label.SetBoundsRect(gfx::Rect(required_size)); 566 label()->SetBoundsRect(gfx::Rect(required_size));
514 567
515 label.SetElideBehavior(gfx::ELIDE_TAIL); 568 label()->SetElideBehavior(gfx::ELIDE_TAIL);
516 EXPECT_EQ(required_size.ToString(), label.GetPreferredSize().ToString()); 569 EXPECT_EQ(required_size.ToString(), label()->GetPreferredSize().ToString());
517 EXPECT_EQ(text, label.GetDisplayTextForTesting()); 570 EXPECT_EQ(text, label()->GetDisplayTextForTesting());
518 571
519 label.SizeToFit(required_size.width() - 1); 572 label()->SizeToFit(required_size.width() - 1);
520 gfx::Size narrow_size = label.GetPreferredSize(); 573 gfx::Size narrow_size = label()->GetPreferredSize();
521 EXPECT_GT(required_size.width(), narrow_size.width()); 574 EXPECT_GT(required_size.width(), narrow_size.width());
522 EXPECT_LT(required_size.height(), narrow_size.height()); 575 EXPECT_LT(required_size.height(), narrow_size.height());
523 576
524 // SetBounds() doesn't change the preferred size. 577 // SetBounds() doesn't change the preferred size.
525 label.SetBounds(0, 0, narrow_size.width() - 1, narrow_size.height()); 578 label()->SetBounds(0, 0, narrow_size.width() - 1, narrow_size.height());
526 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); 579 EXPECT_EQ(narrow_size.ToString(), label()->GetPreferredSize().ToString());
527 580
528 // Paint() doesn't change the preferred size. 581 // Paint() doesn't change the preferred size.
529 gfx::Canvas canvas; 582 gfx::Canvas canvas;
530 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); 583 label()->OnPaint(&canvas);
531 EXPECT_EQ(narrow_size.ToString(), label.GetPreferredSize().ToString()); 584 EXPECT_EQ(narrow_size.ToString(), label()->GetPreferredSize().ToString());
532 } 585 }
533 586
534 // Check that labels support GetTooltipHandlerForPoint. 587 // Check that labels support GetTooltipHandlerForPoint.
535 TEST_F(LabelTest, GetTooltipHandlerForPoint) { 588 TEST_F(LabelTest, GetTooltipHandlerForPoint) {
536 // A root view must be defined for this test because the hit-testing 589 label()->SetText(
537 // behaviour used by GetTooltipHandlerForPoint() is defined by
538 // the ViewTargeter installed on the root view.
539 Widget widget;
540 Widget::InitParams init_params =
541 CreateParams(Widget::InitParams::TYPE_POPUP);
542 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
543 init_params.bounds = gfx::Rect(0, 0, 200, 200);
544 widget.Init(init_params);
545
546 Label label;
547 label.SetText(
548 ASCIIToUTF16("A string that's long enough to exceed the bounds")); 590 ASCIIToUTF16("A string that's long enough to exceed the bounds"));
549 label.SetBounds(0, 0, 10, 10); 591 label()->SetBounds(0, 0, 10, 10);
550 widget.SetContentsView(&label);
551 592
552 // By default, labels start out as tooltip handlers. 593 // By default, labels start out as tooltip handlers.
553 ASSERT_TRUE(label.handles_tooltips()); 594 ASSERT_TRUE(label()->handles_tooltips());
554 595
555 // There's a default tooltip if the text is too big to fit. 596 // There's a default tooltip if the text is too big to fit.
556 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); 597 EXPECT_EQ(label(), label()->GetTooltipHandlerForPoint(gfx::Point(2, 2)));
557 598
558 // If tooltip handling is disabled, the label should not provide a tooltip 599 // If tooltip handling is disabled, the label should not provide a tooltip
559 // handler. 600 // handler.
560 label.SetHandlesTooltips(false); 601 label()->SetHandlesTooltips(false);
561 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); 602 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 2)));
562 label.SetHandlesTooltips(true); 603 label()->SetHandlesTooltips(true);
563 604
564 // If there's no default tooltip, this should return NULL. 605 // If there's no default tooltip, this should return NULL.
565 label.SetBounds(0, 0, 500, 50); 606 label()->SetBounds(0, 0, 500, 50);
566 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); 607 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 2)));
567 608
568 label.SetTooltipText(ASCIIToUTF16("a tooltip")); 609 label()->SetTooltipText(ASCIIToUTF16("a tooltip"));
569 // If the point hits the label, and tooltip is set, the label should be 610 // If the point hits the label, and tooltip is set, the label should be
570 // returned as its tooltip handler. 611 // returned as its tooltip handler.
571 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); 612 EXPECT_EQ(label(), label()->GetTooltipHandlerForPoint(gfx::Point(2, 2)));
572 613
573 // Additionally, GetTooltipHandlerForPoint should verify that the label 614 // Additionally, GetTooltipHandlerForPoint should verify that the label
574 // actually contains the point. 615 // actually contains the point.
575 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 51))); 616 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 51)));
576 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); 617 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(-1, 20)));
577 618
578 // Again, if tooltip handling is disabled, the label should not provide a 619 // Again, if tooltip handling is disabled, the label should not provide a
579 // tooltip handler. 620 // tooltip handler.
580 label.SetHandlesTooltips(false); 621 label()->SetHandlesTooltips(false);
581 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 2))); 622 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 2)));
582 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(2, 51))); 623 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(2, 51)));
583 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(-1, 20))); 624 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(-1, 20)));
584 label.SetHandlesTooltips(true); 625 label()->SetHandlesTooltips(true);
585 626
586 // GetTooltipHandlerForPoint works should work in child bounds. 627 // GetTooltipHandlerForPoint works should work in child bounds.
587 label.SetBounds(2, 2, 10, 10); 628 label()->SetBounds(2, 2, 10, 10);
588 EXPECT_EQ(&label, label.GetTooltipHandlerForPoint(gfx::Point(1, 5))); 629 EXPECT_EQ(label(), label()->GetTooltipHandlerForPoint(gfx::Point(1, 5)));
589 EXPECT_FALSE(label.GetTooltipHandlerForPoint(gfx::Point(3, 11))); 630 EXPECT_FALSE(label()->GetTooltipHandlerForPoint(gfx::Point(3, 11)));
590 } 631 }
591 632
592 // Check that label releases its internal layout data when it's unnecessary. 633 // Check that label releases its internal layout data when it's unnecessary.
593 TEST_F(LabelTest, ResetRenderTextData) { 634 TEST_F(LabelTest, ResetRenderTextData) {
594 Label label; 635 label()->SetText(ASCIIToUTF16("Example"));
595 label.SetText(ASCIIToUTF16("Example")); 636 label()->SizeToPreferredSize();
596 label.SizeToPreferredSize(); 637 gfx::Size preferred_size = label()->GetPreferredSize();
597 gfx::Size preferred_size = label.GetPreferredSize();
598 638
599 EXPECT_NE(gfx::Size().ToString(), preferred_size.ToString()); 639 EXPECT_NE(gfx::Size().ToString(), preferred_size.ToString());
600 EXPECT_EQ(0u, label.lines_.size()); 640 EXPECT_EQ(0u, label()->lines_.size());
601 641
602 gfx::Canvas canvas(preferred_size, 1.0f, true); 642 gfx::Canvas canvas(preferred_size, 1.0f, true);
603 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); 643 label()->OnPaint(&canvas);
604 EXPECT_EQ(1u, label.lines_.size()); 644 EXPECT_EQ(1u, label()->lines_.size());
605 645
606 // Label should recreate its RenderText object when it's invisible, to release 646 // Label should recreate its RenderText object when it's invisible, to release
607 // the layout structures and data. 647 // the layout structures and data.
608 label.SetVisible(false); 648 label()->SetVisible(false);
609 EXPECT_EQ(0u, label.lines_.size()); 649 EXPECT_EQ(0u, label()->lines_.size());
610 650
611 // Querying fields or size information should not recompute the layout 651 // Querying fields or size information should not recompute the layout
612 // unnecessarily. 652 // unnecessarily.
613 EXPECT_EQ(ASCIIToUTF16("Example"), label.text()); 653 EXPECT_EQ(ASCIIToUTF16("Example"), label()->text());
614 EXPECT_EQ(0u, label.lines_.size()); 654 EXPECT_EQ(0u, label()->lines_.size());
615 655
616 EXPECT_EQ(preferred_size.ToString(), label.GetPreferredSize().ToString()); 656 EXPECT_EQ(preferred_size.ToString(), label()->GetPreferredSize().ToString());
617 EXPECT_EQ(0u, label.lines_.size()); 657 EXPECT_EQ(0u, label()->lines_.size());
618 658
619 // RenderText data should be back when it's necessary. 659 // RenderText data should be back when it's necessary.
620 label.SetVisible(true); 660 label()->SetVisible(true);
621 EXPECT_EQ(0u, label.lines_.size()); 661 EXPECT_EQ(0u, label()->lines_.size());
622 662
623 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); 663 label()->OnPaint(&canvas);
624 EXPECT_EQ(1u, label.lines_.size()); 664 EXPECT_EQ(1u, label()->lines_.size());
625 665
626 // Changing layout just resets |lines_|. It'll recover next time it's drawn. 666 // Changing layout just resets |lines_|. It'll recover next time it's drawn.
627 label.SetBounds(0, 0, 10, 10); 667 label()->SetBounds(0, 0, 10, 10);
628 EXPECT_EQ(0u, label.lines_.size()); 668 EXPECT_EQ(0u, label()->lines_.size());
629 669
630 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); 670 label()->OnPaint(&canvas);
631 EXPECT_EQ(1u, label.lines_.size()); 671 EXPECT_EQ(1u, label()->lines_.size());
632 } 672 }
633 673
634 #if !defined(OS_MACOSX) 674 #if !defined(OS_MACOSX)
635 TEST_F(LabelTest, MultilineSupportedRenderText) { 675 TEST_F(LabelTest, MultilineSupportedRenderText) {
636 std::unique_ptr<gfx::RenderText> render_text( 676 std::unique_ptr<gfx::RenderText> render_text(
637 gfx::RenderText::CreateInstance()); 677 gfx::RenderText::CreateInstance());
638 ASSERT_TRUE(render_text->MultilineSupported()); 678 ASSERT_TRUE(render_text->MultilineSupported());
639 679
640 Label label; 680 label()->SetText(ASCIIToUTF16("Example of\nmultilined label"));
641 label.SetText(ASCIIToUTF16("Example of\nmultilined label")); 681 label()->SetMultiLine(true);
642 label.SetMultiLine(true); 682 label()->SizeToPreferredSize();
643 label.SizeToPreferredSize();
644 683
645 gfx::Canvas canvas(label.GetPreferredSize(), 1.0f, true); 684 gfx::Canvas canvas(label()->GetPreferredSize(), 1.0f, true);
646 label.Paint(ui::CanvasPainter(&canvas, 1.f).context()); 685 label()->OnPaint(&canvas);
647 686
648 // There's only one 'line', RenderText itself supports multiple lines. 687 // There's only one 'line', RenderText itself supports multiple lines.
649 EXPECT_EQ(1u, label.lines_.size()); 688 EXPECT_EQ(1u, label()->lines_.size());
650 } 689 }
651 #endif 690 #endif
652 691
653 // Ensures SchedulePaint() calls are not made in OnPaint(). 692 // Ensures SchedulePaint() calls are not made in OnPaint().
654 TEST_F(LabelTest, NoSchedulePaintInOnPaint) { 693 TEST_F(LabelTest, NoSchedulePaintInOnPaint) {
655 TestLabel label; 694 TestLabel label;
656 695
657 // Initialization should schedule at least one paint, but the precise number 696 // Initialization should schedule at least one paint, but the precise number
658 // doesn't really matter. 697 // doesn't really matter.
659 int count = label.schedule_paint_count(); 698 int count = label.schedule_paint_count();
(...skipping 13 matching lines...) Expand all
673 label.SizeToPreferredSize(); 712 label.SizeToPreferredSize();
674 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); 713 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count));
675 714
676 label.SetEnabledColor(SK_ColorBLUE); 715 label.SetEnabledColor(SK_ColorBLUE);
677 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count)); 716 EXPECT_TRUE(Increased(label.schedule_paint_count(), &count));
678 717
679 label.SimulatePaint(); 718 label.SimulatePaint();
680 EXPECT_EQ(count, label.schedule_paint_count()); // Unchanged. 719 EXPECT_EQ(count, label.schedule_paint_count()); // Unchanged.
681 } 720 }
682 721
683 TEST_F(LabelFocusTest, FocusBounds) { 722 TEST_F(LabelTest, FocusBounds) {
684 label()->SetText(ASCIIToUTF16("Example")); 723 label()->SetText(ASCIIToUTF16("Example"));
685 gfx::Size normal_size = label()->GetPreferredSize(); 724 gfx::Size normal_size = label()->GetPreferredSize();
686 725
687 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); 726 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS);
688 label()->RequestFocus(); 727 label()->RequestFocus();
689 gfx::Size focusable_size = label()->GetPreferredSize(); 728 gfx::Size focusable_size = label()->GetPreferredSize();
690 // Focusable label requires larger size to paint the focus rectangle. 729 // Focusable label requires larger size to paint the focus rectangle.
691 EXPECT_GT(focusable_size.width(), normal_size.width()); 730 EXPECT_GT(focusable_size.width(), normal_size.width());
692 EXPECT_GT(focusable_size.height(), normal_size.height()); 731 EXPECT_GT(focusable_size.height(), normal_size.height());
693 732
(...skipping 19 matching lines...) Expand all
713 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString()); 752 EXPECT_EQ(focusable_size.ToString(), focus_bounds.size().ToString());
714 753
715 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT); 754 label()->SetHorizontalAlignment(gfx::ALIGN_LEFT);
716 label()->SetElideBehavior(gfx::FADE_TAIL); 755 label()->SetElideBehavior(gfx::FADE_TAIL);
717 label()->SetBounds(0, 0, focusable_size.width() / 2, focusable_size.height()); 756 label()->SetBounds(0, 0, focusable_size.width() / 2, focusable_size.height());
718 focus_bounds = label()->GetFocusBounds(); 757 focus_bounds = label()->GetFocusBounds();
719 EXPECT_EQ(0, focus_bounds.x()); 758 EXPECT_EQ(0, focus_bounds.x());
720 EXPECT_EQ(focusable_size.width() / 2, focus_bounds.width()); 759 EXPECT_EQ(focusable_size.width() / 2, focus_bounds.width());
721 } 760 }
722 761
723 TEST_F(LabelFocusTest, EmptyLabel) { 762 TEST_F(LabelTest, EmptyLabel) {
724 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS); 763 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS);
725 label()->RequestFocus(); 764 label()->RequestFocus();
726 label()->SizeToPreferredSize(); 765 label()->SizeToPreferredSize();
727 766
728 gfx::Rect focus_bounds = label()->GetFocusBounds(); 767 gfx::Rect focus_bounds = label()->GetFocusBounds();
729 EXPECT_FALSE(focus_bounds.IsEmpty()); 768 EXPECT_FALSE(focus_bounds.IsEmpty());
730 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height()); 769 EXPECT_LT(label()->font_list().GetHeight(), focus_bounds.height());
731 } 770 }
732 771
772 // Disabled on Mac since it uses RenderTextMac, which does not support text
773 // selection.
774 // Todo(karandeepb): Re-enable these once Mac uses RenderTextHarfBuzz for
msw 2016/11/01 23:42:22 ditto nit: TODO
karandeepb 2016/11/02 06:29:38 Done.
775 // Labels. Tracked in https://crbug.com/630365.
msw 2016/11/01 23:42:22 aside: careful overloading a single issue with man
karandeepb 2016/11/02 06:29:38 Yeah you are correct. Have filed a new issue block
776 #if !defined(OS_MACOSX)
777 TEST_F(LabelTest, Selectable) {
778 // By default labels don't support text selection.
msw 2016/11/01 23:42:22 grammar nit: add a comma after "By default" (match
karandeepb 2016/11/02 06:29:38 Done.
779 EXPECT_FALSE(label()->selectable());
780
781 EXPECT_TRUE(label()->SetSelectable(true));
782 EXPECT_TRUE(label()->selectable());
783
784 // Verify making a label multiline, causes the label to not support text
msw 2016/11/01 23:42:22 grammar nit: remove the comma and optionally say "
karandeepb 2016/11/02 06:29:38 Done.
785 // selection.
786 label()->SetMultiLine(true);
787 EXPECT_FALSE(label()->selectable());
788 }
789
790 // Verify labels supporting text selection, get focus on clicks.
msw 2016/11/01 23:42:22 ditto grammar nit: remove the comma and optionally
karandeepb 2016/11/02 06:29:38 Done.
791 TEST_F(LabelTest, FocusOnClick) {
792 label()->SetText(ASCIIToUTF16("text"));
793 label()->SizeToPreferredSize();
794
795 // By default, labels don't get focus on click.
796 PerformClick(gfx::Point());
797 EXPECT_NE(label(), GetFocusedView());
msw 2016/11/01 23:42:22 Be careful checking focus in a unit test; multiple
karandeepb 2016/11/02 06:29:37 There are similar unit tests at many other places.
798
799 EXPECT_TRUE(label()->SetSelectable(true));
800 PerformClick(gfx::Point());
801 EXPECT_EQ(label(), GetFocusedView());
802 }
803
804 // Verify labels supporting text selection do not get focus on tab traversal by
805 // default.
806 TEST_F(LabelTest, FocusTraversal) {
807 // Add another view before |label()|.
808 View* view = new View();
809 view->SetFocusBehavior(View::FocusBehavior::ALWAYS);
810 widget()->GetContentsView()->AddChildViewAt(view, 0);
811
812 // By default, labels are not focusable.
813 view->RequestFocus();
814 EXPECT_EQ(view, GetFocusedView());
815 widget()->GetFocusManager()->AdvanceFocus(false);
816 EXPECT_NE(label(), GetFocusedView());
817
818 // On enabling text selection, labels can get focus on clicks but not via tab
819 // traversal.
820 view->RequestFocus();
821 EXPECT_EQ(view, GetFocusedView());
822 EXPECT_TRUE(label()->SetSelectable(true));
823 widget()->GetFocusManager()->AdvanceFocus(false);
824 EXPECT_NE(label(), GetFocusedView());
825
826 // A label with FocusBehavior::ALWAYS should get focus via tab traversal.
827 view->RequestFocus();
828 EXPECT_EQ(view, GetFocusedView());
829 EXPECT_TRUE(label()->SetSelectable(false));
830 label()->SetFocusBehavior(View::FocusBehavior::ALWAYS);
831 widget()->GetFocusManager()->AdvanceFocus(false);
832 EXPECT_EQ(label(), GetFocusedView());
833 }
834
835 // Verify label text selection behavior on double and triple clicks.
836 TEST_F(LabelTest, DoubleTripleClick) {
837 label()->SetText(ASCIIToUTF16("Label double click"));
838 label()->SizeToPreferredSize();
839 EXPECT_TRUE(label()->SetSelectable(true));
840
841 PerformClick(gfx::Point());
842 EXPECT_TRUE(label()->GetSelectedText().empty());
843
844 // Double clicking should select the word under cursor.
845 PerformClick(gfx::Point(), ui::EF_IS_DOUBLE_CLICK);
846 EXPECT_STR_EQ("Label", label()->GetSelectedText());
847
848 // Triple clicking should select all the text.
849 PerformClick(gfx::Point());
850 EXPECT_EQ(label()->text(), label()->GetSelectedText());
851
852 // Clicking again should alternate to double click.
853 PerformClick(gfx::Point());
854 EXPECT_STR_EQ("Label", label()->GetSelectedText());
855
856 // Clicking at another location should clear the selection.
857 PerformClick(GetCursorPoint(8));
858 EXPECT_TRUE(label()->GetSelectedText().empty());
859 PerformClick(GetCursorPoint(8), ui::EF_IS_DOUBLE_CLICK);
860 EXPECT_STR_EQ("double", label()->GetSelectedText());
861 }
862
863 // Verify label text selection behavior on mouse drag.
864 TEST_F(LabelTest, MouseDrag) {
865 label()->SetText(ASCIIToUTF16("Label mouse drag"));
866 label()->SizeToPreferredSize();
867 EXPECT_TRUE(label()->SetSelectable(true));
868
869 PerformClick(GetCursorPoint(5));
870 DragMouseTo(gfx::Point());
msw 2016/11/01 23:42:22 It seems odd to send a drag event after release (p
karandeepb 2016/11/02 06:29:38 Yeah you are correct. Done.
871 EXPECT_STR_EQ("Label", label()->GetSelectedText());
872
873 DragMouseTo(GetCursorPoint(8));
874 EXPECT_STR_EQ(" mo", label()->GetSelectedText());
875
876 DragMouseTo(gfx::Point(200, 0));
877 EXPECT_STR_EQ(" mouse drag", label()->GetSelectedText());
878 }
879
880 // Verify the initially selected word on a double click, remains selected on
881 // mouse dragging.
882 TEST_F(LabelTest, MouseDragWord) {
883 label()->SetText(ASCIIToUTF16("Label drag word"));
884 label()->SizeToPreferredSize();
885 EXPECT_TRUE(label()->SetSelectable(true));
886
887 PerformClick(GetCursorPoint(8));
888 PerformClick(GetCursorPoint(8), ui::EF_IS_DOUBLE_CLICK);
889 EXPECT_STR_EQ("drag", label()->GetSelectedText());
890
891 DragMouseTo(gfx::Point());
892 EXPECT_STR_EQ("Label drag", label()->GetSelectedText());
893
894 DragMouseTo(gfx::Point(200, 0));
895 EXPECT_STR_EQ("drag word", label()->GetSelectedText());
896 }
897 #endif // OS_MACOSX
898
899 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
900 // Verify selection clipboard behavior on text selection. Disabled due to
901 // http://crbug.com/396477.
902 TEST_F(LabelTest, DISABLED_SelectionClipboard) {
903 label()->SetText(ASCIIToUTF16("Label selection clipboard"));
904 label()->SizeToPreferredSize();
905 EXPECT_TRUE(label()->SetSelectable(true));
906
907 // Verify programmatic modification of selection, does not modify the
908 // selection clipboard.
909 label()->SelectRange(gfx::Range(2, 5));
910 EXPECT_STR_EQ("bel", label()->GetSelectedText());
911 EXPECT_TRUE(GetSelectionClipboardText().empty());
912
913 // Verify text selection using the mouse, updates the selection clipboard.
msw 2016/11/01 23:42:22 grammar nit: remove comma
karandeepb 2016/11/02 06:29:38 Done.
914 PerformClick(GetCursorPoint(5));
915 DragMouseTo(gfx::Point());
916 PerformMouseRelease(gfx::Point());
917 EXPECT_STR_EQ("Label", label()->GetSelectedText());
918 EXPECT_STR_EQ("Label", GetSelectionClipboardText());
919 }
920 #endif
921
733 } // namespace views 922 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698