Index: chrome/browser/favicon/favicon_tab_helper_unittest.cc |
diff --git a/chrome/browser/favicon/favicon_tab_helper_unittest.cc b/chrome/browser/favicon/favicon_tab_helper_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..40a0644d58bf150fbef2359deadce560436c6c9d |
--- /dev/null |
+++ b/chrome/browser/favicon/favicon_tab_helper_unittest.cc |
@@ -0,0 +1,149 @@ |
+// Copyright (c) 2012 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/favicon_tab_helper.h" |
+ |
+#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
+#include "content/public/browser/favicon_status.h" |
+#include "content/public/browser/navigation_entry.h" |
+#include "content/public/browser/notification_types.h" |
+#include "content/public/test/test_notification_tracker.h" |
+#include "content/public/test/test_renderer_host.h" |
+#include "ui/gfx/image/image_skia.h" |
+ |
+class FaviconTabHelperTest : public ChromeRenderViewHostTestHarness { |
+ public: |
+ FaviconTabHelperTest() { |
+ } |
+ |
+ virtual ~FaviconTabHelperTest() { |
+ } |
+ |
+ bool DoImagesMatch(const gfx::Image& a, const gfx::Image& b) { |
+ // Assume that if the 1x bitmaps match, the images match. |
+ SkBitmap a_bitmap = a.AsBitmap(); |
+ SkBitmap b_bitmap = b.AsBitmap(); |
+ |
+ if (a_bitmap.width() != b_bitmap.width() || |
+ a_bitmap.height() != b_bitmap.height()) { |
+ return false; |
+ } |
+ |
+ SkAutoLockPixels a_bitmap_lock(a_bitmap); |
+ SkAutoLockPixels b_bitmap_lock(b_bitmap); |
+ |
+ return memcmp(a_bitmap.getPixels(), |
+ b_bitmap.getPixels(), |
+ b_bitmap.getSize()) == 0; |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(FaviconTabHelperTest); |
+}; |
+ |
+// Ensure that the default favicon is used for pages without favicons. |
+// This test checks that this is the case when the page is navigated to via a |
+// client redirect. (crbug.com/28515) |
+TEST_F(FaviconTabHelperTest, ClearFaviconOnRedirect) { |
+ const GURL kPageWithFavicon("http://withfavicon.html"); |
+ const GURL kPageWithoutFavicon("http://withoutfavicon.html"); |
+ const GURL kIconURL("http://withfavicon.ico"); |
+ const gfx::Image kDefaultFavicon = content::FaviconStatus().image; |
+ |
+ content::NavigationController& controller = web_contents()->GetController(); |
+ content::TestNotificationTracker notifications; |
+ notifications.ListenFor( |
+ content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
+ content::Source<content::NavigationController>(&controller)); |
+ |
+ // Navigate to a page with a simulated favicon. |
+ rvh_tester()->SendNavigate(0, kPageWithFavicon); |
+ EXPECT_TRUE(notifications.Check1AndReset( |
+ content::NOTIFICATION_NAV_ENTRY_COMMITTED)); |
+ |
+ content::NavigationEntry* entry = controller.GetLastCommittedEntry(); |
+ EXPECT_TRUE(entry); |
+ EXPECT_EQ(kPageWithFavicon, entry->GetURL()); |
+ |
+ // Simulate the favicon being set as a result of |
+ // FaviconTabHelper::FetchFavicon(). |
+ SkBitmap bitmap; |
+ bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
+ bitmap.allocPixels(); |
+ |
+ content::FaviconStatus& favicon_status = entry->GetFavicon(); |
+ favicon_status.image = gfx::Image(bitmap); |
+ favicon_status.url = kIconURL; |
+ favicon_status.valid = true; |
+ |
+ FaviconTabHelper::CreateForWebContents(web_contents()); |
+ FaviconTabHelper* favicon_tab_helper = |
+ FaviconTabHelper::FromWebContents(web_contents()); |
+ EXPECT_FALSE(DoImagesMatch(kDefaultFavicon, |
+ favicon_tab_helper->GetFavicon())); |
+ |
+ // Redirect to a page without a favicon. |
+ rvh_tester()->SendNavigateWithTransition( |
+ 0, |
+ kPageWithoutFavicon, |
+ content::PAGE_TRANSITION_CLIENT_REDIRECT); |
+ EXPECT_TRUE(notifications.Check1AndReset( |
+ content::NOTIFICATION_NAV_ENTRY_COMMITTED)); |
+ |
+ entry = controller.GetLastCommittedEntry(); |
+ EXPECT_TRUE(entry); |
+ EXPECT_EQ(kPageWithoutFavicon, entry->GetURL()); |
+ |
+ EXPECT_TRUE(DoImagesMatch(kDefaultFavicon, |
+ favicon_tab_helper->GetFavicon())); |
+} |
+ |
+// Check that favicons for NavigationEntrys which were previously navigated to |
+// are not cleared to avoid potential negative interaction with |
+// BackForwardMenuModel. |
+TEST_F(FaviconTabHelperTest, BackNavigationDoesNotClearFavicon) { |
+ const GURL kUrl1("http://www.a.com/1"); |
+ const GURL kUrl2("http://www.a.com/2"); |
+ const GURL kIconURL("http://withfavicon.ico"); |
+ |
+ // Ensure that the favicon tab helper exists. |
+ FaviconTabHelper::CreateForWebContents(web_contents()); |
+ |
+ NavigateAndCommit(kUrl1); |
+ |
+ // Simulate the favicon being set for the page at |kUrl1| as a result of |
+ // FaviconTabHelper::FetchFavicon(). |
+ SkBitmap favicon_bitmap; |
+ favicon_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
+ favicon_bitmap.allocPixels(); |
+ |
+ content::NavigationEntry* entry = controller().GetLastCommittedEntry(); |
+ EXPECT_TRUE(entry); |
+ content::FaviconStatus& favicon_status = entry->GetFavicon(); |
+ favicon_status.image = gfx::Image(favicon_bitmap); |
+ favicon_status.url = kIconURL; |
+ favicon_status.valid = true; |
+ entry->GetFavicon().image = gfx::Image(favicon_bitmap); |
+ |
+ // Navigate to another page such that it is possible to go back. |
+ NavigateAndCommit(kUrl2); |
+ |
+ // Go back, then forward. |
+ rvh_tester()->SendNavigateWithTransition( |
+ 0, |
+ kUrl1, |
+ content::PAGE_TRANSITION_FORWARD_BACK); |
+ rvh_tester()->SendNavigateWithTransition( |
+ 1, |
+ kUrl2, |
+ content::PAGE_TRANSITION_FORWARD_BACK); |
+ |
+ // Verify that the favicon for the page at |kUrl1| was not cleared. |
+ gfx::Image favicon_after_back; |
+ entry = controller().GetEntryAtIndex(0); |
+ EXPECT_TRUE(entry); |
+ EXPECT_EQ(kUrl1, entry->GetURL()); |
+ EXPECT_TRUE(DoImagesMatch(gfx::Image(favicon_bitmap), |
+ entry->GetFavicon().image)); |
+} |