BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / components / favicon / core / favicon_handler_unittest.cc
blobfb5138552ee5ec5879cd515c8f237b2ef3015397
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
3 // found in the LICENSE file.
5 #include "components/favicon/core/favicon_handler.h"
7 #include<set>
8 #include<vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "components/favicon/core/favicon_driver.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/base/layout.h"
15 #include "ui/gfx/codec/png_codec.h"
16 #include "ui/gfx/favicon_size.h"
17 #include "ui/gfx/image/image.h"
19 namespace favicon {
20 namespace {
22 // Fill the given bmp with valid png data.
23 void FillDataToBitmap(int w, int h, SkBitmap* bmp) {
24 bmp->allocN32Pixels(w, h);
26 unsigned char* src_data =
27 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0));
28 for (int i = 0; i < w * h; i++) {
29 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255);
30 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255);
31 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255);
32 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255);
36 // Fill the given data buffer with valid png data.
37 void FillBitmap(int w, int h, std::vector<unsigned char>* output) {
38 SkBitmap bitmap;
39 FillDataToBitmap(w, h, &bitmap);
40 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output);
43 void SetFaviconRawBitmapResult(
44 const GURL& icon_url,
45 favicon_base::IconType icon_type,
46 bool expired,
47 std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results) {
48 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
49 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data());
50 favicon_base::FaviconRawBitmapResult bitmap_result;
51 bitmap_result.expired = expired;
52 bitmap_result.bitmap_data = data;
53 // Use a pixel size other than (0,0) as (0,0) has a special meaning.
54 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize);
55 bitmap_result.icon_type = icon_type;
56 bitmap_result.icon_url = icon_url;
58 favicon_bitmap_results->push_back(bitmap_result);
61 void SetFaviconRawBitmapResult(
62 const GURL& icon_url,
63 std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results) {
64 SetFaviconRawBitmapResult(icon_url,
65 favicon_base::FAVICON,
66 false /* expired */,
67 favicon_bitmap_results);
70 // This class is used to save the download request for verifying with test case.
71 // It also will be used to invoke the onDidDownload callback.
72 class DownloadHandler {
73 public:
74 explicit DownloadHandler(FaviconHandler* favicon_handler)
75 : favicon_handler_(favicon_handler), callback_invoked_(false) {}
77 ~DownloadHandler() {}
79 void Reset() {
80 download_.reset();
81 callback_invoked_ = false;
82 // Does not affect |should_fail_download_icon_urls_| and
83 // |failed_download_icon_urls_|.
86 // Make downloads for any of |icon_urls| fail.
87 void FailDownloadForIconURLs(const std::set<GURL>& icon_urls) {
88 should_fail_download_icon_urls_ = icon_urls;
91 // Returns whether a download for |icon_url| did fail.
92 bool DidFailDownloadForIconURL(const GURL& icon_url) const {
93 return failed_download_icon_urls_.count(icon_url);
96 void AddDownload(
97 int download_id,
98 const GURL& image_url,
99 const std::vector<int>& image_sizes,
100 int max_image_size) {
101 download_.reset(new Download(
102 download_id, image_url, image_sizes, max_image_size));
105 void InvokeCallback();
107 bool HasDownload() const { return download_.get(); }
108 const GURL& GetImageUrl() const { return download_->image_url; }
109 void SetImageSizes(const std::vector<int>& sizes) {
110 download_->image_sizes = sizes; }
112 private:
113 struct Download {
114 Download(int id,
115 GURL url,
116 const std::vector<int>& sizes,
117 int max_size)
118 : download_id(id),
119 image_url(url),
120 image_sizes(sizes),
121 max_image_size(max_size) {}
122 ~Download() {}
123 int download_id;
124 GURL image_url;
125 std::vector<int> image_sizes;
126 int max_image_size;
129 FaviconHandler* favicon_handler_;
130 scoped_ptr<Download> download_;
131 bool callback_invoked_;
133 // The icon URLs for which the download should fail.
134 std::set<GURL> should_fail_download_icon_urls_;
136 // The icon URLs for which the download did fail. This should be a subset of
137 // |should_fail_download_icon_urls_|.
138 std::set<GURL> failed_download_icon_urls_;
140 DISALLOW_COPY_AND_ASSIGN(DownloadHandler);
143 // This class is used to save the history request for verifying with test case.
144 // It also will be used to simulate the history response.
145 class HistoryRequestHandler {
146 public:
147 HistoryRequestHandler(const GURL& page_url,
148 const GURL& icon_url,
149 int icon_type,
150 const favicon_base::FaviconResultsCallback& callback)
151 : page_url_(page_url),
152 icon_url_(icon_url),
153 icon_type_(icon_type),
154 callback_(callback) {
157 HistoryRequestHandler(const GURL& page_url,
158 const GURL& icon_url,
159 int icon_type,
160 const std::vector<unsigned char>& bitmap_data,
161 const gfx::Size& size)
162 : page_url_(page_url),
163 icon_url_(icon_url),
164 icon_type_(icon_type),
165 bitmap_data_(bitmap_data),
166 size_(size) {
169 ~HistoryRequestHandler() {}
170 void InvokeCallback();
172 const GURL page_url_;
173 const GURL icon_url_;
174 const int icon_type_;
175 const std::vector<unsigned char> bitmap_data_;
176 const gfx::Size size_;
177 std::vector<favicon_base::FaviconRawBitmapResult> history_results_;
178 favicon_base::FaviconResultsCallback callback_;
180 private:
181 DISALLOW_COPY_AND_ASSIGN(HistoryRequestHandler);
184 } // namespace
186 class TestFaviconDriver : public FaviconDriver {
187 public:
188 TestFaviconDriver()
189 : favicon_validity_(false),
190 num_active_favicon_(0),
191 num_favicon_available_(0),
192 update_active_favicon_(false) {}
194 ~TestFaviconDriver() override {}
196 // FaviconDriver implementation.
197 void FetchFavicon(const GURL& url) override {
198 ADD_FAILURE() << "TestFaviconDriver::FetchFavicon() "
199 << "should never be called in tests.";
202 gfx::Image GetFavicon() const override {
203 ADD_FAILURE() << "TestFaviconDriver::GetFavicon() "
204 << "should never be called in tests.";
205 return gfx::Image();
208 bool FaviconIsValid() const override {
209 ADD_FAILURE() << "TestFaviconDriver::FaviconIsValid() "
210 << "should never be called in tests.";
211 return false;
214 bool HasPendingTasksForTest() override {
215 ADD_FAILURE() << "TestFaviconDriver::HasPendingTasksForTest() "
216 << "should never be called in tests.";
217 return false;
220 int StartDownload(const GURL& url, int max_bitmap_size) override {
221 ADD_FAILURE() << "TestFaviconDriver::StartDownload() "
222 << "should never be called in tests.";
223 return -1;
226 bool IsOffTheRecord() override { return false; }
228 bool IsBookmarked(const GURL& url) override { return false; }
230 GURL GetActiveURL() override { return url_; }
232 bool GetActiveFaviconValidity() override { return favicon_validity_; }
234 void SetActiveFaviconValidity(bool favicon_validity) override {
235 favicon_validity_ = favicon_validity;
238 GURL GetActiveFaviconURL() override { return favicon_url_; }
240 void SetActiveFaviconURL(const GURL& favicon_url) override {
241 favicon_url_ = favicon_url;
244 gfx::Image GetActiveFaviconImage() { return image_; }
246 void SetActiveFaviconImage(const gfx::Image& image) override {
247 image_ = image;
250 void OnFaviconAvailable(const gfx::Image& image,
251 const GURL& icon_url,
252 bool update_active_favicon) override {
253 ++num_favicon_available_;
254 available_image_ = image;
255 available_icon_url_ = icon_url;
256 update_active_favicon_ = update_active_favicon;
257 if (!update_active_favicon)
258 return;
260 ++num_active_favicon_;
261 SetActiveFaviconURL(icon_url);
262 SetActiveFaviconValidity(true);
263 SetActiveFaviconImage(image);
266 size_t num_active_favicon() const { return num_active_favicon_; }
267 size_t num_favicon_available() const { return num_favicon_available_; }
268 void ResetNumActiveFavicon() { num_active_favicon_ = 0; }
269 void ResetNumFaviconAvailable() { num_favicon_available_ = 0; }
271 void SetActiveURL(GURL url) { url_ = url; }
273 const gfx::Image& available_favicon() { return available_image_; }
275 const GURL& available_icon_url() { return available_icon_url_; }
277 bool update_active_favicon() { return update_active_favicon_; }
279 private:
280 GURL favicon_url_;
281 GURL url_;
282 gfx::Image image_;
283 bool favicon_validity_;
285 // The number of times that NotifyFaviconAvailable() has been called with
286 // |is_active_favicon| is true.
287 size_t num_active_favicon_;
288 // The number of times that NotifyFaviconAvailable() has been called.
289 size_t num_favicon_available_;
290 gfx::Image available_image_;
291 GURL available_icon_url_;
292 bool update_active_favicon_;
294 DISALLOW_COPY_AND_ASSIGN(TestFaviconDriver);
297 // This class is used to catch the FaviconHandler's download and history
298 // request, and also provide the methods to access the FaviconHandler
299 // internals.
300 class TestFaviconHandler : public FaviconHandler {
301 public:
302 static int GetMaximalIconSize(favicon_base::IconType icon_type) {
303 return FaviconHandler::GetMaximalIconSize(icon_type);
306 TestFaviconHandler(const GURL& page_url,
307 TestFaviconDriver* driver,
308 Type type,
309 bool download_largest_icon)
310 : FaviconHandler(nullptr, driver, type, download_largest_icon),
311 download_id_(0) {
312 driver->SetActiveURL(page_url);
313 download_handler_.reset(new DownloadHandler(this));
316 ~TestFaviconHandler() override {}
318 HistoryRequestHandler* history_handler() {
319 return history_handler_.get();
322 // This method will take the ownership of the given handler.
323 void set_history_handler(HistoryRequestHandler* handler) {
324 history_handler_.reset(handler);
327 DownloadHandler* download_handler() {
328 return download_handler_.get();
331 // Methods to access favicon internals.
332 const std::vector<FaviconURL>& urls() {
333 return image_urls_;
336 FaviconURL* current_candidate() {
337 return FaviconHandler::current_candidate();
340 const FaviconCandidate& best_favicon_candidate() {
341 return best_favicon_candidate_;
344 protected:
345 void UpdateFaviconMappingAndFetch(
346 const GURL& page_url,
347 const GURL& icon_url,
348 favicon_base::IconType icon_type,
349 const favicon_base::FaviconResultsCallback& callback,
350 base::CancelableTaskTracker* tracker) override {
351 history_handler_.reset(new HistoryRequestHandler(page_url, icon_url,
352 icon_type, callback));
355 void GetFaviconFromFaviconService(
356 const GURL& icon_url,
357 favicon_base::IconType icon_type,
358 const favicon_base::FaviconResultsCallback& callback,
359 base::CancelableTaskTracker* tracker) override {
360 history_handler_.reset(new HistoryRequestHandler(GURL(), icon_url,
361 icon_type, callback));
364 void GetFaviconForURLFromFaviconService(
365 const GURL& page_url,
366 int icon_types,
367 const favicon_base::FaviconResultsCallback& callback,
368 base::CancelableTaskTracker* tracker) override {
369 history_handler_.reset(new HistoryRequestHandler(page_url, GURL(),
370 icon_types, callback));
373 int DownloadFavicon(const GURL& image_url, int max_bitmap_size) override {
374 // Do not do a download if downloading |image_url| failed previously. This
375 // emulates the behavior of FaviconDriver::StartDownload()
376 if (download_handler_->DidFailDownloadForIconURL(image_url)) {
377 download_handler_->AddDownload(download_id_, image_url,
378 std::vector<int>(), 0);
379 return 0;
382 download_id_++;
383 std::vector<int> sizes;
384 sizes.push_back(0);
385 download_handler_->AddDownload(
386 download_id_, image_url, sizes, max_bitmap_size);
387 return download_id_;
390 void SetHistoryFavicons(const GURL& page_url,
391 const GURL& icon_url,
392 favicon_base::IconType icon_type,
393 const gfx::Image& image) override {
394 scoped_refptr<base::RefCountedMemory> bytes = image.As1xPNGBytes();
395 std::vector<unsigned char> bitmap_data(bytes->front(),
396 bytes->front() + bytes->size());
397 history_handler_.reset(new HistoryRequestHandler(
398 page_url, icon_url, icon_type, bitmap_data, image.Size()));
401 bool ShouldSaveFavicon(const GURL& url) override { return true; }
403 GURL page_url_;
405 private:
407 // The unique id of a download request. It will be returned to a
408 // FaviconHandler.
409 int download_id_;
411 scoped_ptr<DownloadHandler> download_handler_;
412 scoped_ptr<HistoryRequestHandler> history_handler_;
414 DISALLOW_COPY_AND_ASSIGN(TestFaviconHandler);
417 namespace {
419 void HistoryRequestHandler::InvokeCallback() {
420 if (!callback_.is_null()) {
421 callback_.Run(history_results_);
425 void DownloadHandler::InvokeCallback() {
426 if (callback_invoked_)
427 return;
429 std::vector<gfx::Size> original_bitmap_sizes;
430 std::vector<SkBitmap> bitmaps;
431 if (should_fail_download_icon_urls_.count(download_->image_url)) {
432 failed_download_icon_urls_.insert(download_->image_url);
433 } else {
434 for (std::vector<int>::const_iterator i = download_->image_sizes.begin();
435 i != download_->image_sizes.end(); ++i) {
436 int original_size = (*i > 0) ? *i : gfx::kFaviconSize;
437 int downloaded_size = original_size;
438 if (download_->max_image_size != 0 &&
439 downloaded_size > download_->max_image_size) {
440 downloaded_size = download_->max_image_size;
442 SkBitmap bitmap;
443 FillDataToBitmap(downloaded_size, downloaded_size, &bitmap);
444 bitmaps.push_back(bitmap);
445 original_bitmap_sizes.push_back(gfx::Size(original_size, original_size));
448 favicon_handler_->OnDidDownloadFavicon(download_->download_id,
449 download_->image_url, bitmaps,
450 original_bitmap_sizes);
451 callback_invoked_ = true;
454 class FaviconHandlerTest : public testing::Test {
455 public:
456 FaviconHandlerTest() {
459 ~FaviconHandlerTest() override {}
461 // Simulates requesting a favicon for |page_url| given:
462 // - We have not previously cached anything in history for |page_url| or for
463 // any of |candidates|.
464 // - The page provides favicons at |candidate_icons|.
465 // - The favicons at |candidate_icons| have edge pixel sizes of
466 // |candidate_icon_sizes|.
467 void DownloadTillDoneIgnoringHistory(
468 TestFaviconDriver* favicon_driver,
469 TestFaviconHandler* favicon_handler,
470 const GURL& page_url,
471 const std::vector<FaviconURL>& candidate_icons,
472 const int* candidate_icon_sizes) {
473 UpdateFaviconURL(
474 favicon_driver, favicon_handler, page_url, candidate_icons);
475 EXPECT_EQ(candidate_icons.size(), favicon_handler->image_urls().size());
477 DownloadHandler* download_handler = favicon_handler->download_handler();
478 for (size_t i = 0; i < candidate_icons.size(); ++i) {
479 favicon_handler->history_handler()->history_results_.clear();
480 favicon_handler->history_handler()->InvokeCallback();
481 ASSERT_TRUE(download_handler->HasDownload());
482 EXPECT_EQ(download_handler->GetImageUrl(),
483 candidate_icons[i].icon_url);
484 std::vector<int> sizes;
485 sizes.push_back(candidate_icon_sizes[i]);
486 download_handler->SetImageSizes(sizes);
487 download_handler->InvokeCallback();
489 download_handler->Reset();
491 if (favicon_driver->num_active_favicon())
492 return;
496 void UpdateFaviconURL(TestFaviconDriver* favicon_driver,
497 TestFaviconHandler* favicon_handler,
498 const GURL& page_url,
499 const std::vector<FaviconURL>& candidate_icons) {
500 favicon_driver->ResetNumActiveFavicon();
502 favicon_handler->FetchFavicon(page_url);
503 favicon_handler->history_handler()->InvokeCallback();
505 favicon_handler->OnUpdateFaviconURL(candidate_icons);
508 void SetUp() override {
509 // The score computed by SelectFaviconFrames() is dependent on the supported
510 // scale factors of the platform. It is used for determining the goodness of
511 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon().
512 // Force the values of the scale factors so that the tests produce the same
513 // results on all platforms.
514 std::vector<ui::ScaleFactor> scale_factors;
515 scale_factors.push_back(ui::SCALE_FACTOR_100P);
516 scoped_set_supported_scale_factors_.reset(
517 new ui::test::ScopedSetSupportedScaleFactors(scale_factors));
518 testing::Test::SetUp();
521 private:
522 scoped_ptr<ui::test::ScopedSetSupportedScaleFactors>
523 scoped_set_supported_scale_factors_;
524 DISALLOW_COPY_AND_ASSIGN(FaviconHandlerTest);
527 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
528 const GURL page_url("http://www.google.com");
529 const GURL icon_url("http://www.google.com/favicon");
531 TestFaviconDriver driver;
532 TestFaviconHandler helper(page_url, &driver, FaviconHandler::FAVICON, false);
534 helper.FetchFavicon(page_url);
535 HistoryRequestHandler* history_handler = helper.history_handler();
536 // Ensure the data given to history is correct.
537 ASSERT_TRUE(history_handler);
538 EXPECT_EQ(page_url, history_handler->page_url_);
539 EXPECT_EQ(GURL(), history_handler->icon_url_);
540 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
542 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_);
544 // Send history response.
545 history_handler->InvokeCallback();
546 // Verify FaviconHandler status
547 EXPECT_TRUE(driver.GetActiveFaviconValidity());
548 EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
550 // Simulates update favicon url.
551 std::vector<FaviconURL> urls;
552 urls.push_back(
553 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
554 helper.OnUpdateFaviconURL(urls);
556 // Verify FaviconHandler status
557 EXPECT_EQ(1U, helper.urls().size());
558 ASSERT_TRUE(helper.current_candidate());
559 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
560 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
562 // Favicon shouldn't request to download icon.
563 EXPECT_FALSE(helper.download_handler()->HasDownload());
566 TEST_F(FaviconHandlerTest, DownloadFavicon) {
567 const GURL page_url("http://www.google.com");
568 const GURL icon_url("http://www.google.com/favicon");
570 TestFaviconDriver driver;
571 TestFaviconHandler helper(page_url, &driver, FaviconHandler::FAVICON, false);
573 helper.FetchFavicon(page_url);
574 HistoryRequestHandler* history_handler = helper.history_handler();
575 // Ensure the data given to history is correct.
576 ASSERT_TRUE(history_handler);
577 EXPECT_EQ(page_url, history_handler->page_url_);
578 EXPECT_EQ(GURL(), history_handler->icon_url_);
579 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
581 // Set icon data expired
582 SetFaviconRawBitmapResult(icon_url,
583 favicon_base::FAVICON,
584 true /* expired */,
585 &history_handler->history_results_);
586 // Send history response.
587 history_handler->InvokeCallback();
588 // Verify FaviconHandler status
589 EXPECT_TRUE(driver.GetActiveFaviconValidity());
590 EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
592 // Simulates update favicon url.
593 std::vector<FaviconURL> urls;
594 urls.push_back(
595 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
596 helper.OnUpdateFaviconURL(urls);
598 // Verify FaviconHandler status
599 EXPECT_EQ(1U, helper.urls().size());
600 ASSERT_TRUE(helper.current_candidate());
601 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
602 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
604 // Favicon should request to download icon now.
605 DownloadHandler* download_handler = helper.download_handler();
606 EXPECT_TRUE(helper.download_handler()->HasDownload());
608 // Verify the download request.
609 EXPECT_EQ(icon_url, download_handler->GetImageUrl());
611 // Reset the history_handler to verify whether favicon is set.
612 helper.set_history_handler(nullptr);
614 // Smulates download done.
615 download_handler->InvokeCallback();
617 // New icon should be saved to history backend and navigation entry.
618 history_handler = helper.history_handler();
619 ASSERT_TRUE(history_handler);
620 EXPECT_EQ(icon_url, history_handler->icon_url_);
621 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
622 EXPECT_LT(0U, history_handler->bitmap_data_.size());
623 EXPECT_EQ(page_url, history_handler->page_url_);
625 // Verify NavigationEntry.
626 EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
627 EXPECT_TRUE(driver.GetActiveFaviconValidity());
628 EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
629 EXPECT_EQ(gfx::kFaviconSize, driver.GetActiveFaviconImage().Width());
632 TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) {
633 const GURL page_url("http://www.google.com");
634 const GURL icon_url("http://www.google.com/favicon");
635 const GURL new_icon_url("http://www.google.com/new_favicon");
637 TestFaviconDriver driver;
638 TestFaviconHandler helper(page_url, &driver, FaviconHandler::FAVICON, false);
640 helper.FetchFavicon(page_url);
641 HistoryRequestHandler* history_handler = helper.history_handler();
642 // Ensure the data given to history is correct.
643 ASSERT_TRUE(history_handler);
644 EXPECT_EQ(page_url, history_handler->page_url_);
645 EXPECT_EQ(GURL(), history_handler->icon_url_);
646 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
648 // Set valid icon data.
649 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_);
651 // Send history response.
652 history_handler->InvokeCallback();
653 // Verify FaviconHandler status.
654 EXPECT_TRUE(driver.GetActiveFaviconValidity());
655 EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
657 // Reset the history_handler to verify whether new icon is requested from
658 // history.
659 helper.set_history_handler(nullptr);
661 // Simulates update with the different favicon url.
662 std::vector<FaviconURL> urls;
663 urls.push_back(FaviconURL(
664 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
665 helper.OnUpdateFaviconURL(urls);
667 // Verify FaviconHandler status.
668 EXPECT_EQ(1U, helper.urls().size());
669 ASSERT_TRUE(helper.current_candidate());
670 ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url);
671 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
673 // Favicon should be requested from history.
674 history_handler = helper.history_handler();
675 ASSERT_TRUE(history_handler);
676 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
677 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
678 EXPECT_EQ(page_url, history_handler->page_url_);
680 // Simulate not find icon.
681 history_handler->history_results_.clear();
682 history_handler->InvokeCallback();
684 // Favicon should request to download icon now.
685 DownloadHandler* download_handler = helper.download_handler();
686 EXPECT_TRUE(helper.download_handler()->HasDownload());
688 // Verify the download request.
689 EXPECT_EQ(new_icon_url, download_handler->GetImageUrl());
691 // Reset the history_handler to verify whether favicon is set.
692 helper.set_history_handler(nullptr);
694 // Smulates download done.
695 download_handler->InvokeCallback();
697 // New icon should be saved to history backend and navigation entry.
698 history_handler = helper.history_handler();
699 ASSERT_TRUE(history_handler);
700 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
701 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
702 EXPECT_LT(0U, history_handler->bitmap_data_.size());
703 EXPECT_EQ(page_url, history_handler->page_url_);
705 // Verify NavigationEntry.
706 EXPECT_EQ(new_icon_url, driver.GetActiveFaviconURL());
707 EXPECT_TRUE(driver.GetActiveFaviconValidity());
708 EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
709 EXPECT_EQ(gfx::kFaviconSize, driver.GetActiveFaviconImage().Width());
712 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) {
713 const GURL page_url("http://www.google.com");
714 const GURL icon_url("http://www.google.com/favicon");
716 TestFaviconDriver driver;
717 TestFaviconHandler helper(page_url, &driver, FaviconHandler::FAVICON, false);
719 helper.FetchFavicon(page_url);
720 HistoryRequestHandler* history_handler = helper.history_handler();
721 // Ensure the data given to history is correct.
722 ASSERT_TRUE(history_handler);
723 EXPECT_EQ(page_url, history_handler->page_url_);
724 EXPECT_EQ(GURL(), history_handler->icon_url_);
725 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
727 // Set non empty but invalid data.
728 favicon_base::FaviconRawBitmapResult bitmap_result;
729 bitmap_result.expired = false;
730 // Empty bitmap data is invalid.
731 bitmap_result.bitmap_data = new base::RefCountedBytes();
732 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize);
733 bitmap_result.icon_type = favicon_base::FAVICON;
734 bitmap_result.icon_url = icon_url;
735 history_handler->history_results_.clear();
736 history_handler->history_results_.push_back(bitmap_result);
738 // Send history response.
739 history_handler->InvokeCallback();
740 // The NavigationEntry should not be set yet as the history data is invalid.
741 EXPECT_FALSE(driver.GetActiveFaviconValidity());
742 EXPECT_EQ(GURL(), driver.GetActiveFaviconURL());
744 // Reset the history_handler to verify whether new icon is requested from
745 // history.
746 helper.set_history_handler(nullptr);
748 // Simulates update with matching favicon URL.
749 std::vector<FaviconURL> urls;
750 urls.push_back(
751 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
752 helper.OnUpdateFaviconURL(urls);
754 // A download for the favicon should be requested, and we should not do
755 // another history request.
756 DownloadHandler* download_handler = helper.download_handler();
757 EXPECT_TRUE(helper.download_handler()->HasDownload());
758 EXPECT_EQ(nullptr, helper.history_handler());
760 // Verify the download request.
761 EXPECT_EQ(icon_url, download_handler->GetImageUrl());
763 // Simulates download done.
764 download_handler->InvokeCallback();
766 // New icon should be saved to history backend and navigation entry.
767 history_handler = helper.history_handler();
768 ASSERT_TRUE(history_handler);
769 EXPECT_EQ(icon_url, history_handler->icon_url_);
770 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
771 EXPECT_LT(0U, history_handler->bitmap_data_.size());
772 EXPECT_EQ(page_url, history_handler->page_url_);
774 // Verify NavigationEntry.
775 EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
776 EXPECT_TRUE(driver.GetActiveFaviconValidity());
777 EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
778 EXPECT_EQ(gfx::kFaviconSize, driver.GetActiveFaviconImage().Width());
781 TEST_F(FaviconHandlerTest, UpdateFavicon) {
782 const GURL page_url("http://www.google.com");
783 const GURL icon_url("http://www.google.com/favicon");
784 const GURL new_icon_url("http://www.google.com/new_favicon");
786 TestFaviconDriver driver;
787 TestFaviconHandler helper(page_url, &driver, FaviconHandler::FAVICON, false);
789 helper.FetchFavicon(page_url);
790 HistoryRequestHandler* history_handler = helper.history_handler();
791 // Ensure the data given to history is correct.
792 ASSERT_TRUE(history_handler);
793 EXPECT_EQ(page_url, history_handler->page_url_);
794 EXPECT_EQ(GURL(), history_handler->icon_url_);
795 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
797 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_);
799 // Send history response.
800 history_handler->InvokeCallback();
801 // Verify FaviconHandler status.
802 EXPECT_TRUE(driver.GetActiveFaviconValidity());
803 EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
805 // Reset the history_handler to verify whether new icon is requested from
806 // history.
807 helper.set_history_handler(nullptr);
809 // Simulates update with the different favicon url.
810 std::vector<FaviconURL> urls;
811 urls.push_back(FaviconURL(
812 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
813 helper.OnUpdateFaviconURL(urls);
815 // Verify FaviconHandler status.
816 EXPECT_EQ(1U, helper.urls().size());
817 ASSERT_TRUE(helper.current_candidate());
818 ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url);
819 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
821 // Favicon should be requested from history.
822 history_handler = helper.history_handler();
823 ASSERT_TRUE(history_handler);
824 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
825 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
826 EXPECT_EQ(page_url, history_handler->page_url_);
828 // Simulate find icon.
829 SetFaviconRawBitmapResult(new_icon_url, &history_handler->history_results_);
830 history_handler->InvokeCallback();
832 // Shouldn't request download favicon
833 EXPECT_FALSE(helper.download_handler()->HasDownload());
835 // Verify the favicon status.
836 EXPECT_EQ(new_icon_url, driver.GetActiveFaviconURL());
837 EXPECT_TRUE(driver.GetActiveFaviconValidity());
838 EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
841 TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
842 const GURL page_url("http://www.google.com");
843 const GURL icon_url("http://www.google.com/favicon");
844 const GURL new_icon_url("http://www.google.com/new_favicon");
846 TestFaviconDriver driver;
847 TestFaviconHandler helper(page_url, &driver, FaviconHandler::TOUCH, false);
848 std::set<GURL> fail_downloads;
849 fail_downloads.insert(icon_url);
850 helper.download_handler()->FailDownloadForIconURLs(fail_downloads);
852 helper.FetchFavicon(page_url);
853 HistoryRequestHandler* history_handler = helper.history_handler();
854 // Ensure the data given to history is correct.
855 ASSERT_TRUE(history_handler);
856 EXPECT_EQ(page_url, history_handler->page_url_);
857 EXPECT_EQ(GURL(), history_handler->icon_url_);
858 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON,
859 history_handler->icon_type_);
861 // Icon not found.
862 history_handler->history_results_.clear();
863 // Send history response.
864 history_handler->InvokeCallback();
865 // Verify FaviconHandler status.
866 EXPECT_FALSE(driver.GetActiveFaviconValidity());
867 EXPECT_EQ(GURL(), driver.GetActiveFaviconURL());
869 // Reset the history_handler to verify whether new icon is requested from
870 // history.
871 helper.set_history_handler(nullptr);
873 // Simulates update with the different favicon url.
874 std::vector<FaviconURL> urls;
875 urls.push_back(FaviconURL(icon_url,
876 favicon_base::TOUCH_PRECOMPOSED_ICON,
877 std::vector<gfx::Size>()));
878 urls.push_back(FaviconURL(
879 new_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>()));
880 urls.push_back(FaviconURL(
881 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
882 helper.OnUpdateFaviconURL(urls);
884 // Verify FaviconHandler status.
885 EXPECT_EQ(2U, helper.urls().size());
886 ASSERT_TRUE(helper.current_candidate());
887 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
888 ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON,
889 helper.current_candidate()->icon_type);
891 // Favicon should be requested from history.
892 history_handler = helper.history_handler();
893 ASSERT_TRUE(history_handler);
894 EXPECT_EQ(icon_url, history_handler->icon_url_);
895 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_);
896 EXPECT_EQ(page_url, history_handler->page_url_);
898 // Simulate not find icon.
899 history_handler->history_results_.clear();
900 history_handler->InvokeCallback();
902 // Should request download favicon.
903 DownloadHandler* download_handler = helper.download_handler();
904 EXPECT_TRUE(helper.download_handler()->HasDownload());
906 // Verify the download request.
907 EXPECT_EQ(icon_url, download_handler->GetImageUrl());
909 // Reset the history_handler to verify whether favicon is request from
910 // history.
911 helper.set_history_handler(nullptr);
912 download_handler->InvokeCallback();
914 // Left 1 url.
915 EXPECT_EQ(1U, helper.urls().size());
916 ASSERT_TRUE(helper.current_candidate());
917 EXPECT_EQ(new_icon_url, helper.current_candidate()->icon_url);
918 EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type);
920 // Favicon should be requested from history.
921 history_handler = helper.history_handler();
922 ASSERT_TRUE(history_handler);
923 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
924 EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_);
925 EXPECT_EQ(page_url, history_handler->page_url_);
927 // Reset download handler
928 download_handler->Reset();
930 // Simulates getting a expired icon from history.
931 SetFaviconRawBitmapResult(new_icon_url,
932 favicon_base::TOUCH_ICON,
933 true /* expired */,
934 &history_handler->history_results_);
935 history_handler->InvokeCallback();
937 // Verify the download request.
938 EXPECT_TRUE(helper.download_handler()->HasDownload());
939 EXPECT_EQ(new_icon_url, download_handler->GetImageUrl());
941 helper.set_history_handler(nullptr);
943 // Simulates icon being downloaded.
944 download_handler->InvokeCallback();
946 // New icon should be saved to history backend.
947 history_handler = helper.history_handler();
948 ASSERT_TRUE(history_handler);
949 EXPECT_EQ(new_icon_url, history_handler->icon_url_);
950 EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_);
951 EXPECT_LT(0U, history_handler->bitmap_data_.size());
952 EXPECT_EQ(page_url, history_handler->page_url_);
955 TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
956 const GURL page_url("http://www.google.com");
957 const GURL icon_url("http://www.google.com/favicon");
958 const GURL new_icon_url("http://www.google.com/new_favicon");
960 TestFaviconDriver driver;
961 TestFaviconHandler helper(page_url, &driver, FaviconHandler::TOUCH, false);
963 helper.FetchFavicon(page_url);
964 HistoryRequestHandler* history_handler = helper.history_handler();
965 // Ensure the data given to history is correct.
966 ASSERT_TRUE(history_handler);
967 EXPECT_EQ(page_url, history_handler->page_url_);
968 EXPECT_EQ(GURL(), history_handler->icon_url_);
969 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON,
970 history_handler->icon_type_);
972 // Icon not found.
973 history_handler->history_results_.clear();
974 // Send history response.
975 history_handler->InvokeCallback();
976 // Verify FaviconHandler status.
977 EXPECT_FALSE(driver.GetActiveFaviconValidity());
978 EXPECT_EQ(GURL(), driver.GetActiveFaviconURL());
980 // Reset the history_handler to verify whether new icon is requested from
981 // history.
982 helper.set_history_handler(nullptr);
984 // Simulates update with the different favicon url.
985 std::vector<FaviconURL> urls;
986 urls.push_back(FaviconURL(icon_url,
987 favicon_base::TOUCH_PRECOMPOSED_ICON,
988 std::vector<gfx::Size>()));
989 urls.push_back(FaviconURL(
990 new_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>()));
991 urls.push_back(FaviconURL(
992 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
993 helper.OnUpdateFaviconURL(urls);
995 // Verify FaviconHandler status.
996 EXPECT_EQ(2U, helper.urls().size());
997 ASSERT_TRUE(helper.current_candidate());
998 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
999 ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON,
1000 helper.current_candidate()->icon_type);
1002 // Favicon should be requested from history.
1003 history_handler = helper.history_handler();
1004 ASSERT_TRUE(history_handler);
1005 EXPECT_EQ(icon_url, history_handler->icon_url_);
1006 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_);
1007 EXPECT_EQ(page_url, history_handler->page_url_);
1009 // Simulate not find icon.
1010 history_handler->history_results_.clear();
1011 history_handler->InvokeCallback();
1013 // Should request download favicon.
1014 DownloadHandler* download_handler = helper.download_handler();
1015 EXPECT_TRUE(helper.download_handler()->HasDownload());
1017 // Verify the download request.
1018 EXPECT_EQ(icon_url, download_handler->GetImageUrl());
1020 // Reset the history_handler to verify whether favicon is request from
1021 // history.
1022 helper.set_history_handler(nullptr);
1023 const GURL latest_icon_url("http://www.google.com/latest_favicon");
1024 std::vector<FaviconURL> latest_urls;
1025 latest_urls.push_back(FaviconURL(
1026 latest_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>()));
1027 helper.OnUpdateFaviconURL(latest_urls);
1029 EXPECT_EQ(1U, helper.urls().size());
1030 EXPECT_EQ(latest_icon_url, helper.current_candidate()->icon_url);
1031 EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type);
1033 // Whether new icon is requested from history
1034 history_handler = helper.history_handler();
1035 ASSERT_TRUE(history_handler);
1036 EXPECT_EQ(latest_icon_url, history_handler->icon_url_);
1037 EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_);
1038 EXPECT_EQ(page_url, history_handler->page_url_);
1040 // Reset the history_handler to verify whether favicon is request from
1041 // history.
1042 // Save the callback for late use.
1043 favicon_base::FaviconResultsCallback callback = history_handler->callback_;
1044 helper.set_history_handler(nullptr);
1046 // Simulates download succeed.
1047 download_handler->InvokeCallback();
1048 // The downloaded icon should be thrown away as there is favicon update.
1049 EXPECT_FALSE(helper.history_handler());
1051 download_handler->Reset();
1053 // Simulates getting the icon from history.
1054 scoped_ptr<HistoryRequestHandler> handler;
1055 handler.reset(new HistoryRequestHandler(
1056 page_url, latest_icon_url, favicon_base::TOUCH_ICON, callback));
1057 SetFaviconRawBitmapResult(latest_icon_url,
1058 favicon_base::TOUCH_ICON,
1059 false /* expired */,
1060 &handler->history_results_);
1061 handler->InvokeCallback();
1063 // No download request.
1064 EXPECT_FALSE(download_handler->HasDownload());
1067 // Test the favicon which is selected when the web page provides several
1068 // favicons and none of the favicons are cached in history.
1069 // The goal of this test is to be more of an integration test than
1070 // SelectFaviconFramesTest.*.
1071 TEST_F(FaviconHandlerTest, MultipleFavicons) {
1072 const GURL kPageURL("http://www.google.com");
1073 const FaviconURL kSourceIconURLs[] = {
1074 FaviconURL(GURL("http://www.google.com/a"),
1075 favicon_base::FAVICON,
1076 std::vector<gfx::Size>()),
1077 FaviconURL(GURL("http://www.google.com/b"),
1078 favicon_base::FAVICON,
1079 std::vector<gfx::Size>()),
1080 FaviconURL(GURL("http://www.google.com/c"),
1081 favicon_base::FAVICON,
1082 std::vector<gfx::Size>()),
1083 FaviconURL(GURL("http://www.google.com/d"),
1084 favicon_base::FAVICON,
1085 std::vector<gfx::Size>()),
1086 FaviconURL(GURL("http://www.google.com/e"),
1087 favicon_base::FAVICON,
1088 std::vector<gfx::Size>())};
1090 // Set the supported scale factors to 1x and 2x. This affects the behavior of
1091 // SelectFaviconFrames().
1092 std::vector<ui::ScaleFactor> scale_factors;
1093 scale_factors.push_back(ui::SCALE_FACTOR_100P);
1094 scale_factors.push_back(ui::SCALE_FACTOR_200P);
1095 ui::test::ScopedSetSupportedScaleFactors scoped_supported(scale_factors);
1097 // 1) Test that if there are several single resolution favicons to choose from
1098 // that the largest exact match is chosen.
1099 TestFaviconDriver driver1;
1100 TestFaviconHandler handler1(kPageURL, &driver1, FaviconHandler::FAVICON,
1101 false);
1103 const int kSizes1[] = { 16, 24, 32, 48, 256 };
1104 std::vector<FaviconURL> urls1(kSourceIconURLs,
1105 kSourceIconURLs + arraysize(kSizes1));
1106 DownloadTillDoneIgnoringHistory(
1107 &driver1, &handler1, kPageURL, urls1, kSizes1);
1109 EXPECT_EQ(0u, handler1.image_urls().size());
1110 EXPECT_TRUE(driver1.GetActiveFaviconValidity());
1111 EXPECT_FALSE(driver1.GetActiveFaviconImage().IsEmpty());
1112 EXPECT_EQ(gfx::kFaviconSize, driver1.GetActiveFaviconImage().Width());
1114 size_t expected_index = 2u;
1115 EXPECT_EQ(32, kSizes1[expected_index]);
1116 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url,
1117 driver1.GetActiveFaviconURL());
1119 // 2) Test that if there are several single resolution favicons to choose
1120 // from, the exact match is preferred even if it results in upsampling.
1121 TestFaviconDriver driver2;
1122 TestFaviconHandler handler2(kPageURL, &driver2, FaviconHandler::FAVICON,
1123 false);
1125 const int kSizes2[] = { 16, 24, 48, 256 };
1126 std::vector<FaviconURL> urls2(kSourceIconURLs,
1127 kSourceIconURLs + arraysize(kSizes2));
1128 DownloadTillDoneIgnoringHistory(
1129 &driver2, &handler2, kPageURL, urls2, kSizes2);
1130 EXPECT_TRUE(driver2.GetActiveFaviconValidity());
1131 expected_index = 0u;
1132 EXPECT_EQ(16, kSizes2[expected_index]);
1133 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url,
1134 driver2.GetActiveFaviconURL());
1136 // 3) Test that favicons which need to be upsampled a little or downsampled
1137 // a little are preferred over huge favicons.
1138 TestFaviconDriver driver3;
1139 TestFaviconHandler handler3(kPageURL, &driver3, FaviconHandler::FAVICON,
1140 false);
1142 const int kSizes3[] = { 256, 48 };
1143 std::vector<FaviconURL> urls3(kSourceIconURLs,
1144 kSourceIconURLs + arraysize(kSizes3));
1145 DownloadTillDoneIgnoringHistory(
1146 &driver3, &handler3, kPageURL, urls3, kSizes3);
1147 EXPECT_TRUE(driver3.GetActiveFaviconValidity());
1148 expected_index = 1u;
1149 EXPECT_EQ(48, kSizes3[expected_index]);
1150 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url,
1151 driver3.GetActiveFaviconURL());
1153 TestFaviconDriver driver4;
1154 TestFaviconHandler handler4(kPageURL, &driver4, FaviconHandler::FAVICON,
1155 false);
1157 const int kSizes4[] = { 17, 256 };
1158 std::vector<FaviconURL> urls4(kSourceIconURLs,
1159 kSourceIconURLs + arraysize(kSizes4));
1160 DownloadTillDoneIgnoringHistory(
1161 &driver4, &handler4, kPageURL, urls4, kSizes4);
1162 EXPECT_TRUE(driver4.GetActiveFaviconValidity());
1163 expected_index = 0u;
1164 EXPECT_EQ(17, kSizes4[expected_index]);
1165 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url,
1166 driver4.GetActiveFaviconURL());
1169 // Test that the best favicon is selected when:
1170 // - The page provides several favicons.
1171 // - Downloading one of the page's icon URLs previously returned a 404.
1172 // - None of the favicons are cached in the Favicons database.
1173 TEST_F(FaviconHandlerTest, MultipleFavicons404) {
1174 const GURL kPageURL("http://www.google.com");
1175 const GURL k404IconURL("http://www.google.com/404.png");
1176 const FaviconURL k404FaviconURL(
1177 k404IconURL, favicon_base::FAVICON, std::vector<gfx::Size>());
1178 const FaviconURL kFaviconURLs[] = {
1179 FaviconURL(GURL("http://www.google.com/a"),
1180 favicon_base::FAVICON,
1181 std::vector<gfx::Size>()),
1182 k404FaviconURL,
1183 FaviconURL(GURL("http://www.google.com/c"),
1184 favicon_base::FAVICON,
1185 std::vector<gfx::Size>()),
1188 TestFaviconDriver driver;
1189 TestFaviconHandler handler(kPageURL, &driver, FaviconHandler::FAVICON, false);
1190 DownloadHandler* download_handler = handler.download_handler();
1192 std::set<GURL> k404URLs;
1193 k404URLs.insert(k404IconURL);
1194 download_handler->FailDownloadForIconURLs(k404URLs);
1196 // Make the initial download for |k404IconURL| fail.
1197 const int kSizes1[] = { 0 };
1198 std::vector<FaviconURL> urls1(1u, k404FaviconURL);
1199 DownloadTillDoneIgnoringHistory(
1200 &driver, &handler, kPageURL, urls1, kSizes1);
1201 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL));
1203 // Do a fetch now that the initial download for |k404IconURL| has failed. The
1204 // behavior is different because OnDidDownloadFavicon() is invoked
1205 // synchronously from DownloadFavicon().
1206 const int kSizes2[] = { 10, 0, 16 };
1207 std::vector<FaviconURL> urls2(kFaviconURLs,
1208 kFaviconURLs + arraysize(kFaviconURLs));
1209 DownloadTillDoneIgnoringHistory(
1210 &driver, &handler, kPageURL, urls2, kSizes2);
1212 EXPECT_EQ(0u, handler.image_urls().size());
1213 EXPECT_TRUE(driver.GetActiveFaviconValidity());
1214 EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
1215 int expected_index = 2u;
1216 EXPECT_EQ(16, kSizes2[expected_index]);
1217 EXPECT_EQ(kFaviconURLs[expected_index].icon_url,
1218 driver.GetActiveFaviconURL());
1221 // Test that no favicon is selected when:
1222 // - The page provides several favicons.
1223 // - Downloading the page's icons has previously returned a 404.
1224 // - None of the favicons are cached in the Favicons database.
1225 TEST_F(FaviconHandlerTest, MultipleFaviconsAll404) {
1226 const GURL kPageURL("http://www.google.com");
1227 const GURL k404IconURL1("http://www.google.com/4041.png");
1228 const GURL k404IconURL2("http://www.google.com/4042.png");
1229 const FaviconURL kFaviconURLs[] = {
1230 FaviconURL(k404IconURL1,
1231 favicon_base::FAVICON,
1232 std::vector<gfx::Size>()),
1233 FaviconURL(k404IconURL2,
1234 favicon_base::FAVICON,
1235 std::vector<gfx::Size>()),
1238 TestFaviconDriver driver;
1239 TestFaviconHandler handler(kPageURL, &driver, FaviconHandler::FAVICON, false);
1240 DownloadHandler* download_handler = handler.download_handler();
1242 std::set<GURL> k404URLs;
1243 k404URLs.insert(k404IconURL1);
1244 k404URLs.insert(k404IconURL2);
1245 download_handler->FailDownloadForIconURLs(k404URLs);
1247 // Make the initial downloads for |kFaviconURLs| fail.
1248 for (const FaviconURL& favicon_url : kFaviconURLs) {
1249 const int kSizes[] = { 0 };
1250 std::vector<FaviconURL> urls(1u, favicon_url);
1251 DownloadTillDoneIgnoringHistory(&driver, &handler, kPageURL, urls, kSizes);
1253 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL1));
1254 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL2));
1256 // Do a fetch now that the initial downloads for |kFaviconURLs| have failed.
1257 // The behavior is different because OnDidDownloadFavicon() is invoked
1258 // synchronously from DownloadFavicon().
1259 const int kSizes[] = { 0, 0 };
1260 std::vector<FaviconURL> urls(kFaviconURLs,
1261 kFaviconURLs + arraysize(kFaviconURLs));
1262 DownloadTillDoneIgnoringHistory(&driver, &handler, kPageURL, urls, kSizes);
1264 EXPECT_EQ(0u, handler.image_urls().size());
1265 EXPECT_FALSE(driver.GetActiveFaviconValidity());
1266 EXPECT_TRUE(driver.GetActiveFaviconImage().IsEmpty());
1269 // Test that no favicon is selected when the page's only icon uses an invalid
1270 // URL syntax.
1271 TEST_F(FaviconHandlerTest, FaviconInvalidURL) {
1272 const GURL kPageURL("http://www.google.com");
1273 const GURL kInvalidFormatURL("invalid");
1274 ASSERT_TRUE(kInvalidFormatURL.is_empty());
1276 FaviconURL favicon_url(kInvalidFormatURL, favicon_base::FAVICON,
1277 std::vector<gfx::Size>());
1279 TestFaviconDriver driver;
1280 TestFaviconHandler handler(kPageURL, &driver, FaviconHandler::FAVICON, false);
1281 UpdateFaviconURL(&driver, &handler, kPageURL,
1282 std::vector<FaviconURL>(1u, favicon_url));
1283 EXPECT_EQ(0u, handler.image_urls().size());
1286 TEST_F(FaviconHandlerTest, TestSortFavicon) {
1287 const GURL kPageURL("http://www.google.com");
1288 std::vector<gfx::Size> icon1;
1289 icon1.push_back(gfx::Size(1024, 1024));
1290 icon1.push_back(gfx::Size(512, 512));
1292 std::vector<gfx::Size> icon2;
1293 icon2.push_back(gfx::Size(15, 15));
1294 icon2.push_back(gfx::Size(16, 16));
1296 std::vector<gfx::Size> icon3;
1297 icon3.push_back(gfx::Size(16, 16));
1298 icon3.push_back(gfx::Size(14, 14));
1300 const FaviconURL kSourceIconURLs[] = {
1301 FaviconURL(GURL("http://www.google.com/a"), favicon_base::FAVICON, icon1),
1302 FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON, icon2),
1303 FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON, icon3),
1304 FaviconURL(GURL("http://www.google.com/d"),
1305 favicon_base::FAVICON,
1306 std::vector<gfx::Size>()),
1307 FaviconURL(GURL("http://www.google.com/e"),
1308 favicon_base::FAVICON,
1309 std::vector<gfx::Size>())};
1311 TestFaviconDriver driver1;
1312 TestFaviconHandler handler1(kPageURL, &driver1, FaviconHandler::FAVICON,
1313 true);
1314 std::vector<FaviconURL> urls1(kSourceIconURLs,
1315 kSourceIconURLs + arraysize(kSourceIconURLs));
1316 UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
1318 struct ExpectedResult {
1319 // The favicon's index in kSourceIconURLs.
1320 size_t favicon_index;
1321 // Width of largest bitmap.
1322 int width;
1323 } results[] = {
1324 // First is icon1, though its size larger than maximal.
1325 {0, 1024},
1326 // Second is icon2
1327 // The 16x16 is largest.
1328 {1, 16},
1329 // Third is icon3 though it has same size as icon2.
1330 // The 16x16 is largest.
1331 {2, 16},
1332 // The rest of bitmaps come in order, there is no sizes attribute.
1333 {3, -1},
1334 {4, -1},
1336 const std::vector<FaviconURL>& icons = handler1.image_urls();
1337 ASSERT_EQ(5u, icons.size());
1338 for (size_t i = 0; i < icons.size(); ++i) {
1339 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
1340 icons[i].icon_url);
1341 if (results[i].width != -1)
1342 EXPECT_EQ(results[i].width, icons[i].icon_sizes[0].width());
1346 TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) {
1347 const GURL kPageURL("http://www.google.com");
1348 std::vector<gfx::Size> icon1;
1349 icon1.push_back(gfx::Size(1024, 1024));
1350 icon1.push_back(gfx::Size(512, 512));
1352 std::vector<gfx::Size> icon2;
1353 icon2.push_back(gfx::Size(15, 15));
1354 icon2.push_back(gfx::Size(14, 14));
1356 std::vector<gfx::Size> icon3;
1357 icon3.push_back(gfx::Size(16, 16));
1358 icon3.push_back(gfx::Size(512, 512));
1360 const FaviconURL kSourceIconURLs[] = {
1361 FaviconURL(
1362 GURL("http://www.google.com/a"), favicon_base::FAVICON, icon1),
1363 FaviconURL(
1364 GURL("http://www.google.com/b"), favicon_base::FAVICON, icon2),
1365 FaviconURL(
1366 GURL("http://www.google.com/c"), favicon_base::FAVICON, icon3),
1367 FaviconURL(GURL("http://www.google.com/d"),
1368 favicon_base::FAVICON,
1369 std::vector<gfx::Size>()),
1370 FaviconURL(GURL("http://www.google.com/e"),
1371 favicon_base::FAVICON,
1372 std::vector<gfx::Size>())};
1374 TestFaviconDriver driver1;
1375 TestFaviconHandler handler1(kPageURL, &driver1, FaviconHandler::FAVICON,
1376 true);
1378 std::set<GURL> fail_icon_urls;
1379 for (size_t i = 0; i < arraysize(kSourceIconURLs); ++i) {
1380 fail_icon_urls.insert(kSourceIconURLs[i].icon_url);
1382 handler1.download_handler()->FailDownloadForIconURLs(fail_icon_urls);
1384 std::vector<FaviconURL> urls1(kSourceIconURLs,
1385 kSourceIconURLs + arraysize(kSourceIconURLs));
1386 UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
1388 // Simulate the download failed, to check whether the icons were requested
1389 // to download according their size.
1390 struct ExpectedResult {
1391 // The size of image_urls_.
1392 size_t image_urls_size;
1393 // The favicon's index in kSourceIconURLs.
1394 size_t favicon_index;
1395 // Width of largest bitmap.
1396 int width;
1397 } results[] = {
1398 {5, 0, 1024},
1399 {4, 2, 512},
1400 {3, 1, 15},
1401 // The rest of bitmaps come in order.
1402 {2, 3, -1},
1403 {1, 4, -1},
1406 for (int i = 0; i < 5; ++i) {
1407 ASSERT_EQ(results[i].image_urls_size, handler1.image_urls().size());
1408 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
1409 handler1.current_candidate()->icon_url);
1410 if (results[i].width != -1) {
1411 EXPECT_EQ(results[i].width, handler1.current_candidate()->
1412 icon_sizes[0].width());
1415 // Simulate no favicon from history.
1416 handler1.history_handler()->history_results_.clear();
1417 handler1.history_handler()->InvokeCallback();
1419 // Verify download request
1420 ASSERT_TRUE(handler1.download_handler()->HasDownload());
1421 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
1422 handler1.download_handler()->GetImageUrl());
1424 handler1.download_handler()->InvokeCallback();
1425 handler1.download_handler()->Reset();
1429 TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) {
1430 const GURL kPageURL("http://www.google.com");
1432 std::vector<gfx::Size> one_icon;
1433 one_icon.push_back(gfx::Size(15, 15));
1435 std::vector<gfx::Size> two_icons;
1436 two_icons.push_back(gfx::Size(14, 14));
1437 two_icons.push_back(gfx::Size(16, 16));
1439 const FaviconURL kSourceIconURLs[] = {
1440 FaviconURL(
1441 GURL("http://www.google.com/b"), favicon_base::FAVICON, one_icon),
1442 FaviconURL(
1443 GURL("http://www.google.com/c"), favicon_base::FAVICON, two_icons)};
1445 TestFaviconDriver driver1;
1446 TestFaviconHandler handler1(kPageURL, &driver1, FaviconHandler::FAVICON,
1447 true);
1448 std::vector<FaviconURL> urls1(kSourceIconURLs,
1449 kSourceIconURLs + arraysize(kSourceIconURLs));
1450 UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
1452 ASSERT_EQ(2u, handler1.urls().size());
1454 // Index of largest favicon in kSourceIconURLs.
1455 size_t i = 1;
1456 // The largest bitmap's index in Favicon .
1457 int b = 1;
1459 // Verify the icon_bitmaps_ was initialized correctly.
1460 EXPECT_EQ(kSourceIconURLs[i].icon_url,
1461 handler1.current_candidate()->icon_url);
1462 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
1463 handler1.current_candidate()->icon_sizes[0]);
1465 // Simulate no favicon from history.
1466 handler1.history_handler()->history_results_.clear();
1467 handler1.history_handler()->InvokeCallback();
1469 // Verify download request
1470 ASSERT_TRUE(handler1.download_handler()->HasDownload());
1471 EXPECT_EQ(kSourceIconURLs[i].icon_url,
1472 handler1.download_handler()->GetImageUrl());
1474 // Give the correct download result.
1475 std::vector<int> sizes;
1476 for (std::vector<gfx::Size>::const_iterator j =
1477 kSourceIconURLs[i].icon_sizes.begin();
1478 j != kSourceIconURLs[i].icon_sizes.end(); ++j)
1479 sizes.push_back(j->width());
1481 handler1.download_handler()->SetImageSizes(sizes);
1482 handler1.download_handler()->InvokeCallback();
1484 // Verify the largest bitmap has been saved into history.
1485 EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_);
1486 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
1487 handler1.history_handler()->size_);
1488 // Verify NotifyFaviconAvailable().
1489 EXPECT_FALSE(driver1.update_active_favicon());
1490 EXPECT_EQ(kSourceIconURLs[i].icon_url, driver1.available_icon_url());
1491 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
1492 driver1.available_favicon().Size());
1495 TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) {
1496 const GURL kPageURL("http://www.google.com");
1497 const int kMaximalSize =
1498 TestFaviconHandler::GetMaximalIconSize(favicon_base::FAVICON);
1500 std::vector<gfx::Size> icon1;
1501 icon1.push_back(gfx::Size(kMaximalSize + 1, kMaximalSize + 1));
1503 std::vector<gfx::Size> icon2;
1504 icon2.push_back(gfx::Size(kMaximalSize + 2, kMaximalSize + 2));
1506 const FaviconURL kSourceIconURLs[] = {
1507 FaviconURL(
1508 GURL("http://www.google.com/b"), favicon_base::FAVICON, icon1),
1509 FaviconURL(
1510 GURL("http://www.google.com/c"), favicon_base::FAVICON, icon2)};
1512 TestFaviconDriver driver1;
1513 TestFaviconHandler handler1(kPageURL, &driver1, FaviconHandler::FAVICON,
1514 true);
1515 std::vector<FaviconURL> urls1(kSourceIconURLs,
1516 kSourceIconURLs + arraysize(kSourceIconURLs));
1517 UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
1519 ASSERT_EQ(2u, handler1.urls().size());
1521 // Index of largest favicon in kSourceIconURLs.
1522 size_t i = 1;
1523 // The largest bitmap's index in Favicon .
1524 int b = 0;
1526 // Verify the icon_bitmaps_ was initialized correctly.
1527 EXPECT_EQ(kSourceIconURLs[i].icon_url,
1528 handler1.current_candidate()->icon_url);
1529 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
1530 handler1.current_candidate()->icon_sizes[0]);
1532 // Simulate no favicon from history.
1533 handler1.history_handler()->history_results_.clear();
1534 handler1.history_handler()->InvokeCallback();
1536 // Verify download request
1537 ASSERT_TRUE(handler1.download_handler()->HasDownload());
1538 EXPECT_EQ(kSourceIconURLs[i].icon_url,
1539 handler1.download_handler()->GetImageUrl());
1541 // Give the scaled download bitmap.
1542 std::vector<int> sizes;
1543 sizes.push_back(kMaximalSize);
1545 handler1.download_handler()->SetImageSizes(sizes);
1546 handler1.download_handler()->InvokeCallback();
1548 // Verify the largest bitmap has been saved into history though it was
1549 // scaled down to maximal size and smaller than icon1 now.
1550 EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_);
1551 EXPECT_EQ(gfx::Size(kMaximalSize, kMaximalSize),
1552 handler1.history_handler()->size_);
1555 TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) {
1556 const GURL kPageURL("http://www.google.com");
1558 std::vector<gfx::Size> icon1;
1559 icon1.push_back(gfx::Size(16, 16));
1560 const int actual_size1 = 10;
1562 std::vector<gfx::Size> icon2;
1563 icon2.push_back(gfx::Size(15, 15));
1564 const int actual_size2 = 12;
1566 const FaviconURL kSourceIconURLs[] = {
1567 FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON, icon1),
1568 FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON, icon2),
1569 FaviconURL(GURL("http://www.google.com/d"),
1570 favicon_base::FAVICON,
1571 std::vector<gfx::Size>())};
1573 TestFaviconDriver driver1;
1574 TestFaviconHandler handler1(kPageURL, &driver1, FaviconHandler::FAVICON,
1575 true);
1576 std::vector<FaviconURL> urls1(kSourceIconURLs,
1577 kSourceIconURLs + arraysize(kSourceIconURLs));
1578 UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
1579 ASSERT_EQ(3u, handler1.urls().size());
1581 // Simulate no favicon from history.
1582 handler1.history_handler()->history_results_.clear();
1583 handler1.history_handler()->InvokeCallback();
1585 // Verify the first icon was request to download
1586 ASSERT_TRUE(handler1.download_handler()->HasDownload());
1587 EXPECT_EQ(kSourceIconURLs[0].icon_url,
1588 handler1.download_handler()->GetImageUrl());
1590 // Give the incorrect size.
1591 std::vector<int> sizes;
1592 sizes.push_back(actual_size1);
1593 handler1.download_handler()->SetImageSizes(sizes);
1594 handler1.download_handler()->InvokeCallback();
1595 handler1.download_handler()->Reset();
1597 // Simulate no favicon from history.
1598 handler1.history_handler()->history_results_.clear();
1599 handler1.history_handler()->InvokeCallback();
1601 // Verify the 2nd icon was request to download
1602 ASSERT_TRUE(handler1.download_handler()->HasDownload());
1603 EXPECT_EQ(kSourceIconURLs[1].icon_url,
1604 handler1.download_handler()->GetImageUrl());
1606 // Very the best candidate is icon1
1607 EXPECT_EQ(kSourceIconURLs[0].icon_url,
1608 handler1.best_favicon_candidate().image_url);
1609 EXPECT_EQ(gfx::Size(actual_size1, actual_size1),
1610 handler1.best_favicon_candidate().image.Size());
1612 // Give the incorrect size.
1613 sizes.clear();
1614 sizes.push_back(actual_size2);
1615 handler1.download_handler()->SetImageSizes(sizes);
1616 handler1.download_handler()->InvokeCallback();
1617 handler1.download_handler()->Reset();
1619 // Verify icon2 has been saved into history.
1620 EXPECT_EQ(kSourceIconURLs[1].icon_url, handler1.history_handler()->icon_url_);
1621 EXPECT_EQ(gfx::Size(actual_size2, actual_size2),
1622 handler1.history_handler()->size_);
1625 class FaviconHandlerActiveFaviconValidityParamTest :
1626 public FaviconHandlerTest,
1627 public ::testing::WithParamInterface<bool> {
1628 public:
1629 FaviconHandlerActiveFaviconValidityParamTest() {}
1631 ~FaviconHandlerActiveFaviconValidityParamTest() override {}
1633 bool GetActiveFaviconValiditySetting() {
1634 return GetParam();
1637 private:
1638 DISALLOW_COPY_AND_ASSIGN(FaviconHandlerActiveFaviconValidityParamTest);
1641 TEST_P(FaviconHandlerActiveFaviconValidityParamTest,
1642 TestDownloadLargestIconDoesNotImpactActiveFaviconValidity) {
1643 const GURL page_url("http://www.google.com");
1645 std::vector<gfx::Size> one_icon;
1646 one_icon.push_back(gfx::Size(15, 15));
1648 const GURL old_favicon_url("http://www.google.com/old");
1649 const GURL new_favicon_url("http://www.google.com/b");
1650 const FaviconURL source_icon_urls[] = {
1651 FaviconURL(new_favicon_url, favicon_base::FAVICON, one_icon)};
1652 TestFaviconDriver driver1;
1653 TestFaviconHandler handler1(page_url, &driver1, FaviconHandler::FAVICON,
1654 true);
1655 std::vector<FaviconURL> urls1(source_icon_urls,
1656 source_icon_urls + arraysize(source_icon_urls));
1657 UpdateFaviconURL(&driver1, &handler1, page_url, urls1);
1659 HistoryRequestHandler* history_handler = handler1.history_handler();
1661 // Simulate the active favicon is updated, this shouldn't happen in real
1662 // use case, but we want to verify the behavior below is not impacted by
1663 // accident.
1664 driver1.SetActiveFaviconValidity(GetActiveFaviconValiditySetting());
1665 // Simulate the get favicon from history, but favicon URL didn't match.
1666 SetFaviconRawBitmapResult(old_favicon_url,
1667 &history_handler->history_results_);
1668 history_handler->InvokeCallback();
1669 // Since we downloaded largest icon, and don't want to set active favicon
1670 // NotifyFaviconAvaliable() should be called with is_active_favicon as false.
1671 EXPECT_EQ(old_favicon_url, driver1.available_icon_url());
1672 EXPECT_FALSE(driver1.update_active_favicon());
1673 EXPECT_EQ(1u, driver1.num_favicon_available());
1675 // We are trying to get favicon from history again.
1676 history_handler = handler1.history_handler();
1677 EXPECT_EQ(new_favicon_url, history_handler->icon_url_);
1678 // Simulate the get expired favicon from history.
1679 history_handler->history_results_.clear();
1680 SetFaviconRawBitmapResult(new_favicon_url, favicon_base::FAVICON, true,
1681 &history_handler->history_results_);
1682 history_handler->InvokeCallback();
1683 // Since we downloaded largest icon, and don't want to set active favicon
1684 // NotifyFaviconAvaliable() should be called with is_active_favicon as false.
1685 EXPECT_EQ(new_favicon_url, driver1.available_icon_url());
1686 EXPECT_FALSE(driver1.update_active_favicon());
1687 EXPECT_EQ(2u, driver1.num_favicon_available());
1689 // We are trying to download favicon.
1690 DownloadHandler* download_handler = handler1.download_handler();
1691 EXPECT_TRUE(download_handler->HasDownload());
1692 EXPECT_EQ(new_favicon_url, download_handler->GetImageUrl());
1693 // Simulate the download succeed.
1694 download_handler->InvokeCallback();
1695 // Since we downloaded largest icon, and don't want to set active favicon
1696 // NotifyFaviconAvaliable() should be called with is_active_favicon as false.
1697 EXPECT_EQ(new_favicon_url, driver1.available_icon_url());
1698 EXPECT_FALSE(driver1.update_active_favicon());
1699 EXPECT_EQ(3u, driver1.num_favicon_available());
1702 INSTANTIATE_TEST_CASE_P(FaviconHandlerTestActiveFaviconValidityTrueOrFalse,
1703 FaviconHandlerActiveFaviconValidityParamTest,
1704 ::testing::Bool());
1706 } // namespace
1707 } // namespace favicon