Index: components/favicon/content/content_favicon_driver_unittest.cc |
diff --git a/components/favicon/content/content_favicon_driver_unittest.cc b/components/favicon/content/content_favicon_driver_unittest.cc |
index 0c75c52a22d38e76bbd200648c50ee2be985e97e..d1ede99adfffb8888b2b18cecbb4c564245131ae 100644 |
--- a/components/favicon/content/content_favicon_driver_unittest.cc |
+++ b/components/favicon/content/content_favicon_driver_unittest.cc |
@@ -8,11 +8,14 @@ |
#include <vector> |
#include "base/macros.h" |
+#include "base/run_loop.h" |
#include "components/favicon/core/favicon_client.h" |
+#include "components/favicon/core/favicon_handler.h" |
#include "components/favicon/core/test/mock_favicon_service.h" |
#include "content/public/browser/web_contents_observer.h" |
#include "content/public/common/favicon_url.h" |
#include "content/public/test/test_renderer_host.h" |
+#include "content/public/test/web_contents_tester.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "third_party/skia/include/core/SkBitmap.h" |
@@ -21,12 +24,23 @@ |
namespace favicon { |
namespace { |
-using testing::Mock; |
using testing::Return; |
+using testing::_; |
class ContentFaviconDriverTest : public content::RenderViewHostTestHarness { |
protected: |
- ContentFaviconDriverTest() {} |
+ const std::vector<gfx::Size> kEmptyIconSizes; |
+ const std::vector<SkBitmap> kEmptyIcons; |
+ const std::vector<favicon_base::FaviconRawBitmapResult> kEmptyRawBitmapResult; |
+ const GURL kPageURL = GURL("http://www.google.com/"); |
+ const GURL kIconURL = GURL("http://www.google.com/favicon.ico"); |
+ |
+ ContentFaviconDriverTest() { |
+ ON_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(_, _, _, _, _, _)) |
+ .WillByDefault(PostReply<6>(kEmptyRawBitmapResult)); |
+ ON_CALL(favicon_service_, GetFaviconForPageURL(_, _, _, _, _)) |
+ .WillByDefault(PostReply<5>(kEmptyRawBitmapResult)); |
+ } |
~ContentFaviconDriverTest() override {} |
@@ -38,53 +52,80 @@ class ContentFaviconDriverTest : public content::RenderViewHostTestHarness { |
web_contents(), &favicon_service_, nullptr, nullptr); |
} |
- testing::StrictMock<MockFaviconService> favicon_service_; |
+ content::WebContentsTester* web_contents_tester() { |
+ return content::WebContentsTester::For(web_contents()); |
+ } |
+ |
+ void TestFetchFaviconForPage( |
+ const GURL& page_url, |
+ const std::vector<content::FaviconURL>& candidates) { |
+ ContentFaviconDriver* favicon_driver = |
+ ContentFaviconDriver::FromWebContents(web_contents()); |
+ web_contents_tester()->NavigateAndCommit(page_url); |
+ static_cast<content::WebContentsObserver*>(favicon_driver) |
+ ->DidUpdateFaviconURL(candidates); |
+ base::RunLoop().RunUntilIdle(); |
+ } |
+ |
+ testing::NiceMock<MockFaviconService> favicon_service_; |
}; |
+// Test that UnableToDownloadFavicon() is not called as a result of a favicon |
+// download with 200 status. |
+TEST_F(ContentFaviconDriverTest, ShouldNotCallUnableToDownloadFaviconFor200) { |
+ EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconURL)).Times(0); |
+ // Mimic a page load. |
+ TestFetchFaviconForPage( |
+ kPageURL, |
+ {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, |
+ kEmptyIconSizes)}); |
+ // Completing the download should not cause a call to |
+ // UnableToDownloadFavicon(). |
+ EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
+ kIconURL, 200, kEmptyIcons, kEmptyIconSizes)); |
+} |
+ |
+// Test that UnableToDownloadFavicon() is called as a result of a favicon |
+// download with 404 status. |
+TEST_F(ContentFaviconDriverTest, ShouldCallUnableToDownloadFaviconFor404) { |
+ EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconURL)); |
+ // Mimic a page load. |
+ TestFetchFaviconForPage( |
+ kPageURL, |
+ {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, |
+ kEmptyIconSizes)}); |
+ // Mimic the completion of an image download. |
+ EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
+ kIconURL, 404, kEmptyIcons, kEmptyIconSizes)); |
+} |
+ |
+// Test that UnableToDownloadFavicon() is not called as a result of a favicon |
+// download with 503 status. |
+TEST_F(ContentFaviconDriverTest, ShouldNotCallUnableToDownloadFaviconFor503) { |
+ EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconURL)).Times(0); |
+ // Mimic a page load. |
+ TestFetchFaviconForPage( |
+ kPageURL, |
+ {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, |
+ kEmptyIconSizes)}); |
+ // Completing the download should not cause a call to |
+ // UnableToDownloadFavicon(). |
+ EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
+ kIconURL, 503, kEmptyIcons, kEmptyIconSizes)); |
+} |
+ |
// Test that Favicon is not requested repeatedly during the same session if |
-// server returns HTTP 404 status. |
-TEST_F(ContentFaviconDriverTest, UnableToDownloadFavicon) { |
- const GURL missing_icon_url("http://www.google.com/favicon.ico"); |
- |
- ContentFaviconDriver* content_favicon_driver = |
- ContentFaviconDriver::FromWebContents(web_contents()); |
- |
- std::vector<SkBitmap> empty_icons; |
- std::vector<gfx::Size> empty_icon_sizes; |
- int download_id = 0; |
- |
- // Try to download missing icon. |
- EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) |
- .WillOnce(Return(false)); |
- download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
- EXPECT_NE(0, download_id); |
- |
- // Report download failure with HTTP 503 status. |
- content_favicon_driver->DidDownloadFavicon(download_id, 503, missing_icon_url, |
- empty_icons, empty_icon_sizes); |
- Mock::VerifyAndClearExpectations(&favicon_service_); |
- |
- // Try to download again. |
- EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) |
- .WillOnce(Return(false)); |
- download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
- EXPECT_NE(0, download_id); |
- Mock::VerifyAndClearExpectations(&favicon_service_); |
- |
- // Report download failure with HTTP 404 status, which causes the icon to be |
- // marked as UnableToDownload. |
- EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(missing_icon_url)); |
- content_favicon_driver->DidDownloadFavicon(download_id, 404, missing_icon_url, |
- empty_icons, empty_icon_sizes); |
- Mock::VerifyAndClearExpectations(&favicon_service_); |
- |
- // Try to download again. |
- EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) |
- .WillOnce(Return(true)); |
- download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
- // Download is not started and Icon is still marked as UnableToDownload. |
- EXPECT_EQ(0, download_id); |
- Mock::VerifyAndClearExpectations(&favicon_service_); |
+// the favicon is known to be unavailable (e.g. due to HTTP 404 status). |
+TEST_F(ContentFaviconDriverTest, ShouldNotRequestRepeatedlyIfUnavailable) { |
+ ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL)) |
+ .WillByDefault(Return(true)); |
+ // Mimic a page load. |
+ TestFetchFaviconForPage( |
+ kPageURL, |
+ {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, |
+ kEmptyIconSizes)}); |
+ // Verify that no download request is pending for the image. |
+ EXPECT_FALSE(web_contents_tester()->HasPendingDownloadImage(kIconURL)); |
} |
// Test that ContentFaviconDriver ignores updated favicon URLs if there is no |