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 that UnableToDownloadFavicon() is not called as a result of a favicon |
| 74 // download with 200 status. |
| 75 TEST_F(ContentFaviconDriverTest, ShouldNotCallUnableToDownloadFaviconFor200) { |
| 76 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconURL)).Times(0); |
| 77 // Mimic a page load. |
| 78 TestFetchFaviconForPage( |
| 79 kPageURL, |
| 80 {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, |
| 81 kEmptyIconSizes)}); |
| 82 // Completing the download should not cause a call to |
| 83 // UnableToDownloadFavicon(). |
| 84 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
| 85 kIconURL, 200, kEmptyIcons, kEmptyIconSizes)); |
| 86 } |
| 87 |
| 88 // Test that UnableToDownloadFavicon() is called as a result of a favicon |
| 89 // download with 404 status. |
| 90 TEST_F(ContentFaviconDriverTest, ShouldCallUnableToDownloadFaviconFor404) { |
| 91 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconURL)); |
| 92 // Mimic a page load. |
| 93 TestFetchFaviconForPage( |
| 94 kPageURL, |
| 95 {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, |
| 96 kEmptyIconSizes)}); |
| 97 // Mimic the completion of an image download. |
| 98 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
| 99 kIconURL, 404, kEmptyIcons, kEmptyIconSizes)); |
| 100 } |
| 101 |
| 102 // Test that UnableToDownloadFavicon() is not called as a result of a favicon |
| 103 // download with 503 status. |
| 104 TEST_F(ContentFaviconDriverTest, ShouldNotCallUnableToDownloadFaviconFor503) { |
| 105 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kIconURL)).Times(0); |
| 106 // Mimic a page load. |
| 107 TestFetchFaviconForPage( |
| 108 kPageURL, |
| 109 {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, |
| 110 kEmptyIconSizes)}); |
| 111 // Completing the download should not cause a call to |
| 112 // UnableToDownloadFavicon(). |
| 113 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
| 114 kIconURL, 503, kEmptyIcons, kEmptyIconSizes)); |
| 115 } |
| 116 |
44 // Test that Favicon is not requested repeatedly during the same session if | 117 // Test that Favicon is not requested repeatedly during the same session if |
45 // server returns HTTP 404 status. | 118 // the favicon is known to be unavailable (e.g. due to HTTP 404 status). |
46 TEST_F(ContentFaviconDriverTest, UnableToDownloadFavicon) { | 119 TEST_F(ContentFaviconDriverTest, ShouldNotRequestRepeatedlyIfUnavailable) { |
47 const GURL missing_icon_url("http://www.google.com/favicon.ico"); | 120 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kIconURL)) |
48 | 121 .WillByDefault(Return(true)); |
49 ContentFaviconDriver* content_favicon_driver = | 122 // Mimic a page load. |
50 ContentFaviconDriver::FromWebContents(web_contents()); | 123 TestFetchFaviconForPage( |
51 | 124 kPageURL, |
52 std::vector<SkBitmap> empty_icons; | 125 {content::FaviconURL(kIconURL, content::FaviconURL::FAVICON, |
53 std::vector<gfx::Size> empty_icon_sizes; | 126 kEmptyIconSizes)}); |
54 int download_id = 0; | 127 // Verify that no download request is pending for the image. |
55 | 128 EXPECT_FALSE(web_contents_tester()->HasPendingDownloadImage(kIconURL)); |
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 } | 129 } |
89 | 130 |
90 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no | 131 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no |
91 // last committed entry. This occurs when script is injected in about:blank. | 132 // last committed entry. This occurs when script is injected in about:blank. |
92 // See crbug.com/520759 for more details | 133 // See crbug.com/520759 for more details |
93 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { | 134 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { |
94 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); | 135 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); |
95 | 136 |
96 std::vector<content::FaviconURL> favicon_urls; | 137 std::vector<content::FaviconURL> favicon_urls; |
97 favicon_urls.push_back(content::FaviconURL( | 138 favicon_urls.push_back(content::FaviconURL( |
98 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, | 139 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, |
99 std::vector<gfx::Size>())); | 140 std::vector<gfx::Size>())); |
100 favicon::ContentFaviconDriver* driver = | 141 favicon::ContentFaviconDriver* driver = |
101 favicon::ContentFaviconDriver::FromWebContents(web_contents()); | 142 favicon::ContentFaviconDriver::FromWebContents(web_contents()); |
102 static_cast<content::WebContentsObserver*>(driver) | 143 static_cast<content::WebContentsObserver*>(driver) |
103 ->DidUpdateFaviconURL(favicon_urls); | 144 ->DidUpdateFaviconURL(favicon_urls); |
104 | 145 |
105 // Test that ContentFaviconDriver ignored the favicon url update. | 146 // Test that ContentFaviconDriver ignored the favicon url update. |
106 EXPECT_TRUE(driver->favicon_urls().empty()); | 147 EXPECT_TRUE(driver->favicon_urls().empty()); |
107 } | 148 } |
108 | 149 |
109 } // namespace | 150 } // namespace |
110 } // namespace favicon | 151 } // namespace favicon |
OLD | NEW |