MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / chrome / browser / extensions / favicon_downloader_unittest.cc
blob8d700cca97b42a658753ff490cbfb808669217d3
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 virtual ~FaviconDownloaderTest() {
37 private:
38 DISALLOW_COPY_AND_ASSIGN(FaviconDownloaderTest);
41 } // namespace
43 class TestFaviconDownloader : public FaviconDownloader {
44 public:
45 TestFaviconDownloader(content::WebContents* web_contents,
46 std::vector<GURL> extra_favicon_urls)
47 : FaviconDownloader(
48 web_contents,
49 extra_favicon_urls,
50 base::Bind(&TestFaviconDownloader::DownloadsComplete,
51 base::Unretained(this))),
52 id_counter_(0) {
54 ~TestFaviconDownloader() override {}
56 int DownloadImage(const GURL& url) override { return id_counter_++; }
58 std::vector<content::FaviconURL> GetFaviconURLsFromWebContents() override {
59 return initial_favicon_urls_;
62 size_t pending_requests() const {
63 return in_progress_requests_.size();
66 void DownloadsComplete(bool success,
67 const FaviconDownloader::FaviconMap& map) {
68 favicon_map_ = map;
71 FaviconDownloader::FaviconMap favicon_map() const {
72 return favicon_map_;
75 void CompleteImageDownload(
76 int id,
77 const GURL& image_url,
78 const std::vector<gfx::Size>& original_bitmap_sizes) {
79 FaviconDownloader::DidDownloadFavicon(id, 200, image_url,
80 CreateTestBitmaps(original_bitmap_sizes), original_bitmap_sizes);
83 void UpdateFaviconURLs(const std::vector<content::FaviconURL>& candidates) {
84 FaviconDownloader::DidUpdateFaviconURL(candidates);
87 void set_initial_favicon_urls(const std::vector<content::FaviconURL>& urls) {
88 initial_favicon_urls_ = urls;
91 private:
92 std::vector<content::FaviconURL> initial_favicon_urls_;
93 FaviconDownloader::FaviconMap favicon_map_;
94 int id_counter_;
95 DISALLOW_COPY_AND_ASSIGN(TestFaviconDownloader);
98 TEST_F(FaviconDownloaderTest, SimpleDownload) {
99 const GURL favicon_url("http://www.google.com/favicon.ico");
100 TestFaviconDownloader downloader(web_contents(), std::vector<GURL>());
102 std::vector<content::FaviconURL> favicon_urls;
103 favicon_urls.push_back(content::FaviconURL(
104 favicon_url, content::FaviconURL::FAVICON, std::vector<gfx::Size>()));
105 downloader.set_initial_favicon_urls(favicon_urls);
106 EXPECT_EQ(0u, downloader.pending_requests());
108 downloader.Start();
109 EXPECT_EQ(1u, downloader.pending_requests());
111 std::vector<gfx::Size> sizes(1, gfx::Size(32, 32));
112 downloader.CompleteImageDownload(0, favicon_urls[0].icon_url, sizes);
113 EXPECT_EQ(0u, downloader.pending_requests());
115 EXPECT_EQ(1u, downloader.favicon_map().size());
116 EXPECT_EQ(1u, downloader.favicon_map()[favicon_url].size());
119 TEST_F(FaviconDownloaderTest, DownloadWithUrlsFromWebContentsNotification) {
120 const GURL favicon_url("http://www.google.com/favicon.ico");
121 TestFaviconDownloader downloader(web_contents(), std::vector<GURL>());
123 std::vector<content::FaviconURL> favicon_urls;
124 favicon_urls.push_back(content::FaviconURL(
125 favicon_url, content::FaviconURL::FAVICON, std::vector<gfx::Size>()));
126 EXPECT_EQ(0u, downloader.pending_requests());
128 // Start downloader before favicon URLs are loaded.
129 downloader.Start();
130 EXPECT_EQ(0u, downloader.pending_requests());
132 downloader.UpdateFaviconURLs(favicon_urls);
133 EXPECT_EQ(1u, downloader.pending_requests());
135 std::vector<gfx::Size> sizes(1, gfx::Size(32, 32));
136 downloader.CompleteImageDownload(0, favicon_urls[0].icon_url, sizes);
137 EXPECT_EQ(0u, downloader.pending_requests());
139 EXPECT_EQ(1u, downloader.favicon_map().size());
140 EXPECT_EQ(1u, downloader.favicon_map()[favicon_url].size());
143 TEST_F(FaviconDownloaderTest, DownloadMultipleUrls) {
144 const GURL empty_favicon("http://www.google.com/empty_favicon.ico");
145 const GURL favicon_url_1("http://www.google.com/favicon.ico");
146 const GURL favicon_url_2("http://www.google.com/favicon2.ico");
148 std::vector<GURL> extra_urls;
149 // This should get downloaded.
150 extra_urls.push_back(favicon_url_2);
151 // This is duplicated in the favicon urls and should only be downloaded once.
152 extra_urls.push_back(empty_favicon);
154 TestFaviconDownloader downloader(web_contents(), extra_urls);
155 std::vector<content::FaviconURL> favicon_urls;
156 favicon_urls.push_back(content::FaviconURL(
157 favicon_url_1, content::FaviconURL::FAVICON, std::vector<gfx::Size>()));
158 // This is duplicated in the favicon urls and should only be downloaded once.
159 favicon_urls.push_back(content::FaviconURL(
160 empty_favicon, content::FaviconURL::FAVICON, std::vector<gfx::Size>()));
161 // Invalid icons shouldn't get put into the download queue.
162 favicon_urls.push_back(
163 content::FaviconURL(GURL("http://www.google.com/invalid.ico"),
164 content::FaviconURL::INVALID_ICON,
165 std::vector<gfx::Size>()));
166 downloader.set_initial_favicon_urls(favicon_urls);
167 downloader.Start();
168 EXPECT_EQ(3u, downloader.pending_requests());
170 std::vector<gfx::Size> sizes_1(1, gfx::Size(16, 16));
171 downloader.CompleteImageDownload(0, favicon_url_1, sizes_1);
173 std::vector<gfx::Size> sizes_2;
174 sizes_2.push_back(gfx::Size(32, 32));
175 sizes_2.push_back(gfx::Size(64, 64));
176 downloader.CompleteImageDownload(1, favicon_url_2, sizes_2);
178 // Only 1 download should have been initiated for |empty_favicon| even though
179 // the URL was in both the web app info and the favicon urls.
180 downloader.CompleteImageDownload(2, empty_favicon, std::vector<gfx::Size>());
181 EXPECT_EQ(0u, downloader.pending_requests());
183 EXPECT_EQ(3u, downloader.favicon_map().size());
184 EXPECT_EQ(0u, downloader.favicon_map()[empty_favicon].size());
185 EXPECT_EQ(1u, downloader.favicon_map()[favicon_url_1].size());
186 EXPECT_EQ(2u, downloader.favicon_map()[favicon_url_2].size());