| 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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "chrome/browser/favicon/favicon_handler.h" | 6 #include "chrome/browser/favicon/favicon_handler.h" |
| 7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 9 #include "content/public/browser/invalidate_type.h" | 9 #include "content/public/browser/invalidate_type.h" |
| 10 #include "content/public/browser/navigation_entry.h" | 10 #include "content/public/browser/navigation_entry.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 // Fill the given data buffer with valid png data. | 40 // Fill the given data buffer with valid png data. |
| 41 void FillBitmap(int w, int h, std::vector<unsigned char>* output) { | 41 void FillBitmap(int w, int h, std::vector<unsigned char>* output) { |
| 42 SkBitmap bitmap; | 42 SkBitmap bitmap; |
| 43 FillDataToBitmap(w, h, &bitmap); | 43 FillDataToBitmap(w, h, &bitmap); |
| 44 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output); | 44 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void SetFaviconDataWithOneBitmap(const GURL& icon_url, |
| 48 history::IconType icon_type, |
| 49 bool expired, |
| 50 history::FaviconData* favicon_data) { |
| 51 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes()); |
| 52 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); |
| 53 history::FaviconBitmapData favicon_bitmap_data; |
| 54 favicon_bitmap_data.expired = expired; |
| 55 favicon_bitmap_data.bitmap_data = data; |
| 56 favicon_bitmap_data.pixel_size = gfx::Size(); |
| 57 |
| 58 favicon_data->known_icon = true; |
| 59 favicon_data->icon_type = icon_type; |
| 60 favicon_data->icon_url = icon_url; |
| 61 favicon_data->bitmaps.clear(); |
| 62 favicon_data->bitmaps.push_back(favicon_bitmap_data); |
| 63 } |
| 64 |
| 65 void SetFaviconDataWithOneBitmap(const GURL& icon_url, |
| 66 history::FaviconData* favicon_data) { |
| 67 SetFaviconDataWithOneBitmap(icon_url, history::FAVICON, false /* expired */, |
| 68 favicon_data); |
| 69 } |
| 70 |
| 47 // This class is used to save the download request for verifying with test case. | 71 // This class is used to save the download request for verifying with test case. |
| 48 // It also will be used to invoke the onDidDownload callback. | 72 // It also will be used to invoke the onDidDownload callback. |
| 49 class DownloadHandler { | 73 class DownloadHandler { |
| 50 public: | 74 public: |
| 51 explicit DownloadHandler(TestFaviconHandler* favicon_helper) | 75 explicit DownloadHandler(TestFaviconHandler* favicon_helper) |
| 52 : favicon_helper_(favicon_helper), | 76 : favicon_helper_(favicon_helper), |
| 53 failed_(false) { | 77 failed_(false) { |
| 54 } | 78 } |
| 55 | 79 |
| 56 virtual ~DownloadHandler() { | 80 virtual ~DownloadHandler() { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 const FaviconService::FaviconDataCallback& callback) | 127 const FaviconService::FaviconDataCallback& callback) |
| 104 : page_url_(page_url), | 128 : page_url_(page_url), |
| 105 icon_url_(icon_url), | 129 icon_url_(icon_url), |
| 106 icon_type_(icon_type), | 130 icon_type_(icon_type), |
| 107 callback_(callback) { | 131 callback_(callback) { |
| 108 } | 132 } |
| 109 | 133 |
| 110 HistoryRequestHandler(const GURL& page_url, | 134 HistoryRequestHandler(const GURL& page_url, |
| 111 const GURL& icon_url, | 135 const GURL& icon_url, |
| 112 int icon_type, | 136 int icon_type, |
| 113 const std::vector<unsigned char>& image_data, | 137 const std::vector<unsigned char>& bitmap_data, |
| 114 const FaviconService::FaviconDataCallback& callback) | 138 const FaviconService::FaviconDataCallback& callback) |
| 115 : page_url_(page_url), | 139 : page_url_(page_url), |
| 116 icon_url_(icon_url), | 140 icon_url_(icon_url), |
| 117 icon_type_(icon_type), | 141 icon_type_(icon_type), |
| 118 image_data_(image_data), | 142 bitmap_data_(bitmap_data), |
| 119 callback_(callback) { | 143 callback_(callback) { |
| 120 } | 144 } |
| 121 | 145 |
| 122 virtual ~HistoryRequestHandler() {} | 146 virtual ~HistoryRequestHandler() {} |
| 123 void InvokeCallback(); | 147 void InvokeCallback(); |
| 124 | 148 |
| 125 const GURL page_url_; | 149 const GURL page_url_; |
| 126 const GURL icon_url_; | 150 const GURL icon_url_; |
| 127 const int icon_type_; | 151 const int icon_type_; |
| 128 const std::vector<unsigned char> image_data_; | 152 const std::vector<unsigned char> bitmap_data_; |
| 129 history::FaviconData favicon_data_; | 153 history::FaviconData favicon_data_; |
| 130 FaviconService::FaviconDataCallback callback_; | 154 FaviconService::FaviconDataCallback callback_; |
| 131 | 155 |
| 132 private: | 156 private: |
| 133 DISALLOW_COPY_AND_ASSIGN(HistoryRequestHandler); | 157 DISALLOW_COPY_AND_ASSIGN(HistoryRequestHandler); |
| 134 }; | 158 }; |
| 135 | 159 |
| 136 } // namespace | 160 } // namespace |
| 137 | 161 |
| 138 | 162 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 } | 270 } |
| 247 | 271 |
| 248 virtual int DownloadFavicon(const GURL& image_url, int image_size) OVERRIDE { | 272 virtual int DownloadFavicon(const GURL& image_url, int image_size) OVERRIDE { |
| 249 download_id_++; | 273 download_id_++; |
| 250 download_handler_->AddDownload(download_id_, image_url, image_size); | 274 download_handler_->AddDownload(download_id_, image_url, image_size); |
| 251 return download_id_; | 275 return download_id_; |
| 252 } | 276 } |
| 253 | 277 |
| 254 virtual void SetHistoryFavicon(const GURL& page_url, | 278 virtual void SetHistoryFavicon(const GURL& page_url, |
| 255 const GURL& icon_url, | 279 const GURL& icon_url, |
| 256 const std::vector<unsigned char>& image_data, | 280 const std::vector<unsigned char>& bitmap_data, |
| 257 history::IconType icon_type) OVERRIDE { | 281 history::IconType icon_type) OVERRIDE { |
| 258 history_handler_.reset(new HistoryRequestHandler( | 282 history_handler_.reset(new HistoryRequestHandler( |
| 259 page_url, icon_url,icon_type, image_data, | 283 page_url, icon_url,icon_type, bitmap_data, |
| 260 FaviconService::FaviconDataCallback())); | 284 FaviconService::FaviconDataCallback())); |
| 261 } | 285 } |
| 262 | 286 |
| 263 virtual FaviconService* GetFaviconService() OVERRIDE { | 287 virtual FaviconService* GetFaviconService() OVERRIDE { |
| 264 // Just give none NULL value, so overridden methods can be hit. | 288 // Just give none NULL value, so overridden methods can be hit. |
| 265 return (FaviconService*)(1); | 289 return (FaviconService*)(1); |
| 266 } | 290 } |
| 267 | 291 |
| 268 virtual bool ShouldSaveFavicon(const GURL& url) OVERRIDE { | 292 virtual bool ShouldSaveFavicon(const GURL& url) OVERRIDE { |
| 269 return true; | 293 return true; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 284 scoped_ptr<DownloadHandler> download_handler_; | 308 scoped_ptr<DownloadHandler> download_handler_; |
| 285 scoped_ptr<HistoryRequestHandler> history_handler_; | 309 scoped_ptr<HistoryRequestHandler> history_handler_; |
| 286 | 310 |
| 287 DISALLOW_COPY_AND_ASSIGN(TestFaviconHandler); | 311 DISALLOW_COPY_AND_ASSIGN(TestFaviconHandler); |
| 288 }; | 312 }; |
| 289 | 313 |
| 290 namespace { | 314 namespace { |
| 291 | 315 |
| 292 void HistoryRequestHandler::InvokeCallback() { | 316 void HistoryRequestHandler::InvokeCallback() { |
| 293 if (!callback_.is_null()) | 317 if (!callback_.is_null()) |
| 294 callback_.Run(0, favicon_data_); | 318 callback_.Run(0, favicon_data_, std::vector<GURL>()); |
| 295 } | 319 } |
| 296 | 320 |
| 297 void DownloadHandler::InvokeCallback() { | 321 void DownloadHandler::InvokeCallback() { |
| 298 SkBitmap bitmap; | 322 SkBitmap bitmap; |
| 299 int bitmap_size = (download_->image_size > 0) ? | 323 int bitmap_size = (download_->image_size > 0) ? |
| 300 download_->image_size : gfx::kFaviconSize; | 324 download_->image_size : gfx::kFaviconSize; |
| 301 FillDataToBitmap(bitmap_size, bitmap_size, &bitmap); | 325 FillDataToBitmap(bitmap_size, bitmap_size, &bitmap); |
| 302 gfx::Image image(bitmap); | 326 gfx::Image image(bitmap); |
| 303 favicon_helper_->OnDidDownloadFavicon( | 327 favicon_helper_->OnDidDownloadFavicon( |
| 304 download_->download_id, download_->image_url, failed_, image, | 328 download_->download_id, download_->image_url, failed_, image, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 319 &delegate, FaviconHandler::FAVICON); | 343 &delegate, FaviconHandler::FAVICON); |
| 320 | 344 |
| 321 helper.FetchFavicon(page_url); | 345 helper.FetchFavicon(page_url); |
| 322 HistoryRequestHandler* history_handler = helper.history_handler(); | 346 HistoryRequestHandler* history_handler = helper.history_handler(); |
| 323 // Ensure the data given to history is correct. | 347 // Ensure the data given to history is correct. |
| 324 ASSERT_TRUE(history_handler); | 348 ASSERT_TRUE(history_handler); |
| 325 EXPECT_EQ(page_url, history_handler->page_url_); | 349 EXPECT_EQ(page_url, history_handler->page_url_); |
| 326 EXPECT_EQ(GURL(), history_handler->icon_url_); | 350 EXPECT_EQ(GURL(), history_handler->icon_url_); |
| 327 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); | 351 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); |
| 328 | 352 |
| 329 // Set valid icon data. | 353 SetFaviconDataWithOneBitmap(icon_url, &history_handler->favicon_data_); |
| 330 history_handler->favicon_data_.known_icon = true; | |
| 331 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 332 history_handler->favicon_data_.expired = false; | |
| 333 history_handler->favicon_data_.icon_url = icon_url; | |
| 334 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
| 335 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); | |
| 336 history_handler->favicon_data_.image_data = data; | |
| 337 | 354 |
| 338 // Send history response. | 355 // Send history response. |
| 339 history_handler->InvokeCallback(); | 356 history_handler->InvokeCallback(); |
| 340 // Verify FaviconHandler status | 357 // Verify FaviconHandler status |
| 341 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); | 358 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); |
| 342 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); | 359 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); |
| 343 | 360 |
| 344 // Simulates update favicon url. | 361 // Simulates update favicon url. |
| 345 std::vector<FaviconURL> urls; | 362 std::vector<FaviconURL> urls; |
| 346 urls.push_back(FaviconURL(icon_url, FaviconURL::FAVICON)); | 363 urls.push_back(FaviconURL(icon_url, FaviconURL::FAVICON)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 368 | 385 |
| 369 helper.FetchFavicon(page_url); | 386 helper.FetchFavicon(page_url); |
| 370 HistoryRequestHandler* history_handler = helper.history_handler(); | 387 HistoryRequestHandler* history_handler = helper.history_handler(); |
| 371 // Ensure the data given to history is correct. | 388 // Ensure the data given to history is correct. |
| 372 ASSERT_TRUE(history_handler); | 389 ASSERT_TRUE(history_handler); |
| 373 EXPECT_EQ(page_url, history_handler->page_url_); | 390 EXPECT_EQ(page_url, history_handler->page_url_); |
| 374 EXPECT_EQ(GURL(), history_handler->icon_url_); | 391 EXPECT_EQ(GURL(), history_handler->icon_url_); |
| 375 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); | 392 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); |
| 376 | 393 |
| 377 // Set icon data expired | 394 // Set icon data expired |
| 378 history_handler->favicon_data_.known_icon = true; | 395 SetFaviconDataWithOneBitmap(icon_url, history::FAVICON, true /* expired */, |
| 379 history_handler->favicon_data_.icon_type = history::FAVICON; | 396 &history_handler->favicon_data_); |
| 380 history_handler->favicon_data_.expired = true; | |
| 381 history_handler->favicon_data_.icon_url = icon_url; | |
| 382 // Send history response. | 397 // Send history response. |
| 383 history_handler->InvokeCallback(); | 398 history_handler->InvokeCallback(); |
| 384 // Verify FaviconHandler status | 399 // Verify FaviconHandler status |
| 385 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); | 400 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); |
| 386 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); | 401 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); |
| 387 | 402 |
| 388 // Simulates update favicon url. | 403 // Simulates update favicon url. |
| 389 std::vector<FaviconURL> urls; | 404 std::vector<FaviconURL> urls; |
| 390 urls.push_back(FaviconURL(icon_url, FaviconURL::FAVICON)); | 405 urls.push_back(FaviconURL(icon_url, FaviconURL::FAVICON)); |
| 391 helper.OnUpdateFaviconURL(0, urls); | 406 helper.OnUpdateFaviconURL(0, urls); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 408 helper.set_history_handler(NULL); | 423 helper.set_history_handler(NULL); |
| 409 | 424 |
| 410 // Smulates download done. | 425 // Smulates download done. |
| 411 download_handler->InvokeCallback(); | 426 download_handler->InvokeCallback(); |
| 412 | 427 |
| 413 // New icon should be saved to history backend and navigation entry. | 428 // New icon should be saved to history backend and navigation entry. |
| 414 history_handler = helper.history_handler(); | 429 history_handler = helper.history_handler(); |
| 415 ASSERT_TRUE(history_handler); | 430 ASSERT_TRUE(history_handler); |
| 416 EXPECT_EQ(icon_url, history_handler->icon_url_); | 431 EXPECT_EQ(icon_url, history_handler->icon_url_); |
| 417 EXPECT_EQ(FaviconURL::FAVICON, history_handler->icon_type_); | 432 EXPECT_EQ(FaviconURL::FAVICON, history_handler->icon_type_); |
| 418 EXPECT_LT(0U, history_handler->image_data_.size()); | 433 EXPECT_LT(0U, history_handler->bitmap_data_.size()); |
| 419 EXPECT_EQ(page_url, history_handler->page_url_); | 434 EXPECT_EQ(page_url, history_handler->page_url_); |
| 420 | 435 |
| 421 // Verify NavigationEntry. | 436 // Verify NavigationEntry. |
| 422 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); | 437 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); |
| 423 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); | 438 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); |
| 424 EXPECT_FALSE(helper.GetEntry()->GetFavicon().image.IsEmpty()); | 439 EXPECT_FALSE(helper.GetEntry()->GetFavicon().image.IsEmpty()); |
| 425 } | 440 } |
| 426 | 441 |
| 427 TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) { | 442 TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) { |
| 428 const GURL page_url("http://www.google.com"); | 443 const GURL page_url("http://www.google.com"); |
| 429 const GURL icon_url("http://www.google.com/favicon"); | 444 const GURL icon_url("http://www.google.com/favicon"); |
| 430 const GURL new_icon_url("http://www.google.com/new_favicon"); | 445 const GURL new_icon_url("http://www.google.com/new_favicon"); |
| 431 | 446 |
| 432 TestFaviconHandlerDelegate delegate(contents()); | 447 TestFaviconHandlerDelegate delegate(contents()); |
| 433 Profile* profile = Profile::FromBrowserContext( | 448 Profile* profile = Profile::FromBrowserContext( |
| 434 contents()->GetBrowserContext()); | 449 contents()->GetBrowserContext()); |
| 435 TestFaviconHandler helper(page_url, profile, | 450 TestFaviconHandler helper(page_url, profile, |
| 436 &delegate, FaviconHandler::FAVICON); | 451 &delegate, FaviconHandler::FAVICON); |
| 437 | 452 |
| 438 helper.FetchFavicon(page_url); | 453 helper.FetchFavicon(page_url); |
| 439 HistoryRequestHandler* history_handler = helper.history_handler(); | 454 HistoryRequestHandler* history_handler = helper.history_handler(); |
| 440 // Ensure the data given to history is correct. | 455 // Ensure the data given to history is correct. |
| 441 ASSERT_TRUE(history_handler); | 456 ASSERT_TRUE(history_handler); |
| 442 EXPECT_EQ(page_url, history_handler->page_url_); | 457 EXPECT_EQ(page_url, history_handler->page_url_); |
| 443 EXPECT_EQ(GURL(), history_handler->icon_url_); | 458 EXPECT_EQ(GURL(), history_handler->icon_url_); |
| 444 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); | 459 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); |
| 445 | 460 |
| 446 // Set valid icon data. | 461 // Set valid icon data. |
| 447 history_handler->favicon_data_.known_icon = true; | 462 SetFaviconDataWithOneBitmap(icon_url, &history_handler->favicon_data_); |
| 448 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 449 history_handler->favicon_data_.expired = false; | |
| 450 history_handler->favicon_data_.icon_url = icon_url; | |
| 451 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
| 452 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); | |
| 453 history_handler->favicon_data_.image_data = data; | |
| 454 | 463 |
| 455 // Send history response. | 464 // Send history response. |
| 456 history_handler->InvokeCallback(); | 465 history_handler->InvokeCallback(); |
| 457 // Verify FaviconHandler status. | 466 // Verify FaviconHandler status. |
| 458 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); | 467 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); |
| 459 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); | 468 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); |
| 460 | 469 |
| 461 // Reset the history_handler to verify whether new icon is requested from | 470 // Reset the history_handler to verify whether new icon is requested from |
| 462 // history. | 471 // history. |
| 463 helper.set_history_handler(NULL); | 472 helper.set_history_handler(NULL); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 helper.set_history_handler(NULL); | 507 helper.set_history_handler(NULL); |
| 499 | 508 |
| 500 // Smulates download done. | 509 // Smulates download done. |
| 501 download_handler->InvokeCallback(); | 510 download_handler->InvokeCallback(); |
| 502 | 511 |
| 503 // New icon should be saved to history backend and navigation entry. | 512 // New icon should be saved to history backend and navigation entry. |
| 504 history_handler = helper.history_handler(); | 513 history_handler = helper.history_handler(); |
| 505 ASSERT_TRUE(history_handler); | 514 ASSERT_TRUE(history_handler); |
| 506 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | 515 EXPECT_EQ(new_icon_url, history_handler->icon_url_); |
| 507 EXPECT_EQ(FaviconURL::FAVICON, history_handler->icon_type_); | 516 EXPECT_EQ(FaviconURL::FAVICON, history_handler->icon_type_); |
| 508 EXPECT_LT(0U, history_handler->image_data_.size()); | 517 EXPECT_LT(0U, history_handler->bitmap_data_.size()); |
| 509 EXPECT_EQ(page_url, history_handler->page_url_); | 518 EXPECT_EQ(page_url, history_handler->page_url_); |
| 510 | 519 |
| 511 // Verify NavigationEntry. | 520 // Verify NavigationEntry. |
| 512 EXPECT_EQ(new_icon_url, helper.GetEntry()->GetFavicon().url); | 521 EXPECT_EQ(new_icon_url, helper.GetEntry()->GetFavicon().url); |
| 513 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); | 522 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); |
| 514 EXPECT_FALSE(helper.GetEntry()->GetFavicon().image.IsEmpty()); | 523 EXPECT_FALSE(helper.GetEntry()->GetFavicon().image.IsEmpty()); |
| 515 } | 524 } |
| 516 | 525 |
| 517 TEST_F(FaviconHandlerTest, UpdateFavicon) { | 526 TEST_F(FaviconHandlerTest, UpdateFavicon) { |
| 518 const GURL page_url("http://www.google.com"); | 527 const GURL page_url("http://www.google.com"); |
| 519 const GURL icon_url("http://www.google.com/favicon"); | 528 const GURL icon_url("http://www.google.com/favicon"); |
| 520 const GURL new_icon_url("http://www.google.com/new_favicon"); | 529 const GURL new_icon_url("http://www.google.com/new_favicon"); |
| 521 | 530 |
| 522 TestFaviconHandlerDelegate delegate(contents()); | 531 TestFaviconHandlerDelegate delegate(contents()); |
| 523 Profile* profile = Profile::FromBrowserContext( | 532 Profile* profile = Profile::FromBrowserContext( |
| 524 contents()->GetBrowserContext()); | 533 contents()->GetBrowserContext()); |
| 525 TestFaviconHandler helper(page_url, profile, | 534 TestFaviconHandler helper(page_url, profile, |
| 526 &delegate, FaviconHandler::FAVICON); | 535 &delegate, FaviconHandler::FAVICON); |
| 527 | 536 |
| 528 helper.FetchFavicon(page_url); | 537 helper.FetchFavicon(page_url); |
| 529 HistoryRequestHandler* history_handler = helper.history_handler(); | 538 HistoryRequestHandler* history_handler = helper.history_handler(); |
| 530 // Ensure the data given to history is correct. | 539 // Ensure the data given to history is correct. |
| 531 ASSERT_TRUE(history_handler); | 540 ASSERT_TRUE(history_handler); |
| 532 EXPECT_EQ(page_url, history_handler->page_url_); | 541 EXPECT_EQ(page_url, history_handler->page_url_); |
| 533 EXPECT_EQ(GURL(), history_handler->icon_url_); | 542 EXPECT_EQ(GURL(), history_handler->icon_url_); |
| 534 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); | 543 EXPECT_EQ(history::FAVICON, history_handler->icon_type_); |
| 535 | 544 |
| 536 // Set valid icon data. | 545 SetFaviconDataWithOneBitmap(icon_url, &history_handler->favicon_data_); |
| 537 history_handler->favicon_data_.known_icon = true; | |
| 538 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 539 history_handler->favicon_data_.expired = false; | |
| 540 history_handler->favicon_data_.icon_url = icon_url; | |
| 541 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
| 542 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); | |
| 543 history_handler->favicon_data_.image_data = data; | |
| 544 | 546 |
| 545 // Send history response. | 547 // Send history response. |
| 546 history_handler->InvokeCallback(); | 548 history_handler->InvokeCallback(); |
| 547 // Verify FaviconHandler status. | 549 // Verify FaviconHandler status. |
| 548 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); | 550 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); |
| 549 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); | 551 EXPECT_EQ(icon_url, helper.GetEntry()->GetFavicon().url); |
| 550 | 552 |
| 551 // Reset the history_handler to verify whether new icon is requested from | 553 // Reset the history_handler to verify whether new icon is requested from |
| 552 // history. | 554 // history. |
| 553 helper.set_history_handler(NULL); | 555 helper.set_history_handler(NULL); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 566 ASSERT_EQ(new_icon_url, helper.GetEntry()->GetFavicon().url); | 568 ASSERT_EQ(new_icon_url, helper.GetEntry()->GetFavicon().url); |
| 567 | 569 |
| 568 // Favicon should be requested from history. | 570 // Favicon should be requested from history. |
| 569 history_handler = helper.history_handler(); | 571 history_handler = helper.history_handler(); |
| 570 ASSERT_TRUE(history_handler); | 572 ASSERT_TRUE(history_handler); |
| 571 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | 573 EXPECT_EQ(new_icon_url, history_handler->icon_url_); |
| 572 EXPECT_EQ(FaviconURL::FAVICON, history_handler->icon_type_); | 574 EXPECT_EQ(FaviconURL::FAVICON, history_handler->icon_type_); |
| 573 EXPECT_EQ(page_url, history_handler->page_url_); | 575 EXPECT_EQ(page_url, history_handler->page_url_); |
| 574 | 576 |
| 575 // Simulate find icon. | 577 // Simulate find icon. |
| 576 history_handler->favicon_data_.known_icon = true; | 578 SetFaviconDataWithOneBitmap(new_icon_url, &history_handler->favicon_data_); |
| 577 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 578 history_handler->favicon_data_.expired = false; | |
| 579 history_handler->favicon_data_.icon_url = new_icon_url; | |
| 580 history_handler->favicon_data_.image_data = data; | |
| 581 history_handler->InvokeCallback(); | 579 history_handler->InvokeCallback(); |
| 582 | 580 |
| 583 // Shouldn't request download favicon | 581 // Shouldn't request download favicon |
| 584 EXPECT_FALSE(helper.download_handler()->HasDownload()); | 582 EXPECT_FALSE(helper.download_handler()->HasDownload()); |
| 585 | 583 |
| 586 // Verify the favicon status. | 584 // Verify the favicon status. |
| 587 EXPECT_EQ(new_icon_url, helper.GetEntry()->GetFavicon().url); | 585 EXPECT_EQ(new_icon_url, helper.GetEntry()->GetFavicon().url); |
| 588 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); | 586 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); |
| 589 EXPECT_FALSE(helper.GetEntry()->GetFavicon().image.IsEmpty()); | 587 EXPECT_FALSE(helper.GetEntry()->GetFavicon().image.IsEmpty()); |
| 590 } | 588 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 // Favicon should be requested from history. | 668 // Favicon should be requested from history. |
| 671 history_handler = helper.history_handler(); | 669 history_handler = helper.history_handler(); |
| 672 ASSERT_TRUE(history_handler); | 670 ASSERT_TRUE(history_handler); |
| 673 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | 671 EXPECT_EQ(new_icon_url, history_handler->icon_url_); |
| 674 EXPECT_EQ(FaviconURL::TOUCH_ICON, history_handler->icon_type_); | 672 EXPECT_EQ(FaviconURL::TOUCH_ICON, history_handler->icon_type_); |
| 675 EXPECT_EQ(page_url, history_handler->page_url_); | 673 EXPECT_EQ(page_url, history_handler->page_url_); |
| 676 | 674 |
| 677 // Reset download handler | 675 // Reset download handler |
| 678 download_handler->Reset(); | 676 download_handler->Reset(); |
| 679 | 677 |
| 680 // Smulates getting a expired icon from history. | 678 // Simulates getting a expired icon from history. |
| 681 history_handler->favicon_data_.known_icon = true; | 679 SetFaviconDataWithOneBitmap(new_icon_url, history::TOUCH_ICON, |
| 682 history_handler->favicon_data_.icon_type = history::TOUCH_ICON; | 680 true /* expired */, &history_handler->favicon_data_); |
| 683 history_handler->favicon_data_.expired = true; | |
| 684 history_handler->favicon_data_.icon_url = new_icon_url; | |
| 685 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
| 686 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); | |
| 687 history_handler->favicon_data_.image_data = data; | |
| 688 history_handler->InvokeCallback(); | 681 history_handler->InvokeCallback(); |
| 689 | 682 |
| 690 // Verify the download request. | 683 // Verify the download request. |
| 691 EXPECT_TRUE(helper.download_handler()->HasDownload()); | 684 EXPECT_TRUE(helper.download_handler()->HasDownload()); |
| 692 EXPECT_EQ(new_icon_url, download_handler->GetImageUrl()); | 685 EXPECT_EQ(new_icon_url, download_handler->GetImageUrl()); |
| 693 EXPECT_EQ(0, download_handler->GetImageSize()); | 686 EXPECT_EQ(0, download_handler->GetImageSize()); |
| 694 | 687 |
| 695 helper.set_history_handler(NULL); | 688 helper.set_history_handler(NULL); |
| 696 | 689 |
| 697 // Simulates icon being downloaded. | 690 // Simulates icon being downloaded. |
| 698 download_handler->InvokeCallback(); | 691 download_handler->InvokeCallback(); |
| 699 | 692 |
| 700 // New icon should be saved to history backend. | 693 // New icon should be saved to history backend. |
| 701 history_handler = helper.history_handler(); | 694 history_handler = helper.history_handler(); |
| 702 ASSERT_TRUE(history_handler); | 695 ASSERT_TRUE(history_handler); |
| 703 EXPECT_EQ(new_icon_url, history_handler->icon_url_); | 696 EXPECT_EQ(new_icon_url, history_handler->icon_url_); |
| 704 EXPECT_EQ(FaviconURL::TOUCH_ICON, history_handler->icon_type_); | 697 EXPECT_EQ(FaviconURL::TOUCH_ICON, history_handler->icon_type_); |
| 705 EXPECT_LT(0U, history_handler->image_data_.size()); | 698 EXPECT_LT(0U, history_handler->bitmap_data_.size()); |
| 706 EXPECT_EQ(page_url, history_handler->page_url_); | 699 EXPECT_EQ(page_url, history_handler->page_url_); |
| 707 } | 700 } |
| 708 | 701 |
| 709 TEST_F(FaviconHandlerTest, UpdateDuringDownloading) { | 702 TEST_F(FaviconHandlerTest, UpdateDuringDownloading) { |
| 710 const GURL page_url("http://www.google.com"); | 703 const GURL page_url("http://www.google.com"); |
| 711 const GURL icon_url("http://www.google.com/favicon"); | 704 const GURL icon_url("http://www.google.com/favicon"); |
| 712 const GURL new_icon_url("http://www.google.com/new_favicon"); | 705 const GURL new_icon_url("http://www.google.com/new_favicon"); |
| 713 | 706 |
| 714 TestFaviconHandlerDelegate delegate(contents()); | 707 TestFaviconHandlerDelegate delegate(contents()); |
| 715 Profile* profile = Profile::FromBrowserContext( | 708 Profile* profile = Profile::FromBrowserContext( |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 download_handler->InvokeCallback(); | 793 download_handler->InvokeCallback(); |
| 801 // The downloaded icon should be thrown away as there is favicon update. | 794 // The downloaded icon should be thrown away as there is favicon update. |
| 802 EXPECT_FALSE(helper.history_handler()); | 795 EXPECT_FALSE(helper.history_handler()); |
| 803 | 796 |
| 804 download_handler->Reset(); | 797 download_handler->Reset(); |
| 805 | 798 |
| 806 // Simulates getting the icon from history. | 799 // Simulates getting the icon from history. |
| 807 scoped_ptr<HistoryRequestHandler> handler; | 800 scoped_ptr<HistoryRequestHandler> handler; |
| 808 handler.reset(new HistoryRequestHandler(page_url, latest_icon_url, | 801 handler.reset(new HistoryRequestHandler(page_url, latest_icon_url, |
| 809 history::TOUCH_ICON, callback)); | 802 history::TOUCH_ICON, callback)); |
| 810 handler->favicon_data_.known_icon = true; | 803 SetFaviconDataWithOneBitmap(latest_icon_url, history::TOUCH_ICON, |
| 811 handler->favicon_data_.expired = false; | 804 false /* expired */, &handler->favicon_data_); |
| 812 handler->favicon_data_.icon_type = history::TOUCH_ICON; | |
| 813 handler->favicon_data_.icon_url = latest_icon_url; | |
| 814 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
| 815 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); | |
| 816 handler->favicon_data_.image_data = data; | |
| 817 | |
| 818 handler->InvokeCallback(); | 805 handler->InvokeCallback(); |
| 819 | 806 |
| 820 // No download request. | 807 // No download request. |
| 821 EXPECT_FALSE(download_handler->HasDownload()); | 808 EXPECT_FALSE(download_handler->HasDownload()); |
| 822 } | 809 } |
| 823 | 810 |
| 824 TEST_F(FaviconHandlerTest, MultipleFavicon) { | 811 TEST_F(FaviconHandlerTest, MultipleFavicon) { |
| 825 const GURL page_url("http://www.google.com"); | 812 const GURL page_url("http://www.google.com"); |
| 826 const GURL icon_url("http://www.google.com/favicon"); | 813 const GURL icon_url("http://www.google.com/favicon"); |
| 827 const GURL icon_url_small("http://www.google.com/favicon_small"); | 814 const GURL icon_url_small("http://www.google.com/favicon_small"); |
| 828 const GURL icon_url_large("http://www.google.com/favicon_large"); | 815 const GURL icon_url_large("http://www.google.com/favicon_large"); |
| 829 const GURL icon_url_preferred1("http://www.google.com/favicon_preferred1"); | 816 const GURL icon_url_preferred1("http://www.google.com/favicon_preferred1"); |
| 830 const GURL icon_url_preferred2("http://www.google.com/favicon_preferred2"); | 817 const GURL icon_url_preferred2("http://www.google.com/favicon_preferred2"); |
| 831 | 818 |
| 832 TestFaviconHandlerDelegate delegate(contents()); | 819 TestFaviconHandlerDelegate delegate(contents()); |
| 833 Profile* profile = Profile::FromBrowserContext( | 820 Profile* profile = Profile::FromBrowserContext( |
| 834 contents()->GetBrowserContext()); | 821 contents()->GetBrowserContext()); |
| 835 TestFaviconHandler handler(page_url, profile, | 822 TestFaviconHandler handler(page_url, profile, |
| 836 &delegate, FaviconHandler::FAVICON); | 823 &delegate, FaviconHandler::FAVICON); |
| 837 | 824 |
| 838 handler.FetchFavicon(page_url); | 825 handler.FetchFavicon(page_url); |
| 839 HistoryRequestHandler* history_handler = handler.history_handler(); | 826 HistoryRequestHandler* history_handler = handler.history_handler(); |
| 840 | 827 |
| 841 // Set valid icon data. | 828 SetFaviconDataWithOneBitmap(icon_url, &history_handler->favicon_data_); |
| 842 history_handler->favicon_data_.known_icon = true; | |
| 843 history_handler->favicon_data_.icon_type = history::FAVICON; | |
| 844 history_handler->favicon_data_.expired = false; | |
| 845 history_handler->favicon_data_.icon_url = icon_url; | |
| 846 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
| 847 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); | |
| 848 history_handler->favicon_data_.image_data = data; | |
| 849 | 829 |
| 850 // Send history response. | 830 // Send history response. |
| 851 history_handler->InvokeCallback(); | 831 history_handler->InvokeCallback(); |
| 852 | 832 |
| 853 // Simulates update with the different favicon url. | 833 // Simulates update with the different favicon url. |
| 854 std::vector<FaviconURL> urls; | 834 std::vector<FaviconURL> urls; |
| 855 // Note: the code will stop making requests when an icon matching the | 835 // Note: the code will stop making requests when an icon matching the |
| 856 // preferred size is found, so icon_url_preferred must be last. | 836 // preferred size is found, so icon_url_preferred must be last. |
| 857 urls.push_back(FaviconURL(icon_url_small, FaviconURL::FAVICON)); | 837 urls.push_back(FaviconURL(icon_url_small, FaviconURL::FAVICON)); |
| 858 urls.push_back(FaviconURL(icon_url_large, FaviconURL::FAVICON)); | 838 urls.push_back(FaviconURL(icon_url_large, FaviconURL::FAVICON)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 TestFaviconHandlerDelegate delegate(contents()); | 888 TestFaviconHandlerDelegate delegate(contents()); |
| 909 Profile* profile = Profile::FromBrowserContext( | 889 Profile* profile = Profile::FromBrowserContext( |
| 910 contents()->GetBrowserContext()); | 890 contents()->GetBrowserContext()); |
| 911 TestFaviconHandler handler(page_url, profile, | 891 TestFaviconHandler handler(page_url, profile, |
| 912 &delegate, FaviconHandler::FAVICON); | 892 &delegate, FaviconHandler::FAVICON); |
| 913 | 893 |
| 914 handler.FetchFavicon(page_url); | 894 handler.FetchFavicon(page_url); |
| 915 HistoryRequestHandler* history_handler = handler.history_handler(); | 895 HistoryRequestHandler* history_handler = handler.history_handler(); |
| 916 | 896 |
| 917 // Set valid icon data. | 897 // Set valid icon data. |
| 918 history_handler->favicon_data_.known_icon = true; | 898 SetFaviconDataWithOneBitmap(icon_url, &history_handler->favicon_data_); |
| 919 history_handler->favicon_data_.icon_type = history::FAVICON; | 899 history_handler->favicon_data_.known_icon = false; |
| 920 history_handler->favicon_data_.expired = false; | |
| 921 history_handler->favicon_data_.icon_url = icon_url; | |
| 922 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
| 923 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); | |
| 924 history_handler->favicon_data_.image_data = data; | |
| 925 | 900 |
| 926 // Send history response. | 901 // Send history response. |
| 927 history_handler->InvokeCallback(); | 902 history_handler->InvokeCallback(); |
| 928 | 903 |
| 929 // Simulates update with the different favicon url. | 904 // Simulates update with the different favicon url. |
| 930 std::vector<FaviconURL> urls; | 905 std::vector<FaviconURL> urls; |
| 931 // Note: the code will stop making requests when an icon matching the | 906 // Note: the code will stop making requests when an icon matching the |
| 932 // preferred size is found, so icon_url_preferred must be last. | 907 // preferred size is found, so icon_url_preferred must be last. |
| 933 urls.push_back(FaviconURL(icon_url_preferred1, FaviconURL::FAVICON)); | 908 urls.push_back(FaviconURL(icon_url_preferred1, FaviconURL::FAVICON)); |
| 934 urls.push_back(FaviconURL(icon_url_large, FaviconURL::FAVICON)); | 909 urls.push_back(FaviconURL(icon_url_large, FaviconURL::FAVICON)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 949 | 924 |
| 950 // Verify correct icon size chosen. | 925 // Verify correct icon size chosen. |
| 951 EXPECT_EQ(icon_url_preferred1, handler.GetEntry()->GetFavicon().url); | 926 EXPECT_EQ(icon_url_preferred1, handler.GetEntry()->GetFavicon().url); |
| 952 EXPECT_TRUE(handler.GetEntry()->GetFavicon().valid); | 927 EXPECT_TRUE(handler.GetEntry()->GetFavicon().valid); |
| 953 EXPECT_FALSE(handler.GetEntry()->GetFavicon().image.IsEmpty()); | 928 EXPECT_FALSE(handler.GetEntry()->GetFavicon().image.IsEmpty()); |
| 954 EXPECT_EQ(gfx::kFaviconSize, | 929 EXPECT_EQ(gfx::kFaviconSize, |
| 955 handler.GetEntry()->GetFavicon().image.ToSkBitmap()->width()); | 930 handler.GetEntry()->GetFavicon().image.ToSkBitmap()->width()); |
| 956 } | 931 } |
| 957 | 932 |
| 958 } // namespace. | 933 } // namespace. |
| OLD | NEW |