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

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

Issue 2799273002: Add support to process favicons from Web Manifests (Closed)
Patch Set: Added comment. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/favicon/core/favicon_handler.h" 5 #include "components/favicon/core/favicon_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 using IntVector = std::vector<int>; 44 using IntVector = std::vector<int>;
45 using URLVector = std::vector<GURL>; 45 using URLVector = std::vector<GURL>;
46 using BitmapVector = std::vector<SkBitmap>; 46 using BitmapVector = std::vector<SkBitmap>;
47 using SizeVector = std::vector<gfx::Size>; 47 using SizeVector = std::vector<gfx::Size>;
48 48
49 MATCHER_P2(ImageSizeIs, width, height, "") { 49 MATCHER_P2(ImageSizeIs, width, height, "") {
50 *result_listener << "where size is " << arg.Width() << "x" << arg.Height(); 50 *result_listener << "where size is " << arg.Width() << "x" << arg.Height();
51 return arg.Size() == gfx::Size(width, height); 51 return arg.Size() == gfx::Size(width, height);
52 } 52 }
53 53
54 ACTION_P2(PostDownloadReply, status_code, candidates) {
55 base::ThreadTaskRunnerHandle::Get()->PostTask(
56 FROM_HERE, base::Bind(arg1, status_code, candidates));
57 }
58
54 // Fill the given bmp with some test data. 59 // Fill the given bmp with some test data.
55 SkBitmap CreateBitmapWithEdgeSize(int size) { 60 SkBitmap CreateBitmapWithEdgeSize(int size) {
56 SkBitmap bmp; 61 SkBitmap bmp;
57 bmp.allocN32Pixels(size, size); 62 bmp.allocN32Pixels(size, size);
58 63
59 unsigned char* src_data = 64 unsigned char* src_data =
60 reinterpret_cast<unsigned char*>(bmp.getAddr32(0, 0)); 65 reinterpret_cast<unsigned char*>(bmp.getAddr32(0, 0));
61 for (int i = 0; i < size * size; i++) { 66 for (int i = 0; i < size * size; i++) {
62 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); 67 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255);
63 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); 68 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 // Delegate image downloading to FakeImageDownloader. 200 // Delegate image downloading to FakeImageDownloader.
196 ON_CALL(*this, DownloadImage(_, _, _)) 201 ON_CALL(*this, DownloadImage(_, _, _))
197 .WillByDefault( 202 .WillByDefault(
198 Invoke(&fake_downloader_, &FakeImageDownloader::DownloadImage)); 203 Invoke(&fake_downloader_, &FakeImageDownloader::DownloadImage));
199 } 204 }
200 205
201 MOCK_METHOD3(DownloadImage, 206 MOCK_METHOD3(DownloadImage,
202 int(const GURL& url, 207 int(const GURL& url,
203 int max_image_size, 208 int max_image_size,
204 ImageDownloadCallback callback)); 209 ImageDownloadCallback callback));
210 MOCK_METHOD2(DownloadManifest,
211 void(const GURL& url, ManifestDownloadCallback callback));
205 MOCK_METHOD0(IsOffTheRecord, bool()); 212 MOCK_METHOD0(IsOffTheRecord, bool());
206 MOCK_METHOD1(IsBookmarked, bool(const GURL& url)); 213 MOCK_METHOD1(IsBookmarked, bool(const GURL& url));
207 MOCK_METHOD5(OnFaviconUpdated, 214 MOCK_METHOD5(OnFaviconUpdated,
208 void(const GURL& page_url, 215 void(const GURL& page_url,
209 FaviconDriverObserver::NotificationIconType type, 216 FaviconDriverObserver::NotificationIconType type,
210 const GURL& icon_url, 217 const GURL& icon_url,
211 bool icon_url_changed, 218 bool icon_url_changed,
212 const gfx::Image& image)); 219 const gfx::Image& image));
213 220
214 FakeImageDownloader& fake_downloader() { return fake_downloader_; } 221 FakeImageDownloader& fake_downloader() { return fake_downloader_; }
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 favicon_service_.fake()->ClearDbRequests(); 350 favicon_service_.fake()->ClearDbRequests();
344 delegate_.fake_downloader().ClearDownloads(); 351 delegate_.fake_downloader().ClearDownloads();
345 return testing::Mock::VerifyAndClearExpectations(&favicon_service_) && 352 return testing::Mock::VerifyAndClearExpectations(&favicon_service_) &&
346 testing::Mock::VerifyAndClearExpectations(&delegate_); 353 testing::Mock::VerifyAndClearExpectations(&delegate_);
347 } 354 }
348 355
349 // Creates a new handler and feeds in the page URL and the candidates. 356 // Creates a new handler and feeds in the page URL and the candidates.
350 // Returns the handler in case tests want to exercise further steps. 357 // Returns the handler in case tests want to exercise further steps.
351 std::unique_ptr<FaviconHandler> RunHandlerWithCandidates( 358 std::unique_ptr<FaviconHandler> RunHandlerWithCandidates(
352 FaviconDriverObserver::NotificationIconType handler_type, 359 FaviconDriverObserver::NotificationIconType handler_type,
353 const std::vector<favicon::FaviconURL>& candidates) { 360 const std::vector<favicon::FaviconURL>& candidates,
361 const base::Optional<GURL>& manifest_url = base::nullopt) {
354 auto handler = base::MakeUnique<FaviconHandler>(&favicon_service_, 362 auto handler = base::MakeUnique<FaviconHandler>(&favicon_service_,
355 &delegate_, handler_type); 363 &delegate_, handler_type);
356 handler->FetchFavicon(kPageURL); 364 handler->FetchFavicon(kPageURL);
357 // The first RunUntilIdle() causes the FaviconService lookups be faster than 365 // The first RunUntilIdle() causes the FaviconService lookups be faster than
358 // OnUpdateFaviconURL(), which is the most likely scenario. 366 // OnUpdateCandidates(), which is the most likely scenario.
359 base::RunLoop().RunUntilIdle(); 367 base::RunLoop().RunUntilIdle();
360 handler->OnUpdateFaviconURL(kPageURL, candidates); 368 handler->OnUpdateCandidates(kPageURL, candidates, manifest_url);
361 base::RunLoop().RunUntilIdle(); 369 base::RunLoop().RunUntilIdle();
362 return handler; 370 return handler;
363 } 371 }
364 372
365 // Same as above, but for the simplest case where all types are FAVICON and 373 // Same as above, but for the simplest case where all types are FAVICON and
366 // no sizes are provided, using a FaviconHandler of type NON_TOUCH_16_DIP. 374 // no sizes are provided, using a FaviconHandler of type NON_TOUCH_16_DIP.
367 std::unique_ptr<FaviconHandler> RunHandlerWithSimpleFaviconCandidates( 375 std::unique_ptr<FaviconHandler> RunHandlerWithSimpleFaviconCandidates(
368 const std::vector<GURL>& urls) { 376 const std::vector<GURL>& urls,
377 const base::Optional<GURL>& manifest_url = base::nullopt) {
369 std::vector<favicon::FaviconURL> candidates; 378 std::vector<favicon::FaviconURL> candidates;
370 for (const GURL& url : urls) { 379 for (const GURL& url : urls) {
371 candidates.emplace_back(url, FAVICON, kEmptySizes); 380 candidates.emplace_back(url, FAVICON, kEmptySizes);
372 } 381 }
373 return RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP, 382 return RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP,
374 candidates); 383 candidates, manifest_url);
375 } 384 }
376 385
377 base::MessageLoopForUI message_loop_; 386 base::MessageLoopForUI message_loop_;
378 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> 387 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors>
379 scoped_set_supported_scale_factors_; 388 scoped_set_supported_scale_factors_;
380 testing::NiceMock<MockFaviconServiceWithFake> favicon_service_; 389 testing::NiceMock<MockFaviconServiceWithFake> favicon_service_;
381 testing::NiceMock<MockDelegate> delegate_; 390 testing::NiceMock<MockDelegate> delegate_;
382 }; 391 };
383 392
384 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) { 393 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
(...skipping 17 matching lines...) Expand all
402 kPageURL, URLVector{kIconURL16x16}, FAVICON, 411 kPageURL, URLVector{kIconURL16x16}, FAVICON,
403 /*desired_size_in_dip=*/16, _, _)); 412 /*desired_size_in_dip=*/16, _, _));
404 413
405 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}); 414 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16});
406 } 415 }
407 416
408 // Test that the FaviconHandler process finishes when: 417 // Test that the FaviconHandler process finishes when:
409 // - There is data in the database for neither the page URL nor the icon URL. 418 // - There is data in the database for neither the page URL nor the icon URL.
410 // AND 419 // AND
411 // - FaviconService::GetFaviconForPageURL() callback returns before 420 // - FaviconService::GetFaviconForPageURL() callback returns before
412 // FaviconHandler::OnUpdateFaviconURL() is called. 421 // FaviconHandler::OnUpdateCandidates() is called.
413 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesSlower) { 422 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesSlower) {
414 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON, 423 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON,
415 ImageSizeIs(16, 16))); 424 ImageSizeIs(16, 16)));
416 EXPECT_CALL(delegate_, OnFaviconUpdated( 425 EXPECT_CALL(delegate_, OnFaviconUpdated(
417 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP, 426 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
418 kIconURL16x16, /*icon_url_changed=*/true, _)); 427 kIconURL16x16, /*icon_url_changed=*/true, _));
419 428
420 FaviconHandler handler(&favicon_service_, &delegate_, 429 FaviconHandler handler(&favicon_service_, &delegate_,
421 FaviconDriverObserver::NON_TOUCH_16_DIP); 430 FaviconDriverObserver::NON_TOUCH_16_DIP);
422 handler.FetchFavicon(kPageURL); 431 handler.FetchFavicon(kPageURL);
423 // Causes FaviconService lookups be faster than OnUpdateFaviconURL(). 432 // Causes FaviconService lookups be faster than OnUpdateCandidates().
424 base::RunLoop().RunUntilIdle(); 433 base::RunLoop().RunUntilIdle();
425 handler.OnUpdateFaviconURL(kPageURL, 434 handler.OnUpdateCandidates(kPageURL,
426 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); 435 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)},
436 base::nullopt);
427 base::RunLoop().RunUntilIdle(); 437 base::RunLoop().RunUntilIdle();
428 438
429 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); 439 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
430 EXPECT_THAT(favicon_service_.fake()->db_requests(), 440 EXPECT_THAT(favicon_service_.fake()->db_requests(),
431 ElementsAre(kPageURL, kIconURL16x16)); 441 ElementsAre(kPageURL, kIconURL16x16));
432 } 442 }
433 443
434 // Test that the FaviconHandler process finishes when: 444 // Test that the FaviconHandler process finishes when:
435 // - There is data in the database for neither the page URL nor the icon URL. 445 // - There is data in the database for neither the page URL nor the icon URL.
436 // AND 446 // AND
437 // - FaviconService::GetFaviconForPageURL() callback returns after 447 // - FaviconService::GetFaviconForPageURL() callback returns after
438 // FaviconHandler::OnUpdateFaviconURL() is called. 448 // FaviconHandler::OnUpdateCandidates() is called.
439 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesFaster) { 449 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesFaster) {
440 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON, 450 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON,
441 ImageSizeIs(16, 16))); 451 ImageSizeIs(16, 16)));
442 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)); 452 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _));
443 453
444 FaviconHandler handler(&favicon_service_, &delegate_, 454 FaviconHandler handler(&favicon_service_, &delegate_,
445 FaviconDriverObserver::NON_TOUCH_16_DIP); 455 FaviconDriverObserver::NON_TOUCH_16_DIP);
446 handler.FetchFavicon(kPageURL); 456 handler.FetchFavicon(kPageURL);
447 ASSERT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL)); 457 ASSERT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL));
448 458
449 // Feed in favicons without processing posted tasks (RunUntilIdle()). 459 // Feed in favicons without processing posted tasks (RunUntilIdle()).
450 handler.OnUpdateFaviconURL(kPageURL, 460 handler.OnUpdateCandidates(kPageURL,
451 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); 461 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)},
462 base::nullopt);
452 base::RunLoop().RunUntilIdle(); 463 base::RunLoop().RunUntilIdle();
453 464
454 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); 465 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
455 } 466 }
456 467
457 // Test that the FaviconHandler process does not save anything to the database 468 // Test that the FaviconHandler process does not save anything to the database
458 // for incognito tabs. 469 // for incognito tabs.
459 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconInIncognito) { 470 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconInIncognito) {
460 ON_CALL(delegate_, IsOffTheRecord()).WillByDefault(Return(true)); 471 ON_CALL(delegate_, IsOffTheRecord()).WillByDefault(Return(true));
461 472
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 FaviconURL(kIconURLReturning500, TOUCH_PRECOMPOSED_ICON, kEmptySizes), 613 FaviconURL(kIconURLReturning500, TOUCH_PRECOMPOSED_ICON, kEmptySizes),
603 FaviconURL(kIconURL64x64, TOUCH_ICON, kEmptySizes), 614 FaviconURL(kIconURL64x64, TOUCH_ICON, kEmptySizes),
604 }); 615 });
605 // First download fails, second succeeds. 616 // First download fails, second succeeds.
606 EXPECT_THAT(delegate_.downloads(), 617 EXPECT_THAT(delegate_.downloads(),
607 ElementsAre(kIconURLReturning500, kIconURL64x64)); 618 ElementsAre(kIconURLReturning500, kIconURL64x64));
608 } 619 }
609 620
610 // Test that download data for icon URLs other than the current favicon 621 // Test that download data for icon URLs other than the current favicon
611 // candidate URLs is ignored. This test tests the scenario where a download is 622 // candidate URLs is ignored. This test tests the scenario where a download is
612 // in flight when FaviconHandler::OnUpdateFaviconURL() is called. 623 // in flight when FaviconHandler::OnUpdateCandidates() is called.
613 // TODO(mastiz): Make this test deal with FaviconURLs of type 624 // TODO(mastiz): Make this test deal with FaviconURLs of type
614 // favicon_base::FAVICON and add new ones like OnlyDownloadMatchingIconType and 625 // favicon_base::FAVICON and add new ones like OnlyDownloadMatchingIconType and
615 // CallSetFaviconsWithCorrectIconType. 626 // CallSetFaviconsWithCorrectIconType.
616 TEST_F(FaviconHandlerTest, UpdateDuringDownloading) { 627 TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
617 const GURL kIconURL1("http://www.google.com/favicon"); 628 const GURL kIconURL1("http://www.google.com/favicon");
618 const GURL kIconURL2 = kIconURL16x16; 629 const GURL kIconURL2 = kIconURL16x16;
619 const GURL kIconURL3 = kIconURL64x64; 630 const GURL kIconURL3 = kIconURL64x64;
620 631
621 // Defer the download completion such that RunUntilIdle() doesn't complete 632 // Defer the download completion such that RunUntilIdle() doesn't complete
622 // the download. 633 // the download.
623 delegate_.fake_downloader().SetRunCallbackManuallyForUrl(kIconURL1); 634 delegate_.fake_downloader().SetRunCallbackManuallyForUrl(kIconURL1);
624 635
625 delegate_.fake_downloader().Add(kIconURL1, IntVector{16}); 636 delegate_.fake_downloader().Add(kIconURL1, IntVector{16});
626 delegate_.fake_downloader().Add(kIconURL3, IntVector{64}); 637 delegate_.fake_downloader().Add(kIconURL3, IntVector{64});
627 638
628 std::unique_ptr<FaviconHandler> handler = 639 std::unique_ptr<FaviconHandler> handler =
629 RunHandlerWithSimpleFaviconCandidates({kIconURL1, kIconURL2}); 640 RunHandlerWithSimpleFaviconCandidates({kIconURL1, kIconURL2});
630 641
631 ASSERT_TRUE(VerifyAndClearExpectations()); 642 ASSERT_TRUE(VerifyAndClearExpectations());
632 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback()); 643 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback());
633 644
634 // Favicon update should invalidate the ongoing download. 645 // Favicon update should invalidate the ongoing download.
635 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL3, _, _)); 646 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL3, _, _));
636 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL3, _, _)); 647 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL3, _, _));
637 648
638 handler->OnUpdateFaviconURL(kPageURL, 649 handler->OnUpdateCandidates(
639 {FaviconURL(kIconURL3, FAVICON, kEmptySizes)}); 650 kPageURL, {FaviconURL(kIconURL3, FAVICON, kEmptySizes)}, base::nullopt);
640 651
641 // Finalizes download, which should be thrown away as the favicon URLs were 652 // Finalizes download, which should be thrown away as the favicon URLs were
642 // updated. 653 // updated.
643 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually()); 654 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually());
644 base::RunLoop().RunUntilIdle(); 655 base::RunLoop().RunUntilIdle();
645 656
646 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kIconURL3)); 657 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kIconURL3));
647 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL3)); 658 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL3));
648 } 659 }
649 660
(...skipping 13 matching lines...) Expand all
663 delegate_.fake_downloader().Add(kSlowLoadingIconURL, IntVector{16}); 674 delegate_.fake_downloader().Add(kSlowLoadingIconURL, IntVector{16});
664 675
665 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates( 676 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates(
666 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls); 677 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls);
667 678
668 ASSERT_THAT(favicon_service_.fake()->db_requests(), 679 ASSERT_THAT(favicon_service_.fake()->db_requests(),
669 ElementsAre(kPageURL, kIconURL64x64, kSlowLoadingIconURL)); 680 ElementsAre(kPageURL, kIconURL64x64, kSlowLoadingIconURL));
670 ASSERT_TRUE(VerifyAndClearExpectations()); 681 ASSERT_TRUE(VerifyAndClearExpectations());
671 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback()); 682 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback());
672 683
673 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect, 684 // Calling OnUpdateCandidates() with the same icon URLs should have no effect,
674 // despite the ongoing download. 685 // despite the ongoing download.
675 handler->OnUpdateFaviconURL(kPageURL, favicon_urls); 686 handler->OnUpdateCandidates(kPageURL, favicon_urls, base::nullopt);
676 base::RunLoop().RunUntilIdle(); 687 base::RunLoop().RunUntilIdle();
677 688
678 // Complete the download. 689 // Complete the download.
679 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)); 690 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _));
680 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)); 691 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _));
681 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually()); 692 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually());
682 base::RunLoop().RunUntilIdle(); 693 base::RunLoop().RunUntilIdle();
683 EXPECT_THAT(delegate_.downloads(), IsEmpty()); 694 EXPECT_THAT(delegate_.downloads(), IsEmpty());
684 } 695 }
685 696
686 // Test that calling OnUpdateFaviconUrl() with the same icon URLs as before is a 697 // Test that calling OnUpdateFaviconUrl() with the same icon URLs as before is a
687 // no-op. This is important because OnUpdateFaviconUrl() is called when the page 698 // no-op. This is important because OnUpdateFaviconUrl() is called when the page
688 // finishes loading. This can occur several times for pages with iframes. 699 // finishes loading. This can occur several times for pages with iframes.
689 TEST_F(FaviconHandlerTest, UpdateSameIconURLsAfterFinishedShouldBeNoop) { 700 TEST_F(FaviconHandlerTest, UpdateSameIconURLsAfterFinishedShouldBeNoop) {
690 const std::vector<FaviconURL> favicon_urls = { 701 const std::vector<FaviconURL> favicon_urls = {
691 FaviconURL(kIconURL10x10, FAVICON, kEmptySizes), 702 FaviconURL(kIconURL10x10, FAVICON, kEmptySizes),
692 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes), 703 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes),
693 }; 704 };
694 705
695 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates( 706 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates(
696 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls); 707 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls);
697 708
698 ASSERT_TRUE(VerifyAndClearExpectations()); 709 ASSERT_TRUE(VerifyAndClearExpectations());
699 710
700 // Calling OnUpdateFaviconURL() with identical data should be a no-op. 711 // Calling OnUpdateCandidates() with identical data should be a no-op.
701 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); 712 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0);
702 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); 713 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0);
703 714
704 handler->OnUpdateFaviconURL(kPageURL, favicon_urls); 715 handler->OnUpdateCandidates(kPageURL, favicon_urls, base::nullopt);
705 base::RunLoop().RunUntilIdle(); 716 base::RunLoop().RunUntilIdle();
706 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty()); 717 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty());
707 EXPECT_THAT(delegate_.downloads(), IsEmpty()); 718 EXPECT_THAT(delegate_.downloads(), IsEmpty());
708 } 719 }
709 720
710 // Fixes crbug.com/544560 721 // Fixes crbug.com/544560
711 // Tests that Delegate::OnFaviconUpdated() is called if: 722 // Tests that Delegate::OnFaviconUpdated() is called if:
712 // - The best icon on the initial page is not the last icon. 723 // - The best icon on the initial page is not the last icon.
713 // - All of the initial page's icons are downloaded. 724 // - All of the initial page's icons are downloaded.
714 // AND 725 // AND
(...skipping 21 matching lines...) Expand all
736 // database and downloaded. |kIconURL2| should have been fetched from the 747 // database and downloaded. |kIconURL2| should have been fetched from the
737 // database and downloaded last. 748 // database and downloaded last.
738 ASSERT_THAT(delegate_.downloads(), ElementsAre(kIconURL1, kIconURL2)); 749 ASSERT_THAT(delegate_.downloads(), ElementsAre(kIconURL1, kIconURL2));
739 ASSERT_THAT(favicon_service_.fake()->db_requests(), 750 ASSERT_THAT(favicon_service_.fake()->db_requests(),
740 ElementsAre(kPageURL, kIconURL1, kIconURL2)); 751 ElementsAre(kPageURL, kIconURL1, kIconURL2));
741 ASSERT_TRUE(VerifyAndClearExpectations()); 752 ASSERT_TRUE(VerifyAndClearExpectations());
742 753
743 // Simulate the page changing it's icon URL to just |kIconURL2| via 754 // Simulate the page changing it's icon URL to just |kIconURL2| via
744 // Javascript. 755 // Javascript.
745 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2, _, _)); 756 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2, _, _));
746 handler->OnUpdateFaviconURL(kPageURL, 757 handler->OnUpdateCandidates(
747 {FaviconURL(kIconURL2, FAVICON, kEmptySizes)}); 758 kPageURL, {FaviconURL(kIconURL2, FAVICON, kEmptySizes)}, base::nullopt);
748 base::RunLoop().RunUntilIdle(); 759 base::RunLoop().RunUntilIdle();
749 } 760 }
750 761
751 // Test the favicon which is selected when the web page provides several 762 // Test the favicon which is selected when the web page provides several
752 // favicons and none of the favicons are cached in history. 763 // favicons and none of the favicons are cached in history.
753 // The goal of this test is to be more of an integration test than 764 // The goal of this test is to be more of an integration test than
754 // SelectFaviconFramesTest.*. 765 // SelectFaviconFramesTest.*.
755 class FaviconHandlerMultipleFaviconsTest : public FaviconHandlerTest { 766 class FaviconHandlerMultipleFaviconsTest : public FaviconHandlerTest {
756 protected: 767 protected:
757 FaviconHandlerMultipleFaviconsTest() { 768 FaviconHandlerMultipleFaviconsTest() {
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 EXPECT_CALL(delegate_, 1035 EXPECT_CALL(delegate_,
1025 OnFaviconUpdated(_, _, kIconURL12x12, _, ImageSizeIs(12, 12))); 1036 OnFaviconUpdated(_, _, kIconURL12x12, _, ImageSizeIs(12, 12)));
1026 1037
1027 RunHandlerWithCandidates( 1038 RunHandlerWithCandidates(
1028 FaviconDriverObserver::NON_TOUCH_LARGEST, 1039 FaviconDriverObserver::NON_TOUCH_LARGEST,
1029 {FaviconURL(kIconURL10x10, FAVICON, SizeVector{gfx::Size(16, 16)}), 1040 {FaviconURL(kIconURL10x10, FAVICON, SizeVector{gfx::Size(16, 16)}),
1030 FaviconURL(kIconURL12x12, FAVICON, SizeVector{gfx::Size(15, 15)}), 1041 FaviconURL(kIconURL12x12, FAVICON, SizeVector{gfx::Size(15, 15)}),
1031 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); 1042 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)});
1032 } 1043 }
1033 1044
1045 TEST_F(FaviconHandlerTest, GetFaviconFromManifestInHistory) {
1046 const GURL kManifestURL("http://www.google.com/manifest.json");
1047
1048 favicon_service_.fake()->Store(kManifestURL, kIconURL16x16,
1049 CreateRawBitmapResult(kIconURL16x16));
pkotwicz 2017/04/12 22:10:07 This test looks like it is left over from when the
mastiz 2017/04/20 18:06:33 Done.
1050
1051 EXPECT_CALL(delegate_, DownloadManifest(_, _)).Times(0);
1052 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0);
1053
1054 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
1055 kPageURL, URLVector{kManifestURL}, FAVICON,
1056 /*desired_size_in_dip=*/16, _, _));
1057 EXPECT_CALL(delegate_, OnFaviconUpdated(
1058 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
1059 kManifestURL, /*icon_url_changed=*/true, _));
1060
1061 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL);
1062 EXPECT_THAT(favicon_service_.fake()->db_requests(),
1063 ElementsAre(kPageURL, kManifestURL));
1064 EXPECT_THAT(delegate_.downloads(), IsEmpty());
1065 }
1066
1067 TEST_F(FaviconHandlerTest, GetFaviconFromManifestInHistoryIfCandidatesFaster) {
pkotwicz 2017/04/12 22:10:07 This test looks like it is left over from when the
mastiz 2017/04/20 18:06:33 Done.
1068 const GURL kManifestURL("http://www.google.com/manifest.json");
1069
1070 favicon_service_.fake()->Store(kManifestURL, kIconURL16x16,
1071 CreateRawBitmapResult(kIconURL16x16));
1072
1073 EXPECT_CALL(delegate_, DownloadManifest(_, _)).Times(0);
1074 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0);
1075
1076 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
1077 kPageURL, URLVector{kManifestURL}, FAVICON,
1078 /*desired_size_in_dip=*/16, _, _));
1079 EXPECT_CALL(delegate_, OnFaviconUpdated(
1080 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP,
1081 kManifestURL, /*icon_url_changed=*/true, _));
1082
1083 FaviconHandler handler(&favicon_service_, &delegate_,
1084 FaviconDriverObserver::NON_TOUCH_16_DIP);
1085 handler.FetchFavicon(kPageURL);
1086 // Feed in candidates without processing posted tasks (RunUntilIdle()).
1087 handler.OnUpdateCandidates(kPageURL,
1088 {FaviconURL(kIconURL12x12, FAVICON, kEmptySizes)},
1089 kManifestURL);
1090 base::RunLoop().RunUntilIdle();
1091
1092 EXPECT_THAT(favicon_service_.fake()->db_requests(),
1093 ElementsAre(kPageURL, kManifestURL));
1094 EXPECT_THAT(delegate_.downloads(), IsEmpty());
1095 }
1096
1097 TEST_F(FaviconHandlerTest, GetFaviconFromUnknownManifest) {
1098 const GURL kManifestURL("http://www.google.com/manifest.json");
1099 const std::vector<favicon::FaviconURL> kManifestIcons = {
1100 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes),
1101 };
1102
1103 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0);
1104
1105 EXPECT_CALL(delegate_, DownloadManifest(kManifestURL, _))
1106 .WillOnce(PostDownloadReply(200, kManifestIcons));
1107 EXPECT_CALL(favicon_service_,
1108 SetFavicons(kPageURL, kManifestURL, FAVICON, _));
1109 EXPECT_CALL(delegate_, OnFaviconUpdated(kPageURL, _, kManifestURL, _, _));
1110
1111 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL);
1112 EXPECT_THAT(favicon_service_.fake()->db_requests(),
1113 ElementsAre(kPageURL, kManifestURL));
1114 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16));
1115 }
1116
1117 TEST_F(FaviconHandlerTest, GetFaviconFromExpiredManifestIcon) {
1118 const GURL kManifestURL("http://www.google.com/manifest.json");
1119 const std::vector<favicon::FaviconURL> kManifestIcons = {
1120 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes),
1121 };
1122
1123 favicon_service_.fake()->Store(kPageURL, kManifestURL,
1124 CreateRawBitmapResult(kManifestURL, FAVICON,
1125 /*expired=*/true));
1126
1127 EXPECT_CALL(delegate_, OnFaviconUpdated(kPageURL, _, kManifestURL, _, _))
1128 .Times(2);
1129 EXPECT_CALL(delegate_, DownloadManifest(kManifestURL, _))
1130 .WillOnce(PostDownloadReply(200, kManifestIcons));
1131 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kManifestURL, _, _));
1132
1133 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL);
1134 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL));
1135 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL64x64));
1136 }
1137
1138 TEST_F(FaviconHandlerTest, UnknownManifestReturning404) {
1139 const GURL kManifestURL("http://www.google.com/manifest.json");
1140
1141 ON_CALL(delegate_, DownloadManifest(kManifestURL, _))
1142 .WillByDefault(
1143 PostDownloadReply(404, std::vector<favicon::FaviconURL>()));
1144
1145 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kManifestURL));
1146 EXPECT_CALL(favicon_service_,
1147 SetFavicons(kPageURL, kIconURL12x12, FAVICON, _));
1148
1149 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL);
1150 EXPECT_THAT(favicon_service_.fake()->db_requests(),
1151 ElementsAre(kPageURL, kManifestURL, kIconURL12x12));
1152 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL12x12));
1153 }
1154
1155 TEST_F(FaviconHandlerTest, UnknownManifestReturning503) {
1156 const GURL kManifestURL("http://www.google.com/manifest.json");
1157
1158 ON_CALL(delegate_, DownloadManifest(kManifestURL, _))
1159 .WillByDefault(
1160 PostDownloadReply(503, std::vector<favicon::FaviconURL>()));
1161
1162 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0);
1163
1164 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL);
1165 }
1166
1167 TEST_F(FaviconHandlerTest, IgnoreManifestWithPrior404) {
1168 const GURL kManifestURL("http://www.google.com/manifest.json");
1169
1170 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kManifestURL))
1171 .WillByDefault(Return(true));
1172
1173 EXPECT_CALL(delegate_, DownloadManifest(_, _)).Times(0);
1174 EXPECT_CALL(favicon_service_,
1175 SetFavicons(kPageURL, kIconURL12x12, FAVICON, _));
1176
1177 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL);
1178 EXPECT_THAT(favicon_service_.fake()->db_requests(),
1179 ElementsAre(kPageURL, kIconURL12x12));
1180 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL12x12));
1181 }
1182
1183 TEST_F(FaviconHandlerTest, UnknownManifestWithoutIcons) {
1184 const GURL kManifestURL("http://www.google.com/manifest.json");
1185
1186 ON_CALL(delegate_, DownloadManifest(kManifestURL, _))
1187 .WillByDefault(
1188 PostDownloadReply(200, std::vector<favicon::FaviconURL>()));
1189
1190 // UnableToDownloadFavicon() is expected to prevent repeated downloads of the
1191 // same manifest (which is not otherwise cached, since it doesn't contain
1192 // icons).
1193 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kManifestURL));
1194 EXPECT_CALL(favicon_service_,
1195 SetFavicons(kPageURL, kIconURL12x12, FAVICON, _));
1196
1197 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL);
1198 EXPECT_THAT(favicon_service_.fake()->db_requests(),
1199 ElementsAre(kPageURL, kManifestURL, kIconURL12x12));
1200 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL12x12));
1201 }
1202
1203 TEST_F(FaviconHandlerTest, ManifestUpdateViaJavascript) {
1204 const GURL kManifestURL1("http://www.google.com/manifest1.json");
1205 const GURL kManifestURL2("http://www.google.com/manifest2.json");
1206 const std::vector<favicon::FaviconURL> kManifestIcons1 = {
1207 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes),
1208 };
1209 const std::vector<favicon::FaviconURL> kManifestIcons2 = {
1210 FaviconURL(kIconURL10x10, FAVICON, kEmptySizes),
1211 };
1212
1213 ON_CALL(delegate_, DownloadManifest(kManifestURL1, _))
1214 .WillByDefault(PostDownloadReply(200, kManifestIcons1));
1215 ON_CALL(delegate_, DownloadManifest(kManifestURL2, _))
1216 .WillByDefault(PostDownloadReply(200, kManifestIcons2));
1217
1218 EXPECT_CALL(delegate_, DownloadManifest(kManifestURL1, _));
1219 std::unique_ptr<FaviconHandler> handler =
1220 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL1);
1221 ASSERT_THAT(favicon_service_.fake()->db_requests(),
1222 ElementsAre(kPageURL, kManifestURL1));
1223 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL64x64));
1224 ASSERT_TRUE(VerifyAndClearExpectations());
1225
1226 // Simulate the page changing it's manifest URL via Javascript.
1227 EXPECT_CALL(delegate_, DownloadManifest(kManifestURL2, _));
1228 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL2, _, _));
1229 handler->OnUpdateCandidates(kPageURL,
1230 {FaviconURL(kIconURL12x12, FAVICON, kEmptySizes)},
1231 kManifestURL2);
1232 base::RunLoop().RunUntilIdle();
1233 ASSERT_THAT(favicon_service_.fake()->db_requests(),
1234 ElementsAre(kManifestURL2));
1235 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL10x10));
1236 }
1237
1238 // TODO(mastiz) / DONOTSUBMIT: Add missing tests.
pkotwicz 2017/04/12 22:10:07 Can you please add a test for when: - There is a W
mastiz 2017/04/20 18:06:33 Done, UnknownManifestWithoutIconsAndKnownRegularIc
1239
1034 } // namespace 1240 } // namespace
1035 } // namespace favicon 1241 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698