Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: components/favicon/core/favicon_handler_unittest.cc

Issue 1079523005: Fix FaviconHandler when it has multiple icon URLs and one of them is invalid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix_favicon3
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/favicon/core/favicon_handler.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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<set>
8 #include<vector>
9
7 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
8 #include "components/favicon/core/favicon_driver.h" 11 #include "components/favicon/core/favicon_driver.h"
9 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkBitmap.h" 13 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "ui/base/layout.h" 14 #include "ui/base/layout.h"
12 #include "ui/gfx/codec/png_codec.h" 15 #include "ui/gfx/codec/png_codec.h"
13 #include "ui/gfx/favicon_size.h" 16 #include "ui/gfx/favicon_size.h"
14 #include "ui/gfx/image/image.h" 17 #include "ui/gfx/image/image.h"
15 18
16 namespace favicon { 19 namespace favicon {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 favicon_base::FAVICON, 65 favicon_base::FAVICON,
63 false /* expired */, 66 false /* expired */,
64 favicon_bitmap_results); 67 favicon_bitmap_results);
65 } 68 }
66 69
67 // This class is used to save the download request for verifying with test case. 70 // This class is used to save the download request for verifying with test case.
68 // It also will be used to invoke the onDidDownload callback. 71 // It also will be used to invoke the onDidDownload callback.
69 class DownloadHandler { 72 class DownloadHandler {
70 public: 73 public:
71 explicit DownloadHandler(FaviconHandler* favicon_handler) 74 explicit DownloadHandler(FaviconHandler* favicon_handler)
72 : favicon_handler_(favicon_handler), failed_(false) {} 75 : favicon_handler_(favicon_handler), callback_invoked_(false) {}
73 76
74 ~DownloadHandler() {} 77 ~DownloadHandler() {}
75 78
76 void Reset() { 79 void Reset() {
77 download_.reset(); 80 download_.reset();
78 failed_ = false; 81 callback_invoked_ = false;
82 // Does not affect |should_fail_download_icon_urls_| and
83 // |failed_download_icon_urls_|.
84 }
85
86 // Make downloads for any of |icon_urls| fail.
87 void FailDownloadForIconURLs(const std::set<GURL>& icon_urls) {
88 should_fail_download_icon_urls_ = icon_urls;
89 }
90
91 // Returns whether a download for |icon_url| did fail.
92 bool DidFailDownloadForIconURL(const GURL& icon_url) const {
93 return failed_download_icon_urls_.count(icon_url);
79 } 94 }
80 95
81 void AddDownload( 96 void AddDownload(
82 int download_id, 97 int download_id,
83 const GURL& image_url, 98 const GURL& image_url,
84 const std::vector<int>& image_sizes, 99 const std::vector<int>& image_sizes,
85 int max_image_size) { 100 int max_image_size) {
86 download_.reset(new Download( 101 download_.reset(new Download(
87 download_id, image_url, image_sizes, max_image_size, false)); 102 download_id, image_url, image_sizes, max_image_size));
88 } 103 }
89 104
90 void InvokeCallback(); 105 void InvokeCallback();
91 106
92 void set_failed(bool failed) { failed_ = failed; }
93
94 bool HasDownload() const { return download_.get(); } 107 bool HasDownload() const { return download_.get(); }
95 const GURL& GetImageUrl() const { return download_->image_url; } 108 const GURL& GetImageUrl() const { return download_->image_url; }
96 void SetImageSizes(const std::vector<int>& sizes) { 109 void SetImageSizes(const std::vector<int>& sizes) {
97 download_->image_sizes = sizes; } 110 download_->image_sizes = sizes; }
98 111
99 private: 112 private:
100 struct Download { 113 struct Download {
101 Download(int id, 114 Download(int id,
102 GURL url, 115 GURL url,
103 const std::vector<int>& sizes, 116 const std::vector<int>& sizes,
104 int max_size, 117 int max_size)
105 bool failed)
106 : download_id(id), 118 : download_id(id),
107 image_url(url), 119 image_url(url),
108 image_sizes(sizes), 120 image_sizes(sizes),
109 max_image_size(max_size) {} 121 max_image_size(max_size) {}
110 ~Download() {} 122 ~Download() {}
111 int download_id; 123 int download_id;
112 GURL image_url; 124 GURL image_url;
113 std::vector<int> image_sizes; 125 std::vector<int> image_sizes;
114 int max_image_size; 126 int max_image_size;
115 }; 127 };
116 128
117 FaviconHandler* favicon_handler_; 129 FaviconHandler* favicon_handler_;
118 scoped_ptr<Download> download_; 130 scoped_ptr<Download> download_;
119 bool failed_; 131 bool callback_invoked_;
132
133 // The icon URLs for which the download should fail.
134 std::set<GURL> should_fail_download_icon_urls_;
135
136 // The icon URLs for which the download did fail. This should be a subset of
137 // |should_fail_download_icon_urls_|.
138 std::set<GURL> failed_download_icon_urls_;
120 139
121 DISALLOW_COPY_AND_ASSIGN(DownloadHandler); 140 DISALLOW_COPY_AND_ASSIGN(DownloadHandler);
122 }; 141 };
123 142
124 // This class is used to save the history request for verifying with test case. 143 // This class is used to save the history request for verifying with test case.
125 // It also will be used to simulate the history response. 144 // It also will be used to simulate the history response.
126 class HistoryRequestHandler { 145 class HistoryRequestHandler {
127 public: 146 public:
128 HistoryRequestHandler(const GURL& page_url, 147 HistoryRequestHandler(const GURL& page_url,
129 const GURL& icon_url, 148 const GURL& icon_url,
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 void GetFaviconForURLFromFaviconService( 371 void GetFaviconForURLFromFaviconService(
353 const GURL& page_url, 372 const GURL& page_url,
354 int icon_types, 373 int icon_types,
355 const favicon_base::FaviconResultsCallback& callback, 374 const favicon_base::FaviconResultsCallback& callback,
356 base::CancelableTaskTracker* tracker) override { 375 base::CancelableTaskTracker* tracker) override {
357 history_handler_.reset(new HistoryRequestHandler(page_url, GURL(), 376 history_handler_.reset(new HistoryRequestHandler(page_url, GURL(),
358 icon_types, callback)); 377 icon_types, callback));
359 } 378 }
360 379
361 int DownloadFavicon(const GURL& image_url, int max_bitmap_size) override { 380 int DownloadFavicon(const GURL& image_url, int max_bitmap_size) override {
381 // Do not do a download if downloading |image_url| failed previously. This
382 // emulates the behavior of FaviconDriver::StartDownload()
383 if (download_handler_->DidFailDownloadForIconURL(image_url)) {
384 download_handler_->AddDownload(download_id_, image_url,
385 std::vector<int>(), 0);
386 return 0;
387 }
388
362 download_id_++; 389 download_id_++;
363 std::vector<int> sizes; 390 std::vector<int> sizes;
364 sizes.push_back(0); 391 sizes.push_back(0);
365 download_handler_->AddDownload( 392 download_handler_->AddDownload(
366 download_id_, image_url, sizes, max_bitmap_size); 393 download_id_, image_url, sizes, max_bitmap_size);
367 return download_id_; 394 return download_id_;
368 } 395 }
369 396
370 void SetHistoryFavicons(const GURL& page_url, 397 void SetHistoryFavicons(const GURL& page_url,
371 const GURL& icon_url, 398 const GURL& icon_url,
(...skipping 24 matching lines...) Expand all
396 423
397 namespace { 424 namespace {
398 425
399 void HistoryRequestHandler::InvokeCallback() { 426 void HistoryRequestHandler::InvokeCallback() {
400 if (!callback_.is_null()) { 427 if (!callback_.is_null()) {
401 callback_.Run(history_results_); 428 callback_.Run(history_results_);
402 } 429 }
403 } 430 }
404 431
405 void DownloadHandler::InvokeCallback() { 432 void DownloadHandler::InvokeCallback() {
433 if (callback_invoked_)
434 return;
435
406 std::vector<gfx::Size> original_bitmap_sizes; 436 std::vector<gfx::Size> original_bitmap_sizes;
407 std::vector<SkBitmap> bitmaps; 437 std::vector<SkBitmap> bitmaps;
408 if (!failed_) { 438 if (should_fail_download_icon_urls_.count(download_->image_url)) {
439 failed_download_icon_urls_.insert(download_->image_url);
440 } else {
409 for (std::vector<int>::const_iterator i = download_->image_sizes.begin(); 441 for (std::vector<int>::const_iterator i = download_->image_sizes.begin();
410 i != download_->image_sizes.end(); ++i) { 442 i != download_->image_sizes.end(); ++i) {
411 int original_size = (*i > 0) ? *i : gfx::kFaviconSize; 443 int original_size = (*i > 0) ? *i : gfx::kFaviconSize;
412 int downloaded_size = original_size; 444 int downloaded_size = original_size;
413 if (download_->max_image_size != 0 && 445 if (download_->max_image_size != 0 &&
414 downloaded_size > download_->max_image_size) { 446 downloaded_size > download_->max_image_size) {
415 downloaded_size = download_->max_image_size; 447 downloaded_size = download_->max_image_size;
416 } 448 }
417 SkBitmap bitmap; 449 SkBitmap bitmap;
418 FillDataToBitmap(downloaded_size, downloaded_size, &bitmap); 450 FillDataToBitmap(downloaded_size, downloaded_size, &bitmap);
419 bitmaps.push_back(bitmap); 451 bitmaps.push_back(bitmap);
420 original_bitmap_sizes.push_back(gfx::Size(original_size, original_size)); 452 original_bitmap_sizes.push_back(gfx::Size(original_size, original_size));
421 } 453 }
422 } 454 }
423 favicon_handler_->OnDidDownloadFavicon(download_->download_id, 455 favicon_handler_->OnDidDownloadFavicon(download_->download_id,
424 download_->image_url, bitmaps, 456 download_->image_url, bitmaps,
425 original_bitmap_sizes); 457 original_bitmap_sizes);
458 callback_invoked_ = true;
426 } 459 }
427 460
428 class FaviconHandlerTest : public testing::Test { 461 class FaviconHandlerTest : public testing::Test {
429 public: 462 public:
430 FaviconHandlerTest() { 463 FaviconHandlerTest() {
431 } 464 }
432 465
433 ~FaviconHandlerTest() override {} 466 ~FaviconHandlerTest() override {}
434 467
435 // Simulates requesting a favicon for |page_url| given: 468 // Simulates requesting a favicon for |page_url| given:
(...skipping 17 matching lines...) Expand all
453 favicon_handler->history_handler()->history_results_.clear(); 486 favicon_handler->history_handler()->history_results_.clear();
454 favicon_handler->history_handler()->InvokeCallback(); 487 favicon_handler->history_handler()->InvokeCallback();
455 ASSERT_TRUE(download_handler->HasDownload()); 488 ASSERT_TRUE(download_handler->HasDownload());
456 EXPECT_EQ(download_handler->GetImageUrl(), 489 EXPECT_EQ(download_handler->GetImageUrl(),
457 candidate_icons[i].icon_url); 490 candidate_icons[i].icon_url);
458 std::vector<int> sizes; 491 std::vector<int> sizes;
459 sizes.push_back(candidate_icon_sizes[i]); 492 sizes.push_back(candidate_icon_sizes[i]);
460 download_handler->SetImageSizes(sizes); 493 download_handler->SetImageSizes(sizes);
461 download_handler->InvokeCallback(); 494 download_handler->InvokeCallback();
462 495
496 download_handler->Reset();
497
463 if (favicon_driver->num_active_favicon()) 498 if (favicon_driver->num_active_favicon())
464 return; 499 return;
465 } 500 }
466 } 501 }
467 502
468 void UpdateFaviconURL(TestFaviconDriver* favicon_driver, 503 void UpdateFaviconURL(TestFaviconDriver* favicon_driver,
469 TestFaviconHandler* favicon_handler, 504 TestFaviconHandler* favicon_handler,
470 const GURL& page_url, 505 const GURL& page_url,
471 const std::vector<FaviconURL>& candidate_icons) { 506 const std::vector<FaviconURL>& candidate_icons) {
472 favicon_driver->ResetNumActiveFavicon(); 507 favicon_driver->ResetNumActiveFavicon();
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty()); 845 EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
811 } 846 }
812 847
813 TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) { 848 TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
814 const GURL page_url("http://www.google.com"); 849 const GURL page_url("http://www.google.com");
815 const GURL icon_url("http://www.google.com/favicon"); 850 const GURL icon_url("http://www.google.com/favicon");
816 const GURL new_icon_url("http://www.google.com/new_favicon"); 851 const GURL new_icon_url("http://www.google.com/new_favicon");
817 852
818 TestFaviconDriver driver; 853 TestFaviconDriver driver;
819 TestFaviconHandler helper(page_url, &driver, FaviconHandler::TOUCH, false); 854 TestFaviconHandler helper(page_url, &driver, FaviconHandler::TOUCH, false);
855 std::set<GURL> fail_downloads;
856 fail_downloads.insert(icon_url);
857 helper.download_handler()->FailDownloadForIconURLs(fail_downloads);
820 858
821 helper.FetchFavicon(page_url); 859 helper.FetchFavicon(page_url);
822 HistoryRequestHandler* history_handler = helper.history_handler(); 860 HistoryRequestHandler* history_handler = helper.history_handler();
823 // Ensure the data given to history is correct. 861 // Ensure the data given to history is correct.
824 ASSERT_TRUE(history_handler); 862 ASSERT_TRUE(history_handler);
825 EXPECT_EQ(page_url, history_handler->page_url_); 863 EXPECT_EQ(page_url, history_handler->page_url_);
826 EXPECT_EQ(GURL(), history_handler->icon_url_); 864 EXPECT_EQ(GURL(), history_handler->icon_url_);
827 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON, 865 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON,
828 history_handler->icon_type_); 866 history_handler->icon_type_);
829 867
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 // Should request download favicon. 909 // Should request download favicon.
872 DownloadHandler* download_handler = helper.download_handler(); 910 DownloadHandler* download_handler = helper.download_handler();
873 EXPECT_TRUE(helper.download_handler()->HasDownload()); 911 EXPECT_TRUE(helper.download_handler()->HasDownload());
874 912
875 // Verify the download request. 913 // Verify the download request.
876 EXPECT_EQ(icon_url, download_handler->GetImageUrl()); 914 EXPECT_EQ(icon_url, download_handler->GetImageUrl());
877 915
878 // Reset the history_handler to verify whether favicon is request from 916 // Reset the history_handler to verify whether favicon is request from
879 // history. 917 // history.
880 helper.set_history_handler(nullptr); 918 helper.set_history_handler(nullptr);
881 // Smulates download failed.
882 download_handler->set_failed(true);
883 download_handler->InvokeCallback(); 919 download_handler->InvokeCallback();
884 920
885 // Left 1 url. 921 // Left 1 url.
886 EXPECT_EQ(1U, helper.urls().size()); 922 EXPECT_EQ(1U, helper.urls().size());
887 ASSERT_TRUE(helper.current_candidate()); 923 ASSERT_TRUE(helper.current_candidate());
888 EXPECT_EQ(new_icon_url, helper.current_candidate()->icon_url); 924 EXPECT_EQ(new_icon_url, helper.current_candidate()->icon_url);
889 EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type); 925 EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type);
890 926
891 // Favicon should be requested from history. 927 // Favicon should be requested from history.
892 history_handler = helper.history_handler(); 928 history_handler = helper.history_handler();
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 SetFaviconRawBitmapResult(latest_icon_url, 1064 SetFaviconRawBitmapResult(latest_icon_url,
1029 favicon_base::TOUCH_ICON, 1065 favicon_base::TOUCH_ICON,
1030 false /* expired */, 1066 false /* expired */,
1031 &handler->history_results_); 1067 &handler->history_results_);
1032 handler->InvokeCallback(); 1068 handler->InvokeCallback();
1033 1069
1034 // No download request. 1070 // No download request.
1035 EXPECT_FALSE(download_handler->HasDownload()); 1071 EXPECT_FALSE(download_handler->HasDownload());
1036 } 1072 }
1037 1073
1038 #if !defined(OS_ANDROID)
1039
1040 // Test the favicon which is selected when the web page provides several 1074 // Test the favicon which is selected when the web page provides several
1041 // favicons and none of the favicons are cached in history. 1075 // favicons and none of the favicons are cached in history.
1042 // The goal of this test is to be more of an integration test than 1076 // The goal of this test is to be more of an integration test than
1043 // SelectFaviconFramesTest.*. 1077 // SelectFaviconFramesTest.*.
1044 TEST_F(FaviconHandlerTest, MultipleFavicons) { 1078 TEST_F(FaviconHandlerTest, MultipleFavicons) {
1045 const GURL kPageURL("http://www.google.com"); 1079 const GURL kPageURL("http://www.google.com");
1046 const FaviconURL kSourceIconURLs[] = { 1080 const FaviconURL kSourceIconURLs[] = {
1047 FaviconURL(GURL("http://www.google.com/a"), 1081 FaviconURL(GURL("http://www.google.com/a"),
1048 favicon_base::FAVICON, 1082 favicon_base::FAVICON,
1049 std::vector<gfx::Size>()), 1083 std::vector<gfx::Size>()),
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1132 kSourceIconURLs + arraysize(kSizes4)); 1166 kSourceIconURLs + arraysize(kSizes4));
1133 DownloadTillDoneIgnoringHistory( 1167 DownloadTillDoneIgnoringHistory(
1134 &driver4, &handler4, kPageURL, urls4, kSizes4); 1168 &driver4, &handler4, kPageURL, urls4, kSizes4);
1135 EXPECT_TRUE(driver4.GetActiveFaviconValidity()); 1169 EXPECT_TRUE(driver4.GetActiveFaviconValidity());
1136 expected_index = 0u; 1170 expected_index = 0u;
1137 EXPECT_EQ(17, kSizes4[expected_index]); 1171 EXPECT_EQ(17, kSizes4[expected_index]);
1138 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url, 1172 EXPECT_EQ(kSourceIconURLs[expected_index].icon_url,
1139 driver4.GetActiveFaviconURL()); 1173 driver4.GetActiveFaviconURL());
1140 } 1174 }
1141 1175
1142 #endif 1176 // Test that the best favicon is selected when:
1177 // - The page provides several favicons.
1178 // - Downloading one of the page's icon URLs previously returned a 404.
1179 // - None of the favicons are cached in the Favicons database.
1180 TEST_F(FaviconHandlerTest, MultipleFavicons404) {
1181 const GURL kPageURL("http://www.google.com");
1182 const GURL k404IconURL("http://www.google.com/404.png");
1183 const FaviconURL k404FaviconURL(
1184 k404IconURL, favicon_base::FAVICON, std::vector<gfx::Size>());
1185 const FaviconURL kFaviconURLs[] = {
1186 FaviconURL(GURL("http://www.google.com/a"),
1187 favicon_base::FAVICON,
1188 std::vector<gfx::Size>()),
1189 k404FaviconURL,
1190 FaviconURL(GURL("http://www.google.com/c"),
1191 favicon_base::FAVICON,
1192 std::vector<gfx::Size>()),
1193 };
1194
1195 TestFaviconDriver driver;
1196 TestFaviconHandler handler(kPageURL, &driver, FaviconHandler::FAVICON, false);
1197 DownloadHandler* download_handler = handler.download_handler();
1198
1199 std::set<GURL> k404URLs;
1200 k404URLs.insert(k404IconURL);
1201 download_handler->FailDownloadForIconURLs(k404URLs);
1202
1203 // Make the initial download for |k404IconURL| fail.
1204 const int kSizes1[] = { 0 };
1205 std::vector<FaviconURL> urls1(1u, k404FaviconURL);
1206 DownloadTillDoneIgnoringHistory(
1207 &driver, &handler, kPageURL, urls1, kSizes1);
1208 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL));
1209
1210 // Do a fetch now that the initial download for |k404IconURL| has failed. The
1211 // behavior is different because OnDidDownloadFavicon() is invoked
1212 // synchronously from DownloadFavicon().
1213 const int kSizes2[] = { 10, 0, 16 };
1214 std::vector<FaviconURL> urls2(kFaviconURLs,
1215 kFaviconURLs + arraysize(kFaviconURLs));
1216 DownloadTillDoneIgnoringHistory(
1217 &driver, &handler, kPageURL, urls2, kSizes2);
1218
1219 EXPECT_EQ(0u, handler.image_urls().size());
1220 EXPECT_TRUE(driver.GetActiveFaviconValidity());
1221 EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
1222 int expected_index = 2u;
1223 EXPECT_EQ(16, kSizes2[expected_index]);
1224 EXPECT_EQ(kFaviconURLs[expected_index].icon_url,
1225 driver.GetActiveFaviconURL());
1226 }
1227
1228 // Test that no favicon is selected when:
1229 // - The page provides several favicons.
1230 // - Downloading the page's icons has previously returned a 404.
1231 // - None of the favicons are cached in the Favicons database.
1232 TEST_F(FaviconHandlerTest, MultipleFaviconsAll404) {
1233 const GURL kPageURL("http://www.google.com");
1234 const GURL k404IconURL1("http://www.google.com/4041.png");
1235 const GURL k404IconURL2("http://www.google.com/4042.png");
1236 const FaviconURL kFaviconURLs[] = {
1237 FaviconURL(k404IconURL1,
1238 favicon_base::FAVICON,
1239 std::vector<gfx::Size>()),
1240 FaviconURL(k404IconURL2,
1241 favicon_base::FAVICON,
1242 std::vector<gfx::Size>()),
1243 };
1244
1245 TestFaviconDriver driver;
1246 TestFaviconHandler handler(kPageURL, &driver, FaviconHandler::FAVICON, false);
1247 DownloadHandler* download_handler = handler.download_handler();
1248
1249 std::set<GURL> k404URLs;
1250 k404URLs.insert(k404IconURL1);
1251 k404URLs.insert(k404IconURL2);
1252 download_handler->FailDownloadForIconURLs(k404URLs);
1253
1254 // Make the initial downloads for |kFaviconURLs| fail.
1255 for (const FaviconURL& favicon_url : kFaviconURLs) {
1256 const int kSizes[] = { 0 };
1257 std::vector<FaviconURL> urls(1u, favicon_url);
1258 DownloadTillDoneIgnoringHistory(&driver, &handler, kPageURL, urls, kSizes);
1259 }
1260 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL1));
1261 EXPECT_TRUE(download_handler->DidFailDownloadForIconURL(k404IconURL2));
1262
1263 // Do a fetch now that the initial downloads for |kFaviconURLs| have failed.
1264 // The behavior is different because OnDidDownloadFavicon() is invoked
1265 // synchronously from DownloadFavicon().
1266 const int kSizes[] = { 0, 0 };
1267 std::vector<FaviconURL> urls(kFaviconURLs,
1268 kFaviconURLs + arraysize(kFaviconURLs));
1269 DownloadTillDoneIgnoringHistory(&driver, &handler, kPageURL, urls, kSizes);
1270
1271 EXPECT_EQ(0u, handler.image_urls().size());
1272 EXPECT_FALSE(driver.GetActiveFaviconValidity());
1273 EXPECT_TRUE(driver.GetActiveFaviconImage().IsEmpty());
1274 }
1275
1276 // Test that no favicon is selected when the page's only icon uses an invalid
1277 // URL syntax.
1278 TEST_F(FaviconHandlerTest, FaviconInvalidURL) {
1279 const GURL kPageURL("http://www.google.com");
1280 const GURL kInvalidFormatURL("invalid");
1281 ASSERT_TRUE(kInvalidFormatURL.is_empty());
1282
1283 FaviconURL favicon_url(kInvalidFormatURL, favicon_base::FAVICON,
1284 std::vector<gfx::Size>());
1285
1286 TestFaviconDriver driver;
1287 TestFaviconHandler handler(kPageURL, &driver, FaviconHandler::FAVICON, false);
1288 UpdateFaviconURL(&driver, &handler, kPageURL,
1289 std::vector<FaviconURL>(1u, favicon_url));
1290 EXPECT_EQ(0u, handler.image_urls().size());
1291 }
1143 1292
1144 TEST_F(FaviconHandlerTest, TestSortFavicon) { 1293 TEST_F(FaviconHandlerTest, TestSortFavicon) {
1145 const GURL kPageURL("http://www.google.com"); 1294 const GURL kPageURL("http://www.google.com");
1146 std::vector<gfx::Size> icon1; 1295 std::vector<gfx::Size> icon1;
1147 icon1.push_back(gfx::Size(1024, 1024)); 1296 icon1.push_back(gfx::Size(1024, 1024));
1148 icon1.push_back(gfx::Size(512, 512)); 1297 icon1.push_back(gfx::Size(512, 512));
1149 1298
1150 std::vector<gfx::Size> icon2; 1299 std::vector<gfx::Size> icon2;
1151 icon2.push_back(gfx::Size(15, 15)); 1300 icon2.push_back(gfx::Size(15, 15));
1152 icon2.push_back(gfx::Size(16, 16)); 1301 icon2.push_back(gfx::Size(16, 16));
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1225 FaviconURL(GURL("http://www.google.com/d"), 1374 FaviconURL(GURL("http://www.google.com/d"),
1226 favicon_base::FAVICON, 1375 favicon_base::FAVICON,
1227 std::vector<gfx::Size>()), 1376 std::vector<gfx::Size>()),
1228 FaviconURL(GURL("http://www.google.com/e"), 1377 FaviconURL(GURL("http://www.google.com/e"),
1229 favicon_base::FAVICON, 1378 favicon_base::FAVICON,
1230 std::vector<gfx::Size>())}; 1379 std::vector<gfx::Size>())};
1231 1380
1232 TestFaviconDriver driver1; 1381 TestFaviconDriver driver1;
1233 TestFaviconHandler handler1(kPageURL, &driver1, FaviconHandler::FAVICON, 1382 TestFaviconHandler handler1(kPageURL, &driver1, FaviconHandler::FAVICON,
1234 true); 1383 true);
1384
1385 std::set<GURL> fail_icon_urls;
1386 for (size_t i = 0; i < arraysize(kSourceIconURLs); ++i) {
1387 fail_icon_urls.insert(kSourceIconURLs[i].icon_url);
1388 }
1389 handler1.download_handler()->FailDownloadForIconURLs(fail_icon_urls);
1390
1235 std::vector<FaviconURL> urls1(kSourceIconURLs, 1391 std::vector<FaviconURL> urls1(kSourceIconURLs,
1236 kSourceIconURLs + arraysize(kSourceIconURLs)); 1392 kSourceIconURLs + arraysize(kSourceIconURLs));
1237 UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1); 1393 UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
1238 1394
1239 // Simulate the download failed, to check whether the icons were requested 1395 // Simulate the download failed, to check whether the icons were requested
1240 // to download according their size. 1396 // to download according their size.
1241 struct ExpectedResult { 1397 struct ExpectedResult {
1242 // The size of image_urls_. 1398 // The size of image_urls_.
1243 size_t image_urls_size; 1399 size_t image_urls_size;
1244 // The favicon's index in kSourceIconURLs. 1400 // The favicon's index in kSourceIconURLs.
(...skipping 20 matching lines...) Expand all
1265 1421
1266 // Simulate no favicon from history. 1422 // Simulate no favicon from history.
1267 handler1.history_handler()->history_results_.clear(); 1423 handler1.history_handler()->history_results_.clear();
1268 handler1.history_handler()->InvokeCallback(); 1424 handler1.history_handler()->InvokeCallback();
1269 1425
1270 // Verify download request 1426 // Verify download request
1271 ASSERT_TRUE(handler1.download_handler()->HasDownload()); 1427 ASSERT_TRUE(handler1.download_handler()->HasDownload());
1272 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url, 1428 EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
1273 handler1.download_handler()->GetImageUrl()); 1429 handler1.download_handler()->GetImageUrl());
1274 1430
1275 // Simulate the download failed.
1276 handler1.download_handler()->set_failed(true);
1277 handler1.download_handler()->InvokeCallback(); 1431 handler1.download_handler()->InvokeCallback();
1432 handler1.download_handler()->Reset();
1278 } 1433 }
1279 } 1434 }
1280 1435
1281 TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) { 1436 TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) {
1282 const GURL kPageURL("http://www.google.com"); 1437 const GURL kPageURL("http://www.google.com");
1283 1438
1284 std::vector<gfx::Size> one_icon; 1439 std::vector<gfx::Size> one_icon;
1285 one_icon.push_back(gfx::Size(15, 15)); 1440 one_icon.push_back(gfx::Size(15, 15));
1286 1441
1287 std::vector<gfx::Size> two_icons; 1442 std::vector<gfx::Size> two_icons;
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1437 // Verify the first icon was request to download 1592 // Verify the first icon was request to download
1438 ASSERT_TRUE(handler1.download_handler()->HasDownload()); 1593 ASSERT_TRUE(handler1.download_handler()->HasDownload());
1439 EXPECT_EQ(kSourceIconURLs[0].icon_url, 1594 EXPECT_EQ(kSourceIconURLs[0].icon_url,
1440 handler1.download_handler()->GetImageUrl()); 1595 handler1.download_handler()->GetImageUrl());
1441 1596
1442 // Give the incorrect size. 1597 // Give the incorrect size.
1443 std::vector<int> sizes; 1598 std::vector<int> sizes;
1444 sizes.push_back(actual_size1); 1599 sizes.push_back(actual_size1);
1445 handler1.download_handler()->SetImageSizes(sizes); 1600 handler1.download_handler()->SetImageSizes(sizes);
1446 handler1.download_handler()->InvokeCallback(); 1601 handler1.download_handler()->InvokeCallback();
1602 handler1.download_handler()->Reset();
1447 1603
1448 // Simulate no favicon from history. 1604 // Simulate no favicon from history.
1449 handler1.history_handler()->history_results_.clear(); 1605 handler1.history_handler()->history_results_.clear();
1450 handler1.history_handler()->InvokeCallback(); 1606 handler1.history_handler()->InvokeCallback();
1451 1607
1452 // Verify the 2nd icon was request to download 1608 // Verify the 2nd icon was request to download
1453 ASSERT_TRUE(handler1.download_handler()->HasDownload()); 1609 ASSERT_TRUE(handler1.download_handler()->HasDownload());
1454 EXPECT_EQ(kSourceIconURLs[1].icon_url, 1610 EXPECT_EQ(kSourceIconURLs[1].icon_url,
1455 handler1.download_handler()->GetImageUrl()); 1611 handler1.download_handler()->GetImageUrl());
1456 1612
1457 // Very the best candidate is icon1 1613 // Very the best candidate is icon1
1458 EXPECT_EQ(kSourceIconURLs[0].icon_url, 1614 EXPECT_EQ(kSourceIconURLs[0].icon_url,
1459 handler1.best_favicon_candidate().image_url); 1615 handler1.best_favicon_candidate().image_url);
1460 EXPECT_EQ(gfx::Size(actual_size1, actual_size1), 1616 EXPECT_EQ(gfx::Size(actual_size1, actual_size1),
1461 handler1.best_favicon_candidate().image.Size()); 1617 handler1.best_favicon_candidate().image.Size());
1462 1618
1463 // Give the incorrect size. 1619 // Give the incorrect size.
1464 sizes.clear(); 1620 sizes.clear();
1465 sizes.push_back(actual_size2); 1621 sizes.push_back(actual_size2);
1466 handler1.download_handler()->SetImageSizes(sizes); 1622 handler1.download_handler()->SetImageSizes(sizes);
1467 handler1.download_handler()->InvokeCallback(); 1623 handler1.download_handler()->InvokeCallback();
1624 handler1.download_handler()->Reset();
1468 1625
1469 // Verify icon2 has been saved into history. 1626 // Verify icon2 has been saved into history.
1470 EXPECT_EQ(kSourceIconURLs[1].icon_url, handler1.history_handler()->icon_url_); 1627 EXPECT_EQ(kSourceIconURLs[1].icon_url, handler1.history_handler()->icon_url_);
1471 EXPECT_EQ(gfx::Size(actual_size2, actual_size2), 1628 EXPECT_EQ(gfx::Size(actual_size2, actual_size2),
1472 handler1.history_handler()->size_); 1629 handler1.history_handler()->size_);
1473 } 1630 }
1474 1631
1475 class FaviconHandlerActiveFaviconValidityParamTest : 1632 class FaviconHandlerActiveFaviconValidityParamTest :
1476 public FaviconHandlerTest, 1633 public FaviconHandlerTest,
1477 public ::testing::WithParamInterface<bool> { 1634 public ::testing::WithParamInterface<bool> {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1548 EXPECT_FALSE(driver1.update_active_favicon()); 1705 EXPECT_FALSE(driver1.update_active_favicon());
1549 EXPECT_EQ(3u, driver1.num_favicon_available()); 1706 EXPECT_EQ(3u, driver1.num_favicon_available());
1550 } 1707 }
1551 1708
1552 INSTANTIATE_TEST_CASE_P(FaviconHandlerTestActiveFaviconValidityTrueOrFalse, 1709 INSTANTIATE_TEST_CASE_P(FaviconHandlerTestActiveFaviconValidityTrueOrFalse,
1553 FaviconHandlerActiveFaviconValidityParamTest, 1710 FaviconHandlerActiveFaviconValidityParamTest,
1554 ::testing::Bool()); 1711 ::testing::Bool());
1555 1712
1556 } // namespace 1713 } // namespace
1557 } // namespace favicon 1714 } // namespace favicon
OLDNEW
« no previous file with comments | « components/favicon/core/favicon_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698