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(0, 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(
110 content::FaviconURL(favicon_url
,
111 content::FaviconURL::FAVICON
));
112 downloader
.set_initial_favicon_urls(favicon_urls
);
113 EXPECT_EQ(0u, downloader
.pending_requests());
116 EXPECT_EQ(1u, downloader
.pending_requests());
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());
122 EXPECT_EQ(1u, downloader
.favicon_map().size());
123 EXPECT_EQ(1u, downloader
.favicon_map()[favicon_url
].size());
126 TEST_F(FaviconDownloaderTest
, DownloadWithUrlsFromWebContentsNotification
) {
127 const GURL
favicon_url("http://www.google.com/favicon.ico");
128 TestFaviconDownloader
downloader(web_contents(), std::vector
<GURL
>());
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());
136 // Start downloader before favicon URLs are loaded.
138 EXPECT_EQ(0u, downloader
.pending_requests());
140 downloader
.UpdateFaviconURLs(favicon_urls
);
141 EXPECT_EQ(1u, downloader
.pending_requests());
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());
147 EXPECT_EQ(1u, downloader
.favicon_map().size());
148 EXPECT_EQ(1u, downloader
.favicon_map()[favicon_url
].size());
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");
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
);
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
);
177 EXPECT_EQ(3u, downloader
.pending_requests());
179 std::vector
<gfx::Size
> sizes_1(1, gfx::Size(16, 16));
180 downloader
.CompleteImageDownload(0, favicon_url_1
, sizes_1
);
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
);
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());
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());