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

Side by Side 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 unified diff | Download patch
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 "components/favicon/core/favicon_client.h" 11 #include "components/favicon/core/favicon_client.h"
12 #include "components/favicon/core/favicon_handler.h"
12 #include "components/favicon/core/test/mock_favicon_service.h" 13 #include "components/favicon/core/test/mock_favicon_service.h"
13 #include "content/public/browser/web_contents_observer.h" 14 #include "content/public/browser/web_contents_observer.h"
14 #include "content/public/common/favicon_url.h" 15 #include "content/public/common/favicon_url.h"
15 #include "content/public/test/test_renderer_host.h" 16 #include "content/public/test/test_renderer_host.h"
17 #include "content/public/test/web_contents_tester.h"
16 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/skia/include/core/SkBitmap.h" 20 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/gfx/favicon_size.h" 21 #include "ui/gfx/favicon_size.h"
20 22
21 namespace favicon { 23 namespace favicon {
22 namespace { 24 namespace {
23 25
24 using testing::Mock; 26 using testing::DoAll;
25 using testing::Return; 27 using testing::Return;
28 using testing::SaveArg;
29 using testing::_;
26 30
27 class ContentFaviconDriverTest : public content::RenderViewHostTestHarness { 31 class ContentFaviconDriverTest : public content::RenderViewHostTestHarness {
28 protected: 32 protected:
33 const std::vector<gfx::Size> kEmptyIconSizes;
34 const std::vector<SkBitmap> kEmptyIcons;
35 const std::vector<favicon_base::FaviconRawBitmapResult> kEmptyRawBitmapResult;
36 const GURL kPageUrl = GURL("http://www.google.com/");
37 const GURL kIconUrl = GURL("http://www.google.com/favicon.ico");
38
29 ContentFaviconDriverTest() {} 39 ContentFaviconDriverTest() {}
30
31 ~ContentFaviconDriverTest() override {} 40 ~ContentFaviconDriverTest() override {}
32 41
33 // content::RenderViewHostTestHarness: 42 // content::RenderViewHostTestHarness:
34 void SetUp() override { 43 void SetUp() override {
35 RenderViewHostTestHarness::SetUp(); 44 RenderViewHostTestHarness::SetUp();
36 45
37 ContentFaviconDriver::CreateForWebContents( 46 ContentFaviconDriver::CreateForWebContents(
38 web_contents(), &favicon_service_, nullptr, nullptr); 47 web_contents(), &favicon_service_, nullptr, nullptr);
39 } 48 }
40 49
50 content::WebContentsTester* web_contents_tester() {
51 return content::WebContentsTester::For(web_contents());
52 }
53
54 void TestDidLoadPage(const GURL& page_url,
55 const std::vector<content::FaviconURL>& candidates) {
56 ContentFaviconDriver* favicon_driver =
57 ContentFaviconDriver::FromWebContents(web_contents());
58 web_contents_tester()->NavigateAndCommit(page_url);
59 static_cast<content::WebContentsObserver*>(favicon_driver)
60 ->DidUpdateFaviconURL(candidates);
61 }
62
41 testing::StrictMock<MockFaviconService> favicon_service_; 63 testing::StrictMock<MockFaviconService> favicon_service_;
42 }; 64 };
43 65
66 TEST_F(ContentFaviconDriverTest, ShouldCacheAvailableFavicon) {
67 // Set up expectations for FaviconService, and store callbacks for later
68 // triggering.
69 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
70 .WillOnce(Return(false));
71
72 favicon_base::FaviconResultsCallback get_favicon_callback;
73 EXPECT_CALL(favicon_service_,
74 GetFaviconForPageURL(kPageUrl, favicon_base::FAVICON, _, _, _))
75 .WillOnce(DoAll(SaveArg<3>(&get_favicon_callback), Return(1)));
76
77 favicon_base::FaviconResultsCallback update_mappings_callback;
78 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
79 kPageUrl, std::vector<GURL>{kIconUrl},
80 content::FaviconURL::FAVICON, _, _, _))
81 .WillOnce(DoAll(SaveArg<4>(&update_mappings_callback), Return(2)));
82
83 // Mimic a page load.
84 TestDidLoadPage(kPageUrl,
85 {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
86 kEmptyIconSizes)});
87
88 // Mimic an empty response from FaviconService::GetFaviconForPageURL().
89 ASSERT_FALSE(get_favicon_callback.is_null());
90 get_favicon_callback.Run(kEmptyRawBitmapResult);
91
92 // Mimic a response from FaviconService::UpdateFaviconMappingsAndFetch.
93 ASSERT_FALSE(update_mappings_callback.is_null());
94 update_mappings_callback.Run(kEmptyRawBitmapResult);
95
96 // Mimic the completion of an image download.
97 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage(
98 kIconUrl, 200, kEmptyIcons, kEmptyIconSizes));
99 }
100
101 TEST_F(ContentFaviconDriverTest, ShouldCacheMissingFaviconFor404) {
102 // Set up expectations for FaviconService, and store callbacks for later
103 // triggering.
104 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
105 .WillOnce(Return(false));
106 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconUrl));
107
108 favicon_base::FaviconResultsCallback get_favicon_callback;
109 EXPECT_CALL(favicon_service_,
110 GetFaviconForPageURL(kPageUrl, favicon_base::FAVICON, _, _, _))
111 .WillOnce(DoAll(SaveArg<3>(&get_favicon_callback), Return(1)));
112
113 favicon_base::FaviconResultsCallback update_mappings_callback;
114 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
115 kPageUrl, std::vector<GURL>{kIconUrl},
116 content::FaviconURL::FAVICON, _, _, _))
117 .WillOnce(DoAll(SaveArg<4>(&update_mappings_callback), Return(2)));
118
119 // Mimic a page load.
120 TestDidLoadPage(kPageUrl,
121 {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
122 kEmptyIconSizes)});
123
124 // Mimic an empty response from FaviconService::GetFaviconForPageURL().
125 ASSERT_FALSE(get_favicon_callback.is_null());
126 get_favicon_callback.Run(kEmptyRawBitmapResult);
127
128 // Mimic a response from FaviconService::UpdateFaviconMappingsAndFetch.
129 ASSERT_FALSE(update_mappings_callback.is_null());
130 update_mappings_callback.Run(kEmptyRawBitmapResult);
131
132 // Mimic the completion of an image download.
133 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage(
134 kIconUrl, 404, kEmptyIcons, kEmptyIconSizes));
135 }
136
137 TEST_F(ContentFaviconDriverTest, ShouldNotCacheMissingFaviconFor503) {
138 // Set up expectations for FaviconService, and store callbacks for later
139 // triggering. No calls expected to UnableToDownloadFavicon() because of
140 // HTTP 503 status.
141 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
142 .WillOnce(Return(false));
143
144 favicon_base::FaviconResultsCallback get_favicon_callback;
145 EXPECT_CALL(favicon_service_,
146 GetFaviconForPageURL(kPageUrl, favicon_base::FAVICON, _, _, _))
147 .WillOnce(DoAll(SaveArg<3>(&get_favicon_callback), Return(1)));
148
149 favicon_base::FaviconResultsCallback update_mappings_callback;
150 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
151 kPageUrl, std::vector<GURL>{kIconUrl},
152 content::FaviconURL::FAVICON, _, _, _))
153 .WillOnce(DoAll(SaveArg<4>(&update_mappings_callback), Return(2)));
154
155 // Mimic a page load.
156 TestDidLoadPage(kPageUrl,
157 {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
158 kEmptyIconSizes)});
159
160 // Mimic an empty response from FaviconService::GetFaviconForPageURL().
161 ASSERT_FALSE(get_favicon_callback.is_null());
162 get_favicon_callback.Run(kEmptyRawBitmapResult);
163
164 // Mimic a response from FaviconService::UpdateFaviconMappingsAndFetch.
165 ASSERT_FALSE(update_mappings_callback.is_null());
166 update_mappings_callback.Run(kEmptyRawBitmapResult);
167
168 // Mimic the completion of an image download.
169 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage(
170 kIconUrl, 503, kEmptyIcons, kEmptyIconSizes));
171 }
172
44 // Test that Favicon is not requested repeatedly during the same session if 173 // Test that Favicon is not requested repeatedly during the same session if
45 // server returns HTTP 404 status. 174 // the favicon is known to be unavailable (e.g. due to HTTP 404 status).
46 TEST_F(ContentFaviconDriverTest, UnableToDownloadFavicon) { 175 TEST_F(ContentFaviconDriverTest, ShouldNotRepequestRepeatedlyIfUnavailable) {
47 const GURL missing_icon_url("http://www.google.com/favicon.ico"); 176 // Set up expectations for FaviconService, and store callbacks for later
177 // triggering.
178 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconUrl))
179 .WillOnce(Return(true));
48 180
49 ContentFaviconDriver* content_favicon_driver = 181 favicon_base::FaviconResultsCallback get_favicon_callback;
50 ContentFaviconDriver::FromWebContents(web_contents()); 182 EXPECT_CALL(favicon_service_,
183 GetFaviconForPageURL(kPageUrl, favicon_base::FAVICON, _, _, _))
184 .WillOnce(DoAll(SaveArg<3>(&get_favicon_callback), Return(1)));
51 185
52 std::vector<SkBitmap> empty_icons; 186 favicon_base::FaviconResultsCallback update_mappings_callback;
53 std::vector<gfx::Size> empty_icon_sizes; 187 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(
54 int download_id = 0; 188 kPageUrl, std::vector<GURL>{kIconUrl},
189 content::FaviconURL::FAVICON, _, _, _))
190 .WillOnce(DoAll(SaveArg<4>(&update_mappings_callback), Return(2)));
55 191
56 // Try to download missing icon. 192 // Mimic a page load.
57 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) 193 TestDidLoadPage(kPageUrl,
58 .WillOnce(Return(false)); 194 {content::FaviconURL(kIconUrl, content::FaviconURL::FAVICON,
59 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); 195 kEmptyIconSizes)});
60 EXPECT_NE(0, download_id);
61 196
62 // Report download failure with HTTP 503 status. 197 // Mimic an empty response from FaviconService::GetFaviconForPageURL().
63 content_favicon_driver->DidDownloadFavicon(download_id, 503, missing_icon_url, 198 ASSERT_FALSE(get_favicon_callback.is_null());
64 empty_icons, empty_icon_sizes); 199 get_favicon_callback.Run(kEmptyRawBitmapResult);
65 Mock::VerifyAndClearExpectations(&favicon_service_);
66 200
67 // Try to download again. 201 // Mimic a response from FaviconService::UpdateFaviconMappingsAndFetch.
68 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) 202 ASSERT_FALSE(update_mappings_callback.is_null());
69 .WillOnce(Return(false)); 203 update_mappings_callback.Run(kEmptyRawBitmapResult);
70 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0);
71 EXPECT_NE(0, download_id);
72 Mock::VerifyAndClearExpectations(&favicon_service_);
73 204
74 // Report download failure with HTTP 404 status, which causes the icon to be 205 // Verify that no download request is pending for the image.
75 // marked as UnableToDownload. 206 EXPECT_FALSE(web_contents_tester()->TestDidDownloadImage(
76 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(missing_icon_url)); 207 kIconUrl, 200, kEmptyIcons, kEmptyIconSizes));
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));
84 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0);
85 // Download is not started and Icon is still marked as UnableToDownload.
86 EXPECT_EQ(0, download_id);
87 Mock::VerifyAndClearExpectations(&favicon_service_);
88 } 208 }
89 209
90 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no 210 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no
91 // last committed entry. This occurs when script is injected in about:blank. 211 // last committed entry. This occurs when script is injected in about:blank.
92 // See crbug.com/520759 for more details 212 // See crbug.com/520759 for more details
93 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { 213 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) {
94 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); 214 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry());
95 215
96 std::vector<content::FaviconURL> favicon_urls; 216 std::vector<content::FaviconURL> favicon_urls;
97 favicon_urls.push_back(content::FaviconURL( 217 favicon_urls.push_back(content::FaviconURL(
98 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, 218 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON,
99 std::vector<gfx::Size>())); 219 std::vector<gfx::Size>()));
100 favicon::ContentFaviconDriver* driver = 220 favicon::ContentFaviconDriver* driver =
101 favicon::ContentFaviconDriver::FromWebContents(web_contents()); 221 favicon::ContentFaviconDriver::FromWebContents(web_contents());
102 static_cast<content::WebContentsObserver*>(driver) 222 static_cast<content::WebContentsObserver*>(driver)
103 ->DidUpdateFaviconURL(favicon_urls); 223 ->DidUpdateFaviconURL(favicon_urls);
104 224
105 // Test that ContentFaviconDriver ignored the favicon url update. 225 // Test that ContentFaviconDriver ignored the favicon url update.
106 EXPECT_TRUE(driver->favicon_urls().empty()); 226 EXPECT_TRUE(driver->favicon_urls().empty());
107 } 227 }
108 228
109 } // namespace 229 } // namespace
110 } // namespace favicon 230 } // namespace favicon
OLDNEW
« 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