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 "base/memory/scoped_ptr.h"
8 #include "chrome/browser/favicon/chrome_favicon_client_factory.h"
9 #include "chrome/browser/favicon/favicon_service_factory.h"
10 #include "chrome/browser/favicon/favicon_tab_helper.h"
11 #include "chrome/browser/history/history_service_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "components/favicon/core/favicon_service.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/gfx/codec/png_codec.h"
17 #include "ui/gfx/favicon_size.h"
18 #include "ui/gfx/image/image.h"
20 class TestFaviconHandler
;
22 using favicon::FaviconURL
;
26 // Fill the given bmp with valid png data.
27 void FillDataToBitmap(int w
, int h
, SkBitmap
* bmp
) {
28 bmp
->allocN32Pixels(w
, h
);
30 unsigned char* src_data
=
31 reinterpret_cast<unsigned char*>(bmp
->getAddr32(0, 0));
32 for (int i
= 0; i
< w
* h
; i
++) {
33 src_data
[i
* 4 + 0] = static_cast<unsigned char>(i
% 255);
34 src_data
[i
* 4 + 1] = static_cast<unsigned char>(i
% 255);
35 src_data
[i
* 4 + 2] = static_cast<unsigned char>(i
% 255);
36 src_data
[i
* 4 + 3] = static_cast<unsigned char>(i
% 255);
40 // Fill the given data buffer with valid png data.
41 void FillBitmap(int w
, int h
, std::vector
<unsigned char>* output
) {
43 FillDataToBitmap(w
, h
, &bitmap
);
44 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap
, false, output
);
47 void SetFaviconRawBitmapResult(
49 favicon_base::IconType icon_type
,
51 std::vector
<favicon_base::FaviconRawBitmapResult
>* favicon_bitmap_results
) {
52 scoped_refptr
<base::RefCountedBytes
> data(new base::RefCountedBytes());
53 FillBitmap(gfx::kFaviconSize
, gfx::kFaviconSize
, &data
->data());
54 favicon_base::FaviconRawBitmapResult bitmap_result
;
55 bitmap_result
.expired
= expired
;
56 bitmap_result
.bitmap_data
= data
;
57 // Use a pixel size other than (0,0) as (0,0) has a special meaning.
58 bitmap_result
.pixel_size
= gfx::Size(gfx::kFaviconSize
, gfx::kFaviconSize
);
59 bitmap_result
.icon_type
= icon_type
;
60 bitmap_result
.icon_url
= icon_url
;
62 favicon_bitmap_results
->push_back(bitmap_result
);
65 void SetFaviconRawBitmapResult(
67 std::vector
<favicon_base::FaviconRawBitmapResult
>* favicon_bitmap_results
) {
68 SetFaviconRawBitmapResult(icon_url
,
69 favicon_base::FAVICON
,
71 favicon_bitmap_results
);
74 // This class is used to save the download request for verifying with test case.
75 // It also will be used to invoke the onDidDownload callback.
76 class DownloadHandler
{
78 explicit DownloadHandler(TestFaviconHandler
* favicon_helper
)
79 : favicon_helper_(favicon_helper
),
92 const GURL
& image_url
,
93 const std::vector
<int>& image_sizes
,
95 download_
.reset(new Download(
96 download_id
, image_url
, image_sizes
, max_image_size
, false));
99 void InvokeCallback();
101 void set_failed(bool failed
) { failed_
= failed
; }
103 bool HasDownload() const { return download_
.get(); }
104 const GURL
& GetImageUrl() const { return download_
->image_url
; }
105 void SetImageSizes(const std::vector
<int>& sizes
) {
106 download_
->image_sizes
= sizes
; }
112 const std::vector
<int>& sizes
,
118 max_image_size(max_size
) {}
122 std::vector
<int> image_sizes
;
126 TestFaviconHandler
* favicon_helper_
;
127 scoped_ptr
<Download
> download_
;
130 DISALLOW_COPY_AND_ASSIGN(DownloadHandler
);
133 // This class is used to save the history request for verifying with test case.
134 // It also will be used to simulate the history response.
135 class HistoryRequestHandler
{
137 HistoryRequestHandler(const GURL
& page_url
,
138 const GURL
& icon_url
,
140 const favicon_base::FaviconResultsCallback
& callback
)
141 : page_url_(page_url
),
143 icon_type_(icon_type
),
144 callback_(callback
) {
147 HistoryRequestHandler(const GURL
& page_url
,
148 const GURL
& icon_url
,
150 const std::vector
<unsigned char>& bitmap_data
,
151 const gfx::Size
& size
)
152 : page_url_(page_url
),
154 icon_type_(icon_type
),
155 bitmap_data_(bitmap_data
),
159 ~HistoryRequestHandler() {}
160 void InvokeCallback();
162 const GURL page_url_
;
163 const GURL icon_url_
;
164 const int icon_type_
;
165 const std::vector
<unsigned char> bitmap_data_
;
166 const gfx::Size size_
;
167 std::vector
<favicon_base::FaviconRawBitmapResult
> history_results_
;
168 favicon_base::FaviconResultsCallback callback_
;
171 DISALLOW_COPY_AND_ASSIGN(HistoryRequestHandler
);
176 class TestFaviconDriver
: public favicon::FaviconDriver
{
179 : favicon_validity_(false),
180 num_active_favicon_(0),
181 num_favicon_available_(0),
182 update_active_favicon_(false) {}
184 ~TestFaviconDriver() override
{}
186 bool IsOffTheRecord() override
{ return false; }
188 bool IsBookmarked(const GURL
& url
) override
{ return false; }
190 const gfx::Image
GetActiveFaviconImage() override
{ return image_
; }
192 const GURL
GetActiveFaviconURL() override
{ return favicon_url_
; }
194 bool GetActiveFaviconValidity() override
{ return favicon_validity_
; }
196 const GURL
GetActiveURL() override
{ return url_
; }
198 void SetActiveFaviconImage(gfx::Image image
) { image_
= image
; }
200 void SetActiveFaviconURL(GURL favicon_url
) { favicon_url_
= favicon_url
; }
202 void SetActiveFaviconValidity(bool favicon_validity
) {
203 favicon_validity_
= favicon_validity
;
206 int StartDownload(const GURL
& url
, int max_bitmap_size
) override
{
207 ADD_FAILURE() << "TestFaviconDriver::StartDownload() "
208 << "should never be called in tests.";
212 void OnFaviconAvailable(const gfx::Image
& image
,
213 const GURL
& icon_url
,
214 bool update_active_favicon
) override
{
215 ++num_favicon_available_
;
216 available_image_
= image
;
217 available_icon_url_
= icon_url
;
218 update_active_favicon_
= update_active_favicon
;
219 if (!update_active_favicon
)
222 ++num_active_favicon_
;
223 SetActiveFaviconURL(icon_url
);
224 SetActiveFaviconValidity(true);
225 SetActiveFaviconImage(image
);
228 size_t num_active_favicon() const { return num_active_favicon_
; }
229 size_t num_favicon_available() const { return num_favicon_available_
; }
230 void ResetNumActiveFavicon() { num_active_favicon_
= 0; }
231 void ResetNumFaviconAvailable() { num_favicon_available_
= 0; }
233 void SetActiveURL(GURL url
) { url_
= url
; }
235 const gfx::Image
available_favicon() { return available_image_
; }
237 const GURL
available_icon_url() { return available_icon_url_
; }
239 bool update_active_favicon() { return update_active_favicon_
; }
245 bool favicon_validity_
;
247 // The number of times that NotifyFaviconAvailable() has been called with
248 // |is_active_favicon| is true.
249 size_t num_active_favicon_
;
250 // The number of times that NotifyFaviconAvailable() has been called.
251 size_t num_favicon_available_
;
252 gfx::Image available_image_
;
253 GURL available_icon_url_
;
254 bool update_active_favicon_
;
256 DISALLOW_COPY_AND_ASSIGN(TestFaviconDriver
);
259 // This class is used to catch the FaviconHandler's download and history
260 // request, and also provide the methods to access the FaviconHandler
262 class TestFaviconHandler
: public favicon::FaviconHandler
{
264 static int GetMaximalIconSize(favicon_base::IconType icon_type
) {
265 return FaviconHandler::GetMaximalIconSize(icon_type
);
268 TestFaviconHandler(const GURL
& page_url
,
269 TestFaviconDriver
* driver
,
271 bool download_largest_icon
)
272 : favicon::FaviconHandler(nullptr,
275 download_largest_icon
),
277 driver
->SetActiveURL(page_url
);
278 download_handler_
.reset(new DownloadHandler(this));
281 ~TestFaviconHandler() override
{}
283 HistoryRequestHandler
* history_handler() {
284 return history_handler_
.get();
287 // This method will take the ownership of the given handler.
288 void set_history_handler(HistoryRequestHandler
* handler
) {
289 history_handler_
.reset(handler
);
292 DownloadHandler
* download_handler() {
293 return download_handler_
.get();
296 // Methods to access favicon internals.
297 const std::vector
<FaviconURL
>& urls() {
301 FaviconURL
* current_candidate() {
302 return favicon::FaviconHandler::current_candidate();
305 const FaviconCandidate
& best_favicon_candidate() {
306 return best_favicon_candidate_
;
310 void UpdateFaviconMappingAndFetch(
311 const GURL
& page_url
,
312 const GURL
& icon_url
,
313 favicon_base::IconType icon_type
,
314 const favicon_base::FaviconResultsCallback
& callback
,
315 base::CancelableTaskTracker
* tracker
) override
{
316 history_handler_
.reset(new HistoryRequestHandler(page_url
, icon_url
,
317 icon_type
, callback
));
320 void GetFaviconFromFaviconService(
321 const GURL
& icon_url
,
322 favicon_base::IconType icon_type
,
323 const favicon_base::FaviconResultsCallback
& callback
,
324 base::CancelableTaskTracker
* tracker
) override
{
325 history_handler_
.reset(new HistoryRequestHandler(GURL(), icon_url
,
326 icon_type
, callback
));
329 void GetFaviconForURLFromFaviconService(
330 const GURL
& page_url
,
332 const favicon_base::FaviconResultsCallback
& callback
,
333 base::CancelableTaskTracker
* tracker
) override
{
334 history_handler_
.reset(new HistoryRequestHandler(page_url
, GURL(),
335 icon_types
, callback
));
338 int DownloadFavicon(const GURL
& image_url
, int max_bitmap_size
) override
{
340 std::vector
<int> sizes
;
342 download_handler_
->AddDownload(
343 download_id_
, image_url
, sizes
, max_bitmap_size
);
347 void SetHistoryFavicons(const GURL
& page_url
,
348 const GURL
& icon_url
,
349 favicon_base::IconType icon_type
,
350 const gfx::Image
& image
) override
{
351 scoped_refptr
<base::RefCountedMemory
> bytes
= image
.As1xPNGBytes();
352 std::vector
<unsigned char> bitmap_data(bytes
->front(),
353 bytes
->front() + bytes
->size());
354 history_handler_
.reset(new HistoryRequestHandler(
355 page_url
, icon_url
, icon_type
, bitmap_data
, image
.Size()));
358 bool ShouldSaveFavicon(const GURL
& url
) override
{ return true; }
364 // The unique id of a download request. It will be returned to a
368 scoped_ptr
<DownloadHandler
> download_handler_
;
369 scoped_ptr
<HistoryRequestHandler
> history_handler_
;
371 DISALLOW_COPY_AND_ASSIGN(TestFaviconHandler
);
376 void HistoryRequestHandler::InvokeCallback() {
377 if (!callback_
.is_null()) {
378 callback_
.Run(history_results_
);
382 void DownloadHandler::InvokeCallback() {
383 std::vector
<gfx::Size
> original_bitmap_sizes
;
384 std::vector
<SkBitmap
> bitmaps
;
386 for (std::vector
<int>::const_iterator i
= download_
->image_sizes
.begin();
387 i
!= download_
->image_sizes
.end(); ++i
) {
388 int original_size
= (*i
> 0) ? *i
: gfx::kFaviconSize
;
389 int downloaded_size
= original_size
;
390 if (download_
->max_image_size
!= 0 &&
391 downloaded_size
> download_
->max_image_size
) {
392 downloaded_size
= download_
->max_image_size
;
395 FillDataToBitmap(downloaded_size
, downloaded_size
, &bitmap
);
396 bitmaps
.push_back(bitmap
);
397 original_bitmap_sizes
.push_back(gfx::Size(original_size
, original_size
));
400 favicon_helper_
->OnDidDownloadFavicon(download_
->download_id
,
401 download_
->image_url
,
403 original_bitmap_sizes
);
406 class FaviconHandlerTest
: public ChromeRenderViewHostTestHarness
{
408 FaviconHandlerTest() {
411 ~FaviconHandlerTest() override
{}
413 // Simulates requesting a favicon for |page_url| given:
414 // - We have not previously cached anything in history for |page_url| or for
415 // any of |candidates|.
416 // - The page provides favicons at |candidate_icons|.
417 // - The favicons at |candidate_icons| have edge pixel sizes of
418 // |candidate_icon_sizes|.
419 void DownloadTillDoneIgnoringHistory(
420 TestFaviconDriver
* favicon_driver
,
421 TestFaviconHandler
* favicon_handler
,
422 const GURL
& page_url
,
423 const std::vector
<FaviconURL
>& candidate_icons
,
424 const int* candidate_icon_sizes
) {
426 favicon_driver
, favicon_handler
, page_url
, candidate_icons
);
427 EXPECT_EQ(candidate_icons
.size(), favicon_handler
->image_urls().size());
429 DownloadHandler
* download_handler
= favicon_handler
->download_handler();
430 for (size_t i
= 0; i
< candidate_icons
.size(); ++i
) {
431 favicon_handler
->history_handler()->history_results_
.clear();
432 favicon_handler
->history_handler()->InvokeCallback();
433 ASSERT_TRUE(download_handler
->HasDownload());
434 EXPECT_EQ(download_handler
->GetImageUrl(),
435 candidate_icons
[i
].icon_url
);
436 std::vector
<int> sizes
;
437 sizes
.push_back(candidate_icon_sizes
[i
]);
438 download_handler
->SetImageSizes(sizes
);
439 download_handler
->InvokeCallback();
441 if (favicon_driver
->num_active_favicon())
446 void UpdateFaviconURL(TestFaviconDriver
* favicon_driver
,
447 TestFaviconHandler
* favicon_handler
,
448 const GURL
& page_url
,
449 const std::vector
<FaviconURL
>& candidate_icons
) {
450 favicon_driver
->ResetNumActiveFavicon();
452 favicon_handler
->FetchFavicon(page_url
);
453 favicon_handler
->history_handler()->InvokeCallback();
455 favicon_handler
->OnUpdateFaviconURL(candidate_icons
);
458 void SetUp() override
{
459 // The score computed by SelectFaviconFrames() is dependent on the supported
460 // scale factors of the platform. It is used for determining the goodness of
461 // a downloaded bitmap in favicon::FaviconHandler::OnDidDownloadFavicon().
462 // Force the values of the scale factors so that the tests produce the same
463 // results on all platforms.
464 std::vector
<ui::ScaleFactor
> scale_factors
;
465 scale_factors
.push_back(ui::SCALE_FACTOR_100P
);
466 scoped_set_supported_scale_factors_
.reset(
467 new ui::test::ScopedSetSupportedScaleFactors(scale_factors
));
469 ChromeRenderViewHostTestHarness::SetUp();
472 void TearDown() override
{
473 Profile
* profile
= Profile::FromBrowserContext(
474 web_contents()->GetBrowserContext());
475 FaviconServiceFactory::GetInstance()->SetTestingFactory(profile
, nullptr);
476 ChromeRenderViewHostTestHarness::TearDown();
480 typedef scoped_ptr
<ui::test::ScopedSetSupportedScaleFactors
>
481 ScopedSetSupportedScaleFactors
;
482 ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_
;
483 DISALLOW_COPY_AND_ASSIGN(FaviconHandlerTest
);
486 TEST_F(FaviconHandlerTest
, GetFaviconFromHistory
) {
487 const GURL
page_url("http://www.google.com");
488 const GURL
icon_url("http://www.google.com/favicon");
490 TestFaviconDriver driver
;
491 TestFaviconHandler
helper(page_url
, &driver
, favicon::FaviconHandler::FAVICON
,
494 helper
.FetchFavicon(page_url
);
495 HistoryRequestHandler
* history_handler
= helper
.history_handler();
496 // Ensure the data given to history is correct.
497 ASSERT_TRUE(history_handler
);
498 EXPECT_EQ(page_url
, history_handler
->page_url_
);
499 EXPECT_EQ(GURL(), history_handler
->icon_url_
);
500 EXPECT_EQ(favicon_base::FAVICON
, history_handler
->icon_type_
);
502 SetFaviconRawBitmapResult(icon_url
, &history_handler
->history_results_
);
504 // Send history response.
505 history_handler
->InvokeCallback();
506 // Verify FaviconHandler status
507 EXPECT_TRUE(driver
.GetActiveFaviconValidity());
508 EXPECT_EQ(icon_url
, driver
.GetActiveFaviconURL());
510 // Simulates update favicon url.
511 std::vector
<FaviconURL
> urls
;
513 FaviconURL(icon_url
, favicon_base::FAVICON
, std::vector
<gfx::Size
>()));
514 helper
.OnUpdateFaviconURL(urls
);
516 // Verify FaviconHandler status
517 EXPECT_EQ(1U, helper
.urls().size());
518 ASSERT_TRUE(helper
.current_candidate());
519 ASSERT_EQ(icon_url
, helper
.current_candidate()->icon_url
);
520 ASSERT_EQ(favicon_base::FAVICON
, helper
.current_candidate()->icon_type
);
522 // Favicon shouldn't request to download icon.
523 EXPECT_FALSE(helper
.download_handler()->HasDownload());
526 TEST_F(FaviconHandlerTest
, DownloadFavicon
) {
527 const GURL
page_url("http://www.google.com");
528 const GURL
icon_url("http://www.google.com/favicon");
530 TestFaviconDriver driver
;
531 TestFaviconHandler
helper(page_url
, &driver
, favicon::FaviconHandler::FAVICON
,
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 // Set icon data expired
543 SetFaviconRawBitmapResult(icon_url
,
544 favicon_base::FAVICON
,
546 &history_handler
->history_results_
);
547 // Send history response.
548 history_handler
->InvokeCallback();
549 // Verify FaviconHandler status
550 EXPECT_TRUE(driver
.GetActiveFaviconValidity());
551 EXPECT_EQ(icon_url
, driver
.GetActiveFaviconURL());
553 // Simulates update favicon url.
554 std::vector
<FaviconURL
> urls
;
556 FaviconURL(icon_url
, favicon_base::FAVICON
, std::vector
<gfx::Size
>()));
557 helper
.OnUpdateFaviconURL(urls
);
559 // Verify FaviconHandler status
560 EXPECT_EQ(1U, helper
.urls().size());
561 ASSERT_TRUE(helper
.current_candidate());
562 ASSERT_EQ(icon_url
, helper
.current_candidate()->icon_url
);
563 ASSERT_EQ(favicon_base::FAVICON
, helper
.current_candidate()->icon_type
);
565 // Favicon should request to download icon now.
566 DownloadHandler
* download_handler
= helper
.download_handler();
567 EXPECT_TRUE(helper
.download_handler()->HasDownload());
569 // Verify the download request.
570 EXPECT_EQ(icon_url
, download_handler
->GetImageUrl());
572 // Reset the history_handler to verify whether favicon is set.
573 helper
.set_history_handler(nullptr);
575 // Smulates download done.
576 download_handler
->InvokeCallback();
578 // New icon should be saved to history backend and navigation entry.
579 history_handler
= helper
.history_handler();
580 ASSERT_TRUE(history_handler
);
581 EXPECT_EQ(icon_url
, history_handler
->icon_url_
);
582 EXPECT_EQ(favicon_base::FAVICON
, history_handler
->icon_type_
);
583 EXPECT_LT(0U, history_handler
->bitmap_data_
.size());
584 EXPECT_EQ(page_url
, history_handler
->page_url_
);
586 // Verify NavigationEntry.
587 EXPECT_EQ(icon_url
, driver
.GetActiveFaviconURL());
588 EXPECT_TRUE(driver
.GetActiveFaviconValidity());
589 EXPECT_FALSE(driver
.GetActiveFaviconImage().IsEmpty());
590 EXPECT_EQ(gfx::kFaviconSize
, driver
.GetActiveFaviconImage().Width());
593 TEST_F(FaviconHandlerTest
, UpdateAndDownloadFavicon
) {
594 const GURL
page_url("http://www.google.com");
595 const GURL
icon_url("http://www.google.com/favicon");
596 const GURL
new_icon_url("http://www.google.com/new_favicon");
598 TestFaviconDriver driver
;
599 TestFaviconHandler
helper(page_url
, &driver
, favicon::FaviconHandler::FAVICON
,
602 helper
.FetchFavicon(page_url
);
603 HistoryRequestHandler
* history_handler
= helper
.history_handler();
604 // Ensure the data given to history is correct.
605 ASSERT_TRUE(history_handler
);
606 EXPECT_EQ(page_url
, history_handler
->page_url_
);
607 EXPECT_EQ(GURL(), history_handler
->icon_url_
);
608 EXPECT_EQ(favicon_base::FAVICON
, history_handler
->icon_type_
);
610 // Set valid icon data.
611 SetFaviconRawBitmapResult(icon_url
, &history_handler
->history_results_
);
613 // Send history response.
614 history_handler
->InvokeCallback();
615 // Verify FaviconHandler status.
616 EXPECT_TRUE(driver
.GetActiveFaviconValidity());
617 EXPECT_EQ(icon_url
, driver
.GetActiveFaviconURL());
619 // Reset the history_handler to verify whether new icon is requested from
621 helper
.set_history_handler(nullptr);
623 // Simulates update with the different favicon url.
624 std::vector
<FaviconURL
> urls
;
625 urls
.push_back(FaviconURL(
626 new_icon_url
, favicon_base::FAVICON
, std::vector
<gfx::Size
>()));
627 helper
.OnUpdateFaviconURL(urls
);
629 // Verify FaviconHandler status.
630 EXPECT_EQ(1U, helper
.urls().size());
631 ASSERT_TRUE(helper
.current_candidate());
632 ASSERT_EQ(new_icon_url
, helper
.current_candidate()->icon_url
);
633 ASSERT_EQ(favicon_base::FAVICON
, helper
.current_candidate()->icon_type
);
635 // Favicon should be requested from history.
636 history_handler
= helper
.history_handler();
637 ASSERT_TRUE(history_handler
);
638 EXPECT_EQ(new_icon_url
, history_handler
->icon_url_
);
639 EXPECT_EQ(favicon_base::FAVICON
, history_handler
->icon_type_
);
640 EXPECT_EQ(page_url
, history_handler
->page_url_
);
642 // Simulate not find icon.
643 history_handler
->history_results_
.clear();
644 history_handler
->InvokeCallback();
646 // Favicon should request to download icon now.
647 DownloadHandler
* download_handler
= helper
.download_handler();
648 EXPECT_TRUE(helper
.download_handler()->HasDownload());
650 // Verify the download request.
651 EXPECT_EQ(new_icon_url
, download_handler
->GetImageUrl());
653 // Reset the history_handler to verify whether favicon is set.
654 helper
.set_history_handler(nullptr);
656 // Smulates download done.
657 download_handler
->InvokeCallback();
659 // New icon should be saved to history backend and navigation entry.
660 history_handler
= helper
.history_handler();
661 ASSERT_TRUE(history_handler
);
662 EXPECT_EQ(new_icon_url
, history_handler
->icon_url_
);
663 EXPECT_EQ(favicon_base::FAVICON
, history_handler
->icon_type_
);
664 EXPECT_LT(0U, history_handler
->bitmap_data_
.size());
665 EXPECT_EQ(page_url
, history_handler
->page_url_
);
667 // Verify NavigationEntry.
668 EXPECT_EQ(new_icon_url
, driver
.GetActiveFaviconURL());
669 EXPECT_TRUE(driver
.GetActiveFaviconValidity());
670 EXPECT_FALSE(driver
.GetActiveFaviconImage().IsEmpty());
671 EXPECT_EQ(gfx::kFaviconSize
, driver
.GetActiveFaviconImage().Width());
674 TEST_F(FaviconHandlerTest
, FaviconInHistoryInvalid
) {
675 const GURL
page_url("http://www.google.com");
676 const GURL
icon_url("http://www.google.com/favicon");
678 TestFaviconDriver driver
;
679 TestFaviconHandler
helper(page_url
, &driver
, favicon::FaviconHandler::FAVICON
,
682 helper
.FetchFavicon(page_url
);
683 HistoryRequestHandler
* history_handler
= helper
.history_handler();
684 // Ensure the data given to history is correct.
685 ASSERT_TRUE(history_handler
);
686 EXPECT_EQ(page_url
, history_handler
->page_url_
);
687 EXPECT_EQ(GURL(), history_handler
->icon_url_
);
688 EXPECT_EQ(favicon_base::FAVICON
, history_handler
->icon_type_
);
690 // Set non empty but invalid data.
691 favicon_base::FaviconRawBitmapResult bitmap_result
;
692 bitmap_result
.expired
= false;
693 // Empty bitmap data is invalid.
694 bitmap_result
.bitmap_data
= new base::RefCountedBytes();
695 bitmap_result
.pixel_size
= gfx::Size(gfx::kFaviconSize
, gfx::kFaviconSize
);
696 bitmap_result
.icon_type
= favicon_base::FAVICON
;
697 bitmap_result
.icon_url
= icon_url
;
698 history_handler
->history_results_
.clear();
699 history_handler
->history_results_
.push_back(bitmap_result
);
701 // Send history response.
702 history_handler
->InvokeCallback();
703 // The NavigationEntry should not be set yet as the history data is invalid.
704 EXPECT_FALSE(driver
.GetActiveFaviconValidity());
705 EXPECT_EQ(GURL(), driver
.GetActiveFaviconURL());
707 // Reset the history_handler to verify whether new icon is requested from
709 helper
.set_history_handler(nullptr);
711 // Simulates update with matching favicon URL.
712 std::vector
<FaviconURL
> urls
;
714 FaviconURL(icon_url
, favicon_base::FAVICON
, std::vector
<gfx::Size
>()));
715 helper
.OnUpdateFaviconURL(urls
);
717 // A download for the favicon should be requested, and we should not do
718 // another history request.
719 DownloadHandler
* download_handler
= helper
.download_handler();
720 EXPECT_TRUE(helper
.download_handler()->HasDownload());
721 EXPECT_EQ(nullptr, helper
.history_handler());
723 // Verify the download request.
724 EXPECT_EQ(icon_url
, download_handler
->GetImageUrl());
726 // Simulates download done.
727 download_handler
->InvokeCallback();
729 // New icon should be saved to history backend and navigation entry.
730 history_handler
= helper
.history_handler();
731 ASSERT_TRUE(history_handler
);
732 EXPECT_EQ(icon_url
, history_handler
->icon_url_
);
733 EXPECT_EQ(favicon_base::FAVICON
, history_handler
->icon_type_
);
734 EXPECT_LT(0U, history_handler
->bitmap_data_
.size());
735 EXPECT_EQ(page_url
, history_handler
->page_url_
);
737 // Verify NavigationEntry.
738 EXPECT_EQ(icon_url
, driver
.GetActiveFaviconURL());
739 EXPECT_TRUE(driver
.GetActiveFaviconValidity());
740 EXPECT_FALSE(driver
.GetActiveFaviconImage().IsEmpty());
741 EXPECT_EQ(gfx::kFaviconSize
, driver
.GetActiveFaviconImage().Width());
744 TEST_F(FaviconHandlerTest
, UpdateFavicon
) {
745 const GURL
page_url("http://www.google.com");
746 const GURL
icon_url("http://www.google.com/favicon");
747 const GURL
new_icon_url("http://www.google.com/new_favicon");
749 TestFaviconDriver driver
;
750 TestFaviconHandler
helper(page_url
, &driver
, favicon::FaviconHandler::FAVICON
,
753 helper
.FetchFavicon(page_url
);
754 HistoryRequestHandler
* history_handler
= helper
.history_handler();
755 // Ensure the data given to history is correct.
756 ASSERT_TRUE(history_handler
);
757 EXPECT_EQ(page_url
, history_handler
->page_url_
);
758 EXPECT_EQ(GURL(), history_handler
->icon_url_
);
759 EXPECT_EQ(favicon_base::FAVICON
, history_handler
->icon_type_
);
761 SetFaviconRawBitmapResult(icon_url
, &history_handler
->history_results_
);
763 // Send history response.
764 history_handler
->InvokeCallback();
765 // Verify FaviconHandler status.
766 EXPECT_TRUE(driver
.GetActiveFaviconValidity());
767 EXPECT_EQ(icon_url
, driver
.GetActiveFaviconURL());
769 // Reset the history_handler to verify whether new icon is requested from
771 helper
.set_history_handler(nullptr);
773 // Simulates update with the different favicon url.
774 std::vector
<FaviconURL
> urls
;
775 urls
.push_back(FaviconURL(
776 new_icon_url
, favicon_base::FAVICON
, std::vector
<gfx::Size
>()));
777 helper
.OnUpdateFaviconURL(urls
);
779 // Verify FaviconHandler status.
780 EXPECT_EQ(1U, helper
.urls().size());
781 ASSERT_TRUE(helper
.current_candidate());
782 ASSERT_EQ(new_icon_url
, helper
.current_candidate()->icon_url
);
783 ASSERT_EQ(favicon_base::FAVICON
, helper
.current_candidate()->icon_type
);
785 // Favicon should be requested from history.
786 history_handler
= helper
.history_handler();
787 ASSERT_TRUE(history_handler
);
788 EXPECT_EQ(new_icon_url
, history_handler
->icon_url_
);
789 EXPECT_EQ(favicon_base::FAVICON
, history_handler
->icon_type_
);
790 EXPECT_EQ(page_url
, history_handler
->page_url_
);
792 // Simulate find icon.
793 SetFaviconRawBitmapResult(new_icon_url
, &history_handler
->history_results_
);
794 history_handler
->InvokeCallback();
796 // Shouldn't request download favicon
797 EXPECT_FALSE(helper
.download_handler()->HasDownload());
799 // Verify the favicon status.
800 EXPECT_EQ(new_icon_url
, driver
.GetActiveFaviconURL());
801 EXPECT_TRUE(driver
.GetActiveFaviconValidity());
802 EXPECT_FALSE(driver
.GetActiveFaviconImage().IsEmpty());
805 TEST_F(FaviconHandlerTest
, Download2ndFaviconURLCandidate
) {
806 const GURL
page_url("http://www.google.com");
807 const GURL
icon_url("http://www.google.com/favicon");
808 const GURL
new_icon_url("http://www.google.com/new_favicon");
810 TestFaviconDriver driver
;
811 TestFaviconHandler
helper(page_url
, &driver
, favicon::FaviconHandler::TOUCH
,
814 helper
.FetchFavicon(page_url
);
815 HistoryRequestHandler
* history_handler
= helper
.history_handler();
816 // Ensure the data given to history is correct.
817 ASSERT_TRUE(history_handler
);
818 EXPECT_EQ(page_url
, history_handler
->page_url_
);
819 EXPECT_EQ(GURL(), history_handler
->icon_url_
);
820 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON
| favicon_base::TOUCH_ICON
,
821 history_handler
->icon_type_
);
824 history_handler
->history_results_
.clear();
825 // Send history response.
826 history_handler
->InvokeCallback();
827 // Verify FaviconHandler status.
828 EXPECT_FALSE(driver
.GetActiveFaviconValidity());
829 EXPECT_EQ(GURL(), driver
.GetActiveFaviconURL());
831 // Reset the history_handler to verify whether new icon is requested from
833 helper
.set_history_handler(nullptr);
835 // Simulates update with the different favicon url.
836 std::vector
<FaviconURL
> urls
;
837 urls
.push_back(FaviconURL(icon_url
,
838 favicon_base::TOUCH_PRECOMPOSED_ICON
,
839 std::vector
<gfx::Size
>()));
840 urls
.push_back(FaviconURL(
841 new_icon_url
, favicon_base::TOUCH_ICON
, std::vector
<gfx::Size
>()));
842 urls
.push_back(FaviconURL(
843 new_icon_url
, favicon_base::FAVICON
, std::vector
<gfx::Size
>()));
844 helper
.OnUpdateFaviconURL(urls
);
846 // Verify FaviconHandler status.
847 EXPECT_EQ(2U, helper
.urls().size());
848 ASSERT_TRUE(helper
.current_candidate());
849 ASSERT_EQ(icon_url
, helper
.current_candidate()->icon_url
);
850 ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON
,
851 helper
.current_candidate()->icon_type
);
853 // Favicon should be requested from history.
854 history_handler
= helper
.history_handler();
855 ASSERT_TRUE(history_handler
);
856 EXPECT_EQ(icon_url
, history_handler
->icon_url_
);
857 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON
, history_handler
->icon_type_
);
858 EXPECT_EQ(page_url
, history_handler
->page_url_
);
860 // Simulate not find icon.
861 history_handler
->history_results_
.clear();
862 history_handler
->InvokeCallback();
864 // Should request download favicon.
865 DownloadHandler
* download_handler
= helper
.download_handler();
866 EXPECT_TRUE(helper
.download_handler()->HasDownload());
868 // Verify the download request.
869 EXPECT_EQ(icon_url
, download_handler
->GetImageUrl());
871 // Reset the history_handler to verify whether favicon is request from
873 helper
.set_history_handler(nullptr);
874 // Smulates download failed.
875 download_handler
->set_failed(true);
876 download_handler
->InvokeCallback();
879 EXPECT_EQ(1U, helper
.urls().size());
880 ASSERT_TRUE(helper
.current_candidate());
881 EXPECT_EQ(new_icon_url
, helper
.current_candidate()->icon_url
);
882 EXPECT_EQ(favicon_base::TOUCH_ICON
, helper
.current_candidate()->icon_type
);
884 // Favicon should be requested from history.
885 history_handler
= helper
.history_handler();
886 ASSERT_TRUE(history_handler
);
887 EXPECT_EQ(new_icon_url
, history_handler
->icon_url_
);
888 EXPECT_EQ(favicon_base::TOUCH_ICON
, history_handler
->icon_type_
);
889 EXPECT_EQ(page_url
, history_handler
->page_url_
);
891 // Reset download handler
892 download_handler
->Reset();
894 // Simulates getting a expired icon from history.
895 SetFaviconRawBitmapResult(new_icon_url
,
896 favicon_base::TOUCH_ICON
,
898 &history_handler
->history_results_
);
899 history_handler
->InvokeCallback();
901 // Verify the download request.
902 EXPECT_TRUE(helper
.download_handler()->HasDownload());
903 EXPECT_EQ(new_icon_url
, download_handler
->GetImageUrl());
905 helper
.set_history_handler(nullptr);
907 // Simulates icon being downloaded.
908 download_handler
->InvokeCallback();
910 // New icon should be saved to history backend.
911 history_handler
= helper
.history_handler();
912 ASSERT_TRUE(history_handler
);
913 EXPECT_EQ(new_icon_url
, history_handler
->icon_url_
);
914 EXPECT_EQ(favicon_base::TOUCH_ICON
, history_handler
->icon_type_
);
915 EXPECT_LT(0U, history_handler
->bitmap_data_
.size());
916 EXPECT_EQ(page_url
, history_handler
->page_url_
);
919 TEST_F(FaviconHandlerTest
, UpdateDuringDownloading
) {
920 const GURL
page_url("http://www.google.com");
921 const GURL
icon_url("http://www.google.com/favicon");
922 const GURL
new_icon_url("http://www.google.com/new_favicon");
924 TestFaviconDriver driver
;
925 TestFaviconHandler
helper(page_url
, &driver
, favicon::FaviconHandler::TOUCH
,
928 helper
.FetchFavicon(page_url
);
929 HistoryRequestHandler
* history_handler
= helper
.history_handler();
930 // Ensure the data given to history is correct.
931 ASSERT_TRUE(history_handler
);
932 EXPECT_EQ(page_url
, history_handler
->page_url_
);
933 EXPECT_EQ(GURL(), history_handler
->icon_url_
);
934 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON
| favicon_base::TOUCH_ICON
,
935 history_handler
->icon_type_
);
938 history_handler
->history_results_
.clear();
939 // Send history response.
940 history_handler
->InvokeCallback();
941 // Verify FaviconHandler status.
942 EXPECT_FALSE(driver
.GetActiveFaviconValidity());
943 EXPECT_EQ(GURL(), driver
.GetActiveFaviconURL());
945 // Reset the history_handler to verify whether new icon is requested from
947 helper
.set_history_handler(nullptr);
949 // Simulates update with the different favicon url.
950 std::vector
<FaviconURL
> urls
;
951 urls
.push_back(FaviconURL(icon_url
,
952 favicon_base::TOUCH_PRECOMPOSED_ICON
,
953 std::vector
<gfx::Size
>()));
954 urls
.push_back(FaviconURL(
955 new_icon_url
, favicon_base::TOUCH_ICON
, std::vector
<gfx::Size
>()));
956 urls
.push_back(FaviconURL(
957 new_icon_url
, favicon_base::FAVICON
, std::vector
<gfx::Size
>()));
958 helper
.OnUpdateFaviconURL(urls
);
960 // Verify FaviconHandler status.
961 EXPECT_EQ(2U, helper
.urls().size());
962 ASSERT_TRUE(helper
.current_candidate());
963 ASSERT_EQ(icon_url
, helper
.current_candidate()->icon_url
);
964 ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON
,
965 helper
.current_candidate()->icon_type
);
967 // Favicon should be requested from history.
968 history_handler
= helper
.history_handler();
969 ASSERT_TRUE(history_handler
);
970 EXPECT_EQ(icon_url
, history_handler
->icon_url_
);
971 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON
, history_handler
->icon_type_
);
972 EXPECT_EQ(page_url
, history_handler
->page_url_
);
974 // Simulate not find icon.
975 history_handler
->history_results_
.clear();
976 history_handler
->InvokeCallback();
978 // Should request download favicon.
979 DownloadHandler
* download_handler
= helper
.download_handler();
980 EXPECT_TRUE(helper
.download_handler()->HasDownload());
982 // Verify the download request.
983 EXPECT_EQ(icon_url
, download_handler
->GetImageUrl());
985 // Reset the history_handler to verify whether favicon is request from
987 helper
.set_history_handler(nullptr);
988 const GURL
latest_icon_url("http://www.google.com/latest_favicon");
989 std::vector
<FaviconURL
> latest_urls
;
990 latest_urls
.push_back(FaviconURL(
991 latest_icon_url
, favicon_base::TOUCH_ICON
, std::vector
<gfx::Size
>()));
992 helper
.OnUpdateFaviconURL(latest_urls
);
994 EXPECT_EQ(1U, helper
.urls().size());
995 EXPECT_EQ(latest_icon_url
, helper
.current_candidate()->icon_url
);
996 EXPECT_EQ(favicon_base::TOUCH_ICON
, helper
.current_candidate()->icon_type
);
998 // Whether new icon is requested from history
999 history_handler
= helper
.history_handler();
1000 ASSERT_TRUE(history_handler
);
1001 EXPECT_EQ(latest_icon_url
, history_handler
->icon_url_
);
1002 EXPECT_EQ(favicon_base::TOUCH_ICON
, history_handler
->icon_type_
);
1003 EXPECT_EQ(page_url
, history_handler
->page_url_
);
1005 // Reset the history_handler to verify whether favicon is request from
1007 // Save the callback for late use.
1008 favicon_base::FaviconResultsCallback callback
= history_handler
->callback_
;
1009 helper
.set_history_handler(nullptr);
1011 // Simulates download succeed.
1012 download_handler
->InvokeCallback();
1013 // The downloaded icon should be thrown away as there is favicon update.
1014 EXPECT_FALSE(helper
.history_handler());
1016 download_handler
->Reset();
1018 // Simulates getting the icon from history.
1019 scoped_ptr
<HistoryRequestHandler
> handler
;
1020 handler
.reset(new HistoryRequestHandler(
1021 page_url
, latest_icon_url
, favicon_base::TOUCH_ICON
, callback
));
1022 SetFaviconRawBitmapResult(latest_icon_url
,
1023 favicon_base::TOUCH_ICON
,
1024 false /* expired */,
1025 &handler
->history_results_
);
1026 handler
->InvokeCallback();
1028 // No download request.
1029 EXPECT_FALSE(download_handler
->HasDownload());
1032 #if !defined(OS_ANDROID)
1034 // Test the favicon which is selected when the web page provides several
1035 // favicons and none of the favicons are cached in history.
1036 // The goal of this test is to be more of an integration test than
1037 // SelectFaviconFramesTest.*.
1038 TEST_F(FaviconHandlerTest
, MultipleFavicons
) {
1039 const GURL
kPageURL("http://www.google.com");
1040 const FaviconURL kSourceIconURLs
[] = {
1041 FaviconURL(GURL("http://www.google.com/a"),
1042 favicon_base::FAVICON
,
1043 std::vector
<gfx::Size
>()),
1044 FaviconURL(GURL("http://www.google.com/b"),
1045 favicon_base::FAVICON
,
1046 std::vector
<gfx::Size
>()),
1047 FaviconURL(GURL("http://www.google.com/c"),
1048 favicon_base::FAVICON
,
1049 std::vector
<gfx::Size
>()),
1050 FaviconURL(GURL("http://www.google.com/d"),
1051 favicon_base::FAVICON
,
1052 std::vector
<gfx::Size
>()),
1053 FaviconURL(GURL("http://www.google.com/e"),
1054 favicon_base::FAVICON
,
1055 std::vector
<gfx::Size
>())};
1057 // Set the supported scale factors to 1x and 2x. This affects the behavior of
1058 // SelectFaviconFrames().
1059 std::vector
<ui::ScaleFactor
> scale_factors
;
1060 scale_factors
.push_back(ui::SCALE_FACTOR_100P
);
1061 scale_factors
.push_back(ui::SCALE_FACTOR_200P
);
1062 ui::test::ScopedSetSupportedScaleFactors
scoped_supported(scale_factors
);
1064 // 1) Test that if there are several single resolution favicons to choose from
1065 // that the largest exact match is chosen.
1066 TestFaviconDriver driver1
;
1067 TestFaviconHandler
handler1(kPageURL
, &driver1
,
1068 favicon::FaviconHandler::FAVICON
, false);
1070 const int kSizes1
[] = { 16, 24, 32, 48, 256 };
1071 std::vector
<FaviconURL
> urls1(kSourceIconURLs
,
1072 kSourceIconURLs
+ arraysize(kSizes1
));
1073 DownloadTillDoneIgnoringHistory(
1074 &driver1
, &handler1
, kPageURL
, urls1
, kSizes1
);
1076 EXPECT_EQ(0u, handler1
.image_urls().size());
1077 EXPECT_TRUE(driver1
.GetActiveFaviconValidity());
1078 EXPECT_FALSE(driver1
.GetActiveFaviconImage().IsEmpty());
1079 EXPECT_EQ(gfx::kFaviconSize
, driver1
.GetActiveFaviconImage().Width());
1081 size_t expected_index
= 2u;
1082 EXPECT_EQ(32, kSizes1
[expected_index
]);
1083 EXPECT_EQ(kSourceIconURLs
[expected_index
].icon_url
,
1084 driver1
.GetActiveFaviconURL());
1086 // 2) Test that if there are several single resolution favicons to choose
1087 // from, the exact match is preferred even if it results in upsampling.
1088 TestFaviconDriver driver2
;
1089 TestFaviconHandler
handler2(kPageURL
, &driver2
,
1090 favicon::FaviconHandler::FAVICON
, false);
1092 const int kSizes2
[] = { 16, 24, 48, 256 };
1093 std::vector
<FaviconURL
> urls2(kSourceIconURLs
,
1094 kSourceIconURLs
+ arraysize(kSizes2
));
1095 DownloadTillDoneIgnoringHistory(
1096 &driver2
, &handler2
, kPageURL
, urls2
, kSizes2
);
1097 EXPECT_TRUE(driver2
.GetActiveFaviconValidity());
1098 expected_index
= 0u;
1099 EXPECT_EQ(16, kSizes2
[expected_index
]);
1100 EXPECT_EQ(kSourceIconURLs
[expected_index
].icon_url
,
1101 driver2
.GetActiveFaviconURL());
1103 // 3) Test that favicons which need to be upsampled a little or downsampled
1104 // a little are preferred over huge favicons.
1105 TestFaviconDriver driver3
;
1106 TestFaviconHandler
handler3(kPageURL
, &driver3
,
1107 favicon::FaviconHandler::FAVICON
, false);
1109 const int kSizes3
[] = { 256, 48 };
1110 std::vector
<FaviconURL
> urls3(kSourceIconURLs
,
1111 kSourceIconURLs
+ arraysize(kSizes3
));
1112 DownloadTillDoneIgnoringHistory(
1113 &driver3
, &handler3
, kPageURL
, urls3
, kSizes3
);
1114 EXPECT_TRUE(driver3
.GetActiveFaviconValidity());
1115 expected_index
= 1u;
1116 EXPECT_EQ(48, kSizes3
[expected_index
]);
1117 EXPECT_EQ(kSourceIconURLs
[expected_index
].icon_url
,
1118 driver3
.GetActiveFaviconURL());
1120 TestFaviconDriver driver4
;
1121 TestFaviconHandler
handler4(kPageURL
, &driver4
,
1122 favicon::FaviconHandler::FAVICON
, false);
1124 const int kSizes4
[] = { 17, 256 };
1125 std::vector
<FaviconURL
> urls4(kSourceIconURLs
,
1126 kSourceIconURLs
+ arraysize(kSizes4
));
1127 DownloadTillDoneIgnoringHistory(
1128 &driver4
, &handler4
, kPageURL
, urls4
, kSizes4
);
1129 EXPECT_TRUE(driver4
.GetActiveFaviconValidity());
1130 expected_index
= 0u;
1131 EXPECT_EQ(17, kSizes4
[expected_index
]);
1132 EXPECT_EQ(kSourceIconURLs
[expected_index
].icon_url
,
1133 driver4
.GetActiveFaviconURL());
1138 TEST_F(FaviconHandlerTest
, TestSortFavicon
) {
1139 const GURL
kPageURL("http://www.google.com");
1140 std::vector
<gfx::Size
> icon1
;
1141 icon1
.push_back(gfx::Size(1024, 1024));
1142 icon1
.push_back(gfx::Size(512, 512));
1144 std::vector
<gfx::Size
> icon2
;
1145 icon2
.push_back(gfx::Size(15, 15));
1146 icon2
.push_back(gfx::Size(16, 16));
1148 std::vector
<gfx::Size
> icon3
;
1149 icon3
.push_back(gfx::Size(16, 16));
1150 icon3
.push_back(gfx::Size(14, 14));
1152 const FaviconURL kSourceIconURLs
[] = {
1153 FaviconURL(GURL("http://www.google.com/a"), favicon_base::FAVICON
, icon1
),
1154 FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON
, icon2
),
1155 FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON
, icon3
),
1156 FaviconURL(GURL("http://www.google.com/d"),
1157 favicon_base::FAVICON
,
1158 std::vector
<gfx::Size
>()),
1159 FaviconURL(GURL("http://www.google.com/e"),
1160 favicon_base::FAVICON
,
1161 std::vector
<gfx::Size
>())};
1163 TestFaviconDriver driver1
;
1164 TestFaviconHandler
handler1(kPageURL
, &driver1
,
1165 favicon::FaviconHandler::FAVICON
, true);
1166 std::vector
<FaviconURL
> urls1(kSourceIconURLs
,
1167 kSourceIconURLs
+ arraysize(kSourceIconURLs
));
1168 UpdateFaviconURL(&driver1
, &handler1
, kPageURL
, urls1
);
1170 struct ExpectedResult
{
1171 // The favicon's index in kSourceIconURLs.
1172 size_t favicon_index
;
1173 // Width of largest bitmap.
1176 // First is icon1, though its size larger than maximal.
1179 // The 16x16 is largest.
1181 // Third is icon3 though it has same size as icon2.
1182 // The 16x16 is largest.
1184 // The rest of bitmaps come in order, there is no sizes attribute.
1188 const std::vector
<FaviconURL
>& icons
= handler1
.image_urls();
1189 ASSERT_EQ(5u, icons
.size());
1190 for (size_t i
= 0; i
< icons
.size(); ++i
) {
1191 EXPECT_EQ(kSourceIconURLs
[results
[i
].favicon_index
].icon_url
,
1193 if (results
[i
].width
!= -1)
1194 EXPECT_EQ(results
[i
].width
, icons
[i
].icon_sizes
[0].width());
1198 TEST_F(FaviconHandlerTest
, TestDownloadLargestFavicon
) {
1199 const GURL
kPageURL("http://www.google.com");
1200 std::vector
<gfx::Size
> icon1
;
1201 icon1
.push_back(gfx::Size(1024, 1024));
1202 icon1
.push_back(gfx::Size(512, 512));
1204 std::vector
<gfx::Size
> icon2
;
1205 icon2
.push_back(gfx::Size(15, 15));
1206 icon2
.push_back(gfx::Size(14, 14));
1208 std::vector
<gfx::Size
> icon3
;
1209 icon3
.push_back(gfx::Size(16, 16));
1210 icon3
.push_back(gfx::Size(512, 512));
1212 const FaviconURL kSourceIconURLs
[] = {
1214 GURL("http://www.google.com/a"), favicon_base::FAVICON
, icon1
),
1216 GURL("http://www.google.com/b"), favicon_base::FAVICON
, icon2
),
1218 GURL("http://www.google.com/c"), favicon_base::FAVICON
, icon3
),
1219 FaviconURL(GURL("http://www.google.com/d"),
1220 favicon_base::FAVICON
,
1221 std::vector
<gfx::Size
>()),
1222 FaviconURL(GURL("http://www.google.com/e"),
1223 favicon_base::FAVICON
,
1224 std::vector
<gfx::Size
>())};
1226 TestFaviconDriver driver1
;
1227 TestFaviconHandler
handler1(kPageURL
, &driver1
,
1228 favicon::FaviconHandler::FAVICON
, true);
1229 std::vector
<FaviconURL
> urls1(kSourceIconURLs
,
1230 kSourceIconURLs
+ arraysize(kSourceIconURLs
));
1231 UpdateFaviconURL(&driver1
, &handler1
, kPageURL
, urls1
);
1233 // Simulate the download failed, to check whether the icons were requested
1234 // to download according their size.
1235 struct ExpectedResult
{
1236 // The size of image_urls_.
1237 size_t image_urls_size
;
1238 // The favicon's index in kSourceIconURLs.
1239 size_t favicon_index
;
1240 // Width of largest bitmap.
1246 // The rest of bitmaps come in order.
1251 for (int i
= 0; i
< 5; ++i
) {
1252 ASSERT_EQ(results
[i
].image_urls_size
, handler1
.image_urls().size());
1253 EXPECT_EQ(kSourceIconURLs
[results
[i
].favicon_index
].icon_url
,
1254 handler1
.current_candidate()->icon_url
);
1255 if (results
[i
].width
!= -1) {
1256 EXPECT_EQ(results
[i
].width
, handler1
.current_candidate()->
1257 icon_sizes
[0].width());
1260 // Simulate no favicon from history.
1261 handler1
.history_handler()->history_results_
.clear();
1262 handler1
.history_handler()->InvokeCallback();
1264 // Verify download request
1265 ASSERT_TRUE(handler1
.download_handler()->HasDownload());
1266 EXPECT_EQ(kSourceIconURLs
[results
[i
].favicon_index
].icon_url
,
1267 handler1
.download_handler()->GetImageUrl());
1269 // Simulate the download failed.
1270 handler1
.download_handler()->set_failed(true);
1271 handler1
.download_handler()->InvokeCallback();
1275 TEST_F(FaviconHandlerTest
, TestSelectLargestFavicon
) {
1276 const GURL
kPageURL("http://www.google.com");
1278 std::vector
<gfx::Size
> one_icon
;
1279 one_icon
.push_back(gfx::Size(15, 15));
1281 std::vector
<gfx::Size
> two_icons
;
1282 two_icons
.push_back(gfx::Size(14, 14));
1283 two_icons
.push_back(gfx::Size(16, 16));
1285 const FaviconURL kSourceIconURLs
[] = {
1287 GURL("http://www.google.com/b"), favicon_base::FAVICON
, one_icon
),
1289 GURL("http://www.google.com/c"), favicon_base::FAVICON
, two_icons
)};
1291 TestFaviconDriver driver1
;
1292 TestFaviconHandler
handler1(kPageURL
, &driver1
,
1293 favicon::FaviconHandler::FAVICON
, true);
1294 std::vector
<FaviconURL
> urls1(kSourceIconURLs
,
1295 kSourceIconURLs
+ arraysize(kSourceIconURLs
));
1296 UpdateFaviconURL(&driver1
, &handler1
, kPageURL
, urls1
);
1298 ASSERT_EQ(2u, handler1
.urls().size());
1300 // Index of largest favicon in kSourceIconURLs.
1302 // The largest bitmap's index in Favicon .
1305 // Verify the icon_bitmaps_ was initialized correctly.
1306 EXPECT_EQ(kSourceIconURLs
[i
].icon_url
,
1307 handler1
.current_candidate()->icon_url
);
1308 EXPECT_EQ(kSourceIconURLs
[i
].icon_sizes
[b
],
1309 handler1
.current_candidate()->icon_sizes
[0]);
1311 // Simulate no favicon from history.
1312 handler1
.history_handler()->history_results_
.clear();
1313 handler1
.history_handler()->InvokeCallback();
1315 // Verify download request
1316 ASSERT_TRUE(handler1
.download_handler()->HasDownload());
1317 EXPECT_EQ(kSourceIconURLs
[i
].icon_url
,
1318 handler1
.download_handler()->GetImageUrl());
1320 // Give the correct download result.
1321 std::vector
<int> sizes
;
1322 for (std::vector
<gfx::Size
>::const_iterator j
=
1323 kSourceIconURLs
[i
].icon_sizes
.begin();
1324 j
!= kSourceIconURLs
[i
].icon_sizes
.end(); ++j
)
1325 sizes
.push_back(j
->width());
1327 handler1
.download_handler()->SetImageSizes(sizes
);
1328 handler1
.download_handler()->InvokeCallback();
1330 // Verify the largest bitmap has been saved into history.
1331 EXPECT_EQ(kSourceIconURLs
[i
].icon_url
, handler1
.history_handler()->icon_url_
);
1332 EXPECT_EQ(kSourceIconURLs
[i
].icon_sizes
[b
],
1333 handler1
.history_handler()->size_
);
1334 // Verify NotifyFaviconAvailable().
1335 EXPECT_FALSE(driver1
.update_active_favicon());
1336 EXPECT_EQ(kSourceIconURLs
[i
].icon_url
, driver1
.available_icon_url());
1337 EXPECT_EQ(kSourceIconURLs
[i
].icon_sizes
[b
],
1338 driver1
.available_favicon().Size());
1341 TEST_F(FaviconHandlerTest
, TestFaviconWasScaledAfterDownload
) {
1342 const GURL
kPageURL("http://www.google.com");
1343 const int kMaximalSize
=
1344 TestFaviconHandler::GetMaximalIconSize(favicon_base::FAVICON
);
1346 std::vector
<gfx::Size
> icon1
;
1347 icon1
.push_back(gfx::Size(kMaximalSize
+ 1, kMaximalSize
+ 1));
1349 std::vector
<gfx::Size
> icon2
;
1350 icon2
.push_back(gfx::Size(kMaximalSize
+ 2, kMaximalSize
+ 2));
1352 const FaviconURL kSourceIconURLs
[] = {
1354 GURL("http://www.google.com/b"), favicon_base::FAVICON
, icon1
),
1356 GURL("http://www.google.com/c"), favicon_base::FAVICON
, icon2
)};
1358 TestFaviconDriver driver1
;
1359 TestFaviconHandler
handler1(kPageURL
, &driver1
,
1360 favicon::FaviconHandler::FAVICON
, true);
1361 std::vector
<FaviconURL
> urls1(kSourceIconURLs
,
1362 kSourceIconURLs
+ arraysize(kSourceIconURLs
));
1363 UpdateFaviconURL(&driver1
, &handler1
, kPageURL
, urls1
);
1365 ASSERT_EQ(2u, handler1
.urls().size());
1367 // Index of largest favicon in kSourceIconURLs.
1369 // The largest bitmap's index in Favicon .
1372 // Verify the icon_bitmaps_ was initialized correctly.
1373 EXPECT_EQ(kSourceIconURLs
[i
].icon_url
,
1374 handler1
.current_candidate()->icon_url
);
1375 EXPECT_EQ(kSourceIconURLs
[i
].icon_sizes
[b
],
1376 handler1
.current_candidate()->icon_sizes
[0]);
1378 // Simulate no favicon from history.
1379 handler1
.history_handler()->history_results_
.clear();
1380 handler1
.history_handler()->InvokeCallback();
1382 // Verify download request
1383 ASSERT_TRUE(handler1
.download_handler()->HasDownload());
1384 EXPECT_EQ(kSourceIconURLs
[i
].icon_url
,
1385 handler1
.download_handler()->GetImageUrl());
1387 // Give the scaled download bitmap.
1388 std::vector
<int> sizes
;
1389 sizes
.push_back(kMaximalSize
);
1391 handler1
.download_handler()->SetImageSizes(sizes
);
1392 handler1
.download_handler()->InvokeCallback();
1394 // Verify the largest bitmap has been saved into history though it was
1395 // scaled down to maximal size and smaller than icon1 now.
1396 EXPECT_EQ(kSourceIconURLs
[i
].icon_url
, handler1
.history_handler()->icon_url_
);
1397 EXPECT_EQ(gfx::Size(kMaximalSize
, kMaximalSize
),
1398 handler1
.history_handler()->size_
);
1401 TEST_F(FaviconHandlerTest
, TestKeepDownloadedLargestFavicon
) {
1402 const GURL
kPageURL("http://www.google.com");
1404 std::vector
<gfx::Size
> icon1
;
1405 icon1
.push_back(gfx::Size(16, 16));
1406 const int actual_size1
= 10;
1408 std::vector
<gfx::Size
> icon2
;
1409 icon2
.push_back(gfx::Size(15, 15));
1410 const int actual_size2
= 12;
1412 const FaviconURL kSourceIconURLs
[] = {
1413 FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON
, icon1
),
1414 FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON
, icon2
),
1415 FaviconURL(GURL("http://www.google.com/d"),
1416 favicon_base::FAVICON
,
1417 std::vector
<gfx::Size
>())};
1419 TestFaviconDriver driver1
;
1420 TestFaviconHandler
handler1(kPageURL
, &driver1
,
1421 favicon::FaviconHandler::FAVICON
, true);
1422 std::vector
<FaviconURL
> urls1(kSourceIconURLs
,
1423 kSourceIconURLs
+ arraysize(kSourceIconURLs
));
1424 UpdateFaviconURL(&driver1
, &handler1
, kPageURL
, urls1
);
1425 ASSERT_EQ(3u, handler1
.urls().size());
1427 // Simulate no favicon from history.
1428 handler1
.history_handler()->history_results_
.clear();
1429 handler1
.history_handler()->InvokeCallback();
1431 // Verify the first icon was request to download
1432 ASSERT_TRUE(handler1
.download_handler()->HasDownload());
1433 EXPECT_EQ(kSourceIconURLs
[0].icon_url
,
1434 handler1
.download_handler()->GetImageUrl());
1436 // Give the incorrect size.
1437 std::vector
<int> sizes
;
1438 sizes
.push_back(actual_size1
);
1439 handler1
.download_handler()->SetImageSizes(sizes
);
1440 handler1
.download_handler()->InvokeCallback();
1442 // Simulate no favicon from history.
1443 handler1
.history_handler()->history_results_
.clear();
1444 handler1
.history_handler()->InvokeCallback();
1446 // Verify the 2nd icon was request to download
1447 ASSERT_TRUE(handler1
.download_handler()->HasDownload());
1448 EXPECT_EQ(kSourceIconURLs
[1].icon_url
,
1449 handler1
.download_handler()->GetImageUrl());
1451 // Very the best candidate is icon1
1452 EXPECT_EQ(kSourceIconURLs
[0].icon_url
,
1453 handler1
.best_favicon_candidate().image_url
);
1454 EXPECT_EQ(gfx::Size(actual_size1
, actual_size1
),
1455 handler1
.best_favicon_candidate().image
.Size());
1457 // Give the incorrect size.
1459 sizes
.push_back(actual_size2
);
1460 handler1
.download_handler()->SetImageSizes(sizes
);
1461 handler1
.download_handler()->InvokeCallback();
1463 // Verify icon2 has been saved into history.
1464 EXPECT_EQ(kSourceIconURLs
[1].icon_url
, handler1
.history_handler()->icon_url_
);
1465 EXPECT_EQ(gfx::Size(actual_size2
, actual_size2
),
1466 handler1
.history_handler()->size_
);
1469 static KeyedService
* BuildFaviconService(content::BrowserContext
* context
) {
1470 Profile
* profile
= Profile::FromBrowserContext(context
);
1471 return new favicon::FaviconService(
1472 ChromeFaviconClientFactory::GetForProfile(profile
),
1473 HistoryServiceFactory::GetForProfile(profile
,
1474 ServiceAccessType::EXPLICIT_ACCESS
));
1477 static KeyedService
* BuildHistoryService(content::BrowserContext
* context
) {
1481 // Test that Favicon is not requested repeatedly during the same session if
1482 // server returns HTTP 404 status.
1483 TEST_F(FaviconHandlerTest
, UnableToDownloadFavicon
) {
1484 const GURL
missing_icon_url("http://www.google.com/favicon.ico");
1485 const GURL
another_icon_url("http://www.youtube.com/favicon.ico");
1487 Profile
* profile
= Profile::FromBrowserContext(
1488 web_contents()->GetBrowserContext());
1490 FaviconServiceFactory::GetInstance()->SetTestingFactory(
1491 profile
, BuildFaviconService
);
1493 HistoryServiceFactory::GetInstance()->SetTestingFactory(
1494 profile
, BuildHistoryService
);
1496 favicon::FaviconService
* favicon_service
=
1497 FaviconServiceFactory::GetForProfile(profile
,
1498 ServiceAccessType::IMPLICIT_ACCESS
);
1500 FaviconTabHelper::CreateForWebContents(web_contents());
1501 FaviconTabHelper
* favicon_tab_helper
=
1502 FaviconTabHelper::FromWebContents(web_contents());
1504 std::vector
<SkBitmap
> empty_icons
;
1505 std::vector
<gfx::Size
> empty_icon_sizes
;
1506 int download_id
= 0;
1508 // Try to download missing icon.
1509 download_id
= favicon_tab_helper
->StartDownload(missing_icon_url
, 0);
1510 EXPECT_NE(0, download_id
);
1511 EXPECT_FALSE(favicon_service
->WasUnableToDownloadFavicon(missing_icon_url
));
1513 // Report download failure with HTTP 503 status.
1514 favicon_tab_helper
->DidDownloadFavicon(download_id
, 503, missing_icon_url
,
1515 empty_icons
, empty_icon_sizes
);
1516 // Icon is not marked as UnableToDownload as HTTP status is not 404.
1517 EXPECT_FALSE(favicon_service
->WasUnableToDownloadFavicon(missing_icon_url
));
1519 // Try to download again.
1520 download_id
= favicon_tab_helper
->StartDownload(missing_icon_url
, 0);
1521 EXPECT_NE(0, download_id
);
1522 EXPECT_FALSE(favicon_service
->WasUnableToDownloadFavicon(missing_icon_url
));
1524 // Report download failure with HTTP 404 status.
1525 favicon_tab_helper
->DidDownloadFavicon(download_id
, 404, missing_icon_url
,
1526 empty_icons
, empty_icon_sizes
);
1527 // Icon is marked as UnableToDownload.
1528 EXPECT_TRUE(favicon_service
->WasUnableToDownloadFavicon(missing_icon_url
));
1530 // Try to download again.
1531 download_id
= favicon_tab_helper
->StartDownload(missing_icon_url
, 0);
1532 // Download is not started and Icon is still marked as UnableToDownload.
1533 EXPECT_EQ(0, download_id
);
1534 EXPECT_TRUE(favicon_service
->WasUnableToDownloadFavicon(missing_icon_url
));
1536 // Try to download another icon.
1537 download_id
= favicon_tab_helper
->StartDownload(another_icon_url
, 0);
1538 // Download is started as another icon URL is not same as missing_icon_url.
1539 EXPECT_NE(0, download_id
);
1540 EXPECT_FALSE(favicon_service
->WasUnableToDownloadFavicon(another_icon_url
));
1542 // Clear the list of missing icons.
1543 favicon_service
->ClearUnableToDownloadFavicons();
1544 EXPECT_FALSE(favicon_service
->WasUnableToDownloadFavicon(missing_icon_url
));
1545 EXPECT_FALSE(favicon_service
->WasUnableToDownloadFavicon(another_icon_url
));
1547 // Try to download again.
1548 download_id
= favicon_tab_helper
->StartDownload(missing_icon_url
, 0);
1549 EXPECT_NE(0, download_id
);
1550 // Report download success with HTTP 200 status.
1551 favicon_tab_helper
->DidDownloadFavicon(download_id
, 200, missing_icon_url
,
1552 empty_icons
, empty_icon_sizes
);
1553 // Icon is not marked as UnableToDownload as HTTP status is not 404.
1554 EXPECT_FALSE(favicon_service
->WasUnableToDownloadFavicon(missing_icon_url
));
1557 class FaviconHandlerActiveFaviconValidityParamTest
:
1558 public FaviconHandlerTest
,
1559 public ::testing::WithParamInterface
<bool> {
1561 FaviconHandlerActiveFaviconValidityParamTest() {}
1563 ~FaviconHandlerActiveFaviconValidityParamTest() override
{}
1565 bool GetActiveFaviconValiditySetting() {
1570 DISALLOW_COPY_AND_ASSIGN(FaviconHandlerActiveFaviconValidityParamTest
);
1573 TEST_P(FaviconHandlerActiveFaviconValidityParamTest
,
1574 TestDownloadLargestIconDoesNotImpactActiveFaviconValidity
) {
1575 const GURL
page_url("http://www.google.com");
1577 std::vector
<gfx::Size
> one_icon
;
1578 one_icon
.push_back(gfx::Size(15, 15));
1580 const GURL
old_favicon_url("http://www.google.com/old");
1581 const GURL
new_favicon_url("http://www.google.com/b");
1582 const FaviconURL source_icon_urls
[] = {
1583 FaviconURL(new_favicon_url
, favicon_base::FAVICON
, one_icon
)};
1584 TestFaviconDriver driver1
;
1585 TestFaviconHandler
handler1(page_url
, &driver1
,
1586 favicon::FaviconHandler::FAVICON
, true);
1587 std::vector
<FaviconURL
> urls1(source_icon_urls
,
1588 source_icon_urls
+ arraysize(source_icon_urls
));
1589 UpdateFaviconURL(&driver1
, &handler1
, page_url
, urls1
);
1591 HistoryRequestHandler
* history_handler
= handler1
.history_handler();
1593 // Simulate the active favicon is updated, this shouldn't happen in real
1594 // use case, but we want to verify the behavior below is not impacted by
1596 driver1
.SetActiveFaviconValidity(GetActiveFaviconValiditySetting());
1597 // Simulate the get favicon from history, but favicon URL didn't match.
1598 SetFaviconRawBitmapResult(old_favicon_url
,
1599 &history_handler
->history_results_
);
1600 history_handler
->InvokeCallback();
1601 // Since we downloaded largest icon, and don't want to set active favicon
1602 // NotifyFaviconAvaliable() should be called with is_active_favicon as false.
1603 EXPECT_EQ(old_favicon_url
, driver1
.available_icon_url());
1604 EXPECT_FALSE(driver1
.update_active_favicon());
1605 EXPECT_EQ(1u, driver1
.num_favicon_available());
1607 // We are trying to get favicon from history again.
1608 history_handler
= handler1
.history_handler();
1609 EXPECT_EQ(new_favicon_url
, history_handler
->icon_url_
);
1610 // Simulate the get expired favicon from history.
1611 history_handler
->history_results_
.clear();
1612 SetFaviconRawBitmapResult(new_favicon_url
, favicon_base::FAVICON
, true,
1613 &history_handler
->history_results_
);
1614 history_handler
->InvokeCallback();
1615 // Since we downloaded largest icon, and don't want to set active favicon
1616 // NotifyFaviconAvaliable() should be called with is_active_favicon as false.
1617 EXPECT_EQ(new_favicon_url
, driver1
.available_icon_url());
1618 EXPECT_FALSE(driver1
.update_active_favicon());
1619 EXPECT_EQ(2u, driver1
.num_favicon_available());
1621 // We are trying to download favicon.
1622 DownloadHandler
* download_handler
= handler1
.download_handler();
1623 EXPECT_TRUE(download_handler
->HasDownload());
1624 EXPECT_EQ(new_favicon_url
, download_handler
->GetImageUrl());
1625 // Simulate the download succeed.
1626 download_handler
->InvokeCallback();
1627 // Since we downloaded largest icon, and don't want to set active favicon
1628 // NotifyFaviconAvaliable() should be called with is_active_favicon as false.
1629 EXPECT_EQ(new_favicon_url
, driver1
.available_icon_url());
1630 EXPECT_FALSE(driver1
.update_active_favicon());
1631 EXPECT_EQ(3u, driver1
.num_favicon_available());
1634 INSTANTIATE_TEST_CASE_P(FaviconHandlerTestActiveFaviconValidityTrueOrFalse
,
1635 FaviconHandlerActiveFaviconValidityParamTest
,