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

Side by Side Diff: components/favicon/content/content_favicon_driver_unittest.cc

Issue 2697803003: Improve test coverage for ContentFaviconDriver (Closed)
Patch Set: Minor revert. 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 unified diff | Download patch
« no previous file with comments | « no previous file | content/public/test/web_contents_tester.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/content/content_favicon_driver.h" 5 #include "components/favicon/content/content_favicon_driver.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/run_loop.h"
11 #include "components/favicon/core/favicon_client.h" 12 #include "components/favicon/core/favicon_client.h"
13 #include "components/favicon/core/favicon_handler.h"
12 #include "components/favicon/core/test/mock_favicon_service.h" 14 #include "components/favicon/core/test/mock_favicon_service.h"
13 #include "content/public/browser/web_contents_observer.h" 15 #include "content/public/browser/web_contents_observer.h"
14 #include "content/public/common/favicon_url.h" 16 #include "content/public/common/favicon_url.h"
15 #include "content/public/test/test_renderer_host.h" 17 #include "content/public/test/test_renderer_host.h"
18 #include "content/public/test/web_contents_tester.h"
16 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/skia/include/core/SkBitmap.h" 21 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/gfx/favicon_size.h" 22 #include "ui/gfx/favicon_size.h"
20 23
21 namespace favicon { 24 namespace favicon {
22 namespace { 25 namespace {
23 26
24 using testing::Mock;
25 using testing::Return; 27 using testing::Return;
28 using testing::_;
26 29
27 class ContentFaviconDriverTest : public content::RenderViewHostTestHarness { 30 class ContentFaviconDriverTest : public content::RenderViewHostTestHarness {
28 protected: 31 protected:
29 ContentFaviconDriverTest() {} 32 const std::vector<gfx::Size> kEmptyIconSizes;
33 const std::vector<SkBitmap> kEmptyIcons;
34 const std::vector<favicon_base::FaviconRawBitmapResult> kEmptyRawBitmapResult;
35 const GURL kPageUrl = GURL("http://www.google.com/");
36 const GURL kIconUrl = GURL("http://www.google.com/favicon.ico");
37
38 ContentFaviconDriverTest() {
39 ON_CALL(favicon_service_,
40 UpdateFaviconMappingsAndFetch(kPageUrl, std::vector<GURL>{kIconUrl},
41 _, _, _, _))
pkotwicz 2017/02/22 19:34:16 Might as well make all of the parameters testing::
mastiz 2017/02/22 20:53:13 Done.
42 .WillByDefault(PostReply<6>(kEmptyRawBitmapResult));
43 // Let's be "nice" about reads, to avoid boilerplate in tests.
44 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
45 .WillRepeatedly(Return(false));
46 EXPECT_CALL(favicon_service_, GetFaviconForPageURL(kPageUrl, _, _, _, _))
pkotwicz 2017/02/22 19:34:16 Might as well make all of the parameters testing::
mastiz 2017/02/22 20:53:13 Done.
47 .WillRepeatedly(PostReply<5>(kEmptyRawBitmapResult));
pkotwicz 2017/02/22 19:34:16 Can't these be all ON_CALL() ? I think that the E
mastiz 2017/02/22 20:53:13 Done, adopted NiceMock.
48 }
30 49
31 ~ContentFaviconDriverTest() override {} 50 ~ContentFaviconDriverTest() override {}
32 51
33 // content::RenderViewHostTestHarness: 52 // content::RenderViewHostTestHarness:
34 void SetUp() override { 53 void SetUp() override {
35 RenderViewHostTestHarness::SetUp(); 54 RenderViewHostTestHarness::SetUp();
36 55
37 ContentFaviconDriver::CreateForWebContents( 56 ContentFaviconDriver::CreateForWebContents(
38 web_contents(), &favicon_service_, nullptr, nullptr); 57 web_contents(), &favicon_service_, nullptr, nullptr);
39 } 58 }
40 59
60 content::WebContentsTester* web_contents_tester() {
61 return content::WebContentsTester::For(web_contents());
62 }
63
pkotwicz 2017/02/22 19:34:16 Maybe rename this function to FetchFaviconForPage(
mastiz 2017/02/22 20:53:13 Renamed to TestFetchFaviconForPage because otherwi
pkotwicz 2017/02/23 16:20:49 I had not thought about making it obvious which me
64 void TestDidLoadPage(const GURL& page_url,
65 const std::vector<content::FaviconURL>& candidates) {
66 ContentFaviconDriver* favicon_driver =
67 ContentFaviconDriver::FromWebContents(web_contents());
68 web_contents_tester()->NavigateAndCommit(page_url);
69 static_cast<content::WebContentsObserver*>(favicon_driver)
70 ->DidUpdateFaviconURL(candidates);
71 base::RunLoop().RunUntilIdle();
72 }
73
41 testing::StrictMock<MockFaviconService> favicon_service_; 74 testing::StrictMock<MockFaviconService> favicon_service_;
42 }; 75 };
43 76
77 TEST_F(ContentFaviconDriverTest, ShouldCacheAvailableFavicon) {
78 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
79 kPageUrl, std::vector<GURL>{kIconUrl},
80 content::FaviconURL::FAVICON, _, _, _));
81 // Mimic a page load.
82 TestDidLoadPage(kPageUrl,
83 {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
84 kEmptyIconSizes)});
85 // Mimic the completion of an image download.
86 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage(
87 kIconUrl, 200, kEmptyIcons, kEmptyIconSizes));
88 }
89
90 TEST_F(ContentFaviconDriverTest, ShouldCacheMissingFaviconFor404) {
91 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
92 kPageUrl, std::vector<GURL>{kIconUrl},
93 content::FaviconURL::FAVICON, _, _, _));
94 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconUrl));
95 // Mimic a page load.
96 TestDidLoadPage(kPageUrl,
97 {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
98 kEmptyIconSizes)});
99 // Mimic the completion of an image download.
100 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage(
101 kIconUrl, 404, kEmptyIcons, kEmptyIconSizes));
102 }
103
104 TEST_F(ContentFaviconDriverTest, ShouldNotCacheMissingFaviconFor503) {
105 // No calls expected to UnableToDownloadFavicon() because of HTTP 503 status.
106 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
107 kPageUrl, std::vector<GURL>{kIconUrl},
108 content::FaviconURL::FAVICON, _, _, _));
pkotwicz 2017/02/22 19:34:16 This is not testing for calls for UnableToDownload
mastiz 2017/02/22 20:53:13 The test was indeed testing that no calls were mad
pkotwicz 2017/02/23 16:20:49 Thank you for making the check for UnableToDownloa
mastiz 2017/02/24 21:15:38 Done.
109 // Mimic a page load.
110 TestDidLoadPage(kPageUrl,
111 {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
112 kEmptyIconSizes)});
113 // Mimic the completion of an image download.
114 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage(
115 kIconUrl, 503, kEmptyIcons, kEmptyIconSizes));
116 }
117
44 // Test that Favicon is not requested repeatedly during the same session if 118 // Test that Favicon is not requested repeatedly during the same session if
45 // server returns HTTP 404 status. 119 // the favicon is known to be unavailable (e.g. due to HTTP 404 status).
46 TEST_F(ContentFaviconDriverTest, UnableToDownloadFavicon) { 120 TEST_F(ContentFaviconDriverTest, ShouldNotRepequestRepeatedlyIfUnavailable) {
47 const GURL missing_icon_url("http://www.google.com/favicon.ico"); 121 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
pkotwicz 2017/02/22 19:34:16 This should be ON_CALL()
mastiz 2017/02/22 20:53:13 Done.
48
49 ContentFaviconDriver* content_favicon_driver =
50 ContentFaviconDriver::FromWebContents(web_contents());
51
52 std::vector<SkBitmap> empty_icons;
53 std::vector<gfx::Size> empty_icon_sizes;
54 int download_id = 0;
55
56 // Try to download missing icon.
57 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url))
58 .WillOnce(Return(false));
59 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0);
60 EXPECT_NE(0, download_id);
61
62 // Report download failure with HTTP 503 status.
63 content_favicon_driver->DidDownloadFavicon(download_id, 503, missing_icon_url,
64 empty_icons, empty_icon_sizes);
65 Mock::VerifyAndClearExpectations(&favicon_service_);
66
67 // Try to download again.
68 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url))
69 .WillOnce(Return(false));
70 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0);
71 EXPECT_NE(0, download_id);
72 Mock::VerifyAndClearExpectations(&favicon_service_);
73
74 // Report download failure with HTTP 404 status, which causes the icon to be
75 // marked as UnableToDownload.
76 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(missing_icon_url));
77 content_favicon_driver->DidDownloadFavicon(download_id, 404, missing_icon_url,
78 empty_icons, empty_icon_sizes);
79 Mock::VerifyAndClearExpectations(&favicon_service_);
80
81 // Try to download again.
82 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url))
83 .WillOnce(Return(true)); 122 .WillOnce(Return(true));
84 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); 123 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
85 // Download is not started and Icon is still marked as UnableToDownload. 124 kPageUrl, std::vector<GURL>{kIconUrl},
86 EXPECT_EQ(0, download_id); 125 content::FaviconURL::FAVICON, _, _, _));
87 Mock::VerifyAndClearExpectations(&favicon_service_); 126 // Mimic a page load.
127 TestDidLoadPage(kPageUrl,
128 {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
129 kEmptyIconSizes)});
130 // Verify that no download request is pending for the image.
131 EXPECT_FALSE(web_contents_tester()->TestDidDownloadImage(
132 kIconUrl, 200, kEmptyIcons, kEmptyIconSizes));
pkotwicz 2017/02/22 19:34:16 Can we have TestWebContents::DidRequestDownload()
mastiz 2017/02/22 20:53:13 Done.
88 } 133 }
89 134
90 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no 135 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no
91 // last committed entry. This occurs when script is injected in about:blank. 136 // last committed entry. This occurs when script is injected in about:blank.
92 // See crbug.com/520759 for more details 137 // See crbug.com/520759 for more details
93 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { 138 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) {
94 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); 139 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry());
95 140
96 std::vector<content::FaviconURL> favicon_urls; 141 std::vector<content::FaviconURL> favicon_urls;
97 favicon_urls.push_back(content::FaviconURL( 142 favicon_urls.push_back(content::FaviconURL(
98 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, 143 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON,
99 std::vector<gfx::Size>())); 144 std::vector<gfx::Size>()));
100 favicon::ContentFaviconDriver* driver = 145 favicon::ContentFaviconDriver* driver =
101 favicon::ContentFaviconDriver::FromWebContents(web_contents()); 146 favicon::ContentFaviconDriver::FromWebContents(web_contents());
102 static_cast<content::WebContentsObserver*>(driver) 147 static_cast<content::WebContentsObserver*>(driver)
103 ->DidUpdateFaviconURL(favicon_urls); 148 ->DidUpdateFaviconURL(favicon_urls);
104 149
105 // Test that ContentFaviconDriver ignored the favicon url update. 150 // Test that ContentFaviconDriver ignored the favicon url update.
106 EXPECT_TRUE(driver->favicon_urls().empty()); 151 EXPECT_TRUE(driver->favicon_urls().empty());
107 } 152 }
108 153
109 } // namespace 154 } // namespace
110 } // namespace favicon 155 } // namespace favicon
OLDNEW
« no previous file with comments | « no previous file | content/public/test/web_contents_tester.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698