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

Unified Diff: components/favicon_base/fallback_icon_style_builder_unittest.cc

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding new host chrome://fallback-icon. Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: components/favicon_base/fallback_icon_style_builder_unittest.cc
diff --git a/components/favicon_base/fallback_icon_style_builder_unittest.cc b/components/favicon_base/fallback_icon_style_builder_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..dbedf2d902c7c8e18d16f8f88b3f31d19637acfb
--- /dev/null
+++ b/components/favicon_base/fallback_icon_style_builder_unittest.cc
@@ -0,0 +1,226 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/favicon_base/fallback_icon_style_builder.h"
+
+#include <string>
+
+#include "base/macros.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace favicon_base {
+namespace test {
+
+class FallbackIconStyleBuilderTest : public testing::Test {
+ public:
+ FallbackIconStyleBuilderTest() {
+ }
+ ~FallbackIconStyleBuilderTest() override {
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(FallbackIconStyleBuilderTest);
+};
+
+TEST_F(FallbackIconStyleBuilderTest, ParseColorSuccess) {
+ SkColor c;
+ EXPECT_TRUE(FallbackIconStyleBuilder::ParseColor("01aBf0f4", &c));
+ EXPECT_EQ(SkColorSetARGB(0xF4, 0x01, 0xAB, 0xF0), c);
+ EXPECT_TRUE(FallbackIconStyleBuilder::ParseColor("01aBf0", &c));
+ EXPECT_EQ(SkColorSetRGB(0x01, 0xAB, 0xF0), c);
+ EXPECT_TRUE(FallbackIconStyleBuilder::ParseColor("01a", &c));
+ EXPECT_EQ(SkColorSetRGB(0x00, 0x11, 0xAA), c);
+ EXPECT_TRUE(FallbackIconStyleBuilder::ParseColor("000000", &c));
+ EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), c);
+}
+
+TEST_F(FallbackIconStyleBuilderTest, ParseColorFailure) {
+ const char* test_cases[] = {
+ "",
+ "00000",
+ "000000000",
+ "0 1 2 3 ",
+ " 0 1 2 3",
+ "0ABCDEFG",
+ "#000000",
+ "000000 ",
+ "red"
+ };
+ for (int i = 0; i < arraysize(test_cases); ++i) {
+ SkColor c;
+ EXPECT_FALSE(FallbackIconStyleBuilder::ParseColor(test_cases[i], &c))
+ << "test_cases[" << i << "]";
+ }
+}
+
+void CheckHas(const FallbackIconStyleBuilder& builder,
+ bool has_background_color,
+ bool has_text_color,
+ bool has_font_size_ratio,
+ bool has_roundness,
+ const std::string& msg) {
+ EXPECT_EQ(has_background_color,
+ builder.Has(FallbackIconStyleBuilder::BACKGROUND_COLOR)) << msg;
+ EXPECT_EQ(has_text_color,
+ builder.Has(FallbackIconStyleBuilder::TEXT_COLOR))<< msg;
+ EXPECT_EQ(has_font_size_ratio,
+ builder.Has(FallbackIconStyleBuilder::FONT_SIZE_RATIO)) << msg;
+ EXPECT_EQ(has_roundness,
+ builder.Has(FallbackIconStyleBuilder::ROUNDNESS)) << msg;
+}
+
+TEST_F(FallbackIconStyleBuilderTest, SettersAndGetters) {
+ FallbackIconStyleBuilder builder;
+ CheckHas(builder, false, false, false, false, "Case 0");
+
+ builder.SetTextColor(SK_ColorBLACK);
+ CheckHas(builder, false, true, false, false, "Case 1");
+
+ builder.SetRoundness(0.25);
+ CheckHas(builder, false, true, false, true, "Case 2");
+
+ builder.SetBackgroundColor(SK_ColorWHITE);
+ CheckHas(builder, true, true, false, true, "Case 3");
+
+ builder.SetFontSizeRatio(0.75);
+ CheckHas(builder, true, true, true, true, "Case 4");
+
+ FallbackIconStyle style = builder.Build();
+ EXPECT_EQ(SK_ColorBLACK, style.text_color);
+ EXPECT_EQ(0.25, style.roundness);
+ EXPECT_EQ(SK_ColorWHITE, style.background_color);
+ EXPECT_EQ(0.75, style.font_size_ratio);
+
+ builder.Reset();
+ CheckHas(builder, false, false, false, false, "Case 5");
+}
+
+TEST_F(FallbackIconStyleBuilderTest, DefaultAll) {
+ FallbackIconStyleBuilder builder;
+ // Get defaults.
+ builder.AssignDefaults();
+ CheckHas(builder, true, true, true, true, "Case 0");
+
+ FallbackIconStyle style = builder.Build();
+ // kDefaultBackgroundColor
+ EXPECT_EQ(SkColorSetRGB(0x80, 0x80, 0x80), style.background_color);
+ // kDefaultTextColorLight
+ EXPECT_EQ(SK_ColorWHITE, style.text_color);
+ // kDefaultFontSizeRatio
+ EXPECT_EQ(0.8, style.font_size_ratio);
+ // kDefaultRoundness
+ EXPECT_EQ(0.125, style.roundness);
+}
+
+TEST_F(FallbackIconStyleBuilderTest, DefaultTextColor) {
+ {
+ // Dark background -> Light default text.
+ FallbackIconStyleBuilder builder;
+ builder.SetBackgroundColor(SkColorSetRGB(0x00, 0x00, 0x00));
+ FallbackIconStyle style = builder.Build();
+ EXPECT_EQ(SK_ColorWHITE, style.text_color); // kDefaultTextColorLight
+ }
+ {
+ FallbackIconStyleBuilder builder;
+ // Light background -> Dark default text.
+ builder.SetBackgroundColor(SkColorSetRGB(0xFF, 0xFF, 0xFF));
+ FallbackIconStyle style = builder.Build();
+ EXPECT_EQ(SK_ColorBLACK, style.text_color); // kDefaultTextColorDark
+ }
+}
+
+TEST_F(FallbackIconStyleBuilderTest, ParseEmpty) {
+ FallbackIconStyleBuilder builder;
+ // Empty string is valid, although nothing gets set.
+ EXPECT_TRUE(builder.Parse(""));
+ CheckHas(builder, false, false, false, false, "Case 0");
+
+ // Empty fields also valid valid, and nothing gets set.
+ EXPECT_TRUE(builder.Parse(",,,"));
+ CheckHas(builder, false, false, false, false, "Case 1");
+}
+
+TEST_F(FallbackIconStyleBuilderTest, ParseBasic) {
+ FallbackIconStyleBuilder builder;
+ EXPECT_TRUE(builder.Parse("000,fff,0.75,0.25"));
+ CheckHas(builder, true, true, true, true, "Case 0");
+ FallbackIconStyle style = builder.Build();
+ EXPECT_EQ(SK_ColorBLACK, style.background_color);
+ EXPECT_EQ(SK_ColorWHITE, style.text_color);
+ EXPECT_EQ(0.75, style.font_size_ratio);
+ EXPECT_EQ(0.25, style.roundness);
+}
+
+TEST_F(FallbackIconStyleBuilderTest, ParseWithMissingFields) {
+ {
+ FallbackIconStyleBuilder builder;
+ EXPECT_TRUE(builder.Parse("000,fff,0.75"));
+ CheckHas(builder, true, true, true, false, "Case 0");
+ FallbackIconStyle style = builder.Build();
+ EXPECT_EQ(SK_ColorBLACK, style.background_color);
+ EXPECT_EQ(SK_ColorWHITE, style.text_color);
+ EXPECT_EQ(0.75, style.font_size_ratio);
+ EXPECT_EQ(0.125, style.roundness); // default
+ }
+
+ SkColor default_background_color = SkColorSetRGB(0x80, 0x80, 0x80);
+ {
+ FallbackIconStyleBuilder builder;
+ EXPECT_TRUE(builder.Parse(",f00,0.8,0"));
+ CheckHas(builder, false, true, true, true, "Case 1");
+ FallbackIconStyle style = builder.Build();
+ EXPECT_EQ(default_background_color, style.background_color); // default
+ EXPECT_EQ(SK_ColorRED, style.text_color);
+ EXPECT_EQ(0.8, style.font_size_ratio);
+ EXPECT_EQ(0, style.roundness);
+ }
+
+ {
+ FallbackIconStyleBuilder builder;
+ EXPECT_TRUE(builder.Parse(",0000FfFf"));
+ CheckHas(builder, false, true, false, false, "Case 2");
+ FallbackIconStyle style = builder.Build();
+ EXPECT_EQ(default_background_color, style.background_color); // default
+ EXPECT_EQ(SK_ColorBLUE, style.text_color);
+ EXPECT_EQ(0.8, style.font_size_ratio); // default
+ EXPECT_EQ(0.125, style.roundness); // default
+ }
+
+ {
+ FallbackIconStyleBuilder builder;
+ EXPECT_TRUE(builder.Parse("00FF00,,,0.1"));
+ CheckHas(builder, true, false, false, true, "Case 3");
+ FallbackIconStyle style = builder.Build();
+ EXPECT_EQ(SK_ColorGREEN, style.background_color);
+ EXPECT_EQ(SK_ColorWHITE, style.text_color); // default
+ EXPECT_EQ(0.8, style.font_size_ratio); // default
+ EXPECT_EQ(0.1, style.roundness);
+ }
+}
+
+TEST_F(FallbackIconStyleBuilderTest, ParseFailure) {
+ const char* test_cases[] = {
+ " ",
+ ",,,,",
+ "000,fff, 0.75,0.25", // Don't allow any space.
+ "000 ,fff,0.75,0.25",
+ "000,fff,0.75,0.25 ",
+ "000,fff,0.75,0.25,junk",
+ "junk,fff,0.75,0.25",
+ "000,junk,0.75,0.25",
+ "000,fff,junk,0.25",
+ "000,fff,0.75,junk",
+ "000,fff,-0,1.75", // FontSizeRatio out of bound.
+ "000,fff,1.1,0.75", // FontSizeRatio out of bound.
+ "000,fff,0.75,-0.1", // Roundness out of bound.
+ "000,fff,0.75,1.1", // Roundness out of bound.
+ };
+ for (int i = 0; i < arraysize(test_cases); ++i) {
+ FallbackIconStyleBuilder builder;
+ EXPECT_FALSE(builder.Parse(test_cases[i])) << "test_cases[" << i << "]";
+ }
+}
+
+} // namespace test
+} // namespace favicon_base

Powered by Google App Engine
This is Rietveld 408576698