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

Side by Side Diff: chrome/browser/favicon_helper_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698