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

Unified Diff: chrome/common/favicon/fallback_icon_url_parser_unittest.cc

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removing FallbackIconStyleBuilder; trimming down chrome://fallback-icon syntax. 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: chrome/common/favicon/fallback_icon_url_parser_unittest.cc
diff --git a/chrome/common/favicon/fallback_icon_url_parser_unittest.cc b/chrome/common/favicon/fallback_icon_url_parser_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..de9abb96e64e9998e4369e5539ebd051b98baf9d
--- /dev/null
+++ b/chrome/common/favicon/fallback_icon_url_parser_unittest.cc
@@ -0,0 +1,229 @@
+// 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 "chrome/common/favicon/fallback_icon_url_parser.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "components/favicon_base/favicon_types.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/skia/include/core/SkColor.h"
+#include "ui/base/layout.h"
+#include "ui/gfx/favicon_size.h"
+
+using favicon_base::FallbackIconStyle;
+
+namespace {
+
+// Default values for FallbackIconStyle, from
+ // /components/favicon_base/favicon_types.h
+SkColor kDefaultBackgroundColor = SkColorSetRGB(0x80, 0x80, 0x80);
+SkColor kDefaultTextColorDark = SK_ColorBLACK;
+SkColor kDefaultTextColorLight = SK_ColorWHITE;
+double kDefaultFontSizeRatio = 0.8;
+double kDefaultRoundness = 0.125; // 1 / 8.
+
+const char kTestUrl[] = "https://www.google.ca/imghp?hl=en&tab=wi";
+
+} // namespace
+
+namespace chrome {
+
+class FallbackIconUrlParserTest : public testing::Test {
+ public:
+ FallbackIconUrlParserTest() {
pkotwicz 2015/01/23 16:00:47 Setting the supported scale factors should no long
huangs 2015/01/23 19:47:51 Removed, also got rid of #include "ui/base/layout.
+ // Set the supported scale factors because the supported scale factors
+ // affect the result of ParsePathAndScale().
+ std::vector<ui::ScaleFactor> supported_scale_factors;
+ supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
+ supported_scale_factors.push_back(ui::SCALE_FACTOR_140P);
+ scoped_set_supported_scale_factors_.reset(
+ new ui::test::ScopedSetSupportedScaleFactors(supported_scale_factors));
+ }
+
+ ~FallbackIconUrlParserTest() override {}
+
+ private:
+ typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors>
+ ScopedSetSupportedScaleFactors;
+ ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
+
+ DISALLOW_COPY_AND_ASSIGN(FallbackIconUrlParserTest);
+};
+
+TEST_F(FallbackIconUrlParserTest, ParseIconColorSuccess) {
+ SkColor c;
+ EXPECT_TRUE(ParseIconColor("01aBf0f4", &c));
+ EXPECT_EQ(SkColorSetARGB(0xF4, 0x01, 0xAB, 0xF0), c);
+ EXPECT_TRUE(ParseIconColor("01aBf0", &c));
+ EXPECT_EQ(SkColorSetRGB(0x01, 0xAB, 0xF0), c);
+ EXPECT_TRUE(ParseIconColor("01a", &c));
+ EXPECT_EQ(SkColorSetRGB(0x00, 0x11, 0xAA), c);
+ EXPECT_TRUE(ParseIconColor("000000", &c));
+ EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), c);
+}
+
+TEST_F(FallbackIconUrlParserTest, ParseIconColorFailure) {
+ 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(ParseIconColor(test_cases[i], &c))
+ << "test_cases[" << i << "]";
+ }
+}
+
+TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsEmpty) {
+ int size;
+ FallbackIconStyle style;
+ EXPECT_TRUE(ParseFallbackIconSpecs(",,,,", &size, &style));
+ EXPECT_EQ(16, size);
+ EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
+ EXPECT_EQ(kDefaultTextColorLight, style.text_color);
+ EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
+ EXPECT_EQ(kDefaultRoundness, style.roundness);
+}
+
+TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsPartial) {
+ int size;
+ FallbackIconStyle style;
+ EXPECT_TRUE(ParseFallbackIconSpecs(",,aCE,,0.1", &size, &style));
+ EXPECT_EQ(16, size);
+ EXPECT_EQ(kDefaultBackgroundColor, style.background_color);
+ EXPECT_EQ(SkColorSetRGB(0xAA, 0xCC, 0xEE), style.text_color);
+ EXPECT_EQ(kDefaultFontSizeRatio, style.font_size_ratio);
+ EXPECT_EQ(0.1, style.roundness);
+}
+
+TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsFull) {
+ int size1;
+ FallbackIconStyle style1;
+ EXPECT_TRUE(ParseFallbackIconSpecs("16,000,fff,0.75,0.25", &size1, &style1));
+ EXPECT_EQ(16, size1);
+ EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style1.background_color);
+ EXPECT_EQ(SkColorSetRGB(0xff, 0xff, 0xff), style1.text_color);
+ EXPECT_EQ(0.75, style1.font_size_ratio);
+ EXPECT_EQ(0.25, style1.roundness);
+
+ int size2;
+ FallbackIconStyle style2;
+ EXPECT_TRUE(ParseFallbackIconSpecs("48,f05,123456,0.5,0.3", &size2, &style2));
+ EXPECT_EQ(48, size2);
+ EXPECT_EQ(SkColorSetRGB(0xff, 0x00, 0x55), style2.background_color);
+ EXPECT_EQ(SkColorSetRGB(0x12, 0x34, 0x56), style2.text_color);
+ EXPECT_EQ(0.5, style2.font_size_ratio);
+ EXPECT_EQ(0.3, style2.roundness);
+
+ int size3;
+ FallbackIconStyle style3;
+ EXPECT_TRUE(ParseFallbackIconSpecs("1,000,000,0,0", &size3, &style3));
+ EXPECT_EQ(1, size3);
+ EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style3.background_color);
+ EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), style3.text_color);
+ EXPECT_EQ(0, style3.font_size_ratio);
+ EXPECT_EQ(0, style3.roundness);
+}
+
+TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsDefaultTextColor) {
+ int size;
+
+ // Dark background -> Light text.
+ FallbackIconStyle style1;
+ EXPECT_TRUE(ParseFallbackIconSpecs(",000,,,", &size, &style1));
+ EXPECT_EQ(kDefaultTextColorLight, style1.text_color);
+
+ // Light background -> Dark text.
+ FallbackIconStyle style2;
+ EXPECT_TRUE(ParseFallbackIconSpecs(",fff,,,", &size, &style2));
+ EXPECT_EQ(kDefaultTextColorDark, style2.text_color);
+
+ // Light background -> Dark text, more params don't matter.
+ FallbackIconStyle style3;
+ EXPECT_TRUE(ParseFallbackIconSpecs("107,fff,,0.3,0.5", &size, &style3));
+ EXPECT_EQ(kDefaultTextColorDark, style2.text_color);
+}
+
+TEST_F(FallbackIconUrlParserTest, ParseFallbackIconSpecsFailure) {
+ const char* test_cases[] = {
+ // Need exactly 5 params.
+ "",
+ "16",
+ "16,000",
+ "16,000,fff",
+ "16,000,fff,0.75",
+ ",,,"
+ ",,,,,",
+ // Don't allow any space.
+ "16,000,fff, 0.75,0.25",
+ "16,000 ,fff,0.75,0.25",
+ "16,000,fff,0.75,0.25 ",
+ // Adding junk text.
+ "16,000,fff,0.75,0.25junk",
+ "16,000,fff,0.75,0.25,junk",
pkotwicz 2015/01/23 16:00:48 The above test case belongs in the "exactly 5 para
huangs 2015/01/23 19:47:51 Done.
+ "junk,000,fff,0.75,0.25",
+ "16,junk,fff,0.75,0.25",
+ "16,000,junk,0.75,0.25",
+ "16,000,fff,junk,0.25",
+ "16,000,fff,0.75,junk",
+ // Out of bound.
+ "0,000,fff,0.75,0.25", // size.
+ "4294967296,000,fff,0.75,0.25", // size.
+ "-1,000,fff,0.75,0.25", // size.
+ "16,000,fff,-0.1,0.25", // font_size_ratio.
+ "16,000,fff,1.1,0.25", // font_size_ratio.
+ "16,000,fff,0.75,-0.1", // roundness.
+ "16,000,fff,0.75,1.1", // roundness.
+ };
+ for (int i = 0; i < arraysize(test_cases); ++i) {
+ int size;
+ FallbackIconStyle style;
+ EXPECT_FALSE(ParseFallbackIconSpecs(test_cases[i], &size, &style))
+ << "test_cases[" << i << "]";
+
+ }
+}
+
+TEST_F(FallbackIconUrlParserTest, ParseFallbackIconPath) {
+ const std::string specs = "31,000,fff,0.75,0.25";
+ const std::string url = kTestUrl;
+ chrome::ParsedFallbackIconPath parsed;
+
+ // Everything populated.
+ EXPECT_TRUE(chrome::ParseFallbackIconPath(specs + "/" + url, &parsed));
+ EXPECT_EQ(kTestUrl, parsed.url);
+ EXPECT_EQ(31, parsed.size_in_pixels);
+ EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), parsed.style.background_color);
+ EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), parsed.style.text_color);
+ EXPECT_EQ(0.75, parsed.style.font_size_ratio);
+ EXPECT_EQ(0.25, parsed.style.roundness);
+
pkotwicz 2015/01/23 16:00:48 Nit: Remove extra blank line
huangs 2015/01/23 19:47:51 Done.
+
+ // Empty URL..
pkotwicz 2015/01/23 16:00:48 Nit: Remove extra period
huangs 2015/01/23 19:47:51 Done.
+ EXPECT_TRUE(chrome::ParseFallbackIconPath(specs + "/", &parsed));
+ EXPECT_EQ(kTestUrl, "");
+ EXPECT_EQ(31, parsed.size_in_pixels);
+ EXPECT_EQ(SkColorSetRGB(0x00, 0x00, 0x00), parsed.style.background_color);
+ EXPECT_EQ(SkColorSetRGB(0xFF, 0xFF, 0xFF), parsed.style.text_color);
+ EXPECT_EQ(0.75, parsed.style.font_size_ratio);
+ EXPECT_EQ(0.25, parsed.style.roundness);
+
+ // Size and style are default.
+ EXPECT_TRUE(chrome::ParseFallbackIconPath(",,,,/" + url, &parsed));
+ EXPECT_EQ(kTestUrl, parsed.url);
+ EXPECT_EQ(gfx::kFaviconSize, parsed.size_in_pixels);
+ EXPECT_EQ(kDefaultBackgroundColor, parsed.style.background_color);
+ EXPECT_EQ(kDefaultTextColorLight, parsed.style.text_color);
+ EXPECT_EQ(kDefaultFontSizeRatio, parsed.style.font_size_ratio);
+ EXPECT_EQ(kDefaultRoundness, parsed.style.roundness);
+}
+
+} // namespace chrome

Powered by Google App Engine
This is Rietveld 408576698