OLD | NEW |
(Empty) | |
| 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/browser/extensions/favicon_downloader.h" |
| 6 |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 9 #include "content/public/common/favicon_url.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 |
| 13 using content::RenderViewHostTester; |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Creates valid SkBitmaps of the dimensions found in |sizes| and pushes them |
| 18 // into |bitmaps|. |
| 19 std::vector<SkBitmap> CreateTestBitmaps(const std::vector<gfx::Size>& sizes) { |
| 20 std::vector<SkBitmap> bitmaps(sizes.size()); |
| 21 for (size_t i = 0; i < sizes.size(); ++i) { |
| 22 SkBitmap& bitmap = bitmaps[i]; |
| 23 bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 24 sizes[i].width(), |
| 25 sizes[i].height()); |
| 26 bitmap.allocPixels(); |
| 27 bitmap.eraseColor(SK_ColorRED); |
| 28 } |
| 29 return bitmaps; |
| 30 } |
| 31 |
| 32 class FaviconDownloaderTest : public ChromeRenderViewHostTestHarness { |
| 33 protected: |
| 34 FaviconDownloaderTest() { |
| 35 } |
| 36 |
| 37 virtual ~FaviconDownloaderTest() { |
| 38 } |
| 39 |
| 40 private: |
| 41 DISALLOW_COPY_AND_ASSIGN(FaviconDownloaderTest); |
| 42 }; |
| 43 |
| 44 } // namespace |
| 45 |
| 46 class TestFaviconDownloader : public FaviconDownloader { |
| 47 public: |
| 48 TestFaviconDownloader(content::WebContents* web_contents, |
| 49 std::vector<GURL> extra_favicon_urls) |
| 50 : FaviconDownloader( |
| 51 web_contents, |
| 52 extra_favicon_urls, |
| 53 base::Bind(&TestFaviconDownloader::DownloadsComplete, |
| 54 base::Unretained(this))), |
| 55 id_counter_(0) { |
| 56 } |
| 57 virtual ~TestFaviconDownloader() {} |
| 58 |
| 59 virtual int DownloadImage(const GURL& url) OVERRIDE { |
| 60 return id_counter_++; |
| 61 } |
| 62 |
| 63 virtual std::vector<content::FaviconURL> GetFaviconURLsFromWebContents() |
| 64 OVERRIDE { |
| 65 return initial_favicon_urls_; |
| 66 } |
| 67 |
| 68 size_t pending_requests() const { |
| 69 return in_progress_requests_.size(); |
| 70 } |
| 71 |
| 72 void DownloadsComplete(bool success, |
| 73 const FaviconDownloader::FaviconMap& map) { |
| 74 favicon_map_ = map; |
| 75 } |
| 76 |
| 77 FaviconDownloader::FaviconMap favicon_map() const { |
| 78 return favicon_map_; |
| 79 } |
| 80 |
| 81 void CompleteImageDownload( |
| 82 int id, |
| 83 const GURL& image_url, |
| 84 const std::vector<gfx::Size>& original_bitmap_sizes) { |
| 85 FaviconDownloader::DidDownloadFavicon(id, 200, image_url, |
| 86 CreateTestBitmaps(original_bitmap_sizes), original_bitmap_sizes); |
| 87 } |
| 88 |
| 89 void UpdateFaviconURLs(const std::vector<content::FaviconURL>& candidates) { |
| 90 FaviconDownloader::DidUpdateFaviconURL(0, candidates); |
| 91 } |
| 92 |
| 93 void set_initial_favicon_urls(const std::vector<content::FaviconURL>& urls) { |
| 94 initial_favicon_urls_ = urls; |
| 95 } |
| 96 |
| 97 private: |
| 98 std::vector<content::FaviconURL> initial_favicon_urls_; |
| 99 FaviconDownloader::FaviconMap favicon_map_; |
| 100 int id_counter_; |
| 101 DISALLOW_COPY_AND_ASSIGN(TestFaviconDownloader); |
| 102 }; |
| 103 |
| 104 TEST_F(FaviconDownloaderTest, SimpleDownload) { |
| 105 const GURL favicon_url("http://www.google.com/favicon.ico"); |
| 106 TestFaviconDownloader downloader(web_contents(), std::vector<GURL>()); |
| 107 |
| 108 std::vector<content::FaviconURL> favicon_urls; |
| 109 favicon_urls.push_back( |
| 110 content::FaviconURL(favicon_url, |
| 111 content::FaviconURL::FAVICON)); |
| 112 downloader.set_initial_favicon_urls(favicon_urls); |
| 113 EXPECT_EQ(0u, downloader.pending_requests()); |
| 114 |
| 115 downloader.Start(); |
| 116 EXPECT_EQ(1u, downloader.pending_requests()); |
| 117 |
| 118 std::vector<gfx::Size> sizes(1, gfx::Size(32, 32)); |
| 119 downloader.CompleteImageDownload(0, favicon_urls[0].icon_url, sizes); |
| 120 EXPECT_EQ(0u, downloader.pending_requests()); |
| 121 |
| 122 EXPECT_EQ(1u, downloader.favicon_map().size()); |
| 123 EXPECT_EQ(1u, downloader.favicon_map()[favicon_url].size()); |
| 124 } |
| 125 |
| 126 TEST_F(FaviconDownloaderTest, DownloadWithUrlsFromWebContentsNotification) { |
| 127 const GURL favicon_url("http://www.google.com/favicon.ico"); |
| 128 TestFaviconDownloader downloader(web_contents(), std::vector<GURL>()); |
| 129 |
| 130 std::vector<content::FaviconURL> favicon_urls; |
| 131 favicon_urls.push_back( |
| 132 content::FaviconURL(favicon_url, |
| 133 content::FaviconURL::FAVICON)); |
| 134 EXPECT_EQ(0u, downloader.pending_requests()); |
| 135 |
| 136 // Start downloader before favicon URLs are loaded. |
| 137 downloader.Start(); |
| 138 EXPECT_EQ(0u, downloader.pending_requests()); |
| 139 |
| 140 downloader.UpdateFaviconURLs(favicon_urls); |
| 141 EXPECT_EQ(1u, downloader.pending_requests()); |
| 142 |
| 143 std::vector<gfx::Size> sizes(1, gfx::Size(32, 32)); |
| 144 downloader.CompleteImageDownload(0, favicon_urls[0].icon_url, sizes); |
| 145 EXPECT_EQ(0u, downloader.pending_requests()); |
| 146 |
| 147 EXPECT_EQ(1u, downloader.favicon_map().size()); |
| 148 EXPECT_EQ(1u, downloader.favicon_map()[favicon_url].size()); |
| 149 } |
| 150 |
| 151 TEST_F(FaviconDownloaderTest, DownloadMultipleUrls) { |
| 152 const GURL empty_favicon("http://www.google.com/empty_favicon.ico"); |
| 153 const GURL favicon_url_1("http://www.google.com/favicon.ico"); |
| 154 const GURL favicon_url_2("http://www.google.com/favicon2.ico"); |
| 155 |
| 156 std::vector<GURL> extra_urls; |
| 157 // This should get downloaded. |
| 158 extra_urls.push_back(favicon_url_2); |
| 159 // This is duplicated in the favicon urls and should only be downloaded once. |
| 160 extra_urls.push_back(empty_favicon); |
| 161 |
| 162 TestFaviconDownloader downloader(web_contents(), extra_urls); |
| 163 std::vector<content::FaviconURL> favicon_urls; |
| 164 favicon_urls.push_back( |
| 165 content::FaviconURL(favicon_url_1, |
| 166 content::FaviconURL::FAVICON)); |
| 167 // This is duplicated in the favicon urls and should only be downloaded once. |
| 168 favicon_urls.push_back( |
| 169 content::FaviconURL(empty_favicon, |
| 170 content::FaviconURL::FAVICON)); |
| 171 // Invalid icons shouldn't get put into the download queue. |
| 172 favicon_urls.push_back( |
| 173 content::FaviconURL(GURL("http://www.google.com/invalid.ico"), |
| 174 content::FaviconURL::INVALID_ICON)); |
| 175 downloader.set_initial_favicon_urls(favicon_urls); |
| 176 downloader.Start(); |
| 177 EXPECT_EQ(3u, downloader.pending_requests()); |
| 178 |
| 179 std::vector<gfx::Size> sizes_1(1, gfx::Size(16, 16)); |
| 180 downloader.CompleteImageDownload(0, favicon_url_1, sizes_1); |
| 181 |
| 182 std::vector<gfx::Size> sizes_2; |
| 183 sizes_2.push_back(gfx::Size(32, 32)); |
| 184 sizes_2.push_back(gfx::Size(64, 64)); |
| 185 downloader.CompleteImageDownload(1, favicon_url_2, sizes_2); |
| 186 |
| 187 // Only 1 download should have been initiated for |empty_favicon| even though |
| 188 // the URL was in both the web app info and the favicon urls. |
| 189 downloader.CompleteImageDownload(2, empty_favicon, std::vector<gfx::Size>()); |
| 190 EXPECT_EQ(0u, downloader.pending_requests()); |
| 191 |
| 192 EXPECT_EQ(3u, downloader.favicon_map().size()); |
| 193 EXPECT_EQ(0u, downloader.favicon_map()[empty_favicon].size()); |
| 194 EXPECT_EQ(1u, downloader.favicon_map()[favicon_url_1].size()); |
| 195 EXPECT_EQ(2u, downloader.favicon_map()[favicon_url_2].size()); |
| 196 } |
| 197 |
OLD | NEW |