Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/favicon_helper.h" | |
|
sky
2011/03/28 15:02:58
The style of including the header first, then a ne
michaelbai
2011/03/29 00:00:50
Done.
| |
| 6 | |
| 7 #include "content/browser/renderer_host/test_render_view_host.h" | |
| 8 #include "content/browser/tab_contents/navigation_entry.h" | |
| 9 #include "content/browser/tab_contents/test_tab_contents.h" | |
| 10 #include "ui/gfx/codec/png_codec.h" | |
| 11 #include "ui/gfx/favicon_size.h" | |
| 12 | |
| 13 class TestFaviconHelper; | |
| 14 | |
| 15 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { | |
|
sky
2011/03/28 15:02:58
Static or put in a namespace and add a description
michaelbai
2011/03/29 00:00:50
Done.
| |
| 16 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | |
| 17 bmp->allocPixels(); | |
| 18 | |
| 19 unsigned char* src_data = | |
| 20 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0)); | |
| 21 for (int i = 0; i < w * h; i++) { | |
| 22 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); | |
| 23 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); | |
| 24 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255); | |
| 25 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255); | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 void FillBitmap(int w, int h, std::vector<unsigned char>* output) { | |
|
sky
2011/03/28 15:02:58
Same here.
michaelbai
2011/03/29 00:00:50
Done.
| |
| 30 SkBitmap bitmap; | |
| 31 FillDataToBitmap(w, h, &bitmap); | |
| 32 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output); | |
| 33 } | |
| 34 | |
| 35 class DownloadHandler { | |
|
sky
2011/03/28 15:02:58
Move into anonymous namespace, and add description
michaelbai
2011/03/29 00:00:50
Done.
| |
| 36 public: | |
| 37 DownloadHandler(int download_id, | |
| 38 const GURL& image_url, | |
| 39 int image_size, | |
| 40 TestFaviconHelper* favicon_helper) | |
| 41 : image_url_(image_url), | |
| 42 image_size_(image_size), | |
| 43 failed_(false), | |
| 44 download_id_(download_id), | |
| 45 favicon_helper_(favicon_helper) { | |
| 46 FillDataToBitmap(16, 16, &bitmap_); | |
| 47 } | |
| 48 | |
| 49 virtual ~DownloadHandler() { | |
| 50 } | |
| 51 | |
| 52 static void UpdateFaviconURL(FaviconHelper* helper, | |
| 53 const std::vector<FaviconURL> urls); | |
| 54 | |
| 55 void InvokeCallback(); | |
| 56 | |
| 57 void UpdateFaviconURL(const std::vector<FaviconURL> urls); | |
| 58 | |
| 59 const GURL image_url_; | |
| 60 const int image_size_; | |
| 61 bool failed_; | |
| 62 | |
| 63 private: | |
| 64 int download_id_; | |
|
sky
2011/03/28 15:02:58
Descriptions?
michaelbai
2011/03/29 00:00:50
Done.
| |
| 65 TestFaviconHelper* favicon_helper_; | |
| 66 SkBitmap bitmap_; | |
| 67 }; | |
|
sky
2011/03/28 15:02:58
DISALLOW_COPY_AND_ASSIGN
michaelbai
2011/03/29 00:00:50
Done.
| |
| 68 | |
| 69 class HistoryRequestHandler { | |
|
sky
2011/03/28 15:02:58
Move into anonymous namespace, and add description
michaelbai
2011/03/29 00:00:50
Done.
| |
| 70 public: | |
| 71 HistoryRequestHandler(const GURL& page_url, | |
| 72 const GURL& icon_url, | |
| 73 int icon_type, | |
| 74 FaviconService::FaviconDataCallback* callback) | |
| 75 : page_url_(page_url), | |
| 76 icon_url_(icon_url), | |
| 77 icon_type_(icon_type), | |
| 78 callback_(callback) { | |
| 79 } | |
| 80 | |
| 81 HistoryRequestHandler(const GURL& page_url, | |
| 82 const GURL& icon_url, | |
| 83 int icon_type, | |
| 84 const std::vector<unsigned char>& image_data, | |
| 85 FaviconService::FaviconDataCallback* callback) | |
| 86 : page_url_(page_url), | |
| 87 icon_url_(icon_url), | |
| 88 icon_type_(icon_type), | |
| 89 image_data_(image_data), | |
| 90 callback_(callback) { | |
| 91 } | |
| 92 | |
| 93 void InvokeCallback(); | |
| 94 | |
| 95 const GURL page_url_; | |
| 96 const GURL icon_url_; | |
| 97 const int icon_type_; | |
| 98 const std::vector<unsigned char> image_data_; | |
| 99 history::FaviconData favicon_data_; | |
| 100 | |
| 101 private: | |
| 102 FaviconService::FaviconDataCallback* callback_; | |
| 103 }; | |
|
sky
2011/03/28 15:02:58
DISALLOW_COPY_AND_ASSIGN
michaelbai
2011/03/29 00:00:50
Done.
| |
| 104 | |
| 105 class TestFaviconHelper : public FaviconHelper { | |
| 106 public: | |
| 107 TestFaviconHelper(const GURL& page_url, | |
| 108 TabContents* tab_contents, | |
| 109 Types type) | |
| 110 : FaviconHelper(tab_contents, type), | |
| 111 download_id_(0), | |
| 112 tab_contents_(tab_contents){ | |
| 113 entry_.set_url(page_url); | |
|
sky
2011/03/28 15:02:58
Initialize download_image_size_.
michaelbai
2011/03/29 00:00:50
Done.
| |
| 114 } | |
| 115 | |
| 116 virtual ~TestFaviconHelper() { | |
| 117 } | |
| 118 | |
| 119 HistoryRequestHandler* history_handler() { | |
| 120 return history_handler_.get(); | |
| 121 } | |
| 122 | |
| 123 void set_history_handler(HistoryRequestHandler* handler) { | |
|
sky
2011/03/28 15:02:58
Method ownership of these setters.
michaelbai
2011/03/29 00:00:50
Done.
| |
| 124 history_handler_.reset(handler); | |
| 125 } | |
| 126 | |
| 127 DownloadHandler* download_handler() { | |
| 128 return download_handler_.get(); | |
| 129 } | |
| 130 | |
| 131 void set_download_handler(DownloadHandler* download_handler) { | |
| 132 download_handler_.reset(download_handler); | |
| 133 } | |
| 134 | |
| 135 virtual NavigationEntry* GetEntry() { | |
| 136 return &entry_; | |
| 137 } | |
| 138 | |
| 139 protected: | |
| 140 virtual void UpdateFaviconMappingAndFetch( | |
|
sky
2011/03/28 15:02:58
Add OVERRIDE on all these methods.
michaelbai
2011/03/29 00:00:50
Done.
| |
| 141 const GURL& page_url, | |
| 142 const GURL& icon_url, | |
| 143 history::IconType icon_type, | |
| 144 CancelableRequestConsumerBase* consumer, | |
| 145 FaviconService::FaviconDataCallback* callback) { | |
| 146 history_handler_.reset(new HistoryRequestHandler(page_url, icon_url, | |
| 147 icon_type, callback)); | |
| 148 } | |
| 149 | |
| 150 virtual void GetFavicon( | |
| 151 const GURL& icon_url, | |
| 152 history::IconType icon_type, | |
| 153 CancelableRequestConsumerBase* consumer, | |
| 154 FaviconService::FaviconDataCallback* callback) { | |
| 155 history_handler_.reset(new HistoryRequestHandler(GURL(), icon_url, | |
| 156 icon_type, callback)); | |
| 157 } | |
| 158 | |
| 159 virtual void GetFaviconForURL( | |
| 160 const GURL& page_url, | |
| 161 int icon_types, | |
| 162 CancelableRequestConsumerBase* consumer, | |
| 163 FaviconService::FaviconDataCallback* callback) { | |
| 164 history_handler_.reset(new HistoryRequestHandler(page_url, GURL(), | |
| 165 icon_types, callback)); | |
| 166 } | |
| 167 | |
| 168 virtual int DownloadFavicon(const GURL& image_url, int image_size) { | |
| 169 download_id_++; | |
| 170 download_handler_.reset(new DownloadHandler(download_id_, image_url, | |
| 171 image_size, this)); | |
| 172 return download_id_; | |
| 173 } | |
| 174 | |
| 175 virtual void SetHistoryFavicon(const GURL& page_url, | |
| 176 const GURL& icon_url, | |
| 177 const std::vector<unsigned char>& image_data, | |
| 178 history::IconType icon_type) { | |
| 179 history_handler_.reset(new HistoryRequestHandler(page_url, icon_url, | |
| 180 icon_type, image_data, NULL)); | |
| 181 } | |
| 182 | |
| 183 virtual FaviconService* GetFaviconService() { | |
| 184 // Just give none NULL value, so overridden methods can be hit. | |
| 185 return (FaviconService*)(1); | |
| 186 } | |
| 187 | |
| 188 virtual bool ShouldSaveFavicon(const GURL& url) { | |
| 189 return true; | |
| 190 } | |
| 191 | |
| 192 GURL page_url_; | |
| 193 | |
| 194 GURL download_image_url_; | |
| 195 int download_image_size_; | |
| 196 | |
| 197 private: | |
| 198 NavigationEntry entry_; | |
|
sky
2011/03/28 15:02:58
Descriptions?
michaelbai
2011/03/29 00:00:50
Done.
| |
| 199 int download_id_; | |
| 200 TabContents* tab_contents_; | |
| 201 scoped_ptr<DownloadHandler> download_handler_; | |
| 202 scoped_ptr<HistoryRequestHandler> history_handler_; | |
| 203 }; | |
|
sky
2011/03/28 15:02:58
DISALLOW_COPY_AND_ASSIGN
michaelbai
2011/03/29 00:00:50
Done.
| |
| 204 | |
| 205 void DownloadHandler::UpdateFaviconURL(FaviconHelper* helper, | |
| 206 const std::vector<FaviconURL> urls) { | |
| 207 helper->OnUpdateFaviconURL(0, urls); | |
| 208 } | |
| 209 | |
| 210 void DownloadHandler::UpdateFaviconURL(const std::vector<FaviconURL> urls) { | |
| 211 UpdateFaviconURL(favicon_helper_, urls); | |
| 212 } | |
| 213 | |
| 214 void DownloadHandler::InvokeCallback() { | |
| 215 favicon_helper_->OnDidDownloadFavicon(download_id_, image_url_, failed_, | |
| 216 bitmap_); | |
| 217 } | |
| 218 | |
| 219 void HistoryRequestHandler::InvokeCallback() { | |
| 220 callback_->Run(0, favicon_data_); | |
| 221 } | |
| 222 | |
| 223 class FaviconHelperTest : public RenderViewHostTestHarness { | |
| 224 }; | |
| 225 | |
| 226 TEST_F(FaviconHelperTest, GetFaviconFromHistory) { | |
| 227 const GURL page_url("http://www.google.com"); | |
| 228 const GURL icon_url("http://www.google.com/favicon"); | |
| 229 | |
| 230 TestFaviconHelper helper(page_url, contents(), FaviconHelper::FAVICON); | |
| 231 | |
| 232 helper.FetchFavicon(page_url); | |
| 233 HistoryRequestHandler* history_handler = helper.history_handler(); | |
| 234 // Ensure the data given to history is correct. | |
| 235 ASSERT_TRUE(history_handler); | |
| 236 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 237 EXPECT_EQ(GURL(), history_handler->icon_url_); | |
| 238 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); | |
| 239 | |
| 240 // Set valid icon data. | |
| 241 history_handler->favicon_data_.known_icon = true; | |
| 242 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 243 history_handler->favicon_data_.expired = false; | |
| 244 history_handler->favicon_data_.icon_url = icon_url; | |
| 245 scoped_refptr<RefCountedBytes> data = new RefCountedBytes(); | |
| 246 FillBitmap(kFaviconSize, kFaviconSize, &data->data); | |
| 247 history_handler->favicon_data_.image_data = data; | |
| 248 | |
| 249 // Send history response. | |
| 250 history_handler->InvokeCallback(); | |
| 251 // Verify FaviconHelper status | |
| 252 EXPECT_TRUE(helper.GetEntry()->favicon().is_valid()); | |
| 253 EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url()); | |
| 254 | |
| 255 // Simulates update favicon url. | |
| 256 std::vector<FaviconURL> urls; | |
| 257 urls.push_back(FaviconURL(icon_url, FAVICON)); | |
| 258 DownloadHandler::UpdateFaviconURL(&helper, urls); | |
| 259 | |
| 260 // Verify FaviconHelper status | |
| 261 EXPECT_EQ(1U, helper.urls_.size()); | |
| 262 ASSERT_TRUE(helper.current_candidate()); | |
| 263 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); | |
| 264 ASSERT_EQ(FAVICON, helper.current_candidate()->icon_type); | |
| 265 | |
| 266 // Favicon shouldn't request to download icon. | |
| 267 DownloadHandler* download_handler = helper.download_handler(); | |
| 268 ASSERT_FALSE(download_handler); | |
| 269 } | |
| 270 | |
| 271 TEST_F(FaviconHelperTest, DownloadFavicon) { | |
| 272 const GURL page_url("http://www.google.com"); | |
| 273 const GURL icon_url("http://www.google.com/favicon"); | |
| 274 | |
| 275 TestFaviconHelper helper(page_url, contents(), FaviconHelper::FAVICON); | |
| 276 | |
| 277 helper.FetchFavicon(page_url); | |
| 278 HistoryRequestHandler* history_handler = helper.history_handler(); | |
| 279 // Ensure the data given to history is correct. | |
| 280 ASSERT_TRUE(history_handler); | |
| 281 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 282 EXPECT_EQ(GURL(), history_handler->icon_url_); | |
| 283 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); | |
| 284 | |
| 285 // Set icon data expired | |
| 286 history_handler->favicon_data_.known_icon = true; | |
| 287 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 288 history_handler->favicon_data_.expired = true; | |
| 289 history_handler->favicon_data_.icon_url = icon_url; | |
| 290 // Send history response. | |
| 291 history_handler->InvokeCallback(); | |
| 292 // Verify FaviconHelper status | |
| 293 EXPECT_TRUE(helper.GetEntry()->favicon().is_valid()); | |
| 294 EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url()); | |
| 295 | |
| 296 // Simulates update favicon url. | |
| 297 std::vector<FaviconURL> urls; | |
| 298 urls.push_back(FaviconURL(icon_url, FAVICON)); | |
| 299 DownloadHandler::UpdateFaviconURL(&helper, urls); | |
| 300 | |
| 301 // Verify FaviconHelper status | |
| 302 EXPECT_EQ(1U, helper.urls_.size()); | |
| 303 ASSERT_TRUE(helper.current_candidate()); | |
| 304 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); | |
| 305 ASSERT_EQ(FAVICON, helper.current_candidate()->icon_type); | |
| 306 | |
| 307 // Favicon should request to download icon now. | |
| 308 DownloadHandler* download_handler = helper.download_handler(); | |
| 309 ASSERT_TRUE(download_handler); | |
| 310 // Verify the download request. | |
| 311 EXPECT_EQ(icon_url, download_handler->image_url_); | |
| 312 EXPECT_EQ(kFaviconSize, download_handler->image_size_); | |
| 313 | |
| 314 // Reset the history_handler to verify whether favicon is set. | |
| 315 helper.set_history_handler(NULL); | |
| 316 | |
| 317 // Smulates download done. | |
| 318 download_handler->InvokeCallback(); | |
| 319 | |
| 320 // New icon should be saved to history backend and navigation entry. | |
| 321 history_handler = helper.history_handler(); | |
| 322 ASSERT_TRUE(history_handler); | |
| 323 EXPECT_EQ(icon_url, history_handler->icon_url_); | |
| 324 EXPECT_EQ(FAVICON, history_handler->icon_type_); | |
| 325 EXPECT_LT(0U, history_handler->image_data_.size()); | |
| 326 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 327 | |
| 328 // Verify NavigationEntry. | |
| 329 EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url()); | |
| 330 EXPECT_TRUE(helper.GetEntry()->favicon().is_valid()); | |
| 331 EXPECT_FALSE(helper.GetEntry()->favicon().bitmap().empty()); | |
| 332 } | |
| 333 | |
| 334 TEST_F(FaviconHelperTest, UpdateAndDownloadFavicon) { | |
| 335 const GURL page_url("http://www.google.com"); | |
| 336 const GURL icon_url("http://www.google.com/favicon"); | |
| 337 const GURL new_icon_url("http://www.google.com/new_favicon"); | |
| 338 | |
| 339 TestFaviconHelper helper(page_url, contents(), FaviconHelper::FAVICON); | |
| 340 | |
| 341 helper.FetchFavicon(page_url); | |
| 342 HistoryRequestHandler* history_handler = helper.history_handler(); | |
| 343 // Ensure the data given to history is correct. | |
| 344 ASSERT_TRUE(history_handler); | |
| 345 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 346 EXPECT_EQ(GURL(), history_handler->icon_url_); | |
| 347 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); | |
| 348 | |
| 349 // Set valid icon data. | |
| 350 history_handler->favicon_data_.known_icon = true; | |
| 351 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 352 history_handler->favicon_data_.expired = false; | |
| 353 history_handler->favicon_data_.icon_url = icon_url; | |
| 354 scoped_refptr<RefCountedBytes> data = new RefCountedBytes(); | |
| 355 FillBitmap(kFaviconSize, kFaviconSize, &data->data); | |
| 356 history_handler->favicon_data_.image_data = data; | |
| 357 | |
| 358 // Send history response. | |
| 359 history_handler->InvokeCallback(); | |
| 360 // Verify FaviconHelper status. | |
| 361 EXPECT_TRUE(helper.GetEntry()->favicon().is_valid()); | |
| 362 EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url()); | |
| 363 | |
| 364 // Reset the history_handler to verify whether new icon is requested from | |
| 365 // history. | |
| 366 helper.set_history_handler(NULL); | |
| 367 | |
| 368 // Simulates update with the different favicon url. | |
| 369 std::vector<FaviconURL> urls; | |
| 370 urls.push_back(FaviconURL(new_icon_url, FAVICON)); | |
| 371 DownloadHandler::UpdateFaviconURL(&helper, urls); | |
| 372 | |
| 373 // Verify FaviconHelper status. | |
| 374 EXPECT_EQ(1U, helper.urls_.size()); | |
| 375 ASSERT_TRUE(helper.current_candidate()); | |
| 376 ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url); | |
| 377 ASSERT_EQ(FAVICON, helper.current_candidate()->icon_type); | |
| 378 // The favicon status's url should be updated. | |
| 379 ASSERT_EQ(new_icon_url, helper.GetEntry()->favicon().url()); | |
| 380 | |
| 381 // Favicon should be requested from history. | |
| 382 history_handler = helper.history_handler(); | |
| 383 ASSERT_TRUE(history_handler); | |
| 384 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 385 EXPECT_EQ(FAVICON, history_handler->icon_type_); | |
| 386 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 387 | |
| 388 // Simulate not find icon. | |
| 389 history_handler->favicon_data_.known_icon = false; | |
| 390 history_handler->InvokeCallback(); | |
| 391 | |
| 392 // Favicon should request to download icon now. | |
| 393 DownloadHandler* download_handler = helper.download_handler(); | |
| 394 ASSERT_TRUE(download_handler); | |
| 395 // Verify the download request. | |
| 396 EXPECT_EQ(new_icon_url, download_handler->image_url_); | |
| 397 EXPECT_EQ(kFaviconSize, download_handler->image_size_); | |
| 398 | |
| 399 // Reset the history_handler to verify whether favicon is set. | |
| 400 helper.set_history_handler(NULL); | |
| 401 | |
| 402 // Smulates download done. | |
| 403 download_handler->InvokeCallback(); | |
| 404 | |
| 405 // New icon should be saved to history backend and navigation entry. | |
| 406 history_handler = helper.history_handler(); | |
| 407 ASSERT_TRUE(history_handler); | |
| 408 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 409 EXPECT_EQ(FAVICON, history_handler->icon_type_); | |
| 410 EXPECT_LT(0U, history_handler->image_data_.size()); | |
| 411 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 412 | |
| 413 // Verify NavigationEntry. | |
| 414 EXPECT_EQ(new_icon_url, helper.GetEntry()->favicon().url()); | |
| 415 EXPECT_TRUE(helper.GetEntry()->favicon().is_valid()); | |
| 416 EXPECT_FALSE(helper.GetEntry()->favicon().bitmap().empty()); | |
| 417 } | |
| 418 | |
| 419 TEST_F(FaviconHelperTest, UpdateFavicon) { | |
| 420 const GURL page_url("http://www.google.com"); | |
| 421 const GURL icon_url("http://www.google.com/favicon"); | |
| 422 const GURL new_icon_url("http://www.google.com/new_favicon"); | |
| 423 | |
| 424 TestFaviconHelper helper(page_url, contents(), FaviconHelper::FAVICON); | |
| 425 | |
| 426 helper.FetchFavicon(page_url); | |
| 427 HistoryRequestHandler* history_handler = helper.history_handler(); | |
| 428 // Ensure the data given to history is correct. | |
| 429 ASSERT_TRUE(history_handler); | |
| 430 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 431 EXPECT_EQ(GURL(), history_handler->icon_url_); | |
| 432 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); | |
| 433 | |
| 434 // Set valid icon data. | |
| 435 history_handler->favicon_data_.known_icon = true; | |
| 436 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 437 history_handler->favicon_data_.expired = false; | |
| 438 history_handler->favicon_data_.icon_url = icon_url; | |
| 439 scoped_refptr<RefCountedBytes> data = new RefCountedBytes(); | |
| 440 FillBitmap(kFaviconSize, kFaviconSize, &data->data); | |
| 441 history_handler->favicon_data_.image_data = data; | |
| 442 | |
| 443 // Send history response. | |
| 444 history_handler->InvokeCallback(); | |
| 445 // Verify FaviconHelper status. | |
| 446 EXPECT_TRUE(helper.GetEntry()->favicon().is_valid()); | |
| 447 EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url()); | |
| 448 | |
| 449 // Reset the history_handler to verify whether new icon is requested from | |
| 450 // history. | |
| 451 helper.set_history_handler(NULL); | |
| 452 | |
| 453 // Simulates update with the different favicon url. | |
| 454 std::vector<FaviconURL> urls; | |
| 455 urls.push_back(FaviconURL(new_icon_url, FAVICON)); | |
| 456 DownloadHandler::UpdateFaviconURL(&helper, urls); | |
| 457 | |
| 458 // Verify FaviconHelper status. | |
| 459 EXPECT_EQ(1U, helper.urls_.size()); | |
| 460 ASSERT_TRUE(helper.current_candidate()); | |
| 461 ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url); | |
| 462 ASSERT_EQ(FAVICON, helper.current_candidate()->icon_type); | |
| 463 // The favicon status's url should be updated. | |
| 464 ASSERT_EQ(new_icon_url, helper.GetEntry()->favicon().url()); | |
| 465 | |
| 466 // Favicon should be requested from history. | |
| 467 history_handler = helper.history_handler(); | |
| 468 ASSERT_TRUE(history_handler); | |
| 469 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 470 EXPECT_EQ(FAVICON, history_handler->icon_type_); | |
| 471 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 472 | |
| 473 // Simulate find icon. | |
| 474 history_handler->favicon_data_.known_icon = true; | |
| 475 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 476 history_handler->favicon_data_.expired = false; | |
| 477 history_handler->favicon_data_.icon_url = new_icon_url; | |
| 478 history_handler->favicon_data_.image_data = data; | |
| 479 history_handler->InvokeCallback(); | |
| 480 | |
| 481 // Shouldn't request download favicon | |
| 482 EXPECT_FALSE(helper.download_handler()); | |
| 483 | |
| 484 // Verify the favicon status. | |
| 485 EXPECT_EQ(new_icon_url, helper.GetEntry()->favicon().url()); | |
| 486 EXPECT_TRUE(helper.GetEntry()->favicon().is_valid()); | |
| 487 EXPECT_FALSE(helper.GetEntry()->favicon().bitmap().empty()); | |
| 488 } | |
| 489 | |
| 490 TEST_F(FaviconHelperTest, Download2ndFaviconURLCandidate) { | |
| 491 const GURL page_url("http://www.google.com"); | |
| 492 const GURL icon_url("http://www.google.com/favicon"); | |
| 493 const GURL new_icon_url("http://www.google.com/new_favicon"); | |
| 494 | |
| 495 TestFaviconHelper helper(page_url, contents(), FaviconHelper::TOUCH); | |
| 496 | |
| 497 helper.FetchFavicon(page_url); | |
| 498 HistoryRequestHandler* history_handler = helper.history_handler(); | |
| 499 // Ensure the data given to history is correct. | |
| 500 ASSERT_TRUE(history_handler); | |
| 501 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 502 EXPECT_EQ(GURL(), history_handler->icon_url_); | |
| 503 EXPECT_EQ(history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON, | |
| 504 history_handler->icon_type_); | |
| 505 | |
| 506 // Icon not found. | |
| 507 history_handler->favicon_data_.known_icon = false; | |
| 508 // Send history response. | |
| 509 history_handler->InvokeCallback(); | |
| 510 // Verify FaviconHelper status. | |
| 511 EXPECT_FALSE(helper.GetEntry()->favicon().is_valid()); | |
| 512 EXPECT_EQ(GURL(), helper.GetEntry()->favicon().url()); | |
| 513 | |
| 514 // Reset the history_handler to verify whether new icon is requested from | |
| 515 // history. | |
| 516 helper.set_history_handler(NULL); | |
| 517 | |
| 518 // Simulates update with the different favicon url. | |
| 519 std::vector<FaviconURL> urls; | |
| 520 urls.push_back(FaviconURL(icon_url, TOUCH_PRECOMPOSED_ICON)); | |
| 521 urls.push_back(FaviconURL(new_icon_url, TOUCH_ICON)); | |
| 522 urls.push_back(FaviconURL(new_icon_url, FAVICON)); | |
| 523 | |
| 524 DownloadHandler::UpdateFaviconURL(&helper, urls); | |
| 525 | |
| 526 // Verify FaviconHelper status. | |
| 527 EXPECT_EQ(2U, helper.urls_.size()); | |
| 528 ASSERT_TRUE(helper.current_candidate()); | |
| 529 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); | |
| 530 ASSERT_EQ(TOUCH_PRECOMPOSED_ICON, helper.current_candidate()->icon_type); | |
| 531 | |
| 532 // Favicon should be requested from history. | |
| 533 history_handler = helper.history_handler(); | |
| 534 ASSERT_TRUE(history_handler); | |
| 535 EXPECT_EQ(icon_url, history_handler->icon_url_); | |
| 536 EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_); | |
| 537 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 538 | |
| 539 // Simulate not find icon. | |
| 540 history_handler->favicon_data_.known_icon = false; | |
| 541 history_handler->InvokeCallback(); | |
| 542 | |
| 543 // Should request download favicon. | |
| 544 DownloadHandler* download_handler = helper.download_handler(); | |
| 545 EXPECT_TRUE(download_handler); | |
| 546 // Verify the download request. | |
| 547 EXPECT_EQ(icon_url, download_handler->image_url_); | |
| 548 EXPECT_EQ(0, download_handler->image_size_); | |
| 549 | |
| 550 // Reset the history_handler to verify whether favicon is request from | |
| 551 // history. | |
| 552 helper.set_history_handler(NULL); | |
| 553 // Smulates download failed. | |
| 554 download_handler->failed_ = true; | |
| 555 download_handler->InvokeCallback(); | |
| 556 | |
| 557 // Left 1 url. | |
| 558 EXPECT_EQ(1U, helper.urls_.size()); | |
| 559 ASSERT_TRUE(helper.current_candidate()); | |
| 560 EXPECT_EQ(new_icon_url, helper.current_candidate()->icon_url); | |
| 561 EXPECT_EQ(TOUCH_ICON, helper.current_candidate()->icon_type); | |
| 562 | |
| 563 // Favicon should be requested from history. | |
| 564 history_handler = helper.history_handler(); | |
| 565 ASSERT_TRUE(history_handler); | |
| 566 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 567 EXPECT_EQ(TOUCH_ICON, history_handler->icon_type_); | |
| 568 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 569 | |
| 570 // Reset download handler | |
| 571 helper.set_download_handler(NULL); | |
| 572 | |
| 573 // Smulates getting a expired icon from history. | |
| 574 history_handler->favicon_data_.known_icon = true; | |
| 575 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 576 history_handler->favicon_data_.expired = true; | |
| 577 history_handler->favicon_data_.icon_url = new_icon_url; | |
| 578 scoped_refptr<RefCountedBytes> data = new RefCountedBytes(); | |
| 579 FillBitmap(kFaviconSize, kFaviconSize, &data->data); | |
| 580 history_handler->favicon_data_.image_data = data; | |
| 581 history_handler->InvokeCallback(); | |
| 582 | |
| 583 // Verify the download request. | |
| 584 download_handler = helper.download_handler(); | |
| 585 EXPECT_TRUE(download_handler); | |
| 586 EXPECT_EQ(new_icon_url, download_handler->image_url_); | |
| 587 EXPECT_EQ(0, download_handler->image_size_); | |
| 588 | |
| 589 helper.set_history_handler(NULL); | |
| 590 | |
| 591 // Simulates icon being downloaded. | |
| 592 download_handler->InvokeCallback(); | |
| 593 | |
| 594 // New icon should be saved to history backend. | |
| 595 history_handler = helper.history_handler(); | |
| 596 ASSERT_TRUE(history_handler); | |
| 597 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | |
| 598 EXPECT_EQ(TOUCH_ICON, history_handler->icon_type_); | |
| 599 EXPECT_LT(0U, history_handler->image_data_.size()); | |
| 600 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 601 } | |
| 602 | |
| 603 TEST_F(FaviconHelperTest, UpdateDuringDownloading) { | |
| 604 const GURL page_url("http://www.google.com"); | |
| 605 const GURL icon_url("http://www.google.com/favicon"); | |
| 606 const GURL new_icon_url("http://www.google.com/new_favicon"); | |
| 607 | |
| 608 TestFaviconHelper helper(page_url, contents(), FaviconHelper::TOUCH); | |
| 609 | |
| 610 helper.FetchFavicon(page_url); | |
| 611 HistoryRequestHandler* history_handler = helper.history_handler(); | |
| 612 // Ensure the data given to history is correct. | |
| 613 ASSERT_TRUE(history_handler); | |
| 614 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 615 EXPECT_EQ(GURL(), history_handler->icon_url_); | |
| 616 EXPECT_EQ(history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON, | |
| 617 history_handler->icon_type_); | |
| 618 | |
| 619 // Icon not found. | |
| 620 history_handler->favicon_data_.known_icon = false; | |
| 621 // Send history response. | |
| 622 history_handler->InvokeCallback(); | |
| 623 // Verify FaviconHelper status. | |
| 624 EXPECT_FALSE(helper.GetEntry()->favicon().is_valid()); | |
| 625 EXPECT_EQ(GURL(), helper.GetEntry()->favicon().url()); | |
| 626 | |
| 627 // Reset the history_handler to verify whether new icon is requested from | |
| 628 // history. | |
| 629 helper.set_history_handler(NULL); | |
| 630 | |
| 631 // Simulates update with the different favicon url. | |
| 632 std::vector<FaviconURL> urls; | |
| 633 urls.push_back(FaviconURL(icon_url, TOUCH_PRECOMPOSED_ICON)); | |
| 634 urls.push_back(FaviconURL(new_icon_url, TOUCH_ICON)); | |
| 635 urls.push_back(FaviconURL(new_icon_url, FAVICON)); | |
| 636 | |
| 637 DownloadHandler::UpdateFaviconURL(&helper, urls); | |
| 638 | |
| 639 // Verify FaviconHelper status. | |
| 640 EXPECT_EQ(2U, helper.urls_.size()); | |
| 641 ASSERT_TRUE(helper.current_candidate()); | |
| 642 ASSERT_EQ(icon_url, helper.current_candidate()->icon_url); | |
| 643 ASSERT_EQ(TOUCH_PRECOMPOSED_ICON, helper.current_candidate()->icon_type); | |
| 644 | |
| 645 // Favicon should be requested from history. | |
| 646 history_handler = helper.history_handler(); | |
| 647 ASSERT_TRUE(history_handler); | |
| 648 EXPECT_EQ(icon_url, history_handler->icon_url_); | |
| 649 EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_); | |
| 650 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 651 | |
| 652 // Simulate not find icon. | |
| 653 history_handler->favicon_data_.known_icon = false; | |
| 654 history_handler->InvokeCallback(); | |
| 655 | |
| 656 // Should request download favicon. | |
| 657 DownloadHandler* download_handler = helper.download_handler(); | |
| 658 EXPECT_TRUE(download_handler); | |
| 659 // Verify the download request. | |
| 660 EXPECT_EQ(icon_url, download_handler->image_url_); | |
| 661 EXPECT_EQ(0, download_handler->image_size_); | |
| 662 | |
| 663 // Reset the history_handler to verify whether favicon is request from | |
| 664 // history. | |
| 665 helper.set_history_handler(NULL); | |
| 666 const GURL latest_icon_url("http://www.google.com/latest_favicon"); | |
| 667 std::vector<FaviconURL> latest_urls; | |
| 668 latest_urls.push_back(FaviconURL(latest_icon_url, TOUCH_ICON)); | |
| 669 DownloadHandler::UpdateFaviconURL(&helper, latest_urls); | |
| 670 EXPECT_EQ(1U, helper.urls_.size()); | |
| 671 EXPECT_EQ(latest_icon_url, helper.current_candidate()->icon_url); | |
| 672 EXPECT_EQ(TOUCH_ICON, helper.current_candidate()->icon_type); | |
| 673 | |
| 674 // Whether new icon is requested from history | |
| 675 history_handler = helper.history_handler(); | |
| 676 ASSERT_TRUE(history_handler); | |
| 677 EXPECT_EQ(latest_icon_url, history_handler->icon_url_); | |
| 678 EXPECT_EQ(TOUCH_ICON, history_handler->icon_type_); | |
| 679 EXPECT_EQ(page_url, history_handler->page_url_); | |
| 680 | |
| 681 // Reset the history_handler to verify whether favicon is request from | |
| 682 // history. | |
| 683 helper.set_history_handler(NULL); | |
| 684 | |
| 685 // Smulates download succeed. | |
| 686 download_handler->InvokeCallback(); | |
| 687 // The downloaded icon should be thrown away as there is faviocn update. | |
| 688 EXPECT_FALSE(helper.history_handler()); | |
| 689 | |
| 690 helper.set_download_handler(NULL); | |
| 691 // Smulates getting the icon from history. | |
| 692 history_handler->favicon_data_.known_icon = true; | |
| 693 history_handler->favicon_data_.icon_type = history::TOUCH_ICON; | |
| 694 history_handler->favicon_data_.expired = false; | |
| 695 history_handler->favicon_data_.icon_url = latest_icon_url; | |
| 696 scoped_refptr<RefCountedBytes> data = new RefCountedBytes(); | |
| 697 FillBitmap(kFaviconSize, kFaviconSize, &data->data); | |
| 698 history_handler->favicon_data_.image_data = data; | |
| 699 history_handler->InvokeCallback(); | |
| 700 | |
| 701 // No download request. | |
| 702 EXPECT_FALSE(helper.download_handler()); | |
| 703 } | |
| OLD | NEW |