Home | History | Annotate | Download | only in favicon
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/common/favicon/favicon_url_parser.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "chrome/common/favicon/favicon_types.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 #include "ui/base/layout.h"
     11 
     12 class FaviconUrlParserTest : public testing::Test {
     13  public:
     14   FaviconUrlParserTest() {
     15     // Set the supported scale factors because the supported scale factors
     16     // affect the result of ParsePathAndScale().
     17     std::vector<ui::ScaleFactor> supported_scale_factors;
     18     supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
     19     supported_scale_factors.push_back(ui::SCALE_FACTOR_140P);
     20     scoped_set_supported_scale_factors_.reset(
     21         new ui::test::ScopedSetSupportedScaleFactors(supported_scale_factors));
     22   }
     23 
     24   virtual ~FaviconUrlParserTest() {
     25   }
     26 
     27  private:
     28   typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors>
     29       ScopedSetSupportedScaleFactors;
     30   ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
     31 
     32   DISALLOW_COPY_AND_ASSIGN(FaviconUrlParserTest);
     33 };
     34 
     35 // Test parsing path with no extra parameters.
     36 TEST_F(FaviconUrlParserTest, ParsingNoExtraParams) {
     37   const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
     38   int icon_types = chrome::TOUCH_PRECOMPOSED_ICON;
     39   chrome::ParsedFaviconPath parsed;
     40 
     41   const std::string path1 = url;
     42   EXPECT_TRUE(chrome::ParseFaviconPath(path1, icon_types, &parsed));
     43   EXPECT_FALSE(parsed.is_icon_url);
     44   EXPECT_EQ(url, parsed.url);
     45   EXPECT_EQ(16, parsed.size_in_dip);
     46   EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor);
     47 }
     48 
     49 // Test parsing path with a 'size' parameter.
     50 TEST_F(FaviconUrlParserTest, ParsingSizeParam) {
     51   const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
     52   int icon_types = chrome::TOUCH_PRECOMPOSED_ICON;
     53   chrome::ParsedFaviconPath parsed;
     54 
     55   // Test that we can still parse the legacy 'size' parameter format.
     56   const std::string path2 = "size/32/" + url;
     57   EXPECT_TRUE(chrome::ParseFaviconPath(path2, icon_types, &parsed));
     58   EXPECT_FALSE(parsed.is_icon_url);
     59   EXPECT_EQ(url, parsed.url);
     60   EXPECT_EQ(32, parsed.size_in_dip);
     61   EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor);
     62 
     63   // Test parsing current 'size' parameter format.
     64   const std::string path3 = "size/32 (at) 1.4x/" + url;
     65   EXPECT_TRUE(chrome::ParseFaviconPath(path3, icon_types, &parsed));
     66   EXPECT_FALSE(parsed.is_icon_url);
     67   EXPECT_EQ(url, parsed.url);
     68   EXPECT_EQ(32, parsed.size_in_dip);
     69   EXPECT_EQ(ui::SCALE_FACTOR_140P, parsed.scale_factor);
     70 
     71   // Test that we pick the ui::ScaleFactor which is closest to the passed in
     72   // scale factor.
     73   const std::string path4 = "size/16 (at) 1.41x/" + url;
     74   EXPECT_TRUE(chrome::ParseFaviconPath(path4, icon_types, &parsed));
     75   EXPECT_FALSE(parsed.is_icon_url);
     76   EXPECT_EQ(url, parsed.url);
     77   EXPECT_EQ(16, parsed.size_in_dip);
     78   EXPECT_EQ(ui::SCALE_FACTOR_140P, parsed.scale_factor);
     79 
     80   // Invalid cases.
     81   const std::string path5 = "size/" + url;
     82   EXPECT_FALSE(chrome::ParseFaviconPath(path5, icon_types, &parsed));
     83   const std::string path6 = "size/@1x/" + url;
     84   EXPECT_FALSE(chrome::ParseFaviconPath(path6, icon_types, &parsed));
     85   const std::string path7 = "size/abc@1x/" + url;
     86   EXPECT_FALSE(chrome::ParseFaviconPath(path7, icon_types, &parsed));
     87 
     88   // Part of url looks like 'size' parameter.
     89   const std::string path8 = "http://www.google.com/size/32@1.4x";
     90   EXPECT_TRUE(chrome::ParseFaviconPath(path8, icon_types, &parsed));
     91   EXPECT_FALSE(parsed.is_icon_url);
     92   EXPECT_EQ(path8, parsed.url);
     93   EXPECT_EQ(16, parsed.size_in_dip);
     94   EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor);
     95 }
     96 
     97 // Test parsing path with the 'largest' parameter.
     98 TEST_F(FaviconUrlParserTest, ParsingLargestParam) {
     99   const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
    100   int icon_types = chrome::TOUCH_PRECOMPOSED_ICON;
    101   chrome::ParsedFaviconPath parsed;
    102 
    103   const std::string path9 = "largest/" + url;
    104   EXPECT_TRUE(chrome::ParseFaviconPath(path9, icon_types, &parsed));
    105   EXPECT_FALSE(parsed.is_icon_url);
    106   EXPECT_EQ(url, parsed.url);
    107   EXPECT_EQ(0, parsed.size_in_dip);
    108   // The scale factor is meaningless when requesting the largest favicon.
    109 }
    110 
    111 // Test parsing path with 'iconurl' parameter.
    112 TEST_F(FaviconUrlParserTest, ParsingIconUrlParam) {
    113   const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
    114   int icon_types = chrome::TOUCH_PRECOMPOSED_ICON;
    115   chrome::ParsedFaviconPath parsed;
    116 
    117   const std::string path10 = "iconurl/http://www.google.com/favicon.ico";
    118   EXPECT_TRUE(chrome::ParseFaviconPath(path10, icon_types, &parsed));
    119   EXPECT_TRUE(parsed.is_icon_url);
    120   EXPECT_EQ("http://www.google.com/favicon.ico", parsed.url);
    121   EXPECT_EQ(16, parsed.size_in_dip);
    122   EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor);
    123 }
    124 
    125 // Test parsing path with 'origin' parameter.
    126 TEST_F(FaviconUrlParserTest, ParsingOriginParam) {
    127   const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
    128   int icon_types = chrome::TOUCH_PRECOMPOSED_ICON;
    129   chrome::ParsedFaviconPath parsed;
    130 
    131   const std::string path11 = "origin/" + url;
    132   EXPECT_TRUE(chrome::ParseFaviconPath(path11, icon_types, &parsed));
    133   EXPECT_FALSE(parsed.is_icon_url);
    134   EXPECT_EQ("https://www.google.ca/", parsed.url);
    135   EXPECT_EQ(16, parsed.size_in_dip);
    136   EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor);
    137 
    138   const std::string path12 = "origin/google.com";
    139   EXPECT_TRUE(chrome::ParseFaviconPath(path12, icon_types, &parsed));
    140   EXPECT_FALSE(parsed.is_icon_url);
    141   EXPECT_EQ("http://google.com/", parsed.url);
    142   EXPECT_EQ(16, parsed.size_in_dip);
    143   EXPECT_EQ(ui::SCALE_FACTOR_100P, parsed.scale_factor);
    144 }
    145 
    146 // Test parsing paths with both a 'size' parameter and a 'url modifier'
    147 // parameter.
    148 TEST_F(FaviconUrlParserTest, ParsingSizeParamAndUrlModifier) {
    149   const std::string url("https://www.google.ca/imghp?hl=en&tab=wi");
    150   int icon_types = chrome::TOUCH_PRECOMPOSED_ICON;
    151   chrome::ParsedFaviconPath parsed;
    152 
    153   const std::string path13 = "size/32 (at) 1.4x/origin/" + url;
    154   EXPECT_TRUE(chrome::ParseFaviconPath(path13, icon_types, &parsed));
    155   EXPECT_FALSE(parsed.is_icon_url);
    156   EXPECT_EQ("https://www.google.ca/", parsed.url);
    157   EXPECT_EQ(32, parsed.size_in_dip);
    158   EXPECT_EQ(ui::SCALE_FACTOR_140P, parsed.scale_factor);
    159 
    160   const std::string path14 =
    161       "largest/iconurl/http://www.google.com/favicon.ico";
    162   EXPECT_TRUE(chrome::ParseFaviconPath(path14, icon_types, &parsed));
    163   EXPECT_TRUE(parsed.is_icon_url);
    164   EXPECT_EQ("http://www.google.com/favicon.ico", parsed.url);
    165   EXPECT_EQ(0, parsed.size_in_dip);
    166 }
    167