| 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 "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/scoped_observer.h" | 11 #include "base/scoped_observer.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/test/scoped_feature_list.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 16 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_commands.h" | 19 #include "chrome/browser/ui/browser_commands.h" |
| 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 19 #include "chrome/test/base/in_process_browser_test.h" | 21 #include "chrome/test/base/in_process_browser_test.h" |
| 20 #include "chrome/test/base/ui_test_utils.h" | 22 #include "chrome/test/base/ui_test_utils.h" |
| 21 #include "components/favicon/core/favicon_driver_observer.h" | 23 #include "components/favicon/core/favicon_driver_observer.h" |
| 22 #include "components/favicon/core/favicon_handler.h" | 24 #include "components/favicon/core/favicon_handler.h" |
| 25 #include "components/favicon/core/favicon_service.h" |
| 23 #include "content/public/browser/notification_observer.h" | 26 #include "content/public/browser/notification_observer.h" |
| 24 #include "content/public/browser/notification_registrar.h" | 27 #include "content/public/browser/notification_registrar.h" |
| 25 #include "content/public/browser/notification_types.h" | 28 #include "content/public/browser/notification_types.h" |
| 26 #include "content/public/browser/resource_dispatcher_host.h" | 29 #include "content/public/browser/resource_dispatcher_host.h" |
| 27 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 30 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 28 #include "net/base/load_flags.h" | 31 #include "net/base/load_flags.h" |
| 29 #include "net/test/embedded_test_server/embedded_test_server.h" | 32 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 30 #include "net/url_request/url_request.h" | 33 #include "net/url_request/url_request.h" |
| 31 #include "url/url_constants.h" | 34 #include "url/url_constants.h" |
| 32 | 35 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 content::WebContents* web_contents() { | 184 content::WebContents* web_contents() { |
| 182 return browser()->tab_strip_model()->GetActiveWebContents(); | 185 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 183 } | 186 } |
| 184 | 187 |
| 185 // FaviconDriverPendingTaskChecker: | 188 // FaviconDriverPendingTaskChecker: |
| 186 bool HasPendingTasks() override { | 189 bool HasPendingTasks() override { |
| 187 return favicon::ContentFaviconDriver::FromWebContents(web_contents()) | 190 return favicon::ContentFaviconDriver::FromWebContents(web_contents()) |
| 188 ->HasPendingTasksForTest(); | 191 ->HasPendingTasksForTest(); |
| 189 } | 192 } |
| 190 | 193 |
| 194 favicon_base::FaviconRawBitmapResult GetFaviconForPageURL(const GURL& url) { |
| 195 favicon::FaviconService* favicon_service = |
| 196 FaviconServiceFactory::GetForProfile( |
| 197 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS); |
| 198 |
| 199 std::vector<favicon_base::FaviconRawBitmapResult> results; |
| 200 base::CancelableTaskTracker tracker; |
| 201 base::RunLoop loop; |
| 202 favicon_service->GetFaviconForPageURL( |
| 203 url, favicon_base::FAVICON, /*desired_size_in_dip=*/0, |
| 204 base::Bind( |
| 205 [](std::vector<favicon_base::FaviconRawBitmapResult>* save_results, |
| 206 base::RunLoop* loop, |
| 207 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 208 results) { |
| 209 *save_results = results; |
| 210 loop->Quit(); |
| 211 }, |
| 212 &results, &loop), |
| 213 &tracker); |
| 214 loop.Run(); |
| 215 for (const favicon_base::FaviconRawBitmapResult& result : results) { |
| 216 if (result.is_valid()) |
| 217 return result; |
| 218 } |
| 219 return favicon_base::FaviconRawBitmapResult(); |
| 220 } |
| 221 |
| 191 private: | 222 private: |
| 192 DISALLOW_COPY_AND_ASSIGN(ContentFaviconDriverTest); | 223 DISALLOW_COPY_AND_ASSIGN(ContentFaviconDriverTest); |
| 193 }; | 224 }; |
| 194 | 225 |
| 195 // Test that when a user reloads a page ignoring the cache that the favicon is | 226 // Test that when a user reloads a page ignoring the cache that the favicon is |
| 196 // is redownloaded and (not returned from either the favicon cache or the HTTP | 227 // is redownloaded and (not returned from either the favicon cache or the HTTP |
| 197 // cache). | 228 // cache). |
| 198 IN_PROC_BROWSER_TEST_F(ContentFaviconDriverTest, ReloadBypassingCache) { | 229 IN_PROC_BROWSER_TEST_F(ContentFaviconDriverTest, ReloadBypassingCache) { |
| 199 ASSERT_TRUE(embedded_test_server()->Start()); | 230 ASSERT_TRUE(embedded_test_server()->Start()); |
| 200 GURL url = embedded_test_server()->GetURL("/favicon/page_with_favicon.html"); | 231 GURL url = embedded_test_server()->GetURL("/favicon/page_with_favicon.html"); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 | 263 |
| 233 // A reload ignoring the cache should refetch the favicon from the website. | 264 // A reload ignoring the cache should refetch the favicon from the website. |
| 234 { | 265 { |
| 235 PendingTaskWaiter waiter(web_contents(), this); | 266 PendingTaskWaiter waiter(web_contents(), this); |
| 236 chrome::ExecuteCommand(browser(), IDC_RELOAD_BYPASSING_CACHE); | 267 chrome::ExecuteCommand(browser(), IDC_RELOAD_BYPASSING_CACHE); |
| 237 waiter.Wait(); | 268 waiter.Wait(); |
| 238 } | 269 } |
| 239 ASSERT_TRUE(delegate->was_requested()); | 270 ASSERT_TRUE(delegate->was_requested()); |
| 240 EXPECT_TRUE(delegate->bypassed_cache()); | 271 EXPECT_TRUE(delegate->bypassed_cache()); |
| 241 } | 272 } |
| 273 |
| 274 // Test that loading a page that contains icons only in the Web Manifest causes |
| 275 // those icons to be used. |
| 276 IN_PROC_BROWSER_TEST_F(ContentFaviconDriverTest, LoadIconFromWebManifest) { |
| 277 base::test::ScopedFeatureList override_features; |
| 278 override_features.InitAndEnableFeature(favicon::kFaviconsFromWebManifest); |
| 279 |
| 280 ASSERT_TRUE(embedded_test_server()->Start()); |
| 281 GURL url = embedded_test_server()->GetURL("/favicon/page_with_manifest.html"); |
| 282 GURL icon_url = embedded_test_server()->GetURL("/favicon/icon.png"); |
| 283 |
| 284 std::unique_ptr<TestResourceDispatcherHostDelegate> delegate( |
| 285 new TestResourceDispatcherHostDelegate(icon_url)); |
| 286 content::ResourceDispatcherHost::Get()->SetDelegate(delegate.get()); |
| 287 |
| 288 PendingTaskWaiter waiter(web_contents(), this); |
| 289 ui_test_utils::NavigateToURLWithDisposition( |
| 290 browser(), url, WindowOpenDisposition::CURRENT_TAB, |
| 291 ui_test_utils::BROWSER_TEST_NONE); |
| 292 waiter.Wait(); |
| 293 |
| 294 EXPECT_TRUE(delegate->was_requested()); |
| 295 } |
| 296 |
| 297 // Test that loading a page that contains a Web Manifest without icons and a |
| 298 // regular favicon in the HTML reports the icon. The regular icon is initially |
| 299 // cached in the Favicon database. |
| 300 IN_PROC_BROWSER_TEST_F(ContentFaviconDriverTest, |
| 301 LoadRegularIconDespiteWebManifestWithoutIcons) { |
| 302 ASSERT_TRUE(embedded_test_server()->Start()); |
| 303 GURL url = embedded_test_server()->GetURL( |
| 304 "/favicon/page_with_manifest_without_icons.html"); |
| 305 GURL icon_url = embedded_test_server()->GetURL("/favicon/icon.png"); |
| 306 |
| 307 // Initial visit with the feature still disabled. |
| 308 std::unique_ptr<TestResourceDispatcherHostDelegate> delegate( |
| 309 new TestResourceDispatcherHostDelegate(icon_url)); |
| 310 content::ResourceDispatcherHost::Get()->SetDelegate(delegate.get()); |
| 311 |
| 312 // Initial visit with the feature still disabled, to populate the cache. |
| 313 { |
| 314 PendingTaskWaiter waiter(web_contents(), this); |
| 315 ui_test_utils::NavigateToURLWithDisposition( |
| 316 browser(), url, WindowOpenDisposition::CURRENT_TAB, |
| 317 ui_test_utils::BROWSER_TEST_NONE); |
| 318 waiter.Wait(); |
| 319 } |
| 320 ASSERT_TRUE(delegate->was_requested()); |
| 321 delegate->Reset(); |
| 322 ASSERT_NE(nullptr, GetFaviconForPageURL(url).bitmap_data); |
| 323 |
| 324 // Enable the feature and visit the page again. |
| 325 base::test::ScopedFeatureList override_features; |
| 326 override_features.InitAndEnableFeature(favicon::kFaviconsFromWebManifest); |
| 327 |
| 328 { |
| 329 PendingTaskWaiter waiter(web_contents(), this); |
| 330 ui_test_utils::NavigateToURLWithDisposition( |
| 331 browser(), url, WindowOpenDisposition::CURRENT_TAB, |
| 332 ui_test_utils::BROWSER_TEST_NONE); |
| 333 waiter.Wait(); |
| 334 } |
| 335 |
| 336 EXPECT_NE(nullptr, GetFaviconForPageURL(url).bitmap_data); |
| 337 } |
| OLD | NEW |