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

Unified Diff: chrome/browser/favicon/favicon_handler_unittest.cc

Issue 9696057: Prioritize smaller favicons over larger ones for tabs, etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 8 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/favicon_handler_unittest.cc
diff --git a/chrome/browser/favicon/favicon_handler_unittest.cc b/chrome/browser/favicon/favicon_handler_unittest.cc
index 16952d14efa520ee0b4a20cbd7c76563e82ba37d..2b0bb5f4e8d1097da919fcdd99c072092547a656 100644
--- a/chrome/browser/favicon/favicon_handler_unittest.cc
+++ b/chrome/browser/favicon/favicon_handler_unittest.cc
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/memory/scoped_ptr.h"
#include "chrome/browser/favicon/favicon_handler.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
@@ -45,40 +46,47 @@ void FillBitmap(int w, int h, std::vector<unsigned char>* output) {
// It also will be used to invoke the onDidDownload callback.
class DownloadHandler {
public:
- DownloadHandler(int download_id,
- const GURL& image_url,
- int image_size,
- TestFaviconHandler* favicon_helper)
- : image_url_(image_url),
- image_size_(image_size),
- failed_(false),
- download_id_(download_id),
- favicon_helper_(favicon_helper) {
- FillDataToBitmap(16, 16, &bitmap_);
+ explicit DownloadHandler(TestFaviconHandler* favicon_helper)
+ : favicon_helper_(favicon_helper),
+ failed_(false) {
}
virtual ~DownloadHandler() {
}
- static void UpdateFaviconURL(FaviconHandler* helper,
- const std::vector<FaviconURL> urls);
+ void Reset() {
+ download_.reset(NULL);
+ failed_ = false;
+ }
- void InvokeCallback();
+ void AddDownload(int download_id, const GURL& image_url, int image_size) {
+ download_.reset(new Download(download_id, image_url, image_size, false));
+ }
- void UpdateFaviconURL(const std::vector<FaviconURL> urls);
+ void InvokeCallback();
- const GURL image_url_;
- const int image_size_;
+ void set_failed(bool failed) { failed_ = failed; }
- // Simulates download failed or not.
- bool failed_;
+ bool HasDownload() const { return download_.get() != NULL; }
+ const GURL& GetImageUrl() const { return download_->image_url; }
+ int GetImageSize() const { return download_->image_size; }
+ void SetImageSize(int size) { download_->image_size = size; }
private:
- // Identified the specific download, will also be passed in
- // OnDidDownloadFavicon callback.
- int download_id_;
+ struct Download {
+ Download(int id, GURL url, int size, bool failed)
+ : download_id(id),
+ image_url(url),
+ image_size(size) {}
+ ~Download() {}
+ int download_id;
+ GURL image_url;
+ int image_size;
+ };
+
TestFaviconHandler* favicon_helper_;
- SkBitmap bitmap_;
+ scoped_ptr<Download> download_;
+ bool failed_;
DISALLOW_COPY_AND_ASSIGN(DownloadHandler);
};
@@ -169,6 +177,7 @@ class TestFaviconHandler : public FaviconHandler {
entry_(NavigationEntry::Create()),
download_id_(0) {
entry_->SetURL(page_url);
+ download_handler_.reset(new DownloadHandler(this));
}
virtual ~TestFaviconHandler() {
@@ -187,17 +196,12 @@ class TestFaviconHandler : public FaviconHandler {
return download_handler_.get();
}
- // This method will take the ownership of the given download_handler.
- void set_download_handler(DownloadHandler* download_handler) {
- download_handler_.reset(download_handler);
- }
-
virtual NavigationEntry* GetEntry() {
return entry_.get();
}
- const std::vector<FaviconURL>& urls() {
- return urls_;
+ const std::deque<FaviconURL>& urls() {
+ return image_urls_;
}
void FetchFavicon(const GURL& url) {
@@ -209,13 +213,6 @@ class TestFaviconHandler : public FaviconHandler {
return FaviconHandler::current_candidate();
}
- void OnDidDownloadFavicon(int id,
- const GURL& image_url,
- bool errored,
- gfx::Image& image) {
- FaviconHandler::OnDidDownloadFavicon(id, image_url, errored, image);
- }
-
protected:
virtual void UpdateFaviconMappingAndFetch(
const GURL& page_url,
@@ -247,8 +244,7 @@ class TestFaviconHandler : public FaviconHandler {
virtual int DownloadFavicon(const GURL& image_url, int image_size) OVERRIDE {
download_id_++;
- download_handler_.reset(new DownloadHandler(download_id_, image_url,
- image_size, this));
+ download_handler_->AddDownload(download_id_, image_url, image_size);
return download_id_;
}
@@ -290,23 +286,19 @@ class TestFaviconHandler : public FaviconHandler {
namespace {
-void DownloadHandler::UpdateFaviconURL(FaviconHandler* helper,
- const std::vector<FaviconURL> urls) {
- helper->OnUpdateFaviconURL(0, urls);
-}
-
-void DownloadHandler::UpdateFaviconURL(const std::vector<FaviconURL> urls) {
- UpdateFaviconURL(favicon_helper_, urls);
+void HistoryRequestHandler::InvokeCallback() {
+ if (!callback_.is_null())
+ callback_.Run(0, favicon_data_);
}
void DownloadHandler::InvokeCallback() {
- gfx::Image image(new SkBitmap(bitmap_));
- favicon_helper_->OnDidDownloadFavicon(download_id_, image_url_, failed_,
- image);
-}
-
-void HistoryRequestHandler::InvokeCallback() {
- callback_.Run(0, favicon_data_);
+ SkBitmap bitmap;
+ int bitmap_size = (download_->image_size > 0) ?
+ download_->image_size : gfx::kFaviconSize;
+ FillDataToBitmap(bitmap_size, bitmap_size, &bitmap);
+ gfx::Image image(bitmap);
+ favicon_helper_->OnDidDownloadFavicon(
+ download_->download_id, download_->image_url, failed_, image);
}
class FaviconHandlerTest : public ChromeRenderViewHostTestHarness {
@@ -348,7 +340,7 @@ TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
// Simulates update favicon url.
std::vector<FaviconURL> urls;
urls.push_back(FaviconURL(icon_url, FaviconURL::FAVICON));
- DownloadHandler::UpdateFaviconURL(&helper, urls);
+ helper.OnUpdateFaviconURL(0, urls);
// Verify FaviconHandler status
EXPECT_EQ(1U, helper.urls().size());
@@ -357,8 +349,7 @@ TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
ASSERT_EQ(FaviconURL::FAVICON, helper.current_candidate()->icon_type);
// Favicon shouldn't request to download icon.
- DownloadHandler* download_handler = helper.download_handler();
- ASSERT_FALSE(download_handler);
+ EXPECT_FALSE(helper.download_handler()->HasDownload());
}
TEST_F(FaviconHandlerTest, DownloadFavicon) {
@@ -393,7 +384,7 @@ TEST_F(FaviconHandlerTest, DownloadFavicon) {
// Simulates update favicon url.
std::vector<FaviconURL> urls;
urls.push_back(FaviconURL(icon_url, FaviconURL::FAVICON));
- DownloadHandler::UpdateFaviconURL(&helper, urls);
+ helper.OnUpdateFaviconURL(0, urls);
// Verify FaviconHandler status
EXPECT_EQ(1U, helper.urls().size());
@@ -403,10 +394,11 @@ TEST_F(FaviconHandlerTest, DownloadFavicon) {
// Favicon should request to download icon now.
DownloadHandler* download_handler = helper.download_handler();
- ASSERT_TRUE(download_handler);
+ EXPECT_TRUE(helper.download_handler()->HasDownload());
+
// Verify the download request.
- EXPECT_EQ(icon_url, download_handler->image_url_);
- EXPECT_EQ(gfx::kFaviconSize, download_handler->image_size_);
+ EXPECT_EQ(icon_url, download_handler->GetImageUrl());
+ EXPECT_EQ(gfx::kFaviconSize, download_handler->GetImageSize());
// Reset the history_handler to verify whether favicon is set.
helper.set_history_handler(NULL);
@@ -469,7 +461,7 @@ TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) {
// Simulates update with the different favicon url.
std::vector<FaviconURL> urls;
urls.push_back(FaviconURL(new_icon_url, FaviconURL::FAVICON));
- DownloadHandler::UpdateFaviconURL(&helper, urls);
+ helper.OnUpdateFaviconURL(0, urls);
// Verify FaviconHandler status.
EXPECT_EQ(1U, helper.urls().size());
@@ -492,10 +484,11 @@ TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) {
// Favicon should request to download icon now.
DownloadHandler* download_handler = helper.download_handler();
- ASSERT_TRUE(download_handler);
+ EXPECT_TRUE(helper.download_handler()->HasDownload());
+
// Verify the download request.
- EXPECT_EQ(new_icon_url, download_handler->image_url_);
- EXPECT_EQ(gfx::kFaviconSize, download_handler->image_size_);
+ EXPECT_EQ(new_icon_url, download_handler->GetImageUrl());
+ EXPECT_EQ(gfx::kFaviconSize, download_handler->GetImageSize());
// Reset the history_handler to verify whether favicon is set.
helper.set_history_handler(NULL);
@@ -558,7 +551,7 @@ TEST_F(FaviconHandlerTest, UpdateFavicon) {
// Simulates update with the different favicon url.
std::vector<FaviconURL> urls;
urls.push_back(FaviconURL(new_icon_url, FaviconURL::FAVICON));
- DownloadHandler::UpdateFaviconURL(&helper, urls);
+ helper.OnUpdateFaviconURL(0, urls);
// Verify FaviconHandler status.
EXPECT_EQ(1U, helper.urls().size());
@@ -584,7 +577,7 @@ TEST_F(FaviconHandlerTest, UpdateFavicon) {
history_handler->InvokeCallback();
// Shouldn't request download favicon
- EXPECT_FALSE(helper.download_handler());
+ EXPECT_FALSE(helper.download_handler()->HasDownload());
// Verify the favicon status.
EXPECT_EQ(new_icon_url, helper.GetEntry()->GetFavicon().url);
@@ -629,8 +622,7 @@ TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
urls.push_back(FaviconURL(icon_url, FaviconURL::TOUCH_PRECOMPOSED_ICON));
urls.push_back(FaviconURL(new_icon_url, FaviconURL::TOUCH_ICON));
urls.push_back(FaviconURL(new_icon_url, FaviconURL::FAVICON));
-
- DownloadHandler::UpdateFaviconURL(&helper, urls);
+ helper.OnUpdateFaviconURL(0, urls);
// Verify FaviconHandler status.
EXPECT_EQ(2U, helper.urls().size());
@@ -652,16 +644,17 @@ TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
// Should request download favicon.
DownloadHandler* download_handler = helper.download_handler();
- EXPECT_TRUE(download_handler);
+ EXPECT_TRUE(helper.download_handler()->HasDownload());
+
// Verify the download request.
- EXPECT_EQ(icon_url, download_handler->image_url_);
- EXPECT_EQ(0, download_handler->image_size_);
+ EXPECT_EQ(icon_url, download_handler->GetImageUrl());
+ EXPECT_EQ(0, download_handler->GetImageSize());
// 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->set_failed(true);
download_handler->InvokeCallback();
// Left 1 url.
@@ -678,7 +671,7 @@ TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
EXPECT_EQ(page_url, history_handler->page_url_);
// Reset download handler
- helper.set_download_handler(NULL);
+ download_handler->Reset();
// Smulates getting a expired icon from history.
history_handler->favicon_data_.known_icon = true;
@@ -691,10 +684,9 @@ TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
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_);
+ EXPECT_TRUE(helper.download_handler()->HasDownload());
+ EXPECT_EQ(new_icon_url, download_handler->GetImageUrl());
+ EXPECT_EQ(0, download_handler->GetImageSize());
helper.set_history_handler(NULL);
@@ -747,8 +739,7 @@ TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
urls.push_back(FaviconURL(icon_url, FaviconURL::TOUCH_PRECOMPOSED_ICON));
urls.push_back(FaviconURL(new_icon_url, FaviconURL::TOUCH_ICON));
urls.push_back(FaviconURL(new_icon_url, FaviconURL::FAVICON));
-
- DownloadHandler::UpdateFaviconURL(&helper, urls);
+ helper.OnUpdateFaviconURL(0, urls);
// Verify FaviconHandler status.
EXPECT_EQ(2U, helper.urls().size());
@@ -770,10 +761,11 @@ TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
// Should request download favicon.
DownloadHandler* download_handler = helper.download_handler();
- EXPECT_TRUE(download_handler);
+ EXPECT_TRUE(helper.download_handler()->HasDownload());
+
// Verify the download request.
- EXPECT_EQ(icon_url, download_handler->image_url_);
- EXPECT_EQ(0, download_handler->image_size_);
+ EXPECT_EQ(icon_url, download_handler->GetImageUrl());
+ EXPECT_EQ(0, download_handler->GetImageSize());
// Reset the history_handler to verify whether favicon is request from
// history.
@@ -781,7 +773,8 @@ TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
const GURL latest_icon_url("http://www.google.com/latest_favicon");
std::vector<FaviconURL> latest_urls;
latest_urls.push_back(FaviconURL(latest_icon_url, FaviconURL::TOUCH_ICON));
- DownloadHandler::UpdateFaviconURL(&helper, latest_urls);
+ helper.OnUpdateFaviconURL(0, latest_urls);
+
EXPECT_EQ(1U, helper.urls().size());
EXPECT_EQ(latest_icon_url, helper.current_candidate()->icon_url);
EXPECT_EQ(FaviconURL::TOUCH_ICON, helper.current_candidate()->icon_type);
@@ -804,7 +797,7 @@ TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
// The downloaded icon should be thrown away as there is favicon update.
EXPECT_FALSE(helper.history_handler());
- helper.set_download_handler(NULL);
+ download_handler->Reset();
// Simulates getting the icon from history.
scoped_ptr<HistoryRequestHandler> handler;
@@ -821,7 +814,77 @@ TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
handler->InvokeCallback();
// No download request.
- EXPECT_FALSE(helper.download_handler());
+ EXPECT_FALSE(download_handler->HasDownload());
+}
+
+TEST_F(FaviconHandlerTest, MultipleFavicon) {
+ const GURL page_url("http://www.google.com");
+ const GURL icon_url("http://www.google.com/favicon");
+ const GURL icon_url_small("http://www.google.com/favicon_small");
+ const GURL icon_url_large("http://www.google.com/favicon_large");
+ const GURL icon_url_preferred("http://www.google.com/favicon_preferred");
+
+ TestFaviconHandlerDelegate delegate(contents());
+ Profile* profile = Profile::FromBrowserContext(
+ contents()->GetBrowserContext());
+ TestFaviconHandler helper(page_url, profile,
+ &delegate, FaviconHandler::FAVICON);
+
+ helper.FetchFavicon(page_url);
+ HistoryRequestHandler* history_handler = helper.history_handler();
+
+ // 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(gfx::kFaviconSize, gfx::kFaviconSize, &data->data());
+ history_handler->favicon_data_.image_data = data;
+
+ // Send history response.
+ history_handler->InvokeCallback();
+
+ // Simulates update with the different favicon url.
+ std::vector<FaviconURL> urls;
+ // Note: the code will stop making requests when an icon matching the
+ // preferred size is found, so icon_url_preferred must be last.
+ urls.push_back(FaviconURL(icon_url_small, FaviconURL::FAVICON));
+ urls.push_back(FaviconURL(icon_url_large, FaviconURL::FAVICON));
+ urls.push_back(FaviconURL(icon_url_preferred, FaviconURL::FAVICON));
+ helper.OnUpdateFaviconURL(0, urls);
+
+ DownloadHandler* download_handler = helper.download_handler();
+
+ // Download the first icon (set not in history).
+ helper.history_handler()->favicon_data_.known_icon = false;
+ helper.history_handler()->InvokeCallback();
+ ASSERT_TRUE(download_handler->HasDownload());
+ EXPECT_EQ(icon_url_small, download_handler->GetImageUrl());
+ download_handler->SetImageSize(gfx::kFaviconSize / 2);
+ download_handler->InvokeCallback();
+
+ // Download the second icon (set not in history).
+ helper.history_handler()->favicon_data_.known_icon = false;
+ helper.history_handler()->InvokeCallback();
+ ASSERT_TRUE(download_handler->HasDownload());
+ EXPECT_EQ(icon_url_large, download_handler->GetImageUrl());
+ download_handler->SetImageSize(gfx::kFaviconSize * 2);
+ download_handler->InvokeCallback();
+
+ // Download the third icon (set not in history).
+ helper.history_handler()->favicon_data_.known_icon = false;
+ helper.history_handler()->InvokeCallback();
+ ASSERT_TRUE(download_handler->HasDownload());
+ EXPECT_EQ(icon_url_preferred, download_handler->GetImageUrl());
+ download_handler->SetImageSize(gfx::kFaviconSize);
+ download_handler->InvokeCallback();
+
+ // Verify correct icon size chosen.
+ EXPECT_EQ(icon_url_preferred, helper.GetEntry()->GetFavicon().url);
+ EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid);
+ EXPECT_FALSE(helper.GetEntry()->GetFavicon().bitmap.empty());
+ EXPECT_EQ(gfx::kFaviconSize, helper.GetEntry()->GetFavicon().bitmap.width());
}
} // namespace.
« chrome/browser/favicon/favicon_handler.cc ('K') | « chrome/browser/favicon/favicon_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698