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

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

Issue 2703603002: Improve FaviconHandler tests by testing public API (Closed)
Patch Set: Reverted ifdef. 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 "components/favicon/core/favicon_client.h"
14 #include "components/favicon/core/favicon_driver.h" 18 #include "components/favicon/core/favicon_driver.h"
19 #include "components/favicon/core/test/mock_favicon_service.h"
20 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/skia/include/core/SkBitmap.h" 22 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/base/layout.h" 23 #include "ui/base/layout.h"
18 #include "ui/gfx/codec/png_codec.h" 24 #include "ui/gfx/codec/png_codec.h"
19 #include "ui/gfx/favicon_size.h" 25 #include "ui/gfx/favicon_size.h"
20 #include "ui/gfx/image/image.h" 26 #include "ui/gfx/image/image.h"
21 27
22 namespace favicon { 28 namespace favicon {
23 namespace { 29 namespace {
24 30
25 // Fill the given bmp with valid png data. 31 using favicon_base::FAVICON;
26 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { 32 using favicon_base::FaviconRawBitmapResult;
27 bmp->allocN32Pixels(w, h); 33 using favicon_base::TOUCH_ICON;
34 using favicon_base::TOUCH_PRECOMPOSED_ICON;
35 using testing::AnyNumber;
36 using testing::AtMost;
37 using testing::ElementsAre;
38 using testing::InSequence;
39 using testing::Invoke;
40 using testing::IsEmpty;
41 using testing::Return;
42 using testing::SaveArg;
43 using testing::WithArgs;
44 using testing::_;
45
46 using IntVector = std::vector<int>;
47 using URLVector = std::vector<GURL>;
48 using BitmapVector = std::vector<SkBitmap>;
49 using SizeVector = std::vector<gfx::Size>;
50
51 MATCHER_P2(ImageSizeIs, width, height, "") {
52 *result_listener << "where size is " << arg.Width() << "x" << arg.Height();
53 return arg.Size() == gfx::Size(width, height);
54 }
55
56 // Fill the given bmp with some test data.
57 SkBitmap CreateBitmap(int w, int h) {
58 SkBitmap bmp;
59 bmp.allocN32Pixels(w, h);
28 60
29 unsigned char* src_data = 61 unsigned char* src_data =
30 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0)); 62 reinterpret_cast<unsigned char*>(bmp.getAddr32(0, 0));
31 for (int i = 0; i < w * h; i++) { 63 for (int i = 0; i < w * h; i++) {
32 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); 64 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255);
33 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); 65 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255);
34 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255); 66 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255);
35 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255); 67 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255);
36 } 68 }
69 return bmp;
37 } 70 }
38 71
39 // Fill the given data buffer with valid png data. 72 // Fill the given data buffer with valid png data.
40 void FillBitmap(int w, int h, std::vector<unsigned char>* output) { 73 void FillBitmap(int w, int h, std::vector<unsigned char>* output) {
41 SkBitmap bitmap; 74 SkBitmap bitmap = CreateBitmap(w, h);
42 FillDataToBitmap(w, h, &bitmap);
43 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output); 75 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output);
44 } 76 }
45 77
46 void SetFaviconRawBitmapResult( 78 std::vector<gfx::Size> CreateSquareSizes(const IntVector& sizes) {
79 std::vector<gfx::Size> result;
80 for (int size : sizes) {
81 result.emplace_back(size, size);
82 }
83 return result;
84 }
85
86 std::vector<SkBitmap> CreateBitmaps(const std::vector<gfx::Size>& sizes) {
87 std::vector<SkBitmap> bitmaps;
88 for (const gfx::Size& size : sizes) {
89 bitmaps.push_back(CreateBitmap(size.width(), size.height()));
90 }
91 return bitmaps;
92 }
93
94 std::vector<FaviconRawBitmapResult> CreateRawBitmapResult(
47 const GURL& icon_url, 95 const GURL& icon_url,
48 favicon_base::IconType icon_type, 96 favicon_base::IconType icon_type,
49 bool expired, 97 bool expired) {
50 std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results) {
51 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); 98 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
52 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); 99 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data());
53 favicon_base::FaviconRawBitmapResult bitmap_result; 100 FaviconRawBitmapResult bitmap_result;
54 bitmap_result.expired = expired; 101 bitmap_result.expired = expired;
55 bitmap_result.bitmap_data = data; 102 bitmap_result.bitmap_data = data;
56 // Use a pixel size other than (0,0) as (0,0) has a special meaning. 103 // 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); 104 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize);
58 bitmap_result.icon_type = icon_type; 105 bitmap_result.icon_type = icon_type;
59 bitmap_result.icon_url = icon_url; 106 bitmap_result.icon_url = icon_url;
60 107 return {bitmap_result};
61 favicon_bitmap_results->push_back(bitmap_result); 108 }
62 } 109
63 110 // Fake that implements the calls to FaviconHalder::Delegate's DownloadImage(),
64 void SetFaviconRawBitmapResult( 111 // delegated to this class through MockDelegate.
65 const GURL& icon_url, 112 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: 113 public:
76 DownloadHandler() : callback_invoked_(false) {} 114 struct Response {
77 ~DownloadHandler() {} 115 int http_status_code = 404;
78 116 BitmapVector bitmaps;
79 void Reset() { 117 SizeVector original_bitmap_sizes;
80 download_.reset(); 118 };
81 callback_invoked_ = false; 119
82 // Does not affect |should_fail_download_icon_urls_| and 120 // Implementation of FaviconHalder::Delegate's DownloadImage(). If a given
83 // |failed_download_icon_urls_|. 121 // URL is not known (i.e. not previously added via Add()), it produces 404s.
84 } 122 int DownloadImage(const GURL& url,
85 123 int max_image_size,
86 // Make downloads for any of |icon_urls| fail. 124 FaviconHandler::Delegate::ImageDownloadCallback callback) {
87 void FailDownloadForIconURLs(const std::set<GURL>& icon_urls) { 125 downloads_.push_back(url);
88 should_fail_download_icon_urls_ = icon_urls; 126
89 } 127 const Response& response = responses_[url];
90 128 int download_id = static_cast<int>(downloads_.size());
91 // Returns whether a download for |icon_url| did fail. 129 base::Closure bound_callback =
92 bool DidFailDownloadForIconURL(const GURL& icon_url) const { 130 base::Bind(callback, download_id, response.http_status_code, url,
93 return failed_download_icon_urls_.count(icon_url); 131 response.bitmaps, response.original_bitmap_sizes);
94 } 132 if (url == manual_callback_url_)
95 133 manual_callback_ = bound_callback;
96 void AddDownload(int download_id, 134 else
97 const GURL& image_url, 135 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, bound_callback);
98 const std::vector<int>& image_sizes, 136 return download_id;
137 }
138
139 void Add(const GURL& icon_url, const IntVector& sizes) {
140 AddWithOriginalSizes(icon_url, sizes, sizes);
141 }
142
143 void AddWithOriginalSizes(const GURL& icon_url,
144 const IntVector& sizes,
145 const IntVector& original_sizes) {
146 DCHECK_EQ(sizes.size(), original_sizes.size());
147 Response response;
148 response.http_status_code = 200;
149 response.original_bitmap_sizes = CreateSquareSizes(original_sizes);
150 response.bitmaps = CreateBitmaps(CreateSquareSizes(sizes));
151 responses_[icon_url] = response;
152 }
153
154 void AddError(const GURL& icon_url, int http_status_code) {
155 Response response;
156 response.http_status_code = http_status_code;
157 responses_[icon_url] = response;
158 }
159
160 // Disables automatic callback for |url|. This is useful for emulating a
161 // download taking a long time. The callback will for DownloadImage() will be
162 // stored in |manual_callback_|.
163 void SetRunCallbackManuallyForUrl(const GURL& url) {
164 manual_callback_url_ = url;
165 }
166
167 // Returns whether an ongoing download exists for a url previously selected
168 // via SetRunCallbackManuallyForUrl().
169 bool HasPendingManualCallback() { return !manual_callback_.is_null(); }
170
171 // Triggers the response for a download previously selected for manual
172 // triggering via SetRunCallbackManuallyForUrl().
173 bool RunCallbackManually() {
174 if (!HasPendingManualCallback())
175 return false;
176 manual_callback_.Run();
177 return true;
178 }
179
180 // Returns pending and completed download URLs.
181 const URLVector& downloads() const { return downloads_; }
182
183 void ClearDownloads() { downloads_.clear(); }
184
185 private:
186 // URL to disable automatic callbacks for.
187 GURL manual_callback_url_;
188
189 // Pending and completed download URLs.
190 URLVector downloads_;
191
192 // Callback for DownloadImage() request for |manual_callback_url_|.
193 base::Closure manual_callback_;
194
195 // Registered images.
196 std::map<GURL, Response> responses_;
197 };
198
199 class MockDelegate : public FaviconHandler::Delegate {
200 public:
201 MockDelegate() {
202 // Delegate image downloading to FakeImageDownloader.
203 ON_CALL(*this, DownloadImage(_, _, _))
204 .WillByDefault(
205 Invoke(&fake_downloader_, &FakeImageDownloader::DownloadImage));
206 }
207
208 MOCK_METHOD3(DownloadImage,
209 int(const GURL& url,
99 int max_image_size, 210 int max_image_size,
100 FaviconHandler::Delegate::ImageDownloadCallback callback) { 211 ImageDownloadCallback callback));
101 download_.reset(new Download(download_id, image_url, image_sizes, 212 MOCK_METHOD0(IsOffTheRecord, bool());
102 max_image_size, callback)); 213 MOCK_METHOD1(IsBookmarked, bool(const GURL& url));
103 } 214 MOCK_METHOD5(OnFaviconUpdated,
104 215 void(const GURL& page_url,
105 void InvokeCallback(); 216 FaviconDriverObserver::NotificationIconType type,
106 217 const GURL& icon_url,
107 bool HasDownload() const { return download_.get(); } 218 bool icon_url_changed,
108 const GURL& GetImageUrl() const { return download_->image_url; } 219 const gfx::Image& image));
109 void SetImageSizes(const std::vector<int>& sizes) { 220
110 download_->image_sizes = sizes; } 221 FakeImageDownloader& fake_downloader() { return fake_downloader_; }
222
223 // Convenience getter for test readability. Returns pending and completed
224 // download URLs.
225 const URLVector& downloads() const { return fake_downloader_.downloads(); }
111 226
112 private: 227 private:
113 struct Download { 228 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 }; 229 };
144 230
145 // This class is used to save the history request for verifying with test case. 231 // FakeFaviconService mimics a FaviconService backend that allows setting up
146 // It also will be used to simulate the history response. 232 // test data stored via Store(). If Store() has not been called for a
147 class HistoryRequestHandler { 233 // particular URL, the callback is called with empty database results.
234 class FakeFaviconService {
148 public: 235 public:
149 HistoryRequestHandler(const GURL& page_url, 236 // Stores favicon with bitmap data in |results| at |page_or_icon_url|.
150 const GURL& icon_url, 237 void Store(const GURL& page_or_icon_url,
pkotwicz 2017/03/13 05:16:32 This function should take both a page_url and an i
mastiz 2017/03/13 12:46:03 Done.
151 int icon_type, 238 const std::vector<favicon_base::FaviconRawBitmapResult>& result) {
152 const favicon_base::FaviconResultsCallback& callback) 239 results_[page_or_icon_url] = result;
153 : page_url_(page_url), 240 }
154 icon_url_(icon_url), 241
155 icon_type_(icon_type), 242 // Returns pending and completed database request URLs.
156 callback_(callback) { 243 const URLVector& db_requests() const { return db_requests_; }
157 } 244
158 245 void ClearDbRequests() { db_requests_.clear(); }
159 HistoryRequestHandler(const GURL& page_url, 246
160 const GURL& icon_url, 247 base::CancelableTaskTracker::TaskId GetFaviconForPageOrIconURL(
161 int icon_type, 248 const GURL& page_or_icon_url,
162 const std::vector<unsigned char>& bitmap_data, 249 const favicon_base::FaviconResultsCallback& callback,
163 const gfx::Size& size) 250 base::CancelableTaskTracker* tracker) {
164 : page_url_(page_url), 251 db_requests_.push_back(page_or_icon_url);
165 icon_url_(icon_url), 252
166 icon_type_(icon_type), 253 return tracker->PostTask(base::ThreadTaskRunnerHandle::Get().get(),
167 bitmap_data_(bitmap_data), 254 FROM_HERE,
168 size_(size) { 255 base::Bind(callback, results_[page_or_icon_url]));
169 } 256 }
170 257
171 ~HistoryRequestHandler() {} 258 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch(
172 void InvokeCallback(); 259 const URLVector& icon_urls,
173 260 const favicon_base::FaviconResultsCallback& callback,
174 const GURL page_url_; 261 base::CancelableTaskTracker* tracker) {
175 const GURL icon_url_; 262 CHECK_EQ(1U, icon_urls.size()) << "Multi-icon lookup not implemented";
176 const int icon_type_; 263 return GetFaviconForPageOrIconURL(icon_urls.front(), callback, tracker);
177 const std::vector<unsigned char> bitmap_data_; 264 }
178 const gfx::Size size_;
179 std::vector<favicon_base::FaviconRawBitmapResult> history_results_;
180 favicon_base::FaviconResultsCallback callback_;
181 265
182 private: 266 private:
183 DISALLOW_COPY_AND_ASSIGN(HistoryRequestHandler); 267 std::map<GURL, std::vector<favicon_base::FaviconRawBitmapResult>> results_;
268 URLVector db_requests_;
184 }; 269 };
185 270
186 class TestDelegate : public FaviconHandler::Delegate { 271 // MockFaviconService subclass that delegates DB reads to FakeFaviconService.
272 class MockFaviconServiceWithFake : public MockFaviconService {
187 public: 273 public:
188 TestDelegate() : num_notifications_(0), download_id_(0) {} 274 MockFaviconServiceWithFake() {
189 275 // Delegate the various methods that read from the DB.
190 int DownloadImage(const GURL& image_url, 276 ON_CALL(*this, GetFavicon(_, _, _, _, _))
191 int max_bitmap_size, 277 .WillByDefault(WithArgs<0, 3, 4>(
192 ImageDownloadCallback callback) override { 278 Invoke(&fake_, &FakeFaviconService::GetFaviconForPageOrIconURL)));
pkotwicz 2017/03/13 05:16:32 Nit: Add to FakeFaviconService FakeFaviconService:
mastiz 2017/03/13 12:46:04 Done.
193 // Do not do a download if downloading |image_url| failed previously. This 279 ON_CALL(*this, GetFaviconForPageURL(_, _, _, _, _))
194 // emulates the behavior of FaviconDriver::DownloadImage() 280 .WillByDefault(WithArgs<0, 3, 4>(
195 if (download_handler_.DidFailDownloadForIconURL(image_url)) { 281 Invoke(&fake_, &FakeFaviconService::GetFaviconForPageOrIconURL)));
196 download_handler_.AddDownload(download_id_, image_url, std::vector<int>(), 282 ON_CALL(*this, UpdateFaviconMappingsAndFetch(_, _, _, _, _, _))
197 0, callback); 283 .WillByDefault(WithArgs<1, 4, 5>(Invoke(
198 return 0; 284 &fake_, &FakeFaviconService::UpdateFaviconMappingsAndFetch)));
199 } 285 }
200 286
201 download_id_++; 287 FakeFaviconService& fake() { return fake_; }
pkotwicz 2017/03/13 05:16:32 Returning a non const reference is kind of weird.
mastiz 2017/03/13 12:46:05 Done.
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 288
230 private: 289 private:
231 GURL icon_url_; 290 FakeFaviconService fake_;
pkotwicz 2017/03/13 05:16:31 DISALLOW_COPY_AND_ASSIGN(MockFaviconServiceWithFak
mastiz 2017/03/13 12:46:05 Done.
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 }; 291 };
243 292
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,
286 favicon_base::IconType icon_type,
287 const favicon_base::FaviconResultsCallback& callback,
288 base::CancelableTaskTracker* tracker) override {
289 history_handler_.reset(new HistoryRequestHandler(page_url, icon_url,
290 icon_type, callback));
291 }
292
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,
304 int icon_types,
305 const favicon_base::FaviconResultsCallback& callback,
306 base::CancelableTaskTracker* tracker) override {
307 history_handler_.reset(new HistoryRequestHandler(page_url, GURL(),
308 icon_types, callback));
309 }
310
311 void SetHistoryFavicons(const GURL& page_url,
312 const GURL& icon_url,
313 favicon_base::IconType icon_type,
314 const gfx::Image& image) override {
315 scoped_refptr<base::RefCountedMemory> bytes = image.As1xPNGBytes();
316 std::vector<unsigned char> bitmap_data(bytes->front(),
317 bytes->front() + bytes->size());
318 history_handler_.reset(new HistoryRequestHandler(
319 page_url, icon_url, icon_type, bitmap_data, image.Size()));
320 }
321
322 bool ShouldSaveFavicon() override { return true; }
323
324 GURL page_url_;
325
326 private:
327 std::unique_ptr<HistoryRequestHandler> history_handler_;
328
329 DISALLOW_COPY_AND_ASSIGN(TestFaviconHandler);
330 };
331
332 namespace {
333
334 void HistoryRequestHandler::InvokeCallback() {
335 if (!callback_.is_null()) {
336 callback_.Run(history_results_);
337 }
338 }
339
340 void DownloadHandler::InvokeCallback() {
341 if (callback_invoked_)
342 return;
343
344 std::vector<gfx::Size> original_bitmap_sizes;
345 std::vector<SkBitmap> bitmaps;
346 if (should_fail_download_icon_urls_.count(download_->image_url)) {
347 failed_download_icon_urls_.insert(download_->image_url);
348 } else {
349 for (std::vector<int>::const_iterator i = download_->image_sizes.begin();
350 i != download_->image_sizes.end(); ++i) {
351 int original_size = (*i > 0) ? *i : gfx::kFaviconSize;
352 int downloaded_size = original_size;
353 if (download_->max_image_size != 0 &&
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
369 class FaviconHandlerTest : public testing::Test { 293 class FaviconHandlerTest : public testing::Test {
370 protected: 294 protected:
295 const std::vector<gfx::Size> kEmptySizes;
296 const std::vector<SkBitmap> kEmptyBitmaps;
pkotwicz 2017/03/13 05:16:33 You don't seem to use |kEmptyBitmaps| anywhere
mastiz 2017/03/13 12:46:04 Done, thx.
297 const std::vector<FaviconRawBitmapResult> kEmptyRawBitmapResult;
pkotwicz 2017/03/13 05:16:31 You don't seem to use |kEmptyRawBitmapResult| anyw
mastiz 2017/03/13 12:46:03 Done.
298
299 // Some known icons for which download will succeed.
300 const GURL kPageURL = GURL("http://www.google.com");
301 const GURL kIconURL10x10 = GURL("http://www.google.com/favicon10x10");
302 const GURL kIconURL12x12 = GURL("http://www.google.com/favicon12x12");
303 const GURL kIconURL16x16 = GURL("http://www.google.com/favicon16x16");
304 const GURL kIconURL64x64 = GURL("http://www.google.com/favicon64x64");
305
371 FaviconHandlerTest() { 306 FaviconHandlerTest() {
372 } 307 // Register various known icon URLs.
373 308 delegate_.fake_downloader().Add(kIconURL10x10, IntVector{10});
374 ~FaviconHandlerTest() override {} 309 delegate_.fake_downloader().Add(kIconURL12x12, IntVector{12});
310 delegate_.fake_downloader().Add(kIconURL16x16, IntVector{16});
311 delegate_.fake_downloader().Add(kIconURL64x64, IntVector{64});
pkotwicz 2017/03/13 05:16:33 Super Nit: Can you please add a new line?
mastiz 2017/03/13 12:46:05 Done.
312 // The score computed by SelectFaviconFrames() is dependent on the supported
313 // scale factors of the platform. It is used for determining the goodness of
314 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon().
315 // Force the values of the scale factors so that the tests produce the same
316 // results on all platforms.
317 scoped_set_supported_scale_factors_.reset(
318 new ui::test::ScopedSetSupportedScaleFactors({ui::SCALE_FACTOR_100P}));
319 }
320
321 bool VerifyAndClearExpectations() {
322 base::RunLoop().RunUntilIdle();
323 favicon_service_.fake().ClearDbRequests();
324 delegate_.fake_downloader().ClearDownloads();
325 return testing::Mock::VerifyAndClearExpectations(&favicon_service_) &&
326 testing::Mock::VerifyAndClearExpectations(&delegate_);
327 }
328
329 // Creates a new handler and feeds in the page URL and the candidates.
330 // Returns the handler in case tests want to exercise further steps.
331 std::unique_ptr<FaviconHandler> RunHandlerWithCandidates(
332 FaviconDriverObserver::NotificationIconType handler_type,
333 const std::vector<favicon::FaviconURL>& candidates,
334 bool flush_events_before_candidates = true) {
pkotwicz 2017/03/13 05:16:31 In my opinion, tests which care about |flush_event
mastiz 2017/03/13 12:46:03 Done.
335 auto handler = base::MakeUnique<FaviconHandler>(&favicon_service_,
336 &delegate_, handler_type);
337 handler->FetchFavicon(kPageURL);
338 // The first RunUntilIdle() causes the FaviconService lookups be faster than
339 // OnUpdateFaviconURL(), which is the most likely scenario.
340 if (flush_events_before_candidates)
341 base::RunLoop().RunUntilIdle();
342 handler->OnUpdateFaviconURL(kPageURL, candidates);
343 base::RunLoop().RunUntilIdle();
344 return handler;
345 }
346
347 // Same as above, but for the simplest case where all types are FAVICON and
348 // no sizes are provided.
349 std::unique_ptr<FaviconHandler> RunHandlerWithSimpleFaviconCandidates(
350 const std::vector<GURL>& urls) {
351 std::vector<favicon::FaviconURL> candidates;
352 for (const GURL& url : urls) {
353 candidates.emplace_back(url, FAVICON, kEmptySizes);
354 }
355 return RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP,
356 candidates);
357 }
375 358
376 // Simulates requesting a favicon for |page_url| given: 359 // Simulates requesting a favicon for |page_url| given:
377 // - We have not previously cached anything in history for |page_url| or for 360 // - We have not previously cached anything in history for |page_url| or for
378 // any of |candidates|. 361 // any of |candidates|.
379 // - The page provides favicons at |candidate_icons|. 362 // - The page provides favicons at |candidate_icons|.
380 // - The favicons at |candidate_icons| have edge pixel sizes of 363 // - The favicons at |candidate_icons| have edge pixel sizes of
381 // |candidate_icon_sizes|. 364 // |candidate_icon_sizes|.
382 void DownloadTillDoneIgnoringHistory( 365 //
383 TestDelegate* delegate, 366 // Returns the chosen size among |candidate_icon_sizes| or -1 if none was
384 TestFaviconHandler* favicon_handler, 367 // chosen.
385 const GURL& page_url, 368 int DownloadTillDoneIgnoringHistory(
386 const std::vector<FaviconURL>& candidate_icons, 369 const std::vector<FaviconURL>& candidate_icons,
387 const int* candidate_icon_sizes) { 370 const IntVector& candidate_icon_sizes) {
pkotwicz 2017/03/13 05:16:33 It looks like all of the tests which call this fun
mastiz 2017/03/13 12:46:03 Done. For the URL, I used the size instead, e.g. 1
388 size_t old_num_notifications = delegate->num_notifications(); 371 DCHECK_EQ(candidate_icons.size(), candidate_icon_sizes.size());
389 372 // Set up 200 responses for all images, and the corresponding size.
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) { 373 for (size_t i = 0; i < candidate_icons.size(); ++i) {
395 favicon_handler->history_handler()->history_results_.clear(); 374 const FaviconURL& candidate_icon = candidate_icons[i];
396 favicon_handler->history_handler()->InvokeCallback(); 375 const GURL& icon_url = candidate_icon.icon_url;
397 ASSERT_TRUE(download_handler->HasDownload()); 376 const int icon_size = candidate_icon_sizes[i];
398 EXPECT_EQ(download_handler->GetImageUrl(), 377 delegate_.fake_downloader().Add(icon_url, IntVector{icon_size});
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 } 378 }
379
380 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, _, FAVICON, _))
381 .Times(AtMost(1));
pkotwicz 2017/03/13 05:16:32 Like the majority of tests this function does not
mastiz 2017/03/13 12:46:04 Done.
382
383 GURL chosen_icon_url;
384 ON_CALL(delegate_, OnFaviconUpdated(
385 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP, _,
386 /*icon_url_changed=*/true, _))
pkotwicz 2017/03/13 05:16:34 The icon URL is the only parameter that we care ab
mastiz 2017/03/13 12:46:04 Done.
387 .WillByDefault(SaveArg<2>(&chosen_icon_url));
388
389 RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP,
390 candidate_icons);
391 for (size_t i = 0; i < candidate_icons.size(); ++i) {
392 if (candidate_icons[i].icon_url == chosen_icon_url)
393 return candidate_icon_sizes[i];
394 }
395 return -1;
410 } 396 }
411 397
412 void UpdateFaviconURL(TestDelegate* delegate, 398 base::MessageLoopForUI message_loop_;
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
426 // scale factors of the platform. It is used for determining the goodness of
427 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon().
428 // Force the values of the scale factors so that the tests produce the same
429 // 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(
433 new ui::test::ScopedSetSupportedScaleFactors(scale_factors));
434 testing::Test::SetUp();
435 }
436
437 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> 399 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors>
438 scoped_set_supported_scale_factors_; 400 scoped_set_supported_scale_factors_;
401 testing::NiceMock<MockFaviconServiceWithFake> favicon_service_;
402 testing::NiceMock<MockDelegate> delegate_;
439 }; 403 };
440 404
441 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) { 405 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
442 const GURL page_url("http://www.google.com"); 406 const GURL kIconURL("http://www.google.com/favicon");
443 const GURL icon_url("http://www.google.com/favicon"); 407
444 408 favicon_service_.fake().Store(
445 TestDelegate delegate; 409 kPageURL, CreateRawBitmapResult(kIconURL, FAVICON, /*expired=*/false));
446 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 410
447 411 // Shouldn't request to download icon.
448 helper.FetchFavicon(page_url); 412 EXPECT_CALL(delegate_, OnFaviconUpdated(
449 HistoryRequestHandler* history_handler = helper.history_handler(); 413 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
450 // Ensure the data given to history is correct. 414 kIconURL, /*icon_url_changed=*/true, _));
451 ASSERT_TRUE(history_handler); 415
452 EXPECT_EQ(page_url, history_handler->page_url_); 416 RunHandlerWithSimpleFaviconCandidates({kIconURL});
453 EXPECT_EQ(GURL(), history_handler->icon_url_); 417 EXPECT_THAT(delegate_.downloads(), IsEmpty());
454 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 418 }
455 419
pkotwicz 2017/03/13 05:16:33 For the comment, how about: Test that the FaviconH
mastiz 2017/03/13 12:46:03 Done.
456 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_); 420 TEST_F(FaviconHandlerTest, DownloadUnknownFavicon) {
457 421 InSequence seq;
458 // Send history response. 422 EXPECT_CALL(favicon_service_,
459 history_handler->InvokeCallback(); 423 UpdateFaviconMappingsAndFetch(kPageURL, URLVector{kIconURL16x16},
460 // Verify FaviconHandler status 424 FAVICON, _, _, _));
461 EXPECT_EQ(1u, delegate.num_notifications()); 425 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON,
462 EXPECT_EQ(icon_url, delegate.icon_url()); 426 ImageSizeIs(16, 16)));
463 427 EXPECT_CALL(delegate_, OnFaviconUpdated(
464 // Simulates update favicon url. 428 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
465 std::vector<FaviconURL> urls; 429 kIconURL16x16, /*icon_url_changed=*/true, _));
pkotwicz 2017/03/13 05:16:32 For the sake of this test, can you call FaviconHan
mastiz 2017/03/13 12:46:03 Done.
466 urls.push_back( 430
467 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); 431 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
468 helper.OnUpdateFaviconURL(page_url, urls); 432 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
469 433 }
470 // Verify FaviconHandler status 434
pkotwicz 2017/03/13 05:16:34 For the comment, how about: "Test that the Favico
mastiz 2017/03/13 12:46:05 Done.
471 EXPECT_EQ(1u, helper.image_urls().size()); 435 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconInIncognito) {
472 ASSERT_TRUE(helper.current_candidate()); 436 ON_CALL(delegate_, IsOffTheRecord()).WillByDefault(Return(true));
473 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); 437
474 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type); 438 // No writes expected.
475 439 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(_, _, _, _, _, _))
476 // Favicon shouldn't request to download icon. 440 .Times(0);
477 EXPECT_FALSE(delegate.download_handler()->HasDownload()); 441 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0);
478 } 442
479 443 InSequence seq;
pkotwicz 2017/03/13 05:16:31 InSequence when there is just one EXPECT_CALL() in
mastiz 2017/03/13 12:46:04 Done, this was a leftover.
480 TEST_F(FaviconHandlerTest, DownloadFavicon) { 444 EXPECT_CALL(delegate_,
481 const GURL page_url("http://www.google.com"); 445 OnFaviconUpdated(_, _, _, /*icon_url_changed=*/true, _));
pkotwicz 2017/03/13 05:16:32 We care about the "icon URL" parameter but not the
mastiz 2017/03/13 12:46:05 Done.
482 const GURL icon_url("http://www.google.com/favicon"); 446
483 447 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
484 TestDelegate delegate; 448 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
485 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 449 }
486 450
pkotwicz 2017/03/13 05:16:34 For the comment, how about: Test that the FaviconH
mastiz 2017/03/13 12:46:03 Done.
487 helper.FetchFavicon(page_url); 451 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesFaster) {
488 HistoryRequestHandler* history_handler = helper.history_handler(); 452 InSequence seq;
489 // Ensure the data given to history is correct. 453 EXPECT_CALL(favicon_service_,
490 ASSERT_TRUE(history_handler); 454 UpdateFaviconMappingsAndFetch(kPageURL, URLVector{kIconURL16x16},
491 EXPECT_EQ(page_url, history_handler->page_url_); 455 FAVICON, _, _, _));
492 EXPECT_EQ(GURL(), history_handler->icon_url_); 456 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON,
493 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 457 ImageSizeIs(16, 16)));
494 458 EXPECT_CALL(delegate_,
495 // Set icon data expired 459 OnFaviconUpdated(_, _, _, /*icon_url_changed=*/true, _));
496 SetFaviconRawBitmapResult(icon_url, 460
497 favicon_base::FAVICON, 461 RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP,
498 true /* expired */, 462 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)},
499 &history_handler->history_results_); 463 /*flush_events_before_candidates=*/false);
pkotwicz 2017/03/13 05:16:31 For the sake of this test, can you call FaviconHan
mastiz 2017/03/13 12:46:03 Done.
500 // Send history response. 464 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
501 history_handler->InvokeCallback(); 465 }
502 // Verify FaviconHandler status 466
pkotwicz 2017/03/13 05:16:33 Nits: How about this as the test comment: - "Test
mastiz 2017/03/13 12:46:04 Done.
503 EXPECT_EQ(1u, delegate.num_notifications()); 467 TEST_F(FaviconHandlerTest, RedownloadExpiredFavicon) {
504 EXPECT_EQ(icon_url, delegate.icon_url()); 468 favicon_service_.fake().Store(
505 469 kPageURL,
506 // Simulates update favicon url. 470 CreateRawBitmapResult(kIconURL16x16, FAVICON, /*expired=*/true));
507 std::vector<FaviconURL> urls; 471
508 urls.push_back( 472 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(_, _, _, _, _, _))
509 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); 473 .Times(0);
pkotwicz 2017/03/13 05:16:31 This should be EXPECT_THAT(favicon_service_.fake()
mastiz 2017/03/13 12:46:03 Done.
510 helper.OnUpdateFaviconURL(page_url, urls); 474
511 475 InSequence seq;
512 // Verify FaviconHandler status 476 EXPECT_CALL(delegate_,
513 EXPECT_EQ(1u, helper.image_urls().size()); 477 OnFaviconUpdated(_, _, _, /*icon_url_changed=*/true, _));
pkotwicz 2017/03/13 05:16:33 kIconURL16x16 should be part of the EXPECT_CALL()
mastiz 2017/03/13 12:46:04 Done.
514 ASSERT_TRUE(helper.current_candidate()); 478 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON,
515 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); 479 ImageSizeIs(16, 16)));
pkotwicz 2017/03/13 05:16:31 kIconURL16x16 is the only parameter that we care a
mastiz 2017/03/13 12:46:05 Done.
516 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type); 480 // TODO(mastiz): It would be nice if we could check whether the two
517 481 // OnFaviconUpdated() calls are called with different gfx::Images (as opposed
518 // Favicon should request to download icon now. 482 // to calling OnFaviconUpdated() with the expired gfx::Image both times).
519 DownloadHandler* download_handler = delegate.download_handler(); 483 EXPECT_CALL(delegate_,
520 EXPECT_TRUE(download_handler->HasDownload()); 484 OnFaviconUpdated(_, _, _, /*icon_url_changed=*/false, _));
521 485
522 // Verify the download request. 486 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
523 EXPECT_EQ(icon_url, download_handler->GetImageUrl()); 487 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
524 488 }
525 // Reset the history_handler to verify whether favicon is set. 489
526 helper.set_history_handler(nullptr); 490 TEST_F(FaviconHandlerTest, Report404) {
527 491 const GURL k404IconURL("http://www.google.com/404.png");
528 // Smulates download done. 492
529 download_handler->InvokeCallback(); 493 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(k404IconURL));
530 494
531 // New icon should be saved to history backend and navigation entry. 495 RunHandlerWithSimpleFaviconCandidates({k404IconURL});
532 history_handler = helper.history_handler(); 496 EXPECT_THAT(delegate_.downloads(), ElementsAre(k404IconURL));
533 ASSERT_TRUE(history_handler); 497 }
534 EXPECT_EQ(icon_url, history_handler->icon_url_); 498
535 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 499 // Test that WasUnableToDownloadFavicon() is not called if a download returns
536 EXPECT_LT(0U, history_handler->bitmap_data_.size()); 500 // HTTP status 503.
537 EXPECT_EQ(page_url, history_handler->page_url_); 501 TEST_F(FaviconHandlerTest, NotReport503) {
538 502 const GURL k503IconURL("http://www.google.com/503.png");
539 // Verify NavigationEntry. 503
540 EXPECT_EQ(2u, delegate.num_notifications()); 504 delegate_.fake_downloader().AddError(k503IconURL, 503);
541 EXPECT_EQ(icon_url, delegate.icon_url()); 505
542 EXPECT_FALSE(delegate.image().IsEmpty()); 506 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0);
543 EXPECT_EQ(gfx::kFaviconSize, delegate.image().Width()); 507
544 } 508 RunHandlerWithSimpleFaviconCandidates({k503IconURL});
545 509 EXPECT_THAT(delegate_.downloads(), ElementsAre(k503IconURL));
510 }
511
512 // Test that FaviconHandler process finishes when:
513 // - The icon URL used by the page has changed.
514 // AND
515 // - FaviconService::GetFaviconForPageURL() callback returns before
516 // FaviconHandler::OnUpdateFaviconURL() is called.
546 TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) { 517 TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) {
547 const GURL page_url("http://www.google.com"); 518 const GURL kOldIconURL("http://www.google.com/old_favicon");
548 const GURL icon_url("http://www.google.com/favicon"); 519 const GURL kNewIconURL = kIconURL16x16;
549 const GURL new_icon_url("http://www.google.com/new_favicon"); 520
550 521 favicon_service_.fake().Store(
551 TestDelegate delegate; 522 kPageURL, CreateRawBitmapResult(kOldIconURL, FAVICON, /*expired=*/false));
552 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 523
553 524 InSequence seq;
554 helper.FetchFavicon(page_url); 525 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kOldIconURL,
555 HistoryRequestHandler* history_handler = helper.history_handler(); 526 /*icon_url_changed=*/true, _));
556 // Ensure the data given to history is correct. 527 EXPECT_CALL(favicon_service_,
557 ASSERT_TRUE(history_handler); 528 UpdateFaviconMappingsAndFetch(_, URLVector{kNewIconURL}, FAVICON,
558 EXPECT_EQ(page_url, history_handler->page_url_); 529 _, _, _));
pkotwicz 2017/03/13 05:16:32 Nit: We don't care about whether UpdateFaviconMapp
mastiz 2017/03/13 12:46:04 Done.
559 EXPECT_EQ(GURL(), history_handler->icon_url_); 530 EXPECT_CALL(favicon_service_, SetFavicons(_, kNewIconURL, _, _));
560 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 531 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kNewIconURL,
561 532 /*icon_url_changed=*/true, _));
pkotwicz 2017/03/13 05:16:33 We don't care about the |icon_url_changed| paramet
mastiz 2017/03/13 12:46:03 Done.
562 // Set valid icon data. 533
563 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_); 534 RunHandlerWithSimpleFaviconCandidates({kNewIconURL});
564 535 EXPECT_THAT(delegate_.downloads(), ElementsAre(kNewIconURL));
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 } 536 }
625 537
pkotwicz 2017/03/13 05:16:32 For the comment, how about: If there is data for
mastiz 2017/03/13 12:46:04 Done.
626 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) { 538 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. 539 // Set non empty but invalid data.
642 favicon_base::FaviconRawBitmapResult bitmap_result; 540 FaviconRawBitmapResult bitmap_result;
643 bitmap_result.expired = false; 541 bitmap_result.expired = false;
644 // Empty bitmap data is invalid. 542 // Empty bitmap data is invalid.
645 bitmap_result.bitmap_data = new base::RefCountedBytes(); 543 bitmap_result.bitmap_data = new base::RefCountedBytes();
646 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); 544 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize);
647 bitmap_result.icon_type = favicon_base::FAVICON; 545 bitmap_result.icon_type = FAVICON;
648 bitmap_result.icon_url = icon_url; 546 bitmap_result.icon_url = kIconURL16x16;
649 history_handler->history_results_.clear(); 547
650 history_handler->history_results_.push_back(bitmap_result); 548 favicon_service_.fake().Store(kPageURL, {bitmap_result});
651 549
652 // Send history response. 550 InSequence seq;
653 history_handler->InvokeCallback(); 551 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, _, _));
pkotwicz 2017/03/13 05:16:31 We don't care about the FaviconService::SetFavicon
mastiz 2017/03/13 12:46:04 Done.
654 // The NavigationEntry should not be set yet as the history data is invalid. 552 EXPECT_CALL(delegate_, OnFaviconUpdated(
655 EXPECT_EQ(0u, delegate.num_notifications()); 553 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
656 EXPECT_EQ(GURL(), delegate.icon_url()); 554 kIconURL16x16, /*icon_url_changed=*/true, _));
pkotwicz 2017/03/13 05:16:31 We don't care about any of the parameters to Deleg
mastiz 2017/03/13 12:46:04 Done and added TODO.
657 555
658 // Reset the history_handler to verify whether new icon is requested from 556 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
pkotwicz 2017/03/13 05:16:32 You should also check that |kPageURL| was requeste
mastiz 2017/03/13 12:46:05 Done. You might want to replace ElementsAre(kPage
659 // history. 557 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
660 helper.set_history_handler(nullptr); 558 }
661 559
662 // Simulates update with matching favicon URL. 560 // Test that no downloads are done if a user visits a page which changed its
663 std::vector<FaviconURL> urls; 561 // 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) { 562 TEST_F(FaviconHandlerTest, UpdateFavicon) {
696 const GURL page_url("http://www.google.com"); 563 const GURL kIconURL("http://www.google.com/favicon");
697 const GURL icon_url("http://www.google.com/favicon"); 564 const GURL kNewIconURL("http://www.google.com/new_favicon");
698 const GURL new_icon_url("http://www.google.com/new_favicon"); 565
699 566 favicon_service_.fake().Store(
700 TestDelegate delegate; 567 kPageURL, CreateRawBitmapResult(kIconURL, FAVICON, /*expired=*/false));
701 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 568 favicon_service_.fake().Store(kNewIconURL,
702 569 CreateRawBitmapResult(kNewIconURL, FAVICON,
703 helper.FetchFavicon(page_url); 570 /*expired=*/false));
pkotwicz 2017/03/13 05:16:32 I think that the code would be more readable if St
mastiz 2017/03/13 12:46:03 Done.
704 HistoryRequestHandler* history_handler = helper.history_handler(); 571
705 // Ensure the data given to history is correct. 572 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0);
706 ASSERT_TRUE(history_handler); 573
707 EXPECT_EQ(page_url, history_handler->page_url_); 574 InSequence seq;
708 EXPECT_EQ(GURL(), history_handler->icon_url_); 575 EXPECT_CALL(delegate_, OnFaviconUpdated(
709 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); 576 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
710 577 kIconURL, /*icon_url_changed=*/true, _));
pkotwicz 2017/03/13 05:16:32 This test doesn't care about any of the parameters
mastiz 2017/03/13 12:46:03 Done.
711 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_); 578 EXPECT_CALL(favicon_service_,
712 579 UpdateFaviconMappingsAndFetch(kPageURL, URLVector{kNewIconURL},
713 // Send history response. 580 FAVICON, _, _, _));
pkotwicz 2017/03/13 05:16:31 This should be replaced with EXPECT_THAT(favicon_s
mastiz 2017/03/13 12:46:03 Done.
714 history_handler->InvokeCallback(); 581 EXPECT_CALL(delegate_, OnFaviconUpdated(
715 // Verify FaviconHandler status. 582 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
716 EXPECT_EQ(1u, delegate.num_notifications()); 583 kNewIconURL, /*icon_url_changed=*/true, _));
pkotwicz 2017/03/13 05:16:33 This test doesn't care about any of the parameters
mastiz 2017/03/13 12:46:03 Done.
717 EXPECT_EQ(icon_url, delegate.icon_url()); 584
718 585 RunHandlerWithSimpleFaviconCandidates({kNewIconURL});
719 // Reset the history_handler to verify whether new icon is requested from 586 EXPECT_THAT(delegate_.downloads(), IsEmpty());
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 } 587 }
754 588
755 TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) { 589 TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
756 const GURL page_url("http://www.google.com"); 590 const GURL kIconURLReturning500("http://www.google.com/500.png");
757 const GURL icon_url("http://www.google.com/favicon"); 591 delegate_.fake_downloader().AddError(kIconURLReturning500, 500);
758 const GURL new_icon_url("http://www.google.com/new_favicon"); 592
759 593 favicon_service_.fake().Store(kPageURL,
760 TestDelegate delegate; 594 CreateRawBitmapResult(kIconURL64x64, TOUCH_ICON,
761 TestFaviconHandler helper(&delegate, FaviconDriverObserver::TOUCH_LARGEST); 595 /*expired=*/true));
762 std::set<GURL> fail_downloads; 596
763 fail_downloads.insert(icon_url); 597 EXPECT_CALL(delegate_,
764 delegate.download_handler()->FailDownloadForIconURLs(fail_downloads); 598 OnFaviconUpdated(kPageURL, FaviconDriverObserver::TOUCH_LARGEST,
765 599 kIconURL64x64, /*icon_url_changed=*/true, _));
766 helper.FetchFavicon(page_url); 600 EXPECT_CALL(delegate_,
767 HistoryRequestHandler* history_handler = helper.history_handler(); 601 OnFaviconUpdated(kPageURL, FaviconDriverObserver::TOUCH_LARGEST,
768 // Ensure the data given to history is correct. 602 kIconURL64x64, /*icon_url_changed=*/false, _));
769 ASSERT_TRUE(history_handler); 603
770 EXPECT_EQ(page_url, history_handler->page_url_); 604 RunHandlerWithCandidates(
771 EXPECT_EQ(GURL(), history_handler->icon_url_); 605 FaviconDriverObserver::TOUCH_LARGEST,
772 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON, 606 {
773 history_handler->icon_type_); 607 FaviconURL(kIconURLReturning500, TOUCH_PRECOMPOSED_ICON, kEmptySizes),
774 608 FaviconURL(kIconURL64x64, TOUCH_ICON, kEmptySizes),
775 // Icon not found. 609 });
776 history_handler->history_results_.clear(); 610 // First download fails, second succeeds.
777 // Send history response. 611 EXPECT_THAT(delegate_.downloads(),
778 history_handler->InvokeCallback(); 612 ElementsAre(kIconURLReturning500, kIconURL64x64));
779 // Verify FaviconHandler status. 613 }
780 EXPECT_EQ(0u, delegate.num_notifications()); 614
781 EXPECT_EQ(GURL(), delegate.icon_url()); 615 // TODO(mastiz): Make this test deal with FaviconURLs of type
782 616 // favicon_base::FAVICON and add new ones like OnlyDownloadMatchingIconType and
783 // Reset the history_handler to verify whether new icon is requested from 617 // CallSetFaviconsWithCorrectIconType.
784 // history.
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) {
871 const GURL page_url("http://www.google.com"); 619 const GURL kIconURL("http://www.google.com/favicon");
872 const GURL icon_url("http://www.google.com/favicon"); 620 const GURL kNewIconURL = kIconURL16x16;
pkotwicz 2017/03/13 05:16:34 I know that the old test used the name |kNewIconUR
mastiz 2017/03/13 12:46:03 Done.
873 const GURL new_icon_url("http://www.google.com/new_favicon"); 621 const GURL kLatestIconURL = 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);
878 helper.FetchFavicon(page_url); 626 delegate_.fake_downloader().Add(kIconURL, IntVector{16});
pkotwicz 2017/03/13 05:16:33 To stay true to the original test (and your previo
mastiz 2017/03/13 12:46:04 Done.
879 HistoryRequestHandler* history_handler = helper.history_handler(); 627
880 // Ensure the data given to history is correct. 628 std::unique_ptr<FaviconHandler> handler =
881 ASSERT_TRUE(history_handler); 629 RunHandlerWithSimpleFaviconCandidates({kIconURL, kNewIconURL});
882 EXPECT_EQ(page_url, history_handler->page_url_); 630
883 EXPECT_EQ(GURL(), history_handler->icon_url_); 631 ASSERT_TRUE(VerifyAndClearExpectations());
884 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON, 632 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback());
885 history_handler->icon_type_); 633
886 634 // Favicon update should invalidate the ongoing download.
887 // Icon not found. 635 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL, _, _)).Times(0);
888 history_handler->history_results_.clear(); 636 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL, _, _)).Times(0);
pkotwicz 2017/03/13 05:16:33 Don't the EXPECT_CALL() statements on lines 638 an
mastiz 2017/03/13 12:46:04 They implicitly do, but I wanted to be more explic
889 // Send history response. 637
890 history_handler->InvokeCallback(); 638 EXPECT_CALL(favicon_service_, SetFavicons(_, kLatestIconURL, _, _));
891 // Verify FaviconHandler status. 639 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kLatestIconURL, _, _));
892 EXPECT_EQ(0u, delegate.num_notifications()); 640
893 EXPECT_EQ(GURL(), delegate.icon_url()); 641 handler->OnUpdateFaviconURL(
894 642 kPageURL, {FaviconURL(kLatestIconURL, FAVICON, kEmptySizes)});
895 // Reset the history_handler to verify whether new icon is requested from 643
896 // history. 644 // Finalizes download, which should be thrown away as there is favicon update.
pkotwicz 2017/03/13 05:16:34 Nit: "as there is favicon update." -> "as the favi
mastiz 2017/03/13 12:46:04 Done.
897 helper.set_history_handler(nullptr); 645 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually());
898 646 base::RunLoop().RunUntilIdle();
899 // Simulates update with the different favicon url. 647
900 std::vector<FaviconURL> urls; 648 EXPECT_THAT(favicon_service_.fake().db_requests(),
901 urls.push_back(FaviconURL(icon_url, 649 ElementsAre(kLatestIconURL));
902 favicon_base::TOUCH_PRECOMPOSED_ICON, 650 EXPECT_THAT(delegate_.downloads(), ElementsAre(kLatestIconURL));
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 } 651 }
982 652
983 // Test that sending an icon URL update identical to the previous icon URL 653 // Test that sending an icon URL update identical to the previous icon URL
984 // update is a no-op. 654 // update is a no-op.
985 TEST_F(FaviconHandlerTest, UpdateSameIconURLs) { 655 TEST_F(FaviconHandlerTest, UpdateSameIconURLs) {
pkotwicz 2017/03/13 05:16:32 You should split this up into two tests: - One tes
mastiz 2017/03/13 12:46:04 Isn't the first precisely covered by UpdateSameIco
pkotwicz 2017/03/17 05:43:10 My bad. Yes the first one is the same.
986 const GURL page_url("http://www.google.com"); 656 const GURL kSlowLoadingIconURL("http://www.google.com/slow_favicon");
987 const GURL icon_url1("http://www.google.com/favicon1"); 657
988 const GURL icon_url2("http://www.google.com/favicon2"); 658 const std::vector<FaviconURL> favicon_urls = {
989 std::vector<FaviconURL> favicon_urls; 659 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes),
990 favicon_urls.push_back(FaviconURL(GURL("http://www.google.com/favicon1"), 660 FaviconURL(kSlowLoadingIconURL, FAVICON, kEmptySizes),
991 favicon_base::FAVICON, 661 };
992 std::vector<gfx::Size>())); 662
993 favicon_urls.push_back(FaviconURL(GURL("http://www.google.com/favicon2"), 663 // Defer the download completion such that RunUntilIdle() doesn't complete
994 favicon_base::FAVICON, 664 // the download.
995 std::vector<gfx::Size>())); 665 delegate_.fake_downloader().SetRunCallbackManuallyForUrl(kSlowLoadingIconURL);
996 666 delegate_.fake_downloader().Add(kSlowLoadingIconURL, IntVector{16});
997 TestDelegate delegate; 667
998 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); 668 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates(
999 669 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls);
1000 // Initiate a request for favicon data for |page_url|. History does not know 670
pkotwicz 2017/03/13 05:16:33 Probably also worth checking: - that FaviconServic
mastiz 2017/03/13 12:46:05 These are all verified later below. Are you sugges
pkotwicz 2017/03/17 05:43:11 I was mostly thinking of testing that FaviconHandl
1001 // about the page URL or the icon URLs. 671 ASSERT_THAT(favicon_service_.fake().db_requests(),
1002 helper.FetchFavicon(page_url); 672 ElementsAre(kPageURL, kIconURL64x64, kSlowLoadingIconURL));
1003 helper.history_handler()->InvokeCallback(); 673 ASSERT_TRUE(VerifyAndClearExpectations());
1004 helper.set_history_handler(nullptr); 674 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback());
1005 675
pkotwicz 2017/03/13 05:16:33 We should verify that nothing did in fact happen.
mastiz 2017/03/13 12:46:04 One was missing in UpdateSameIconURLsAfterFinished
pkotwicz 2017/03/17 05:43:11 This test has several parts: Part 1 - Setup std:
1006 // Got icon URLs. 676 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect,
1007 helper.OnUpdateFaviconURL(page_url, favicon_urls); 677 // despite the ongoing download.
1008 678 handler->OnUpdateFaviconURL(kPageURL, favicon_urls);
1009 // There should be an ongoing history request for |icon_url1|. 679 base::RunLoop().RunUntilIdle();
1010 ASSERT_EQ(2u, helper.image_urls().size()); 680
pkotwicz 2017/03/13 05:16:31 We should VerifyAndClearExpectations() now. (Other
mastiz 2017/03/13 12:46:05 678 is precisely the code under test and should fo
1011 ASSERT_EQ(0u, helper.current_candidate_index()); 681 // Complete the download.
1012 HistoryRequestHandler* history_handler = helper.history_handler(); 682 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _));
1013 ASSERT_TRUE(history_handler); 683 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _));
1014 684 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually());
1015 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect. 685 base::RunLoop().RunUntilIdle();
1016 helper.OnUpdateFaviconURL(page_url, favicon_urls); 686 EXPECT_THAT(delegate_.downloads(), IsEmpty());
1017 EXPECT_EQ(history_handler, helper.history_handler()); 687 }
1018 688
1019 // Complete history request for |icon_url1| and do download. 689 // Test that calling OnUpdateFaviconUrl() with the same icon URLs as before is a
1020 helper.history_handler()->InvokeCallback(); 690 // no-op. This is important because OnUpdateFaviconUrl() is called when the page
1021 helper.set_history_handler(nullptr); 691 // finishes loading. This can occur several for pages with iframes.
pkotwicz 2017/03/13 05:16:32 "several for pages with iframes." -> "several time
mastiz 2017/03/13 12:46:05 Done.
1022 delegate.download_handler()->SetImageSizes(std::vector<int>(1u, 10)); 692 TEST_F(FaviconHandlerTest, UpdateSameIconURLsAfterFinished) {
1023 delegate.download_handler()->InvokeCallback(); 693 const std::vector<FaviconURL> favicon_urls = {
1024 delegate.download_handler()->Reset(); 694 FaviconURL(kIconURL10x10, FAVICON, kEmptySizes),
1025 695 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes),
1026 // There should now be an ongoing history request for |icon_url2|. 696 };
1027 ASSERT_EQ(1u, helper.current_candidate_index()); 697
1028 history_handler = helper.history_handler(); 698 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates(
1029 ASSERT_TRUE(history_handler); 699 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls);
pkotwicz 2017/03/13 05:16:31 Nit: You can use RunHandlerWithSimpleFaviconCandid
mastiz 2017/03/13 12:46:05 Done.
1030 700
1031 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect. 701 ASSERT_TRUE(VerifyAndClearExpectations());
1032 helper.OnUpdateFaviconURL(page_url, favicon_urls); 702
1033 EXPECT_EQ(history_handler, helper.history_handler()); 703 // Calling OnUpdateFaviconURL() with identical data should be a no-op.
704 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0);
705 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0);
706
707 handler->OnUpdateFaviconURL(kPageURL, favicon_urls);
708 base::RunLoop().RunUntilIdle();
pkotwicz 2017/03/13 05:16:32 Might as well check that there haven't been any da
mastiz 2017/03/13 12:46:03 Done.
709 EXPECT_THAT(delegate_.downloads(), IsEmpty());
1034 } 710 }
1035 711
1036 // Fixes crbug.com/544560 712 // Fixes crbug.com/544560
713 // Tests that Delegate::OnFaviconUpdated() is called if:
714 // - The best icon on the initial page is not the last icon.
715 // - All of the initial page's icons are downloaded.
716 // AND
717 // - JavaScript modifies the page's <link rel="icon"> tags to contain only the
718 // last icon.
1037 TEST_F(FaviconHandlerTest, 719 TEST_F(FaviconHandlerTest,
1038 OnFaviconAvailableNotificationSentAfterIconURLChange) { 720 OnFaviconAvailableNotificationSentAfterIconURLChange) {
1039 const GURL kPageURL("http://www.page_which_animates_favicon.com"); 721 const GURL kIconURL1(
1040 const GURL kIconURL1("http://wwww.page_which_animates_favicon.com/frame1.png") ; 722 "http://wwww.page_which_animates_favicon.com/frame1.png");
1041 const GURL kIconURL2("http://wwww.page_which_animates_favicon.com/frame2.png") ; 723 const GURL kIconURL2(
1042 724 "http://wwww.page_which_animates_favicon.com/frame2.png");
1043 TestDelegate delegate;
1044 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP);
1045 725
1046 // Initial state: 726 // Initial state:
1047 // - The database does not know about |kPageURL|. 727 // - The database does not know about |kPageURL|.
1048 // - The page uses |kIconURL1| and |kIconURL2|. 728 // - The page uses |kIconURL1| and |kIconURL2|.
1049 // - The database knows about both |kIconURL1| and |kIconURl2|. Both icons 729 // - The database doesn't know about |kIconURL1| or |kIconURl2|.
pkotwicz 2017/03/13 05:16:32 Might as well delete this comment. Hopefully the s
mastiz 2017/03/13 12:46:03 Done.
1050 // are expired in the database. 730
1051 helper.FetchFavicon(kPageURL); 731 // FaviconHandler should download |kIconURL1| and |kIconURL2|. |kIconURL1| is
1052 ASSERT_TRUE(helper.history_handler()); 732 // the better match.
1053 helper.history_handler()->InvokeCallback(); 733 delegate_.fake_downloader().Add(kIconURL1, IntVector{15});
1054 { 734 delegate_.fake_downloader().Add(kIconURL2, IntVector{10});
1055 std::vector<FaviconURL> icon_urls; 735
1056 icon_urls.push_back( 736 // Two FaviconDriver::OnFaviconUpdated() notifications should be sent for
1057 FaviconURL(kIconURL1, favicon_base::FAVICON, std::vector<gfx::Size>())); 737 // |kIconURL1|, one before and one after the download.
pkotwicz 2017/03/13 05:16:32 This comment seems to be out of date. How about:
mastiz 2017/03/13 12:46:04 Done.
1058 icon_urls.push_back( 738 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL1, _, _));
1059 FaviconURL(kIconURL2, favicon_base::FAVICON, std::vector<gfx::Size>())); 739 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL1, _, _));
pkotwicz 2017/03/13 05:16:33 Calls to FaviconService::SetFavicons() are unimpor
mastiz 2017/03/13 12:46:05 Done.
1060 helper.OnUpdateFaviconURL(kPageURL, icon_urls); 740
1061 } 741 std::unique_ptr<FaviconHandler> handler =
1062 742 RunHandlerWithSimpleFaviconCandidates({kIconURL1, kIconURL2});
1063 // FaviconHandler should request from history and download |kIconURL1| and 743
1064 // |kIconURL2|. |kIconURL1| is the better match. A 744 // For the setup of this test it is critical that the download of |kIconURL1|
1065 // FaviconDriver::OnFaviconUpdated() notification should be sent for 745 // occurs before the download of |kIconURL2|.
pkotwicz 2017/03/13 05:16:32 How about: "Both |kIconURL1| and |kIconURL2| shoul
mastiz 2017/03/13 12:46:04 Done.
1066 // |kIconURL1|. 746 ASSERT_THAT(delegate_.downloads(), ElementsAre(kIconURL1, kIconURL2));
1067 ASSERT_EQ(0u, delegate.num_notifications()); 747 // FaviconHandler should have asked for the mappings.
pkotwicz 2017/03/13 05:16:33 I think that you can remove this comment in favor
mastiz 2017/03/13 12:46:04 Done.
1068 ASSERT_TRUE(helper.history_handler()); 748 ASSERT_THAT(favicon_service_.fake().db_requests(),
1069 SetFaviconRawBitmapResult(kIconURL1, 749 ElementsAre(kPageURL, kIconURL1, kIconURL2));
1070 favicon_base::FAVICON, 750 ASSERT_TRUE(VerifyAndClearExpectations());
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 751
1099 // Simulate the page changing it's icon URL to just |kIconURL2| via 752 // Simulate the page changing it's icon URL to just |kIconURL2| via
1100 // Javascript. 753 // Javascript.
1101 helper.OnUpdateFaviconURL( 754 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2,
1102 kPageURL, 755 /*icon_url_changed=*/true, _));
pkotwicz 2017/03/13 05:16:31 Nit: We only care about the icon URL parameter to
mastiz 2017/03/13 12:46:03 Done.
1103 std::vector<FaviconURL>(1u, FaviconURL(kIconURL2, favicon_base::FAVICON, 756 handler->OnUpdateFaviconURL(kPageURL,
1104 std::vector<gfx::Size>()))); 757 {FaviconURL(kIconURL2, FAVICON, kEmptySizes)});
1105 758 base::RunLoop().RunUntilIdle();
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 } 759 }
1123 760
1124 // Test the favicon which is selected when the web page provides several 761 // Test the favicon which is selected when the web page provides several
1125 // favicons and none of the favicons are cached in history. 762 // 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 763 // The goal of this test is to be more of an integration test than
1127 // SelectFaviconFramesTest.*. 764 // SelectFaviconFramesTest.*.
1128 TEST_F(FaviconHandlerTest, MultipleFavicons) { 765 class FaviconHandlerMultipleFaviconsTest : public FaviconHandlerTest {
1129 const GURL kPageURL("http://www.google.com"); 766 protected:
1130 const FaviconURL kSourceIconURLs[] = { 767 const std::vector<FaviconURL> kSourceIconURLs = {
1131 FaviconURL(GURL("http://www.google.com/a"), 768 FaviconURL(GURL("http://www.google.com/a"), FAVICON, kEmptySizes),
1132 favicon_base::FAVICON, 769 FaviconURL(GURL("http://www.google.com/b"), FAVICON, kEmptySizes),
1133 std::vector<gfx::Size>()), 770 FaviconURL(GURL("http://www.google.com/c"), FAVICON, kEmptySizes),
1134 FaviconURL(GURL("http://www.google.com/b"), 771 FaviconURL(GURL("http://www.google.com/d"), FAVICON, kEmptySizes),
1135 favicon_base::FAVICON, 772 FaviconURL(GURL("http://www.google.com/e"), FAVICON, kEmptySizes)};
1136 std::vector<gfx::Size>()), 773
1137 FaviconURL(GURL("http://www.google.com/c"), 774 FaviconHandlerMultipleFaviconsTest() {
1138 favicon_base::FAVICON, 775 // Set the supported scale factors to 1x and 2x. This affects the behavior
1139 std::vector<gfx::Size>()), 776 // of SelectFaviconFrames().
1140 FaviconURL(GURL("http://www.google.com/d"), 777 scoped_set_supported_scale_factors_.reset(
1141 favicon_base::FAVICON, 778 new ui::test::ScopedSetSupportedScaleFactors(
1142 std::vector<gfx::Size>()), 779 {ui::SCALE_FACTOR_100P, ui::SCALE_FACTOR_200P}));
1143 FaviconURL(GURL("http://www.google.com/e"), 780 }
1144 favicon_base::FAVICON, 781
1145 std::vector<gfx::Size>())}; 782 // Same as from the base class but with fixed FaviconURLs.
1146 783 int DownloadTillDoneIgnoringHistory(const IntVector& candidate_icon_sizes) {
1147 // Set the supported scale factors to 1x and 2x. This affects the behavior of 784 return FaviconHandlerTest::DownloadTillDoneIgnoringHistory(
1148 // SelectFaviconFrames(). 785 std::vector<FaviconURL>(
1149 std::vector<ui::ScaleFactor> scale_factors; 786 kSourceIconURLs.begin(),
1150 scale_factors.push_back(ui::SCALE_FACTOR_100P); 787 kSourceIconURLs.begin() + candidate_icon_sizes.size()),
1151 scale_factors.push_back(ui::SCALE_FACTOR_200P); 788 candidate_icon_sizes);
1152 ui::test::ScopedSetSupportedScaleFactors scoped_supported(scale_factors); 789 }
1153 790 };
1154 // 1) Test that if there are several single resolution favicons to choose from 791
1155 // that the largest exact match is chosen. 792 // Test that if there are several single resolution favicons to choose from
1156 TestDelegate delegate1; 793 // that the largest exact match is chosen.
pkotwicz 2017/03/13 05:16:33 For the comment, how about: Tests that running Fa
mastiz 2017/03/13 12:46:05 Done.
1157 TestFaviconHandler handler1(&delegate1, 794 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseLargestExactMatch) {
1158 FaviconDriverObserver::NON_TOUCH_16_DIP); 795 EXPECT_EQ(32,
1159 796 DownloadTillDoneIgnoringHistory(IntVector{16, 24, 32, 48, 256}));
1160 const int kSizes1[] = { 16, 24, 32, 48, 256 }; 797 }
1161 std::vector<FaviconURL> urls1(kSourceIconURLs, 798
1162 kSourceIconURLs + arraysize(kSizes1)); 799 // Test that if there are several single resolution favicons to choose
1163 DownloadTillDoneIgnoringHistory(&delegate1, &handler1, kPageURL, urls1, 800 // from, the exact match is preferred even if it results in upsampling.
1164 kSizes1); 801 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseExactMatchDespiteUpsampling) {
1165 802 EXPECT_EQ(16, DownloadTillDoneIgnoringHistory(IntVector{16, 24, 48, 256}));
1166 EXPECT_EQ(nullptr, handler1.current_candidate()); 803 }
1167 EXPECT_EQ(1u, delegate1.num_notifications()); 804
1168 EXPECT_FALSE(delegate1.image().IsEmpty()); 805 // Test that favicons which need to be upsampled a little or downsampled
1169 EXPECT_EQ(gfx::kFaviconSize, delegate1.image().Width()); 806 // a little are preferred over huge favicons.
1170 807 TEST_F(FaviconHandlerMultipleFaviconsTest,
1171 size_t expected_index = 2u; 808 ChooseMinorResamplingOverHugeFavicons) {
pkotwicz 2017/03/13 05:16:32 Rename this test to ChooseMinorDownsamplingOverHug
mastiz 2017/03/13 12:46:04 Done.
1172 EXPECT_EQ(32, kSizes1[expected_index]); 809 EXPECT_EQ(48, DownloadTillDoneIgnoringHistory(IntVector{256, 48}));
1173 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate1.icon_url()); 810 }
1174 811
1175 // 2) Test that if there are several single resolution favicons to choose 812 TEST_F(FaviconHandlerMultipleFaviconsTest,
1176 // from, the exact match is preferred even if it results in upsampling. 813 ChooseMinorResamplingOverHugeFavicons2) {
pkotwicz 2017/03/13 05:16:33 Rename this test to ChooseMinorUpsamplingOverHugeI
mastiz 2017/03/13 12:46:05 Done.
1177 TestDelegate delegate2; 814 EXPECT_EQ(17, DownloadTillDoneIgnoringHistory(IntVector{17, 256}));
1178 TestFaviconHandler handler2(&delegate2,
1179 FaviconDriverObserver::NON_TOUCH_16_DIP);
1180
1181 const int kSizes2[] = { 16, 24, 48, 256 };
1182 std::vector<FaviconURL> urls2(kSourceIconURLs,
1183 kSourceIconURLs + arraysize(kSizes2));
1184 DownloadTillDoneIgnoringHistory(&delegate2, &handler2, kPageURL, urls2,
1185 kSizes2);
1186 EXPECT_EQ(1u, delegate2.num_notifications());
1187 expected_index = 0u;
1188 EXPECT_EQ(16, kSizes2[expected_index]);
1189 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate2.icon_url());
1190
1191 // 3) Test that favicons which need to be upsampled a little or downsampled
1192 // a little are preferred over huge favicons.
1193 TestDelegate delegate3;
1194 TestFaviconHandler handler3(&delegate3,
1195 FaviconDriverObserver::NON_TOUCH_16_DIP);
1196
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 } 815 }
1221 816
1222 // Test that the best favicon is selected when: 817 // Test that the best favicon is selected when:
1223 // - The page provides several favicons. 818 // - The page provides several favicons.
1224 // - Downloading one of the page's icon URLs previously returned a 404. 819 // - Downloading one of the page's icon URLs previously returned a 404.
1225 // - None of the favicons are cached in the Favicons database. 820 // - None of the favicons are cached in the Favicons database.
1226 TEST_F(FaviconHandlerTest, MultipleFavicons404) { 821 TEST_F(FaviconHandlerTest, MultipleFavicons404) {
1227 const GURL kPageURL("http://www.google.com"); 822 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL64x64))
1228 const GURL k404IconURL("http://www.google.com/404.png"); 823 .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 824
1241 TestDelegate delegate; 825 EXPECT_EQ(16, DownloadTillDoneIgnoringHistory(
1242 TestFaviconHandler handler(&delegate, 826 {
1243 FaviconDriverObserver::NON_TOUCH_16_DIP); 827 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes),
1244 DownloadHandler* download_handler = delegate.download_handler(); 828 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes),
829 },
830 IntVector{16, 64}));
pkotwicz 2017/03/13 05:16:31 This test does not test the same functionality as
mastiz 2017/03/13 12:46:05 Done.
831 }
1245 832
1246 std::set<GURL> k404URLs; 833 // Test that the best favicon is selected when:
1247 k404URLs.insert(k404IconURL); 834 // - The page provides several favicons.
1248 download_handler->FailDownloadForIconURLs(k404URLs); 835 // - Downloading the last page icon URL previously returned a 404.
836 // - None of the favicons are cached in the Favicons database.
837 // - Size hints are misleading and cause the 404 icon to be processed last.
838 TEST_F(FaviconHandlerTest, MultipleFaviconsLast404) {
pkotwicz 2017/03/13 05:16:31 - You can simplify this test case by providing no
mastiz 2017/03/13 12:46:04 Done.
839 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL64x64))
840 .WillByDefault(Return(true));
1249 841
1250 // Make the initial download for |k404IconURL| fail. 842 EXPECT_EQ(10, DownloadTillDoneIgnoringHistory(
1251 const int kSizes1[] = { 0 }; 843 {
1252 std::vector<FaviconURL> urls1(1u, k404FaviconURL); 844 // Sizes intentionally wrong to reproduce a corner case.
1253 DownloadTillDoneIgnoringHistory(&delegate, &handler, kPageURL, urls1, 845 FaviconURL(kIconURL10x10, FAVICON, {gfx::Size(16, 16)}),
1254 kSizes1); 846 FaviconURL(kIconURL64x64, FAVICON, {gfx::Size(15, 15)}),
1255 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL)); 847 },
1256 848 IntVector{10, 64}));
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 } 849 }
1273 850
1274 // Test that no favicon is selected when: 851 // Test that no favicon is selected when:
1275 // - The page provides several favicons. 852 // - The page provides several favicons.
1276 // - Downloading the page's icons has previously returned a 404. 853 // - Downloading the page's icons has previously returned a 404.
1277 // - None of the favicons are cached in the Favicons database. 854 // - None of the favicons are cached in the Favicons database.
1278 TEST_F(FaviconHandlerTest, MultipleFaviconsAll404) { 855 TEST_F(FaviconHandlerTest, MultipleFaviconsAll404) {
1279 const GURL kPageURL("http://www.google.com"); 856 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL16x16))
1280 const GURL k404IconURL1("http://www.google.com/4041.png"); 857 .WillByDefault(Return(true));
1281 const GURL k404IconURL2("http://www.google.com/4042.png"); 858 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL64x64))
1282 const FaviconURL kFaviconURLs[] = { 859 .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 860
1291 TestDelegate delegate; 861 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0);
1292 TestFaviconHandler handler(&delegate,
1293 FaviconDriverObserver::NON_TOUCH_16_DIP);
1294 DownloadHandler* download_handler = delegate.download_handler();
1295 862
1296 std::set<GURL> k404URLs; 863 EXPECT_EQ(-1, DownloadTillDoneIgnoringHistory(
1297 k404URLs.insert(k404IconURL1); 864 {
1298 k404URLs.insert(k404IconURL2); 865 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes),
1299 download_handler->FailDownloadForIconURLs(k404URLs); 866 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes),
1300 867 },
1301 // Make the initial downloads for |kFaviconURLs| fail. 868 IntVector{16, 64}));
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 } 869 }
1323 870
1324 // Test that no favicon is selected when the page's only icon uses an invalid 871 // Test that no favicon is selected when the page's only icon uses an invalid
1325 // URL syntax. 872 // URL syntax.
1326 TEST_F(FaviconHandlerTest, FaviconInvalidURL) { 873 TEST_F(FaviconHandlerTest, FaviconInvalidURL) {
1327 const GURL kPageURL("http://www.google.com");
1328 const GURL kInvalidFormatURL("invalid"); 874 const GURL kInvalidFormatURL("invalid");
1329 ASSERT_TRUE(kInvalidFormatURL.is_empty()); 875 ASSERT_TRUE(kInvalidFormatURL.is_empty());
1330 876
1331 FaviconURL favicon_url(kInvalidFormatURL, favicon_base::FAVICON, 877 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0);
1332 std::vector<gfx::Size>());
1333 878
1334 TestDelegate delegate; 879 RunHandlerWithSimpleFaviconCandidates({kInvalidFormatURL});
1335 TestFaviconHandler handler(&delegate, 880 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 } 881 }
1341 882
1342 TEST_F(FaviconHandlerTest, TestSortFavicon) { 883 TEST_F(FaviconHandlerTest, TestSortFavicon) {
1343 const GURL kPageURL("http://www.google.com"); 884 const std::vector<favicon::FaviconURL> kSourceIconURLs{
1344 std::vector<gfx::Size> icon1; 885 FaviconURL(GURL("http://www.google.com/b"), FAVICON,
pkotwicz 2017/03/13 05:16:32 Might as well alphabetize the URLs with 'a' coming
mastiz 2017/03/13 12:46:03 Done.
1345 icon1.push_back(gfx::Size(1024, 1024)); 886 {gfx::Size(15, 15), gfx::Size(16, 16)}),
pkotwicz 2017/03/13 05:16:33 I think that it would be more interesting if this
mastiz 2017/03/13 12:46:05 Done. The original goal was to stick to previous t
1346 icon1.push_back(gfx::Size(512, 512)); 887 FaviconURL(GURL("http://www.google.com/a"), FAVICON,
888 {gfx::Size(1024, 1024), gfx::Size(512, 512)}),
889 FaviconURL(GURL("http://www.google.com/c"), FAVICON,
890 {gfx::Size(16, 16), gfx::Size(14, 14)}),
891 FaviconURL(GURL("http://www.google.com/d"), FAVICON, kEmptySizes),
892 FaviconURL(GURL("http://www.google.com/e"), FAVICON, kEmptySizes)};
1347 893
1348 std::vector<gfx::Size> icon2; 894 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates(
1349 icon2.push_back(gfx::Size(15, 15)); 895 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 896
1374 struct ExpectedResult { 897 struct ExpectedResult {
1375 // The favicon's index in kSourceIconURLs. 898 // The favicon's index in kSourceIconURLs.
1376 size_t favicon_index; 899 size_t favicon_index;
1377 // Width of largest bitmap. 900 // Width of largest bitmap.
1378 int width; 901 int width;
1379 } results[] = { 902 } results[] = {
1380 // First is icon1, though its size larger than maximal. 903 // First is icon2, though its size larger than maximal.
1381 {0, 1024}, 904 {1, 1024},
1382 // Second is icon2 905 // Second is icon1
1383 // The 16x16 is largest. 906 // The 16x16 is largest.
1384 {1, 16}, 907 {0, 16},
1385 // Third is icon3 though it has same size as icon2. 908 // Third is icon3 though it has same size as icon1.
1386 // The 16x16 is largest. 909 // The 16x16 is largest.
1387 {2, 16}, 910 {2, 16},
1388 // The rest of bitmaps come in order, there is no sizes attribute. 911 // The rest of bitmaps come in order, there is no sizes attribute.
1389 {3, -1}, 912 {3, -1},
1390 {4, -1}, 913 {4, -1},
1391 }; 914 };
1392 const std::vector<FaviconURL>& icons = handler1.image_urls(); 915 const std::vector<FaviconURL>& icons = handler->image_urls();
1393 ASSERT_EQ(5u, icons.size()); 916 ASSERT_EQ(5u, icons.size());
1394 for (size_t i = 0; i < icons.size(); ++i) { 917 for (size_t i = 0; i < icons.size(); ++i) {
1395 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url, 918 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
1396 icons[i].icon_url); 919 icons[i].icon_url);
1397 if (results[i].width != -1) 920 if (results[i].width != -1)
1398 EXPECT_EQ(results[i].width, icons[i].icon_sizes[0].width()); 921 EXPECT_EQ(results[i].width, icons[i].icon_sizes[0].width());
1399 } 922 }
1400 } 923 }
1401 924
1402 TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) { 925 TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) {
1403 const GURL kPageURL("http://www.google.com"); 926 // Names represent the bitmap sizes per icon.
1404 std::vector<gfx::Size> icon1; 927 const GURL kIconURL1024_512("http://www.google.com/a");
1405 icon1.push_back(gfx::Size(1024, 1024)); 928 const GURL kIconURL15_14("http://www.google.com/b");
1406 icon1.push_back(gfx::Size(512, 512)); 929 const GURL kIconURL16_512("http://www.google.com/c");
930 const GURL kIconURLWithoutSize1("http://www.google.com/d");
931 const GURL kIconURLWithoutSize2("http://www.google.com/e");
1407 932
1408 std::vector<gfx::Size> icon2; 933 RunHandlerWithCandidates(
1409 icon2.push_back(gfx::Size(15, 15)); 934 FaviconDriverObserver::NON_TOUCH_LARGEST,
1410 icon2.push_back(gfx::Size(14, 14)); 935 {FaviconURL(kIconURL1024_512, FAVICON,
936 {gfx::Size(1024, 1024), gfx::Size(512, 512)}),
937 FaviconURL(kIconURL15_14, FAVICON,
938 {gfx::Size(15, 15), gfx::Size(14, 14)}),
939 FaviconURL(kIconURL16_512, FAVICON,
940 {gfx::Size(16, 16), gfx::Size(512, 512)}),
941 FaviconURL(kIconURLWithoutSize1, FAVICON, kEmptySizes),
942 FaviconURL(kIconURLWithoutSize2, FAVICON, kEmptySizes)});
1411 943
1412 std::vector<gfx::Size> icon3; 944 // Icon URLs are not registered and hence 404s will be produced, which
1413 icon3.push_back(gfx::Size(16, 16)); 945 // allows checking whether the icons were requested according to their
1414 icon3.push_back(gfx::Size(512, 512)); 946 // size.
pkotwicz 2017/03/13 05:16:33 Please add to the comment: "... which allows check
mastiz 2017/03/13 12:46:05 Done.
1415 947 EXPECT_THAT(delegate_.downloads(),
1416 const FaviconURL kSourceIconURLs[] = { 948 ElementsAre(kIconURL1024_512, kIconURL16_512, kIconURL15_14,
1417 FaviconURL( 949 kIconURLWithoutSize1, kIconURLWithoutSize2));
1418 GURL("http://www.google.com/a"), favicon_base::FAVICON, icon1),
1419 FaviconURL(
1420 GURL("http://www.google.com/b"), favicon_base::FAVICON, icon2),
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 } 950 }
1481 951
1482 TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) { 952 TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) {
1483 const GURL kPageURL("http://www.google.com"); 953 const GURL kIconURL1("http://www.google.com/b");
954 const GURL kIconURL2("http://www.google.com/c");
1484 955
1485 std::vector<gfx::Size> one_icon; 956 delegate_.fake_downloader().Add(kIconURL2, IntVector{14, 16});
1486 one_icon.push_back(gfx::Size(15, 15));
1487
1488 std::vector<gfx::Size> two_icons;
1489 two_icons.push_back(gfx::Size(14, 14));
1490 two_icons.push_back(gfx::Size(16, 16));
1491
1492 const FaviconURL kSourceIconURLs[] = {
1493 FaviconURL(
1494 GURL("http://www.google.com/b"), favicon_base::FAVICON, one_icon),
1495 FaviconURL(
1496 GURL("http://www.google.com/c"), favicon_base::FAVICON, two_icons)};
1497
1498 TestDelegate delegate1;
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 957
1537 // Verify the largest bitmap has been saved into history. 958 // Verify the largest bitmap has been saved into history.
1538 EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_); 959 EXPECT_CALL(favicon_service_,
1539 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b], 960 SetFavicons(kPageURL, kIconURL2, FAVICON, ImageSizeIs(16, 16)));
pkotwicz 2017/03/13 05:16:31 Nit: Remove the check for SetFavicons(). I know th
mastiz 2017/03/13 12:46:03 Done.
1540 handler1.history_handler()->size_);
1541 // Verify NotifyFaviconAvailable(). 961 // Verify NotifyFaviconAvailable().
1542 EXPECT_EQ(1u, delegate1.num_notifications()); 962 EXPECT_CALL(delegate_, OnFaviconUpdated(
1543 EXPECT_EQ(kSourceIconURLs[i].icon_url, delegate1.icon_url()); 963 kPageURL, FaviconDriverObserver::NON_TOUCH_LARGEST,
1544 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b], delegate1.image().Size()); 964 kIconURL2, /*icon_url_changed=*/true, _));
pkotwicz 2017/03/13 05:16:34 Nit: We don't care about any of the parameters exc
mastiz 2017/03/13 12:46:05 Done, but leaving NON_TOUCH_LARGEST because I beli
965
966 RunHandlerWithCandidates(
967 FaviconDriverObserver::NON_TOUCH_LARGEST,
968 {FaviconURL(kIconURL1, FAVICON, {gfx::Size(15, 15)}),
969 FaviconURL(kIconURL2, FAVICON, {gfx::Size(14, 14), gfx::Size(16, 16)})});
pkotwicz 2017/03/13 05:16:32 Can you test which images got downloaded. Otherwis
mastiz 2017/03/13 12:46:05 Done, I registered kIconURL1 as available download
1545 } 970 }
1546 971
1547 TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) { 972 TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) {
1548 const GURL kPageURL("http://www.google.com"); 973 const int kMaximalSize = FaviconHandler::GetMaximalIconSize(FAVICON);
1549 const int kMaximalSize =
1550 TestFaviconHandler::GetMaximalIconSize(favicon_base::FAVICON);
1551 974
1552 std::vector<gfx::Size> icon1; 975 const GURL kIconURL1("http://www.google.com/b");
1553 icon1.push_back(gfx::Size(kMaximalSize + 1, kMaximalSize + 1)); 976 const GURL kIconURL2("http://www.google.com/c");
1554 977
1555 std::vector<gfx::Size> icon2; 978 const int kOriginalSize1 = kMaximalSize + 1;
1556 icon2.push_back(gfx::Size(kMaximalSize + 2, kMaximalSize + 2)); 979 const int kOriginalSize2 = kMaximalSize + 2;
1557 980
1558 const FaviconURL kSourceIconURLs[] = { 981 delegate_.fake_downloader().AddWithOriginalSizes(
1559 FaviconURL( 982 kIconURL2, IntVector{kMaximalSize}, IntVector{kOriginalSize2});
1560 GURL("http://www.google.com/b"), favicon_base::FAVICON, icon1),
1561 FaviconURL(
1562 GURL("http://www.google.com/c"), favicon_base::FAVICON, icon2)};
1563
1564 TestDelegate delegate1;
1565 TestFaviconHandler handler1(&delegate1,
1566 FaviconDriverObserver::NON_TOUCH_LARGEST);
1567 std::vector<FaviconURL> urls1(kSourceIconURLs,
1568 kSourceIconURLs + arraysize(kSourceIconURLs));
1569 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1);
1570
1571 ASSERT_EQ(2u, handler1.image_urls().size());
1572
1573 // Index of largest favicon in kSourceIconURLs.
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 983
1600 // Verify the largest bitmap has been saved into history though it was 984 // Verify the largest bitmap has been saved into history though it was
1601 // scaled down to maximal size and smaller than icon1 now. 985 // scaled down to maximal size and smaller than |kIconURL1| now.
1602 EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_); 986 EXPECT_CALL(favicon_service_,
1603 EXPECT_EQ(gfx::Size(kMaximalSize, kMaximalSize), 987 SetFavicons(kPageURL, kIconURL2, FAVICON,
1604 handler1.history_handler()->size_); 988 ImageSizeIs(kMaximalSize, kMaximalSize)));
pkotwicz 2017/03/13 05:16:33 Nit: Make this test check Delegate::OnFaviconUpdat
mastiz 2017/03/13 12:46:03 Done.
989
990 RunHandlerWithCandidates(
991 FaviconDriverObserver::NON_TOUCH_LARGEST,
992 {FaviconURL(kIconURL1, FAVICON,
993 SizeVector{gfx::Size(kOriginalSize1, kOriginalSize1)}),
994 FaviconURL(kIconURL2, FAVICON,
995 SizeVector{gfx::Size(kOriginalSize2, kOriginalSize2)})});
pkotwicz 2017/03/13 05:16:33 Nit: Can you also test what images were downloaded
mastiz 2017/03/13 12:46:03 Done. This however seems wrong because it's trying
pkotwicz 2017/03/17 05:43:10 Thank you for making the change.
1605 } 996 }
1606 997
pkotwicz 2017/03/13 05:16:31 For the comment, how about: "Test that if several
mastiz 2017/03/13 12:46:04 Done.
1607 TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) { 998 TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) {
1608 const GURL kPageURL("http://www.google.com"); 999 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL12x12, FAVICON,
1000 ImageSizeIs(12, 12)));
pkotwicz 2017/03/13 05:16:34 Now that FaviconService no longer users StrictMock
mastiz 2017/03/13 12:46:03 Done.
1609 1001
1610 std::vector<gfx::Size> icon1; 1002 RunHandlerWithCandidates(
1611 icon1.push_back(gfx::Size(16, 16)); 1003 FaviconDriverObserver::NON_TOUCH_LARGEST,
1612 const int actual_size1 = 10; 1004 {FaviconURL(kIconURL10x10, FAVICON, SizeVector{gfx::Size(16, 16)}),
1613 1005 FaviconURL(kIconURL12x12, FAVICON, SizeVector{gfx::Size(15, 15)}),
1614 std::vector<gfx::Size> icon2; 1006 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 } 1007 }
1676 1008
1677 } // namespace 1009 } // namespace
1678 } // namespace favicon 1010 } // 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