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

Unified 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: fix style 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/favicon_helper_unittest.cc
diff --git a/chrome/browser/favicon_helper_unittest.cc b/chrome/browser/favicon_helper_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4b1f6400ec9c68a5a75a2a29d70aa4bdbc0e4d2b
--- /dev/null
+++ b/chrome/browser/favicon_helper_unittest.cc
@@ -0,0 +1,703 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/favicon_helper.h"
sky 2011/03/28 15:02:58 The style of including the header first, then a ne
michaelbai 2011/03/29 00:00:50 Done.
+
+#include "content/browser/renderer_host/test_render_view_host.h"
+#include "content/browser/tab_contents/navigation_entry.h"
+#include "content/browser/tab_contents/test_tab_contents.h"
+#include "ui/gfx/codec/png_codec.h"
+#include "ui/gfx/favicon_size.h"
+
+class TestFaviconHelper;
+
+void FillDataToBitmap(int w, int h, SkBitmap* bmp) {
sky 2011/03/28 15:02:58 Static or put in a namespace and add a description
michaelbai 2011/03/29 00:00:50 Done.
+ bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
+ bmp->allocPixels();
+
+ unsigned char* src_data =
+ reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0));
+ for (int i = 0; i < w * h; i++) {
+ src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255);
+ src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255);
+ src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255);
+ src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255);
+ }
+}
+
+void FillBitmap(int w, int h, std::vector<unsigned char>* output) {
sky 2011/03/28 15:02:58 Same here.
michaelbai 2011/03/29 00:00:50 Done.
+ SkBitmap bitmap;
+ FillDataToBitmap(w, h, &bitmap);
+ gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output);
+}
+
+class DownloadHandler {
sky 2011/03/28 15:02:58 Move into anonymous namespace, and add description
michaelbai 2011/03/29 00:00:50 Done.
+ public:
+ DownloadHandler(int download_id,
+ const GURL& image_url,
+ int image_size,
+ TestFaviconHelper* favicon_helper)
+ : image_url_(image_url),
+ image_size_(image_size),
+ failed_(false),
+ download_id_(download_id),
+ favicon_helper_(favicon_helper) {
+ FillDataToBitmap(16, 16, &bitmap_);
+ }
+
+ virtual ~DownloadHandler() {
+ }
+
+ static void UpdateFaviconURL(FaviconHelper* helper,
+ const std::vector<FaviconURL> urls);
+
+ void InvokeCallback();
+
+ void UpdateFaviconURL(const std::vector<FaviconURL> urls);
+
+ const GURL image_url_;
+ const int image_size_;
+ bool failed_;
+
+ private:
+ int download_id_;
sky 2011/03/28 15:02:58 Descriptions?
michaelbai 2011/03/29 00:00:50 Done.
+ TestFaviconHelper* favicon_helper_;
+ SkBitmap bitmap_;
+};
sky 2011/03/28 15:02:58 DISALLOW_COPY_AND_ASSIGN
michaelbai 2011/03/29 00:00:50 Done.
+
+class HistoryRequestHandler {
sky 2011/03/28 15:02:58 Move into anonymous namespace, and add description
michaelbai 2011/03/29 00:00:50 Done.
+ public:
+ HistoryRequestHandler(const GURL& page_url,
+ const GURL& icon_url,
+ int icon_type,
+ FaviconService::FaviconDataCallback* callback)
+ : page_url_(page_url),
+ icon_url_(icon_url),
+ icon_type_(icon_type),
+ callback_(callback) {
+ }
+
+ HistoryRequestHandler(const GURL& page_url,
+ const GURL& icon_url,
+ int icon_type,
+ const std::vector<unsigned char>& image_data,
+ FaviconService::FaviconDataCallback* callback)
+ : page_url_(page_url),
+ icon_url_(icon_url),
+ icon_type_(icon_type),
+ image_data_(image_data),
+ callback_(callback) {
+ }
+
+ void InvokeCallback();
+
+ const GURL page_url_;
+ const GURL icon_url_;
+ const int icon_type_;
+ const std::vector<unsigned char> image_data_;
+ history::FaviconData favicon_data_;
+
+ private:
+ FaviconService::FaviconDataCallback* callback_;
+};
sky 2011/03/28 15:02:58 DISALLOW_COPY_AND_ASSIGN
michaelbai 2011/03/29 00:00:50 Done.
+
+class TestFaviconHelper : public FaviconHelper {
+ public:
+ TestFaviconHelper(const GURL& page_url,
+ TabContents* tab_contents,
+ Types type)
+ : FaviconHelper(tab_contents, type),
+ download_id_(0),
+ tab_contents_(tab_contents){
+ entry_.set_url(page_url);
sky 2011/03/28 15:02:58 Initialize download_image_size_.
michaelbai 2011/03/29 00:00:50 Done.
+ }
+
+ virtual ~TestFaviconHelper() {
+ }
+
+ HistoryRequestHandler* history_handler() {
+ return history_handler_.get();
+ }
+
+ void set_history_handler(HistoryRequestHandler* handler) {
sky 2011/03/28 15:02:58 Method ownership of these setters.
michaelbai 2011/03/29 00:00:50 Done.
+ history_handler_.reset(handler);
+ }
+
+ DownloadHandler* download_handler() {
+ return download_handler_.get();
+ }
+
+ void set_download_handler(DownloadHandler* download_handler) {
+ download_handler_.reset(download_handler);
+ }
+
+ virtual NavigationEntry* GetEntry() {
+ return &entry_;
+ }
+
+ protected:
+ virtual void UpdateFaviconMappingAndFetch(
sky 2011/03/28 15:02:58 Add OVERRIDE on all these methods.
michaelbai 2011/03/29 00:00:50 Done.
+ const GURL& page_url,
+ const GURL& icon_url,
+ history::IconType icon_type,
+ CancelableRequestConsumerBase* consumer,
+ FaviconService::FaviconDataCallback* callback) {
+ history_handler_.reset(new HistoryRequestHandler(page_url, icon_url,
+ icon_type, callback));
+ }
+
+ virtual void GetFavicon(
+ const GURL& icon_url,
+ history::IconType icon_type,
+ CancelableRequestConsumerBase* consumer,
+ FaviconService::FaviconDataCallback* callback) {
+ history_handler_.reset(new HistoryRequestHandler(GURL(), icon_url,
+ icon_type, callback));
+ }
+
+ virtual void GetFaviconForURL(
+ const GURL& page_url,
+ int icon_types,
+ CancelableRequestConsumerBase* consumer,
+ FaviconService::FaviconDataCallback* callback) {
+ history_handler_.reset(new HistoryRequestHandler(page_url, GURL(),
+ icon_types, callback));
+ }
+
+ virtual int DownloadFavicon(const GURL& image_url, int image_size) {
+ download_id_++;
+ download_handler_.reset(new DownloadHandler(download_id_, image_url,
+ image_size, this));
+ return download_id_;
+ }
+
+ virtual void SetHistoryFavicon(const GURL& page_url,
+ const GURL& icon_url,
+ const std::vector<unsigned char>& image_data,
+ history::IconType icon_type) {
+ history_handler_.reset(new HistoryRequestHandler(page_url, icon_url,
+ icon_type, image_data, NULL));
+ }
+
+ virtual FaviconService* GetFaviconService() {
+ // Just give none NULL value, so overridden methods can be hit.
+ return (FaviconService*)(1);
+ }
+
+ virtual bool ShouldSaveFavicon(const GURL& url) {
+ return true;
+ }
+
+ GURL page_url_;
+
+ GURL download_image_url_;
+ int download_image_size_;
+
+ private:
+ NavigationEntry entry_;
sky 2011/03/28 15:02:58 Descriptions?
michaelbai 2011/03/29 00:00:50 Done.
+ int download_id_;
+ TabContents* tab_contents_;
+ scoped_ptr<DownloadHandler> download_handler_;
+ scoped_ptr<HistoryRequestHandler> history_handler_;
+};
sky 2011/03/28 15:02:58 DISALLOW_COPY_AND_ASSIGN
michaelbai 2011/03/29 00:00:50 Done.
+
+void DownloadHandler::UpdateFaviconURL(FaviconHelper* helper,
+ const std::vector<FaviconURL> urls) {
+ helper->OnUpdateFaviconURL(0, urls);
+}
+
+void DownloadHandler::UpdateFaviconURL(const std::vector<FaviconURL> urls) {
+ UpdateFaviconURL(favicon_helper_, urls);
+}
+
+void DownloadHandler::InvokeCallback() {
+ favicon_helper_->OnDidDownloadFavicon(download_id_, image_url_, failed_,
+ bitmap_);
+}
+
+void HistoryRequestHandler::InvokeCallback() {
+ callback_->Run(0, favicon_data_);
+}
+
+class FaviconHelperTest : public RenderViewHostTestHarness {
+};
+
+TEST_F(FaviconHelperTest, GetFaviconFromHistory) {
+ const GURL page_url("http://www.google.com");
+ const GURL icon_url("http://www.google.com/favicon");
+
+ TestFaviconHelper helper(page_url, contents(), FaviconHelper::FAVICON);
+
+ helper.FetchFavicon(page_url);
+ HistoryRequestHandler* history_handler = helper.history_handler();
+ // Ensure the data given to history is correct.
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+ EXPECT_EQ(GURL(), history_handler->icon_url_);
+ EXPECT_EQ(history::FAVICON, history_handler->icon_type_);
+
+ // Set valid icon data.
+ history_handler->favicon_data_.known_icon = true;
+ history_handler->favicon_data_.icon_type = history::FAVICON;
+ history_handler->favicon_data_.expired = false;
+ history_handler->favicon_data_.icon_url = icon_url;
+ scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
+ FillBitmap(kFaviconSize, kFaviconSize, &data->data);
+ history_handler->favicon_data_.image_data = data;
+
+ // Send history response.
+ history_handler->InvokeCallback();
+ // Verify FaviconHelper status
+ EXPECT_TRUE(helper.GetEntry()->favicon().is_valid());
+ EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url());
+
+ // Simulates update favicon url.
+ std::vector<FaviconURL> urls;
+ urls.push_back(FaviconURL(icon_url, FAVICON));
+ DownloadHandler::UpdateFaviconURL(&helper, urls);
+
+ // Verify FaviconHelper status
+ EXPECT_EQ(1U, helper.urls_.size());
+ ASSERT_TRUE(helper.current_candidate());
+ ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
+ ASSERT_EQ(FAVICON, helper.current_candidate()->icon_type);
+
+ // Favicon shouldn't request to download icon.
+ DownloadHandler* download_handler = helper.download_handler();
+ ASSERT_FALSE(download_handler);
+}
+
+TEST_F(FaviconHelperTest, DownloadFavicon) {
+ const GURL page_url("http://www.google.com");
+ const GURL icon_url("http://www.google.com/favicon");
+
+ TestFaviconHelper helper(page_url, contents(), FaviconHelper::FAVICON);
+
+ helper.FetchFavicon(page_url);
+ HistoryRequestHandler* history_handler = helper.history_handler();
+ // Ensure the data given to history is correct.
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+ EXPECT_EQ(GURL(), history_handler->icon_url_);
+ EXPECT_EQ(history::FAVICON, history_handler->icon_type_);
+
+ // Set icon data expired
+ history_handler->favicon_data_.known_icon = true;
+ history_handler->favicon_data_.icon_type = history::FAVICON;
+ history_handler->favicon_data_.expired = true;
+ history_handler->favicon_data_.icon_url = icon_url;
+ // Send history response.
+ history_handler->InvokeCallback();
+ // Verify FaviconHelper status
+ EXPECT_TRUE(helper.GetEntry()->favicon().is_valid());
+ EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url());
+
+ // Simulates update favicon url.
+ std::vector<FaviconURL> urls;
+ urls.push_back(FaviconURL(icon_url, FAVICON));
+ DownloadHandler::UpdateFaviconURL(&helper, urls);
+
+ // Verify FaviconHelper status
+ EXPECT_EQ(1U, helper.urls_.size());
+ ASSERT_TRUE(helper.current_candidate());
+ ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
+ ASSERT_EQ(FAVICON, helper.current_candidate()->icon_type);
+
+ // Favicon should request to download icon now.
+ DownloadHandler* download_handler = helper.download_handler();
+ ASSERT_TRUE(download_handler);
+ // Verify the download request.
+ EXPECT_EQ(icon_url, download_handler->image_url_);
+ EXPECT_EQ(kFaviconSize, download_handler->image_size_);
+
+ // Reset the history_handler to verify whether favicon is set.
+ helper.set_history_handler(NULL);
+
+ // Smulates download done.
+ download_handler->InvokeCallback();
+
+ // New icon should be saved to history backend and navigation entry.
+ history_handler = helper.history_handler();
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(icon_url, history_handler->icon_url_);
+ EXPECT_EQ(FAVICON, history_handler->icon_type_);
+ EXPECT_LT(0U, history_handler->image_data_.size());
+ EXPECT_EQ(page_url, history_handler->page_url_);
+
+ // Verify NavigationEntry.
+ EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url());
+ EXPECT_TRUE(helper.GetEntry()->favicon().is_valid());
+ EXPECT_FALSE(helper.GetEntry()->favicon().bitmap().empty());
+}
+
+TEST_F(FaviconHelperTest, UpdateAndDownloadFavicon) {
+ const GURL page_url("http://www.google.com");
+ const GURL icon_url("http://www.google.com/favicon");
+ const GURL new_icon_url("http://www.google.com/new_favicon");
+
+ TestFaviconHelper helper(page_url, contents(), FaviconHelper::FAVICON);
+
+ helper.FetchFavicon(page_url);
+ HistoryRequestHandler* history_handler = helper.history_handler();
+ // Ensure the data given to history is correct.
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+ EXPECT_EQ(GURL(), history_handler->icon_url_);
+ EXPECT_EQ(history::FAVICON, history_handler->icon_type_);
+
+ // Set valid icon data.
+ history_handler->favicon_data_.known_icon = true;
+ history_handler->favicon_data_.icon_type = history::FAVICON;
+ history_handler->favicon_data_.expired = false;
+ history_handler->favicon_data_.icon_url = icon_url;
+ scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
+ FillBitmap(kFaviconSize, kFaviconSize, &data->data);
+ history_handler->favicon_data_.image_data = data;
+
+ // Send history response.
+ history_handler->InvokeCallback();
+ // Verify FaviconHelper status.
+ EXPECT_TRUE(helper.GetEntry()->favicon().is_valid());
+ EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url());
+
+ // Reset the history_handler to verify whether new icon is requested from
+ // history.
+ helper.set_history_handler(NULL);
+
+ // Simulates update with the different favicon url.
+ std::vector<FaviconURL> urls;
+ urls.push_back(FaviconURL(new_icon_url, FAVICON));
+ DownloadHandler::UpdateFaviconURL(&helper, urls);
+
+ // Verify FaviconHelper status.
+ EXPECT_EQ(1U, helper.urls_.size());
+ ASSERT_TRUE(helper.current_candidate());
+ ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url);
+ ASSERT_EQ(FAVICON, helper.current_candidate()->icon_type);
+ // The favicon status's url should be updated.
+ ASSERT_EQ(new_icon_url, helper.GetEntry()->favicon().url());
+
+ // Favicon should be requested from history.
+ history_handler = helper.history_handler();
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(new_icon_url, history_handler->icon_url_);
+ EXPECT_EQ(FAVICON, history_handler->icon_type_);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+
+ // Simulate not find icon.
+ history_handler->favicon_data_.known_icon = false;
+ history_handler->InvokeCallback();
+
+ // Favicon should request to download icon now.
+ DownloadHandler* download_handler = helper.download_handler();
+ ASSERT_TRUE(download_handler);
+ // Verify the download request.
+ EXPECT_EQ(new_icon_url, download_handler->image_url_);
+ EXPECT_EQ(kFaviconSize, download_handler->image_size_);
+
+ // Reset the history_handler to verify whether favicon is set.
+ helper.set_history_handler(NULL);
+
+ // Smulates download done.
+ download_handler->InvokeCallback();
+
+ // New icon should be saved to history backend and navigation entry.
+ history_handler = helper.history_handler();
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(new_icon_url, history_handler->icon_url_);
+ EXPECT_EQ(FAVICON, history_handler->icon_type_);
+ EXPECT_LT(0U, history_handler->image_data_.size());
+ EXPECT_EQ(page_url, history_handler->page_url_);
+
+ // Verify NavigationEntry.
+ EXPECT_EQ(new_icon_url, helper.GetEntry()->favicon().url());
+ EXPECT_TRUE(helper.GetEntry()->favicon().is_valid());
+ EXPECT_FALSE(helper.GetEntry()->favicon().bitmap().empty());
+}
+
+TEST_F(FaviconHelperTest, UpdateFavicon) {
+ const GURL page_url("http://www.google.com");
+ const GURL icon_url("http://www.google.com/favicon");
+ const GURL new_icon_url("http://www.google.com/new_favicon");
+
+ TestFaviconHelper helper(page_url, contents(), FaviconHelper::FAVICON);
+
+ helper.FetchFavicon(page_url);
+ HistoryRequestHandler* history_handler = helper.history_handler();
+ // Ensure the data given to history is correct.
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+ EXPECT_EQ(GURL(), history_handler->icon_url_);
+ EXPECT_EQ(history::FAVICON, history_handler->icon_type_);
+
+ // Set valid icon data.
+ history_handler->favicon_data_.known_icon = true;
+ history_handler->favicon_data_.icon_type = history::FAVICON;
+ history_handler->favicon_data_.expired = false;
+ history_handler->favicon_data_.icon_url = icon_url;
+ scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
+ FillBitmap(kFaviconSize, kFaviconSize, &data->data);
+ history_handler->favicon_data_.image_data = data;
+
+ // Send history response.
+ history_handler->InvokeCallback();
+ // Verify FaviconHelper status.
+ EXPECT_TRUE(helper.GetEntry()->favicon().is_valid());
+ EXPECT_EQ(icon_url, helper.GetEntry()->favicon().url());
+
+ // Reset the history_handler to verify whether new icon is requested from
+ // history.
+ helper.set_history_handler(NULL);
+
+ // Simulates update with the different favicon url.
+ std::vector<FaviconURL> urls;
+ urls.push_back(FaviconURL(new_icon_url, FAVICON));
+ DownloadHandler::UpdateFaviconURL(&helper, urls);
+
+ // Verify FaviconHelper status.
+ EXPECT_EQ(1U, helper.urls_.size());
+ ASSERT_TRUE(helper.current_candidate());
+ ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url);
+ ASSERT_EQ(FAVICON, helper.current_candidate()->icon_type);
+ // The favicon status's url should be updated.
+ ASSERT_EQ(new_icon_url, helper.GetEntry()->favicon().url());
+
+ // Favicon should be requested from history.
+ history_handler = helper.history_handler();
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(new_icon_url, history_handler->icon_url_);
+ EXPECT_EQ(FAVICON, history_handler->icon_type_);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+
+ // Simulate find icon.
+ history_handler->favicon_data_.known_icon = true;
+ history_handler->favicon_data_.icon_type = history::FAVICON;
+ history_handler->favicon_data_.expired = false;
+ history_handler->favicon_data_.icon_url = new_icon_url;
+ history_handler->favicon_data_.image_data = data;
+ history_handler->InvokeCallback();
+
+ // Shouldn't request download favicon
+ EXPECT_FALSE(helper.download_handler());
+
+ // Verify the favicon status.
+ EXPECT_EQ(new_icon_url, helper.GetEntry()->favicon().url());
+ EXPECT_TRUE(helper.GetEntry()->favicon().is_valid());
+ EXPECT_FALSE(helper.GetEntry()->favicon().bitmap().empty());
+}
+
+TEST_F(FaviconHelperTest, Download2ndFaviconURLCandidate) {
+ const GURL page_url("http://www.google.com");
+ const GURL icon_url("http://www.google.com/favicon");
+ const GURL new_icon_url("http://www.google.com/new_favicon");
+
+ TestFaviconHelper helper(page_url, contents(), FaviconHelper::TOUCH);
+
+ helper.FetchFavicon(page_url);
+ HistoryRequestHandler* history_handler = helper.history_handler();
+ // Ensure the data given to history is correct.
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+ EXPECT_EQ(GURL(), history_handler->icon_url_);
+ EXPECT_EQ(history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON,
+ history_handler->icon_type_);
+
+ // Icon not found.
+ history_handler->favicon_data_.known_icon = false;
+ // Send history response.
+ history_handler->InvokeCallback();
+ // Verify FaviconHelper status.
+ EXPECT_FALSE(helper.GetEntry()->favicon().is_valid());
+ EXPECT_EQ(GURL(), helper.GetEntry()->favicon().url());
+
+ // Reset the history_handler to verify whether new icon is requested from
+ // history.
+ helper.set_history_handler(NULL);
+
+ // Simulates update with the different favicon url.
+ std::vector<FaviconURL> urls;
+ urls.push_back(FaviconURL(icon_url, TOUCH_PRECOMPOSED_ICON));
+ urls.push_back(FaviconURL(new_icon_url, TOUCH_ICON));
+ urls.push_back(FaviconURL(new_icon_url, FAVICON));
+
+ DownloadHandler::UpdateFaviconURL(&helper, urls);
+
+ // Verify FaviconHelper status.
+ EXPECT_EQ(2U, helper.urls_.size());
+ ASSERT_TRUE(helper.current_candidate());
+ ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
+ ASSERT_EQ(TOUCH_PRECOMPOSED_ICON, helper.current_candidate()->icon_type);
+
+ // Favicon should be requested from history.
+ history_handler = helper.history_handler();
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(icon_url, history_handler->icon_url_);
+ EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+
+ // Simulate not find icon.
+ history_handler->favicon_data_.known_icon = false;
+ history_handler->InvokeCallback();
+
+ // Should request download favicon.
+ DownloadHandler* download_handler = helper.download_handler();
+ EXPECT_TRUE(download_handler);
+ // Verify the download request.
+ EXPECT_EQ(icon_url, download_handler->image_url_);
+ EXPECT_EQ(0, download_handler->image_size_);
+
+ // Reset the history_handler to verify whether favicon is request from
+ // history.
+ helper.set_history_handler(NULL);
+ // Smulates download failed.
+ download_handler->failed_ = true;
+ download_handler->InvokeCallback();
+
+ // Left 1 url.
+ EXPECT_EQ(1U, helper.urls_.size());
+ ASSERT_TRUE(helper.current_candidate());
+ EXPECT_EQ(new_icon_url, helper.current_candidate()->icon_url);
+ EXPECT_EQ(TOUCH_ICON, helper.current_candidate()->icon_type);
+
+ // Favicon should be requested from history.
+ history_handler = helper.history_handler();
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(new_icon_url, history_handler->icon_url_);
+ EXPECT_EQ(TOUCH_ICON, history_handler->icon_type_);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+
+ // Reset download handler
+ helper.set_download_handler(NULL);
+
+ // Smulates getting a expired icon from history.
+ history_handler->favicon_data_.known_icon = true;
+ history_handler->favicon_data_.icon_type = history::FAVICON;
+ history_handler->favicon_data_.expired = true;
+ history_handler->favicon_data_.icon_url = new_icon_url;
+ scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
+ FillBitmap(kFaviconSize, kFaviconSize, &data->data);
+ history_handler->favicon_data_.image_data = data;
+ history_handler->InvokeCallback();
+
+ // Verify the download request.
+ download_handler = helper.download_handler();
+ EXPECT_TRUE(download_handler);
+ EXPECT_EQ(new_icon_url, download_handler->image_url_);
+ EXPECT_EQ(0, download_handler->image_size_);
+
+ helper.set_history_handler(NULL);
+
+ // Simulates icon being downloaded.
+ download_handler->InvokeCallback();
+
+ // New icon should be saved to history backend.
+ history_handler = helper.history_handler();
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(new_icon_url, history_handler->icon_url_);
+ EXPECT_EQ(TOUCH_ICON, history_handler->icon_type_);
+ EXPECT_LT(0U, history_handler->image_data_.size());
+ EXPECT_EQ(page_url, history_handler->page_url_);
+}
+
+TEST_F(FaviconHelperTest, UpdateDuringDownloading) {
+ const GURL page_url("http://www.google.com");
+ const GURL icon_url("http://www.google.com/favicon");
+ const GURL new_icon_url("http://www.google.com/new_favicon");
+
+ TestFaviconHelper helper(page_url, contents(), FaviconHelper::TOUCH);
+
+ helper.FetchFavicon(page_url);
+ HistoryRequestHandler* history_handler = helper.history_handler();
+ // Ensure the data given to history is correct.
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+ EXPECT_EQ(GURL(), history_handler->icon_url_);
+ EXPECT_EQ(history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON,
+ history_handler->icon_type_);
+
+ // Icon not found.
+ history_handler->favicon_data_.known_icon = false;
+ // Send history response.
+ history_handler->InvokeCallback();
+ // Verify FaviconHelper status.
+ EXPECT_FALSE(helper.GetEntry()->favicon().is_valid());
+ EXPECT_EQ(GURL(), helper.GetEntry()->favicon().url());
+
+ // Reset the history_handler to verify whether new icon is requested from
+ // history.
+ helper.set_history_handler(NULL);
+
+ // Simulates update with the different favicon url.
+ std::vector<FaviconURL> urls;
+ urls.push_back(FaviconURL(icon_url, TOUCH_PRECOMPOSED_ICON));
+ urls.push_back(FaviconURL(new_icon_url, TOUCH_ICON));
+ urls.push_back(FaviconURL(new_icon_url, FAVICON));
+
+ DownloadHandler::UpdateFaviconURL(&helper, urls);
+
+ // Verify FaviconHelper status.
+ EXPECT_EQ(2U, helper.urls_.size());
+ ASSERT_TRUE(helper.current_candidate());
+ ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
+ ASSERT_EQ(TOUCH_PRECOMPOSED_ICON, helper.current_candidate()->icon_type);
+
+ // Favicon should be requested from history.
+ history_handler = helper.history_handler();
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(icon_url, history_handler->icon_url_);
+ EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+
+ // Simulate not find icon.
+ history_handler->favicon_data_.known_icon = false;
+ history_handler->InvokeCallback();
+
+ // Should request download favicon.
+ DownloadHandler* download_handler = helper.download_handler();
+ EXPECT_TRUE(download_handler);
+ // Verify the download request.
+ EXPECT_EQ(icon_url, download_handler->image_url_);
+ EXPECT_EQ(0, download_handler->image_size_);
+
+ // Reset the history_handler to verify whether favicon is request from
+ // history.
+ helper.set_history_handler(NULL);
+ const GURL latest_icon_url("http://www.google.com/latest_favicon");
+ std::vector<FaviconURL> latest_urls;
+ latest_urls.push_back(FaviconURL(latest_icon_url, TOUCH_ICON));
+ DownloadHandler::UpdateFaviconURL(&helper, latest_urls);
+ EXPECT_EQ(1U, helper.urls_.size());
+ EXPECT_EQ(latest_icon_url, helper.current_candidate()->icon_url);
+ EXPECT_EQ(TOUCH_ICON, helper.current_candidate()->icon_type);
+
+ // Whether new icon is requested from history
+ history_handler = helper.history_handler();
+ ASSERT_TRUE(history_handler);
+ EXPECT_EQ(latest_icon_url, history_handler->icon_url_);
+ EXPECT_EQ(TOUCH_ICON, history_handler->icon_type_);
+ EXPECT_EQ(page_url, history_handler->page_url_);
+
+ // Reset the history_handler to verify whether favicon is request from
+ // history.
+ helper.set_history_handler(NULL);
+
+ // Smulates download succeed.
+ download_handler->InvokeCallback();
+ // The downloaded icon should be thrown away as there is faviocn update.
+ EXPECT_FALSE(helper.history_handler());
+
+ helper.set_download_handler(NULL);
+ // Smulates getting the icon from history.
+ history_handler->favicon_data_.known_icon = true;
+ history_handler->favicon_data_.icon_type = history::TOUCH_ICON;
+ history_handler->favicon_data_.expired = false;
+ history_handler->favicon_data_.icon_url = latest_icon_url;
+ scoped_refptr<RefCountedBytes> data = new RefCountedBytes();
+ FillBitmap(kFaviconSize, kFaviconSize, &data->data);
+ history_handler->favicon_data_.image_data = data;
+ history_handler->InvokeCallback();
+
+ // No download request.
+ EXPECT_FALSE(helper.download_handler());
+}

Powered by Google App Engine
This is Rietveld 408576698