| 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 | 9 |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 10 #include "components/favicon/core/favicon_client.h" | 11 #include "components/favicon/core/favicon_client.h" |
| 11 #include "components/favicon/core/favicon_handler.h" | 12 #include "components/favicon/core/test/mock_favicon_service.h" |
| 12 #include "components/favicon/core/favicon_service.h" | |
| 13 #include "content/public/browser/web_contents_observer.h" | 13 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "content/public/common/favicon_url.h" | 14 #include "content/public/common/favicon_url.h" |
| 15 #include "content/public/test/test_renderer_host.h" | 15 #include "content/public/test/test_renderer_host.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 #include "ui/gfx/favicon_size.h" | 19 #include "ui/gfx/favicon_size.h" |
| 19 | 20 |
| 20 namespace favicon { | 21 namespace favicon { |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 24 using testing::Mock; |
| 25 using testing::Return; |
| 26 |
| 23 class ContentFaviconDriverTest : public content::RenderViewHostTestHarness { | 27 class ContentFaviconDriverTest : public content::RenderViewHostTestHarness { |
| 24 public: | 28 protected: |
| 25 ContentFaviconDriverTest() {} | 29 ContentFaviconDriverTest() {} |
| 26 | 30 |
| 27 ~ContentFaviconDriverTest() override {} | 31 ~ContentFaviconDriverTest() override {} |
| 28 | 32 |
| 29 // content::RenderViewHostTestHarness: | 33 // content::RenderViewHostTestHarness: |
| 30 void SetUp() override { | 34 void SetUp() override { |
| 31 RenderViewHostTestHarness::SetUp(); | 35 RenderViewHostTestHarness::SetUp(); |
| 32 | 36 |
| 33 favicon_service_.reset(new FaviconService(nullptr, nullptr)); | |
| 34 ContentFaviconDriver::CreateForWebContents( | 37 ContentFaviconDriver::CreateForWebContents( |
| 35 web_contents(), favicon_service(), nullptr, nullptr); | 38 web_contents(), &favicon_service_, nullptr, nullptr); |
| 36 } | 39 } |
| 37 | 40 |
| 38 FaviconService* favicon_service() { | 41 testing::StrictMock<MockFaviconService> favicon_service_; |
| 39 return favicon_service_.get(); | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 std::unique_ptr<FaviconService> favicon_service_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(ContentFaviconDriverTest); | |
| 46 }; | 42 }; |
| 47 | 43 |
| 48 // Test that Favicon is not requested repeatedly during the same session if | 44 // Test that Favicon is not requested repeatedly during the same session if |
| 49 // server returns HTTP 404 status. | 45 // server returns HTTP 404 status. |
| 50 TEST_F(ContentFaviconDriverTest, UnableToDownloadFavicon) { | 46 TEST_F(ContentFaviconDriverTest, UnableToDownloadFavicon) { |
| 51 const GURL missing_icon_url("http://www.google.com/favicon.ico"); | 47 const GURL missing_icon_url("http://www.google.com/favicon.ico"); |
| 52 const GURL another_icon_url("http://www.youtube.com/favicon.ico"); | |
| 53 | 48 |
| 54 ContentFaviconDriver* content_favicon_driver = | 49 ContentFaviconDriver* content_favicon_driver = |
| 55 ContentFaviconDriver::FromWebContents(web_contents()); | 50 ContentFaviconDriver::FromWebContents(web_contents()); |
| 56 | 51 |
| 57 std::vector<SkBitmap> empty_icons; | 52 std::vector<SkBitmap> empty_icons; |
| 58 std::vector<gfx::Size> empty_icon_sizes; | 53 std::vector<gfx::Size> empty_icon_sizes; |
| 59 int download_id = 0; | 54 int download_id = 0; |
| 60 | 55 |
| 61 // Try to download missing icon. | 56 // Try to download missing icon. |
| 57 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) |
| 58 .WillOnce(Return(false)); |
| 62 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 59 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
| 63 EXPECT_NE(0, download_id); | 60 EXPECT_NE(0, download_id); |
| 64 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | |
| 65 | 61 |
| 66 // Report download failure with HTTP 503 status. | 62 // Report download failure with HTTP 503 status. |
| 67 content_favicon_driver->DidDownloadFavicon(download_id, 503, missing_icon_url, | 63 content_favicon_driver->DidDownloadFavicon(download_id, 503, missing_icon_url, |
| 68 empty_icons, empty_icon_sizes); | 64 empty_icons, empty_icon_sizes); |
| 69 // Icon is not marked as UnableToDownload as HTTP status is not 404. | 65 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 70 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | |
| 71 | 66 |
| 72 // Try to download again. | 67 // Try to download again. |
| 68 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) |
| 69 .WillOnce(Return(false)); |
| 73 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 70 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
| 74 EXPECT_NE(0, download_id); | 71 EXPECT_NE(0, download_id); |
| 75 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 72 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 76 | 73 |
| 77 // Report download failure with HTTP 404 status. | 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)); |
| 78 content_favicon_driver->DidDownloadFavicon(download_id, 404, missing_icon_url, | 77 content_favicon_driver->DidDownloadFavicon(download_id, 404, missing_icon_url, |
| 79 empty_icons, empty_icon_sizes); | 78 empty_icons, empty_icon_sizes); |
| 80 // Icon is marked as UnableToDownload. | 79 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 81 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | |
| 82 | 80 |
| 83 // Try to download again. | 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); | 84 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
| 85 // Download is not started and Icon is still marked as UnableToDownload. | 85 // Download is not started and Icon is still marked as UnableToDownload. |
| 86 EXPECT_EQ(0, download_id); | 86 EXPECT_EQ(0, download_id); |
| 87 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 87 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 88 | |
| 89 // Try to download another icon. | |
| 90 download_id = content_favicon_driver->StartDownload(another_icon_url, 0); | |
| 91 // Download is started as another icon URL is not same as missing_icon_url. | |
| 92 EXPECT_NE(0, download_id); | |
| 93 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url)); | |
| 94 | |
| 95 // Clear the list of missing icons. | |
| 96 favicon_service()->ClearUnableToDownloadFavicons(); | |
| 97 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | |
| 98 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url)); | |
| 99 | |
| 100 // Try to download again. | |
| 101 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | |
| 102 EXPECT_NE(0, download_id); | |
| 103 // Report download success with HTTP 200 status. | |
| 104 content_favicon_driver->DidDownloadFavicon(download_id, 200, missing_icon_url, | |
| 105 empty_icons, empty_icon_sizes); | |
| 106 // Icon is not marked as UnableToDownload as HTTP status is not 404. | |
| 107 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | |
| 108 | |
| 109 favicon_service()->Shutdown(); | |
| 110 } | 88 } |
| 111 | 89 |
| 112 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no | 90 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no |
| 113 // last committed entry. This occurs when script is injected in about:blank. | 91 // last committed entry. This occurs when script is injected in about:blank. |
| 114 // See crbug.com/520759 for more details | 92 // See crbug.com/520759 for more details |
| 115 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { | 93 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { |
| 116 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); | 94 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); |
| 117 | 95 |
| 118 std::vector<content::FaviconURL> favicon_urls; | 96 std::vector<content::FaviconURL> favicon_urls; |
| 119 favicon_urls.push_back(content::FaviconURL( | 97 favicon_urls.push_back(content::FaviconURL( |
| 120 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, | 98 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, |
| 121 std::vector<gfx::Size>())); | 99 std::vector<gfx::Size>())); |
| 122 favicon::ContentFaviconDriver* driver = | 100 favicon::ContentFaviconDriver* driver = |
| 123 favicon::ContentFaviconDriver::FromWebContents(web_contents()); | 101 favicon::ContentFaviconDriver::FromWebContents(web_contents()); |
| 124 static_cast<content::WebContentsObserver*>(driver) | 102 static_cast<content::WebContentsObserver*>(driver) |
| 125 ->DidUpdateFaviconURL(favicon_urls); | 103 ->DidUpdateFaviconURL(favicon_urls); |
| 126 | 104 |
| 127 // Test that ContentFaviconDriver ignored the favicon url update. | 105 // Test that ContentFaviconDriver ignored the favicon url update. |
| 128 EXPECT_TRUE(driver->favicon_urls().empty()); | 106 EXPECT_TRUE(driver->favicon_urls().empty()); |
| 129 } | 107 } |
| 130 | 108 |
| 131 } // namespace | 109 } // namespace |
| 132 } // namespace favicon | 110 } // namespace favicon |
| OLD | NEW |