| 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 <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/run_loop.h" |
| 17 #include "base/strings/stringprintf.h" |
| 14 #include "components/favicon/core/favicon_driver.h" | 18 #include "components/favicon/core/favicon_driver.h" |
| 19 #include "components/favicon/core/test/mock_favicon_service.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "ui/base/layout.h" | 23 #include "ui/base/layout.h" |
| 18 #include "ui/gfx/codec/png_codec.h" | 24 #include "ui/gfx/codec/png_codec.h" |
| 19 #include "ui/gfx/favicon_size.h" | 25 #include "ui/gfx/favicon_size.h" |
| 20 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 21 | 27 |
| 22 namespace favicon { | 28 namespace favicon { |
| 23 namespace { | 29 namespace { |
| 24 | 30 |
| 25 // Fill the given bmp with valid png data. | 31 using favicon_base::FAVICON; |
| 26 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { | 32 using favicon_base::FaviconRawBitmapResult; |
| 27 bmp->allocN32Pixels(w, h); | 33 using favicon_base::TOUCH_ICON; |
| 34 using favicon_base::TOUCH_PRECOMPOSED_ICON; |
| 35 using testing::Assign; |
| 36 using testing::ElementsAre; |
| 37 using testing::InSequence; |
| 38 using testing::Invoke; |
| 39 using testing::IsEmpty; |
| 40 using testing::Return; |
| 41 using testing::_; |
| 42 |
| 43 using IntVector = std::vector<int>; |
| 44 using URLVector = std::vector<GURL>; |
| 45 using BitmapVector = std::vector<SkBitmap>; |
| 46 using SizeVector = std::vector<gfx::Size>; |
| 47 |
| 48 MATCHER_P2(ImageSizeIs, width, height, "") { |
| 49 *result_listener << "where size is " << arg.Width() << "x" << arg.Height(); |
| 50 return arg.Size() == gfx::Size(width, height); |
| 51 } |
| 52 |
| 53 // Fill the given bmp with some test data. |
| 54 SkBitmap CreateBitmapWithEdgeSize(int size) { |
| 55 SkBitmap bmp; |
| 56 bmp.allocN32Pixels(size, size); |
| 28 | 57 |
| 29 unsigned char* src_data = | 58 unsigned char* src_data = |
| 30 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0)); | 59 reinterpret_cast<unsigned char*>(bmp.getAddr32(0, 0)); |
| 31 for (int i = 0; i < w * h; i++) { | 60 for (int i = 0; i < size * size; i++) { |
| 32 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); | 61 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); |
| 33 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); | 62 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); |
| 34 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255); | 63 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255); |
| 35 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255); | 64 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255); |
| 36 } | 65 } |
| 66 return bmp; |
| 37 } | 67 } |
| 38 | 68 |
| 39 // Fill the given data buffer with valid png data. | 69 // Fill the given data buffer with valid png data. |
| 40 void FillBitmap(int w, int h, std::vector<unsigned char>* output) { | 70 std::vector<unsigned char> FillBitmapWithEdgeSize(int size) { |
| 41 SkBitmap bitmap; | 71 SkBitmap bitmap = CreateBitmapWithEdgeSize(size); |
| 42 FillDataToBitmap(w, h, &bitmap); | 72 std::vector<unsigned char> output; |
| 43 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output); | 73 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &output); |
| 44 } | 74 return output; |
| 45 | 75 } |
| 46 void SetFaviconRawBitmapResult( | 76 |
| 77 std::vector<FaviconRawBitmapResult> CreateRawBitmapResult( |
| 47 const GURL& icon_url, | 78 const GURL& icon_url, |
| 48 favicon_base::IconType icon_type, | 79 favicon_base::IconType icon_type = FAVICON, |
| 49 bool expired, | 80 bool expired = false) { |
| 50 std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results) { | |
| 51 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); | 81 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); |
| 52 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); | 82 data->data() = FillBitmapWithEdgeSize(gfx::kFaviconSize); |
| 53 favicon_base::FaviconRawBitmapResult bitmap_result; | 83 FaviconRawBitmapResult bitmap_result; |
| 54 bitmap_result.expired = expired; | 84 bitmap_result.expired = expired; |
| 55 bitmap_result.bitmap_data = data; | 85 bitmap_result.bitmap_data = data; |
| 56 // Use a pixel size other than (0,0) as (0,0) has a special meaning. | 86 // Use a pixel size other than (0,0) as (0,0) has a special meaning. |
| 57 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); | 87 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); |
| 58 bitmap_result.icon_type = icon_type; | 88 bitmap_result.icon_type = icon_type; |
| 59 bitmap_result.icon_url = icon_url; | 89 bitmap_result.icon_url = icon_url; |
| 60 | 90 return {bitmap_result}; |
| 61 favicon_bitmap_results->push_back(bitmap_result); | 91 } |
| 62 } | 92 |
| 63 | 93 // Fake that implements the calls to FaviconHalder::Delegate's DownloadImage(), |
| 64 void SetFaviconRawBitmapResult( | 94 // delegated to this class through MockDelegate. |
| 65 const GURL& icon_url, | 95 class FakeImageDownloader { |
| 66 std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results) { | |
| 67 SetFaviconRawBitmapResult(icon_url, | |
| 68 favicon_base::FAVICON, | |
| 69 false /* expired */, | |
| 70 favicon_bitmap_results); | |
| 71 } | |
| 72 | |
| 73 // This class is used to save the download request for verifying with test case. | |
| 74 class DownloadHandler { | |
| 75 public: | 96 public: |
| 76 DownloadHandler() : callback_invoked_(false) {} | 97 struct Response { |
| 77 ~DownloadHandler() {} | 98 int http_status_code = 404; |
| 78 | 99 BitmapVector bitmaps; |
| 79 void Reset() { | 100 SizeVector original_bitmap_sizes; |
| 80 download_.reset(); | 101 }; |
| 81 callback_invoked_ = false; | 102 |
| 82 // Does not affect |should_fail_download_icon_urls_| and | 103 FakeImageDownloader() : next_download_id_(1) {} |
| 83 // |failed_download_icon_urls_|. | 104 |
| 84 } | 105 // Implementation of FaviconHalder::Delegate's DownloadImage(). If a given |
| 85 | 106 // URL is not known (i.e. not previously added via Add()), it produces 404s. |
| 86 // Make downloads for any of |icon_urls| fail. | 107 int DownloadImage(const GURL& url, |
| 87 void FailDownloadForIconURLs(const std::set<GURL>& icon_urls) { | 108 int max_image_size, |
| 88 should_fail_download_icon_urls_ = icon_urls; | 109 FaviconHandler::Delegate::ImageDownloadCallback callback) { |
| 89 } | 110 downloads_.push_back(url); |
| 90 | 111 |
| 91 // Returns whether a download for |icon_url| did fail. | 112 const Response& response = responses_[url]; |
| 92 bool DidFailDownloadForIconURL(const GURL& icon_url) const { | 113 int download_id = next_download_id_++; |
| 93 return failed_download_icon_urls_.count(icon_url); | 114 base::Closure bound_callback = |
| 94 } | 115 base::Bind(callback, download_id, response.http_status_code, url, |
| 95 | 116 response.bitmaps, response.original_bitmap_sizes); |
| 96 void AddDownload(int download_id, | 117 if (url == manual_callback_url_) |
| 97 const GURL& image_url, | 118 manual_callback_ = bound_callback; |
| 98 const std::vector<int>& image_sizes, | 119 else |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, bound_callback); |
| 121 return download_id; |
| 122 } |
| 123 |
| 124 void Add(const GURL& icon_url, const IntVector& sizes) { |
| 125 AddWithOriginalSizes(icon_url, sizes, sizes); |
| 126 } |
| 127 |
| 128 void AddWithOriginalSizes(const GURL& icon_url, |
| 129 const IntVector& sizes, |
| 130 const IntVector& original_sizes) { |
| 131 DCHECK_EQ(sizes.size(), original_sizes.size()); |
| 132 Response response; |
| 133 response.http_status_code = 200; |
| 134 for (int size : sizes) { |
| 135 response.original_bitmap_sizes.push_back(gfx::Size(size, size)); |
| 136 response.bitmaps.push_back(CreateBitmapWithEdgeSize(size)); |
| 137 } |
| 138 responses_[icon_url] = response; |
| 139 } |
| 140 |
| 141 void AddError(const GURL& icon_url, int http_status_code) { |
| 142 Response response; |
| 143 response.http_status_code = http_status_code; |
| 144 responses_[icon_url] = response; |
| 145 } |
| 146 |
| 147 // Disables automatic callback for |url|. This is useful for emulating a |
| 148 // download taking a long time. The callback for DownloadImage() will be |
| 149 // stored in |manual_callback_|. |
| 150 void SetRunCallbackManuallyForUrl(const GURL& url) { |
| 151 manual_callback_url_ = url; |
| 152 } |
| 153 |
| 154 // Returns whether an ongoing download exists for a url previously selected |
| 155 // via SetRunCallbackManuallyForUrl(). |
| 156 bool HasPendingManualCallback() { return !manual_callback_.is_null(); } |
| 157 |
| 158 // Triggers the response for a download previously selected for manual |
| 159 // triggering via SetRunCallbackManuallyForUrl(). |
| 160 bool RunCallbackManually() { |
| 161 if (!HasPendingManualCallback()) |
| 162 return false; |
| 163 manual_callback_.Run(); |
| 164 manual_callback_.Reset(); |
| 165 return true; |
| 166 } |
| 167 |
| 168 // Returns pending and completed download URLs. |
| 169 const URLVector& downloads() const { return downloads_; } |
| 170 |
| 171 void ClearDownloads() { downloads_.clear(); } |
| 172 |
| 173 private: |
| 174 int next_download_id_; |
| 175 |
| 176 // Pending and completed download URLs. |
| 177 URLVector downloads_; |
| 178 |
| 179 // URL to disable automatic callbacks for. |
| 180 GURL manual_callback_url_; |
| 181 |
| 182 // Callback for DownloadImage() request for |manual_callback_url_|. |
| 183 base::Closure manual_callback_; |
| 184 |
| 185 // Registered responses. |
| 186 std::map<GURL, Response> responses_; |
| 187 |
| 188 DISALLOW_COPY_AND_ASSIGN(FakeImageDownloader); |
| 189 }; |
| 190 |
| 191 class MockDelegate : public FaviconHandler::Delegate { |
| 192 public: |
| 193 MockDelegate() { |
| 194 // Delegate image downloading to FakeImageDownloader. |
| 195 ON_CALL(*this, DownloadImage(_, _, _)) |
| 196 .WillByDefault( |
| 197 Invoke(&fake_downloader_, &FakeImageDownloader::DownloadImage)); |
| 198 } |
| 199 |
| 200 MOCK_METHOD3(DownloadImage, |
| 201 int(const GURL& url, |
| 99 int max_image_size, | 202 int max_image_size, |
| 100 FaviconHandler::Delegate::ImageDownloadCallback callback) { | 203 ImageDownloadCallback callback)); |
| 101 download_.reset(new Download(download_id, image_url, image_sizes, | 204 MOCK_METHOD0(IsOffTheRecord, bool()); |
| 102 max_image_size, callback)); | 205 MOCK_METHOD1(IsBookmarked, bool(const GURL& url)); |
| 103 } | 206 MOCK_METHOD5(OnFaviconUpdated, |
| 104 | 207 void(const GURL& page_url, |
| 105 void InvokeCallback(); | 208 FaviconDriverObserver::NotificationIconType type, |
| 106 | 209 const GURL& icon_url, |
| 107 bool HasDownload() const { return download_.get(); } | 210 bool icon_url_changed, |
| 108 const GURL& GetImageUrl() const { return download_->image_url; } | 211 const gfx::Image& image)); |
| 109 void SetImageSizes(const std::vector<int>& sizes) { | 212 |
| 110 download_->image_sizes = sizes; } | 213 FakeImageDownloader& fake_downloader() { return fake_downloader_; } |
| 214 |
| 215 // Convenience getter for test readability. Returns pending and completed |
| 216 // download URLs. |
| 217 const URLVector& downloads() const { return fake_downloader_.downloads(); } |
| 111 | 218 |
| 112 private: | 219 private: |
| 113 struct Download { | 220 FakeImageDownloader fake_downloader_; |
| 114 Download(int id, | |
| 115 GURL url, | |
| 116 const std::vector<int>& sizes, | |
| 117 int max_size, | |
| 118 FaviconHandler::Delegate::ImageDownloadCallback callback) | |
| 119 : download_id(id), | |
| 120 image_url(url), | |
| 121 image_sizes(sizes), | |
| 122 max_image_size(max_size), | |
| 123 callback(callback) {} | |
| 124 ~Download() {} | |
| 125 int download_id; | |
| 126 GURL image_url; | |
| 127 std::vector<int> image_sizes; | |
| 128 int max_image_size; | |
| 129 FaviconHandler::Delegate::ImageDownloadCallback callback; | |
| 130 }; | |
| 131 | |
| 132 std::unique_ptr<Download> download_; | |
| 133 bool callback_invoked_; | |
| 134 | |
| 135 // The icon URLs for which the download should fail. | |
| 136 std::set<GURL> should_fail_download_icon_urls_; | |
| 137 | |
| 138 // The icon URLs for which the download did fail. This should be a subset of | |
| 139 // |should_fail_download_icon_urls_|. | |
| 140 std::set<GURL> failed_download_icon_urls_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(DownloadHandler); | |
| 143 }; | 221 }; |
| 144 | 222 |
| 145 // This class is used to save the history request for verifying with test case. | 223 // FakeFaviconService mimics a FaviconService backend that allows setting up |
| 146 // It also will be used to simulate the history response. | 224 // test data stored via Store(). If Store() has not been called for a |
| 147 class HistoryRequestHandler { | 225 // particular URL, the callback is called with empty database results. |
| 226 class FakeFaviconService { |
| 148 public: | 227 public: |
| 149 HistoryRequestHandler(const GURL& page_url, | 228 FakeFaviconService() = default; |
| 150 const GURL& icon_url, | 229 |
| 151 int icon_type, | 230 // Stores favicon with bitmap data in |results| at |page_url| and |icon_url|. |
| 152 const favicon_base::FaviconResultsCallback& callback) | 231 void Store(const GURL& page_url, |
| 153 : page_url_(page_url), | 232 const GURL& icon_url, |
| 154 icon_url_(icon_url), | 233 const std::vector<favicon_base::FaviconRawBitmapResult>& result) { |
| 155 icon_type_(icon_type), | 234 results_[icon_url] = result; |
| 156 callback_(callback) { | 235 results_[page_url] = result; |
| 157 } | 236 } |
| 158 | 237 |
| 159 HistoryRequestHandler(const GURL& page_url, | 238 // Returns pending and completed database request URLs. |
| 160 const GURL& icon_url, | 239 const URLVector& db_requests() const { return db_requests_; } |
| 161 int icon_type, | 240 |
| 162 const std::vector<unsigned char>& bitmap_data, | 241 void ClearDbRequests() { db_requests_.clear(); } |
| 163 const gfx::Size& size) | 242 |
| 164 : page_url_(page_url), | 243 base::CancelableTaskTracker::TaskId GetFavicon( |
| 165 icon_url_(icon_url), | |
| 166 icon_type_(icon_type), | |
| 167 bitmap_data_(bitmap_data), | |
| 168 size_(size) { | |
| 169 } | |
| 170 | |
| 171 ~HistoryRequestHandler() {} | |
| 172 void InvokeCallback(); | |
| 173 | |
| 174 const GURL page_url_; | |
| 175 const GURL icon_url_; | |
| 176 const int icon_type_; | |
| 177 const std::vector<unsigned char> bitmap_data_; | |
| 178 const gfx::Size size_; | |
| 179 std::vector<favicon_base::FaviconRawBitmapResult> history_results_; | |
| 180 favicon_base::FaviconResultsCallback callback_; | |
| 181 | |
| 182 private: | |
| 183 DISALLOW_COPY_AND_ASSIGN(HistoryRequestHandler); | |
| 184 }; | |
| 185 | |
| 186 class TestDelegate : public FaviconHandler::Delegate { | |
| 187 public: | |
| 188 TestDelegate() : num_notifications_(0), download_id_(0) {} | |
| 189 | |
| 190 int DownloadImage(const GURL& image_url, | |
| 191 int max_bitmap_size, | |
| 192 ImageDownloadCallback callback) override { | |
| 193 // Do not do a download if downloading |image_url| failed previously. This | |
| 194 // emulates the behavior of FaviconDriver::DownloadImage() | |
| 195 if (download_handler_.DidFailDownloadForIconURL(image_url)) { | |
| 196 download_handler_.AddDownload(download_id_, image_url, std::vector<int>(), | |
| 197 0, callback); | |
| 198 return 0; | |
| 199 } | |
| 200 | |
| 201 download_id_++; | |
| 202 std::vector<int> sizes; | |
| 203 sizes.push_back(0); | |
| 204 download_handler_.AddDownload(download_id_, image_url, sizes, | |
| 205 max_bitmap_size, callback); | |
| 206 return download_id_; | |
| 207 } | |
| 208 | |
| 209 bool IsOffTheRecord() override { return false; } | |
| 210 | |
| 211 bool IsBookmarked(const GURL& url) override { return false; } | |
| 212 | |
| 213 void OnFaviconUpdated( | |
| 214 const GURL& page_url, | |
| 215 FaviconDriverObserver::NotificationIconType notification_icon_type, | |
| 216 const GURL& icon_url, | |
| 217 bool icon_url_changed, | |
| 218 const gfx::Image& image) override { | |
| 219 ++num_notifications_; | |
| 220 icon_url_ = icon_url; | |
| 221 image_ = image; | |
| 222 } | |
| 223 | |
| 224 DownloadHandler* download_handler() { return &download_handler_; } | |
| 225 const GURL& icon_url() const { return icon_url_; } | |
| 226 const gfx::Image& image() const { return image_; } | |
| 227 size_t num_notifications() const { return num_notifications_; } | |
| 228 void ResetNumNotifications() { num_notifications_ = 0; } | |
| 229 | |
| 230 private: | |
| 231 GURL icon_url_; | |
| 232 gfx::Image image_; | |
| 233 size_t num_notifications_; | |
| 234 | |
| 235 // The unique id of a download request. It will be returned to a | |
| 236 // FaviconHandler. | |
| 237 int download_id_; | |
| 238 | |
| 239 DownloadHandler download_handler_; | |
| 240 | |
| 241 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
| 242 }; | |
| 243 | |
| 244 } // namespace | |
| 245 | |
| 246 // This class is used to catch the FaviconHandler's download and history | |
| 247 // request, and also provide the methods to access the FaviconHandler | |
| 248 // internals. | |
| 249 class TestFaviconHandler : public FaviconHandler { | |
| 250 public: | |
| 251 static int GetMaximalIconSize(favicon_base::IconType icon_type) { | |
| 252 return FaviconHandler::GetMaximalIconSize(icon_type); | |
| 253 } | |
| 254 | |
| 255 TestFaviconHandler(FaviconHandler::Delegate* delegate, | |
| 256 FaviconDriverObserver::NotificationIconType handler_type) | |
| 257 : FaviconHandler(nullptr, delegate, handler_type) {} | |
| 258 | |
| 259 ~TestFaviconHandler() override {} | |
| 260 | |
| 261 HistoryRequestHandler* history_handler() { | |
| 262 return history_handler_.get(); | |
| 263 } | |
| 264 | |
| 265 // This method will take the ownership of the given handler. | |
| 266 void set_history_handler(HistoryRequestHandler* handler) { | |
| 267 history_handler_.reset(handler); | |
| 268 } | |
| 269 | |
| 270 FaviconURL* current_candidate() { | |
| 271 return FaviconHandler::current_candidate(); | |
| 272 } | |
| 273 | |
| 274 size_t current_candidate_index() const { | |
| 275 return current_candidate_index_; | |
| 276 } | |
| 277 | |
| 278 const FaviconCandidate& best_favicon_candidate() { | |
| 279 return best_favicon_candidate_; | |
| 280 } | |
| 281 | |
| 282 protected: | |
| 283 void UpdateFaviconMappingAndFetch( | |
| 284 const GURL& page_url, | |
| 285 const GURL& icon_url, | 244 const GURL& icon_url, |
| 286 favicon_base::IconType icon_type, | 245 favicon_base::IconType icon_type, |
| 246 int desired_size_in_dip, |
| 287 const favicon_base::FaviconResultsCallback& callback, | 247 const favicon_base::FaviconResultsCallback& callback, |
| 288 base::CancelableTaskTracker* tracker) override { | 248 base::CancelableTaskTracker* tracker) { |
| 289 history_handler_.reset(new HistoryRequestHandler(page_url, icon_url, | 249 return GetFaviconForPageOrIconURL(icon_url, callback, tracker); |
| 290 icon_type, callback)); | 250 } |
| 291 } | 251 |
| 292 | 252 base::CancelableTaskTracker::TaskId GetFaviconForPageURL( |
| 293 void GetFaviconFromFaviconService( | |
| 294 const GURL& icon_url, | |
| 295 favicon_base::IconType icon_type, | |
| 296 const favicon_base::FaviconResultsCallback& callback, | |
| 297 base::CancelableTaskTracker* tracker) override { | |
| 298 history_handler_.reset(new HistoryRequestHandler(GURL(), icon_url, | |
| 299 icon_type, callback)); | |
| 300 } | |
| 301 | |
| 302 void GetFaviconForURLFromFaviconService( | |
| 303 const GURL& page_url, | 253 const GURL& page_url, |
| 304 int icon_types, | 254 int icon_types, |
| 255 int desired_size_in_dip, |
| 305 const favicon_base::FaviconResultsCallback& callback, | 256 const favicon_base::FaviconResultsCallback& callback, |
| 306 base::CancelableTaskTracker* tracker) override { | 257 base::CancelableTaskTracker* tracker) { |
| 307 history_handler_.reset(new HistoryRequestHandler(page_url, GURL(), | 258 return GetFaviconForPageOrIconURL(page_url, callback, tracker); |
| 308 icon_types, callback)); | 259 } |
| 309 } | 260 |
| 310 | 261 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch( |
| 311 void SetHistoryFavicons(const GURL& page_url, | 262 const GURL& page_url, |
| 312 const GURL& icon_url, | 263 const std::vector<GURL>& icon_urls, |
| 313 favicon_base::IconType icon_type, | 264 int icon_types, |
| 314 const gfx::Image& image) override { | 265 int desired_size_in_dip, |
| 315 scoped_refptr<base::RefCountedMemory> bytes = image.As1xPNGBytes(); | 266 const favicon_base::FaviconResultsCallback& callback, |
| 316 std::vector<unsigned char> bitmap_data(bytes->front(), | 267 base::CancelableTaskTracker* tracker) { |
| 317 bytes->front() + bytes->size()); | 268 CHECK_EQ(1U, icon_urls.size()) << "Multi-icon lookup not implemented"; |
| 318 history_handler_.reset(new HistoryRequestHandler( | 269 return GetFaviconForPageOrIconURL(icon_urls.front(), callback, tracker); |
| 319 page_url, icon_url, icon_type, bitmap_data, image.Size())); | 270 } |
| 320 } | |
| 321 | |
| 322 bool ShouldSaveFavicon() override { return true; } | |
| 323 | |
| 324 GURL page_url_; | |
| 325 | 271 |
| 326 private: | 272 private: |
| 327 std::unique_ptr<HistoryRequestHandler> history_handler_; | 273 base::CancelableTaskTracker::TaskId GetFaviconForPageOrIconURL( |
| 328 | 274 const GURL& page_or_icon_url, |
| 329 DISALLOW_COPY_AND_ASSIGN(TestFaviconHandler); | 275 const favicon_base::FaviconResultsCallback& callback, |
| 276 base::CancelableTaskTracker* tracker) { |
| 277 db_requests_.push_back(page_or_icon_url); |
| 278 |
| 279 return tracker->PostTask(base::ThreadTaskRunnerHandle::Get().get(), |
| 280 FROM_HERE, |
| 281 base::Bind(callback, results_[page_or_icon_url])); |
| 282 } |
| 283 |
| 284 std::map<GURL, std::vector<favicon_base::FaviconRawBitmapResult>> results_; |
| 285 URLVector db_requests_; |
| 286 |
| 287 DISALLOW_COPY_AND_ASSIGN(FakeFaviconService); |
| 330 }; | 288 }; |
| 331 | 289 |
| 332 namespace { | 290 // MockFaviconService subclass that delegates DB reads to FakeFaviconService. |
| 333 | 291 class MockFaviconServiceWithFake : public MockFaviconService { |
| 334 void HistoryRequestHandler::InvokeCallback() { | 292 public: |
| 335 if (!callback_.is_null()) { | 293 MockFaviconServiceWithFake() { |
| 336 callback_.Run(history_results_); | 294 // Delegate the various methods that read from the DB. |
| 337 } | 295 ON_CALL(*this, GetFavicon(_, _, _, _, _)) |
| 338 } | 296 .WillByDefault(Invoke(&fake_, &FakeFaviconService::GetFavicon)); |
| 339 | 297 ON_CALL(*this, GetFaviconForPageURL(_, _, _, _, _)) |
| 340 void DownloadHandler::InvokeCallback() { | 298 .WillByDefault( |
| 341 if (callback_invoked_) | 299 Invoke(&fake_, &FakeFaviconService::GetFaviconForPageURL)); |
| 342 return; | 300 ON_CALL(*this, UpdateFaviconMappingsAndFetch(_, _, _, _, _, _)) |
| 343 | 301 .WillByDefault( |
| 344 std::vector<gfx::Size> original_bitmap_sizes; | 302 Invoke(&fake_, &FakeFaviconService::UpdateFaviconMappingsAndFetch)); |
| 345 std::vector<SkBitmap> bitmaps; | 303 } |
| 346 if (should_fail_download_icon_urls_.count(download_->image_url)) { | 304 |
| 347 failed_download_icon_urls_.insert(download_->image_url); | 305 FakeFaviconService* fake() { return &fake_; } |
| 348 } else { | 306 |
| 349 for (std::vector<int>::const_iterator i = download_->image_sizes.begin(); | 307 private: |
| 350 i != download_->image_sizes.end(); ++i) { | 308 FakeFaviconService fake_; |
| 351 int original_size = (*i > 0) ? *i : gfx::kFaviconSize; | 309 |
| 352 int downloaded_size = original_size; | 310 DISALLOW_COPY_AND_ASSIGN(MockFaviconServiceWithFake); |
| 353 if (download_->max_image_size != 0 && | 311 }; |
| 354 downloaded_size > download_->max_image_size) { | |
| 355 downloaded_size = download_->max_image_size; | |
| 356 } | |
| 357 SkBitmap bitmap; | |
| 358 FillDataToBitmap(downloaded_size, downloaded_size, &bitmap); | |
| 359 bitmaps.push_back(bitmap); | |
| 360 original_bitmap_sizes.push_back(gfx::Size(original_size, original_size)); | |
| 361 } | |
| 362 } | |
| 363 download_->callback.Run(download_->download_id, | |
| 364 /*=status_code=*/200, download_->image_url, bitmaps, | |
| 365 original_bitmap_sizes); | |
| 366 callback_invoked_ = true; | |
| 367 } | |
| 368 | 312 |
| 369 class FaviconHandlerTest : public testing::Test { | 313 class FaviconHandlerTest : public testing::Test { |
| 370 protected: | 314 protected: |
| 315 const std::vector<gfx::Size> kEmptySizes; |
| 316 |
| 317 // Some known icons for which download will succeed. |
| 318 const GURL kPageURL = GURL("http://www.google.com"); |
| 319 const GURL kIconURL10x10 = GURL("http://www.google.com/favicon10x10"); |
| 320 const GURL kIconURL12x12 = GURL("http://www.google.com/favicon12x12"); |
| 321 const GURL kIconURL16x16 = GURL("http://www.google.com/favicon16x16"); |
| 322 const GURL kIconURL64x64 = GURL("http://www.google.com/favicon64x64"); |
| 323 |
| 371 FaviconHandlerTest() { | 324 FaviconHandlerTest() { |
| 372 } | 325 // Register various known icon URLs. |
| 373 | 326 delegate_.fake_downloader().Add(kIconURL10x10, IntVector{10}); |
| 374 ~FaviconHandlerTest() override {} | 327 delegate_.fake_downloader().Add(kIconURL12x12, IntVector{12}); |
| 375 | 328 delegate_.fake_downloader().Add(kIconURL16x16, IntVector{16}); |
| 376 // Simulates requesting a favicon for |page_url| given: | 329 delegate_.fake_downloader().Add(kIconURL64x64, IntVector{64}); |
| 377 // - We have not previously cached anything in history for |page_url| or for | 330 |
| 378 // any of |candidates|. | |
| 379 // - The page provides favicons at |candidate_icons|. | |
| 380 // - The favicons at |candidate_icons| have edge pixel sizes of | |
| 381 // |candidate_icon_sizes|. | |
| 382 void DownloadTillDoneIgnoringHistory( | |
| 383 TestDelegate* delegate, | |
| 384 TestFaviconHandler* favicon_handler, | |
| 385 const GURL& page_url, | |
| 386 const std::vector<FaviconURL>& candidate_icons, | |
| 387 const int* candidate_icon_sizes) { | |
| 388 size_t old_num_notifications = delegate->num_notifications(); | |
| 389 | |
| 390 UpdateFaviconURL(delegate, favicon_handler, page_url, candidate_icons); | |
| 391 EXPECT_EQ(candidate_icons.size(), favicon_handler->image_urls().size()); | |
| 392 | |
| 393 DownloadHandler* download_handler = delegate->download_handler(); | |
| 394 for (size_t i = 0; i < candidate_icons.size(); ++i) { | |
| 395 favicon_handler->history_handler()->history_results_.clear(); | |
| 396 favicon_handler->history_handler()->InvokeCallback(); | |
| 397 ASSERT_TRUE(download_handler->HasDownload()); | |
| 398 EXPECT_EQ(download_handler->GetImageUrl(), | |
| 399 candidate_icons[i].icon_url); | |
| 400 std::vector<int> sizes; | |
| 401 sizes.push_back(candidate_icon_sizes[i]); | |
| 402 download_handler->SetImageSizes(sizes); | |
| 403 download_handler->InvokeCallback(); | |
| 404 | |
| 405 download_handler->Reset(); | |
| 406 | |
| 407 if (delegate->num_notifications() > old_num_notifications) | |
| 408 return; | |
| 409 } | |
| 410 } | |
| 411 | |
| 412 void UpdateFaviconURL(TestDelegate* delegate, | |
| 413 TestFaviconHandler* favicon_handler, | |
| 414 const GURL& page_url, | |
| 415 const std::vector<FaviconURL>& candidate_icons) { | |
| 416 delegate->ResetNumNotifications(); | |
| 417 | |
| 418 favicon_handler->FetchFavicon(page_url); | |
| 419 favicon_handler->history_handler()->InvokeCallback(); | |
| 420 | |
| 421 favicon_handler->OnUpdateFaviconURL(page_url, candidate_icons); | |
| 422 } | |
| 423 | |
| 424 void SetUp() override { | |
| 425 // The score computed by SelectFaviconFrames() is dependent on the supported | 331 // The score computed by SelectFaviconFrames() is dependent on the supported |
| 426 // scale factors of the platform. It is used for determining the goodness of | 332 // scale factors of the platform. It is used for determining the goodness of |
| 427 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon(). | 333 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon(). |
| 428 // Force the values of the scale factors so that the tests produce the same | 334 // Force the values of the scale factors so that the tests produce the same |
| 429 // results on all platforms. | 335 // results on all platforms. |
| 430 std::vector<ui::ScaleFactor> scale_factors; | |
| 431 scale_factors.push_back(ui::SCALE_FACTOR_100P); | |
| 432 scoped_set_supported_scale_factors_.reset( | 336 scoped_set_supported_scale_factors_.reset( |
| 433 new ui::test::ScopedSetSupportedScaleFactors(scale_factors)); | 337 new ui::test::ScopedSetSupportedScaleFactors({ui::SCALE_FACTOR_100P})); |
| 434 testing::Test::SetUp(); | 338 } |
| 435 } | 339 |
| 436 | 340 bool VerifyAndClearExpectations() { |
| 341 base::RunLoop().RunUntilIdle(); |
| 342 favicon_service_.fake()->ClearDbRequests(); |
| 343 delegate_.fake_downloader().ClearDownloads(); |
| 344 return testing::Mock::VerifyAndClearExpectations(&favicon_service_) && |
| 345 testing::Mock::VerifyAndClearExpectations(&delegate_); |
| 346 } |
| 347 |
| 348 // Creates a new handler and feeds in the page URL and the candidates. |
| 349 // Returns the handler in case tests want to exercise further steps. |
| 350 std::unique_ptr<FaviconHandler> RunHandlerWithCandidates( |
| 351 FaviconDriverObserver::NotificationIconType handler_type, |
| 352 const std::vector<favicon::FaviconURL>& candidates) { |
| 353 auto handler = base::MakeUnique<FaviconHandler>(&favicon_service_, |
| 354 &delegate_, handler_type); |
| 355 handler->FetchFavicon(kPageURL); |
| 356 // The first RunUntilIdle() causes the FaviconService lookups be faster than |
| 357 // OnUpdateFaviconURL(), which is the most likely scenario. |
| 358 base::RunLoop().RunUntilIdle(); |
| 359 handler->OnUpdateFaviconURL(kPageURL, candidates); |
| 360 base::RunLoop().RunUntilIdle(); |
| 361 return handler; |
| 362 } |
| 363 |
| 364 // Same as above, but for the simplest case where all types are FAVICON and |
| 365 // no sizes are provided, using a FaviconHandler of type NON_TOUCH_16_DIP. |
| 366 std::unique_ptr<FaviconHandler> RunHandlerWithSimpleFaviconCandidates( |
| 367 const std::vector<GURL>& urls) { |
| 368 std::vector<favicon::FaviconURL> candidates; |
| 369 for (const GURL& url : urls) { |
| 370 candidates.emplace_back(url, FAVICON, kEmptySizes); |
| 371 } |
| 372 return RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP, |
| 373 candidates); |
| 374 } |
| 375 |
| 376 base::MessageLoopForUI message_loop_; |
| 437 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> | 377 std::unique_ptr<ui::test::ScopedSetSupportedScaleFactors> |
| 438 scoped_set_supported_scale_factors_; | 378 scoped_set_supported_scale_factors_; |
| 379 testing::NiceMock<MockFaviconServiceWithFake> favicon_service_; |
| 380 testing::NiceMock<MockDelegate> delegate_; |
| 439 }; | 381 }; |
| 440 | 382 |
| 441 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) { | 383 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) { |
| 442 const GURL page_url("http://www.google.com"); | 384 const GURL kIconURL("http://www.google.com/favicon"); |
| 443 const GURL icon_url("http://www.google.com/favicon"); | 385 |
| 444 | 386 favicon_service_.fake()->Store(kPageURL, kIconURL, |
| 445 TestDelegate delegate; | 387 CreateRawBitmapResult(kIconURL)); |
| 446 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); | 388 |
| 447 | 389 EXPECT_CALL(delegate_, OnFaviconUpdated( |
| 448 helper.FetchFavicon(page_url); | 390 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP, |
| 449 HistoryRequestHandler* history_handler = helper.history_handler(); | 391 kIconURL, /*icon_url_changed=*/true, _)); |
| 450 // Ensure the data given to history is correct. | 392 |
| 451 ASSERT_TRUE(history_handler); | 393 RunHandlerWithSimpleFaviconCandidates({kIconURL}); |
| 452 EXPECT_EQ(page_url, history_handler->page_url_); | 394 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| 453 EXPECT_EQ(GURL(), history_handler->icon_url_); | 395 } |
| 454 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | 396 |
| 455 | 397 // Test that the FaviconHandler process finishes when: |
| 456 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_); | 398 // - There is data in the database for neither the page URL nor the icon URL. |
| 457 | 399 // AND |
| 458 // Send history response. | 400 // - FaviconService::GetFaviconForPageURL() callback returns before |
| 459 history_handler->InvokeCallback(); | 401 // FaviconHandler::OnUpdateFaviconURL() is called. |
| 460 // Verify FaviconHandler status | 402 // TODO(mastiz): Add test to verify UpdateFaviconMappingsAndFetch(). |
| 461 EXPECT_EQ(1u, delegate.num_notifications()); | 403 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesSlower) { |
| 462 EXPECT_EQ(icon_url, delegate.icon_url()); | 404 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON, |
| 463 | 405 ImageSizeIs(16, 16))); |
| 464 // Simulates update favicon url. | 406 EXPECT_CALL(delegate_, OnFaviconUpdated( |
| 465 std::vector<FaviconURL> urls; | 407 kPageURL, FaviconDriverObserver::NON_TOUCH_16_DIP, |
| 466 urls.push_back( | 408 kIconURL16x16, /*icon_url_changed=*/true, _)); |
| 467 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); | 409 |
| 468 helper.OnUpdateFaviconURL(page_url, urls); | 410 FaviconHandler handler(&favicon_service_, &delegate_, |
| 469 | 411 FaviconDriverObserver::NON_TOUCH_16_DIP); |
| 470 // Verify FaviconHandler status | 412 handler.FetchFavicon(kPageURL); |
| 471 EXPECT_EQ(1u, helper.image_urls().size()); | 413 // Causes FaviconService lookups be faster than OnUpdateFaviconURL(). |
| 472 ASSERT_TRUE(helper.current_candidate()); | 414 base::RunLoop().RunUntilIdle(); |
| 473 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); | 415 handler.OnUpdateFaviconURL(kPageURL, |
| 474 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type); | 416 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); |
| 475 | 417 base::RunLoop().RunUntilIdle(); |
| 476 // Favicon shouldn't request to download icon. | 418 |
| 477 EXPECT_FALSE(delegate.download_handler()->HasDownload()); | 419 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); |
| 478 } | 420 EXPECT_THAT(favicon_service_.fake()->db_requests(), |
| 479 | 421 ElementsAre(kPageURL, kIconURL16x16)); |
| 480 TEST_F(FaviconHandlerTest, DownloadFavicon) { | 422 } |
| 481 const GURL page_url("http://www.google.com"); | 423 |
| 482 const GURL icon_url("http://www.google.com/favicon"); | 424 // Test that the FaviconHandler process finishes when: |
| 483 | 425 // - There is no data in the database for neither the page URL nor the icon URL. |
| 484 TestDelegate delegate; | 426 // AND |
| 485 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); | 427 // - FaviconService::GetFaviconForPageURL() callback returns after |
| 486 | 428 // FaviconHandler::OnUpdateFaviconURL() is called. |
| 487 helper.FetchFavicon(page_url); | 429 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconIfCandidatesFaster) { |
| 488 HistoryRequestHandler* history_handler = helper.history_handler(); | 430 InSequence seq; |
| 489 // Ensure the data given to history is correct. | 431 EXPECT_CALL(favicon_service_, |
| 490 ASSERT_TRUE(history_handler); | 432 UpdateFaviconMappingsAndFetch(kPageURL, URLVector{kIconURL16x16}, |
| 491 EXPECT_EQ(page_url, history_handler->page_url_); | 433 FAVICON, _, _, _)); |
| 492 EXPECT_EQ(GURL(), history_handler->icon_url_); | 434 EXPECT_CALL(favicon_service_, SetFavicons(kPageURL, kIconURL16x16, FAVICON, |
| 493 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | 435 ImageSizeIs(16, 16))); |
| 494 | 436 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)); |
| 495 // Set icon data expired | 437 |
| 496 SetFaviconRawBitmapResult(icon_url, | 438 FaviconHandler handler(&favicon_service_, &delegate_, |
| 497 favicon_base::FAVICON, | 439 FaviconDriverObserver::NON_TOUCH_16_DIP); |
| 498 true /* expired */, | 440 handler.FetchFavicon(kPageURL); |
| 499 &history_handler->history_results_); | 441 ASSERT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL)); |
| 500 // Send history response. | 442 |
| 501 history_handler->InvokeCallback(); | 443 // Feed in favicons without processing posted tasks (RunUntilIdle()). |
| 502 // Verify FaviconHandler status | 444 handler.OnUpdateFaviconURL(kPageURL, |
| 503 EXPECT_EQ(1u, delegate.num_notifications()); | 445 {FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); |
| 504 EXPECT_EQ(icon_url, delegate.icon_url()); | 446 base::RunLoop().RunUntilIdle(); |
| 505 | 447 |
| 506 // Simulates update favicon url. | 448 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); |
| 507 std::vector<FaviconURL> urls; | 449 } |
| 508 urls.push_back( | 450 |
| 509 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); | 451 // Test that the FaviconHandler process does not save anything to the database |
| 510 helper.OnUpdateFaviconURL(page_url, urls); | 452 // for incognito tabs. |
| 511 | 453 TEST_F(FaviconHandlerTest, DownloadUnknownFaviconInIncognito) { |
| 512 // Verify FaviconHandler status | 454 ON_CALL(delegate_, IsOffTheRecord()).WillByDefault(Return(true)); |
| 513 EXPECT_EQ(1u, helper.image_urls().size()); | 455 |
| 514 ASSERT_TRUE(helper.current_candidate()); | 456 // No writes expected. |
| 515 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); | 457 EXPECT_CALL(favicon_service_, UpdateFaviconMappingsAndFetch(_, _, _, _, _, _)) |
| 516 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type); | 458 .Times(0); |
| 517 | 459 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); |
| 518 // Favicon should request to download icon now. | 460 |
| 519 DownloadHandler* download_handler = delegate.download_handler(); | 461 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)); |
| 520 EXPECT_TRUE(download_handler->HasDownload()); | 462 |
| 521 | 463 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}); |
| 522 // Verify the download request. | 464 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); |
| 523 EXPECT_EQ(icon_url, download_handler->GetImageUrl()); | 465 EXPECT_THAT(favicon_service_.fake()->db_requests(), |
| 524 | 466 ElementsAre(kPageURL, kIconURL16x16)); |
| 525 // Reset the history_handler to verify whether favicon is set. | 467 } |
| 526 helper.set_history_handler(nullptr); | 468 |
| 527 | 469 // Test that the icon is redownloaded if the icon cached for the page URL |
| 528 // Smulates download done. | 470 // expired. |
| 529 download_handler->InvokeCallback(); | 471 TEST_F(FaviconHandlerTest, RedownloadExpiredPageUrlFavicon) { |
| 530 | 472 favicon_service_.fake()->Store( |
| 531 // New icon should be saved to history backend and navigation entry. | 473 kPageURL, kIconURL16x16, |
| 532 history_handler = helper.history_handler(); | 474 CreateRawBitmapResult(kIconURL16x16, FAVICON, /*expired=*/true)); |
| 533 ASSERT_TRUE(history_handler); | 475 |
| 534 EXPECT_EQ(icon_url, history_handler->icon_url_); | 476 // TODO(crbug.com/700811): It would be nice if we could check whether the two |
| 535 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | 477 // OnFaviconUpdated() calls are called with different gfx::Images (as opposed |
| 536 EXPECT_LT(0U, history_handler->bitmap_data_.size()); | 478 // to calling OnFaviconUpdated() with the expired gfx::Image both times). |
| 537 EXPECT_EQ(page_url, history_handler->page_url_); | 479 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)).Times(2); |
| 538 | 480 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL16x16, _, _)); |
| 539 // Verify NavigationEntry. | 481 |
| 540 EXPECT_EQ(2u, delegate.num_notifications()); | 482 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}); |
| 541 EXPECT_EQ(icon_url, delegate.icon_url()); | 483 // We know from the |kPageUrl| database request that |kIconURL16x16| has |
| 542 EXPECT_FALSE(delegate.image().IsEmpty()); | 484 // expired. A second request for |kIconURL16x16| should not have been made |
| 543 EXPECT_EQ(gfx::kFaviconSize, delegate.image().Width()); | 485 // because it is redundant. |
| 544 } | 486 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL)); |
| 545 | 487 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); |
| 488 } |
| 489 |
| 490 // Test that FaviconHandler requests the new data when: |
| 491 // - There is valid data in the database for the page URL. |
| 492 // AND |
| 493 // - The icon URL used by the page has changed. |
| 494 // AND |
| 495 // - There is no data in database for the new icon URL. |
| 546 TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) { | 496 TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) { |
| 547 const GURL page_url("http://www.google.com"); | 497 const GURL kOldIconURL("http://www.google.com/old_favicon"); |
| 548 const GURL icon_url("http://www.google.com/favicon"); | 498 const GURL kNewIconURL = kIconURL16x16; |
| 549 const GURL new_icon_url("http://www.google.com/new_favicon"); | 499 |
| 550 | 500 favicon_service_.fake()->Store(kPageURL, kOldIconURL, |
| 551 TestDelegate delegate; | 501 CreateRawBitmapResult(kOldIconURL)); |
| 552 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); | 502 |
| 553 | 503 InSequence seq; |
| 554 helper.FetchFavicon(page_url); | 504 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kOldIconURL, _, _)); |
| 555 HistoryRequestHandler* history_handler = helper.history_handler(); | 505 EXPECT_CALL(favicon_service_, SetFavicons(_, kNewIconURL, _, _)); |
| 556 // Ensure the data given to history is correct. | 506 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kNewIconURL, _, _)); |
| 557 ASSERT_TRUE(history_handler); | 507 |
| 558 EXPECT_EQ(page_url, history_handler->page_url_); | 508 RunHandlerWithSimpleFaviconCandidates({kNewIconURL}); |
| 559 EXPECT_EQ(GURL(), history_handler->icon_url_); | 509 EXPECT_THAT(delegate_.downloads(), ElementsAre(kNewIconURL)); |
| 560 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | 510 } |
| 561 | 511 |
| 562 // Set valid icon data. | 512 // If there is data for the page URL in history which is invalid, test that: |
| 563 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_); | 513 // - The invalid data is not sent to the UI. |
| 564 | 514 // - The icon is redownloaded. |
| 565 // Send history response. | |
| 566 history_handler->InvokeCallback(); | |
| 567 // Verify FaviconHandler status. | |
| 568 EXPECT_EQ(1u, delegate.num_notifications()); | |
| 569 EXPECT_EQ(icon_url, delegate.icon_url()); | |
| 570 | |
| 571 // Reset the history_handler to verify whether new icon is requested from | |
| 572 // history. | |
| 573 helper.set_history_handler(nullptr); | |
| 574 | |
| 575 // Simulates update with the different favicon url. | |
| 576 std::vector<FaviconURL> urls; | |
| 577 urls.push_back(FaviconURL( | |
| 578 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); | |
| 579 helper.OnUpdateFaviconURL(page_url, urls); | |
| 580 | |
| 581 // Verify FaviconHandler status. | |
| 582 EXPECT_EQ(1u, helper.image_urls().size()); | |
| 583 ASSERT_TRUE(helper.current_candidate()); | |
| 584 ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url); | |
| 585 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type); | |
| 586 | |
| 587 // Favicon should be requested from history. | |
| 588 history_handler = helper.history_handler(); | |
| 589 ASSERT_TRUE(history_handler); | |
| 590 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 591 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | |
| 592 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 593 | |
| 594 // Simulate not find icon. | |
| 595 history_handler->history_results_.clear(); | |
| 596 history_handler->InvokeCallback(); | |
| 597 | |
| 598 // Favicon should request to download icon now. | |
| 599 DownloadHandler* download_handler = delegate.download_handler(); | |
| 600 EXPECT_TRUE(download_handler->HasDownload()); | |
| 601 | |
| 602 // Verify the download request. | |
| 603 EXPECT_EQ(new_icon_url, download_handler->GetImageUrl()); | |
| 604 | |
| 605 // Reset the history_handler to verify whether favicon is set. | |
| 606 helper.set_history_handler(nullptr); | |
| 607 | |
| 608 // Smulates download done. | |
| 609 download_handler->InvokeCallback(); | |
| 610 | |
| 611 // New icon should be saved to history backend and navigation entry. | |
| 612 history_handler = helper.history_handler(); | |
| 613 ASSERT_TRUE(history_handler); | |
| 614 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 615 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | |
| 616 EXPECT_LT(0U, history_handler->bitmap_data_.size()); | |
| 617 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 618 | |
| 619 // Verify NavigationEntry. | |
| 620 EXPECT_EQ(2u, delegate.num_notifications()); | |
| 621 EXPECT_EQ(new_icon_url, delegate.icon_url()); | |
| 622 EXPECT_FALSE(delegate.image().IsEmpty()); | |
| 623 EXPECT_EQ(gfx::kFaviconSize, delegate.image().Width()); | |
| 624 } | |
| 625 | |
| 626 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) { | 515 TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) { |
| 627 const GURL page_url("http://www.google.com"); | |
| 628 const GURL icon_url("http://www.google.com/favicon"); | |
| 629 | |
| 630 TestDelegate delegate; | |
| 631 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); | |
| 632 | |
| 633 helper.FetchFavicon(page_url); | |
| 634 HistoryRequestHandler* history_handler = helper.history_handler(); | |
| 635 // Ensure the data given to history is correct. | |
| 636 ASSERT_TRUE(history_handler); | |
| 637 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 638 EXPECT_EQ(GURL(), history_handler->icon_url_); | |
| 639 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | |
| 640 | |
| 641 // Set non empty but invalid data. | 516 // Set non empty but invalid data. |
| 642 favicon_base::FaviconRawBitmapResult bitmap_result; | 517 FaviconRawBitmapResult bitmap_result; |
| 643 bitmap_result.expired = false; | 518 bitmap_result.expired = false; |
| 644 // Empty bitmap data is invalid. | 519 // Empty bitmap data is invalid. |
| 645 bitmap_result.bitmap_data = new base::RefCountedBytes(); | 520 bitmap_result.bitmap_data = new base::RefCountedBytes(); |
| 646 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); | 521 bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize); |
| 647 bitmap_result.icon_type = favicon_base::FAVICON; | 522 bitmap_result.icon_type = FAVICON; |
| 648 bitmap_result.icon_url = icon_url; | 523 bitmap_result.icon_url = kIconURL16x16; |
| 649 history_handler->history_results_.clear(); | 524 |
| 650 history_handler->history_results_.push_back(bitmap_result); | 525 favicon_service_.fake()->Store(kPageURL, kIconURL16x16, {bitmap_result}); |
| 651 | 526 |
| 652 // Send history response. | 527 // TODO(crbug.com/700811): It would be nice if we could check the image |
| 653 history_handler->InvokeCallback(); | 528 // being published to rule out invalid data. |
| 654 // The NavigationEntry should not be set yet as the history data is invalid. | 529 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL16x16, _, _)); |
| 655 EXPECT_EQ(0u, delegate.num_notifications()); | 530 |
| 656 EXPECT_EQ(GURL(), delegate.icon_url()); | 531 RunHandlerWithSimpleFaviconCandidates({kIconURL16x16}); |
| 657 | 532 |
| 658 // Reset the history_handler to verify whether new icon is requested from | 533 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kPageURL)); |
| 659 // history. | 534 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL16x16)); |
| 660 helper.set_history_handler(nullptr); | 535 } |
| 661 | 536 |
| 662 // Simulates update with matching favicon URL. | 537 // Test that no downloads are done if a user visits a page which changed its |
| 663 std::vector<FaviconURL> urls; | 538 // favicon URL to a favicon URL which is already cached in the database. |
| 664 urls.push_back( | |
| 665 FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); | |
| 666 helper.OnUpdateFaviconURL(page_url, urls); | |
| 667 | |
| 668 // A download for the favicon should be requested, and we should not do | |
| 669 // another history request. | |
| 670 DownloadHandler* download_handler = delegate.download_handler(); | |
| 671 EXPECT_TRUE(download_handler->HasDownload()); | |
| 672 EXPECT_EQ(nullptr, helper.history_handler()); | |
| 673 | |
| 674 // Verify the download request. | |
| 675 EXPECT_EQ(icon_url, download_handler->GetImageUrl()); | |
| 676 | |
| 677 // Simulates download done. | |
| 678 download_handler->InvokeCallback(); | |
| 679 | |
| 680 // New icon should be saved to history backend and navigation entry. | |
| 681 history_handler = helper.history_handler(); | |
| 682 ASSERT_TRUE(history_handler); | |
| 683 EXPECT_EQ(icon_url, history_handler->icon_url_); | |
| 684 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | |
| 685 EXPECT_LT(0U, history_handler->bitmap_data_.size()); | |
| 686 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 687 | |
| 688 // Verify NavigationEntry. | |
| 689 EXPECT_EQ(1u, delegate.num_notifications()); | |
| 690 EXPECT_EQ(icon_url, delegate.icon_url()); | |
| 691 EXPECT_FALSE(delegate.image().IsEmpty()); | |
| 692 EXPECT_EQ(gfx::kFaviconSize, delegate.image().Width()); | |
| 693 } | |
| 694 | |
| 695 TEST_F(FaviconHandlerTest, UpdateFavicon) { | 539 TEST_F(FaviconHandlerTest, UpdateFavicon) { |
| 696 const GURL page_url("http://www.google.com"); | 540 const GURL kSomePreviousPageURL("https://www.google.com/previous"); |
| 697 const GURL icon_url("http://www.google.com/favicon"); | 541 const GURL kIconURL("http://www.google.com/favicon"); |
| 698 const GURL new_icon_url("http://www.google.com/new_favicon"); | 542 const GURL kNewIconURL("http://www.google.com/new_favicon"); |
| 699 | 543 |
| 700 TestDelegate delegate; | 544 favicon_service_.fake()->Store(kPageURL, kIconURL, |
| 701 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); | 545 CreateRawBitmapResult(kIconURL)); |
| 702 | 546 favicon_service_.fake()->Store(kSomePreviousPageURL, kNewIconURL, |
| 703 helper.FetchFavicon(page_url); | 547 CreateRawBitmapResult(kNewIconURL)); |
| 704 HistoryRequestHandler* history_handler = helper.history_handler(); | 548 |
| 705 // Ensure the data given to history is correct. | 549 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); |
| 706 ASSERT_TRUE(history_handler); | 550 |
| 707 EXPECT_EQ(page_url, history_handler->page_url_); | 551 InSequence seq; |
| 708 EXPECT_EQ(GURL(), history_handler->icon_url_); | 552 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL, _, _)); |
| 709 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | 553 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kNewIconURL, _, _)); |
| 710 | 554 |
| 711 SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_); | 555 RunHandlerWithSimpleFaviconCandidates({kNewIconURL}); |
| 712 | 556 EXPECT_THAT(favicon_service_.fake()->db_requests(), |
| 713 // Send history response. | 557 ElementsAre(kPageURL, kNewIconURL)); |
| 714 history_handler->InvokeCallback(); | 558 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| 715 // Verify FaviconHandler status. | |
| 716 EXPECT_EQ(1u, delegate.num_notifications()); | |
| 717 EXPECT_EQ(icon_url, delegate.icon_url()); | |
| 718 | |
| 719 // Reset the history_handler to verify whether new icon is requested from | |
| 720 // history. | |
| 721 helper.set_history_handler(nullptr); | |
| 722 | |
| 723 // Simulates update with the different favicon url. | |
| 724 std::vector<FaviconURL> urls; | |
| 725 urls.push_back(FaviconURL( | |
| 726 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); | |
| 727 helper.OnUpdateFaviconURL(page_url, urls); | |
| 728 | |
| 729 // Verify FaviconHandler status. | |
| 730 EXPECT_EQ(1u, helper.image_urls().size()); | |
| 731 ASSERT_TRUE(helper.current_candidate()); | |
| 732 ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url); | |
| 733 ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type); | |
| 734 | |
| 735 // Favicon should be requested from history. | |
| 736 history_handler = helper.history_handler(); | |
| 737 ASSERT_TRUE(history_handler); | |
| 738 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 739 EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_); | |
| 740 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 741 | |
| 742 // Simulate find icon. | |
| 743 SetFaviconRawBitmapResult(new_icon_url, &history_handler->history_results_); | |
| 744 history_handler->InvokeCallback(); | |
| 745 | |
| 746 // Shouldn't request download favicon | |
| 747 EXPECT_FALSE(delegate.download_handler()->HasDownload()); | |
| 748 | |
| 749 // Verify the favicon status. | |
| 750 EXPECT_EQ(2u, delegate.num_notifications()); | |
| 751 EXPECT_EQ(new_icon_url, delegate.icon_url()); | |
| 752 EXPECT_FALSE(delegate.image().IsEmpty()); | |
| 753 } | 559 } |
| 754 | 560 |
| 755 TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) { | 561 TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) { |
| 756 const GURL page_url("http://www.google.com"); | 562 const GURL kIconURLReturning500("http://www.google.com/500.png"); |
| 757 const GURL icon_url("http://www.google.com/favicon"); | 563 |
| 758 const GURL new_icon_url("http://www.google.com/new_favicon"); | 564 delegate_.fake_downloader().AddError(kIconURLReturning500, 500); |
| 759 | 565 |
| 760 TestDelegate delegate; | 566 favicon_service_.fake()->Store( |
| 761 TestFaviconHandler helper(&delegate, FaviconDriverObserver::TOUCH_LARGEST); | 567 kPageURL, kIconURL64x64, |
| 762 std::set<GURL> fail_downloads; | 568 CreateRawBitmapResult(kIconURL64x64, TOUCH_ICON, |
| 763 fail_downloads.insert(icon_url); | 569 /*expired=*/true)); |
| 764 delegate.download_handler()->FailDownloadForIconURLs(fail_downloads); | 570 |
| 765 | 571 EXPECT_CALL(delegate_, |
| 766 helper.FetchFavicon(page_url); | 572 OnFaviconUpdated(kPageURL, FaviconDriverObserver::TOUCH_LARGEST, |
| 767 HistoryRequestHandler* history_handler = helper.history_handler(); | 573 kIconURL64x64, /*icon_url_changed=*/true, _)); |
| 768 // Ensure the data given to history is correct. | 574 EXPECT_CALL(delegate_, |
| 769 ASSERT_TRUE(history_handler); | 575 OnFaviconUpdated(kPageURL, FaviconDriverObserver::TOUCH_LARGEST, |
| 770 EXPECT_EQ(page_url, history_handler->page_url_); | 576 kIconURL64x64, /*icon_url_changed=*/false, _)); |
| 771 EXPECT_EQ(GURL(), history_handler->icon_url_); | 577 |
| 772 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON, | 578 RunHandlerWithCandidates( |
| 773 history_handler->icon_type_); | 579 FaviconDriverObserver::TOUCH_LARGEST, |
| 774 | 580 { |
| 775 // Icon not found. | 581 FaviconURL(kIconURLReturning500, TOUCH_PRECOMPOSED_ICON, kEmptySizes), |
| 776 history_handler->history_results_.clear(); | 582 FaviconURL(kIconURL64x64, TOUCH_ICON, kEmptySizes), |
| 777 // Send history response. | 583 }); |
| 778 history_handler->InvokeCallback(); | 584 // First download fails, second succeeds. |
| 779 // Verify FaviconHandler status. | 585 EXPECT_THAT(delegate_.downloads(), |
| 780 EXPECT_EQ(0u, delegate.num_notifications()); | 586 ElementsAre(kIconURLReturning500, kIconURL64x64)); |
| 781 EXPECT_EQ(GURL(), delegate.icon_url()); | 587 } |
| 782 | 588 |
| 783 // Reset the history_handler to verify whether new icon is requested from | 589 // Test that download data for icon URLs other than the current favicon |
| 784 // history. | 590 // candidate URLs is ignored. This test tests the scenario where a download is |
| 785 helper.set_history_handler(nullptr); | 591 // in flight when FaviconHandler::OnUpdateFaviconURL() is called. |
| 786 | 592 // TODO(mastiz): Make this test deal with FaviconURLs of type |
| 787 // Simulates update with the different favicon url. | 593 // favicon_base::FAVICON and add new ones like OnlyDownloadMatchingIconType and |
| 788 std::vector<FaviconURL> urls; | 594 // CallSetFaviconsWithCorrectIconType. |
| 789 urls.push_back(FaviconURL(icon_url, | |
| 790 favicon_base::TOUCH_PRECOMPOSED_ICON, | |
| 791 std::vector<gfx::Size>())); | |
| 792 urls.push_back(FaviconURL( | |
| 793 new_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>())); | |
| 794 urls.push_back(FaviconURL( | |
| 795 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); | |
| 796 helper.OnUpdateFaviconURL(page_url, urls); | |
| 797 | |
| 798 // Verify FaviconHandler status. | |
| 799 EXPECT_EQ(2u, helper.image_urls().size()); | |
| 800 EXPECT_EQ(0u, helper.current_candidate_index()); | |
| 801 ASSERT_TRUE(helper.current_candidate()); | |
| 802 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); | |
| 803 ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, | |
| 804 helper.current_candidate()->icon_type); | |
| 805 | |
| 806 // Favicon should be requested from history. | |
| 807 history_handler = helper.history_handler(); | |
| 808 ASSERT_TRUE(history_handler); | |
| 809 EXPECT_EQ(icon_url, history_handler->icon_url_); | |
| 810 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_); | |
| 811 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 812 | |
| 813 // Simulate not find icon. | |
| 814 history_handler->history_results_.clear(); | |
| 815 history_handler->InvokeCallback(); | |
| 816 | |
| 817 // Should request download favicon. | |
| 818 DownloadHandler* download_handler = delegate.download_handler(); | |
| 819 EXPECT_TRUE(download_handler->HasDownload()); | |
| 820 | |
| 821 // Verify the download request. | |
| 822 EXPECT_EQ(icon_url, download_handler->GetImageUrl()); | |
| 823 | |
| 824 // Reset the history_handler to verify whether favicon is request from | |
| 825 // history. | |
| 826 helper.set_history_handler(nullptr); | |
| 827 download_handler->InvokeCallback(); | |
| 828 | |
| 829 // Left 1 url. | |
| 830 EXPECT_EQ(1u, helper.current_candidate_index()); | |
| 831 ASSERT_TRUE(helper.current_candidate()); | |
| 832 EXPECT_EQ(new_icon_url, helper.current_candidate()->icon_url); | |
| 833 EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type); | |
| 834 | |
| 835 // Favicon should be requested from history. | |
| 836 history_handler = helper.history_handler(); | |
| 837 ASSERT_TRUE(history_handler); | |
| 838 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 839 EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_); | |
| 840 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 841 | |
| 842 // Reset download handler | |
| 843 download_handler->Reset(); | |
| 844 | |
| 845 // Simulates getting a expired icon from history. | |
| 846 SetFaviconRawBitmapResult(new_icon_url, | |
| 847 favicon_base::TOUCH_ICON, | |
| 848 true /* expired */, | |
| 849 &history_handler->history_results_); | |
| 850 history_handler->InvokeCallback(); | |
| 851 | |
| 852 // Verify the download request. | |
| 853 EXPECT_TRUE(delegate.download_handler()->HasDownload()); | |
| 854 EXPECT_EQ(new_icon_url, download_handler->GetImageUrl()); | |
| 855 | |
| 856 helper.set_history_handler(nullptr); | |
| 857 | |
| 858 // Simulates icon being downloaded. | |
| 859 download_handler->InvokeCallback(); | |
| 860 | |
| 861 // New icon should be saved to history backend. | |
| 862 history_handler = helper.history_handler(); | |
| 863 ASSERT_TRUE(history_handler); | |
| 864 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 865 EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_); | |
| 866 EXPECT_LT(0U, history_handler->bitmap_data_.size()); | |
| 867 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 868 } | |
| 869 | |
| 870 TEST_F(FaviconHandlerTest, UpdateDuringDownloading) { | 595 TEST_F(FaviconHandlerTest, UpdateDuringDownloading) { |
| 871 const GURL page_url("http://www.google.com"); | 596 const GURL kIconURL1("http://www.google.com/favicon"); |
| 872 const GURL icon_url("http://www.google.com/favicon"); | 597 const GURL kIconURL2 = kIconURL16x16; |
| 873 const GURL new_icon_url("http://www.google.com/new_favicon"); | 598 const GURL kIconURL3 = kIconURL64x64; |
| 874 | 599 |
| 875 TestDelegate delegate; | 600 // Defer the download completion such that RunUntilIdle() doesn't complete |
| 876 TestFaviconHandler helper(&delegate, FaviconDriverObserver::TOUCH_LARGEST); | 601 // the download. |
| 877 | 602 delegate_.fake_downloader().SetRunCallbackManuallyForUrl(kIconURL1); |
| 878 helper.FetchFavicon(page_url); | 603 |
| 879 HistoryRequestHandler* history_handler = helper.history_handler(); | 604 delegate_.fake_downloader().Add(kIconURL1, IntVector{16}); |
| 880 // Ensure the data given to history is correct. | 605 delegate_.fake_downloader().Add(kIconURL3, IntVector{64}); |
| 881 ASSERT_TRUE(history_handler); | 606 |
| 882 EXPECT_EQ(page_url, history_handler->page_url_); | 607 std::unique_ptr<FaviconHandler> handler = |
| 883 EXPECT_EQ(GURL(), history_handler->icon_url_); | 608 RunHandlerWithSimpleFaviconCandidates({kIconURL1, kIconURL2}); |
| 884 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON, | 609 |
| 885 history_handler->icon_type_); | 610 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 886 | 611 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback()); |
| 887 // Icon not found. | 612 |
| 888 history_handler->history_results_.clear(); | 613 // Favicon update should invalidate the ongoing download. |
| 889 // Send history response. | 614 EXPECT_CALL(favicon_service_, SetFavicons(_, kIconURL3, _, _)); |
| 890 history_handler->InvokeCallback(); | 615 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL3, _, _)); |
| 891 // Verify FaviconHandler status. | 616 |
| 892 EXPECT_EQ(0u, delegate.num_notifications()); | 617 handler->OnUpdateFaviconURL(kPageURL, |
| 893 EXPECT_EQ(GURL(), delegate.icon_url()); | 618 {FaviconURL(kIconURL3, FAVICON, kEmptySizes)}); |
| 894 | 619 |
| 895 // Reset the history_handler to verify whether new icon is requested from | 620 // Finalizes download, which should be thrown away as the favicon URLs were |
| 896 // history. | 621 // updated. |
| 897 helper.set_history_handler(nullptr); | 622 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually()); |
| 898 | 623 base::RunLoop().RunUntilIdle(); |
| 899 // Simulates update with the different favicon url. | 624 |
| 900 std::vector<FaviconURL> urls; | 625 EXPECT_THAT(favicon_service_.fake()->db_requests(), ElementsAre(kIconURL3)); |
| 901 urls.push_back(FaviconURL(icon_url, | 626 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL3)); |
| 902 favicon_base::TOUCH_PRECOMPOSED_ICON, | |
| 903 std::vector<gfx::Size>())); | |
| 904 urls.push_back(FaviconURL( | |
| 905 new_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>())); | |
| 906 urls.push_back(FaviconURL( | |
| 907 new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>())); | |
| 908 helper.OnUpdateFaviconURL(page_url, urls); | |
| 909 | |
| 910 // Verify FaviconHandler status. | |
| 911 EXPECT_EQ(2u, helper.image_urls().size()); | |
| 912 ASSERT_EQ(0u, helper.current_candidate_index()); | |
| 913 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); | |
| 914 ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, | |
| 915 helper.current_candidate()->icon_type); | |
| 916 | |
| 917 // Favicon should be requested from history. | |
| 918 history_handler = helper.history_handler(); | |
| 919 ASSERT_TRUE(history_handler); | |
| 920 EXPECT_EQ(icon_url, history_handler->icon_url_); | |
| 921 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_); | |
| 922 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 923 | |
| 924 // Simulate not find icon. | |
| 925 history_handler->history_results_.clear(); | |
| 926 history_handler->InvokeCallback(); | |
| 927 | |
| 928 // Should request download favicon. | |
| 929 DownloadHandler* download_handler = delegate.download_handler(); | |
| 930 EXPECT_TRUE(download_handler->HasDownload()); | |
| 931 | |
| 932 // Verify the download request. | |
| 933 EXPECT_EQ(icon_url, download_handler->GetImageUrl()); | |
| 934 | |
| 935 // Reset the history_handler to verify whether favicon is request from | |
| 936 // history. | |
| 937 helper.set_history_handler(nullptr); | |
| 938 const GURL latest_icon_url("http://www.google.com/latest_favicon"); | |
| 939 std::vector<FaviconURL> latest_urls; | |
| 940 latest_urls.push_back(FaviconURL( | |
| 941 latest_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>())); | |
| 942 helper.OnUpdateFaviconURL(page_url, latest_urls); | |
| 943 | |
| 944 EXPECT_EQ(1u, helper.image_urls().size()); | |
| 945 EXPECT_EQ(0u, helper.current_candidate_index()); | |
| 946 EXPECT_EQ(latest_icon_url, helper.current_candidate()->icon_url); | |
| 947 EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type); | |
| 948 | |
| 949 // Whether new icon is requested from history | |
| 950 history_handler = helper.history_handler(); | |
| 951 ASSERT_TRUE(history_handler); | |
| 952 EXPECT_EQ(latest_icon_url, history_handler->icon_url_); | |
| 953 EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_); | |
| 954 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 955 | |
| 956 // Reset the history_handler to verify whether favicon is request from | |
| 957 // history. | |
| 958 // Save the callback for late use. | |
| 959 favicon_base::FaviconResultsCallback callback = history_handler->callback_; | |
| 960 helper.set_history_handler(nullptr); | |
| 961 | |
| 962 // Simulates download succeed. | |
| 963 download_handler->InvokeCallback(); | |
| 964 // The downloaded icon should be thrown away as there is favicon update. | |
| 965 EXPECT_FALSE(helper.history_handler()); | |
| 966 | |
| 967 download_handler->Reset(); | |
| 968 | |
| 969 // Simulates getting the icon from history. | |
| 970 std::unique_ptr<HistoryRequestHandler> handler; | |
| 971 handler.reset(new HistoryRequestHandler( | |
| 972 page_url, latest_icon_url, favicon_base::TOUCH_ICON, callback)); | |
| 973 SetFaviconRawBitmapResult(latest_icon_url, | |
| 974 favicon_base::TOUCH_ICON, | |
| 975 false /* expired */, | |
| 976 &handler->history_results_); | |
| 977 handler->InvokeCallback(); | |
| 978 | |
| 979 // No download request. | |
| 980 EXPECT_FALSE(download_handler->HasDownload()); | |
| 981 } | 627 } |
| 982 | 628 |
| 983 // Test that sending an icon URL update identical to the previous icon URL | 629 // Test that sending an icon URL update identical to the previous icon URL |
| 984 // update is a no-op. | 630 // update is a no-op. |
| 985 TEST_F(FaviconHandlerTest, UpdateSameIconURLs) { | 631 TEST_F(FaviconHandlerTest, UpdateSameIconURLsWhileProcessingShouldBeNoop) { |
| 986 const GURL page_url("http://www.google.com"); | 632 const GURL kSlowLoadingIconURL("http://www.google.com/slow_favicon"); |
| 987 const GURL icon_url1("http://www.google.com/favicon1"); | 633 |
| 988 const GURL icon_url2("http://www.google.com/favicon2"); | 634 const std::vector<FaviconURL> favicon_urls = { |
| 989 std::vector<FaviconURL> favicon_urls; | 635 FaviconURL(kIconURL64x64, FAVICON, kEmptySizes), |
| 990 favicon_urls.push_back(FaviconURL(GURL("http://www.google.com/favicon1"), | 636 FaviconURL(kSlowLoadingIconURL, FAVICON, kEmptySizes), |
| 991 favicon_base::FAVICON, | 637 }; |
| 992 std::vector<gfx::Size>())); | 638 |
| 993 favicon_urls.push_back(FaviconURL(GURL("http://www.google.com/favicon2"), | 639 // Defer the download completion such that RunUntilIdle() doesn't complete |
| 994 favicon_base::FAVICON, | 640 // the download. |
| 995 std::vector<gfx::Size>())); | 641 delegate_.fake_downloader().SetRunCallbackManuallyForUrl(kSlowLoadingIconURL); |
| 996 | 642 delegate_.fake_downloader().Add(kSlowLoadingIconURL, IntVector{16}); |
| 997 TestDelegate delegate; | 643 |
| 998 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); | 644 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates( |
| 999 | 645 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls); |
| 1000 // Initiate a request for favicon data for |page_url|. History does not know | 646 |
| 1001 // about the page URL or the icon URLs. | 647 ASSERT_THAT(favicon_service_.fake()->db_requests(), |
| 1002 helper.FetchFavicon(page_url); | 648 ElementsAre(kPageURL, kIconURL64x64, kSlowLoadingIconURL)); |
| 1003 helper.history_handler()->InvokeCallback(); | 649 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 1004 helper.set_history_handler(nullptr); | 650 ASSERT_TRUE(delegate_.fake_downloader().HasPendingManualCallback()); |
| 1005 | 651 |
| 1006 // Got icon URLs. | 652 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect, |
| 1007 helper.OnUpdateFaviconURL(page_url, favicon_urls); | 653 // despite the ongoing download. |
| 1008 | 654 handler->OnUpdateFaviconURL(kPageURL, favicon_urls); |
| 1009 // There should be an ongoing history request for |icon_url1|. | 655 base::RunLoop().RunUntilIdle(); |
| 1010 ASSERT_EQ(2u, helper.image_urls().size()); | 656 |
| 1011 ASSERT_EQ(0u, helper.current_candidate_index()); | 657 // Complete the download. |
| 1012 HistoryRequestHandler* history_handler = helper.history_handler(); | 658 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)); |
| 1013 ASSERT_TRUE(history_handler); | 659 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)); |
| 1014 | 660 EXPECT_TRUE(delegate_.fake_downloader().RunCallbackManually()); |
| 1015 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect. | 661 base::RunLoop().RunUntilIdle(); |
| 1016 helper.OnUpdateFaviconURL(page_url, favicon_urls); | 662 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| 1017 EXPECT_EQ(history_handler, helper.history_handler()); | 663 } |
| 1018 | 664 |
| 1019 // Complete history request for |icon_url1| and do download. | 665 // Test that calling OnUpdateFaviconUrl() with the same icon URLs as before is a |
| 1020 helper.history_handler()->InvokeCallback(); | 666 // no-op. This is important because OnUpdateFaviconUrl() is called when the page |
| 1021 helper.set_history_handler(nullptr); | 667 // finishes loading. This can occur several times for pages with iframes. |
| 1022 delegate.download_handler()->SetImageSizes(std::vector<int>(1u, 10)); | 668 TEST_F(FaviconHandlerTest, UpdateSameIconURLsAfterFinishedShouldBeNoop) { |
| 1023 delegate.download_handler()->InvokeCallback(); | 669 const std::vector<FaviconURL> favicon_urls = { |
| 1024 delegate.download_handler()->Reset(); | 670 FaviconURL(kIconURL10x10, FAVICON, kEmptySizes), |
| 1025 | 671 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes), |
| 1026 // There should now be an ongoing history request for |icon_url2|. | 672 }; |
| 1027 ASSERT_EQ(1u, helper.current_candidate_index()); | 673 |
| 1028 history_handler = helper.history_handler(); | 674 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates( |
| 1029 ASSERT_TRUE(history_handler); | 675 FaviconDriverObserver::NON_TOUCH_16_DIP, favicon_urls); |
| 1030 | 676 |
| 1031 // Calling OnUpdateFaviconURL() with the same icon URLs should have no effect. | 677 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 1032 helper.OnUpdateFaviconURL(page_url, favicon_urls); | 678 |
| 1033 EXPECT_EQ(history_handler, helper.history_handler()); | 679 // Calling OnUpdateFaviconURL() with identical data should be a no-op. |
| 680 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); |
| 681 EXPECT_CALL(favicon_service_, SetFavicons(_, _, _, _)).Times(0); |
| 682 |
| 683 handler->OnUpdateFaviconURL(kPageURL, favicon_urls); |
| 684 base::RunLoop().RunUntilIdle(); |
| 685 EXPECT_THAT(favicon_service_.fake()->db_requests(), IsEmpty()); |
| 686 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| 1034 } | 687 } |
| 1035 | 688 |
| 1036 // Fixes crbug.com/544560 | 689 // Fixes crbug.com/544560 |
| 690 // Tests that Delegate::OnFaviconUpdated() is called if: |
| 691 // - The best icon on the initial page is not the last icon. |
| 692 // - All of the initial page's icons are downloaded. |
| 693 // AND |
| 694 // - JavaScript modifies the page's <link rel="icon"> tags to contain only the |
| 695 // last icon. |
| 1037 TEST_F(FaviconHandlerTest, | 696 TEST_F(FaviconHandlerTest, |
| 1038 OnFaviconAvailableNotificationSentAfterIconURLChange) { | 697 OnFaviconAvailableNotificationSentAfterIconURLChange) { |
| 1039 const GURL kPageURL("http://www.page_which_animates_favicon.com"); | 698 const GURL kIconURL1( |
| 1040 const GURL kIconURL1("http://wwww.page_which_animates_favicon.com/frame1.png")
; | 699 "http://wwww.page_which_animates_favicon.com/frame1.png"); |
| 1041 const GURL kIconURL2("http://wwww.page_which_animates_favicon.com/frame2.png")
; | 700 const GURL kIconURL2( |
| 1042 | 701 "http://wwww.page_which_animates_favicon.com/frame2.png"); |
| 1043 TestDelegate delegate; | 702 |
| 1044 TestFaviconHandler helper(&delegate, FaviconDriverObserver::NON_TOUCH_16_DIP); | 703 // |kIconURL1| is the better match. |
| 1045 | 704 delegate_.fake_downloader().Add(kIconURL1, IntVector{15}); |
| 1046 // Initial state: | 705 delegate_.fake_downloader().Add(kIconURL2, IntVector{10}); |
| 1047 // - The database does not know about |kPageURL|. | 706 |
| 1048 // - The page uses |kIconURL1| and |kIconURL2|. | 707 // Two FaviconDriver::OnFaviconUpdated() notifications should be sent for |
| 1049 // - The database knows about both |kIconURL1| and |kIconURl2|. Both icons | 708 // |kIconURL1|, one before and one after the download. |
| 1050 // are expired in the database. | 709 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL1, _, _)); |
| 1051 helper.FetchFavicon(kPageURL); | 710 |
| 1052 ASSERT_TRUE(helper.history_handler()); | 711 std::unique_ptr<FaviconHandler> handler = |
| 1053 helper.history_handler()->InvokeCallback(); | 712 RunHandlerWithSimpleFaviconCandidates({kIconURL1, kIconURL2}); |
| 1054 { | 713 |
| 1055 std::vector<FaviconURL> icon_urls; | 714 // Both |kIconURL1| and |kIconURL2| should have been requested from the |
| 1056 icon_urls.push_back( | 715 // database and downloaded. |kIconURL2| should have been fetched from the |
| 1057 FaviconURL(kIconURL1, favicon_base::FAVICON, std::vector<gfx::Size>())); | 716 // database and downloaded last. |
| 1058 icon_urls.push_back( | 717 ASSERT_THAT(delegate_.downloads(), ElementsAre(kIconURL1, kIconURL2)); |
| 1059 FaviconURL(kIconURL2, favicon_base::FAVICON, std::vector<gfx::Size>())); | 718 ASSERT_THAT(favicon_service_.fake()->db_requests(), |
| 1060 helper.OnUpdateFaviconURL(kPageURL, icon_urls); | 719 ElementsAre(kPageURL, kIconURL1, kIconURL2)); |
| 1061 } | 720 ASSERT_TRUE(VerifyAndClearExpectations()); |
| 1062 | |
| 1063 // FaviconHandler should request from history and download |kIconURL1| and | |
| 1064 // |kIconURL2|. |kIconURL1| is the better match. A | |
| 1065 // FaviconDriver::OnFaviconUpdated() notification should be sent for | |
| 1066 // |kIconURL1|. | |
| 1067 ASSERT_EQ(0u, delegate.num_notifications()); | |
| 1068 ASSERT_TRUE(helper.history_handler()); | |
| 1069 SetFaviconRawBitmapResult(kIconURL1, | |
| 1070 favicon_base::FAVICON, | |
| 1071 true /* expired */, | |
| 1072 &helper.history_handler()->history_results_); | |
| 1073 helper.history_handler()->InvokeCallback(); | |
| 1074 helper.set_history_handler(nullptr); | |
| 1075 ASSERT_TRUE(delegate.download_handler()->HasDownload()); | |
| 1076 delegate.download_handler()->SetImageSizes(std::vector<int>(1u, 15)); | |
| 1077 delegate.download_handler()->InvokeCallback(); | |
| 1078 delegate.download_handler()->Reset(); | |
| 1079 | |
| 1080 ASSERT_TRUE(helper.history_handler()); | |
| 1081 helper.history_handler()->InvokeCallback(); | |
| 1082 SetFaviconRawBitmapResult(kIconURL2, | |
| 1083 favicon_base::FAVICON, | |
| 1084 true /* expired */, | |
| 1085 &helper.history_handler()->history_results_); | |
| 1086 helper.history_handler()->InvokeCallback(); | |
| 1087 helper.set_history_handler(nullptr); | |
| 1088 ASSERT_TRUE(delegate.download_handler()->HasDownload()); | |
| 1089 delegate.download_handler()->SetImageSizes(std::vector<int>(1u, 10)); | |
| 1090 delegate.download_handler()->InvokeCallback(); | |
| 1091 delegate.download_handler()->Reset(); | |
| 1092 | |
| 1093 ASSERT_LT(0u, delegate.num_notifications()); | |
| 1094 ASSERT_EQ(kIconURL1, delegate.icon_url()); | |
| 1095 | |
| 1096 // Clear the history handler because SetHistoryFavicons() sets it. | |
| 1097 helper.set_history_handler(nullptr); | |
| 1098 | 721 |
| 1099 // Simulate the page changing it's icon URL to just |kIconURL2| via | 722 // Simulate the page changing it's icon URL to just |kIconURL2| via |
| 1100 // Javascript. | 723 // Javascript. |
| 1101 helper.OnUpdateFaviconURL( | 724 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL2, _, _)); |
| 1102 kPageURL, | 725 handler->OnUpdateFaviconURL(kPageURL, |
| 1103 std::vector<FaviconURL>(1u, FaviconURL(kIconURL2, favicon_base::FAVICON, | 726 {FaviconURL(kIconURL2, FAVICON, kEmptySizes)}); |
| 1104 std::vector<gfx::Size>()))); | 727 base::RunLoop().RunUntilIdle(); |
| 1105 | |
| 1106 // FaviconHandler should request from history and download |kIconURL2|. A | |
| 1107 // FaviconDriver::OnFaviconUpdated() notification should be sent for | |
| 1108 // |kIconURL2|. | |
| 1109 delegate.ResetNumNotifications(); | |
| 1110 ASSERT_TRUE(helper.history_handler()); | |
| 1111 SetFaviconRawBitmapResult(kIconURL2, | |
| 1112 favicon_base::FAVICON, | |
| 1113 true /* expired */, | |
| 1114 &helper.history_handler()->history_results_); | |
| 1115 helper.history_handler()->InvokeCallback(); | |
| 1116 helper.set_history_handler(nullptr); | |
| 1117 ASSERT_TRUE(delegate.download_handler()->HasDownload()); | |
| 1118 delegate.download_handler()->InvokeCallback(); | |
| 1119 delegate.download_handler()->Reset(); | |
| 1120 ASSERT_LT(0u, delegate.num_notifications()); | |
| 1121 EXPECT_EQ(kIconURL2, delegate.icon_url()); | |
| 1122 } | 728 } |
| 1123 | 729 |
| 1124 // Test the favicon which is selected when the web page provides several | 730 // Test the favicon which is selected when the web page provides several |
| 1125 // favicons and none of the favicons are cached in history. | 731 // favicons and none of the favicons are cached in history. |
| 1126 // The goal of this test is to be more of an integration test than | 732 // The goal of this test is to be more of an integration test than |
| 1127 // SelectFaviconFramesTest.*. | 733 // SelectFaviconFramesTest.*. |
| 1128 TEST_F(FaviconHandlerTest, MultipleFavicons) { | 734 class FaviconHandlerMultipleFaviconsTest : public FaviconHandlerTest { |
| 1129 const GURL kPageURL("http://www.google.com"); | 735 protected: |
| 1130 const FaviconURL kSourceIconURLs[] = { | 736 FaviconHandlerMultipleFaviconsTest() { |
| 1131 FaviconURL(GURL("http://www.google.com/a"), | 737 // Set the supported scale factors to 1x and 2x. This affects the behavior |
| 1132 favicon_base::FAVICON, | 738 // of SelectFaviconFrames(). |
| 1133 std::vector<gfx::Size>()), | 739 scoped_set_supported_scale_factors_.reset(); // Need to delete first. |
| 1134 FaviconURL(GURL("http://www.google.com/b"), | 740 scoped_set_supported_scale_factors_.reset( |
| 1135 favicon_base::FAVICON, | 741 new ui::test::ScopedSetSupportedScaleFactors( |
| 1136 std::vector<gfx::Size>()), | 742 {ui::SCALE_FACTOR_100P, ui::SCALE_FACTOR_200P})); |
| 1137 FaviconURL(GURL("http://www.google.com/c"), | 743 } |
| 1138 favicon_base::FAVICON, | 744 |
| 1139 std::vector<gfx::Size>()), | 745 // Simulates requesting a favicon for |page_url| given: |
| 1140 FaviconURL(GURL("http://www.google.com/d"), | 746 // - We have not previously cached anything in history for |page_url| or for |
| 1141 favicon_base::FAVICON, | 747 // any of candidates. |
| 1142 std::vector<gfx::Size>()), | 748 // - The page provides favicons with edge pixel sizes of |
| 1143 FaviconURL(GURL("http://www.google.com/e"), | 749 // |candidate_icon_sizes|. |
| 1144 favicon_base::FAVICON, | 750 // - Candidates are assumed of type FAVICON and the URLs are generated |
| 1145 std::vector<gfx::Size>())}; | 751 // internally for testing purposes. |
| 1146 | 752 // |
| 1147 // Set the supported scale factors to 1x and 2x. This affects the behavior of | 753 // Returns the chosen size among |candidate_icon_sizes| or -1 if none was |
| 1148 // SelectFaviconFrames(). | 754 // chosen. |
| 1149 std::vector<ui::ScaleFactor> scale_factors; | 755 int DownloadTillDoneIgnoringHistory(const IntVector& candidate_icon_sizes) { |
| 1150 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 756 std::vector<FaviconURL> candidate_icons; |
| 1151 scale_factors.push_back(ui::SCALE_FACTOR_200P); | 757 int chosen_icon_size = -1; |
| 1152 ui::test::ScopedSetSupportedScaleFactors scoped_supported(scale_factors); | 758 |
| 1153 | 759 for (int icon_size : candidate_icon_sizes) { |
| 1154 // 1) Test that if there are several single resolution favicons to choose from | 760 const GURL icon_url(base::StringPrintf( |
| 1155 // that the largest exact match is chosen. | 761 "https://www.google.com/generated/%dx%d", icon_size, icon_size)); |
| 1156 TestDelegate delegate1; | 762 // Set up 200 responses for all images, and the corresponding size. |
| 1157 TestFaviconHandler handler1(&delegate1, | 763 delegate_.fake_downloader().Add(icon_url, IntVector{icon_size}); |
| 1158 FaviconDriverObserver::NON_TOUCH_16_DIP); | 764 // Create test candidates of type FAVICON and a fake URL. |
| 1159 | 765 candidate_icons.emplace_back(icon_url, FAVICON, kEmptySizes); |
| 1160 const int kSizes1[] = { 16, 24, 32, 48, 256 }; | 766 |
| 1161 std::vector<FaviconURL> urls1(kSourceIconURLs, | 767 ON_CALL(delegate_, OnFaviconUpdated(_, _, icon_url, _, _)) |
| 1162 kSourceIconURLs + arraysize(kSizes1)); | 768 .WillByDefault(Assign(&chosen_icon_size, icon_size)); |
| 1163 DownloadTillDoneIgnoringHistory(&delegate1, &handler1, kPageURL, urls1, | 769 } |
| 1164 kSizes1); | 770 |
| 1165 | 771 RunHandlerWithCandidates(FaviconDriverObserver::NON_TOUCH_16_DIP, |
| 1166 EXPECT_EQ(nullptr, handler1.current_candidate()); | 772 candidate_icons); |
| 1167 EXPECT_EQ(1u, delegate1.num_notifications()); | 773 return chosen_icon_size; |
| 1168 EXPECT_FALSE(delegate1.image().IsEmpty()); | 774 } |
| 1169 EXPECT_EQ(gfx::kFaviconSize, delegate1.image().Width()); | 775 }; |
| 1170 | 776 |
| 1171 size_t expected_index = 2u; | 777 // Tests that running FaviconHandler |
| 1172 EXPECT_EQ(32, kSizes1[expected_index]); | 778 // - On an OS which supports the 1x and 2x scale factor |
| 1173 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate1.icon_url()); | 779 // - On a page with <link rel="icon"> tags with no "sizes" information. |
| 1174 | 780 // Selects the largest exact match. Note that a 32x32 PNG image is not a "true |
| 1175 // 2) Test that if there are several single resolution favicons to choose | 781 // exact match" on an OS which supports an 1x and 2x. A "true exact match" is |
| 1176 // from, the exact match is preferred even if it results in upsampling. | 782 // a .ico file with 16x16 and 32x32 bitmaps. |
| 1177 TestDelegate delegate2; | 783 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseLargestExactMatch) { |
| 1178 TestFaviconHandler handler2(&delegate2, | 784 EXPECT_EQ(32, |
| 1179 FaviconDriverObserver::NON_TOUCH_16_DIP); | 785 DownloadTillDoneIgnoringHistory(IntVector{16, 24, 32, 48, 256})); |
| 1180 | 786 } |
| 1181 const int kSizes2[] = { 16, 24, 48, 256 }; | 787 |
| 1182 std::vector<FaviconURL> urls2(kSourceIconURLs, | 788 // Test that if there are several single resolution favicons to choose |
| 1183 kSourceIconURLs + arraysize(kSizes2)); | 789 // from, the exact match is preferred even if it results in upsampling. |
| 1184 DownloadTillDoneIgnoringHistory(&delegate2, &handler2, kPageURL, urls2, | 790 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseExactMatchDespiteUpsampling) { |
| 1185 kSizes2); | 791 EXPECT_EQ(16, DownloadTillDoneIgnoringHistory(IntVector{16, 24, 48, 256})); |
| 1186 EXPECT_EQ(1u, delegate2.num_notifications()); | 792 } |
| 1187 expected_index = 0u; | 793 |
| 1188 EXPECT_EQ(16, kSizes2[expected_index]); | 794 // Test that favicons which need to be upsampled a little or downsampled |
| 1189 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate2.icon_url()); | 795 // a little are preferred over huge favicons. |
| 1190 | 796 TEST_F(FaviconHandlerMultipleFaviconsTest, |
| 1191 // 3) Test that favicons which need to be upsampled a little or downsampled | 797 ChooseMinorDownsamplingOverHugeIcon) { |
| 1192 // a little are preferred over huge favicons. | 798 EXPECT_EQ(48, DownloadTillDoneIgnoringHistory(IntVector{256, 48})); |
| 1193 TestDelegate delegate3; | 799 } |
| 1194 TestFaviconHandler handler3(&delegate3, | 800 |
| 1195 FaviconDriverObserver::NON_TOUCH_16_DIP); | 801 TEST_F(FaviconHandlerMultipleFaviconsTest, ChooseMinorUpsamplingOverHugeIcon) { |
| 1196 | 802 EXPECT_EQ(17, DownloadTillDoneIgnoringHistory(IntVector{17, 256})); |
| 1197 const int kSizes3[] = { 256, 48 }; | 803 } |
| 1198 std::vector<FaviconURL> urls3(kSourceIconURLs, | 804 |
| 1199 kSourceIconURLs + arraysize(kSizes3)); | 805 TEST_F(FaviconHandlerTest, Report404) { |
| 1200 DownloadTillDoneIgnoringHistory(&delegate3, &handler3, kPageURL, urls3, | 806 const GURL k404IconURL("http://www.google.com/404.png"); |
| 1201 kSizes3); | 807 |
| 1202 EXPECT_EQ(1u, delegate3.num_notifications()); | 808 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(k404IconURL)); |
| 1203 expected_index = 1u; | 809 |
| 1204 EXPECT_EQ(48, kSizes3[expected_index]); | 810 RunHandlerWithSimpleFaviconCandidates({k404IconURL}); |
| 1205 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate3.icon_url()); | 811 EXPECT_THAT(delegate_.downloads(), ElementsAre(k404IconURL)); |
| 1206 | 812 } |
| 1207 TestDelegate delegate4; | 813 |
| 1208 TestFaviconHandler handler4(&delegate4, | 814 // Test that WasUnableToDownloadFavicon() is not called if a download returns |
| 1209 FaviconDriverObserver::NON_TOUCH_16_DIP); | 815 // HTTP status 503. |
| 1210 | 816 TEST_F(FaviconHandlerTest, NotReport503) { |
| 1211 const int kSizes4[] = { 17, 256 }; | 817 const GURL k503IconURL("http://www.google.com/503.png"); |
| 1212 std::vector<FaviconURL> urls4(kSourceIconURLs, | 818 |
| 1213 kSourceIconURLs + arraysize(kSizes4)); | 819 delegate_.fake_downloader().AddError(k503IconURL, 503); |
| 1214 DownloadTillDoneIgnoringHistory(&delegate4, &handler4, kPageURL, urls4, | 820 |
| 1215 kSizes4); | 821 EXPECT_CALL(favicon_service_, UnableToDownloadFavicon(_)).Times(0); |
| 1216 EXPECT_EQ(1u, delegate4.num_notifications()); | 822 |
| 1217 expected_index = 0u; | 823 RunHandlerWithSimpleFaviconCandidates({k503IconURL}); |
| 1218 EXPECT_EQ(17, kSizes4[expected_index]); | 824 EXPECT_THAT(delegate_.downloads(), ElementsAre(k503IconURL)); |
| 1219 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, delegate4.icon_url()); | |
| 1220 } | 825 } |
| 1221 | 826 |
| 1222 // Test that the best favicon is selected when: | 827 // Test that the best favicon is selected when: |
| 1223 // - The page provides several favicons. | 828 // - The page provides several favicons. |
| 1224 // - Downloading one of the page's icon URLs previously returned a 404. | 829 // - Downloading one of the page's icon URLs previously returned a 404. |
| 1225 // - None of the favicons are cached in the Favicons database. | 830 // - None of the favicons are cached in the Favicons database. |
| 1226 TEST_F(FaviconHandlerTest, MultipleFavicons404) { | 831 TEST_F(FaviconHandlerTest, MultipleFavicons404) { |
| 1227 const GURL kPageURL("http://www.google.com"); | |
| 1228 const GURL k404IconURL("http://www.google.com/404.png"); | 832 const GURL k404IconURL("http://www.google.com/404.png"); |
| 1229 const FaviconURL k404FaviconURL( | |
| 1230 k404IconURL, favicon_base::FAVICON, std::vector<gfx::Size>()); | |
| 1231 const FaviconURL kFaviconURLs[] = { | |
| 1232 FaviconURL(GURL("http://www.google.com/a"), | |
| 1233 favicon_base::FAVICON, | |
| 1234 std::vector<gfx::Size>()), | |
| 1235 k404FaviconURL, | |
| 1236 FaviconURL(GURL("http://www.google.com/c"), | |
| 1237 favicon_base::FAVICON, | |
| 1238 std::vector<gfx::Size>()), | |
| 1239 }; | |
| 1240 | 833 |
| 1241 TestDelegate delegate; | 834 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(k404IconURL)) |
| 1242 TestFaviconHandler handler(&delegate, | 835 .WillByDefault(Return(true)); |
| 1243 FaviconDriverObserver::NON_TOUCH_16_DIP); | |
| 1244 DownloadHandler* download_handler = delegate.download_handler(); | |
| 1245 | 836 |
| 1246 std::set<GURL> k404URLs; | 837 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL64x64, _, _)); |
| 1247 k404URLs.insert(k404IconURL); | 838 RunHandlerWithSimpleFaviconCandidates({k404IconURL, kIconURL64x64}); |
| 1248 download_handler->FailDownloadForIconURLs(k404URLs); | 839 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL64x64)); |
| 840 } |
| 1249 | 841 |
| 1250 // Make the initial download for |k404IconURL| fail. | 842 // Test that the best favicon is selected when: |
| 1251 const int kSizes1[] = { 0 }; | 843 // - The page provides several favicons. |
| 1252 std::vector<FaviconURL> urls1(1u, k404FaviconURL); | 844 // - Downloading the last page icon URL previously returned a 404. |
| 1253 DownloadTillDoneIgnoringHistory(&delegate, &handler, kPageURL, urls1, | 845 // - None of the favicons are cached in the Favicons database. |
| 1254 kSizes1); | 846 // - All of the icons are downloaded because none of the icons have the ideal |
| 1255 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL)); | 847 // size. |
| 848 // - The 404 icon is last. |
| 849 TEST_F(FaviconHandlerTest, MultipleFaviconsLast404) { |
| 850 const GURL k404IconURL("http://www.google.com/404.png"); |
| 1256 | 851 |
| 1257 // Do a fetch now that the initial download for |k404IconURL| has failed. The | 852 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(k404IconURL)) |
| 1258 // behavior is different because OnDidDownloadFavicon() is invoked | 853 .WillByDefault(Return(true)); |
| 1259 // synchronously from DownloadFavicon(). | |
| 1260 const int kSizes2[] = { 10, 0, 16 }; | |
| 1261 std::vector<FaviconURL> urls2(kFaviconURLs, | |
| 1262 kFaviconURLs + arraysize(kFaviconURLs)); | |
| 1263 DownloadTillDoneIgnoringHistory(&delegate, &handler, kPageURL, urls2, | |
| 1264 kSizes2); | |
| 1265 | 854 |
| 1266 EXPECT_EQ(nullptr, handler.current_candidate()); | 855 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, kIconURL64x64, _, _)); |
| 1267 EXPECT_EQ(1u, delegate.num_notifications()); | 856 RunHandlerWithSimpleFaviconCandidates({kIconURL64x64, k404IconURL}); |
| 1268 EXPECT_FALSE(delegate.image().IsEmpty()); | 857 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL64x64)); |
| 1269 int expected_index = 2u; | |
| 1270 EXPECT_EQ(16, kSizes2[expected_index]); | |
| 1271 EXPECT_EQ(kFaviconURLs[expected_index].icon_url, delegate.icon_url()); | |
| 1272 } | 858 } |
| 1273 | 859 |
| 1274 // Test that no favicon is selected when: | 860 // Test that no favicon is selected when: |
| 1275 // - The page provides several favicons. | 861 // - The page provides several favicons. |
| 1276 // - Downloading the page's icons has previously returned a 404. | 862 // - Downloading the page's icons has previously returned a 404. |
| 1277 // - None of the favicons are cached in the Favicons database. | 863 // - None of the favicons are cached in the Favicons database. |
| 1278 TEST_F(FaviconHandlerTest, MultipleFaviconsAll404) { | 864 TEST_F(FaviconHandlerTest, MultipleFaviconsAll404) { |
| 1279 const GURL kPageURL("http://www.google.com"); | 865 const GURL k404IconURL1("http://www.google.com/a/404.png"); |
| 1280 const GURL k404IconURL1("http://www.google.com/4041.png"); | 866 const GURL k404IconURL2("http://www.google.com/b/404.png"); |
| 1281 const GURL k404IconURL2("http://www.google.com/4042.png"); | |
| 1282 const FaviconURL kFaviconURLs[] = { | |
| 1283 FaviconURL(k404IconURL1, | |
| 1284 favicon_base::FAVICON, | |
| 1285 std::vector<gfx::Size>()), | |
| 1286 FaviconURL(k404IconURL2, | |
| 1287 favicon_base::FAVICON, | |
| 1288 std::vector<gfx::Size>()), | |
| 1289 }; | |
| 1290 | 867 |
| 1291 TestDelegate delegate; | 868 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(k404IconURL1)) |
| 1292 TestFaviconHandler handler(&delegate, | 869 .WillByDefault(Return(true)); |
| 1293 FaviconDriverObserver::NON_TOUCH_16_DIP); | 870 ON_CALL(favicon_service_, WasUnableToDownloadFavicon(k404IconURL2)) |
| 1294 DownloadHandler* download_handler = delegate.download_handler(); | 871 .WillByDefault(Return(true)); |
| 1295 | 872 |
| 1296 std::set<GURL> k404URLs; | 873 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); |
| 1297 k404URLs.insert(k404IconURL1); | 874 RunHandlerWithSimpleFaviconCandidates({k404IconURL1, k404IconURL2}); |
| 1298 k404URLs.insert(k404IconURL2); | 875 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| 1299 download_handler->FailDownloadForIconURLs(k404URLs); | |
| 1300 | |
| 1301 // Make the initial downloads for |kFaviconURLs| fail. | |
| 1302 for (const FaviconURL& favicon_url : kFaviconURLs) { | |
| 1303 const int kSizes[] = { 0 }; | |
| 1304 std::vector<FaviconURL> urls(1u, favicon_url); | |
| 1305 DownloadTillDoneIgnoringHistory(&delegate, &handler, kPageURL, urls, | |
| 1306 kSizes); | |
| 1307 } | |
| 1308 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL1)); | |
| 1309 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL2)); | |
| 1310 | |
| 1311 // Do a fetch now that the initial downloads for |kFaviconURLs| have failed. | |
| 1312 // The behavior is different because OnDidDownloadFavicon() is invoked | |
| 1313 // synchronously from DownloadFavicon(). | |
| 1314 const int kSizes[] = { 0, 0 }; | |
| 1315 std::vector<FaviconURL> urls(kFaviconURLs, | |
| 1316 kFaviconURLs + arraysize(kFaviconURLs)); | |
| 1317 DownloadTillDoneIgnoringHistory(&delegate, &handler, kPageURL, urls, kSizes); | |
| 1318 | |
| 1319 EXPECT_EQ(nullptr, handler.current_candidate()); | |
| 1320 EXPECT_EQ(0u, delegate.num_notifications()); | |
| 1321 EXPECT_TRUE(delegate.image().IsEmpty()); | |
| 1322 } | 876 } |
| 1323 | 877 |
| 1324 // Test that no favicon is selected when the page's only icon uses an invalid | 878 // Test that no favicon is selected when the page's only icon uses an invalid |
| 1325 // URL syntax. | 879 // URL syntax. |
| 1326 TEST_F(FaviconHandlerTest, FaviconInvalidURL) { | 880 TEST_F(FaviconHandlerTest, FaviconInvalidURL) { |
| 1327 const GURL kPageURL("http://www.google.com"); | |
| 1328 const GURL kInvalidFormatURL("invalid"); | 881 const GURL kInvalidFormatURL("invalid"); |
| 1329 ASSERT_TRUE(kInvalidFormatURL.is_empty()); | 882 ASSERT_TRUE(kInvalidFormatURL.is_empty()); |
| 1330 | 883 |
| 1331 FaviconURL favicon_url(kInvalidFormatURL, favicon_base::FAVICON, | 884 EXPECT_CALL(delegate_, OnFaviconUpdated(_, _, _, _, _)).Times(0); |
| 1332 std::vector<gfx::Size>()); | |
| 1333 | 885 |
| 1334 TestDelegate delegate; | 886 RunHandlerWithSimpleFaviconCandidates({kInvalidFormatURL}); |
| 1335 TestFaviconHandler handler(&delegate, | 887 EXPECT_THAT(delegate_.downloads(), IsEmpty()); |
| 1336 FaviconDriverObserver::NON_TOUCH_16_DIP); | |
| 1337 UpdateFaviconURL(&delegate, &handler, kPageURL, | |
| 1338 std::vector<FaviconURL>(1u, favicon_url)); | |
| 1339 EXPECT_EQ(0u, handler.image_urls().size()); | |
| 1340 } | 888 } |
| 1341 | 889 |
| 1342 TEST_F(FaviconHandlerTest, TestSortFavicon) { | 890 TEST_F(FaviconHandlerTest, TestSortFavicon) { |
| 1343 const GURL kPageURL("http://www.google.com"); | 891 const std::vector<favicon::FaviconURL> kSourceIconURLs{ |
| 1344 std::vector<gfx::Size> icon1; | 892 FaviconURL(GURL("http://www.google.com/a"), FAVICON, |
| 1345 icon1.push_back(gfx::Size(1024, 1024)); | 893 {gfx::Size(1, 1), gfx::Size(17, 17)}), |
| 1346 icon1.push_back(gfx::Size(512, 512)); | 894 FaviconURL(GURL("http://www.google.com/b"), FAVICON, |
| 895 {gfx::Size(1024, 1024), gfx::Size(512, 512)}), |
| 896 FaviconURL(GURL("http://www.google.com/c"), FAVICON, |
| 897 {gfx::Size(16, 16), gfx::Size(14, 14)}), |
| 898 FaviconURL(GURL("http://www.google.com/d"), FAVICON, kEmptySizes), |
| 899 FaviconURL(GURL("http://www.google.com/e"), FAVICON, kEmptySizes)}; |
| 1347 | 900 |
| 1348 std::vector<gfx::Size> icon2; | 901 std::unique_ptr<FaviconHandler> handler = RunHandlerWithCandidates( |
| 1349 icon2.push_back(gfx::Size(15, 15)); | 902 FaviconDriverObserver::NON_TOUCH_LARGEST, kSourceIconURLs); |
| 1350 icon2.push_back(gfx::Size(16, 16)); | |
| 1351 | |
| 1352 std::vector<gfx::Size> icon3; | |
| 1353 icon3.push_back(gfx::Size(16, 16)); | |
| 1354 icon3.push_back(gfx::Size(14, 14)); | |
| 1355 | |
| 1356 const FaviconURL kSourceIconURLs[] = { | |
| 1357 FaviconURL(GURL("http://www.google.com/a"), favicon_base::FAVICON, icon1), | |
| 1358 FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON, icon2), | |
| 1359 FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON, icon3), | |
| 1360 FaviconURL(GURL("http://www.google.com/d"), | |
| 1361 favicon_base::FAVICON, | |
| 1362 std::vector<gfx::Size>()), | |
| 1363 FaviconURL(GURL("http://www.google.com/e"), | |
| 1364 favicon_base::FAVICON, | |
| 1365 std::vector<gfx::Size>())}; | |
| 1366 | |
| 1367 TestDelegate delegate1; | |
| 1368 TestFaviconHandler handler1(&delegate1, | |
| 1369 FaviconDriverObserver::NON_TOUCH_LARGEST); | |
| 1370 std::vector<FaviconURL> urls1(kSourceIconURLs, | |
| 1371 kSourceIconURLs + arraysize(kSourceIconURLs)); | |
| 1372 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1); | |
| 1373 | 903 |
| 1374 struct ExpectedResult { | 904 struct ExpectedResult { |
| 1375 // The favicon's index in kSourceIconURLs. | 905 // The favicon's index in kSourceIconURLs. |
| 1376 size_t favicon_index; | 906 size_t favicon_index; |
| 1377 // Width of largest bitmap. | 907 // Width of largest bitmap. |
| 1378 int width; | 908 int width; |
| 1379 } results[] = { | 909 } results[] = { |
| 1380 // First is icon1, though its size larger than maximal. | 910 // First is icon2, though its size larger than maximal. |
| 1381 {0, 1024}, | 911 {1, 1024}, |
| 1382 // Second is icon2 | 912 // Second is icon1 |
| 1383 // The 16x16 is largest. | 913 // The 17x17 is largest. |
| 1384 {1, 16}, | 914 {0, 17}, |
| 1385 // Third is icon3 though it has same size as icon2. | 915 // Third is icon3. |
| 1386 // The 16x16 is largest. | 916 // The 16x16 is largest. |
| 1387 {2, 16}, | 917 {2, 16}, |
| 1388 // The rest of bitmaps come in order, there is no sizes attribute. | 918 // The rest of bitmaps come in order, there is no "sizes" attribute. |
| 1389 {3, -1}, | 919 {3, -1}, |
| 1390 {4, -1}, | 920 {4, -1}, |
| 1391 }; | 921 }; |
| 1392 const std::vector<FaviconURL>& icons = handler1.image_urls(); | 922 const std::vector<FaviconURL>& icons = handler->image_urls(); |
| 1393 ASSERT_EQ(5u, icons.size()); | 923 ASSERT_EQ(5u, icons.size()); |
| 1394 for (size_t i = 0; i < icons.size(); ++i) { | 924 for (size_t i = 0; i < icons.size(); ++i) { |
| 1395 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url, | 925 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url, |
| 1396 icons[i].icon_url); | 926 icons[i].icon_url); |
| 1397 if (results[i].width != -1) | 927 if (results[i].width != -1) |
| 1398 EXPECT_EQ(results[i].width, icons[i].icon_sizes[0].width()); | 928 EXPECT_EQ(results[i].width, icons[i].icon_sizes[0].width()); |
| 1399 } | 929 } |
| 1400 } | 930 } |
| 1401 | 931 |
| 1402 TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) { | 932 TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) { |
| 1403 const GURL kPageURL("http://www.google.com"); | 933 // Names represent the bitmap sizes per icon. |
| 1404 std::vector<gfx::Size> icon1; | 934 const GURL kIconURL1024_512("http://www.google.com/a"); |
| 1405 icon1.push_back(gfx::Size(1024, 1024)); | 935 const GURL kIconURL15_14("http://www.google.com/b"); |
| 1406 icon1.push_back(gfx::Size(512, 512)); | 936 const GURL kIconURL16_512("http://www.google.com/c"); |
| 937 const GURL kIconURLWithoutSize1("http://www.google.com/d"); |
| 938 const GURL kIconURLWithoutSize2("http://www.google.com/e"); |
| 1407 | 939 |
| 1408 std::vector<gfx::Size> icon2; | 940 RunHandlerWithCandidates( |
| 1409 icon2.push_back(gfx::Size(15, 15)); | 941 FaviconDriverObserver::NON_TOUCH_LARGEST, |
| 1410 icon2.push_back(gfx::Size(14, 14)); | 942 {FaviconURL(kIconURL1024_512, FAVICON, |
| 943 {gfx::Size(1024, 1024), gfx::Size(512, 512)}), |
| 944 FaviconURL(kIconURL15_14, FAVICON, |
| 945 {gfx::Size(15, 15), gfx::Size(14, 14)}), |
| 946 FaviconURL(kIconURL16_512, FAVICON, |
| 947 {gfx::Size(16, 16), gfx::Size(512, 512)}), |
| 948 FaviconURL(kIconURLWithoutSize1, FAVICON, kEmptySizes), |
| 949 FaviconURL(kIconURLWithoutSize2, FAVICON, kEmptySizes)}); |
| 1411 | 950 |
| 1412 std::vector<gfx::Size> icon3; | 951 // Icon URLs are not registered and hence 404s will be produced, which |
| 1413 icon3.push_back(gfx::Size(16, 16)); | 952 // allows checking whether the icons were requested according to their size. |
| 1414 icon3.push_back(gfx::Size(512, 512)); | 953 // The favicons should have been requested in decreasing order of their sizes. |
| 1415 | 954 // Favicons without any <link sizes=""> attribute should have been downloaded |
| 1416 const FaviconURL kSourceIconURLs[] = { | 955 // last. |
| 1417 FaviconURL( | 956 EXPECT_THAT(delegate_.downloads(), |
| 1418 GURL("http://www.google.com/a"), favicon_base::FAVICON, icon1), | 957 ElementsAre(kIconURL1024_512, kIconURL16_512, kIconURL15_14, |
| 1419 FaviconURL( | 958 kIconURLWithoutSize1, kIconURLWithoutSize2)); |
| 1420 GURL("http://www.google.com/b"), favicon_base::FAVICON, icon2), | |
| 1421 FaviconURL( | |
| 1422 GURL("http://www.google.com/c"), favicon_base::FAVICON, icon3), | |
| 1423 FaviconURL(GURL("http://www.google.com/d"), | |
| 1424 favicon_base::FAVICON, | |
| 1425 std::vector<gfx::Size>()), | |
| 1426 FaviconURL(GURL("http://www.google.com/e"), | |
| 1427 favicon_base::FAVICON, | |
| 1428 std::vector<gfx::Size>())}; | |
| 1429 | |
| 1430 TestDelegate delegate1; | |
| 1431 TestFaviconHandler handler1(&delegate1, | |
| 1432 FaviconDriverObserver::NON_TOUCH_LARGEST); | |
| 1433 | |
| 1434 std::set<GURL> fail_icon_urls; | |
| 1435 for (size_t i = 0; i < arraysize(kSourceIconURLs); ++i) { | |
| 1436 fail_icon_urls.insert(kSourceIconURLs[i].icon_url); | |
| 1437 } | |
| 1438 delegate1.download_handler()->FailDownloadForIconURLs(fail_icon_urls); | |
| 1439 | |
| 1440 std::vector<FaviconURL> urls1(kSourceIconURLs, | |
| 1441 kSourceIconURLs + arraysize(kSourceIconURLs)); | |
| 1442 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1); | |
| 1443 | |
| 1444 // Simulate the download failed, to check whether the icons were requested | |
| 1445 // to download according their size. | |
| 1446 struct ExpectedResult { | |
| 1447 // The favicon's index in kSourceIconURLs. | |
| 1448 size_t favicon_index; | |
| 1449 // Width of largest bitmap. | |
| 1450 int width; | |
| 1451 } results[] = { | |
| 1452 {0, 1024}, | |
| 1453 {2, 512}, | |
| 1454 {1, 15}, | |
| 1455 // The rest of bitmaps come in order. | |
| 1456 {3, -1}, | |
| 1457 {4, -1}, | |
| 1458 }; | |
| 1459 | |
| 1460 for (int i = 0; i < 5; ++i) { | |
| 1461 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url, | |
| 1462 handler1.current_candidate()->icon_url); | |
| 1463 if (results[i].width != -1) { | |
| 1464 EXPECT_EQ(results[i].width, handler1.current_candidate()-> | |
| 1465 icon_sizes[0].width()); | |
| 1466 } | |
| 1467 | |
| 1468 // Simulate no favicon from history. | |
| 1469 handler1.history_handler()->history_results_.clear(); | |
| 1470 handler1.history_handler()->InvokeCallback(); | |
| 1471 | |
| 1472 // Verify download request | |
| 1473 ASSERT_TRUE(delegate1.download_handler()->HasDownload()); | |
| 1474 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url, | |
| 1475 delegate1.download_handler()->GetImageUrl()); | |
| 1476 | |
| 1477 delegate1.download_handler()->InvokeCallback(); | |
| 1478 delegate1.download_handler()->Reset(); | |
| 1479 } | |
| 1480 } | 959 } |
| 1481 | 960 |
| 1482 TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) { | 961 TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) { |
| 1483 const GURL kPageURL("http://www.google.com"); | 962 const GURL kIconURL1("http://www.google.com/b"); |
| 963 const GURL kIconURL2("http://www.google.com/c"); |
| 1484 | 964 |
| 1485 std::vector<gfx::Size> one_icon; | 965 delegate_.fake_downloader().Add(kIconURL1, IntVector{15}); |
| 1486 one_icon.push_back(gfx::Size(15, 15)); | 966 delegate_.fake_downloader().Add(kIconURL2, IntVector{14, 16}); |
| 1487 | 967 |
| 1488 std::vector<gfx::Size> two_icons; | 968 // Verify NotifyFaviconAvailable(). |
| 1489 two_icons.push_back(gfx::Size(14, 14)); | 969 EXPECT_CALL(delegate_, |
| 1490 two_icons.push_back(gfx::Size(16, 16)); | 970 OnFaviconUpdated(_, FaviconDriverObserver::NON_TOUCH_LARGEST, |
| 971 kIconURL2, _, _)); |
| 1491 | 972 |
| 1492 const FaviconURL kSourceIconURLs[] = { | 973 RunHandlerWithCandidates( |
| 1493 FaviconURL( | 974 FaviconDriverObserver::NON_TOUCH_LARGEST, |
| 1494 GURL("http://www.google.com/b"), favicon_base::FAVICON, one_icon), | 975 {FaviconURL(kIconURL1, FAVICON, {gfx::Size(15, 15)}), |
| 1495 FaviconURL( | 976 FaviconURL(kIconURL2, FAVICON, {gfx::Size(14, 14), gfx::Size(16, 16)})}); |
| 1496 GURL("http://www.google.com/c"), favicon_base::FAVICON, two_icons)}; | |
| 1497 | 977 |
| 1498 TestDelegate delegate1; | 978 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2)); |
| 1499 TestFaviconHandler handler1(&delegate1, | |
| 1500 FaviconDriverObserver::NON_TOUCH_LARGEST); | |
| 1501 std::vector<FaviconURL> urls1(kSourceIconURLs, | |
| 1502 kSourceIconURLs + arraysize(kSourceIconURLs)); | |
| 1503 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1); | |
| 1504 | |
| 1505 ASSERT_EQ(2u, handler1.image_urls().size()); | |
| 1506 | |
| 1507 // Index of largest favicon in kSourceIconURLs. | |
| 1508 size_t i = 1; | |
| 1509 // The largest bitmap's index in Favicon . | |
| 1510 int b = 1; | |
| 1511 | |
| 1512 // Verify the icon_bitmaps_ was initialized correctly. | |
| 1513 EXPECT_EQ(kSourceIconURLs[i].icon_url, | |
| 1514 handler1.current_candidate()->icon_url); | |
| 1515 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b], | |
| 1516 handler1.current_candidate()->icon_sizes[0]); | |
| 1517 | |
| 1518 // Simulate no favicon from history. | |
| 1519 handler1.history_handler()->history_results_.clear(); | |
| 1520 handler1.history_handler()->InvokeCallback(); | |
| 1521 | |
| 1522 // Verify download request | |
| 1523 ASSERT_TRUE(delegate1.download_handler()->HasDownload()); | |
| 1524 EXPECT_EQ(kSourceIconURLs[i].icon_url, | |
| 1525 delegate1.download_handler()->GetImageUrl()); | |
| 1526 | |
| 1527 // Give the correct download result. | |
| 1528 std::vector<int> sizes; | |
| 1529 for (std::vector<gfx::Size>::const_iterator j = | |
| 1530 kSourceIconURLs[i].icon_sizes.begin(); | |
| 1531 j != kSourceIconURLs[i].icon_sizes.end(); ++j) | |
| 1532 sizes.push_back(j->width()); | |
| 1533 | |
| 1534 delegate1.download_handler()->SetImageSizes(sizes); | |
| 1535 delegate1.download_handler()->InvokeCallback(); | |
| 1536 | |
| 1537 // Verify the largest bitmap has been saved into history. | |
| 1538 EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_); | |
| 1539 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b], | |
| 1540 handler1.history_handler()->size_); | |
| 1541 // Verify NotifyFaviconAvailable(). | |
| 1542 EXPECT_EQ(1u, delegate1.num_notifications()); | |
| 1543 EXPECT_EQ(kSourceIconURLs[i].icon_url, delegate1.icon_url()); | |
| 1544 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b], delegate1.image().Size()); | |
| 1545 } | 979 } |
| 1546 | 980 |
| 1547 TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) { | 981 TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) { |
| 1548 const GURL kPageURL("http://www.google.com"); | 982 const int kMaximalSize = FaviconHandler::GetMaximalIconSize(FAVICON); |
| 1549 const int kMaximalSize = | |
| 1550 TestFaviconHandler::GetMaximalIconSize(favicon_base::FAVICON); | |
| 1551 | 983 |
| 1552 std::vector<gfx::Size> icon1; | 984 const GURL kIconURL1("http://www.google.com/b"); |
| 1553 icon1.push_back(gfx::Size(kMaximalSize + 1, kMaximalSize + 1)); | 985 const GURL kIconURL2("http://www.google.com/c"); |
| 1554 | 986 |
| 1555 std::vector<gfx::Size> icon2; | 987 const int kOriginalSize1 = kMaximalSize + 1; |
| 1556 icon2.push_back(gfx::Size(kMaximalSize + 2, kMaximalSize + 2)); | 988 const int kOriginalSize2 = kMaximalSize + 2; |
| 1557 | 989 |
| 1558 const FaviconURL kSourceIconURLs[] = { | 990 delegate_.fake_downloader().AddWithOriginalSizes( |
| 1559 FaviconURL( | 991 kIconURL1, IntVector{kMaximalSize}, IntVector{kOriginalSize1}); |
| 1560 GURL("http://www.google.com/b"), favicon_base::FAVICON, icon1), | 992 delegate_.fake_downloader().AddWithOriginalSizes( |
| 1561 FaviconURL( | 993 kIconURL2, IntVector{kMaximalSize}, IntVector{kOriginalSize2}); |
| 1562 GURL("http://www.google.com/c"), favicon_base::FAVICON, icon2)}; | |
| 1563 | 994 |
| 1564 TestDelegate delegate1; | 995 // Verify the largest bitmap was selected although it was scaled down to |
| 1565 TestFaviconHandler handler1(&delegate1, | 996 // maximal size and smaller than |kIconURL1| now. |
| 1566 FaviconDriverObserver::NON_TOUCH_LARGEST); | 997 EXPECT_CALL(delegate_, |
| 1567 std::vector<FaviconURL> urls1(kSourceIconURLs, | 998 OnFaviconUpdated(_, _, kIconURL2, _, |
| 1568 kSourceIconURLs + arraysize(kSourceIconURLs)); | 999 ImageSizeIs(kMaximalSize, kMaximalSize))); |
| 1569 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1); | |
| 1570 | 1000 |
| 1571 ASSERT_EQ(2u, handler1.image_urls().size()); | 1001 RunHandlerWithCandidates( |
| 1002 FaviconDriverObserver::NON_TOUCH_LARGEST, |
| 1003 {FaviconURL(kIconURL1, FAVICON, |
| 1004 SizeVector{gfx::Size(kOriginalSize1, kOriginalSize1)}), |
| 1005 FaviconURL(kIconURL2, FAVICON, |
| 1006 SizeVector{gfx::Size(kOriginalSize2, kOriginalSize2)})}); |
| 1572 | 1007 |
| 1573 // Index of largest favicon in kSourceIconURLs. | 1008 EXPECT_THAT(delegate_.downloads(), ElementsAre(kIconURL2)); |
| 1574 size_t i = 1; | |
| 1575 // The largest bitmap's index in Favicon . | |
| 1576 int b = 0; | |
| 1577 | |
| 1578 // Verify the icon_bitmaps_ was initialized correctly. | |
| 1579 EXPECT_EQ(kSourceIconURLs[i].icon_url, | |
| 1580 handler1.current_candidate()->icon_url); | |
| 1581 EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b], | |
| 1582 handler1.current_candidate()->icon_sizes[0]); | |
| 1583 | |
| 1584 // Simulate no favicon from history. | |
| 1585 handler1.history_handler()->history_results_.clear(); | |
| 1586 handler1.history_handler()->InvokeCallback(); | |
| 1587 | |
| 1588 // Verify download request | |
| 1589 ASSERT_TRUE(delegate1.download_handler()->HasDownload()); | |
| 1590 EXPECT_EQ(kSourceIconURLs[i].icon_url, | |
| 1591 delegate1.download_handler()->GetImageUrl()); | |
| 1592 | |
| 1593 // Give the scaled download bitmap. | |
| 1594 std::vector<int> sizes; | |
| 1595 sizes.push_back(kMaximalSize); | |
| 1596 | |
| 1597 delegate1.download_handler()->SetImageSizes(sizes); | |
| 1598 delegate1.download_handler()->InvokeCallback(); | |
| 1599 | |
| 1600 // Verify the largest bitmap has been saved into history though it was | |
| 1601 // scaled down to maximal size and smaller than icon1 now. | |
| 1602 EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_); | |
| 1603 EXPECT_EQ(gfx::Size(kMaximalSize, kMaximalSize), | |
| 1604 handler1.history_handler()->size_); | |
| 1605 } | 1009 } |
| 1606 | 1010 |
| 1011 // Test that if several icons are downloaded because the icons are smaller than |
| 1012 // expected that OnFaviconUpdated() is called with the largest downloaded |
| 1013 // bitmap. |
| 1607 TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) { | 1014 TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) { |
| 1608 const GURL kPageURL("http://www.google.com"); | 1015 EXPECT_CALL(delegate_, |
| 1016 OnFaviconUpdated(_, _, kIconURL12x12, _, ImageSizeIs(12, 12))); |
| 1609 | 1017 |
| 1610 std::vector<gfx::Size> icon1; | 1018 RunHandlerWithCandidates( |
| 1611 icon1.push_back(gfx::Size(16, 16)); | 1019 FaviconDriverObserver::NON_TOUCH_LARGEST, |
| 1612 const int actual_size1 = 10; | 1020 {FaviconURL(kIconURL10x10, FAVICON, SizeVector{gfx::Size(16, 16)}), |
| 1613 | 1021 FaviconURL(kIconURL12x12, FAVICON, SizeVector{gfx::Size(15, 15)}), |
| 1614 std::vector<gfx::Size> icon2; | 1022 FaviconURL(kIconURL16x16, FAVICON, kEmptySizes)}); |
| 1615 icon2.push_back(gfx::Size(15, 15)); | |
| 1616 const int actual_size2 = 12; | |
| 1617 | |
| 1618 const FaviconURL kSourceIconURLs[] = { | |
| 1619 FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON, icon1), | |
| 1620 FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON, icon2), | |
| 1621 FaviconURL(GURL("http://www.google.com/d"), | |
| 1622 favicon_base::FAVICON, | |
| 1623 std::vector<gfx::Size>())}; | |
| 1624 | |
| 1625 TestDelegate delegate1; | |
| 1626 TestFaviconHandler handler1(&delegate1, | |
| 1627 FaviconDriverObserver::NON_TOUCH_LARGEST); | |
| 1628 std::vector<FaviconURL> urls1(kSourceIconURLs, | |
| 1629 kSourceIconURLs + arraysize(kSourceIconURLs)); | |
| 1630 UpdateFaviconURL(&delegate1, &handler1, kPageURL, urls1); | |
| 1631 ASSERT_EQ(3u, handler1.image_urls().size()); | |
| 1632 | |
| 1633 // Simulate no favicon from history. | |
| 1634 handler1.history_handler()->history_results_.clear(); | |
| 1635 handler1.history_handler()->InvokeCallback(); | |
| 1636 | |
| 1637 // Verify the first icon was request to download | |
| 1638 ASSERT_TRUE(delegate1.download_handler()->HasDownload()); | |
| 1639 EXPECT_EQ(kSourceIconURLs[0].icon_url, | |
| 1640 delegate1.download_handler()->GetImageUrl()); | |
| 1641 | |
| 1642 // Give the incorrect size. | |
| 1643 std::vector<int> sizes; | |
| 1644 sizes.push_back(actual_size1); | |
| 1645 delegate1.download_handler()->SetImageSizes(sizes); | |
| 1646 delegate1.download_handler()->InvokeCallback(); | |
| 1647 delegate1.download_handler()->Reset(); | |
| 1648 | |
| 1649 // Simulate no favicon from history. | |
| 1650 handler1.history_handler()->history_results_.clear(); | |
| 1651 handler1.history_handler()->InvokeCallback(); | |
| 1652 | |
| 1653 // Verify the 2nd icon was request to download | |
| 1654 ASSERT_TRUE(delegate1.download_handler()->HasDownload()); | |
| 1655 EXPECT_EQ(kSourceIconURLs[1].icon_url, | |
| 1656 delegate1.download_handler()->GetImageUrl()); | |
| 1657 | |
| 1658 // Very the best candidate is icon1 | |
| 1659 EXPECT_EQ(kSourceIconURLs[0].icon_url, | |
| 1660 handler1.best_favicon_candidate().image_url); | |
| 1661 EXPECT_EQ(gfx::Size(actual_size1, actual_size1), | |
| 1662 handler1.best_favicon_candidate().image.Size()); | |
| 1663 | |
| 1664 // Give the incorrect size. | |
| 1665 sizes.clear(); | |
| 1666 sizes.push_back(actual_size2); | |
| 1667 delegate1.download_handler()->SetImageSizes(sizes); | |
| 1668 delegate1.download_handler()->InvokeCallback(); | |
| 1669 delegate1.download_handler()->Reset(); | |
| 1670 | |
| 1671 // Verify icon2 has been saved into history. | |
| 1672 EXPECT_EQ(kSourceIconURLs[1].icon_url, handler1.history_handler()->icon_url_); | |
| 1673 EXPECT_EQ(gfx::Size(actual_size2, actual_size2), | |
| 1674 handler1.history_handler()->size_); | |
| 1675 } | 1023 } |
| 1676 | 1024 |
| 1677 } // namespace | 1025 } // namespace |
| 1678 } // namespace favicon | 1026 } // namespace favicon |
| OLD | NEW |