Chromium Code Reviews| 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"); | 48 const GURL another_icon_url("http://www.youtube.com/favicon.ico"); |
| 53 | 49 |
| 54 ContentFaviconDriver* content_favicon_driver = | 50 ContentFaviconDriver* content_favicon_driver = |
| 55 ContentFaviconDriver::FromWebContents(web_contents()); | 51 ContentFaviconDriver::FromWebContents(web_contents()); |
| 56 | 52 |
| 57 std::vector<SkBitmap> empty_icons; | 53 std::vector<SkBitmap> empty_icons; |
| 58 std::vector<gfx::Size> empty_icon_sizes; | 54 std::vector<gfx::Size> empty_icon_sizes; |
| 59 int download_id = 0; | 55 int download_id = 0; |
| 60 | 56 |
| 61 // Try to download missing icon. | 57 // Try to download missing icon. |
| 58 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) | |
| 59 .WillOnce(Return(false)); | |
| 62 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 60 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
| 63 EXPECT_NE(0, download_id); | 61 EXPECT_NE(0, download_id); |
| 64 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | |
| 65 | 62 |
| 66 // Report download failure with HTTP 503 status. | 63 // Report download failure with HTTP 503 status. |
| 67 content_favicon_driver->DidDownloadFavicon(download_id, 503, missing_icon_url, | 64 content_favicon_driver->DidDownloadFavicon(download_id, 503, missing_icon_url, |
| 68 empty_icons, empty_icon_sizes); | 65 empty_icons, empty_icon_sizes); |
| 69 // Icon is not marked as UnableToDownload as HTTP status is not 404. | 66 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 70 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | |
| 71 | 67 |
| 72 // Try to download again. | 68 // Try to download again. |
| 69 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) | |
| 70 .WillOnce(Return(false)); | |
| 73 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 71 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
| 74 EXPECT_NE(0, download_id); | 72 EXPECT_NE(0, download_id); |
| 75 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 73 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 76 | 74 |
| 77 // Report download failure with HTTP 404 status. | 75 // Report download failure with HTTP 404 status, which causes the icon to be |
| 76 // marked as UnableToDownload. | |
| 77 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(missing_icon_url)); | |
| 78 content_favicon_driver->DidDownloadFavicon(download_id, 404, missing_icon_url, | 78 content_favicon_driver->DidDownloadFavicon(download_id, 404, missing_icon_url, |
| 79 empty_icons, empty_icon_sizes); | 79 empty_icons, empty_icon_sizes); |
| 80 // Icon is marked as UnableToDownload. | 80 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 81 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | |
| 82 | 81 |
| 83 // Try to download again. | 82 // Try to download again. |
| 83 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) | |
| 84 .WillOnce(Return(true)); | |
| 84 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 85 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
| 85 // Download is not started and Icon is still marked as UnableToDownload. | 86 // Download is not started and Icon is still marked as UnableToDownload. |
| 86 EXPECT_EQ(0, download_id); | 87 EXPECT_EQ(0, download_id); |
| 87 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 88 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 88 | 89 |
| 89 // Try to download another icon. | 90 // Try to download another icon. |
| 91 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(another_icon_url)) | |
| 92 .WillOnce(Return(false)); | |
| 90 download_id = content_favicon_driver->StartDownload(another_icon_url, 0); | 93 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. | 94 // Download is started as another icon URL is not same as missing_icon_url. |
| 92 EXPECT_NE(0, download_id); | 95 EXPECT_NE(0, download_id); |
| 93 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url)); | 96 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 94 | 97 |
|
pkotwicz
2017/02/16 04:07:27
These tests were written with the intention of tes
mastiz
2017/02/16 09:46:25
Since testing FaviconService doesn't belong here,
| |
| 95 // Clear the list of missing icons. | 98 // Try to download again as if the list of missing icons was cleared (i.e. |
| 96 favicon_service()->ClearUnableToDownloadFavicons(); | 99 // WasUnableToDownloadFavicon() returns false). |
| 97 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 100 EXPECT_CALL(favicon_service_, WasUnableToDownloadFavicon(missing_icon_url)) |
| 98 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url)); | 101 .WillOnce(Return(false)); |
| 99 | |
| 100 // Try to download again. | |
| 101 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 102 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); |
| 102 EXPECT_NE(0, download_id); | 103 EXPECT_NE(0, download_id); |
| 103 // Report download success with HTTP 200 status. | 104 // Report download success with HTTP 200 status. |
| 104 content_favicon_driver->DidDownloadFavicon(download_id, 200, missing_icon_url, | 105 content_favicon_driver->DidDownloadFavicon(download_id, 200, missing_icon_url, |
| 105 empty_icons, empty_icon_sizes); | 106 empty_icons, empty_icon_sizes); |
| 106 // Icon is not marked as UnableToDownload as HTTP status is not 404. | 107 // Icon is not marked as UnableToDownload as HTTP status is not 404. |
| 107 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 108 Mock::VerifyAndClearExpectations(&favicon_service_); |
| 108 | |
| 109 favicon_service()->Shutdown(); | |
| 110 } | 109 } |
| 111 | 110 |
| 112 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no | 111 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no |
| 113 // last committed entry. This occurs when script is injected in about:blank. | 112 // last committed entry. This occurs when script is injected in about:blank. |
| 114 // See crbug.com/520759 for more details | 113 // See crbug.com/520759 for more details |
| 115 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { | 114 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { |
| 116 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); | 115 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); |
| 117 | 116 |
| 118 std::vector<content::FaviconURL> favicon_urls; | 117 std::vector<content::FaviconURL> favicon_urls; |
| 119 favicon_urls.push_back(content::FaviconURL( | 118 favicon_urls.push_back(content::FaviconURL( |
| 120 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, | 119 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, |
| 121 std::vector<gfx::Size>())); | 120 std::vector<gfx::Size>())); |
| 122 favicon::ContentFaviconDriver* driver = | 121 favicon::ContentFaviconDriver* driver = |
| 123 favicon::ContentFaviconDriver::FromWebContents(web_contents()); | 122 favicon::ContentFaviconDriver::FromWebContents(web_contents()); |
| 124 static_cast<content::WebContentsObserver*>(driver) | 123 static_cast<content::WebContentsObserver*>(driver) |
| 125 ->DidUpdateFaviconURL(favicon_urls); | 124 ->DidUpdateFaviconURL(favicon_urls); |
| 126 | 125 |
| 127 // Test that ContentFaviconDriver ignored the favicon url update. | 126 // Test that ContentFaviconDriver ignored the favicon url update. |
| 128 EXPECT_TRUE(driver->favicon_urls().empty()); | 127 EXPECT_TRUE(driver->favicon_urls().empty()); |
| 129 } | 128 } |
| 130 | 129 |
| 131 } // namespace | 130 } // namespace |
| 132 } // namespace favicon | 131 } // namespace favicon |
| OLD | NEW |