OLD | NEW |
---|---|
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_, UpdateFaviconMappingsAndFetch(_, _, _, _, _, _)) | |
40 .WillByDefault(PostReply<6>(kEmptyRawBitmapResult)); | |
41 ON_CALL(favicon_service_, GetFaviconForPageURL(_, _, _, _, _)) | |
42 .WillByDefault(PostReply<5>(kEmptyRawBitmapResult)); | |
43 } | |
30 | 44 |
31 ~ContentFaviconDriverTest() override {} | 45 ~ContentFaviconDriverTest() override {} |
32 | 46 |
33 // content::RenderViewHostTestHarness: | 47 // content::RenderViewHostTestHarness: |
34 void SetUp() override { | 48 void SetUp() override { |
35 RenderViewHostTestHarness::SetUp(); | 49 RenderViewHostTestHarness::SetUp(); |
36 | 50 |
37 ContentFaviconDriver::CreateForWebContents( | 51 ContentFaviconDriver::CreateForWebContents( |
38 web_contents(), &favicon_service_, nullptr, nullptr); | 52 web_contents(), &favicon_service_, nullptr, nullptr); |
39 } | 53 } |
40 | 54 |
41 testing::StrictMock<MockFaviconService> favicon_service_; | 55 content::WebContentsTester* web_contents_tester() { |
56 return content::WebContentsTester::For(web_contents()); | |
57 } | |
58 | |
59 void TestFetchFaviconForPage( | |
60 const GURL& page_url, | |
61 const std::vector<content::FaviconURL>& candidates) { | |
62 ContentFaviconDriver* favicon_driver = | |
63 ContentFaviconDriver::FromWebContents(web_contents()); | |
64 web_contents_tester()->NavigateAndCommit(page_url); | |
65 static_cast<content::WebContentsObserver*>(favicon_driver) | |
66 ->DidUpdateFaviconURL(candidates); | |
67 base::RunLoop().RunUntilIdle(); | |
68 } | |
69 | |
70 testing::NiceMock<MockFaviconService> favicon_service_; | |
42 }; | 71 }; |
43 | 72 |
73 TEST_F(ContentFaviconDriverTest, ShouldCacheAvailableFavicon) { | |
pkotwicz
2017/02/23 16:20:49
How about for the test comment:
"Test that UnableT
mastiz
2017/02/24 21:15:38
Done.
See comment below about naming convention.
pkotwicz
2017/02/27 15:29:07
It is weird that FaviconService::WasUnableToDownlo
mastiz
2017/02/27 21:05:57
Acknowledged.
| |
74 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch( | |
75 kPageURL, std::vector<GURL>{kIconURL}, | |
76 content::FaviconURL::FAVICON, _, _, _)); | |
77 // Mimic a page load. | |
78 TestFetchFaviconForPage( | |
79 kPageURL, {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, | |
80 kEmptyIconSizes)}); | |
81 // Mimic the completion of an image download. | |
82 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( | |
83 kIconURL, 200, kEmptyIcons, kEmptyIconSizes)); | |
84 } | |
85 | |
pkotwicz
2017/02/23 16:20:49
How about for the test comment:
"Test that UnableT
mastiz
2017/02/24 21:15:38
Done. However, I must say that I like the ShouldXX
mastiz
2017/02/27 21:05:56
Ping? Should I rename tests back to ShouldXXX form
pkotwicz
2017/02/27 23:58:21
The current form of this particular test is Should
| |
86 TEST_F(ContentFaviconDriverTest, ShouldCacheMissingFaviconFor404) { | |
87 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch( | |
88 kPageURL, std::vector<GURL>{kIconURL}, | |
89 content::FaviconURL::FAVICON, _, _, _)); | |
90 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconURL)); | |
91 // Mimic a page load. | |
92 TestFetchFaviconForPage( | |
93 kPageURL, {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, | |
94 kEmptyIconSizes)}); | |
95 // Mimic the completion of an image download. | |
96 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( | |
97 kIconURL, 404, kEmptyIcons, kEmptyIconSizes)); | |
98 } | |
99 | |
pkotwicz
2017/02/23 16:20:49
How about for the test comment:
"Test that UnableT
mastiz
2017/02/24 21:15:38
Done. Ditto about the naming convention.
pkotwicz
2017/02/27 15:29:07
I don't mind the test names:
- ShouldNotCallUnable
mastiz
2017/02/27 21:05:57
Done, renamed.
| |
100 TEST_F(ContentFaviconDriverTest, ShouldNotCacheMissingFaviconFor503) { | |
101 // No calls expected to UnableToDownloadFavicon() because of HTTP 503 status. | |
102 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconURL)).Times(0); | |
103 // Mimic a page load. | |
104 TestFetchFaviconForPage( | |
105 kPageURL, {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, | |
106 kEmptyIconSizes)}); | |
107 // Mimic the completion of an image download. | |
108 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( | |
109 kIconURL, 503, kEmptyIcons, kEmptyIconSizes)); | |
110 } | |
111 | |
44 // Test that Favicon is not requested repeatedly during the same session if | 112 // Test that Favicon is not requested repeatedly during the same session if |
45 // server returns HTTP 404 status. | 113 // the favicon is known to be unavailable (e.g. due to HTTP 404 status). |
46 TEST_F(ContentFaviconDriverTest, UnableToDownloadFavicon) { | 114 TEST_F(ContentFaviconDriverTest, ShouldNotRepequestRepeatedlyIfUnavailable) { |
47 const GURL missing_icon_url("http://www.google.com/favicon.ico"); | 115 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL)) |
48 | 116 .WillByDefault(Return(true)); |
49 ContentFaviconDriver* content_favicon_driver = | 117 // Mimic a page load. |
50 ContentFaviconDriver::FromWebContents(web_contents()); | 118 TestFetchFaviconForPage( |
51 | 119 kPageURL, {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, |
52 std::vector<SkBitmap> empty_icons; | 120 kEmptyIconSizes)}); |
53 std::vector<gfx::Size> empty_icon_sizes; | 121 // Verify that no download request is pending for the image. |
54 int download_id = 0; | 122 EXPECT_FALSE(web_contents_tester()->HasPendingDownloadImage(kIconURL)); |
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)); | |
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 } | 123 } |
89 | 124 |
90 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no | 125 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no |
91 // last committed entry. This occurs when script is injected in about:blank. | 126 // last committed entry. This occurs when script is injected in about:blank. |
92 // See crbug.com/520759 for more details | 127 // See crbug.com/520759 for more details |
93 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { | 128 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { |
94 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); | 129 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); |
95 | 130 |
96 std::vector<content::FaviconURL> favicon_urls; | 131 std::vector<content::FaviconURL> favicon_urls; |
97 favicon_urls.push_back(content::FaviconURL( | 132 favicon_urls.push_back(content::FaviconURL( |
98 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, | 133 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, |
99 std::vector<gfx::Size>())); | 134 std::vector<gfx::Size>())); |
100 favicon::ContentFaviconDriver* driver = | 135 favicon::ContentFaviconDriver* driver = |
101 favicon::ContentFaviconDriver::FromWebContents(web_contents()); | 136 favicon::ContentFaviconDriver::FromWebContents(web_contents()); |
102 static_cast<content::WebContentsObserver*>(driver) | 137 static_cast<content::WebContentsObserver*>(driver) |
103 ->DidUpdateFaviconURL(favicon_urls); | 138 ->DidUpdateFaviconURL(favicon_urls); |
104 | 139 |
105 // Test that ContentFaviconDriver ignored the favicon url update. | 140 // Test that ContentFaviconDriver ignored the favicon url update. |
106 EXPECT_TRUE(driver->favicon_urls().empty()); | 141 EXPECT_TRUE(driver->favicon_urls().empty()); |
107 } | 142 } |
108 | 143 |
109 } // namespace | 144 } // namespace |
110 } // namespace favicon | 145 } // namespace favicon |
OLD | NEW |