OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 6 |
| 7 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 8 #include "content/public/browser/favicon_status.h" |
| 9 #include "content/public/browser/navigation_entry.h" |
| 10 #include "content/public/browser/notification_types.h" |
| 11 #include "content/public/test/test_notification_tracker.h" |
| 12 #include "content/public/test/test_renderer_host.h" |
| 13 #include "ui/gfx/image/image_skia.h" |
| 14 |
| 15 class FaviconTabHelperTest : public ChromeRenderViewHostTestHarness { |
| 16 public: |
| 17 FaviconTabHelperTest() { |
| 18 } |
| 19 |
| 20 virtual ~FaviconTabHelperTest() { |
| 21 } |
| 22 |
| 23 bool DoImagesMatch(const gfx::Image& a, const gfx::Image& b) { |
| 24 // Assume that if the 1x bitmaps match, the images match. |
| 25 SkBitmap a_bitmap = a.AsBitmap(); |
| 26 SkBitmap b_bitmap = b.AsBitmap(); |
| 27 |
| 28 if (a_bitmap.width() != b_bitmap.width() || |
| 29 a_bitmap.height() != b_bitmap.height()) { |
| 30 return false; |
| 31 } |
| 32 |
| 33 SkAutoLockPixels a_bitmap_lock(a_bitmap); |
| 34 SkAutoLockPixels b_bitmap_lock(b_bitmap); |
| 35 |
| 36 return memcmp(a_bitmap.getPixels(), |
| 37 b_bitmap.getPixels(), |
| 38 b_bitmap.getSize()) == 0; |
| 39 } |
| 40 |
| 41 private: |
| 42 DISALLOW_COPY_AND_ASSIGN(FaviconTabHelperTest); |
| 43 }; |
| 44 |
| 45 // Ensure that the default favicon is used for pages without favicons. |
| 46 // This test checks that this is the case when the page is navigated to via a |
| 47 // client redirect. (crbug.com/28515) |
| 48 TEST_F(FaviconTabHelperTest, ClearFaviconOnRedirect) { |
| 49 const GURL kPageWithFavicon("http://withfavicon.html"); |
| 50 const GURL kPageWithoutFavicon("http://withoutfavicon.html"); |
| 51 const GURL kIconURL("http://withfavicon.ico"); |
| 52 const gfx::Image kDefaultFavicon = content::FaviconStatus().image; |
| 53 |
| 54 content::NavigationController& controller = web_contents()->GetController(); |
| 55 content::TestNotificationTracker notifications; |
| 56 notifications.ListenFor( |
| 57 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 58 content::Source<content::NavigationController>(&controller)); |
| 59 |
| 60 // Navigate to a page with a simulated favicon. |
| 61 rvh_tester()->SendNavigate(0, kPageWithFavicon); |
| 62 EXPECT_TRUE(notifications.Check1AndReset( |
| 63 content::NOTIFICATION_NAV_ENTRY_COMMITTED)); |
| 64 |
| 65 content::NavigationEntry* entry = controller.GetLastCommittedEntry(); |
| 66 EXPECT_TRUE(entry); |
| 67 EXPECT_EQ(kPageWithFavicon, entry->GetURL()); |
| 68 |
| 69 // Simulate the favicon being set as a result of |
| 70 // FaviconTabHelper::FetchFavicon(). |
| 71 SkBitmap bitmap; |
| 72 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
| 73 bitmap.allocPixels(); |
| 74 |
| 75 content::FaviconStatus& favicon_status = entry->GetFavicon(); |
| 76 favicon_status.image = gfx::Image(bitmap); |
| 77 favicon_status.url = kIconURL; |
| 78 favicon_status.valid = true; |
| 79 |
| 80 FaviconTabHelper::CreateForWebContents(web_contents()); |
| 81 FaviconTabHelper* favicon_tab_helper = |
| 82 FaviconTabHelper::FromWebContents(web_contents()); |
| 83 EXPECT_FALSE(DoImagesMatch(kDefaultFavicon, |
| 84 favicon_tab_helper->GetFavicon())); |
| 85 |
| 86 // Redirect to a page without a favicon. |
| 87 rvh_tester()->SendNavigateWithTransition( |
| 88 0, |
| 89 kPageWithoutFavicon, |
| 90 content::PAGE_TRANSITION_CLIENT_REDIRECT); |
| 91 EXPECT_TRUE(notifications.Check1AndReset( |
| 92 content::NOTIFICATION_NAV_ENTRY_COMMITTED)); |
| 93 |
| 94 entry = controller.GetLastCommittedEntry(); |
| 95 EXPECT_TRUE(entry); |
| 96 EXPECT_EQ(kPageWithoutFavicon, entry->GetURL()); |
| 97 |
| 98 EXPECT_TRUE(DoImagesMatch(kDefaultFavicon, |
| 99 favicon_tab_helper->GetFavicon())); |
| 100 } |
| 101 |
| 102 // Check that favicons for NavigationEntrys which were previously navigated to |
| 103 // are not cleared to avoid potential negative interaction with |
| 104 // BackForwardMenuModel. |
| 105 TEST_F(FaviconTabHelperTest, BackNavigationDoesNotClearFavicon) { |
| 106 const GURL kUrl1("http://www.a.com/1"); |
| 107 const GURL kUrl2("http://www.a.com/2"); |
| 108 const GURL kIconURL("http://withfavicon.ico"); |
| 109 |
| 110 // Ensure that the favicon tab helper exists. |
| 111 FaviconTabHelper::CreateForWebContents(web_contents()); |
| 112 |
| 113 NavigateAndCommit(kUrl1); |
| 114 |
| 115 // Simulate the favicon being set for the page at |kUrl1| as a result of |
| 116 // FaviconTabHelper::FetchFavicon(). |
| 117 SkBitmap favicon_bitmap; |
| 118 favicon_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
| 119 favicon_bitmap.allocPixels(); |
| 120 |
| 121 content::NavigationEntry* entry = controller().GetLastCommittedEntry(); |
| 122 EXPECT_TRUE(entry); |
| 123 content::FaviconStatus& favicon_status = entry->GetFavicon(); |
| 124 favicon_status.image = gfx::Image(favicon_bitmap); |
| 125 favicon_status.url = kIconURL; |
| 126 favicon_status.valid = true; |
| 127 entry->GetFavicon().image = gfx::Image(favicon_bitmap); |
| 128 |
| 129 // Navigate to another page such that it is possible to go back. |
| 130 NavigateAndCommit(kUrl2); |
| 131 |
| 132 // Go back, then forward. |
| 133 rvh_tester()->SendNavigateWithTransition( |
| 134 0, |
| 135 kUrl1, |
| 136 content::PAGE_TRANSITION_FORWARD_BACK); |
| 137 rvh_tester()->SendNavigateWithTransition( |
| 138 1, |
| 139 kUrl2, |
| 140 content::PAGE_TRANSITION_FORWARD_BACK); |
| 141 |
| 142 // Verify that the favicon for the page at |kUrl1| was not cleared. |
| 143 gfx::Image favicon_after_back; |
| 144 entry = controller().GetEntryAtIndex(0); |
| 145 EXPECT_TRUE(entry); |
| 146 EXPECT_EQ(kUrl1, entry->GetURL()); |
| 147 EXPECT_TRUE(DoImagesMatch(gfx::Image(favicon_bitmap), |
| 148 entry->GetFavicon().image)); |
| 149 } |
OLD | NEW |