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/styled_label_unittest.cc

Issue 17756003: Colors in views::StyledLabel. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Style fix Created 7 years, 5 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/styled_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 2013 The Chromium Authors. All rights reserved. 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 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 <string> 5 #include <string>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/views/controls/link.h" 11 #include "ui/views/controls/link.h"
12 #include "ui/views/controls/styled_label.h" 12 #include "ui/views/controls/styled_label.h"
13 #include "ui/views/controls/styled_label_listener.h" 13 #include "ui/views/controls/styled_label_listener.h"
14 14
15 namespace {
16 const SkColor kBlack = SkColorSetRGB(0, 0, 0);
bcwhite 2013/07/02 14:22:13 The "SkColor.h" file has a number of standard colo
fdoray 2013/07/02 16:49:22 Done.
17 const SkColor kRed = SkColorSetRGB(255, 0, 0);
18 } // namespace
19
15 namespace views { 20 namespace views {
16 21
17 class StyledLabelTest : public testing::Test, public StyledLabelListener { 22 class StyledLabelTest : public testing::Test, public StyledLabelListener {
18 public: 23 public:
19 StyledLabelTest() {} 24 StyledLabelTest() {}
20 virtual ~StyledLabelTest() {} 25 virtual ~StyledLabelTest() {}
21 26
22 // StyledLabelListener implementation. 27 // StyledLabelListener implementation.
23 virtual void StyledLabelLinkClicked(const ui::Range& range, 28 virtual void StyledLabelLinkClicked(const ui::Range& range,
24 int event_flags) OVERRIDE {} 29 int event_flags) OVERRIDE {}
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 EXPECT_EQ(gfx::Font::NORMAL, 254 EXPECT_EQ(gfx::Font::NORMAL,
250 static_cast<Label*>(styled()->child_at(2))->font().GetStyle()); 255 static_cast<Label*>(styled()->child_at(2))->font().GetStyle());
251 256
252 // The second bold part should start on a new line. 257 // The second bold part should start on a new line.
253 EXPECT_EQ(0, styled()->child_at(0)->bounds().x()); 258 EXPECT_EQ(0, styled()->child_at(0)->bounds().x());
254 EXPECT_EQ(0, styled()->child_at(1)->bounds().x()); 259 EXPECT_EQ(0, styled()->child_at(1)->bounds().x());
255 EXPECT_EQ(styled()->child_at(1)->bounds().right() - 2, 260 EXPECT_EQ(styled()->child_at(1)->bounds().right() - 2,
256 styled()->child_at(2)->bounds().x()); 261 styled()->child_at(2)->bounds().x());
257 } 262 }
258 263
264 TEST_F(StyledLabelTest, Color) {
265 const std::string text_red("RED");
266 const std::string text_link("link");
267 const std::string text("word");
268 InitStyledLabel(text_red + text_link + text);
269
270 StyledLabel::RangeStyleInfo style_info_red;
271 style_info_red.color = kRed;
272 styled()->AddStyleRange(ui::Range(0, text_red.size()), style_info_red);
273
274 StyledLabel::RangeStyleInfo style_info_link =
275 StyledLabel::RangeStyleInfo::CreateForLink();
276 styled()->AddStyleRange(ui::Range(text_red.size(),
277 text_red.size() + text_link.size()),
278 style_info_link);
279
280 // Obtain the default text color for a label.
281 Label label(ASCIIToUTF16(text));
282 const SkColor kDefaultTextColor = label.enabled_color();
283
284 // Obtain the default text color for a link;
285 Link link(ASCIIToUTF16(text_link));
286 const SkColor kDefaultLinkColor = link.enabled_color();
287
288 styled()->SetBounds(0, 0, 1000, 1000);
289 styled()->Layout();
290
291 EXPECT_EQ(kRed,
292 static_cast<Label*>(styled()->child_at(0))->enabled_color());
293 EXPECT_EQ(kDefaultLinkColor,
294 static_cast<Label*>(styled()->child_at(1))->enabled_color());
295 EXPECT_EQ(kDefaultTextColor,
296 static_cast<Label*>(styled()->child_at(2))->enabled_color());
297 }
298
299 TEST_F(StyledLabelTest, ColorReadabilityOn) {
300 const std::string text(
301 "This is a block of text that needs color adjustment.");
302 InitStyledLabel(text);
303 styled()->SetBackgroundColor(kBlack);
304
305 // Obtain the text color if it were a pure label.
306 Label label(ASCIIToUTF16(text));
307 label.SetBackgroundColor(kBlack);
308
309 styled()->SetBounds(0, 0, 1000, 1000);
310 styled()->Layout();
311
312 EXPECT_EQ(label.enabled_color(),
313 static_cast<Label*>(styled()->child_at(0))->enabled_color());
314 }
315
316 TEST_F(StyledLabelTest, ColorReadabilityOff) {
317 const std::string text(
318 "This is a block of text that shouldn't change color.");
319 InitStyledLabel(text);
320 styled()->SetBackgroundColor(kBlack);
321 styled()->SetAutoColorReadabilityEnabled(false);
322
323 // Obtain the text color if it were a pure label.
324 Label label(ASCIIToUTF16(text));
325 label.SetBackgroundColor(kBlack);
326 label.SetAutoColorReadabilityEnabled(false);
327
328 styled()->SetBounds(0, 0, 1000, 1000);
329 styled()->Layout();
330
331 EXPECT_EQ(label.enabled_color(),
332 static_cast<Label*>(styled()->child_at(0))->enabled_color());
333 }
334
259 TEST_F(StyledLabelTest, StyledRangeWithTooltip) { 335 TEST_F(StyledLabelTest, StyledRangeWithTooltip) {
260 const std::string text("This is a test block of text, "); 336 const std::string text("This is a test block of text, ");
261 const std::string tooltip_text("this should have a tooltip,"); 337 const std::string tooltip_text("this should have a tooltip,");
262 const std::string normal_text(" this should not have a tooltip, "); 338 const std::string normal_text(" this should not have a tooltip, ");
263 const std::string link_text("and this should be a link"); 339 const std::string link_text("and this should be a link");
264 340
265 const size_t tooltip_start = text.size(); 341 const size_t tooltip_start = text.size();
266 const size_t link_start = 342 const size_t link_start =
267 text.size() + tooltip_text.size() + normal_text.size(); 343 text.size() + tooltip_text.size() + normal_text.size();
268 344
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 } 383 }
308 384
309 TEST_F(StyledLabelTest, HandleEmptyLayout) { 385 TEST_F(StyledLabelTest, HandleEmptyLayout) {
310 const std::string text("This is a test block of text, "); 386 const std::string text("This is a test block of text, ");
311 InitStyledLabel(text); 387 InitStyledLabel(text);
312 styled()->Layout(); 388 styled()->Layout();
313 ASSERT_EQ(0, styled()->child_count()); 389 ASSERT_EQ(0, styled()->child_count());
314 } 390 }
315 391
316 } // namespace 392 } // namespace
OLDNEW
« no previous file with comments | « ui/views/controls/styled_label.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698