Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / favicon_downloader_unittest.cc
blob42de071f332c94630a6e200a55a73f94249ba416
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.
5 #include "chrome/browser/extensions/favicon_downloader.h"
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"
13 using content::RenderViewHostTester;
15 namespace {
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.allocN32Pixels(sizes[i].width(), sizes[i].height());
24 bitmap.eraseColor(SK_ColorRED);
26 return bitmaps;
29 class FaviconDownloaderTest : public ChromeRenderViewHostTestHarness {
30 protected:
31 FaviconDownloaderTest() {
34 ~FaviconDownloaderTest() override {}
36 private:
37 DISALLOW_COPY_AND_ASSIGN(FaviconDownloaderTest);
40 } // namespace
42 class TestFaviconDownloader : public FaviconDownloader {
43 public:
44 TestFaviconDownloader(content::WebContents* web_contents,
45 std::vector<GURL> extra_favicon_urls)
46 : FaviconDownloader(
47 web_contents,
48 extra_favicon_urls,
49 base::Bind(&TestFaviconDownloader::DownloadsComplete,
50 base::Unretained(this))),
51 id_counter_(0) {
53 ~TestFaviconDownloader() override {}
55 int DownloadImage(const GURL& url) override { return id_counter_++; }
57 std::vector<content::FaviconURL> GetFaviconURLsFromWebContents() override {
58 return initial_favicon_urls_;
61 size_t pending_requests() const {
62 return in_progress_requests_.size();
65 void DownloadsComplete(bool success,
66 const FaviconDownloader::FaviconMap& map) {
67 favicon_map_ = map;
70 FaviconDownloader::FaviconMap favicon_map() const {
71 return favicon_map_;
74 void CompleteImageDownload(
75 int id,
76 const GURL& image_url,
77 const std::vector<gfx::Size>& original_bitmap_sizes) {
78 FaviconDownloader::DidDownloadFavicon(id, 200, image_url,
79 CreateTestBitmaps(original_bitmap_sizes), original_bitmap_sizes);
82 void UpdateFaviconURLs(const std::vector<content::FaviconURL>& candidates) {
83 FaviconDownloader::DidUpdateFaviconURL(candidates);
86 void set_initial_favicon_urls(const std::vector<content::FaviconURL>& urls) {
87 initial_favicon_urls_ = urls;
90 private:
91 std::vector<content::FaviconURL> initial_favicon_urls_;
92 FaviconDownloader::FaviconMap favicon_map_;
93 int id_counter_;
94 DISALLOW_COPY_AND_ASSIGN(TestFaviconDownloader);
97 TEST_F(FaviconDownloaderTest, SimpleDownload) {
98 const GURL favicon_url("http://www.google.com/favicon.ico");
99 TestFaviconDownloader downloader(web_contents(), std::vector<GURL>());
101 std::vector<content::FaviconURL> favicon_urls;
102 favicon_urls.push_back(content::FaviconURL(
103 favicon_url, content::FaviconURL::FAVICON, std::vector<gfx::Size>()));
104 downloader.set_initial_favicon_urls(favicon_urls);
105 EXPECT_EQ(0u, downloader.pending_requests());
107 downloader.Start();
108 EXPECT_EQ(1u, downloader.pending_requests());
110 std::vector<gfx::Size> sizes(1, gfx::Size(32, 32));
111 downloader.CompleteImageDownload(0, favicon_urls[0].icon_url, sizes);
112 EXPECT_EQ(0u, downloader.pending_requests());
114 EXPECT_EQ(1u, downloader.favicon_map().size());
115 EXPECT_EQ(1u, downloader.favicon_map()[favicon_url].size());
118 TEST_F(FaviconDownloaderTest, DownloadWithUrlsFromWebContentsNotification) {
119 const GURL favicon_url("http://www.google.com/favicon.ico");
120 TestFaviconDownloader downloader(web_contents(), std::vector<GURL>());
122 std::vector<content::FaviconURL> favicon_urls;
123 favicon_urls.push_back(content::FaviconURL(
124 favicon_url, content::FaviconURL::FAVICON, std::vector<gfx::Size>()));
125 EXPECT_EQ(0u, downloader.pending_requests());
127 // Start downloader before favicon URLs are loaded.
128 downloader.Start();
129 EXPECT_EQ(0u, downloader.pending_requests());
131 downloader.UpdateFaviconURLs(favicon_urls);
132 EXPECT_EQ(1u, downloader.pending_requests());
134 std::vector<gfx::Size> sizes(1, gfx::Size(32, 32));
135 downloader.CompleteImageDownload(0, favicon_urls[0].icon_url, sizes);
136 EXPECT_EQ(0u, downloader.pending_requests());
138 EXPECT_EQ(1u, downloader.favicon_map().size());
139 EXPECT_EQ(1u, downloader.favicon_map()[favicon_url].size());
142 TEST_F(FaviconDownloaderTest, DownloadMultipleUrls) {
143 const GURL empty_favicon("http://www.google.com/empty_favicon.ico");
144 const GURL favicon_url_1("http://www.google.com/favicon.ico");
145 const GURL favicon_url_2("http://www.google.com/favicon2.ico");
147 std::vector<GURL> extra_urls;
148 // This should get downloaded.
149 extra_urls.push_back(favicon_url_2);
150 // This is duplicated in the favicon urls and should only be downloaded once.
151 extra_urls.push_back(empty_favicon);
153 TestFaviconDownloader downloader(web_contents(), extra_urls);
154 std::vector<content::FaviconURL> favicon_urls;
155 favicon_urls.push_back(content::FaviconURL(
156 favicon_url_1, content::FaviconURL::FAVICON, std::vector<gfx::Size>()));
157 // This is duplicated in the favicon urls and should only be downloaded once.
158 favicon_urls.push_back(content::FaviconURL(
159 empty_favicon, content::FaviconURL::FAVICON, std::vector<gfx::Size>()));
160 // Invalid icons shouldn't get put into the download queue.
161 favicon_urls.push_back(
162 content::FaviconURL(GURL("http://www.google.com/invalid.ico"),
163 content::FaviconURL::INVALID_ICON,
164 std::vector<gfx::Size>()));
165 downloader.set_initial_favicon_urls(favicon_urls);
166 downloader.Start();
167 EXPECT_EQ(3u, downloader.pending_requests());
169 std::vector<gfx::Size> sizes_1(1, gfx::Size(16, 16));
170 downloader.CompleteImageDownload(0, favicon_url_1, sizes_1);
172 std::vector<gfx::Size> sizes_2;
173 sizes_2.push_back(gfx::Size(32, 32));
174 sizes_2.push_back(gfx::Size(64, 64));
175 downloader.CompleteImageDownload(1, favicon_url_2, sizes_2);
177 // Only 1 download should have been initiated for |empty_favicon| even though
178 // the URL was in both the web app info and the favicon urls.
179 downloader.CompleteImageDownload(2, empty_favicon, std::vector<gfx::Size>());
180 EXPECT_EQ(0u, downloader.pending_requests());
182 EXPECT_EQ(3u, downloader.favicon_map().size());
183 EXPECT_EQ(0u, downloader.favicon_map()[empty_favicon].size());
184 EXPECT_EQ(1u, downloader.favicon_map()[favicon_url_1].size());
185 EXPECT_EQ(2u, downloader.favicon_map()[favicon_url_2].size());