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

Unified Diff: components/favicon/content/content_favicon_driver_unittest.cc

Issue 2697803003: Improve test coverage for ContentFaviconDriver (Closed)
Patch Set: Rebased. Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
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..19bf7f8321496646ea8d7b1db6d28a5992dc306a 100644
--- a/components/favicon/content/content_favicon_driver_unittest.cc
+++ b/components/favicon/content/content_favicon_driver_unittest.cc
@@ -9,10 +9,12 @@
#include "base/macros.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,13 +23,20 @@
namespace favicon {
namespace {
-using testing::Mock;
+using testing::DoAll;
using testing::Return;
+using testing::SaveArg;
+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() {}
~ContentFaviconDriverTest() override {}
// content::RenderViewHostTestHarness:
@@ -38,53 +47,164 @@ class ContentFaviconDriverTest : public content::RenderViewHostTestHarness {
web_contents(), &favicon_service_, nullptr, nullptr);
}
+ content::WebContentsTester* web_contents_tester() {
+ return content::WebContentsTester::For(web_contents());
+ }
+
+ void TestDidLoadPage(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);
+ }
+
testing::StrictMock<MockFaviconService> favicon_service_;
};
-// 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");
+TEST_F(ContentFaviconDriverTest, ShouldCacheAvailableFavicon) {
+ // Set up expectations for FaviconService, and store callbacks for later
+ // triggering.
+ EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
+ .WillOnce(Return(false));
- ContentFaviconDriver* content_favicon_driver =
- ContentFaviconDriver::FromWebContents(web_contents());
+ favicon_base::FaviconResultsCallback get_favicon_callback;
+ EXPECT_CALL(favicon_service_,
+ GetFaviconForPageURL(kPageUrl, favicon_base::FAVICON, _, _, _))
+ .WillOnce(DoAll(SaveArg<3>(&get_favicon_callback), Return(1)));
+
+ favicon_base::FaviconResultsCallback update_mappings_callback;
+ EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
+ kPageUrl, std::vector<GURL>{kIconUrl},
+ content::FaviconURL::FAVICON, _, _, _))
+ .WillOnce(DoAll(SaveArg<4>(&update_mappings_callback), Return(2)));
+
+ // Mimic a page load.
+ TestDidLoadPage(kPageUrl,
+ {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
+ kEmptyIconSizes)});
+
+ // Mimic an empty response from FaviconService::GetFaviconForPageURL().
+ ASSERT_FALSE(get_favicon_callback.is_null());
+ get_favicon_callback.Run(kEmptyRawBitmapResult);
+
+ // Mimic a response from FaviconService::UpdateFaviconMappingsAndFetch.
+ ASSERT_FALSE(update_mappings_callback.is_null());
+ update_mappings_callback.Run(kEmptyRawBitmapResult);
+
+ // Mimic the completion of an image download.
+ EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage(
+ kIconUrl, 200, kEmptyIcons, kEmptyIconSizes));
+}
- std::vector<SkBitmap> empty_icons;
- std::vector<gfx::Size> empty_icon_sizes;
- int download_id = 0;
+TEST_F(ContentFaviconDriverTest, ShouldCacheMissingFaviconFor404) {
+ // Set up expectations for FaviconService, and store callbacks for later
+ // triggering.
+ EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
+ .WillOnce(Return(false));
+ EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconUrl));
+
+ favicon_base::FaviconResultsCallback get_favicon_callback;
+ EXPECT_CALL(favicon_service_,
+ GetFaviconForPageURL(kPageUrl, favicon_base::FAVICON, _, _, _))
+ .WillOnce(DoAll(SaveArg<3>(&get_favicon_callback), Return(1)));
+
+ favicon_base::FaviconResultsCallback update_mappings_callback;
+ EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
+ kPageUrl, std::vector<GURL>{kIconUrl},
+ content::FaviconURL::FAVICON, _, _, _))
+ .WillOnce(DoAll(SaveArg<4>(&update_mappings_callback), Return(2)));
+
+ // Mimic a page load.
+ TestDidLoadPage(kPageUrl,
+ {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
+ kEmptyIconSizes)});
+
+ // Mimic an empty response from FaviconService::GetFaviconForPageURL().
+ ASSERT_FALSE(get_favicon_callback.is_null());
+ get_favicon_callback.Run(kEmptyRawBitmapResult);
+
+ // Mimic a response from FaviconService::UpdateFaviconMappingsAndFetch.
+ ASSERT_FALSE(update_mappings_callback.is_null());
+ update_mappings_callback.Run(kEmptyRawBitmapResult);
+
+ // Mimic the completion of an image download.
+ EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage(
+ kIconUrl, 404, kEmptyIcons, kEmptyIconSizes));
+}
- // Try to download missing icon.
- EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url))
+TEST_F(ContentFaviconDriverTest, ShouldNotCacheMissingFaviconFor503) {
+ // Set up expectations for FaviconService, and store callbacks for later
+ // triggering. No calls expected to UnableToDownloadFavicon() because of
+ // HTTP 503 status.
+ EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
.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_);
+ favicon_base::FaviconResultsCallback get_favicon_callback;
+ EXPECT_CALL(favicon_service_,
+ GetFaviconForPageURL(kPageUrl, favicon_base::FAVICON, _, _, _))
+ .WillOnce(DoAll(SaveArg<3>(&get_favicon_callback), Return(1)));
+
+ favicon_base::FaviconResultsCallback update_mappings_callback;
+ EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
+ kPageUrl, std::vector<GURL>{kIconUrl},
+ content::FaviconURL::FAVICON, _, _, _))
+ .WillOnce(DoAll(SaveArg<4>(&update_mappings_callback), Return(2)));
+
+ // Mimic a page load.
+ TestDidLoadPage(kPageUrl,
+ {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
+ kEmptyIconSizes)});
+
+ // Mimic an empty response from FaviconService::GetFaviconForPageURL().
+ ASSERT_FALSE(get_favicon_callback.is_null());
+ get_favicon_callback.Run(kEmptyRawBitmapResult);
+
+ // Mimic a response from FaviconService::UpdateFaviconMappingsAndFetch.
+ ASSERT_FALSE(update_mappings_callback.is_null());
+ update_mappings_callback.Run(kEmptyRawBitmapResult);
+
+ // Mimic the completion of an image download.
+ EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage(
+ kIconUrl, 503, kEmptyIcons, kEmptyIconSizes));
+}
- // 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))
+// Test that Favicon is not requested repeatedly during the same session if
+// the favicon is known to be unavailable (e.g. due to HTTP 404 status).
+TEST_F(ContentFaviconDriverTest, ShouldNotRepequestRepeatedlyIfUnavailable) {
+ // Set up expectations for FaviconService, and store callbacks for later
+ // triggering.
+ EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
.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_);
+
+ favicon_base::FaviconResultsCallback get_favicon_callback;
+ EXPECT_CALL(favicon_service_,
+ GetFaviconForPageURL(kPageUrl, favicon_base::FAVICON, _, _, _))
+ .WillOnce(DoAll(SaveArg<3>(&get_favicon_callback), Return(1)));
+
+ favicon_base::FaviconResultsCallback update_mappings_callback;
+ EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
+ kPageUrl, std::vector<GURL>{kIconUrl},
+ content::FaviconURL::FAVICON, _, _, _))
+ .WillOnce(DoAll(SaveArg<4>(&update_mappings_callback), Return(2)));
+
+ // Mimic a page load.
+ TestDidLoadPage(kPageUrl,
+ {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
+ kEmptyIconSizes)});
+
+ // Mimic an empty response from FaviconService::GetFaviconForPageURL().
+ ASSERT_FALSE(get_favicon_callback.is_null());
+ get_favicon_callback.Run(kEmptyRawBitmapResult);
+
+ // Mimic a response from FaviconService::UpdateFaviconMappingsAndFetch.
+ ASSERT_FALSE(update_mappings_callback.is_null());
+ update_mappings_callback.Run(kEmptyRawBitmapResult);
+
+ // Verify that no download request is pending for the image.
+ EXPECT_FALSE(web_contents_tester()->TestDidDownloadImage(
+ kIconUrl, 200, kEmptyIcons, kEmptyIconSizes));
}
// Test that ContentFaviconDriver ignores updated favicon URLs if there is no
« no previous file with comments | « no previous file | components/favicon/core/favicon_handler_unittest.cc » ('j') | content/public/test/web_contents_tester.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698