| 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 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "components/favicon/core/favicon_client.h" | 10 #include "components/favicon/core/favicon_client.h" |
| 11 #include "components/favicon/core/favicon_handler.h" | 11 #include "components/favicon/core/favicon_handler.h" |
| 12 #include "components/favicon/core/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 "content/public/test/web_contents_tester.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 |
| 23 class ContentFaviconDriverTest : public content::RenderViewHostTestHarness { | 24 class ContentFaviconDriverTest : public content::RenderViewHostTestHarness { |
| 24 public: | 25 public: |
| 25 ContentFaviconDriverTest() {} | 26 ContentFaviconDriverTest() {} |
| 26 | 27 |
| 27 ~ContentFaviconDriverTest() override {} | 28 ~ContentFaviconDriverTest() override {} |
| 28 | 29 |
| 29 // content::RenderViewHostTestHarness: | 30 // content::RenderViewHostTestHarness: |
| 30 void SetUp() override { | 31 void SetUp() override { |
| 31 RenderViewHostTestHarness::SetUp(); | 32 RenderViewHostTestHarness::SetUp(); |
| 32 | 33 |
| 33 favicon_service_.reset(new FaviconService(nullptr, nullptr)); | 34 favicon_service_.reset(new FaviconService(nullptr, nullptr)); |
| 34 ContentFaviconDriver::CreateForWebContents( | 35 ContentFaviconDriver::CreateForWebContents( |
| 35 web_contents(), favicon_service(), nullptr, nullptr); | 36 web_contents(), favicon_service(), nullptr, nullptr); |
| 37 |
| 38 download_callback_ = base::Bind( |
| 39 &ContentFaviconDriverTest::OnImageDownloaded, base::Unretained(this)); |
| 36 } | 40 } |
| 37 | 41 |
| 38 FaviconService* favicon_service() { | 42 FaviconService* favicon_service() { |
| 39 return favicon_service_.get(); | 43 return favicon_service_.get(); |
| 40 } | 44 } |
| 41 | 45 |
| 46 FaviconHandler::Delegate::ImageDownloadCallback download_callback() { |
| 47 return download_callback_; |
| 48 } |
| 49 |
| 50 content::WebContentsTester* web_contents_tester() { |
| 51 return content::WebContentsTester::For(web_contents()); |
| 52 } |
| 53 |
| 42 private: | 54 private: |
| 55 void OnImageDownloaded(int id, |
| 56 int status_code, |
| 57 const GURL& image_url, |
| 58 const std::vector<SkBitmap>& bitmaps, |
| 59 const std::vector<gfx::Size>& original_bitmap_sizes) { |
| 60 // ADD_FAILURE() << "OnImageDownloaded() should never be called in tests " |
| 61 // "(not exercised by TestWebContents)."; |
| 62 } |
| 63 |
| 43 std::unique_ptr<FaviconService> favicon_service_; | 64 std::unique_ptr<FaviconService> favicon_service_; |
| 65 FaviconHandler::Delegate::ImageDownloadCallback download_callback_; |
| 44 | 66 |
| 45 DISALLOW_COPY_AND_ASSIGN(ContentFaviconDriverTest); | 67 DISALLOW_COPY_AND_ASSIGN(ContentFaviconDriverTest); |
| 46 }; | 68 }; |
| 47 | 69 |
| 48 // Test that Favicon is not requested repeatedly during the same session if | 70 // Test that Favicon is not requested repeatedly during the same session if |
| 49 // server returns HTTP 404 status. | 71 // server returns HTTP 404 status. |
| 50 TEST_F(ContentFaviconDriverTest, UnableToDownloadFavicon) { | 72 TEST_F(ContentFaviconDriverTest, UnableToDownloadFavicon) { |
| 51 const GURL missing_icon_url("http://www.google.com/favicon.ico"); | 73 const GURL missing_icon_url("http://www.google.com/favicon.ico"); |
| 52 const GURL another_icon_url("http://www.youtube.com/favicon.ico"); | 74 const GURL another_icon_url("http://www.youtube.com/favicon.ico"); |
| 53 | 75 |
| 54 ContentFaviconDriver* content_favicon_driver = | 76 ContentFaviconDriver* content_favicon_driver = |
| 55 ContentFaviconDriver::FromWebContents(web_contents()); | 77 ContentFaviconDriver::FromWebContents(web_contents()); |
| 56 | 78 |
| 57 std::vector<SkBitmap> empty_icons; | 79 std::vector<SkBitmap> empty_icons; |
| 58 std::vector<gfx::Size> empty_icon_sizes; | 80 std::vector<gfx::Size> empty_icon_sizes; |
| 59 int download_id = 0; | 81 int download_id = 0; |
| 60 | 82 |
| 61 // Try to download missing icon. | 83 // Try to download missing icon. |
| 62 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 84 download_id = content_favicon_driver->DownloadImage(missing_icon_url, 0, |
| 85 download_callback()); |
| 63 EXPECT_NE(0, download_id); | 86 EXPECT_NE(0, download_id); |
| 64 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 87 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); |
| 65 | 88 |
| 66 // Report download failure with HTTP 503 status. | 89 // Report download failure with HTTP 503 status. |
| 67 content_favicon_driver->DidDownloadFavicon(download_id, 503, missing_icon_url, | 90 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
| 68 empty_icons, empty_icon_sizes); | 91 download_id, 503, empty_icons, empty_icon_sizes)); |
| 69 // Icon is not marked as UnableToDownload as HTTP status is not 404. | 92 // Icon is not marked as UnableToDownload as HTTP status is not 404. |
| 70 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 93 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); |
| 71 | 94 |
| 72 // Try to download again. | 95 // Try to download again. |
| 73 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 96 download_id = content_favicon_driver->DownloadImage(missing_icon_url, 0, |
| 97 download_callback()); |
| 74 EXPECT_NE(0, download_id); | 98 EXPECT_NE(0, download_id); |
| 75 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 99 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); |
| 76 | 100 |
| 77 // Report download failure with HTTP 404 status. | 101 // Report download failure with HTTP 404 status. |
| 78 content_favicon_driver->DidDownloadFavicon(download_id, 404, missing_icon_url, | 102 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
| 79 empty_icons, empty_icon_sizes); | 103 download_id, 404, empty_icons, empty_icon_sizes)); |
| 80 // Icon is marked as UnableToDownload. | 104 // Icon is marked as UnableToDownload. |
| 81 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 105 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); |
| 82 | 106 |
| 83 // Try to download again. | 107 // Try to download again. |
| 84 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 108 download_id = content_favicon_driver->DownloadImage(missing_icon_url, 0, |
| 109 download_callback()); |
| 85 // Download is not started and Icon is still marked as UnableToDownload. | 110 // Download is not started and Icon is still marked as UnableToDownload. |
| 86 EXPECT_EQ(0, download_id); | 111 EXPECT_EQ(0, download_id); |
| 87 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 112 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); |
| 88 | 113 |
| 89 // Try to download another icon. | 114 // Try to download another icon. |
| 90 download_id = content_favicon_driver->StartDownload(another_icon_url, 0); | 115 download_id = content_favicon_driver->DownloadImage(another_icon_url, 0, |
| 116 download_callback()); |
| 91 // Download is started as another icon URL is not same as missing_icon_url. | 117 // Download is started as another icon URL is not same as missing_icon_url. |
| 92 EXPECT_NE(0, download_id); | 118 EXPECT_NE(0, download_id); |
| 93 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url)); | 119 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url)); |
| 94 | 120 |
| 95 // Clear the list of missing icons. | 121 // Clear the list of missing icons. |
| 96 favicon_service()->ClearUnableToDownloadFavicons(); | 122 favicon_service()->ClearUnableToDownloadFavicons(); |
| 97 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 123 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); |
| 98 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url)); | 124 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url)); |
| 99 | 125 |
| 100 // Try to download again. | 126 // Try to download again. |
| 101 download_id = content_favicon_driver->StartDownload(missing_icon_url, 0); | 127 download_id = content_favicon_driver->DownloadImage(missing_icon_url, 0, |
| 128 download_callback()); |
| 102 EXPECT_NE(0, download_id); | 129 EXPECT_NE(0, download_id); |
| 103 // Report download success with HTTP 200 status. | 130 // Report download success with HTTP 200 status. |
| 104 content_favicon_driver->DidDownloadFavicon(download_id, 200, missing_icon_url, | 131 EXPECT_TRUE(web_contents_tester()->TestDidDownloadImage( |
| 105 empty_icons, empty_icon_sizes); | 132 download_id, 200, empty_icons, empty_icon_sizes)); |
| 106 // Icon is not marked as UnableToDownload as HTTP status is not 404. | 133 // Icon is not marked as UnableToDownload as HTTP status is not 404. |
| 107 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); | 134 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url)); |
| 108 | 135 |
| 109 favicon_service()->Shutdown(); | 136 favicon_service()->Shutdown(); |
| 110 } | 137 } |
| 111 | 138 |
| 112 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no | 139 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no |
| 113 // last committed entry. This occurs when script is injected in about:blank. | 140 // last committed entry. This occurs when script is injected in about:blank. |
| 114 // See crbug.com/520759 for more details | 141 // See crbug.com/520759 for more details |
| 115 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { | 142 TEST_F(ContentFaviconDriverTest, FaviconUpdateNoLastCommittedEntry) { |
| 116 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); | 143 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry()); |
| 117 | 144 |
| 118 std::vector<content::FaviconURL> favicon_urls; | 145 std::vector<content::FaviconURL> favicon_urls; |
| 119 favicon_urls.push_back(content::FaviconURL( | 146 favicon_urls.push_back(content::FaviconURL( |
| 120 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, | 147 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON, |
| 121 std::vector<gfx::Size>())); | 148 std::vector<gfx::Size>())); |
| 122 favicon::ContentFaviconDriver* driver = | 149 favicon::ContentFaviconDriver* driver = |
| 123 favicon::ContentFaviconDriver::FromWebContents(web_contents()); | 150 favicon::ContentFaviconDriver::FromWebContents(web_contents()); |
| 124 static_cast<content::WebContentsObserver*>(driver) | 151 static_cast<content::WebContentsObserver*>(driver) |
| 125 ->DidUpdateFaviconURL(favicon_urls); | 152 ->DidUpdateFaviconURL(favicon_urls); |
| 126 | 153 |
| 127 // Test that ContentFaviconDriver ignored the favicon url update. | 154 // Test that ContentFaviconDriver ignored the favicon url update. |
| 128 EXPECT_TRUE(driver->favicon_urls().empty()); | 155 EXPECT_TRUE(driver->favicon_urls().empty()); |
| 129 } | 156 } |
| 130 | 157 |
| 131 } // namespace | 158 } // namespace |
| 132 } // namespace favicon | 159 } // namespace favicon |
| OLD | NEW |