Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: components/favicon/core/favicon_handler_unittest.cc

Issue 2703603002: Improve FaviconHandler tests by testing public API (Closed)
Patch Set: Reverted injecting scale factors. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/favicon/core/favicon_handler.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/favicon/core/favicon_handler.h" 5 #include "components/favicon/core/favicon_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h"
17 #include "base/strings/stringprintf.h"
18 #include "components/favicon/core/favicon_client.h"
pkotwicz 2017/03/17 05:43:11 I don't think that you need this import?
mastiz 2017/03/17 12:17:31 Removed.
14 #include "components/favicon/core/favicon_driver.h" 19 #include "components/favicon/core/favicon_driver.h"
20 #include "components/favicon/core/test/mock_favicon_service.h"
21 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/skia/include/core/SkBitmap.h" 23 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/base/layout.h" 24 #include "ui/base/layout.h"
18 #include "ui/gfx/codec/png_codec.h" 25 #include "ui/gfx/codec/png_codec.h"
19 #include "ui/gfx/favicon_size.h" 26 #include "ui/gfx/favicon_size.h"
20 #include "ui/gfx/image/image.h" 27 #include "ui/gfx/image/image.h"
21 28
22 namespace favicon { 29 namespace favicon {
23 namespace { 30 namespace {
24 31
25 // Fill the given bmp with valid png data. 32 using favicon_base::FAVICON;
26 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { 33 using favicon_base::FaviconRawBitmapResult;
27 bmp->allocN32Pixels(w, h); 34 using favicon_base::TOUCH_ICON;
35 using favicon_base::TOUCH_PRECOMPOSED_ICON;
36 using testing::AnyNumber;
37 using testing::Assign;
38 using testing::AtLeast;
39 using testing::AtMost;
pkotwicz 2017/03/17 05:43:12 This doesn't seem to be used. :)
mastiz 2017/03/17 12:17:31 Done.
40 using testing::ElementsAre;
41 using testing::InSequence;
42 using testing::Invoke;
43 using testing::IsEmpty;
44 using testing::Return;
45 using testing::_;
46
47 using IntVector = std::vector<int>;
48 using URLVector = std::vector<GURL>;
49 using BitmapVector = std::vector<SkBitmap>;
50 using SizeVector = std::vector<gfx::Size>;
51
52 MATCHER_P2(ImageSizeIs, width, height, "") {
53 *result_listener << "where size is " << arg.Width() << "x" << arg.Height();
54 return arg.Size() == gfx::Size(width, height);
55 }
56
57 // Fill the given bmp with some test data.
58 SkBitmap CreateBitmap(int w, int h) {
59 SkBitmap bmp;
60 bmp.allocN32Pixels(w, h);
28 61
29 unsigned char* src_data = 62 unsigned char* src_data =
30 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0)); 63 reinterpret_cast<unsigned char*>(bmp.getAddr32(0, 0));
31 for (int i = 0; i < w * h; i++) { 64 for (int i = 0; i < w * h; i++) {
32 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); 65 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255);
33 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); 66 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255);
34 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255); 67 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255);
35 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255); 68 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255);
36 } 69 }
70 return bmp;
37 } 71 }
38 72
39 // Fill the given data buffer with valid png data. 73 // Fill the given data buffer with valid png data.
40 void FillBitmap(int w, int h, std::vector<unsigned char>* output) { 74 void FillBitmap(int w, int h, std::vector<unsigned char>* output) {
41 SkBitmap bitmap; 75 SkBitmap bitmap = CreateBitmap(w, h);
42 FillDataToBitmap(w, h, &bitmap);
43 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output); 76 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output);
44 } 77 }
45 78
46 void SetFaviconRawBitmapResult( 79 std::vector<gfx::Size> CreateSquareSizes(const IntVector& sizes) {
pkotwicz 2017/03/17 05:43:11 There are two callers of this method. There is onl
mastiz 2017/03/17 12:17:31 Done. Since you seem to be concerned about the num
80 std::vector<gfx::Size> result;
81 for (int size : sizes) {
82 result.emplace_back(size, size);
83 }
84 return result;
85 }
86
87 std::vector<SkBitmap> CreateBitmaps(const std::vector<gfx::Size>& sizes) {
88 std::vector<SkBitmap> bitmaps;
89 for (const gfx::Size& size : sizes) {
90 bitmaps.push_back(CreateBitmap(size.width(), size.height()));
91 }
92 return bitmaps;
93 }
94
95 std::vector<FaviconRawBitmapResult> CreateRawBitmapResult(
47 const GURL& icon_url, 96 const GURL& icon_url,
48 favicon_base::IconType icon_type, 97 favicon_base::IconType icon_type = FAVICON,
49 bool expired, 98 bool expired = false) {
50 std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results) {
51 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); 99 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
52 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); 100 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data());
53 favicon_base::FaviconRawBitmapResult bitmap_result; 101 FaviconRawBitmapResult bitmap_result;
54 bitmap_result.expired = expired; 102 bitmap_result.expired = expired;
55 bitmap_result.bitmap_data = data; 103 bitmap_result.bitmap_data = data;
56 // Use a pixel size other than (0,0) as (0,0) has a special meaning. 104 // Use a pixel size other than (0,0) as (0,0) has a special meaning.
57 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); 105 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize);
58 bitmap_result.icon_type = icon_type; 106 bitmap_result.icon_type = icon_type;
59 bitmap_result.icon_url = icon_url; 107 bitmap_result.icon_url = icon_url;
60 108 return {bitmap_result};
61 favicon_bitmap_results->push_back(bitmap_result); 109 }
62 } 110
63 111 // Fake that implements the calls to FaviconHalder::Delegate's DownloadImage(),
64 void SetFaviconRawBitmapResult( 112 // delegated to this class through MockDelegate.
65 const GURL& icon_url, 113 class FakeImageDownloader {
66 std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results) {
67 SetFaviconRawBitmapResult(icon_url,
68 favicon_base::FAVICON,
69 false /* expired */,
70 favicon_bitmap_results);
71 }
72
73 // This class is used to save the download request for verifying with test case.
74 class DownloadHandler {
75 public: 114 public:
76 DownloadHandler() : callback_invoked_(false) {} 115 struct Response {
77 ~DownloadHandler() {} 116 int http_status_code = 404;
78 117 BitmapVector bitmaps;
79 void Reset() { 118 SizeVector original_bitmap_sizes;
80 download_.reset(); 119 };
81 callback_invoked_ = false; 120
82 // Does not affect |should_fail_download_icon_urls_| and 121 // Implementation of FaviconHalder::Delegate's DownloadImage(). If a given
83 // |failed_download_icon_urls_|. 122 // URL is not known (i.e. not previously added via Add()), it produces 404s.
84 } 123 int DownloadImage(const GURL& url,
85 124 int max_image_size,
86 // Make downloads for any of |icon_urls| fail. 125 FaviconHandler::Delegate::ImageDownloadCallback callback) {
87 void FailDownloadForIconURLs(const std::set<GURL>& icon_urls) { 126 downloads_.push_back(url);
88 should_fail_download_icon_urls_ = icon_urls; 127
89 } 128 const Response& response = responses_[url];
90 129 int download_id = static_cast<int>(downloads_.size());
pkotwicz 2017/03/17 05:43:12 The download id is affected by ClearDownloads() :(
mastiz 2017/03/17 12:17:32 Done.
91 // Returns whether a download for |icon_url| did fail. 130 base::Closure bound_callback =
92 bool DidFailDownloadForIconURL(const GURL& icon_url) const { 131 base::Bind(callback, download_id, response.http_status_code, url,
93 return failed_download_icon_urls_.count(icon_url); 132 response.bitmaps, response.original_bitmap_sizes);
94 } 133 if (url == manual_callback_url_)
95 134 manual_callback_ = bound_callback;
96 void AddDownload(int download_id, 135 else
97 const GURL& image_url, 136 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, bound_callback);
98 const std::vector<int>& image_sizes, 137 return download_id;
138 }
139
140 void Add(const GURL& icon_url, const IntVector& sizes) {
141 AddWithOriginalSizes(icon_url, sizes, sizes);
142 }
143
144 void AddWithOriginalSizes(const GURL& icon_url,
145 const IntVector& sizes,
146 const IntVector& original_sizes) {
147 DCHECK_EQ(sizes.size(), original_sizes.size());
148 Response response;
149 response.http_status_code = 200;
150 response.original_bitmap_sizes = CreateSquareSizes(original_sizes);
151 response.bitmaps = CreateBitmaps(CreateSquareSizes(sizes));
152 responses_[icon_url] = response;
153 }
154
155 void AddError(const GURL& icon_url, int http_status_code) {
156 Response response;
157 response.http_status_code = http_status_code;
158 responses_[icon_url] = response;
159 }
160
161 // Disables automatic callback for |url|. This is useful for emulating a
162 // download taking a long time. The callback will for DownloadImage() will be
pkotwicz 2017/03/17 05:43:11 Nit: "callback will for" -> "callback for"
mastiz 2017/03/17 12:17:32 Done.
163 // stored in |manual_callback_|.
164 void SetRunCallbackManuallyForUrl(const GURL& url) {
165 manual_callback_url_ = url;
166 }
167
168 // Returns whether an ongoing download exists for a url previously selected
169 // via SetRunCallbackManuallyForUrl().
170 bool HasPendingManualCallback() { return !manual_callback_.is_null(); }
171
172 // Triggers the response for a download previously selected for manual
173 // triggering via SetRunCallbackManuallyForUrl().
174 bool RunCallbackManually() {
175 if (!HasPendingManualCallback())
176 return false;
177 manual_callback_.Run();
pkotwicz 2017/03/17 05:43:12 In order for HasPendingManualCallback() to return
mastiz 2017/03/17 12:17:32 Done. Crashing tests in failed expectations is not
178 return true;
179 }
180
181 // Returns pending and completed download URLs.
182 const URLVector& downloads() const { return downloads_; }
183
184 void ClearDownloads() { downloads_.clear(); }
185
186 private:
187 // URL to disable automatic callbacks for.
188 GURL manual_callback_url_;
189
190 // Pending and completed download URLs.
191 URLVector downloads_;
192
193 // Callback for DownloadImage() request for |manual_callback_url_|.
pkotwicz 2017/03/17 05:43:12 Nit: Group |manual_callback_url_| and |manual_call
mastiz 2017/03/17 12:17:32 Done.
194 base::Closure manual_callback_;
195
196 // Registered images.
pkotwicz 2017/03/17 05:43:12 How about: "Registered responses."
mastiz 2017/03/17 12:17:32 Done.
197 std::map<GURL, Response> responses_;
pkotwicz 2017/03/17 05:43:11 DISALLOW_COPY_AND_ASSIGN(FakeImageDownloader)
mastiz 2017/03/17 12:17:32 Done, although I don't think this buys anything on
198 };
199
200 class MockDelegate : public FaviconHandler::Delegate {
201 public:
202 MockDelegate() {
203 // Delegate image downloading to FakeImageDownloader.
204 ON_CALL(*this, DownloadImage(_, _, _))
205 .WillByDefault(
206 Invoke(&fake_downloader_, &FakeImageDownloader::DownloadImage));
207 }
208
209 MOCK_METHOD3(DownloadImage,
210 int(const GURL& url,
99 int max_image_size, 211 int max_image_size,
100 FaviconHandler::Delegate::ImageDownloadCallback callback) { 212 ImageDownloadCallback callback));
101 download_.reset(new Download(download_id, image_url, image_sizes, 213 MOCK_METHOD0(IsOffTheRecord, bool());
102 max_image_size, callback)); 214 MOCK_METHOD1(IsBookmarked, bool(const GURL& url));
103 } 215 MOCK_METHOD5(OnFaviconUpdated,
104 216 void(const GURL& page_url,
105 void InvokeCallback(); 217 FaviconDriverObserver::NotificationIconType type,
106 218 const GURL& icon_url,
107 bool HasDownload() const { return download_.get(); } 219 bool icon_url_changed,
108 const GURL& GetImageUrl() const { return download_->image_url; } 220 const gfx::Image& image));
109 void SetImageSizes(const std::vector<int>& sizes) { 221
110 download_->image_sizes = sizes; } 222 FakeImageDownloader& fake_downloader() { return fake_downloader_; }
223
224 // Convenience getter for test readability. Returns pending and completed
225 // download URLs.
226 const URLVector& downloads() const { return fake_downloader_.downloads(); }
111 227
112 private: 228 private:
113 struct Download { 229 FakeImageDownloader fake_downloader_;
114 Download(int id,
115 GURL url,
116 const std::vector<int>& sizes,
117 int max_size,
118 FaviconHandler::Delegate::ImageDownloadCallback callback)
119 : download_id(id),
120 image_url(url),
121 image_sizes(sizes),
122 max_image_size(max_size),
123 callback(callback) {}
124 ~Download() {}
125 int download_id;
126 GURL image_url;
127 std::vector<int> image_sizes;
128 int max_image_size;
129 FaviconHandler::Delegate::ImageDownloadCallback callback;
130 };
131
132 std::unique_ptr<Download> download_;
133 bool callback_invoked_;
134
135 // The icon URLs for which the download should fail.
136 std::set<GURL> should_fail_download_icon_urls_;
137
138 // The icon URLs for which the download did fail. This should be a subset of
139 // |should_fail_download_icon_urls_|.
140 std::set<GURL> failed_download_icon_urls_;
141
142 DISALLOW_COPY_AND_ASSIGN(DownloadHandler);
143 }; 230 };
144 231
145 // This class is used to save the history request for verifying with test case. 232 // FakeFaviconService mimics a FaviconService backend that allows setting up
146 // It also will be used to simulate the history response. 233 // test data stored via Store(). If Store() has not been called for a
147 class HistoryRequestHandler { 234 // particular URL, the callback is called with empty database results.
235 class FakeFaviconService {
148 public: 236 public:
149 HistoryRequestHandler(const GURL& page_url, 237 // Stores favicon with bitmap data in |results| at |page_url| and |icon_url|.
150 const GURL& icon_url, 238 void Store(const GURL& page_url,
151 int icon_type, 239 const GURL& icon_url,
152 const favicon_base::FaviconResultsCallback& callback) 240 const std::vector<favicon_base::FaviconRawBitmapResult>& result) {
153 : page_url_(page_url), 241 results_[icon_url] = result;
154 icon_url_(icon_url), 242 results_[page_url] = result;
155 icon_type_(icon_type), 243 }
156 callback_(callback) { 244
157 } 245 // Returns pending and completed database request URLs.
158 246 const URLVector& db_requests() const { return db_requests_; }
159 HistoryRequestHandler(const GURL& page_url, 247
160 const GURL& icon_url, 248 void ClearDbRequests() { db_requests_.clear(); }
161 int icon_type, 249
162 const std::vector<unsigned char>& bitmap_data, 250 base::CancelableTaskTracker::TaskId GetFavicon(
163 const gfx::Size& size)
164 : page_url_(page_url),
165 icon_url_(icon_url),
166 icon_type_(icon_type),
167 bitmap_data_(bitmap_data),
168 size_(size) {
169 }
170
171 ~HistoryRequestHandler() {}
172 void InvokeCallback();
173
174 const GURL page_url_;
175 const GURL icon_url_;
176 const int icon_type_;
177 const std::vector<unsigned char> bitmap_data_;
178 const gfx::Size size_;
179 std::vector<favicon_base::FaviconRawBitmapResult> history_results_;
180 favicon_base::FaviconResultsCallback callback_;
181
182 private:
183 DISALLOW_COPY_AND_ASSIGN(HistoryRequestHandler);
184 };
185
186 class TestDelegate : public FaviconHandler::Delegate {
187 public:
188 TestDelegate() : num_notifications_(0), download_id_(0) {}
189
190 int DownloadImage(const GURL& image_url,
191 int max_bitmap_size,
192 ImageDownloadCallback callback) override {
193 // Do not do a download if downloading |image_url| failed previously. This
194 // emulates the behavior of FaviconDriver::DownloadImage()
195 if (download_handler_.DidFailDownloadForIconURL(image_url)) {
196 download_handler_.AddDownload(download_id_, image_url, std::vector<int>(),
197 0, callback);
198 return 0;
199 }
200
201 download_id_++;
202 std::vector<int> sizes;
203 sizes.push_back(0);
204 download_handler_.AddDownload(download_id_, image_url, sizes,
205 max_bitmap_size, callback);
206 return download_id_;
207 }
208
209 bool IsOffTheRecord() override { return false; }
210
211 bool IsBookmarked(const GURL& url) override { return false; }
212
213 void OnFaviconUpdated(
214 const GURL& page_url,
215 FaviconDriverObserver::NotificationIconType notification_icon_type,
216 const GURL& icon_url,
217 bool icon_url_changed,
218 const gfx::Image& image) override {
219 ++num_notifications_;
220 icon_url_ = icon_url;
221 image_ = image;
222 }
223
224 DownloadHandler* download_handler() { return &download_handler_; }
225 const GURL& icon_url() const { return icon_url_; }
226 const gfx::Image& image() const { return image_; }
227 size_t num_notifications() const { return num_notifications_; }
228 void ResetNumNotifications() { num_notifications_ = 0; }
229
230 private:
231 GURL icon_url_;
232 gfx::Image image_;
233 size_t num_notifications_;
234
235 // The unique id of a download request. It will be returned to a
236 // FaviconHandler.
237 int download_id_;
238
239 DownloadHandler download_handler_;
240
241 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
242 };
243
244 } // namespace
245
246 // This class is used to catch the FaviconHandler's download and history
247 // request, and also provide the methods to access the FaviconHandler
248 // internals.
249 class TestFaviconHandler : public FaviconHandler {
250 public:
251 static int GetMaximalIconSize(favicon_base::IconType icon_type) {
252 return FaviconHandler::GetMaximalIconSize(icon_type);
253 }
254
255 TestFaviconHandler(FaviconHandler::Delegate* delegate,
256 FaviconDriverObserver::NotificationIconType handler_type)
257 : FaviconHandler(nullptr, delegate, handler_type) {}
258
259 ~TestFaviconHandler() override {}
260
261 HistoryRequestHandler* history_handler() {
262 return history_handler_.get();
263 }
264
265 // This method will take the ownership of the given handler.
266 void set_history_handler(HistoryRequestHandler* handler) {
267 history_handler_.reset(handler);
268 }
269
270 FaviconURL* current_candidate() {
271 return FaviconHandler::current_candidate();
272 }
273
274 size_t current_candidate_index() const {
275 return current_candidate_index_;
276 }
277
278 const FaviconCandidate& best_favicon_candidate() {
279 return best_favicon_candidate_;
280 }
281
282 protected:
283 void UpdateFaviconMappingAndFetch(
284 const GURL& page_url,
285 const GURL& icon_url, 251 const GURL& icon_url,
286 favicon_base::IconType icon_type, 252 favicon_base::IconType icon_type,
253 int desired_size_in_dip,
287 const favicon_base::FaviconResultsCallback& callback, 254 const favicon_base::FaviconResultsCallback& callback,
288 base::CancelableTaskTracker* tracker) override { 255 base::CancelableTaskTracker* tracker) {
289 history_handler_.reset(new HistoryRequestHandler(page_url, icon_url, 256 return GetFaviconForPageOrIconURL(icon_url, callback, tracker);
290 icon_type, callback)); 257 }
291 } 258
292 259 base::CancelableTaskTracker::TaskId GetFaviconForPageURL(
293 void GetFaviconFromFaviconService(
294 const GURL& icon_url,
295 favicon_base::IconType icon_type,
296 const favicon_base::FaviconResultsCallback& callback,
297 base::CancelableTaskTracker* tracker) override {
298 history_handler_.reset(new HistoryRequestHandler(GURL(), icon_url,
299 icon_type, callback));
300 }
301
302 void GetFaviconForURLFromFaviconService(
303 const GURL& page_url, 260 const GURL& page_url,
304 int icon_types, 261 int icon_types,
262 int desired_size_in_dip,
305 const favicon_base::FaviconResultsCallback& callback, 263 const favicon_base::FaviconResultsCallback& callback,
306 base::CancelableTaskTracker* tracker) override { 264 base::CancelableTaskTracker* tracker) {
307 history_handler_.reset(new HistoryRequestHandler(page_url, GURL(), 265 return GetFaviconForPageOrIconURL(page_url, callback, tracker);
308 icon_types, callback)); 266 }
309 } 267
310 268 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch(
311 void SetHistoryFavicons(const GURL& page_url, 269 const GURL& page_url,
312 const GURL& icon_url, 270 const std::vector<GURL>& icon_urls,
313 favicon_base::IconType icon_type, 271 int icon_types,
314 const gfx::Image& image) override { 272 int desired_size_in_dip,
315 scoped_refptr<base::RefCountedMemory> bytes = image.As1xPNGBytes(); 273 const favicon_base::FaviconResultsCallback& callback,
316 std::vector<unsigned char> bitmap_data(bytes->front(), 274 base::CancelableTaskTracker* tracker) {
317 bytes->front() + bytes->size()); 275 CHECK_EQ(1U, icon_urls.size()) << "Multi-icon lookup not implemented";
318 history_handler_.reset(new HistoryRequestHandler( 276 return GetFaviconForPageOrIconURL(icon_urls.front(), callback, tracker);
319 page_url, icon_url, icon_type, bitmap_data, image.Size())); 277 }
320 }
321
322 bool ShouldSaveFavicon() override { return true; }
323
324 GURL page_url_;
325 278
326 private: 279 private:
327 std::unique_ptr<HistoryRequestHandler> history_handler_; 280 base::CancelableTaskTracker::TaskId GetFaviconForPageOrIconURL(
328 281 const GURL& page_or_icon_url,
329 DISALLOW_COPY_AND_ASSIGN(TestFaviconHandler); 282 const favicon_base::FaviconResultsCallback& callback,
283 base::CancelableTaskTracker* tracker) {
284 db_requests_.push_back(page_or_icon_url);
285
286 return tracker->PostTask(base::ThreadTaskRunnerHandle::Get().get(),
287 FROM_HERE,
288 base::Bind(callback, results_[page_or_icon_url]));
289 }
290
291 std::map<GURL, std::vector<favicon_base::FaviconRawBitmapResult>> results_;
292 URLVector db_requests_;
pkotwicz 2017/03/17 05:43:12 DISALLOW_COPY_AND_ASSIGN(FakeFaviconService)
mastiz 2017/03/17 12:17:31 Done, same here.
330 }; 293 };
331 294
332 namespace { 295 // MockFaviconService subclass that delegates DB reads to FakeFaviconService.
333 296 class MockFaviconServiceWithFake : public MockFaviconService {
334 void HistoryRequestHandler::InvokeCallback() { 297 public:
335 if (!callback_.is_null()) { 298 MockFaviconServiceWithFake() {
336 callback_.Run(history_results_); 299 // Delegate the various methods that read from the DB.
337 } 300 ON_CALL(*this, GetFavicon(_, _, _, _, _))
338 } 301 .WillByDefault(Invoke(&fake_, &FakeFaviconService::GetFavicon));
339 302 ON_CALL(*this, GetFaviconForPageURL(_, _, _, _, _))
340 void DownloadHandler::InvokeCallback() { 303 .WillByDefault(
341 if (callback_invoked_) 304 Invoke(&fake_, &FakeFaviconService::GetFaviconForPageURL));
342 return; 305 ON_CALL(*this, UpdateFaviconMappingsAndFetch(_, _, _, _, _, _))
343 306 .WillByDefault(
344 std::vector<gfx::Size> original_bitmap_sizes; 307 Invoke(&fake_, &FakeFaviconService::UpdateFaviconMappingsAndFetch));
345 std::vector<SkBitmap> bitmaps; 308 }
346 if (should_fail_download_icon_urls_.count(download_->image_url)) { 309
347 failed_download_icon_urls_.insert(download_->image_url); 310 FakeFaviconService* fake() { return &fake_; }
348 } else { 311
349 for (std::vector<int>::const_iterator i = download_->image_sizes.begin(); 312 private:
350 i != download_->image_sizes.end(); ++i) { 313 FakeFaviconService fake_;
351 int original_size = (*i > 0) ? *i : gfx::kFaviconSize; 314
352 int downloaded_size = original_size; 315 DISALLOW_COPY_AND_ASSIGN(MockFaviconServiceWithFake);
353 if (download_->max_image_size != 0 && 316 };
354 downloaded_size > download_->max_image_size) {
355 downloaded_size = download_->max_image_size;
356 }
357 SkBitmap bitmap;
358 FillDataToBitmap(downloaded_size, downloaded_size, &bitmap);
359 bitmaps.push_back(bitmap);
360 original_bitmap_sizes.push_back(gfx::Size(original_size, original_size));
361 }
362 }
363 download_->callback.Run(download_->download_id,
364 /*=status_code=*/200, download_->image_url, bitmaps,
365 original_bitmap_sizes);
366 callback_invoked_ = true;
367 }
368 317
369 class FaviconHandlerTest : public testing::Test { 318 class FaviconHandlerTest : public testing::Test {
370 protected: 319 protected:
320 const std::vector<gfx::Size> kEmptySizes;
321
322 // Some known icons for which download will succeed.
323 const GURL kPageURL = GURL("http://www.google.com");
324 const GURL kIconURL10x10 = GURL("http://www.google.com/favicon10x10");
325 const GURL kIconURL12x12 = GURL("http://www.google.com/favicon12x12");
326 const GURL kIconURL16x16 = GURL("http://www.google.com/favicon16x16");
327 const GURL kIconURL64x64 = GURL("http://www.google.com/favicon64x64");
328
371 FaviconHandlerTest() { 329 FaviconHandlerTest() {
372 } 330 // Register various known icon URLs.
373 331 delegate_.fake_downloader().Add(kIconURL10x10, IntVector{10});
374 ~FaviconHandlerTest() override {} 332 delegate_.fake_downloader().Add(kIconURL12x12, IntVector{12});
375 333 delegate_.fake_downloader().Add(kIconURL16x16, IntVector{16});
376 // Simulates requesting a favicon for |page_url| given: 334 delegate_.fake_downloader().Add(kIconURL64x64, IntVector{64});
377 // - We have not previously cached anything in history for |page_url| or for 335
378 // any of |candidates|.
379 // - The page provides favicons at |candidate_icons|.
380 // - The favicons at |candidate_icons| have edge pixel sizes of
381 // |candidate_icon_sizes|.
382 void DownloadTillDoneIgnoringHistory(
383 TestDelegate* delegate,
384 TestFaviconHandler* favicon_handler,
385 const GURL& page_url,
386 const std::vector<FaviconURL>& candidate_icons,
387 const int* candidate_icon_sizes) {
388 size_t old_num_notifications = delegate->num_notifications();
389
390 UpdateFaviconURL(delegate, favicon_handler, page_url, candidate_icons);
391 EXPECT_EQ(candidate_icons.size(), favicon_handler->image_urls().size());
392
393 DownloadHandler* download_handler = delegate->download_handler();
394 for (size_t i = 0; i < candidate_icons.size(); ++i) {
395 favicon_handler->history_handler()->history_results_.clear();
396 favicon_handler->history_handler()->InvokeCallback();
397 ASSERT_TRUE(download_handler->HasDownload());
398 EXPECT_EQ(download_handler->GetImageUrl(),
399 candidate_icons[i].icon_url);
400 std::vector<int> sizes;
401 sizes.push_back(candidate_icon_sizes[i]);
402 download_handler->SetImageSizes(sizes);
403 download_handler->InvokeCallback();
404
405 download_handler->Reset();
406
407 if (delegate->num_notifications() > old_num_notifications)
408 return;
409 }
410 }
411
412 void UpdateFaviconURL(TestDelegate* delegate,
413 TestFaviconHandler* favicon_handler,
414 const GURL& page_url,
415 const std::vector<FaviconURL>& candidate_icons) {
416 delegate->ResetNumNotifications();
417
418 favicon_handler->FetchFavicon(page_url);
419 favicon_handler->history_handler()->InvokeCallback();
420
421 favicon_handler->OnUpdateFaviconURL(page_url, candidate_icons);
422 }
423
424 void SetUp() override {
425 // The score computed by SelectFaviconFrames() is dependent on the supported 336 // The score computed by SelectFaviconFrames() is dependent on the supported
426 // scale factors of the platform. It is used for determining the goodness of 337 // scale factors of the platform. It is used for determining the goodness of
427 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon(). 338 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon().
428 // Force the values of the scale factors so that the tests produce the same 339 // Force the values of the scale factors so that the tests produce the same
429 // results on all platforms. 340 // results on all platforms.
430 std::vector<ui::ScaleFactor> scale_factors;
431 scale_factors.push_back(ui::SCALE_FACTOR_100P);
432 scoped_set_supported_scale_factors_.reset( 341 scoped_set_supported_scale_factors_.reset(
433 new ui::test::ScopedSetSupportedScaleFactors(scale_factors)); 342 new ui::test::ScopedSetSupportedScaleFactors({ui::SCALE_FACTOR_100P}));
434 testing::Test::SetUp(); 343 }
435 } 344
436 345 bool VerifyAndClearExpectations() {
346 base::RunLoop().RunUntilIdle();
347 favicon_service_.fake()->ClearDbRequests();
348 delegate_.fake_downloader().ClearDownloads();
349 return testing::Mock::VerifyAndClearExpectations(&favicon_service_) &&
350 testing::Mock::VerifyAndClearExpectations(&delegate_);
351 }
352
353 // Creates a new handler and feeds in the page URL and the candidates.
354 // Returns the handler in case tests want to exercise further steps.
355 std::unique_ptr<FaviconHandler> RunHandlerWithCandidates(
356 FaviconDriverObserver::NotificationIconType handler_type,
357 const std::vector<favicon::FaviconURL>& candidates) {
358 auto handler = base::MakeUnique<FaviconHandler>(&favicon_service_,
359 &delegate_, handler_type);
360 handler->FetchFavicon(kPageURL);
361 // The first RunUntilIdle() causes the FaviconService lookups be faster than
362 // OnUpdateFaviconURL(), which is the most likely scenario.
363 base::RunLoop().RunUntilIdle();
364 handler->OnUpdateFaviconURL(kPageURL, candidates);
365 base::RunLoop().RunUntilIdle();
366 return handler;
367 }
368
369 // Same as above, but for the simplest case where all types are FAVICON and
370 // no sizes are provided.
pkotwicz 2017/03/17 05:43:12 You should document in the comment that a FaviconH
mastiz 2017/03/17 12:17:32 Done.
371 std::unique_ptr<FaviconHandler> RunHandlerWithSimpleFaviconCandidates(
372 const std::vector<GURL>& urls) {
373 std::vector<favicon::FaviconURL> candidates;
374 for (const GURL& url : urls) {
375 candidates.emplace_back(url, FAVICON, kEmptySizes);
376 }
377 return RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP,
378 candidates);
379 }
380
381 base::MessageLoopForUI message_loop_;
437 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> 382 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors>
438 scoped_set_supported_scale_factors_; 383 scoped_set_supported_scale_factors_;
384 testing::NiceMock<MockFaviconServiceWithFake> favicon_service_;
385 testing::NiceMock<MockDelegate> delegate_;
439 }; 386 };
440 387
441 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) { 388 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
442 const GURL page_url("http://www.google.com"); 389 const GURL kIconURL("http://www.google.com/favicon");
443 const GURL icon_url("http://www.google.com/favicon"); 390
444 391 favicon_service_.fake()->Store(kPageURL, kIconURL,
445 TestDelegate delegate; 392 CreateRawBitmapResult(kIconURL));
446 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 393
447 394 // Shouldn't request to download icon.
pkotwicz 2017/03/17 05:43:12 Remove this comment. (Or move it above line 400)
mastiz 2017/03/17 12:17:32 Removed, since I'm not a big fan of comments that
448 helper.FetchFavicon(page_url); 395 EXPECT_CALL(delegate_, OnFaviconUpdated(
449 HistoryRequestHandler* history_handler = helper.history_handler(); 396 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
450 // Ensure the data given to history is correct. 397 kIconURL, /*icon_url_changed=*/true, _));
451 ASSERT_TRUE(history_handler); 398
452 EXPECT_EQ(page_url, history_handler->page_url_); 399 RunHandlerWithSimpleFaviconCandidates({kIconURL});
453 EXPECT_EQ(GURL(), history_handler->icon_url_); 400 EXPECT_THAT(delegate_.downloads(), IsEmpty());
454 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 401 }
455 402
456 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_); 403 // Test that the FaviconHandler process finishes when:
457 404 // - There is no data in the database for neither the page URL nor the icon URL.
pkotwicz 2017/03/17 05:43:12 Sorry my fault. This should be: "There is data in
mastiz 2017/03/17 12:17:31 Done.
458 // Send history response. 405 // AND
459 history_handler->InvokeCallback(); 406 // - FaviconService::GetFaviconForPageURL() callback returns before
460 // Verify FaviconHandler status 407 // FaviconHandler::OnUpdateFaviconURL() is called.
461 EXPECT_EQ(1u, delegate.num_notifications()); 408 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesSlower) {
462 EXPECT_EQ(icon_url, delegate.icon_url()); 409 InSequence seq;
463 410 EXPECT_CALL(favicon_service_,
464 // Simulates update favicon url. 411 UpdateFaviconMappingsAndFetch(kPageURL, URLVector{kIconURL16x16},
465 std::vector<FaviconURL> urls; 412 FAVICON, _, _, _));
pkotwicz 2017/03/17 05:43:13 I don't think that it is useful to test that Updat
mastiz 2017/03/17 12:17:33 InSequence can be removed either way, the question
pkotwicz 2017/03/17 15:22:31 We currently don't have a test which tests that Up
mastiz 2017/03/17 15:49:28 Done, and added a TODO to introduce a new test.
466 urls.push_back( 413 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON,
467 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); 414 ImageSizeIs(16, 16)));
468 helper.OnUpdateFaviconURL(page_url, urls); 415 EXPECT_CALL(delegate_, OnFaviconUpdated(
469 416 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
470 // Verify FaviconHandler status 417 kIconURL16x16, /*icon_url_changed=*/true, _));
pkotwicz 2017/03/17 05:43:13 Only the icon URL parameters are intersting in the
mastiz 2017/03/17 12:17:32 Same here: parameters for OnFaviconUpdated() must
pkotwicz 2017/03/17 15:22:31 Yes, they should be verified somewhere. I suggest
mastiz 2017/03/17 15:49:28 While I see this argument for UpdateFaviconMapping
pkotwicz 2017/03/20 15:01:16 Fair enough. You can test all of the arguments for
471 EXPECT_EQ(1u, helper.image_urls().size()); 418
472 ASSERT_TRUE(helper.current_candidate()); 419 auto handler = base::MakeUnique<FaviconHandler>(
473 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); 420 &favicon_service_, &delegate_, FaviconDriverObserver::NON_TOUCH_16_DIP);
pkotwicz 2017/03/17 05:43:12 Can you create FaviconHandler on the stack instead
mastiz 2017/03/17 12:17:32 Done.
474 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type); 421 handler->FetchFavicon(kPageURL);
475 422 // Causes FaviconService lookups be faster than OnUpdateFaviconURL().
476 // Favicon shouldn't request to download icon. 423 base::RunLoop().RunUntilIdle();
477 EXPECT_FALSE(delegate.download_handler()->HasDownload()); 424 handler->OnUpdateFaviconURL(
478 } 425 kPageURL, {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)});
479 426 base::RunLoop().RunUntilIdle();
480 TEST_F(FaviconHandlerTest, DownloadFavicon) { 427
481 const GURL page_url("http://www.google.com"); 428 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
482 const GURL icon_url("http://www.google.com/favicon"); 429 }
483 430
484 TestDelegate delegate; 431 // Test that the FaviconHandler process does not save anything to the database
485 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 432 // for incognito tabs.
486 433 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconInIncognito) {
487 helper.FetchFavicon(page_url); 434 ON_CALL(delegate_, IsOffTheRecord()).WillByDefault(Return(true));
488 HistoryRequestHandler* history_handler = helper.history_handler(); 435
489 // Ensure the data given to history is correct. 436 // No writes expected.
490 ASSERT_TRUE(history_handler); 437 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(_, _, _, _, _, _))
491 EXPECT_EQ(page_url, history_handler->page_url_); 438 .Times(0);
492 EXPECT_EQ(GURL(), history_handler->icon_url_); 439 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0);
493 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 440
494 441 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _));
495 // Set icon data expired 442
496 SetFaviconRawBitmapResult(icon_url, 443 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
497 favicon_base::FAVICON, 444 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
pkotwicz 2017/03/17 05:43:13 We should check that |kIconURL16x16| is requested
mastiz 2017/03/17 12:17:32 Done.
498 true /* expired */, 445 }
499 &history_handler->history_results_); 446
500 // Send history response. 447 // Test that the FaviconHandler process finishes when:
501 history_handler->InvokeCallback(); 448 // - There is no data in the database for neither the page URL nor the icon URL.
502 // Verify FaviconHandler status 449 // AND
503 EXPECT_EQ(1u, delegate.num_notifications()); 450 // - FaviconService::GetFaviconForPageURL() callback returns after
504 EXPECT_EQ(icon_url, delegate.icon_url()); 451 // FaviconHandler::OnUpdateFaviconURL() is called.
505 452 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesFaster) {
pkotwicz 2017/03/17 05:43:12 Nit: Might as well make DownloadUnknownFaviconIfCa
mastiz 2017/03/17 12:17:32 Done.
506 // Simulates update favicon url. 453 InSequence seq;
507 std::vector<FaviconURL> urls; 454 EXPECT_CALL(favicon_service_,
508 urls.push_back( 455 UpdateFaviconMappingsAndFetch(kPageURL, URLVector{kIconURL16x16},
509 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); 456 FAVICON, _, _, _));
pkotwicz 2017/03/17 05:43:12 You can check this by checking EXPECT_THAT(favicon
mastiz 2017/03/17 12:17:32 Clarification needed in earlier comment, in Downlo
510 helper.OnUpdateFaviconURL(page_url, urls); 457 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON,
511 458 ImageSizeIs(16, 16)));
pkotwicz 2017/03/17 05:43:11 We only care about the icon URL parameter. We can
mastiz 2017/03/17 12:17:32 Ditto.
512 // Verify FaviconHandler status 459 EXPECT_CALL(delegate_,
pkotwicz 2017/03/17 05:43:11 We should check the icon URL. I don't think that i
mastiz 2017/03/17 12:17:32 Done.
513 EXPECT_EQ(1u, helper.image_urls().size()); 460 OnFaviconUpdated(_, _, _, /*icon_url_changed=*/true, _));
514 ASSERT_TRUE(helper.current_candidate()); 461
515 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); 462 auto handler = base::MakeUnique<FaviconHandler>(
516 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type); 463 &favicon_service_, &delegate_, FaviconDriverObserver::NON_TOUCH_16_DIP);
pkotwicz 2017/03/17 05:43:11 Can FaviconHandler be on the stack as opposed to b
mastiz 2017/03/17 12:17:32 Done.
517 464 handler->FetchFavicon(kPageURL);
518 // Favicon should request to download icon now. 465 ASSERT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL));
pkotwicz 2017/03/17 05:43:12 Nit: Can you please add a new line?
mastiz 2017/03/17 12:17:32 Done.
519 DownloadHandler* download_handler = delegate.download_handler(); 466 // Feed in favicons without processing posted tasks (RunUntilIdle()).
520 EXPECT_TRUE(download_handler->HasDownload()); 467 handler->OnUpdateFaviconURL(
521 468 kPageURL, {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)});
522 // Verify the download request. 469 base::RunLoop().RunUntilIdle();
523 EXPECT_EQ(icon_url, download_handler->GetImageUrl()); 470
524 471 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
525 // Reset the history_handler to verify whether favicon is set. 472 }
526 helper.set_history_handler(nullptr); 473
527 474 // Test that the icon is redownloaded if the icon cached for the page URL
528 // Smulates download done. 475 // expired.
529 download_handler->InvokeCallback(); 476 TEST_F(FaviconHandlerTest, RedownloadExpiredPageUrlFavicon) {
530 477 favicon_service_.fake()->Store(
531 // New icon should be saved to history backend and navigation entry. 478 kPageURL, kIconURL16x16,
532 history_handler = helper.history_handler(); 479 CreateRawBitmapResult(kIconURL16x16, FAVICON, /*expired=*/true));
533 ASSERT_TRUE(history_handler); 480
534 EXPECT_EQ(icon_url, history_handler->icon_url_); 481 // TODO(crbug.com/700811): It would be nice if we could check whether the two
535 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 482 // OnFaviconUpdated() calls are called with different gfx::Images (as opposed
536 EXPECT_LT(0U, history_handler->bitmap_data_.size()); 483 // to calling OnFaviconUpdated() with the expired gfx::Image both times).
537 EXPECT_EQ(page_url, history_handler->page_url_); 484 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _))
538 485 .Times(AtLeast(1));
pkotwicz 2017/03/17 05:43:12 Nit: Times(2)
mastiz 2017/03/17 12:17:32 Done.
539 // Verify NavigationEntry. 486 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL16x16, _, _));
540 EXPECT_EQ(2u, delegate.num_notifications()); 487
541 EXPECT_EQ(icon_url, delegate.icon_url()); 488 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
542 EXPECT_FALSE(delegate.image().IsEmpty()); 489 // We know from the |kPageUrl| database request that |kIconURL16x16| has
543 EXPECT_EQ(gfx::kFaviconSize, delegate.image().Width()); 490 // expired. A second request for |kIconURL16x16| should not have been made
544 } 491 // because it is redundant.
545 492 ASSERT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL));
pkotwicz 2017/03/17 05:43:13 ASSERT_THAT() -> EXPECT_THAT()
mastiz 2017/03/17 12:17:31 Done.
493 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
494 }
495
496 TEST_F(FaviconHandlerTest, Report404) {
497 const GURL k404IconURL("http://www.google.com/404.png");
498
499 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(k404IconURL));
500
501 RunHandlerWithSimpleFaviconCandidates({k404IconURL});
502 EXPECT_THAT(delegate_.downloads(), ElementsAre(k404IconURL));
503 }
504
505 // Test that WasUnableToDownloadFavicon() is not called if a download returns
506 // HTTP status 503.
507 TEST_F(FaviconHandlerTest, NotReport503) {
508 const GURL k503IconURL("http://www.google.com/503.png");
509
pkotwicz 2017/03/17 05:43:11 Nit: Remove new line
mastiz 2017/03/17 12:17:31 All tests have a newline after URLs, I'm not follo
pkotwicz 2017/03/17 15:22:31 Ok, it is fine to stay as is
510 delegate_.fake_downloader().AddError(k503IconURL, 503);
511
512 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0);
513
514 RunHandlerWithSimpleFaviconCandidates({k503IconURL});
515 EXPECT_THAT(delegate_.downloads(), ElementsAre(k503IconURL));
516 }
517
518 // Test that FaviconHandler process finishes when:
519 // - The icon URL used by the page has changed.
520 // AND
521 // - FaviconService::GetFaviconForPageURL() callback returns before
522 // FaviconHandler::OnUpdateFaviconURL() is called.
pkotwicz 2017/03/17 05:43:12 I think that this comment better summarizes the te
mastiz 2017/03/17 12:17:32 Done.
546 TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) { 523 TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) {
547 const GURL page_url("http://www.google.com"); 524 const GURL kOldIconURL("http://www.google.com/old_favicon");
548 const GURL icon_url("http://www.google.com/favicon"); 525 const GURL kNewIconURL = kIconURL16x16;
549 const GURL new_icon_url("http://www.google.com/new_favicon"); 526
550 527 favicon_service_.fake()->Store(kPageURL, kOldIconURL,
551 TestDelegate delegate; 528 CreateRawBitmapResult(kOldIconURL));
552 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 529
553 530 InSequence seq;
554 helper.FetchFavicon(page_url); 531 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kOldIconURL, _, _));
555 HistoryRequestHandler* history_handler = helper.history_handler(); 532 EXPECT_CALL(favicon_service_, SetFavicons(_, kNewIconURL, _, _));
556 // Ensure the data given to history is correct. 533 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kNewIconURL, _, _));
557 ASSERT_TRUE(history_handler); 534
558 EXPECT_EQ(page_url, history_handler->page_url_); 535 RunHandlerWithSimpleFaviconCandidates({kNewIconURL});
559 EXPECT_EQ(GURL(), history_handler->icon_url_); 536 EXPECT_THAT(delegate_.downloads(), ElementsAre(kNewIconURL));
560 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 537 }
561 538
562 // Set valid icon data. 539 // If there is data for the page URL in history which is invalid, test that:
563 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_); 540 // - The invalid data is not sent to the UI.
564 541 // - The icon is redownloaded.
565 // Send history response.
566 history_handler->InvokeCallback();
567 // Verify FaviconHandler status.
568 EXPECT_EQ(1u, delegate.num_notifications());
569 EXPECT_EQ(icon_url, delegate.icon_url());
570
571 // Reset the history_handler to verify whether new icon is requested from
572 // history.
573 helper.set_history_handler(nullptr);
574
575 // Simulates update with the different favicon url.
576 std::vector<FaviconURL> urls;
577 urls.push_back(FaviconURL(
578 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
579 helper.OnUpdateFaviconURL(page_url, urls);
580
581 // Verify FaviconHandler status.
582 EXPECT_EQ(1u, helper.image_urls().size());
583 ASSERT_TRUE(helper.current_candidate());
584 ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url);
585 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
586
587 // Favicon should be requested from history.
588 history_handler = helper.history_handler();
589 ASSERT_TRUE(history_handler);
590 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
591 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
592 EXPECT_EQ(page_url, history_handler->page_url_);
593
594 // Simulate not find icon.
595 history_handler->history_results_.clear();
596 history_handler->InvokeCallback();
597
598 // Favicon should request to download icon now.
599 DownloadHandler* download_handler = delegate.download_handler();
600 EXPECT_TRUE(download_handler->HasDownload());
601
602 // Verify the download request.
603 EXPECT_EQ(new_icon_url, download_handler->GetImageUrl());
604
605 // Reset the history_handler to verify whether favicon is set.
606 helper.set_history_handler(nullptr);
607
608 // Smulates download done.
609 download_handler->InvokeCallback();
610
611 // New icon should be saved to history backend and navigation entry.
612 history_handler = helper.history_handler();
613 ASSERT_TRUE(history_handler);
614 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
615 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
616 EXPECT_LT(0U, history_handler->bitmap_data_.size());
617 EXPECT_EQ(page_url, history_handler->page_url_);
618
619 // Verify NavigationEntry.
620 EXPECT_EQ(2u, delegate.num_notifications());
621 EXPECT_EQ(new_icon_url, delegate.icon_url());
622 EXPECT_FALSE(delegate.image().IsEmpty());
623 EXPECT_EQ(gfx::kFaviconSize, delegate.image().Width());
624 }
625
626 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) { 542 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) {
627 const GURL page_url("http://www.google.com");
628 const GURL icon_url("http://www.google.com/favicon");
629
630 TestDelegate delegate;
631 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP);
632
633 helper.FetchFavicon(page_url);
634 HistoryRequestHandler* history_handler = helper.history_handler();
635 // Ensure the data given to history is correct.
636 ASSERT_TRUE(history_handler);
637 EXPECT_EQ(page_url, history_handler->page_url_);
638 EXPECT_EQ(GURL(), history_handler->icon_url_);
639 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
640
641 // Set non empty but invalid data. 543 // Set non empty but invalid data.
642 favicon_base::FaviconRawBitmapResult bitmap_result; 544 FaviconRawBitmapResult bitmap_result;
643 bitmap_result.expired = false; 545 bitmap_result.expired = false;
644 // Empty bitmap data is invalid. 546 // Empty bitmap data is invalid.
645 bitmap_result.bitmap_data = new base::RefCountedBytes(); 547 bitmap_result.bitmap_data = new base::RefCountedBytes();
646 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); 548 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize);
647 bitmap_result.icon_type = favicon_base::FAVICON; 549 bitmap_result.icon_type = FAVICON;
648 bitmap_result.icon_url = icon_url; 550 bitmap_result.icon_url = kIconURL16x16;
649 history_handler->history_results_.clear(); 551
650 history_handler->history_results_.push_back(bitmap_result); 552 favicon_service_.fake()->Store(kPageURL, kIconURL16x16, {bitmap_result});
651 553
652 // Send history response. 554 // TODO(crbug.com/700811): It would be nice if we could check the image
653 history_handler->InvokeCallback(); 555 // being published to rule out invalid data.
654 // The NavigationEntry should not be set yet as the history data is invalid. 556 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _));
655 EXPECT_EQ(0u, delegate.num_notifications()); 557
656 EXPECT_EQ(GURL(), delegate.icon_url()); 558 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
657 559
658 // Reset the history_handler to verify whether new icon is requested from 560 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL));
659 // history. 561 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
660 helper.set_history_handler(nullptr); 562 }
661 563
662 // Simulates update with matching favicon URL. 564 // Test that no downloads are done if a user visits a page which changed its
663 std::vector<FaviconURL> urls; 565 // favicon URL to a favicon URL which is already cached in the database.
664 urls.push_back(
665 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
666 helper.OnUpdateFaviconURL(page_url, urls);
667
668 // A download for the favicon should be requested, and we should not do
669 // another history request.
670 DownloadHandler* download_handler = delegate.download_handler();
671 EXPECT_TRUE(download_handler->HasDownload());
672 EXPECT_EQ(nullptr, helper.history_handler());
673
674 // Verify the download request.
675 EXPECT_EQ(icon_url, download_handler->GetImageUrl());
676
677 // Simulates download done.
678 download_handler->InvokeCallback();
679
680 // New icon should be saved to history backend and navigation entry.
681 history_handler = helper.history_handler();
682 ASSERT_TRUE(history_handler);
683 EXPECT_EQ(icon_url, history_handler->icon_url_);
684 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
685 EXPECT_LT(0U, history_handler->bitmap_data_.size());
686 EXPECT_EQ(page_url, history_handler->page_url_);
687
688 // Verify NavigationEntry.
689 EXPECT_EQ(1u, delegate.num_notifications());
690 EXPECT_EQ(icon_url, delegate.icon_url());
691 EXPECT_FALSE(delegate.image().IsEmpty());
692 EXPECT_EQ(gfx::kFaviconSize, delegate.image().Width());
693 }
694
695 TEST_F(FaviconHandlerTest, UpdateFavicon) { 566 TEST_F(FaviconHandlerTest, UpdateFavicon) {
696 const GURL page_url("http://www.google.com"); 567 const GURL kSomePreviousPageURL("https://www.google.com/previous");
697 const GURL icon_url("http://www.google.com/favicon"); 568 const GURL kIconURL("http://www.google.com/favicon");
698 const GURL new_icon_url("http://www.google.com/new_favicon"); 569 const GURL kNewIconURL("http://www.google.com/new_favicon");
699 570
700 TestDelegate delegate; 571 favicon_service_.fake()->Store(kPageURL, kIconURL,
701 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 572 CreateRawBitmapResult(kIconURL));
702 573 favicon_service_.fake()->Store(kSomePreviousPageURL, kNewIconURL,
703 helper.FetchFavicon(page_url); 574 CreateRawBitmapResult(kNewIconURL));
704 HistoryRequestHandler* history_handler = helper.history_handler(); 575
705 // Ensure the data given to history is correct. 576 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0);
706 ASSERT_TRUE(history_handler); 577
707 EXPECT_EQ(page_url, history_handler->page_url_); 578 InSequence seq;
708 EXPECT_EQ(GURL(), history_handler->icon_url_); 579 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL, _, _));
709 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 580 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kNewIconURL, _, _));
710 581
711 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_); 582 RunHandlerWithSimpleFaviconCandidates({kNewIconURL});
712 583 ASSERT_THAT(favicon_service_.fake()->db_requests(),
713 // Send history response. 584 ElementsAre(kPageURL, kNewIconURL));
714 history_handler->InvokeCallback(); 585 EXPECT_THAT(delegate_.downloads(), IsEmpty());
715 // Verify FaviconHandler status.
716 EXPECT_EQ(1u, delegate.num_notifications());
717 EXPECT_EQ(icon_url, delegate.icon_url());
718
719 // Reset the history_handler to verify whether new icon is requested from
720 // history.
721 helper.set_history_handler(nullptr);
722
723 // Simulates update with the different favicon url.
724 std::vector<FaviconURL> urls;
725 urls.push_back(FaviconURL(
726 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
727 helper.OnUpdateFaviconURL(page_url, urls);
728
729 // Verify FaviconHandler status.
730 EXPECT_EQ(1u, helper.image_urls().size());
731 ASSERT_TRUE(helper.current_candidate());
732 ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url);
733 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
734
735 // Favicon should be requested from history.
736 history_handler = helper.history_handler();
737 ASSERT_TRUE(history_handler);
738 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
739 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
740 EXPECT_EQ(page_url, history_handler->page_url_);
741
742 // Simulate find icon.
743 SetFaviconRawBitmapResult(new_icon_url, &history_handler->history_results_);
744 history_handler->InvokeCallback();
745
746 // Shouldn't request download favicon
747 EXPECT_FALSE(delegate.download_handler()->HasDownload());
748
749 // Verify the favicon status.
750 EXPECT_EQ(2u, delegate.num_notifications());
751 EXPECT_EQ(new_icon_url, delegate.icon_url());
752 EXPECT_FALSE(delegate.image().IsEmpty());
753 } 586 }
754 587
755 TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) { 588 TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
756 const GURL page_url("http://www.google.com"); 589 const GURL kIconURLReturning500("http://www.google.com/500.png");
757 const GURL icon_url("http://www.google.com/favicon"); 590 delegate_.fake_downloader().AddError(kIconURLReturning500, 500);
758 const GURL new_icon_url("http://www.google.com/new_favicon"); 591
759 592 favicon_service_.fake()->Store(
760 TestDelegate delegate; 593 kPageURL, kIconURL64x64,
761 TestFaviconHandler helper(&delegate, FaviconDriverObserver::TOUCH_LARGEST); 594 CreateRawBitmapResult(kIconURL64x64, TOUCH_ICON,
762 std::set<GURL> fail_downloads; 595 /*expired=*/true));
763 fail_downloads.insert(icon_url); 596
764 delegate.download_handler()->FailDownloadForIconURLs(fail_downloads); 597 EXPECT_CALL(delegate_,
765 598 OnFaviconUpdated(kPageURL, FaviconDriverObserver::TOUCH_LARGEST,
766 helper.FetchFavicon(page_url); 599 kIconURL64x64, /*icon_url_changed=*/true, _));
767 HistoryRequestHandler* history_handler = helper.history_handler(); 600 EXPECT_CALL(delegate_,
768 // Ensure the data given to history is correct. 601 OnFaviconUpdated(kPageURL, FaviconDriverObserver::TOUCH_LARGEST,
769 ASSERT_TRUE(history_handler); 602 kIconURL64x64, /*icon_url_changed=*/false, _));
770 EXPECT_EQ(page_url, history_handler->page_url_); 603
771 EXPECT_EQ(GURL(), history_handler->icon_url_); 604 RunHandlerWithCandidates(
772 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON, 605 FaviconDriverObserver::TOUCH_LARGEST,
773 history_handler->icon_type_); 606 {
774 607 FaviconURL(kIconURLReturning500, TOUCH_PRECOMPOSED_ICON, kEmptySizes),
775 // Icon not found. 608 FaviconURL(kIconURL64x64, TOUCH_ICON, kEmptySizes),
776 history_handler->history_results_.clear(); 609 });
777 // Send history response. 610 // First download fails, second succeeds.
778 history_handler->InvokeCallback(); 611 EXPECT_THAT(delegate_.downloads(),
779 // Verify FaviconHandler status. 612 ElementsAre(kIconURLReturning500, kIconURL64x64));
780 EXPECT_EQ(0u, delegate.num_notifications()); 613 }
781 EXPECT_EQ(GURL(), delegate.icon_url()); 614
782 615 // TODO(mastiz): Make this test deal with FaviconURLs of type
783 // Reset the history_handler to verify whether new icon is requested from 616 // favicon_base::FAVICON and add new ones like OnlyDownloadMatchingIconType and
784 // history. 617 // CallSetFaviconsWithCorrectIconType.
785 helper.set_history_handler(nullptr);
786
787 // Simulates update with the different favicon url.
788 std::vector<FaviconURL> urls;
789 urls.push_back(FaviconURL(icon_url,
790 favicon_base::TOUCH_PRECOMPOSED_ICON,
791 std::vector<gfx::Size>()));
792 urls.push_back(FaviconURL(
793 new_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>()));
794 urls.push_back(FaviconURL(
795 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
796 helper.OnUpdateFaviconURL(page_url, urls);
797
798 // Verify FaviconHandler status.
799 EXPECT_EQ(2u, helper.image_urls().size());
800 EXPECT_EQ(0u, helper.current_candidate_index());
801 ASSERT_TRUE(helper.current_candidate());
802 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
803 ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON,
804 helper.current_candidate()->icon_type);
805
806 // Favicon should be requested from history.
807 history_handler = helper.history_handler();
808 ASSERT_TRUE(history_handler);
809 EXPECT_EQ(icon_url, history_handler->icon_url_);
810 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_);
811 EXPECT_EQ(page_url, history_handler->page_url_);
812
813 // Simulate not find icon.
814 history_handler->history_results_.clear();
815 history_handler->InvokeCallback();
816
817 // Should request download favicon.
818 DownloadHandler* download_handler = delegate.download_handler();
819 EXPECT_TRUE(download_handler->HasDownload());
820
821 // Verify the download request.
822 EXPECT_EQ(icon_url, download_handler->GetImageUrl());
823
824 // Reset the history_handler to verify whether favicon is request from
825 // history.
826 helper.set_history_handler(nullptr);
827 download_handler->InvokeCallback();
828
829 // Left 1 url.
830 EXPECT_EQ(1u, helper.current_candidate_index());
831 ASSERT_TRUE(helper.current_candidate());
832 EXPECT_EQ(new_icon_url, helper.current_candidate()->icon_url);
833 EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type);
834
835 // Favicon should be requested from history.
836 history_handler = helper.history_handler();
837 ASSERT_TRUE(history_handler);
838 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
839 EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_);
840 EXPECT_EQ(page_url, history_handler->page_url_);
841
842 // Reset download handler
843 download_handler->Reset();
844
845 // Simulates getting a expired icon from history.
846 SetFaviconRawBitmapResult(new_icon_url,
847 favicon_base::TOUCH_ICON,
848 true /* expired */,
849 &history_handler->history_results_);
850 history_handler->InvokeCallback();
851
852 // Verify the download request.
853 EXPECT_TRUE(delegate.download_handler()->HasDownload());
854 EXPECT_EQ(new_icon_url, download_handler->GetImageUrl());
855
856 helper.set_history_handler(nullptr);
857
858 // Simulates icon being downloaded.
859 download_handler->InvokeCallback();
860
861 // New icon should be saved to history backend.
862 history_handler = helper.history_handler();
863 ASSERT_TRUE(history_handler);
864 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
865 EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_);
866 EXPECT_LT(0U, history_handler->bitmap_data_.size());
867 EXPECT_EQ(page_url, history_handler->page_url_);
868 }
869
870 TEST_F(FaviconHandlerTest, UpdateDuringDownloading) { 618 TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
pkotwicz 2017/03/17 05:43:11 For the commment, how about: "Test that download
mastiz 2017/03/17 12:17:31 Done.
871 const GURL page_url("http://www.google.com"); 619 const GURL kIconURL("http://www.google.com/favicon");
pkotwicz 2017/03/17 05:43:12 Nit: make these |kIconURL1| - |kIconURL3|
mastiz 2017/03/17 12:17:32 Done.
872 const GURL icon_url("http://www.google.com/favicon"); 620 const GURL kIconURL1 = kIconURL16x16;
873 const GURL new_icon_url("http://www.google.com/new_favicon"); 621 const GURL kIconURL2 = kIconURL64x64;
874 622
875 TestDelegate delegate; 623 // Defer the download completion such that RunUntilIdle() doesn't complete
876 TestFaviconHandler helper(&delegate, FaviconDriverObserver::TOUCH_LARGEST); 624 // the download.
877 625 delegate_.fake_downloader().SetRunCallbackManuallyForUrl(kIconURL);
pkotwicz 2017/03/17 05:43:12 Nit: Can you please add a new line?
mastiz 2017/03/17 12:17:32 Done.
878 helper.FetchFavicon(page_url); 626 delegate_.fake_downloader().Add(kIconURL, IntVector{16});
879 HistoryRequestHandler* history_handler = helper.history_handler(); 627 delegate_.fake_downloader().Add(kIconURL2, IntVector{64});
880 // Ensure the data given to history is correct. 628
881 ASSERT_TRUE(history_handler); 629 std::unique_ptr<FaviconHandler> handler =
882 EXPECT_EQ(page_url, history_handler->page_url_); 630 RunHandlerWithSimpleFaviconCandidates({kIconURL, kIconURL1});
883 EXPECT_EQ(GURL(), history_handler->icon_url_); 631
884 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON, 632 ASSERT_TRUE(VerifyAndClearExpectations());
885 history_handler->icon_type_); 633 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback());
886 634
887 // Icon not found. 635 // Favicon update should invalidate the ongoing download.
888 history_handler->history_results_.clear(); 636 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL2, _, _));
889 // Send history response. 637 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2, _, _));
890 history_handler->InvokeCallback(); 638
891 // Verify FaviconHandler status. 639 handler->OnUpdateFaviconURL(kPageURL,
892 EXPECT_EQ(0u, delegate.num_notifications()); 640 {FaviconURL(kIconURL2, FAVICON, kEmptySizes)});
893 EXPECT_EQ(GURL(), delegate.icon_url()); 641
894 642 // Finalizes download, which should be thrown away as the favicon URLs were
895 // Reset the history_handler to verify whether new icon is requested from 643 // updated.
896 // history. 644 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually());
897 helper.set_history_handler(nullptr); 645 base::RunLoop().RunUntilIdle();
898 646
899 // Simulates update with the different favicon url. 647 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kIconURL2));
900 std::vector<FaviconURL> urls; 648 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2));
901 urls.push_back(FaviconURL(icon_url,
902 favicon_base::TOUCH_PRECOMPOSED_ICON,
903 std::vector<gfx::Size>()));
904 urls.push_back(FaviconURL(
905 new_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>()));
906 urls.push_back(FaviconURL(
907 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
908 helper.OnUpdateFaviconURL(page_url, urls);
909
910 // Verify FaviconHandler status.
911 EXPECT_EQ(2u, helper.image_urls().size());
912 ASSERT_EQ(0u, helper.current_candidate_index());
913 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
914 ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON,
915 helper.current_candidate()->icon_type);
916
917 // Favicon should be requested from history.
918 history_handler = helper.history_handler();
919 ASSERT_TRUE(history_handler);
920 EXPECT_EQ(icon_url, history_handler->icon_url_);
921 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_);
922 EXPECT_EQ(page_url, history_handler->page_url_);
923
924 // Simulate not find icon.
925 history_handler->history_results_.clear();
926 history_handler->InvokeCallback();
927
928 // Should request download favicon.
929 DownloadHandler* download_handler = delegate.download_handler();
930 EXPECT_TRUE(download_handler->HasDownload());
931
932 // Verify the download request.
933 EXPECT_EQ(icon_url, download_handler->GetImageUrl());
934
935 // Reset the history_handler to verify whether favicon is request from
936 // history.
937 helper.set_history_handler(nullptr);
938 const GURL latest_icon_url("http://www.google.com/latest_favicon");
939 std::vector<FaviconURL> latest_urls;
940 latest_urls.push_back(FaviconURL(
941 latest_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>()));
942 helper.OnUpdateFaviconURL(page_url, latest_urls);
943
944 EXPECT_EQ(1u, helper.image_urls().size());
945 EXPECT_EQ(0u, helper.current_candidate_index());
946 EXPECT_EQ(latest_icon_url, helper.current_candidate()->icon_url);
947 EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type);
948
949 // Whether new icon is requested from history
950 history_handler = helper.history_handler();
951 ASSERT_TRUE(history_handler);
952 EXPECT_EQ(latest_icon_url, history_handler->icon_url_);
953 EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_);
954 EXPECT_EQ(page_url, history_handler->page_url_);
955
956 // Reset the history_handler to verify whether favicon is request from
957 // history.
958 // Save the callback for late use.
959 favicon_base::FaviconResultsCallback callback = history_handler->callback_;
960 helper.set_history_handler(nullptr);
961
962 // Simulates download succeed.
963 download_handler->InvokeCallback();
964 // The downloaded icon should be thrown away as there is favicon update.
965 EXPECT_FALSE(helper.history_handler());
966
967 download_handler->Reset();
968
969 // Simulates getting the icon from history.
970 std::unique_ptr<HistoryRequestHandler> handler;
971 handler.reset(new HistoryRequestHandler(
972 page_url, latest_icon_url, favicon_base::TOUCH_ICON, callback));
973 SetFaviconRawBitmapResult(latest_icon_url,
974 favicon_base::TOUCH_ICON,
975 false /* expired */,
976 &handler->history_results_);
977 handler->InvokeCallback();
978
979 // No download request.
980 EXPECT_FALSE(download_handler->HasDownload());
981 } 649 }
982 650
983 // Test that sending an icon URL update identical to the previous icon URL 651 // Test that sending an icon URL update identical to the previous icon URL
984 // update is a no-op. 652 // update is a no-op.
985 TEST_F(FaviconHandlerTest, UpdateSameIconURLs) { 653 TEST_F(FaviconHandlerTest, UpdateSameIconURLsWhileProcessingShouldBeNoop) {
986 const GURL page_url("http://www.google.com"); 654 const GURL kSlowLoadingIconURL("http://www.google.com/slow_favicon");
987 const GURL icon_url1("http://www.google.com/favicon1"); 655
988 const GURL icon_url2("http://www.google.com/favicon2"); 656 const std::vector<FaviconURL> favicon_urls = {
989 std::vector<FaviconURL> favicon_urls; 657 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes),
990 favicon_urls.push_back(FaviconURL(GURL("http://www.google.com/favicon1"), 658 FaviconURL(kSlowLoadingIconURL, FAVICON, kEmptySizes),
991 favicon_base::FAVICON, 659 };
992 std::vector<gfx::Size>())); 660
993 favicon_urls.push_back(FaviconURL(GURL("http://www.google.com/favicon2"), 661 // Defer the download completion such that RunUntilIdle() doesn't complete
994 favicon_base::FAVICON, 662 // the download.
995 std::vector<gfx::Size>())); 663 delegate_.fake_downloader().SetRunCallbackManuallyForUrl(kSlowLoadingIconURL);
996 664 delegate_.fake_downloader().Add(kSlowLoadingIconURL, IntVector{16});
997 TestDelegate delegate; 665
998 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 666 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates(
999 667 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls);
1000 // Initiate a request for favicon data for |page_url|. History does not know 668
1001 // about the page URL or the icon URLs. 669 ASSERT_THAT(favicon_service_.fake()->db_requests(),
1002 helper.FetchFavicon(page_url); 670 ElementsAre(kPageURL, kIconURL64x64, kSlowLoadingIconURL));
1003 helper.history_handler()->InvokeCallback(); 671 ASSERT_TRUE(VerifyAndClearExpectations());
1004 helper.set_history_handler(nullptr); 672 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback());
1005 673
1006 // Got icon URLs. 674 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect,
1007 helper.OnUpdateFaviconURL(page_url, favicon_urls); 675 // despite the ongoing download.
1008 676 handler->OnUpdateFaviconURL(kPageURL, favicon_urls);
1009 // There should be an ongoing history request for |icon_url1|. 677 base::RunLoop().RunUntilIdle();
1010 ASSERT_EQ(2u, helper.image_urls().size()); 678
1011 ASSERT_EQ(0u, helper.current_candidate_index()); 679 // Complete the download.
1012 HistoryRequestHandler* history_handler = helper.history_handler(); 680 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _));
1013 ASSERT_TRUE(history_handler); 681 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _));
1014 682 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually());
1015 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect. 683 base::RunLoop().RunUntilIdle();
1016 helper.OnUpdateFaviconURL(page_url, favicon_urls); 684 EXPECT_THAT(delegate_.downloads(), IsEmpty());
1017 EXPECT_EQ(history_handler, helper.history_handler()); 685 }
1018 686
1019 // Complete history request for |icon_url1| and do download. 687 // Test that calling OnUpdateFaviconUrl() with the same icon URLs as before is a
1020 helper.history_handler()->InvokeCallback(); 688 // no-op. This is important because OnUpdateFaviconUrl() is called when the page
1021 helper.set_history_handler(nullptr); 689 // finishes loading. This can occur several times for pages with iframes.
1022 delegate.download_handler()->SetImageSizes(std::vector<int>(1u, 10)); 690 TEST_F(FaviconHandlerTest, UpdateSameIconURLsAfterFinishedShouldBeNoop) {
1023 delegate.download_handler()->InvokeCallback(); 691 const std::vector<FaviconURL> favicon_urls = {
1024 delegate.download_handler()->Reset(); 692 FaviconURL(kIconURL10x10, FAVICON, kEmptySizes),
1025 693 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes),
1026 // There should now be an ongoing history request for |icon_url2|. 694 };
1027 ASSERT_EQ(1u, helper.current_candidate_index()); 695
1028 history_handler = helper.history_handler(); 696 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates(
1029 ASSERT_TRUE(history_handler); 697 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls);
1030 698
1031 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect. 699 ASSERT_TRUE(VerifyAndClearExpectations());
1032 helper.OnUpdateFaviconURL(page_url, favicon_urls); 700
1033 EXPECT_EQ(history_handler, helper.history_handler()); 701 // Calling OnUpdateFaviconURL() with identical data should be a no-op.
702 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0);
703 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0);
704
705 handler->OnUpdateFaviconURL(kPageURL, favicon_urls);
706 base::RunLoop().RunUntilIdle();
707 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty());
708 EXPECT_THAT(delegate_.downloads(), IsEmpty());
1034 } 709 }
1035 710
1036 // Fixes crbug.com/544560 711 // Fixes crbug.com/544560
712 // Tests that Delegate::OnFaviconUpdated() is called if:
713 // - The best icon on the initial page is not the last icon.
714 // - All of the initial page's icons are downloaded.
715 // AND
716 // - JavaScript modifies the page's <link rel="icon"> tags to contain only the
717 // last icon.
1037 TEST_F(FaviconHandlerTest, 718 TEST_F(FaviconHandlerTest,
1038 OnFaviconAvailableNotificationSentAfterIconURLChange) { 719 OnFaviconAvailableNotificationSentAfterIconURLChange) {
1039 const GURL kPageURL("http://www.page_which_animates_favicon.com"); 720 const GURL kIconURL1(
1040 const GURL kIconURL1("http://wwww.page_which_animates_favicon.com/frame1.png") ; 721 "http://wwww.page_which_animates_favicon.com/frame1.png");
1041 const GURL kIconURL2("http://wwww.page_which_animates_favicon.com/frame2.png") ; 722 const GURL kIconURL2(
1042 723 "http://wwww.page_which_animates_favicon.com/frame2.png");
1043 TestDelegate delegate; 724
1044 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 725 // |kIconURL1| is the better match.
1045 726 delegate_.fake_downloader().Add(kIconURL1, IntVector{15});
1046 // Initial state: 727 delegate_.fake_downloader().Add(kIconURL2, IntVector{10});
1047 // - The database does not know about |kPageURL|. 728
1048 // - The page uses |kIconURL1| and |kIconURL2|. 729 // Two FaviconDriver::OnFaviconUpdated() notifications should be sent for
1049 // - The database knows about both |kIconURL1| and |kIconURl2|. Both icons 730 // |kIconURL1|, one before and one after the download.
1050 // are expired in the database. 731 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL1, _, _));
1051 helper.FetchFavicon(kPageURL); 732
1052 ASSERT_TRUE(helper.history_handler()); 733 std::unique_ptr<FaviconHandler> handler =
1053 helper.history_handler()->InvokeCallback(); 734 RunHandlerWithSimpleFaviconCandidates({kIconURL1, kIconURL2});
1054 { 735
1055 std::vector<FaviconURL> icon_urls; 736 // Both |kIconURL1| and |kIconURL2| should have been requested from the
1056 icon_urls.push_back( 737 // database and downloaded. |kIconURL2| should have been fetched from the
1057 FaviconURL(kIconURL1, favicon_base::FAVICON, std::vector<gfx::Size>())); 738 // database and downloaded last.
1058 icon_urls.push_back( 739 ASSERT_THAT(delegate_.downloads(), ElementsAre(kIconURL1, kIconURL2));
1059 FaviconURL(kIconURL2, favicon_base::FAVICON, std::vector<gfx::Size>())); 740 ASSERT_THAT(favicon_service_.fake()->db_requests(),
1060 helper.OnUpdateFaviconURL(kPageURL, icon_urls); 741 ElementsAre(kPageURL, kIconURL1, kIconURL2));
1061 } 742 ASSERT_TRUE(VerifyAndClearExpectations());
1062
1063 // FaviconHandler should request from history and download |kIconURL1| and
1064 // |kIconURL2|. |kIconURL1| is the better match. A
1065 // FaviconDriver::OnFaviconUpdated() notification should be sent for
1066 // |kIconURL1|.
1067 ASSERT_EQ(0u, delegate.num_notifications());
1068 ASSERT_TRUE(helper.history_handler());
1069 SetFaviconRawBitmapResult(kIconURL1,
1070 favicon_base::FAVICON,
1071 true /* expired */,
1072 &helper.history_handler()->history_results_);
1073 helper.history_handler()->InvokeCallback();
1074 helper.set_history_handler(nullptr);
1075 ASSERT_TRUE(delegate.download_handler()->HasDownload());
1076 delegate.download_handler()->SetImageSizes(std::vector<int>(1u, 15));
1077 delegate.download_handler()->InvokeCallback();
1078 delegate.download_handler()->Reset();
1079
1080 ASSERT_TRUE(helper.history_handler());
1081 helper.history_handler()->InvokeCallback();
1082 SetFaviconRawBitmapResult(kIconURL2,
1083 favicon_base::FAVICON,
1084 true /* expired */,
1085 &helper.history_handler()->history_results_);
1086 helper.history_handler()->InvokeCallback();
1087 helper.set_history_handler(nullptr);
1088 ASSERT_TRUE(delegate.download_handler()->HasDownload());
1089 delegate.download_handler()->SetImageSizes(std::vector<int>(1u, 10));
1090 delegate.download_handler()->InvokeCallback();
1091 delegate.download_handler()->Reset();
1092
1093 ASSERT_LT(0u, delegate.num_notifications());
1094 ASSERT_EQ(kIconURL1, delegate.icon_url());
1095
1096 // Clear the history handler because SetHistoryFavicons() sets it.
1097 helper.set_history_handler(nullptr);
1098 743
1099 // Simulate the page changing it's icon URL to just |kIconURL2| via 744 // Simulate the page changing it's icon URL to just |kIconURL2| via
1100 // Javascript. 745 // Javascript.
1101 helper.OnUpdateFaviconURL( 746 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2, _, _));
1102 kPageURL, 747 handler->OnUpdateFaviconURL(kPageURL,
1103 std::vector<FaviconURL>(1u, FaviconURL(kIconURL2, favicon_base::FAVICON, 748 {FaviconURL(kIconURL2, FAVICON, kEmptySizes)});
1104 std::vector<gfx::Size>()))); 749 base::RunLoop().RunUntilIdle();
1105
1106 // FaviconHandler should request from history and download |kIconURL2|. A
1107 // FaviconDriver::OnFaviconUpdated() notification should be sent for
1108 // |kIconURL2|.
1109 delegate.ResetNumNotifications();
1110 ASSERT_TRUE(helper.history_handler());
1111 SetFaviconRawBitmapResult(kIconURL2,
1112 favicon_base::FAVICON,
1113 true /* expired */,
1114 &helper.history_handler()->history_results_);
1115 helper.history_handler()->InvokeCallback();
1116 helper.set_history_handler(nullptr);
1117 ASSERT_TRUE(delegate.download_handler()->HasDownload());
1118 delegate.download_handler()->InvokeCallback();
1119 delegate.download_handler()->Reset();
1120 ASSERT_LT(0u, delegate.num_notifications());
1121 EXPECT_EQ(kIconURL2, delegate.icon_url());
1122 } 750 }
1123 751
1124 // Test the favicon which is selected when the web page provides several 752 // Test the favicon which is selected when the web page provides several
1125 // favicons and none of the favicons are cached in history. 753 // favicons and none of the favicons are cached in history.
1126 // The goal of this test is to be more of an integration test than 754 // The goal of this test is to be more of an integration test than
1127 // SelectFaviconFramesTest.*. 755 // SelectFaviconFramesTest.*.
1128 TEST_F(FaviconHandlerTest, MultipleFavicons) { 756 class FaviconHandlerMultipleFaviconsTest : public FaviconHandlerTest {
1129 const GURL kPageURL("http://www.google.com"); 757 protected:
1130 const FaviconURL kSourceIconURLs[] = { 758 FaviconHandlerMultipleFaviconsTest() {
1131 FaviconURL(GURL("http://www.google.com/a"), 759 // Set the supported scale factors to 1x and 2x. This affects the behavior
1132 favicon_base::FAVICON, 760 // of SelectFaviconFrames().
1133 std::vector<gfx::Size>()), 761 scoped_set_supported_scale_factors_.reset(); // Need to delete first.
1134 FaviconURL(GURL("http://www.google.com/b"), 762 scoped_set_supported_scale_factors_.reset(
1135 favicon_base::FAVICON, 763 new ui::test::ScopedSetSupportedScaleFactors(
1136 std::vector<gfx::Size>()), 764 {ui::SCALE_FACTOR_100P, ui::SCALE_FACTOR_200P}));
1137 FaviconURL(GURL("http://www.google.com/c"), 765 }
1138 favicon_base::FAVICON, 766
1139 std::vector<gfx::Size>()), 767 // Simulates requesting a favicon for |page_url| given:
1140 FaviconURL(GURL("http://www.google.com/d"), 768 // - We have not previously cached anything in history for |page_url| or for
1141 favicon_base::FAVICON, 769 // any of candidates.
1142 std::vector<gfx::Size>()), 770 // - The page provides favicons with edge pixel sizes of
1143 FaviconURL(GURL("http://www.google.com/e"), 771 // |candidate_icon_sizes|.
1144 favicon_base::FAVICON, 772 // - Candidates are assumed of type FAVICON and the URLs are generated
1145 std::vector<gfx::Size>())}; 773 // internally for testing purposes.
1146 774 //
1147 // Set the supported scale factors to 1x and 2x. This affects the behavior of 775 // Returns the chosen size among |candidate_icon_sizes| or -1 if none was
1148 // SelectFaviconFrames(). 776 // chosen.
1149 std::vector<ui::ScaleFactor> scale_factors; 777 int DownloadTillDoneIgnoringHistory(const IntVector& candidate_icon_sizes) {
1150 scale_factors.push_back(ui::SCALE_FACTOR_100P); 778 std::vector<FaviconURL> candidate_icons;
1151 scale_factors.push_back(ui::SCALE_FACTOR_200P); 779 int chosen_icon_size = -1;
1152 ui::test::ScopedSetSupportedScaleFactors scoped_supported(scale_factors); 780
1153 781 for (int icon_size : candidate_icon_sizes) {
1154 // 1) Test that if there are several single resolution favicons to choose from 782 const GURL icon_url(base::StringPrintf(
1155 // that the largest exact match is chosen. 783 "https://www.google.com/generated/%dx%d", icon_size, icon_size));
1156 TestDelegate delegate1; 784 // Set up 200 responses for all images, and the corresponding size.
1157 TestFaviconHandler handler1(&delegate1, 785 delegate_.fake_downloader().Add(icon_url, IntVector{icon_size});
1158 FaviconDriverObserver::NON_TOUCH_16_DIP); 786 // Create test candidates of type FAVICON and a fake URL.
1159 787 candidate_icons.emplace_back(icon_url, FAVICON, kEmptySizes);
1160 const int kSizes1[] = { 16, 24, 32, 48, 256 }; 788
1161 std::vector<FaviconURL> urls1(kSourceIconURLs, 789 ON_CALL(delegate_, OnFaviconUpdated(_, _, icon_url, _, _))
1162 kSourceIconURLs + arraysize(kSizes1)); 790 .WillByDefault(Assign(&chosen_icon_size, icon_size));
1163 DownloadTillDoneIgnoringHistory(&delegate1, &handler1, kPageURL, urls1, 791 }
1164 kSizes1); 792
1165 793 RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP,
1166 EXPECT_EQ(nullptr, handler1.current_candidate()); 794 candidate_icons);
1167 EXPECT_EQ(1u, delegate1.num_notifications()); 795 return chosen_icon_size;
1168 EXPECT_FALSE(delegate1.image().IsEmpty()); 796 }
1169 EXPECT_EQ(gfx::kFaviconSize, delegate1.image().Width()); 797 };
1170 798
1171 size_t expected_index = 2u; 799 // Tests that running FaviconHandler
1172 EXPECT_EQ(32, kSizes1[expected_index]); 800 // - Pn an OS which supports the 1x and 2x scale factor
pkotwicz 2017/03/17 05:43:13 Pn -> On
mastiz 2017/03/17 12:17:31 Done.
1173 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate1.icon_url()); 801 // - On a page with <link rel="icon"> tags with no "sizes" information selects
pkotwicz 2017/03/17 05:43:12 Nit: new line before "selects" ("selects" is not p
mastiz 2017/03/17 12:17:32 Done.
1174 802 // the largest exact match. Note that a 32x32 PNG image is not a "true
1175 // 2) Test that if there are several single resolution favicons to choose 803 // exact match" on an OS which supports an 1x and 2x. A "true exact match" is
1176 // from, the exact match is preferred even if it results in upsampling. 804 // a .ico file with 16x16 and 32x32 bitmaps.
1177 TestDelegate delegate2; 805 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseLargestExactMatch) {
1178 TestFaviconHandler handler2(&delegate2, 806 EXPECT_EQ(32,
1179 FaviconDriverObserver::NON_TOUCH_16_DIP); 807 DownloadTillDoneIgnoringHistory(IntVector{16, 24, 32, 48, 256}));
1180 808 }
1181 const int kSizes2[] = { 16, 24, 48, 256 }; 809
1182 std::vector<FaviconURL> urls2(kSourceIconURLs, 810 // Test that if there are several single resolution favicons to choose
1183 kSourceIconURLs + arraysize(kSizes2)); 811 // from, the exact match is preferred even if it results in upsampling.
1184 DownloadTillDoneIgnoringHistory(&delegate2, &handler2, kPageURL, urls2, 812 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseExactMatchDespiteUpsampling) {
1185 kSizes2); 813 EXPECT_EQ(16, DownloadTillDoneIgnoringHistory(IntVector{16, 24, 48, 256}));
1186 EXPECT_EQ(1u, delegate2.num_notifications()); 814 }
1187 expected_index = 0u; 815
1188 EXPECT_EQ(16, kSizes2[expected_index]); 816 // Test that favicons which need to be upsampled a little or downsampled
1189 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate2.icon_url()); 817 // a little are preferred over huge favicons.
1190 818 TEST_F(FaviconHandlerMultipleFaviconsTest,
1191 // 3) Test that favicons which need to be upsampled a little or downsampled 819 ChooseMinorDownsamplingOverHugeIcon) {
1192 // a little are preferred over huge favicons. 820 EXPECT_EQ(48, DownloadTillDoneIgnoringHistory(IntVector{256, 48}));
1193 TestDelegate delegate3; 821 }
1194 TestFaviconHandler handler3(&delegate3, 822
1195 FaviconDriverObserver::NON_TOUCH_16_DIP); 823 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseMinorUpsamplingOverHugeIcon) {
1196 824 EXPECT_EQ(17, DownloadTillDoneIgnoringHistory(IntVector{17, 256}));
1197 const int kSizes3[] = { 256, 48 };
1198 std::vector<FaviconURL> urls3(kSourceIconURLs,
1199 kSourceIconURLs + arraysize(kSizes3));
1200 DownloadTillDoneIgnoringHistory(&delegate3, &handler3, kPageURL, urls3,
1201 kSizes3);
1202 EXPECT_EQ(1u, delegate3.num_notifications());
1203 expected_index = 1u;
1204 EXPECT_EQ(48, kSizes3[expected_index]);
1205 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate3.icon_url());
1206
1207 TestDelegate delegate4;
1208 TestFaviconHandler handler4(&delegate4,
1209 FaviconDriverObserver::NON_TOUCH_16_DIP);
1210
1211 const int kSizes4[] = { 17, 256 };
1212 std::vector<FaviconURL> urls4(kSourceIconURLs,
1213 kSourceIconURLs + arraysize(kSizes4));
1214 DownloadTillDoneIgnoringHistory(&delegate4, &handler4, kPageURL, urls4,
1215 kSizes4);
1216 EXPECT_EQ(1u, delegate4.num_notifications());
1217 expected_index = 0u;
1218 EXPECT_EQ(17, kSizes4[expected_index]);
1219 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate4.icon_url());
1220 } 825 }
1221 826
pkotwicz 2017/03/17 05:43:12 Nit: Move the other 404 related tests here Report4
mastiz 2017/03/17 12:17:32 Done.
1222 // Test that the best favicon is selected when: 827 // Test that the best favicon is selected when:
1223 // - The page provides several favicons. 828 // - The page provides several favicons.
1224 // - Downloading one of the page's icon URLs previously returned a 404. 829 // - Downloading one of the page's icon URLs previously returned a 404.
1225 // - None of the favicons are cached in the Favicons database. 830 // - None of the favicons are cached in the Favicons database.
1226 TEST_F(FaviconHandlerTest, MultipleFavicons404) { 831 TEST_F(FaviconHandlerTest, MultipleFavicons404) {
1227 const GURL kPageURL("http://www.google.com"); 832 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL16x16))
1228 const GURL k404IconURL("http://www.google.com/404.png"); 833 .WillByDefault(Return(true));
1229 const FaviconURL k404FaviconURL(
1230 k404IconURL, favicon_base::FAVICON, std::vector<gfx::Size>());
1231 const FaviconURL kFaviconURLs[] = {
1232 FaviconURL(GURL("http://www.google.com/a"),
1233 favicon_base::FAVICON,
1234 std::vector<gfx::Size>()),
1235 k404FaviconURL,
1236 FaviconURL(GURL("http://www.google.com/c"),
1237 favicon_base::FAVICON,
1238 std::vector<gfx::Size>()),
1239 };
1240 834
1241 TestDelegate delegate; 835 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL64x64, _, _));
1242 TestFaviconHandler handler(&delegate, 836 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16, kIconURL64x64});
1243 FaviconDriverObserver::NON_TOUCH_16_DIP); 837 ASSERT_THAT(delegate_.downloads(), ElementsAre(kIconURL64x64));
1244 DownloadHandler* download_handler = delegate.download_handler(); 838 }
1245 839
1246 std::set<GURL> k404URLs; 840 // Test that the best favicon is selected when:
1247 k404URLs.insert(k404IconURL); 841 // - The page provides several favicons.
1248 download_handler->FailDownloadForIconURLs(k404URLs); 842 // - Downloading the last page icon URL previously returned a 404.
843 // - None of the favicons are cached in the Favicons database.
844 // - Size hints are misleading and cause the 404 icon to be processed last.
pkotwicz 2017/03/17 05:43:13 Can you please change the last point. The test tes
mastiz 2017/03/17 12:17:31 Done.
845 TEST_F(FaviconHandlerTest, MultipleFaviconsLast404) {
846 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL16x16))
847 .WillByDefault(Return(true));
1249 848
1250 // Make the initial download for |k404IconURL| fail. 849 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL64x64, _, _));
1251 const int kSizes1[] = { 0 }; 850 RunHandlerWithSimpleFaviconCandidates({kIconURL64x64, kIconURL16x16});
pkotwicz 2017/03/17 05:43:12 Can you use a differently sized URL than |kIconURL
mastiz 2017/03/17 12:17:32 Done, also for MultipleFaviconsAll404. The truth
pkotwicz 2017/03/17 15:22:31 I know that it doesn't matter. However, using 16x1
1252 std::vector<FaviconURL> urls1(1u, k404FaviconURL); 851 ASSERT_THAT(delegate_.downloads(), ElementsAre(kIconURL64x64));
1253 DownloadTillDoneIgnoringHistory(&delegate, &handler, kPageURL, urls1,
1254 kSizes1);
1255 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL));
1256
1257 // Do a fetch now that the initial download for |k404IconURL| has failed. The
1258 // behavior is different because OnDidDownloadFavicon() is invoked
1259 // synchronously from DownloadFavicon().
1260 const int kSizes2[] = { 10, 0, 16 };
1261 std::vector<FaviconURL> urls2(kFaviconURLs,
1262 kFaviconURLs + arraysize(kFaviconURLs));
1263 DownloadTillDoneIgnoringHistory(&delegate, &handler, kPageURL, urls2,
1264 kSizes2);
1265
1266 EXPECT_EQ(nullptr, handler.current_candidate());
1267 EXPECT_EQ(1u, delegate.num_notifications());
1268 EXPECT_FALSE(delegate.image().IsEmpty());
1269 int expected_index = 2u;
1270 EXPECT_EQ(16, kSizes2[expected_index]);
1271 EXPECT_EQ(kFaviconURLs[expected_index].icon_url, delegate.icon_url());
1272 } 852 }
1273 853
1274 // Test that no favicon is selected when: 854 // Test that no favicon is selected when:
1275 // - The page provides several favicons. 855 // - The page provides several favicons.
1276 // - Downloading the page's icons has previously returned a 404. 856 // - Downloading the page's icons has previously returned a 404.
1277 // - None of the favicons are cached in the Favicons database. 857 // - None of the favicons are cached in the Favicons database.
1278 TEST_F(FaviconHandlerTest, MultipleFaviconsAll404) { 858 TEST_F(FaviconHandlerTest, MultipleFaviconsAll404) {
1279 const GURL kPageURL("http://www.google.com"); 859 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL16x16))
1280 const GURL k404IconURL1("http://www.google.com/4041.png"); 860 .WillByDefault(Return(true));
1281 const GURL k404IconURL2("http://www.google.com/4042.png"); 861 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL64x64))
1282 const FaviconURL kFaviconURLs[] = { 862 .WillByDefault(Return(true));
1283 FaviconURL(k404IconURL1,
1284 favicon_base::FAVICON,
1285 std::vector<gfx::Size>()),
1286 FaviconURL(k404IconURL2,
1287 favicon_base::FAVICON,
1288 std::vector<gfx::Size>()),
1289 };
1290 863
1291 TestDelegate delegate; 864 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0);
1292 TestFaviconHandler handler(&delegate, 865 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16, kIconURL64x64});
1293 FaviconDriverObserver::NON_TOUCH_16_DIP); 866 ASSERT_THAT(delegate_.downloads(), IsEmpty());
1294 DownloadHandler* download_handler = delegate.download_handler();
1295
1296 std::set<GURL> k404URLs;
1297 k404URLs.insert(k404IconURL1);
1298 k404URLs.insert(k404IconURL2);
1299 download_handler->FailDownloadForIconURLs(k404URLs);
1300
1301 // Make the initial downloads for |kFaviconURLs| fail.
1302 for (const FaviconURL& favicon_url : kFaviconURLs) {
1303 const int kSizes[] = { 0 };
1304 std::vector<FaviconURL> urls(1u, favicon_url);
1305 DownloadTillDoneIgnoringHistory(&delegate, &handler, kPageURL, urls,
1306 kSizes);
1307 }
1308 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL1));
1309 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL2));
1310
1311 // Do a fetch now that the initial downloads for |kFaviconURLs| have failed.
1312 // The behavior is different because OnDidDownloadFavicon() is invoked
1313 // synchronously from DownloadFavicon().
1314 const int kSizes[] = { 0, 0 };
1315 std::vector<FaviconURL> urls(kFaviconURLs,
1316 kFaviconURLs + arraysize(kFaviconURLs));
1317 DownloadTillDoneIgnoringHistory(&delegate, &handler, kPageURL, urls, kSizes);
1318
1319 EXPECT_EQ(nullptr, handler.current_candidate());
1320 EXPECT_EQ(0u, delegate.num_notifications());
1321 EXPECT_TRUE(delegate.image().IsEmpty());
1322 } 867 }
1323 868
1324 // Test that no favicon is selected when the page's only icon uses an invalid 869 // Test that no favicon is selected when the page's only icon uses an invalid
1325 // URL syntax. 870 // URL syntax.
1326 TEST_F(FaviconHandlerTest, FaviconInvalidURL) { 871 TEST_F(FaviconHandlerTest, FaviconInvalidURL) {
1327 const GURL kPageURL("http://www.google.com");
1328 const GURL kInvalidFormatURL("invalid"); 872 const GURL kInvalidFormatURL("invalid");
1329 ASSERT_TRUE(kInvalidFormatURL.is_empty()); 873 ASSERT_TRUE(kInvalidFormatURL.is_empty());
1330 874
1331 FaviconURL favicon_url(kInvalidFormatURL, favicon_base::FAVICON, 875 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0);
1332 std::vector<gfx::Size>());
1333 876
1334 TestDelegate delegate; 877 RunHandlerWithSimpleFaviconCandidates({kInvalidFormatURL});
1335 TestFaviconHandler handler(&delegate, 878 EXPECT_THAT(delegate_.downloads(), IsEmpty());
1336 FaviconDriverObserver::NON_TOUCH_16_DIP);
1337 UpdateFaviconURL(&delegate, &handler, kPageURL,
1338 std::vector<FaviconURL>(1u, favicon_url));
1339 EXPECT_EQ(0u, handler.image_urls().size());
1340 } 879 }
1341 880
1342 TEST_F(FaviconHandlerTest, TestSortFavicon) { 881 TEST_F(FaviconHandlerTest, TestSortFavicon) {
1343 const GURL kPageURL("http://www.google.com"); 882 const std::vector<favicon::FaviconURL> kSourceIconURLs{
1344 std::vector<gfx::Size> icon1; 883 FaviconURL(GURL("http://www.google.com/a"), FAVICON,
1345 icon1.push_back(gfx::Size(1024, 1024)); 884 {gfx::Size(1, 1), gfx::Size(17, 17)}),
1346 icon1.push_back(gfx::Size(512, 512)); 885 FaviconURL(GURL("http://www.google.com/b"), FAVICON,
886 {gfx::Size(1024, 1024), gfx::Size(512, 512)}),
887 FaviconURL(GURL("http://www.google.com/c"), FAVICON,
888 {gfx::Size(16, 16), gfx::Size(14, 14)}),
889 FaviconURL(GURL("http://www.google.com/d"), FAVICON, kEmptySizes),
890 FaviconURL(GURL("http://www.google.com/e"), FAVICON, kEmptySizes)};
1347 891
1348 std::vector<gfx::Size> icon2; 892 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates(
1349 icon2.push_back(gfx::Size(15, 15)); 893 FaviconDriverObserver::NON_TOUCH_LARGEST, kSourceIconURLs);
1350 icon2.push_back(gfx::Size(16, 16));
1351
1352 std::vector<gfx::Size> icon3;
1353 icon3.push_back(gfx::Size(16, 16));
1354 icon3.push_back(gfx::Size(14, 14));
1355
1356 const FaviconURL kSourceIconURLs[] = {
1357 FaviconURL(GURL("http://www.google.com/a"), favicon_base::FAVICON, icon1),
1358 FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON, icon2),
1359 FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON, icon3),
1360 FaviconURL(GURL("http://www.google.com/d"),
1361 favicon_base::FAVICON,
1362 std::vector<gfx::Size>()),
1363 FaviconURL(GURL("http://www.google.com/e"),
1364 favicon_base::FAVICON,
1365 std::vector<gfx::Size>())};
1366
1367 TestDelegate delegate1;
1368 TestFaviconHandler handler1(&delegate1,
1369 FaviconDriverObserver::NON_TOUCH_LARGEST);
1370 std::vector<FaviconURL> urls1(kSourceIconURLs,
1371 kSourceIconURLs + arraysize(kSourceIconURLs));
1372 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1);
1373 894
1374 struct ExpectedResult { 895 struct ExpectedResult {
1375 // The favicon's index in kSourceIconURLs. 896 // The favicon's index in kSourceIconURLs.
1376 size_t favicon_index; 897 size_t favicon_index;
1377 // Width of largest bitmap. 898 // Width of largest bitmap.
1378 int width; 899 int width;
1379 } results[] = { 900 } results[] = {
1380 // First is icon1, though its size larger than maximal. 901 // First is icon2, though its size larger than maximal.
1381 {0, 1024}, 902 {1, 1024},
1382 // Second is icon2 903 // Second is icon1
1383 // The 16x16 is largest. 904 // The 17x17 is largest.
1384 {1, 16}, 905 {0, 17},
1385 // Third is icon3 though it has same size as icon2. 906 // Third is icon3 though it has same size as icon1.
1386 // The 16x16 is largest. 907 // The 16x16 is largest.
pkotwicz 2017/03/17 05:43:11 This comment is no longer accurate.
mastiz 2017/03/17 12:17:32 Done.
1387 {2, 16}, 908 {2, 16},
1388 // The rest of bitmaps come in order, there is no sizes attribute. 909 // The rest of bitmaps come in order, there is no sizes attribute.
pkotwicz 2017/03/17 05:43:12 Nit: Add quotes around "sizes"
mastiz 2017/03/17 12:17:32 Done.
1389 {3, -1}, 910 {3, -1},
1390 {4, -1}, 911 {4, -1},
1391 }; 912 };
1392 const std::vector<FaviconURL>& icons = handler1.image_urls(); 913 const std::vector<FaviconURL>& icons = handler->image_urls();
1393 ASSERT_EQ(5u, icons.size()); 914 ASSERT_EQ(5u, icons.size());
1394 for (size_t i = 0; i < icons.size(); ++i) { 915 for (size_t i = 0; i < icons.size(); ++i) {
1395 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url, 916 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
1396 icons[i].icon_url); 917 icons[i].icon_url);
1397 if (results[i].width != -1) 918 if (results[i].width != -1)
1398 EXPECT_EQ(results[i].width, icons[i].icon_sizes[0].width()); 919 EXPECT_EQ(results[i].width, icons[i].icon_sizes[0].width());
1399 } 920 }
1400 } 921 }
1401 922
1402 TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) { 923 TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) {
1403 const GURL kPageURL("http://www.google.com"); 924 // Names represent the bitmap sizes per icon.
1404 std::vector<gfx::Size> icon1; 925 const GURL kIconURL1024_512("http://www.google.com/a");
1405 icon1.push_back(gfx::Size(1024, 1024)); 926 const GURL kIconURL15_14("http://www.google.com/b");
1406 icon1.push_back(gfx::Size(512, 512)); 927 const GURL kIconURL16_512("http://www.google.com/c");
928 const GURL kIconURLWithoutSize1("http://www.google.com/d");
929 const GURL kIconURLWithoutSize2("http://www.google.com/e");
1407 930
1408 std::vector<gfx::Size> icon2; 931 RunHandlerWithCandidates(
1409 icon2.push_back(gfx::Size(15, 15)); 932 FaviconDriverObserver::NON_TOUCH_LARGEST,
1410 icon2.push_back(gfx::Size(14, 14)); 933 {FaviconURL(kIconURL1024_512, FAVICON,
934 {gfx::Size(1024, 1024), gfx::Size(512, 512)}),
935 FaviconURL(kIconURL15_14, FAVICON,
936 {gfx::Size(15, 15), gfx::Size(14, 14)}),
937 FaviconURL(kIconURL16_512, FAVICON,
938 {gfx::Size(16, 16), gfx::Size(512, 512)}),
939 FaviconURL(kIconURLWithoutSize1, FAVICON, kEmptySizes),
940 FaviconURL(kIconURLWithoutSize2, FAVICON, kEmptySizes)});
1411 941
1412 std::vector<gfx::Size> icon3; 942 // Icon URLs are not registered and hence 404s will be produced, which
1413 icon3.push_back(gfx::Size(16, 16)); 943 // allows checking whether the icons were requested according to their
1414 icon3.push_back(gfx::Size(512, 512)); 944 // size, which allows checking the order that the favicons were downloaded.
pkotwicz 2017/03/17 05:43:13 Nit: Remove "which allows checking whether the ico
mastiz 2017/03/17 12:17:31 Done.
1415 945 // The favicons should have been requested in decreasing order of their sizes.
1416 const FaviconURL kSourceIconURLs[] = { 946 // Favicons without any <link sizes=""> attribute should have been downloaded
1417 FaviconURL( 947 // last.
1418 GURL("http://www.google.com/a"), favicon_base::FAVICON, icon1), 948 EXPECT_THAT(delegate_.downloads(),
1419 FaviconURL( 949 ElementsAre(kIconURL1024_512, kIconURL16_512, kIconURL15_14,
1420 GURL("http://www.google.com/b"), favicon_base::FAVICON, icon2), 950 kIconURLWithoutSize1, kIconURLWithoutSize2));
1421 FaviconURL(
1422 GURL("http://www.google.com/c"), favicon_base::FAVICON, icon3),
1423 FaviconURL(GURL("http://www.google.com/d"),
1424 favicon_base::FAVICON,
1425 std::vector<gfx::Size>()),
1426 FaviconURL(GURL("http://www.google.com/e"),
1427 favicon_base::FAVICON,
1428 std::vector<gfx::Size>())};
1429
1430 TestDelegate delegate1;
1431 TestFaviconHandler handler1(&delegate1,
1432 FaviconDriverObserver::NON_TOUCH_LARGEST);
1433
1434 std::set<GURL> fail_icon_urls;
1435 for (size_t i = 0; i < arraysize(kSourceIconURLs); ++i) {
1436 fail_icon_urls.insert(kSourceIconURLs[i].icon_url);
1437 }
1438 delegate1.download_handler()->FailDownloadForIconURLs(fail_icon_urls);
1439
1440 std::vector<FaviconURL> urls1(kSourceIconURLs,
1441 kSourceIconURLs + arraysize(kSourceIconURLs));
1442 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1);
1443
1444 // Simulate the download failed, to check whether the icons were requested
1445 // to download according their size.
1446 struct ExpectedResult {
1447 // The favicon's index in kSourceIconURLs.
1448 size_t favicon_index;
1449 // Width of largest bitmap.
1450 int width;
1451 } results[] = {
1452 {0, 1024},
1453 {2, 512},
1454 {1, 15},
1455 // The rest of bitmaps come in order.
1456 {3, -1},
1457 {4, -1},
1458 };
1459
1460 for (int i = 0; i < 5; ++i) {
1461 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
1462 handler1.current_candidate()->icon_url);
1463 if (results[i].width != -1) {
1464 EXPECT_EQ(results[i].width, handler1.current_candidate()->
1465 icon_sizes[0].width());
1466 }
1467
1468 // Simulate no favicon from history.
1469 handler1.history_handler()->history_results_.clear();
1470 handler1.history_handler()->InvokeCallback();
1471
1472 // Verify download request
1473 ASSERT_TRUE(delegate1.download_handler()->HasDownload());
1474 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
1475 delegate1.download_handler()->GetImageUrl());
1476
1477 delegate1.download_handler()->InvokeCallback();
1478 delegate1.download_handler()->Reset();
1479 }
1480 } 951 }
1481 952
1482 TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) { 953 TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) {
1483 const GURL kPageURL("http://www.google.com"); 954 const GURL kIconURL1("http://www.google.com/b");
955 const GURL kIconURL2("http://www.google.com/c");
1484 956
1485 std::vector<gfx::Size> one_icon; 957 delegate_.fake_downloader().Add(kIconURL1, IntVector{15});
1486 one_icon.push_back(gfx::Size(15, 15)); 958 delegate_.fake_downloader().Add(kIconURL2, IntVector{14, 16});
1487 959
1488 std::vector<gfx::Size> two_icons; 960 // Verify NotifyFaviconAvailable().
1489 two_icons.push_back(gfx::Size(14, 14)); 961 EXPECT_CALL(delegate_,
1490 two_icons.push_back(gfx::Size(16, 16)); 962 OnFaviconUpdated(_, FaviconDriverObserver::NON_TOUCH_LARGEST,
963 kIconURL2, _, _));
1491 964
1492 const FaviconURL kSourceIconURLs[] = { 965 RunHandlerWithCandidates(
1493 FaviconURL( 966 FaviconDriverObserver::NON_TOUCH_LARGEST,
1494 GURL("http://www.google.com/b"), favicon_base::FAVICON, one_icon), 967 {FaviconURL(kIconURL1, FAVICON, {gfx::Size(15, 15)}),
1495 FaviconURL( 968 FaviconURL(kIconURL2, FAVICON, {gfx::Size(14, 14), gfx::Size(16, 16)})});
1496 GURL("http://www.google.com/c"), favicon_base::FAVICON, two_icons)};
1497 969
1498 TestDelegate delegate1; 970 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2));
1499 TestFaviconHandler handler1(&delegate1,
1500 FaviconDriverObserver::NON_TOUCH_LARGEST);
1501 std::vector<FaviconURL> urls1(kSourceIconURLs,
1502 kSourceIconURLs + arraysize(kSourceIconURLs));
1503 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1);
1504
1505 ASSERT_EQ(2u, handler1.image_urls().size());
1506
1507 // Index of largest favicon in kSourceIconURLs.
1508 size_t i = 1;
1509 // The largest bitmap's index in Favicon .
1510 int b = 1;
1511
1512 // Verify the icon_bitmaps_ was initialized correctly.
1513 EXPECT_EQ(kSourceIconURLs[i].icon_url,
1514 handler1.current_candidate()->icon_url);
1515 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
1516 handler1.current_candidate()->icon_sizes[0]);
1517
1518 // Simulate no favicon from history.
1519 handler1.history_handler()->history_results_.clear();
1520 handler1.history_handler()->InvokeCallback();
1521
1522 // Verify download request
1523 ASSERT_TRUE(delegate1.download_handler()->HasDownload());
1524 EXPECT_EQ(kSourceIconURLs[i].icon_url,
1525 delegate1.download_handler()->GetImageUrl());
1526
1527 // Give the correct download result.
1528 std::vector<int> sizes;
1529 for (std::vector<gfx::Size>::const_iterator j =
1530 kSourceIconURLs[i].icon_sizes.begin();
1531 j != kSourceIconURLs[i].icon_sizes.end(); ++j)
1532 sizes.push_back(j->width());
1533
1534 delegate1.download_handler()->SetImageSizes(sizes);
1535 delegate1.download_handler()->InvokeCallback();
1536
1537 // Verify the largest bitmap has been saved into history.
1538 EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_);
1539 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
1540 handler1.history_handler()->size_);
1541 // Verify NotifyFaviconAvailable().
1542 EXPECT_EQ(1u, delegate1.num_notifications());
1543 EXPECT_EQ(kSourceIconURLs[i].icon_url, delegate1.icon_url());
1544 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b], delegate1.image().Size());
1545 } 971 }
1546 972
1547 TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) { 973 TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) {
1548 const GURL kPageURL("http://www.google.com"); 974 const int kMaximalSize = FaviconHandler::GetMaximalIconSize(FAVICON);
1549 const int kMaximalSize =
1550 TestFaviconHandler::GetMaximalIconSize(favicon_base::FAVICON);
1551 975
1552 std::vector<gfx::Size> icon1; 976 const GURL kIconURL1("http://www.google.com/b");
1553 icon1.push_back(gfx::Size(kMaximalSize + 1, kMaximalSize + 1)); 977 const GURL kIconURL2("http://www.google.com/c");
1554 978
1555 std::vector<gfx::Size> icon2; 979 const int kOriginalSize1 = kMaximalSize + 1;
1556 icon2.push_back(gfx::Size(kMaximalSize + 2, kMaximalSize + 2)); 980 const int kOriginalSize2 = kMaximalSize + 2;
1557 981
1558 const FaviconURL kSourceIconURLs[] = { 982 delegate_.fake_downloader().AddWithOriginalSizes(
1559 FaviconURL( 983 kIconURL1, IntVector{kMaximalSize}, IntVector{kOriginalSize1});
1560 GURL("http://www.google.com/b"), favicon_base::FAVICON, icon1), 984 delegate_.fake_downloader().AddWithOriginalSizes(
1561 FaviconURL( 985 kIconURL2, IntVector{kMaximalSize}, IntVector{kOriginalSize2});
1562 GURL("http://www.google.com/c"), favicon_base::FAVICON, icon2)};
1563 986
1564 TestDelegate delegate1; 987 // Verify the largest bitmap was selected although it was scaled down to
1565 TestFaviconHandler handler1(&delegate1, 988 // maximal size and smaller than |kIconURL1| now.
1566 FaviconDriverObserver::NON_TOUCH_LARGEST); 989 EXPECT_CALL(delegate_,
1567 std::vector<FaviconURL> urls1(kSourceIconURLs, 990 OnFaviconUpdated(_, _, kIconURL2, _,
1568 kSourceIconURLs + arraysize(kSourceIconURLs)); 991 ImageSizeIs(kMaximalSize, kMaximalSize)));
1569 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1);
1570 992
1571 ASSERT_EQ(2u, handler1.image_urls().size()); 993 RunHandlerWithCandidates(
994 FaviconDriverObserver::NON_TOUCH_LARGEST,
995 {FaviconURL(kIconURL1, FAVICON,
996 SizeVector{gfx::Size(kOriginalSize1, kOriginalSize1)}),
997 FaviconURL(kIconURL2, FAVICON,
998 SizeVector{gfx::Size(kOriginalSize2, kOriginalSize2)})});
1572 999
1573 // Index of largest favicon in kSourceIconURLs. 1000 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2));
1574 size_t i = 1;
1575 // The largest bitmap's index in Favicon .
1576 int b = 0;
1577
1578 // Verify the icon_bitmaps_ was initialized correctly.
1579 EXPECT_EQ(kSourceIconURLs[i].icon_url,
1580 handler1.current_candidate()->icon_url);
1581 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
1582 handler1.current_candidate()->icon_sizes[0]);
1583
1584 // Simulate no favicon from history.
1585 handler1.history_handler()->history_results_.clear();
1586 handler1.history_handler()->InvokeCallback();
1587
1588 // Verify download request
1589 ASSERT_TRUE(delegate1.download_handler()->HasDownload());
1590 EXPECT_EQ(kSourceIconURLs[i].icon_url,
1591 delegate1.download_handler()->GetImageUrl());
1592
1593 // Give the scaled download bitmap.
1594 std::vector<int> sizes;
1595 sizes.push_back(kMaximalSize);
1596
1597 delegate1.download_handler()->SetImageSizes(sizes);
1598 delegate1.download_handler()->InvokeCallback();
1599
1600 // Verify the largest bitmap has been saved into history though it was
1601 // scaled down to maximal size and smaller than icon1 now.
1602 EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_);
1603 EXPECT_EQ(gfx::Size(kMaximalSize, kMaximalSize),
1604 handler1.history_handler()->size_);
1605 } 1001 }
1606 1002
1003 // Test that if several icons are downloaded because the icons are smaller than
1004 // expected that OnFaviconUpdated() is called with the largest downloaded
1005 // bitmap.
1607 TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) { 1006 TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) {
1608 const GURL kPageURL("http://www.google.com"); 1007 EXPECT_CALL(delegate_,
1008 OnFaviconUpdated(_, _, kIconURL12x12, _, ImageSizeIs(12, 12)));
1609 1009
1610 std::vector<gfx::Size> icon1; 1010 RunHandlerWithCandidates(
1611 icon1.push_back(gfx::Size(16, 16)); 1011 FaviconDriverObserver::NON_TOUCH_LARGEST,
1612 const int actual_size1 = 10; 1012 {FaviconURL(kIconURL10x10, FAVICON, SizeVector{gfx::Size(16, 16)}),
1613 1013 FaviconURL(kIconURL12x12, FAVICON, SizeVector{gfx::Size(15, 15)}),
1614 std::vector<gfx::Size> icon2; 1014 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)});
1615 icon2.push_back(gfx::Size(15, 15));
1616 const int actual_size2 = 12;
1617
1618 const FaviconURL kSourceIconURLs[] = {
1619 FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON, icon1),
1620 FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON, icon2),
1621 FaviconURL(GURL("http://www.google.com/d"),
1622 favicon_base::FAVICON,
1623 std::vector<gfx::Size>())};
1624
1625 TestDelegate delegate1;
1626 TestFaviconHandler handler1(&delegate1,
1627 FaviconDriverObserver::NON_TOUCH_LARGEST);
1628 std::vector<FaviconURL> urls1(kSourceIconURLs,
1629 kSourceIconURLs + arraysize(kSourceIconURLs));
1630 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1);
1631 ASSERT_EQ(3u, handler1.image_urls().size());
1632
1633 // Simulate no favicon from history.
1634 handler1.history_handler()->history_results_.clear();
1635 handler1.history_handler()->InvokeCallback();
1636
1637 // Verify the first icon was request to download
1638 ASSERT_TRUE(delegate1.download_handler()->HasDownload());
1639 EXPECT_EQ(kSourceIconURLs[0].icon_url,
1640 delegate1.download_handler()->GetImageUrl());
1641
1642 // Give the incorrect size.
1643 std::vector<int> sizes;
1644 sizes.push_back(actual_size1);
1645 delegate1.download_handler()->SetImageSizes(sizes);
1646 delegate1.download_handler()->InvokeCallback();
1647 delegate1.download_handler()->Reset();
1648
1649 // Simulate no favicon from history.
1650 handler1.history_handler()->history_results_.clear();
1651 handler1.history_handler()->InvokeCallback();
1652
1653 // Verify the 2nd icon was request to download
1654 ASSERT_TRUE(delegate1.download_handler()->HasDownload());
1655 EXPECT_EQ(kSourceIconURLs[1].icon_url,
1656 delegate1.download_handler()->GetImageUrl());
1657
1658 // Very the best candidate is icon1
1659 EXPECT_EQ(kSourceIconURLs[0].icon_url,
1660 handler1.best_favicon_candidate().image_url);
1661 EXPECT_EQ(gfx::Size(actual_size1, actual_size1),
1662 handler1.best_favicon_candidate().image.Size());
1663
1664 // Give the incorrect size.
1665 sizes.clear();
1666 sizes.push_back(actual_size2);
1667 delegate1.download_handler()->SetImageSizes(sizes);
1668 delegate1.download_handler()->InvokeCallback();
1669 delegate1.download_handler()->Reset();
1670
1671 // Verify icon2 has been saved into history.
1672 EXPECT_EQ(kSourceIconURLs[1].icon_url, handler1.history_handler()->icon_url_);
1673 EXPECT_EQ(gfx::Size(actual_size2, actual_size2),
1674 handler1.history_handler()->size_);
1675 } 1015 }
1676 1016
1677 } // namespace 1017 } // namespace
1678 } // namespace favicon 1018 } // namespace favicon
OLDNEW
« no previous file with comments | « components/favicon/core/favicon_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698