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
;
17 // Creates valid SkBitmaps of the dimensions found in |sizes| and pushes them
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
,
27 bitmap
.eraseColor(SK_ColorRED
);
32 class FaviconDownloaderTest
: public ChromeRenderViewHostTestHarness
{
34 FaviconDownloaderTest() {
37 virtual ~FaviconDownloaderTest() {
41 DISALLOW_COPY_AND_ASSIGN(FaviconDownloaderTest
);
46 class TestFaviconDownloader
: public FaviconDownloader
{
48 TestFaviconDownloader(content::WebContents
* web_contents
,
49 std::vector
<GURL
> extra_favicon_urls
)
53 base::Bind(&TestFaviconDownloader::DownloadsComplete
,
54 base::Unretained(this))),
57 virtual ~TestFaviconDownloader() {}
59 virtual int DownloadImage(const GURL
& url
) OVERRIDE
{
63 virtual std::vector
<content::FaviconURL
> GetFaviconURLsFromWebContents()
65 return initial_favicon_urls_
;
68 size_t pending_requests() const {
69 return in_progress_requests_
.size();
72 void DownloadsComplete(bool success
,
73 const FaviconDownloader::FaviconMap
& map
) {
77 FaviconDownloader::FaviconMap
favicon_map() const {
81 void CompleteImageDownload(
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
);
89 void UpdateFaviconURLs(const std::vector
<content::FaviconURL
>& candidates
) {
90 FaviconDownloader::DidUpdateFaviconURL(candidates
);
93 void set_initial_favicon_urls(const std::vector
<content::FaviconURL
>& urls
) {
94 initial_favicon_urls_
= urls
;
98 std::vector
<content::FaviconURL
> initial_favicon_urls_
;
99 FaviconDownloader::FaviconMap favicon_map_
;
101 DISALLOW_COPY_AND_ASSIGN(TestFaviconDownloader
);
104 TEST_F(FaviconDownloaderTest
, SimpleDownload
) {
105 const GURL
favicon_url("http://www.google.com/favicon.ico");
106 TestFaviconDownloader
downloader(web_contents(), std::vector
<GURL
>());
108 std::vector
<content::FaviconURL
> favicon_urls
;
109 favicon_urls
.push_back(content::FaviconURL(
110 favicon_url
, content::FaviconURL::FAVICON
, std::vector
<gfx::Size
>()));
111 downloader
.set_initial_favicon_urls(favicon_urls
);
112 EXPECT_EQ(0u, downloader
.pending_requests());
115 EXPECT_EQ(1u, downloader
.pending_requests());
117 std::vector
<gfx::Size
> sizes(1, gfx::Size(32, 32));
118 downloader
.CompleteImageDownload(0, favicon_urls
[0].icon_url
, sizes
);
119 EXPECT_EQ(0u, downloader
.pending_requests());
121 EXPECT_EQ(1u, downloader
.favicon_map().size());
122 EXPECT_EQ(1u, downloader
.favicon_map()[favicon_url
].size());
125 TEST_F(FaviconDownloaderTest
, DownloadWithUrlsFromWebContentsNotification
) {
126 const GURL
favicon_url("http://www.google.com/favicon.ico");
127 TestFaviconDownloader
downloader(web_contents(), std::vector
<GURL
>());
129 std::vector
<content::FaviconURL
> favicon_urls
;
130 favicon_urls
.push_back(content::FaviconURL(
131 favicon_url
, content::FaviconURL::FAVICON
, std::vector
<gfx::Size
>()));
132 EXPECT_EQ(0u, downloader
.pending_requests());
134 // Start downloader before favicon URLs are loaded.
136 EXPECT_EQ(0u, downloader
.pending_requests());
138 downloader
.UpdateFaviconURLs(favicon_urls
);
139 EXPECT_EQ(1u, downloader
.pending_requests());
141 std::vector
<gfx::Size
> sizes(1, gfx::Size(32, 32));
142 downloader
.CompleteImageDownload(0, favicon_urls
[0].icon_url
, sizes
);
143 EXPECT_EQ(0u, downloader
.pending_requests());
145 EXPECT_EQ(1u, downloader
.favicon_map().size());
146 EXPECT_EQ(1u, downloader
.favicon_map()[favicon_url
].size());
149 TEST_F(FaviconDownloaderTest
, DownloadMultipleUrls
) {
150 const GURL
empty_favicon("http://www.google.com/empty_favicon.ico");
151 const GURL
favicon_url_1("http://www.google.com/favicon.ico");
152 const GURL
favicon_url_2("http://www.google.com/favicon2.ico");
154 std::vector
<GURL
> extra_urls
;
155 // This should get downloaded.
156 extra_urls
.push_back(favicon_url_2
);
157 // This is duplicated in the favicon urls and should only be downloaded once.
158 extra_urls
.push_back(empty_favicon
);
160 TestFaviconDownloader
downloader(web_contents(), extra_urls
);
161 std::vector
<content::FaviconURL
> favicon_urls
;
162 favicon_urls
.push_back(content::FaviconURL(
163 favicon_url_1
, content::FaviconURL::FAVICON
, std::vector
<gfx::Size
>()));
164 // This is duplicated in the favicon urls and should only be downloaded once.
165 favicon_urls
.push_back(content::FaviconURL(
166 empty_favicon
, content::FaviconURL::FAVICON
, std::vector
<gfx::Size
>()));
167 // Invalid icons shouldn't get put into the download queue.
168 favicon_urls
.push_back(
169 content::FaviconURL(GURL("http://www.google.com/invalid.ico"),
170 content::FaviconURL::INVALID_ICON
,
171 std::vector
<gfx::Size
>()));
172 downloader
.set_initial_favicon_urls(favicon_urls
);
174 EXPECT_EQ(3u, downloader
.pending_requests());
176 std::vector
<gfx::Size
> sizes_1(1, gfx::Size(16, 16));
177 downloader
.CompleteImageDownload(0, favicon_url_1
, sizes_1
);
179 std::vector
<gfx::Size
> sizes_2
;
180 sizes_2
.push_back(gfx::Size(32, 32));
181 sizes_2
.push_back(gfx::Size(64, 64));
182 downloader
.CompleteImageDownload(1, favicon_url_2
, sizes_2
);
184 // Only 1 download should have been initiated for |empty_favicon| even though
185 // the URL was in both the web app info and the favicon urls.
186 downloader
.CompleteImageDownload(2, empty_favicon
, std::vector
<gfx::Size
>());
187 EXPECT_EQ(0u, downloader
.pending_requests());
189 EXPECT_EQ(3u, downloader
.favicon_map().size());
190 EXPECT_EQ(0u, downloader
.favicon_map()[empty_favicon
].size());
191 EXPECT_EQ(1u, downloader
.favicon_map()[favicon_url_1
].size());
192 EXPECT_EQ(2u, downloader
.favicon_map()[favicon_url_2
].size());