Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/core/favicon_handler.h" | 5 #include "components/favicon/core/favicon_handler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/test/histogram_tester.h" | 18 #include "base/test/histogram_tester.h" |
| 19 #include "base/test/scoped_feature_list.h" | |
| 19 #include "base/test/scoped_task_environment.h" | 20 #include "base/test/scoped_task_environment.h" |
| 20 #include "base/test/test_simple_task_runner.h" | 21 #include "base/test/test_simple_task_runner.h" |
| 21 #include "components/favicon/core/favicon_driver.h" | 22 #include "components/favicon/core/favicon_driver.h" |
| 22 #include "components/favicon/core/test/mock_favicon_service.h" | 23 #include "components/favicon/core/test/mock_favicon_service.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "third_party/skia/include/core/SkBitmap.h" | 26 #include "third_party/skia/include/core/SkBitmap.h" |
| 26 #include "third_party/skia/include/core/SkColor.h" | 27 #include "third_party/skia/include/core/SkColor.h" |
| 27 #include "ui/base/layout.h" | 28 #include "ui/base/layout.h" |
| 28 #include "ui/gfx/codec/png_codec.h" | 29 #include "ui/gfx/codec/png_codec.h" |
| 29 #include "ui/gfx/favicon_size.h" | 30 #include "ui/gfx/favicon_size.h" |
| 30 #include "ui/gfx/image/image.h" | 31 #include "ui/gfx/image/image.h" |
| 31 | 32 |
| 32 namespace favicon { | 33 namespace favicon { |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 using favicon_base::FAVICON; | 36 using favicon_base::FAVICON; |
| 36 using favicon_base::FaviconRawBitmapResult; | 37 using favicon_base::FaviconRawBitmapResult; |
| 37 using favicon_base::TOUCH_ICON; | 38 using favicon_base::TOUCH_ICON; |
| 38 using favicon_base::TOUCH_PRECOMPOSED_ICON; | 39 using favicon_base::TOUCH_PRECOMPOSED_ICON; |
| 40 using testing::AnyNumber; | |
| 39 using testing::Assign; | 41 using testing::Assign; |
| 42 using testing::Contains; | |
| 40 using testing::ElementsAre; | 43 using testing::ElementsAre; |
| 41 using testing::InSequence; | 44 using testing::InSequence; |
| 42 using testing::Invoke; | 45 using testing::Invoke; |
| 43 using testing::IsEmpty; | 46 using testing::IsEmpty; |
| 47 using testing::Not; | |
| 44 using testing::Return; | 48 using testing::Return; |
| 45 using testing::_; | 49 using testing::_; |
| 46 | 50 |
| 47 using DownloadOutcome = FaviconHandler::DownloadOutcome; | 51 using DownloadOutcome = FaviconHandler::DownloadOutcome; |
| 48 using IntVector = std::vector<int>; | 52 using IntVector = std::vector<int>; |
| 49 using URLVector = std::vector<GURL>; | 53 using URLVector = std::vector<GURL>; |
| 50 using BitmapVector = std::vector<SkBitmap>; | 54 using BitmapVector = std::vector<SkBitmap>; |
| 51 using SizeVector = std::vector<gfx::Size>; | 55 using SizeVector = std::vector<gfx::Size>; |
| 52 | 56 |
| 53 MATCHER_P2(ImageSizeIs, width, height, "") { | 57 MATCHER_P2(ImageSizeIs, width, height, "") { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 // Fake that implements the calls to FaviconHandler::Delegate's DownloadImage(), | 114 // Fake that implements the calls to FaviconHandler::Delegate's DownloadImage(), |
| 111 // delegated to this class through MockDelegate. | 115 // delegated to this class through MockDelegate. |
| 112 class FakeImageDownloader { | 116 class FakeImageDownloader { |
| 113 public: | 117 public: |
| 114 struct Response { | 118 struct Response { |
| 115 int http_status_code = 404; | 119 int http_status_code = 404; |
| 116 BitmapVector bitmaps; | 120 BitmapVector bitmaps; |
| 117 SizeVector original_bitmap_sizes; | 121 SizeVector original_bitmap_sizes; |
| 118 }; | 122 }; |
| 119 | 123 |
| 120 FakeImageDownloader() : next_download_id_(1) {} | 124 // |downloads| must not be nullptr and must outlive this object. |
| 125 FakeImageDownloader(URLVector* downloads) | |
| 126 : downloads_(downloads), next_download_id_(1) {} | |
| 121 | 127 |
| 122 // Implementation of FaviconHalder::Delegate's DownloadImage(). If a given | 128 // Implementation of FaviconHalder::Delegate's DownloadImage(). If a given |
| 123 // URL is not known (i.e. not previously added via Add()), it produces 404s. | 129 // URL is not known (i.e. not previously added via Add()), it produces 404s. |
| 124 int DownloadImage(const GURL& url, | 130 int DownloadImage(const GURL& url, |
| 125 int max_image_size, | 131 int max_image_size, |
| 126 FaviconHandler::Delegate::ImageDownloadCallback callback) { | 132 FaviconHandler::Delegate::ImageDownloadCallback callback) { |
| 127 downloads_.push_back(url); | 133 downloads_->push_back(url); |
| 128 | 134 |
| 129 const Response& response = responses_[url]; | 135 const Response& response = responses_[url]; |
| 130 int download_id = next_download_id_++; | 136 int download_id = next_download_id_++; |
| 131 base::Closure bound_callback = | 137 base::Closure bound_callback = |
| 132 base::Bind(callback, download_id, response.http_status_code, url, | 138 base::Bind(callback, download_id, response.http_status_code, url, |
| 133 response.bitmaps, response.original_bitmap_sizes); | 139 response.bitmaps, response.original_bitmap_sizes); |
| 134 if (url == manual_callback_url_) | 140 if (url == manual_callback_url_) |
| 135 manual_callback_ = bound_callback; | 141 manual_callbacks_.push_back(bound_callback); |
| 136 else | 142 else |
| 137 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, bound_callback); | 143 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, bound_callback); |
| 138 return download_id; | 144 return download_id; |
| 139 } | 145 } |
| 140 | 146 |
| 141 void Add(const GURL& icon_url, | 147 void Add(const GURL& icon_url, |
| 142 const IntVector& sizes, | 148 const IntVector& sizes, |
| 143 const IntVector& original_sizes, | 149 const IntVector& original_sizes, |
| 144 SkColor color) { | 150 SkColor color) { |
| 145 DCHECK_EQ(sizes.size(), original_sizes.size()); | 151 DCHECK_EQ(sizes.size(), original_sizes.size()); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 158 } | 164 } |
| 159 | 165 |
| 160 void AddError(const GURL& icon_url, int http_status_code) { | 166 void AddError(const GURL& icon_url, int http_status_code) { |
| 161 Response response; | 167 Response response; |
| 162 response.http_status_code = http_status_code; | 168 response.http_status_code = http_status_code; |
| 163 responses_[icon_url] = response; | 169 responses_[icon_url] = response; |
| 164 } | 170 } |
| 165 | 171 |
| 166 // Disables automatic callback for |url|. This is useful for emulating a | 172 // Disables automatic callback for |url|. This is useful for emulating a |
| 167 // download taking a long time. The callback for DownloadImage() will be | 173 // download taking a long time. The callback for DownloadImage() will be |
| 168 // stored in |manual_callback_|. | 174 // stored in |manual_callbacks_|. |
| 169 void SetRunCallbackManuallyForUrl(const GURL& url) { | 175 void SetRunCallbackManuallyForUrl(const GURL& url) { |
| 170 manual_callback_url_ = url; | 176 manual_callback_url_ = url; |
| 171 } | 177 } |
| 172 | 178 |
| 173 // Returns whether an ongoing download exists for a url previously selected | 179 // Returns whether an ongoing download exists for a url previously selected |
| 174 // via SetRunCallbackManuallyForUrl(). | 180 // via SetRunCallbackManuallyForUrl(). |
| 175 bool HasPendingManualCallback() { return !manual_callback_.is_null(); } | 181 bool HasPendingManualCallback() { return !manual_callbacks_.empty(); } |
| 176 | 182 |
| 177 // Triggers the response for a download previously selected for manual | 183 // Triggers responses for downloads previously selected for manual triggering |
| 178 // triggering via SetRunCallbackManuallyForUrl(). | 184 // via SetRunCallbackManuallyForUrl(). |
| 179 bool RunCallbackManually() { | 185 bool RunCallbackManually() { |
| 180 if (!HasPendingManualCallback()) | 186 if (!HasPendingManualCallback()) |
| 181 return false; | 187 return false; |
| 182 manual_callback_.Run(); | 188 for (base::Closure& callback : std::move(manual_callbacks_)) |
| 183 manual_callback_.Reset(); | 189 callback.Run(); |
| 184 return true; | 190 return true; |
| 185 } | 191 } |
| 186 | 192 |
| 187 // Returns pending and completed download URLs. | |
| 188 const URLVector& downloads() const { return downloads_; } | |
| 189 | |
| 190 void ClearDownloads() { downloads_.clear(); } | |
| 191 | |
| 192 private: | 193 private: |
| 194 URLVector* downloads_; | |
| 193 int next_download_id_; | 195 int next_download_id_; |
| 194 | 196 |
| 195 // Pending and completed download URLs. | |
| 196 URLVector downloads_; | |
| 197 | |
| 198 // URL to disable automatic callbacks for. | 197 // URL to disable automatic callbacks for. |
| 199 GURL manual_callback_url_; | 198 GURL manual_callback_url_; |
| 200 | 199 |
| 201 // Callback for DownloadImage() request for |manual_callback_url_|. | 200 // Callback for DownloadImage() request for |manual_callback_url_|. |
| 202 base::Closure manual_callback_; | 201 std::vector<base::Closure> manual_callbacks_; |
| 203 | 202 |
| 204 // Registered responses. | 203 // Registered responses. |
| 205 std::map<GURL, Response> responses_; | 204 std::map<GURL, Response> responses_; |
| 206 | 205 |
| 207 DISALLOW_COPY_AND_ASSIGN(FakeImageDownloader); | 206 DISALLOW_COPY_AND_ASSIGN(FakeImageDownloader); |
| 208 }; | 207 }; |
| 209 | 208 |
| 209 // Fake that implements the calls to FaviconHandler::Delegate's | |
| 210 // DownloadManifest(), delegated to this class through MockDelegate. | |
| 211 class FakeManifestDownloader { | |
| 212 public: | |
| 213 struct Response { | |
| 214 std::vector<favicon::FaviconURL> favicon_urls; | |
| 215 }; | |
| 216 | |
| 217 // |downloads| must not be nullptr and must outlive this object. | |
| 218 FakeManifestDownloader(URLVector* downloads) : downloads_(downloads) {} | |
| 219 | |
| 220 // Implementation of FaviconHalder::Delegate's DownloadManifest(). If a given | |
| 221 // URL is not known (i.e. not previously added via Add()), it produces 404s. | |
| 222 void DownloadManifest( | |
| 223 const GURL& url, | |
| 224 FaviconHandler::Delegate::ManifestDownloadCallback callback) { | |
| 225 downloads_->push_back(url); | |
| 226 | |
| 227 const Response& response = responses_[url]; | |
| 228 base::Closure bound_callback = base::Bind(callback, response.favicon_urls); | |
| 229 if (url == manual_callback_url_) | |
| 230 manual_callbacks_.push_back(bound_callback); | |
| 231 else | |
| 232 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, bound_callback); | |
| 233 } | |
| 234 | |
| 235 void Add(const GURL& manifest_url, | |
| 236 const std::vector<favicon::FaviconURL>& favicon_urls) { | |
| 237 Response response; | |
| 238 response.favicon_urls = favicon_urls; | |
| 239 responses_[manifest_url] = response; | |
| 240 } | |
| 241 | |
| 242 void AddError(const GURL& manifest_url) { | |
| 243 responses_[manifest_url] = Response(); | |
| 244 } | |
| 245 | |
| 246 // Disables automatic callback for |url|. This is useful for emulating a | |
| 247 // download taking a long time. The callback for DownloadManifest() will be | |
| 248 // stored in |manual_callback_|. | |
| 249 void SetRunCallbackManuallyForUrl(const GURL& url) { | |
| 250 manual_callback_url_ = url; | |
| 251 } | |
| 252 | |
| 253 // Returns whether an ongoing download exists for a url previously selected | |
| 254 // via SetRunCallbackManuallyForUrl(). | |
| 255 bool HasPendingManualCallback() { return !manual_callbacks_.empty(); } | |
| 256 | |
| 257 // Triggers responses for downloads previously selected for manual triggering | |
| 258 // via SetRunCallbackManuallyForUrl(). | |
| 259 bool RunCallbackManually() { | |
| 260 if (!HasPendingManualCallback()) | |
| 261 return false; | |
| 262 for (base::Closure& callback : std::move(manual_callbacks_)) | |
| 263 callback.Run(); | |
| 264 return true; | |
| 265 } | |
| 266 | |
| 267 private: | |
| 268 URLVector* downloads_; | |
| 269 | |
| 270 // URL to disable automatic callbacks for. | |
| 271 GURL manual_callback_url_; | |
| 272 | |
| 273 // Callback for DownloadManifest() request for |manual_callback_url_|. | |
| 274 std::vector<base::Closure> manual_callbacks_; | |
| 275 | |
| 276 // Registered responses. | |
| 277 std::map<GURL, Response> responses_; | |
| 278 | |
| 279 DISALLOW_COPY_AND_ASSIGN(FakeManifestDownloader); | |
| 280 }; | |
| 281 | |
| 210 class MockDelegate : public FaviconHandler::Delegate { | 282 class MockDelegate : public FaviconHandler::Delegate { |
| 211 public: | 283 public: |
| 212 MockDelegate() { | 284 MockDelegate() |
| 285 : fake_image_downloader_(&downloads_), | |
| 286 fake_manifest_downloader_(&downloads_) { | |
| 213 // Delegate image downloading to FakeImageDownloader. | 287 // Delegate image downloading to FakeImageDownloader. |
| 214 ON_CALL(*this, DownloadImage(_, _, _)) | 288 ON_CALL(*this, DownloadImage(_, _, _)) |
| 215 .WillByDefault(Invoke(&fake_image_downloader_, | 289 .WillByDefault(Invoke(&fake_image_downloader_, |
| 216 &FakeImageDownloader::DownloadImage)); | 290 &FakeImageDownloader::DownloadImage)); |
| 291 // Delegate manifest downloading to FakeManifestDownloader. | |
| 292 ON_CALL(*this, DownloadManifest(_, _)) | |
| 293 .WillByDefault(Invoke(&fake_manifest_downloader_, | |
| 294 &FakeManifestDownloader::DownloadManifest)); | |
| 217 } | 295 } |
| 218 | 296 |
| 219 MOCK_METHOD3(DownloadImage, | 297 MOCK_METHOD3(DownloadImage, |
| 220 int(const GURL& url, | 298 int(const GURL& url, |
| 221 int max_image_size, | 299 int max_image_size, |
| 222 ImageDownloadCallback callback)); | 300 ImageDownloadCallback callback)); |
| 301 MOCK_METHOD2(DownloadManifest, | |
| 302 void(const GURL& url, ManifestDownloadCallback callback)); | |
| 223 MOCK_METHOD0(IsOffTheRecord, bool()); | 303 MOCK_METHOD0(IsOffTheRecord, bool()); |
| 224 MOCK_METHOD1(IsBookmarked, bool(const GURL& url)); | 304 MOCK_METHOD1(IsBookmarked, bool(const GURL& url)); |
| 225 MOCK_METHOD5(OnFaviconUpdated, | 305 MOCK_METHOD5(OnFaviconUpdated, |
| 226 void(const GURL& page_url, | 306 void(const GURL& page_url, |
| 227 FaviconDriverObserver::NotificationIconType type, | 307 FaviconDriverObserver::NotificationIconType type, |
| 228 const GURL& icon_url, | 308 const GURL& icon_url, |
| 229 bool icon_url_changed, | 309 bool icon_url_changed, |
| 230 const gfx::Image& image)); | 310 const gfx::Image& image)); |
| 231 | 311 |
| 232 FakeImageDownloader& fake_image_downloader() { | 312 FakeImageDownloader& fake_image_downloader() { |
| 233 return fake_image_downloader_; | 313 return fake_image_downloader_; |
| 234 } | 314 } |
| 235 | 315 |
| 236 // Convenience getter for test readability. Returns pending and completed | 316 FakeManifestDownloader& fake_manifest_downloader() { |
| 237 // download URLs. | 317 return fake_manifest_downloader_; |
| 238 const URLVector& downloads() const { | |
| 239 return fake_image_downloader_.downloads(); | |
| 240 } | 318 } |
| 241 | 319 |
| 320 // Returns pending and completed download URLs. | |
| 321 const URLVector& downloads() const { return downloads_; } | |
| 322 | |
| 323 void ClearDownloads() { downloads_.clear(); } | |
| 324 | |
| 242 private: | 325 private: |
| 326 // Pending and completed download URLs. | |
| 327 URLVector downloads_; | |
| 243 FakeImageDownloader fake_image_downloader_; | 328 FakeImageDownloader fake_image_downloader_; |
| 329 FakeManifestDownloader fake_manifest_downloader_; | |
| 244 }; | 330 }; |
| 245 | 331 |
| 246 // FakeFaviconService mimics a FaviconService backend that allows setting up | 332 // FakeFaviconService mimics a FaviconService backend that allows setting up |
| 247 // test data stored via Store(). If Store() has not been called for a | 333 // test data stored via Store(). If Store() has not been called for a |
| 248 // particular URL, the callback is called with empty database results. | 334 // particular URL, the callback is called with empty database results. |
| 249 class FakeFaviconService { | 335 class FakeFaviconService { |
| 250 public: | 336 public: |
| 251 FakeFaviconService() | 337 FakeFaviconService() |
| 252 : manual_callback_task_runner_(new base::TestSimpleTaskRunner()) {} | 338 : manual_callback_task_runner_(new base::TestSimpleTaskRunner()) {} |
| 253 | 339 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 397 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon(). | 483 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon(). |
| 398 // Force the values of the scale factors so that the tests produce the same | 484 // Force the values of the scale factors so that the tests produce the same |
| 399 // results on all platforms. | 485 // results on all platforms. |
| 400 scoped_set_supported_scale_factors_.reset( | 486 scoped_set_supported_scale_factors_.reset( |
| 401 new ui::test::ScopedSetSupportedScaleFactors({ui::SCALE_FACTOR_100P})); | 487 new ui::test::ScopedSetSupportedScaleFactors({ui::SCALE_FACTOR_100P})); |
| 402 } | 488 } |
| 403 | 489 |
| 404 bool VerifyAndClearExpectations() { | 490 bool VerifyAndClearExpectations() { |
| 405 base::RunLoop().RunUntilIdle(); | 491 base::RunLoop().RunUntilIdle(); |
| 406 favicon_service_.fake()->ClearDbRequests(); | 492 favicon_service_.fake()->ClearDbRequests(); |
| 407 delegate_.fake_image_downloader().ClearDownloads(); | 493 delegate_.ClearDownloads(); |
| 408 return testing::Mock::VerifyAndClearExpectations(&favicon_service_) && | 494 return testing::Mock::VerifyAndClearExpectations(&favicon_service_) && |
| 409 testing::Mock::VerifyAndClearExpectations(&delegate_); | 495 testing::Mock::VerifyAndClearExpectations(&delegate_); |
| 410 } | 496 } |
| 411 | 497 |
| 412 // Creates a new handler and feeds in the page URL and the candidates. | 498 // Creates a new handler and feeds in the page URL and the candidates. |
| 413 // Returns the handler in case tests want to exercise further steps. | 499 // Returns the handler in case tests want to exercise further steps. |
| 414 std::unique_ptr<FaviconHandler> RunHandlerWithCandidates( | 500 std::unique_ptr<FaviconHandler> RunHandlerWithCandidates( |
| 415 FaviconDriverObserver::NotificationIconType handler_type, | 501 FaviconDriverObserver::NotificationIconType handler_type, |
| 416 const std::vector<favicon::FaviconURL>& candidates) { | 502 const std::vector<favicon::FaviconURL>& candidates, |
| 503 const GURL& manifest_url = GURL()) { | |
| 417 auto handler = base::MakeUnique<FaviconHandler>(&favicon_service_, | 504 auto handler = base::MakeUnique<FaviconHandler>(&favicon_service_, |
| 418 &delegate_, handler_type); | 505 &delegate_, handler_type); |
| 419 handler->FetchFavicon(kPageURL); | 506 handler->FetchFavicon(kPageURL); |
| 420 // The first RunUntilIdle() causes the FaviconService lookups be faster than | 507 // The first RunUntilIdle() causes the FaviconService lookups be faster than |
| 421 // OnUpdateCandidates(), which is the most likely scenario. | 508 // OnUpdateCandidates(), which is the most likely scenario. |
| 422 base::RunLoop().RunUntilIdle(); | 509 base::RunLoop().RunUntilIdle(); |
| 423 handler->OnUpdateCandidates(kPageURL, candidates); | 510 handler->OnUpdateCandidates(kPageURL, candidates, manifest_url); |
| 424 base::RunLoop().RunUntilIdle(); | 511 base::RunLoop().RunUntilIdle(); |
| 425 return handler; | 512 return handler; |
| 426 } | 513 } |
| 427 | 514 |
| 428 // Same as above, but for the simplest case where all types are FAVICON and | 515 // Same as above, but for the simplest case where all types are FAVICON and |
| 429 // no sizes are provided, using a FaviconHandler of type NON_TOUCH_16_DIP. | 516 // no sizes are provided, using a FaviconHandler of type NON_TOUCH_16_DIP. |
| 430 std::unique_ptr<FaviconHandler> RunHandlerWithSimpleFaviconCandidates( | 517 std::unique_ptr<FaviconHandler> RunHandlerWithSimpleFaviconCandidates( |
| 431 const std::vector<GURL>& urls) { | 518 const std::vector<GURL>& urls, |
| 519 const GURL& manifest_url = GURL()) { | |
| 432 std::vector<favicon::FaviconURL> candidates; | 520 std::vector<favicon::FaviconURL> candidates; |
| 433 for (const GURL& url : urls) { | 521 for (const GURL& url : urls) { |
| 434 candidates.emplace_back(url, FAVICON, kEmptySizes); | 522 candidates.emplace_back(url, FAVICON, kEmptySizes); |
| 435 } | 523 } |
| 436 return RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP, | 524 return RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP, |
| 437 candidates); | 525 candidates, manifest_url); |
| 438 } | 526 } |
| 439 | 527 |
| 440 base::test::ScopedTaskEnvironment scoped_task_environment_; | 528 base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 441 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> | 529 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> |
| 442 scoped_set_supported_scale_factors_; | 530 scoped_set_supported_scale_factors_; |
| 443 testing::NiceMock<MockFaviconServiceWithFake> favicon_service_; | 531 testing::NiceMock<MockFaviconServiceWithFake> favicon_service_; |
| 444 testing::NiceMock<MockDelegate> delegate_; | 532 testing::NiceMock<MockDelegate> delegate_; |
| 445 }; | 533 }; |
| 446 | 534 |
| 447 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) { | 535 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 496 // Causes FaviconService lookups be faster than OnUpdateCandidates(). | 584 // Causes FaviconService lookups be faster than OnUpdateCandidates(). |
| 497 ASSERT_TRUE(favicon_service_.fake()->RunCallbackManually()); | 585 ASSERT_TRUE(favicon_service_.fake()->RunCallbackManually()); |
| 498 ASSERT_TRUE(VerifyAndClearExpectations()); | 586 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 499 | 587 |
| 500 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON, | 588 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON, |
| 501 ImageSizeIs(16, 16))); | 589 ImageSizeIs(16, 16))); |
| 502 EXPECT_CALL(delegate_, OnFaviconUpdated( | 590 EXPECT_CALL(delegate_, OnFaviconUpdated( |
| 503 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP, | 591 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP, |
| 504 kIconURL16x16, /*icon_url_changed=*/true, _)); | 592 kIconURL16x16, /*icon_url_changed=*/true, _)); |
| 505 // Feed in favicons now that the database lookup is completed. | 593 // Feed in favicons now that the database lookup is completed. |
| 506 handler.OnUpdateCandidates(kPageURL, | 594 handler.OnUpdateCandidates( |
| 507 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); | 595 kPageURL, {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}, GURL()); |
| 508 base::RunLoop().RunUntilIdle(); | 596 base::RunLoop().RunUntilIdle(); |
| 509 | 597 |
| 510 EXPECT_THAT(favicon_service_.fake()->db_requests(), | 598 EXPECT_THAT(favicon_service_.fake()->db_requests(), |
| 511 ElementsAre(kIconURL16x16)); | 599 ElementsAre(kIconURL16x16)); |
| 512 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); | 600 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); |
| 513 } | 601 } |
| 514 | 602 |
| 515 // Test that the FaviconHandler process finishes when: | 603 // Test that the FaviconHandler process finishes when: |
| 516 // - There is data in the database for neither the page URL nor the icon URL. | 604 // - There is data in the database for neither the page URL nor the icon URL. |
| 517 // AND | 605 // AND |
| 518 // - FaviconService::GetFaviconForPageURL() callback returns after | 606 // - FaviconService::GetFaviconForPageURL() callback returns after |
| 519 // FaviconHandler::OnUpdateCandidates() is called. | 607 // FaviconHandler::OnUpdateCandidates() is called. |
| 520 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesFaster) { | 608 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesFaster) { |
| 521 // Defer the database lookup completion to control the exact timing. | 609 // Defer the database lookup completion to control the exact timing. |
| 522 favicon_service_.fake()->SetRunCallbackManuallyForUrl(kPageURL); | 610 favicon_service_.fake()->SetRunCallbackManuallyForUrl(kPageURL); |
| 523 | 611 |
| 524 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); | 612 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); |
| 525 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); | 613 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); |
| 526 | 614 |
| 527 FaviconHandler handler(&favicon_service_, &delegate_, | 615 FaviconHandler handler(&favicon_service_, &delegate_, |
| 528 FaviconDriverObserver::NON_TOUCH_16_DIP); | 616 FaviconDriverObserver::NON_TOUCH_16_DIP); |
| 529 handler.FetchFavicon(kPageURL); | 617 handler.FetchFavicon(kPageURL); |
| 530 base::RunLoop().RunUntilIdle(); | 618 base::RunLoop().RunUntilIdle(); |
| 531 // Feed in favicons before completing the database lookup. | 619 // Feed in favicons before completing the database lookup. |
| 532 handler.OnUpdateCandidates(kPageURL, | 620 handler.OnUpdateCandidates( |
| 533 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); | 621 kPageURL, {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}, GURL()); |
| 534 | 622 |
| 535 ASSERT_TRUE(VerifyAndClearExpectations()); | 623 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 536 // Database lookup for |kPageURL| is ongoing. | 624 // Database lookup for |kPageURL| is ongoing. |
| 537 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); | 625 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); |
| 538 | 626 |
| 539 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON, | 627 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON, |
| 540 ImageSizeIs(16, 16))); | 628 ImageSizeIs(16, 16))); |
| 541 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)); | 629 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)); |
| 542 | 630 |
| 543 // Complete the lookup for |kPageURL|. | 631 // Complete the lookup for |kPageURL|. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 640 // If there is data for the page URL in history which is invalid, test that: | 728 // If there is data for the page URL in history which is invalid, test that: |
| 641 // - The invalid data is not sent to the UI. | 729 // - The invalid data is not sent to the UI. |
| 642 // - The icon is redownloaded. | 730 // - The icon is redownloaded. |
| 643 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) { | 731 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) { |
| 644 const GURL kIconURL("http://www.google.com/favicon"); | 732 const GURL kIconURL("http://www.google.com/favicon"); |
| 645 | 733 |
| 646 delegate_.fake_image_downloader().Add(kIconURL, IntVector{gfx::kFaviconSize}, | 734 delegate_.fake_image_downloader().Add(kIconURL, IntVector{gfx::kFaviconSize}, |
| 647 IntVector{gfx::kFaviconSize}, | 735 IntVector{gfx::kFaviconSize}, |
| 648 SK_ColorBLUE); | 736 SK_ColorBLUE); |
| 649 | 737 |
| 650 // Set non empty but invalid data. | 738 // Set non-empty but invalid data. |
| 651 std::vector<FaviconRawBitmapResult> bitmap_result = | 739 std::vector<FaviconRawBitmapResult> bitmap_result = |
| 652 CreateRawBitmapResult(kIconURL); | 740 CreateRawBitmapResult(kIconURL); |
| 653 // Empty bitmap data is invalid. | 741 // Empty bitmap data is invalid. |
| 654 bitmap_result[0].bitmap_data = new base::RefCountedBytes(); | 742 bitmap_result[0].bitmap_data = new base::RefCountedBytes(); |
| 655 | 743 |
| 656 favicon_service_.fake()->Store(kPageURL, kIconURL, bitmap_result); | 744 favicon_service_.fake()->Store(kPageURL, kIconURL, bitmap_result); |
| 657 | 745 |
| 658 EXPECT_CALL(delegate_, | 746 EXPECT_CALL(delegate_, |
| 659 OnFaviconUpdated(_, _, kIconURL, _, ImageColorIs(SK_ColorBLUE))); | 747 OnFaviconUpdated(_, _, kIconURL, _, ImageColorIs(SK_ColorBLUE))); |
| 660 | 748 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 737 std::unique_ptr<FaviconHandler> handler = | 825 std::unique_ptr<FaviconHandler> handler = |
| 738 RunHandlerWithSimpleFaviconCandidates({kIconURL1, kIconURL2}); | 826 RunHandlerWithSimpleFaviconCandidates({kIconURL1, kIconURL2}); |
| 739 | 827 |
| 740 ASSERT_TRUE(VerifyAndClearExpectations()); | 828 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 741 ASSERT_TRUE(delegate_.fake_image_downloader().HasPendingManualCallback()); | 829 ASSERT_TRUE(delegate_.fake_image_downloader().HasPendingManualCallback()); |
| 742 | 830 |
| 743 // Favicon update should invalidate the ongoing download. | 831 // Favicon update should invalidate the ongoing download. |
| 744 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL3, _, _)); | 832 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL3, _, _)); |
| 745 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL3, _, _)); | 833 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL3, _, _)); |
| 746 | 834 |
| 747 handler->OnUpdateCandidates(kPageURL, | 835 handler->OnUpdateCandidates( |
| 748 {FaviconURL(kIconURL3, FAVICON, kEmptySizes)}); | 836 kPageURL, {FaviconURL(kIconURL3, FAVICON, kEmptySizes)}, GURL()); |
| 749 | 837 |
| 750 // Finalizes download, which should be thrown away as the favicon URLs were | 838 // Finalizes download, which should be thrown away as the favicon URLs were |
| 751 // updated. | 839 // updated. |
| 752 EXPECT_TRUE(delegate_.fake_image_downloader().RunCallbackManually()); | 840 EXPECT_TRUE(delegate_.fake_image_downloader().RunCallbackManually()); |
| 753 base::RunLoop().RunUntilIdle(); | 841 base::RunLoop().RunUntilIdle(); |
| 754 | 842 |
| 755 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kIconURL3)); | 843 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kIconURL3)); |
| 756 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL3)); | 844 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL3)); |
| 757 } | 845 } |
| 758 | 846 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 774 RunHandlerWithSimpleFaviconCandidates(URLVector{kIconURL1}); | 862 RunHandlerWithSimpleFaviconCandidates(URLVector{kIconURL1}); |
| 775 | 863 |
| 776 ASSERT_TRUE(VerifyAndClearExpectations()); | 864 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 777 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); | 865 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); |
| 778 | 866 |
| 779 // SetFavicons() and OnFaviconUpdated() should be called for the new icon URL | 867 // SetFavicons() and OnFaviconUpdated() should be called for the new icon URL |
| 780 // and not |kIconURL1|. | 868 // and not |kIconURL1|. |
| 781 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL2, _, _)); | 869 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL2, _, _)); |
| 782 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2, _, _)); | 870 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2, _, _)); |
| 783 | 871 |
| 784 handler->OnUpdateCandidates(kPageURL, | 872 handler->OnUpdateCandidates( |
| 785 {FaviconURL(kIconURL2, FAVICON, kEmptySizes)}); | 873 kPageURL, {FaviconURL(kIconURL2, FAVICON, kEmptySizes)}, GURL()); |
| 786 | 874 |
| 787 // Finalizes the DB lookup, which should be thrown away as the favicon URLs | 875 // Finalizes the DB lookup, which should be thrown away as the favicon URLs |
| 788 // were updated. | 876 // were updated. |
| 789 EXPECT_TRUE(favicon_service_.fake()->RunCallbackManually()); | 877 EXPECT_TRUE(favicon_service_.fake()->RunCallbackManually()); |
| 790 base::RunLoop().RunUntilIdle(); | 878 base::RunLoop().RunUntilIdle(); |
| 791 | 879 |
| 792 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kIconURL2)); | 880 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kIconURL2)); |
| 793 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2)); | 881 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2)); |
| 794 } | 882 } |
| 795 | 883 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 812 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates( | 900 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates( |
| 813 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls); | 901 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls); |
| 814 | 902 |
| 815 ASSERT_THAT(favicon_service_.fake()->db_requests(), | 903 ASSERT_THAT(favicon_service_.fake()->db_requests(), |
| 816 ElementsAre(kPageURL, kIconURL64x64, kSlowLoadingIconURL)); | 904 ElementsAre(kPageURL, kIconURL64x64, kSlowLoadingIconURL)); |
| 817 ASSERT_TRUE(VerifyAndClearExpectations()); | 905 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 818 ASSERT_TRUE(delegate_.fake_image_downloader().HasPendingManualCallback()); | 906 ASSERT_TRUE(delegate_.fake_image_downloader().HasPendingManualCallback()); |
| 819 | 907 |
| 820 // Calling OnUpdateCandidates() with the same icon URLs should have no effect, | 908 // Calling OnUpdateCandidates() with the same icon URLs should have no effect, |
| 821 // despite the ongoing download. | 909 // despite the ongoing download. |
| 822 handler->OnUpdateCandidates(kPageURL, favicon_urls); | 910 handler->OnUpdateCandidates(kPageURL, favicon_urls, GURL()); |
| 823 base::RunLoop().RunUntilIdle(); | 911 base::RunLoop().RunUntilIdle(); |
| 824 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty()); | 912 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty()); |
| 825 EXPECT_THAT(delegate_.downloads(), IsEmpty()); | 913 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| 826 | 914 |
| 827 // Complete the download. | 915 // Complete the download. |
| 828 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)); | 916 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)); |
| 829 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)); | 917 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)); |
| 830 EXPECT_TRUE(delegate_.fake_image_downloader().RunCallbackManually()); | 918 EXPECT_TRUE(delegate_.fake_image_downloader().RunCallbackManually()); |
| 831 base::RunLoop().RunUntilIdle(); | 919 base::RunLoop().RunUntilIdle(); |
| 832 EXPECT_THAT(delegate_.downloads(), IsEmpty()); | 920 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 846 | 934 |
| 847 // Ongoing database lookup. | 935 // Ongoing database lookup. |
| 848 ASSERT_THAT(favicon_service_.fake()->db_requests(), | 936 ASSERT_THAT(favicon_service_.fake()->db_requests(), |
| 849 ElementsAre(kPageURL, kIconURL64x64)); | 937 ElementsAre(kPageURL, kIconURL64x64)); |
| 850 ASSERT_THAT(delegate_.downloads(), IsEmpty()); | 938 ASSERT_THAT(delegate_.downloads(), IsEmpty()); |
| 851 ASSERT_TRUE(VerifyAndClearExpectations()); | 939 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 852 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); | 940 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); |
| 853 | 941 |
| 854 // Calling OnUpdateCandidates() with the same icon URLs should have no effect, | 942 // Calling OnUpdateCandidates() with the same icon URLs should have no effect, |
| 855 // despite the ongoing DB lookup. | 943 // despite the ongoing DB lookup. |
| 856 handler->OnUpdateCandidates(kPageURL, favicon_urls); | 944 handler->OnUpdateCandidates(kPageURL, favicon_urls, GURL()); |
| 857 base::RunLoop().RunUntilIdle(); | 945 base::RunLoop().RunUntilIdle(); |
| 858 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty()); | 946 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty()); |
| 859 EXPECT_THAT(delegate_.downloads(), IsEmpty()); | 947 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| 860 | 948 |
| 861 // Complete the lookup. | 949 // Complete the lookup. |
| 862 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)); | 950 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)); |
| 863 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)); | 951 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)); |
| 864 EXPECT_TRUE(favicon_service_.fake()->RunCallbackManually()); | 952 EXPECT_TRUE(favicon_service_.fake()->RunCallbackManually()); |
| 865 base::RunLoop().RunUntilIdle(); | 953 base::RunLoop().RunUntilIdle(); |
| 866 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL64x64)); | 954 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL64x64)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 877 | 965 |
| 878 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates( | 966 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates( |
| 879 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls); | 967 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls); |
| 880 | 968 |
| 881 ASSERT_TRUE(VerifyAndClearExpectations()); | 969 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 882 | 970 |
| 883 // Calling OnUpdateCandidates() with identical data should be a no-op. | 971 // Calling OnUpdateCandidates() with identical data should be a no-op. |
| 884 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); | 972 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); |
| 885 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); | 973 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); |
| 886 | 974 |
| 887 handler->OnUpdateCandidates(kPageURL, favicon_urls); | 975 handler->OnUpdateCandidates(kPageURL, favicon_urls, GURL()); |
| 888 base::RunLoop().RunUntilIdle(); | 976 base::RunLoop().RunUntilIdle(); |
| 889 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty()); | 977 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty()); |
| 890 EXPECT_THAT(delegate_.downloads(), IsEmpty()); | 978 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| 891 } | 979 } |
| 892 | 980 |
| 893 // Fixes crbug.com/544560 | 981 // Fixes crbug.com/544560 |
| 894 // Tests that Delegate::OnFaviconUpdated() is called if: | 982 // Tests that Delegate::OnFaviconUpdated() is called if: |
| 895 // - The best icon on the initial page is not the last icon. | 983 // - The best icon on the initial page is not the last icon. |
| 896 // - All of the initial page's icons are downloaded. | 984 // - All of the initial page's icons are downloaded. |
| 897 // AND | 985 // AND |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 919 // database and downloaded. |kIconURL2| should have been fetched from the | 1007 // database and downloaded. |kIconURL2| should have been fetched from the |
| 920 // database and downloaded last. | 1008 // database and downloaded last. |
| 921 ASSERT_THAT(delegate_.downloads(), ElementsAre(kIconURL1, kIconURL2)); | 1009 ASSERT_THAT(delegate_.downloads(), ElementsAre(kIconURL1, kIconURL2)); |
| 922 ASSERT_THAT(favicon_service_.fake()->db_requests(), | 1010 ASSERT_THAT(favicon_service_.fake()->db_requests(), |
| 923 ElementsAre(kPageURL, kIconURL1, kIconURL2)); | 1011 ElementsAre(kPageURL, kIconURL1, kIconURL2)); |
| 924 ASSERT_TRUE(VerifyAndClearExpectations()); | 1012 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 925 | 1013 |
| 926 // Simulate the page changing it's icon URL to just |kIconURL2| via | 1014 // Simulate the page changing it's icon URL to just |kIconURL2| via |
| 927 // Javascript. | 1015 // Javascript. |
| 928 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2, _, _)); | 1016 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2, _, _)); |
| 929 handler->OnUpdateCandidates(kPageURL, | 1017 handler->OnUpdateCandidates( |
| 930 {FaviconURL(kIconURL2, FAVICON, kEmptySizes)}); | 1018 kPageURL, {FaviconURL(kIconURL2, FAVICON, kEmptySizes)}, GURL()); |
| 931 base::RunLoop().RunUntilIdle(); | 1019 base::RunLoop().RunUntilIdle(); |
| 932 } | 1020 } |
| 933 | 1021 |
| 934 // Test the favicon which is selected when the web page provides several | 1022 // Test the favicon which is selected when the web page provides several |
| 935 // favicons and none of the favicons are cached in history. | 1023 // favicons and none of the favicons are cached in history. |
| 936 // The goal of this test is to be more of an integration test than | 1024 // The goal of this test is to be more of an integration test than |
| 937 // SelectFaviconFramesTest.*. | 1025 // SelectFaviconFramesTest.*. |
| 938 class FaviconHandlerMultipleFaviconsTest : public FaviconHandlerTest { | 1026 class FaviconHandlerMultipleFaviconsTest : public FaviconHandlerTest { |
| 939 protected: | 1027 protected: |
| 940 FaviconHandlerMultipleFaviconsTest() { | 1028 FaviconHandlerMultipleFaviconsTest() { |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1363 .WillByDefault(Return(true)); | 1451 .WillByDefault(Return(true)); |
| 1364 | 1452 |
| 1365 RunHandlerWithSimpleFaviconCandidates({k404IconURL}); | 1453 RunHandlerWithSimpleFaviconCandidates({k404IconURL}); |
| 1366 | 1454 |
| 1367 EXPECT_THAT( | 1455 EXPECT_THAT( |
| 1368 histogram_tester.GetAllSamples("Favicons.DownloadOutcome"), | 1456 histogram_tester.GetAllSamples("Favicons.DownloadOutcome"), |
| 1369 ElementsAre(base::Bucket(static_cast<int>(DownloadOutcome::SKIPPED), | 1457 ElementsAre(base::Bucket(static_cast<int>(DownloadOutcome::SKIPPED), |
| 1370 /*expected_count=*/1))); | 1458 /*expected_count=*/1))); |
| 1371 } | 1459 } |
| 1372 | 1460 |
| 1461 // Test that the support for Web Manifest is disabled by default, unless the | |
| 1462 // feature is enabled. | |
| 1463 TEST_F(FaviconHandlerTest, IgnoreWebManifestByDefault) { | |
| 1464 const GURL kManifestURL("http://www.google.com/manifest.json"); | |
| 1465 | |
| 1466 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}, kManifestURL); | |
| 1467 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1468 Not(Contains(kManifestURL))); | |
| 1469 EXPECT_THAT(delegate_.downloads(), Not(Contains(kManifestURL))); | |
| 1470 } | |
| 1471 | |
| 1472 class FaviconHandlerManifestsEnabledTest : public FaviconHandlerTest { | |
| 1473 protected: | |
| 1474 const GURL kManifestURL = GURL("http://www.google.com/manifest.json"); | |
| 1475 | |
| 1476 FaviconHandlerManifestsEnabledTest() { | |
| 1477 override_features_.InitAndEnableFeature(kFaviconsFromWebManifest); | |
| 1478 } | |
| 1479 | |
| 1480 private: | |
| 1481 base::test::ScopedFeatureList override_features_; | |
| 1482 | |
| 1483 DISALLOW_COPY_AND_ASSIGN(FaviconHandlerManifestsEnabledTest); | |
| 1484 }; | |
| 1485 | |
| 1486 // Test that a favicon corresponding to a web manifest is reported when: | |
| 1487 // - There is data in the favicon database for the manifest URL. | |
| 1488 // AND | |
| 1489 // - FaviconService::OnFaviconDataForManifestFromFaviconService() runs before | |
| 1490 // FaviconHandler::OnUpdateCandidates() is called. | |
| 1491 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1492 GetFaviconFromManifestInHistoryIfCandidatesSlower) { | |
| 1493 favicon_service_.fake()->Store(kPageURL, kManifestURL, | |
| 1494 CreateRawBitmapResult(kManifestURL)); | |
| 1495 | |
| 1496 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0); | |
| 1497 | |
| 1498 EXPECT_CALL(favicon_service_, | |
| 1499 UpdateFaviconMappingsAndFetch(_, kManifestURL, FAVICON, | |
| 1500 /*desired_size_in_dip=*/16, _, _)); | |
| 1501 EXPECT_CALL(delegate_, | |
| 1502 OnFaviconUpdated(_, FaviconDriverObserver::NON_TOUCH_16_DIP, | |
| 1503 kManifestURL, _, _)); | |
| 1504 | |
| 1505 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1506 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1507 ElementsAre(kPageURL, kManifestURL)); | |
| 1508 EXPECT_THAT(delegate_.downloads(), IsEmpty()); | |
| 1509 } | |
| 1510 | |
| 1511 // Test that a favicon corresponding to a web manifest is reported when: | |
| 1512 // - There is data in the favicon database for the manifest URL. | |
| 1513 // AND | |
| 1514 // - FaviconHandler::OnUpdateCandidates() is called before | |
| 1515 // FaviconService::OnFaviconDataForManifestFromFaviconService() runs. | |
| 1516 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1517 GetFaviconFromManifestInHistoryIfCandidatesFaster) { | |
| 1518 favicon_service_.fake()->Store(kPageURL, kManifestURL, | |
| 1519 CreateRawBitmapResult(kManifestURL)); | |
| 1520 // Defer the database lookup completion to control the exact timing. | |
| 1521 favicon_service_.fake()->SetRunCallbackManuallyForUrl(kManifestURL); | |
| 1522 | |
| 1523 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0); | |
| 1524 | |
| 1525 EXPECT_CALL(favicon_service_, | |
| 1526 UpdateFaviconMappingsAndFetch(_, kManifestURL, FAVICON, | |
| 1527 /*desired_size_in_dip=*/16, _, _)); | |
| 1528 EXPECT_CALL(delegate_, | |
| 1529 OnFaviconUpdated(_, FaviconDriverObserver::NON_TOUCH_16_DIP, | |
| 1530 kManifestURL, _, _)); | |
| 1531 | |
| 1532 std::unique_ptr<FaviconHandler> handler = | |
| 1533 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1534 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); | |
| 1535 | |
| 1536 // Complete the lookup. | |
| 1537 EXPECT_TRUE(favicon_service_.fake()->RunCallbackManually()); | |
| 1538 base::RunLoop().RunUntilIdle(); | |
| 1539 | |
| 1540 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1541 ElementsAre(kPageURL, kManifestURL)); | |
| 1542 EXPECT_THAT(delegate_.downloads(), IsEmpty()); | |
| 1543 } | |
| 1544 | |
| 1545 // Test that a favicon corresponding to a web manifest is reported when there is | |
| 1546 // data in the database for neither the page URL nor the manifest URL. | |
| 1547 TEST_F(FaviconHandlerManifestsEnabledTest, GetFaviconFromUnknownManifest) { | |
| 1548 const std::vector<favicon::FaviconURL> kManifestIcons = { | |
| 1549 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes), | |
| 1550 }; | |
| 1551 | |
| 1552 delegate_.fake_manifest_downloader().Add(kManifestURL, kManifestIcons); | |
| 1553 | |
| 1554 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0); | |
| 1555 | |
| 1556 EXPECT_CALL(favicon_service_, SetFavicons(_, kManifestURL, FAVICON, _)); | |
| 1557 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL, _, _)); | |
| 1558 | |
| 1559 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1560 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1561 ElementsAre(kPageURL, kManifestURL)); | |
| 1562 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL, kIconURL16x16)); | |
| 1563 } | |
| 1564 | |
| 1565 // Test that the manifest and icon are redownloaded if the icon cached for the | |
| 1566 // page URL expired. | |
| 1567 TEST_F(FaviconHandlerManifestsEnabledTest, GetFaviconFromExpiredManifest) { | |
| 1568 const std::vector<favicon::FaviconURL> kManifestIcons = { | |
| 1569 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes), | |
| 1570 }; | |
| 1571 | |
| 1572 favicon_service_.fake()->Store(kPageURL, kManifestURL, | |
| 1573 CreateRawBitmapResult(kManifestURL, FAVICON, | |
| 1574 /*expired=*/true)); | |
| 1575 delegate_.fake_manifest_downloader().Add(kManifestURL, kManifestIcons); | |
| 1576 | |
| 1577 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL, _, _)).Times(2); | |
| 1578 EXPECT_CALL(favicon_service_, SetFavicons(_, kManifestURL, _, _)); | |
| 1579 | |
| 1580 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1581 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1582 ElementsAre(kPageURL, kManifestURL)); | |
| 1583 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL, kIconURL64x64)); | |
| 1584 } | |
| 1585 | |
| 1586 // Test that the manifest and icon are redownloaded if the icon cached for the | |
| 1587 // manifest URL expired, which was observed during a visit to a different page | |
| 1588 // URL. | |
| 1589 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1590 GetFaviconFromExpiredManifestLinkedFromOtherPage) { | |
| 1591 const GURL kSomePreviousPageURL("https://www.google.com/previous"); | |
| 1592 const std::vector<favicon::FaviconURL> kManifestIcons = { | |
| 1593 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes), | |
| 1594 }; | |
| 1595 | |
| 1596 favicon_service_.fake()->Store(kSomePreviousPageURL, kManifestURL, | |
| 1597 CreateRawBitmapResult(kManifestURL, FAVICON, | |
| 1598 /*expired=*/true)); | |
| 1599 delegate_.fake_manifest_downloader().Add(kManifestURL, kManifestIcons); | |
| 1600 | |
| 1601 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL, _, _)).Times(2); | |
| 1602 EXPECT_CALL(favicon_service_, SetFavicons(_, kManifestURL, _, _)); | |
| 1603 | |
| 1604 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1605 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1606 ElementsAre(kPageURL, kManifestURL)); | |
| 1607 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL, kIconURL64x64)); | |
| 1608 } | |
| 1609 | |
| 1610 // Test that a favicon corresponding to a web manifest is reported when: | |
| 1611 // - There is data in the database for neither the page URL nor the manifest | |
| 1612 // URL. | |
| 1613 // - There is data in the database for the icon URL listed in the manifest. | |
| 1614 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1615 GetFaviconFromUnknownManifestButKnownIcon) { | |
| 1616 const GURL kSomePreviousPageURL("https://www.google.com/previous"); | |
| 1617 const std::vector<favicon::FaviconURL> kManifestIcons = { | |
| 1618 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes), | |
| 1619 }; | |
| 1620 | |
| 1621 favicon_service_.fake()->Store(kSomePreviousPageURL, kIconURL16x16, | |
| 1622 CreateRawBitmapResult(kIconURL16x16)); | |
| 1623 delegate_.fake_manifest_downloader().Add(kManifestURL, kManifestIcons); | |
| 1624 | |
| 1625 EXPECT_CALL(favicon_service_, SetFavicons(_, kManifestURL, _, _)); | |
| 1626 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL, _, _)); | |
| 1627 | |
| 1628 RunHandlerWithSimpleFaviconCandidates(URLVector(), kManifestURL); | |
| 1629 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1630 ElementsAre(kPageURL, kManifestURL)); | |
| 1631 // This is because, in the current implementation, FaviconHandler only checks | |
| 1632 // whether there is an icon cached with the manifest URL as the "icon URL" | |
| 1633 // when a page has a non-empty Web Manifest. | |
| 1634 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL, kIconURL16x16)); | |
| 1635 } | |
| 1636 | |
| 1637 // Test a manifest that returns a 404 gets blacklisted via | |
|
pkotwicz
2017/05/17 05:20:34
Nit: "Test" -> "Test that"
| |
| 1638 // UnableToDownloadFavicon() AND that the regular favicon is selected as | |
| 1639 // fallback. | |
| 1640 TEST_F(FaviconHandlerManifestsEnabledTest, UnknownManifestReturning404) { | |
| 1641 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kManifestURL)); | |
| 1642 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL12x12, _, _)); | |
| 1643 | |
| 1644 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1645 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1646 ElementsAre(kPageURL, kManifestURL, kIconURL12x12)); | |
| 1647 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL, kIconURL12x12)); | |
| 1648 } | |
| 1649 | |
| 1650 // Test that a manifest that was previously blacklisted via | |
| 1651 // UnableToDownloadFavicon() is ignored and that the regular favicon is selected | |
| 1652 // as fallback. | |
| 1653 TEST_F(FaviconHandlerManifestsEnabledTest, IgnoreManifestWithPrior404) { | |
| 1654 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(kManifestURL)) | |
| 1655 .WillByDefault(Return(true)); | |
| 1656 | |
| 1657 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL12x12, _, _)); | |
| 1658 | |
| 1659 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1660 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1661 ElementsAre(kPageURL, kIconURL12x12)); | |
| 1662 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL12x12)); | |
| 1663 } | |
| 1664 | |
| 1665 // Test that the regular favicon is selected when: | |
| 1666 // - The page links to a Web Manifest. | |
| 1667 // - The Web Manifest does not contain any icon URLs (it is not a 404). | |
| 1668 // - The page has an icon URL provided via a <link rel="icon"> tag. | |
| 1669 // - The database does not know about the page URL, manifest URL or icon URL. | |
| 1670 TEST_F(FaviconHandlerManifestsEnabledTest, UnknownManifestWithoutIcons) { | |
| 1671 delegate_.fake_manifest_downloader().Add(kManifestURL, | |
| 1672 std::vector<favicon::FaviconURL>()); | |
| 1673 | |
| 1674 // UnableToDownloadFavicon() is expected to prevent repeated downloads of the | |
| 1675 // same manifest (which is not otherwise cached, since it doesn't contain | |
| 1676 // icons). | |
| 1677 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kManifestURL)); | |
| 1678 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL12x12, _, _)); | |
| 1679 | |
| 1680 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1681 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1682 ElementsAre(kPageURL, kManifestURL, kIconURL12x12)); | |
| 1683 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL, kIconURL12x12)); | |
| 1684 } | |
| 1685 | |
| 1686 // Test that the regular favicon is selected when: | |
| 1687 // - The page links to a Web Manifest. | |
| 1688 // - The Web Manifest does not contain any icon URLs (it is not a 404). | |
| 1689 // - The page has an icon URL provided via a <link rel="icon"> tag. | |
| 1690 // - The database does not know about the page URL. | |
| 1691 // - The database does not know about the manifest URL. | |
| 1692 // - The database knows about the icon URL. | |
| 1693 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1694 UnknownManifestWithoutIconsAndKnownRegularIcons) { | |
| 1695 const GURL kSomePreviousPageURL("https://www.google.com/previous"); | |
| 1696 | |
| 1697 delegate_.fake_manifest_downloader().Add(kManifestURL, | |
| 1698 std::vector<favicon::FaviconURL>()); | |
| 1699 favicon_service_.fake()->Store(kSomePreviousPageURL, kIconURL12x12, | |
| 1700 CreateRawBitmapResult(kIconURL12x12)); | |
| 1701 | |
| 1702 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); | |
| 1703 | |
| 1704 // UnableToDownloadFavicon() is expected to prevent repeated downloads of the | |
| 1705 // same manifest (which is not otherwise cached, since it doesn't contain | |
| 1706 // icons). | |
| 1707 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(kManifestURL)); | |
| 1708 EXPECT_CALL(favicon_service_, | |
| 1709 UpdateFaviconMappingsAndFetch(_, kManifestURL, _, _, _, _)); | |
| 1710 EXPECT_CALL(favicon_service_, | |
| 1711 UpdateFaviconMappingsAndFetch(_, kIconURL12x12, _, _, _, _)); | |
| 1712 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL12x12, _, _)); | |
| 1713 | |
| 1714 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1715 EXPECT_THAT(favicon_service_.fake()->db_requests(), | |
| 1716 ElementsAre(kPageURL, kManifestURL, kIconURL12x12)); | |
| 1717 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL)); | |
| 1718 } | |
| 1719 | |
| 1720 // Test that the database remains unmodified when: | |
| 1721 // - The page links to a Web Manifest. | |
| 1722 // - The Web Manifest does not contain any icon URLs (it is not a 404). | |
| 1723 // - The page has an icon URL provided via a <link rel="icon"> tag. | |
| 1724 // - The database has a mapping between the page URL to the favicon URL. | |
| 1725 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1726 UnknownManifestWithoutIconsAndRegularIconInHistory) { | |
| 1727 delegate_.fake_manifest_downloader().Add(kManifestURL, | |
| 1728 std::vector<favicon::FaviconURL>()); | |
| 1729 favicon_service_.fake()->Store(kPageURL, kIconURL16x16, | |
| 1730 CreateRawBitmapResult(kIconURL16x16)); | |
| 1731 | |
| 1732 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)); | |
| 1733 EXPECT_CALL(favicon_service_, | |
| 1734 UpdateFaviconMappingsAndFetch(_, kManifestURL, _, _, _, _)); | |
| 1735 | |
| 1736 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}, kManifestURL); | |
| 1737 | |
| 1738 ASSERT_THAT(favicon_service_.fake()->db_requests(), | |
| 1739 ElementsAre(kPageURL, kManifestURL)); | |
| 1740 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL)); | |
| 1741 } | |
| 1742 | |
| 1743 // Test that Delegate::OnFaviconUpdated() is called if a page uses Javascript to | |
| 1744 // modify the page's <link rel="manifest"> tag to point to a different manifest. | |
| 1745 TEST_F(FaviconHandlerManifestsEnabledTest, ManifestUpdateViaJavascript) { | |
| 1746 const GURL kManifestURL1("http://www.google.com/manifest1.json"); | |
| 1747 const GURL kManifestURL2("http://www.google.com/manifest2.json"); | |
| 1748 const std::vector<favicon::FaviconURL> kManifestIcons1 = { | |
| 1749 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes), | |
| 1750 }; | |
| 1751 const std::vector<favicon::FaviconURL> kManifestIcons2 = { | |
| 1752 FaviconURL(kIconURL10x10, FAVICON, kEmptySizes), | |
| 1753 }; | |
| 1754 | |
| 1755 delegate_.fake_manifest_downloader().Add(kManifestURL1, kManifestIcons1); | |
| 1756 delegate_.fake_manifest_downloader().Add(kManifestURL2, kManifestIcons2); | |
| 1757 | |
| 1758 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL1, _, _)); | |
| 1759 | |
| 1760 std::unique_ptr<FaviconHandler> handler = | |
| 1761 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL1); | |
| 1762 ASSERT_THAT(favicon_service_.fake()->db_requests(), | |
| 1763 ElementsAre(kPageURL, kManifestURL1)); | |
| 1764 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL1, kIconURL64x64)); | |
| 1765 ASSERT_TRUE(VerifyAndClearExpectations()); | |
| 1766 | |
| 1767 // Simulate the page changing it's manifest URL via Javascript. | |
| 1768 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL2, _, _)); | |
| 1769 handler->OnUpdateCandidates(kPageURL, | |
| 1770 {FaviconURL(kIconURL12x12, FAVICON, kEmptySizes)}, | |
| 1771 kManifestURL2); | |
| 1772 base::RunLoop().RunUntilIdle(); | |
| 1773 ASSERT_THAT(favicon_service_.fake()->db_requests(), | |
| 1774 ElementsAre(kManifestURL2)); | |
| 1775 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL2, kIconURL10x10)); | |
| 1776 } | |
| 1777 | |
| 1778 // Test that Delegate::OnFaviconUpdated() is called if a page uses Javascript to | |
| 1779 // remove the page's <link rel="manifest"> tag (i.e. no web manifest) WHILE a | |
| 1780 // lookup to the history database is ongoing for the manifest URL. | |
| 1781 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1782 RemoveManifestViaJavascriptWhileDatabaseLookup) { | |
| 1783 const std::vector<favicon::FaviconURL> kManifestIcons = { | |
| 1784 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes), | |
| 1785 }; | |
| 1786 | |
| 1787 delegate_.fake_manifest_downloader().Add(kManifestURL, kManifestIcons); | |
| 1788 // Defer the database lookup completion to control the exact timing. | |
| 1789 favicon_service_.fake()->SetRunCallbackManuallyForUrl(kManifestURL); | |
| 1790 | |
| 1791 std::unique_ptr<FaviconHandler> handler = | |
| 1792 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}, kManifestURL); | |
| 1793 ASSERT_THAT(favicon_service_.fake()->db_requests(), | |
| 1794 ElementsAre(kPageURL, kManifestURL)); | |
| 1795 // Database lookup for |kManifestURL| is ongoing. | |
| 1796 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); | |
| 1797 | |
| 1798 // Simulate the page changing it's manifest URL to empty via Javascript. | |
| 1799 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL12x12, _, _)); | |
| 1800 handler->OnUpdateCandidates( | |
| 1801 kPageURL, {FaviconURL(kIconURL12x12, FAVICON, kEmptySizes)}, GURL()); | |
| 1802 // Complete the lookup. | |
| 1803 EXPECT_TRUE(favicon_service_.fake()->RunCallbackManually()); | |
| 1804 base::RunLoop().RunUntilIdle(); | |
| 1805 ASSERT_THAT(favicon_service_.fake()->db_requests(), | |
| 1806 ElementsAre(kPageURL, kManifestURL, kIconURL12x12)); | |
| 1807 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL12x12)); | |
| 1808 } | |
| 1809 | |
| 1810 // Test that Delegate::OnFaviconUpdated() is called a page without manifest uses | |
| 1811 // Javascript to add a <link rel="manifest"> tag (i.e. a new web manifest) WHILE | |
| 1812 // a lookup to the history database is ongoing for the icon URL. | |
| 1813 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1814 AddManifestViaJavascriptWhileDatabaseLookup) { | |
| 1815 const std::vector<favicon::FaviconURL> kManifestIcons = { | |
| 1816 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes), | |
| 1817 }; | |
| 1818 | |
| 1819 delegate_.fake_manifest_downloader().Add(kManifestURL, kManifestIcons); | |
| 1820 // Defer the database lookup completion to control the exact timing. | |
| 1821 favicon_service_.fake()->SetRunCallbackManuallyForUrl(kIconURL12x12); | |
| 1822 | |
| 1823 std::unique_ptr<FaviconHandler> handler = | |
| 1824 RunHandlerWithSimpleFaviconCandidates({kIconURL12x12}); | |
| 1825 ASSERT_THAT(favicon_service_.fake()->db_requests(), | |
| 1826 ElementsAre(kPageURL, kIconURL12x12)); | |
| 1827 // Database lookup for |kIconURL12x12| is ongoing. | |
| 1828 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); | |
| 1829 | |
| 1830 // Simulate the page changing it's manifest URL to |kManifestURL| via | |
| 1831 // Javascript. | |
| 1832 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL, _, _)); | |
| 1833 handler->OnUpdateCandidates(kPageURL, | |
| 1834 {FaviconURL(kIconURL12x12, FAVICON, kEmptySizes)}, | |
| 1835 kManifestURL); | |
| 1836 // Complete the lookup. | |
| 1837 EXPECT_TRUE(favicon_service_.fake()->RunCallbackManually()); | |
| 1838 base::RunLoop().RunUntilIdle(); | |
| 1839 ASSERT_THAT(favicon_service_.fake()->db_requests(), | |
| 1840 ElementsAre(kPageURL, kIconURL12x12, kManifestURL)); | |
| 1841 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL, kIconURL64x64)); | |
| 1842 } | |
| 1843 | |
| 1844 // Test that SetFavicons() is not called when: | |
| 1845 // - The page doesn't initially link to a Web Manifest. | |
| 1846 // - The page has an icon URL provided via a <link rel="icon"> tag. | |
| 1847 // - The database does not know about the page URL or icon URL. | |
| 1848 // - While the icon is being downloaded, the page uses Javascript to add a | |
| 1849 // <link rel="manifest"> tag. | |
| 1850 // - The database has bitmap data for the manifest URL. | |
| 1851 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1852 AddKnownManifestViaJavascriptWhileImageDownload) { | |
| 1853 const GURL kSomePreviousPageURL("https://www.google.com/previous"); | |
| 1854 | |
| 1855 favicon_service_.fake()->Store(kSomePreviousPageURL, kManifestURL, | |
| 1856 CreateRawBitmapResult(kManifestURL)); | |
| 1857 | |
| 1858 // Defer the image download completion to control the exact timing. | |
| 1859 delegate_.fake_image_downloader().SetRunCallbackManuallyForUrl(kIconURL16x16); | |
| 1860 | |
| 1861 std::unique_ptr<FaviconHandler> handler = | |
| 1862 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}); | |
| 1863 | |
| 1864 ASSERT_TRUE(VerifyAndClearExpectations()); | |
| 1865 ASSERT_TRUE(delegate_.fake_image_downloader().HasPendingManualCallback()); | |
| 1866 | |
| 1867 // Simulate the page changing it's manifest URL to |kManifestURL| via | |
| 1868 // Javascript. Should invalidate the ongoing image download. | |
| 1869 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); | |
| 1870 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL, _, _)); | |
| 1871 | |
| 1872 handler->OnUpdateCandidates(kPageURL, | |
| 1873 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}, | |
| 1874 kManifestURL); | |
| 1875 | |
| 1876 // Finalizes download, which should be thrown away as the manifest URL was | |
| 1877 // provided. | |
| 1878 EXPECT_TRUE(delegate_.fake_image_downloader().RunCallbackManually()); | |
| 1879 base::RunLoop().RunUntilIdle(); | |
| 1880 } | |
| 1881 | |
| 1882 // Test that SetFavicons() is called with the icon URL when: | |
| 1883 // - The page doesn't initially link to a Web Manifest. | |
| 1884 // - The page has an icon URL provided via a <link rel="icon"> tag. | |
| 1885 // - The database does not know about the page URL or icon URL. | |
| 1886 // - During the database lookup, the page uses Javascript to add a | |
| 1887 // <link rel="manifest"> tag. | |
| 1888 // - The database does not know about the manifest URL. | |
| 1889 // - The manifest contains no icons. | |
| 1890 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1891 AddManifestWithoutIconsViaJavascriptWhileDatabaseLookup) { | |
| 1892 delegate_.fake_manifest_downloader().Add(kManifestURL, | |
| 1893 std::vector<favicon::FaviconURL>()); | |
| 1894 | |
| 1895 // Defer the database lookup completion to control the exact timing. | |
| 1896 favicon_service_.fake()->SetRunCallbackManuallyForUrl(kIconURL16x16); | |
| 1897 | |
| 1898 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); | |
| 1899 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); | |
| 1900 | |
| 1901 std::unique_ptr<FaviconHandler> handler = | |
| 1902 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}); | |
| 1903 | |
| 1904 ASSERT_TRUE(VerifyAndClearExpectations()); | |
| 1905 ASSERT_TRUE(favicon_service_.fake()->HasPendingManualCallback()); | |
| 1906 | |
| 1907 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL16x16, _, _)); | |
| 1908 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)); | |
| 1909 | |
| 1910 handler->OnUpdateCandidates(kPageURL, | |
| 1911 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}, | |
| 1912 kManifestURL); | |
| 1913 | |
| 1914 // Finalizes lookup, which should be thrown away as the manifest URLs was | |
| 1915 // provided. | |
| 1916 EXPECT_TRUE(favicon_service_.fake()->RunCallbackManually()); | |
| 1917 base::RunLoop().RunUntilIdle(); | |
| 1918 | |
| 1919 // The manifest URL interrupted the original processing of kIconURL16x16, but | |
| 1920 // a second one should have been started. | |
| 1921 EXPECT_TRUE(favicon_service_.fake()->RunCallbackManually()); | |
| 1922 base::RunLoop().RunUntilIdle(); | |
| 1923 } | |
| 1924 | |
| 1925 // Test that SetFavicons() is called when: | |
| 1926 // - The page links to one Web Manifest, which contains one icon. | |
| 1927 // - The database does not know about the page URL, icon URL or manifest URL. | |
| 1928 // - During image download, the page updates the manifest URL to point to | |
| 1929 // another manifest. | |
| 1930 // - The second manifest contains the same icons as the first. | |
| 1931 TEST_F(FaviconHandlerManifestsEnabledTest, | |
| 1932 UpdateManifestWithSameIconURLsWhileDownloading) { | |
| 1933 const GURL kManifestURL1("http://www.google.com/manifest1.json"); | |
| 1934 const GURL kManifestURL2("http://www.google.com/manifest2.json"); | |
| 1935 const std::vector<favicon::FaviconURL> kManifestIcons = { | |
| 1936 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes), | |
| 1937 }; | |
| 1938 | |
| 1939 delegate_.fake_manifest_downloader().Add(kManifestURL1, kManifestIcons); | |
| 1940 delegate_.fake_manifest_downloader().Add(kManifestURL2, kManifestIcons); | |
| 1941 | |
| 1942 // Defer the download completion to control the exact timing. | |
| 1943 delegate_.fake_image_downloader().SetRunCallbackManuallyForUrl(kIconURL64x64); | |
| 1944 | |
| 1945 std::unique_ptr<FaviconHandler> handler = | |
| 1946 RunHandlerWithSimpleFaviconCandidates(URLVector(), kManifestURL1); | |
| 1947 | |
| 1948 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL1, kIconURL64x64)); | |
| 1949 ASSERT_TRUE(VerifyAndClearExpectations()); | |
| 1950 ASSERT_TRUE(delegate_.fake_image_downloader().HasPendingManualCallback()); | |
| 1951 | |
| 1952 // Calling OnUpdateCandidates() with a different manifest URL should trigger | |
| 1953 // its download. | |
| 1954 handler->OnUpdateCandidates(kPageURL, std::vector<favicon::FaviconURL>(), | |
| 1955 kManifestURL2); | |
| 1956 base::RunLoop().RunUntilIdle(); | |
| 1957 EXPECT_THAT(delegate_.downloads(), ElementsAre(kManifestURL2, kIconURL64x64)); | |
| 1958 | |
| 1959 // Complete the download. | |
| 1960 EXPECT_CALL(favicon_service_, SetFavicons(_, kManifestURL2, _, _)); | |
| 1961 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kManifestURL2, _, _)); | |
| 1962 EXPECT_TRUE(delegate_.fake_image_downloader().RunCallbackManually()); | |
| 1963 base::RunLoop().RunUntilIdle(); | |
| 1964 } | |
| 1965 | |
| 1373 } // namespace | 1966 } // namespace |
| 1374 } // namespace favicon | 1967 } // namespace favicon |
| OLD | NEW |