| 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..91cceaa3d4742e17bc36fb7fe37a915469ffb2b8
|
| --- /dev/null
|
| +++ b/chrome/common/favicon/fallback_icon_url_parser_unittest.cc
|
| @@ -0,0 +1,140 @@
|
| +// 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/fallback_icon_style_builder.h"
|
| +#include "components/favicon_base/favicon_types.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "ui/base/layout.h"
|
| +
|
| +using favicon_base::FallbackIconStyle;
|
| +using favicon_base::FallbackIconStyleBuilder;
|
| +
|
| +typedef favicon_base::FallbackIconStyleBuilder Builder;
|
| +
|
| +class FallbackIconUrlParserTest : public testing::Test {
|
| + public:
|
| + FallbackIconUrlParserTest() {
|
| + // 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 parsing path with no extra parameters.
|
| +TEST_F(FallbackIconUrlParserTest, ParsingNoExtraParams) {
|
| + const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
|
| + chrome::ParsedFallbackIconPath parsed;
|
| +
|
| + const std::string path1 = url;
|
| + EXPECT_TRUE(chrome::ParseFallbackIconPath(path1, &parsed));
|
| + EXPECT_EQ(url, parsed.url);
|
| + EXPECT_EQ(16, parsed.size_in_dip);
|
| + EXPECT_EQ(1.0f, parsed.device_scale_factor);
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::BACKGROUND_COLOR));
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::TEXT_COLOR));
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::FONT_SIZE_RATIO));
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::ROUNDNESS));
|
| +}
|
| +
|
| +// Test parsing path with a 'size' parameter.
|
| +TEST_F(FallbackIconUrlParserTest, ParsingSizeParam) {
|
| + const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
|
| + chrome::ParsedFallbackIconPath parsed;
|
| +
|
| + // Test that we can still parse the legacy 'size' parameter format.
|
| + const std::string path2 = "size/32/" + url;
|
| + EXPECT_TRUE(chrome::ParseFallbackIconPath(path2, &parsed));
|
| + EXPECT_EQ(url, parsed.url);
|
| + EXPECT_EQ(32, parsed.size_in_dip);
|
| + EXPECT_EQ(1.0f, parsed.device_scale_factor);
|
| +
|
| + // Test parsing current 'size' parameter format.
|
| + const std::string path3 = "size/32@1.4x/" + url;
|
| + EXPECT_TRUE(chrome::ParseFallbackIconPath(path3, &parsed));
|
| + EXPECT_EQ(url, parsed.url);
|
| + EXPECT_EQ(32, parsed.size_in_dip);
|
| + EXPECT_EQ(1.4f, parsed.device_scale_factor);
|
| +
|
| + // Test that we pick the ui::ScaleFactor which is closest to the passed in
|
| + // scale factor.
|
| + const std::string path4 = "size/16@1.41x/" + url;
|
| + EXPECT_TRUE(chrome::ParseFallbackIconPath(path4, &parsed));
|
| + EXPECT_EQ(url, parsed.url);
|
| + EXPECT_EQ(16, parsed.size_in_dip);
|
| + EXPECT_EQ(1.41f, parsed.device_scale_factor);
|
| +
|
| + // Invalid cases.
|
| + const std::string path5 = "size/" + url;
|
| + EXPECT_FALSE(chrome::ParseFallbackIconPath(path5, &parsed));
|
| + const std::string path6 = "size/@1x/" + url;
|
| + EXPECT_FALSE(chrome::ParseFallbackIconPath(path6, &parsed));
|
| + const std::string path7 = "size/abc@1x/" + url;
|
| + EXPECT_FALSE(chrome::ParseFallbackIconPath(path7, &parsed));
|
| +
|
| + // Part of url looks like 'size' parameter.
|
| + const std::string path8 = "http://www.google.com/size/32@1.4x";
|
| + EXPECT_TRUE(chrome::ParseFallbackIconPath(path8, &parsed));
|
| + EXPECT_EQ(path8, parsed.url);
|
| + EXPECT_EQ(16, parsed.size_in_dip);
|
| + EXPECT_EQ(1.0f, parsed.device_scale_factor);
|
| +}
|
| +
|
| +// Test parsing path with a 'style' parameter.
|
| +TEST_F(FallbackIconUrlParserTest, ParsingStyleParam) {
|
| + const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
|
| + chrome::ParsedFallbackIconPath parsed;
|
| + FallbackIconStyle style;
|
| +
|
| + // Empty is valid.
|
| + const std::string path9 = "style//" + url;
|
| + EXPECT_TRUE(chrome::ParseFallbackIconPath(path9, &parsed));
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::BACKGROUND_COLOR));
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::TEXT_COLOR));
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::FONT_SIZE_RATIO));
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::ROUNDNESS));
|
| +
|
| + // Everything populated.
|
| + const std::string path10 = "style/abc,123456,0.5,0.1/" + url;
|
| + EXPECT_TRUE(chrome::ParseFallbackIconPath(path10, &parsed));
|
| + EXPECT_TRUE(parsed.style_builder.Has(Builder::BACKGROUND_COLOR));
|
| + EXPECT_TRUE(parsed.style_builder.Has(Builder::TEXT_COLOR));
|
| + EXPECT_TRUE(parsed.style_builder.Has(Builder::FONT_SIZE_RATIO));
|
| + EXPECT_TRUE(parsed.style_builder.Has(Builder::ROUNDNESS));
|
| + style = parsed.style_builder.Build();
|
| + EXPECT_EQ(SkColorSetRGB(0xAA, 0xBB, 0xCC), style.background_color);
|
| + EXPECT_EQ(SkColorSetRGB(0x12, 0x34, 0x56), style.text_color);
|
| + EXPECT_EQ(0.5, style.font_size_ratio);
|
| + EXPECT_EQ(0.1, style.roundness);
|
| +
|
| + // Partially populated.
|
| + const std::string path11 = "style/123abc,,0.6/" + url;
|
| + EXPECT_TRUE(chrome::ParseFallbackIconPath(path11, &parsed));
|
| + EXPECT_TRUE(parsed.style_builder.Has(Builder::BACKGROUND_COLOR));
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::TEXT_COLOR));
|
| + EXPECT_TRUE(parsed.style_builder.Has(Builder::FONT_SIZE_RATIO));
|
| + EXPECT_FALSE(parsed.style_builder.Has(Builder::ROUNDNESS));
|
| + style = parsed.style_builder.Build();
|
| + EXPECT_EQ(SkColorSetRGB(0x12, 0x3A, 0xBC), style.background_color);
|
| + EXPECT_EQ(0.6, style.font_size_ratio);
|
| +
|
| + // Error: font_size_ratio too large.
|
| + const std::string path12 = "style/fff,000,100/" + url;
|
| + EXPECT_FALSE(chrome::ParseFallbackIconPath(path12, &parsed));
|
| +}
|
|
|