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

Side by Side Diff: chrome/browser/favicon/favicon_handler_unittest.cc

Issue 10911149: Cleanup FaviconHandler (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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
« no previous file with comments | « chrome/browser/favicon/favicon_handler.cc ('k') | chrome/browser/favicon/favicon_tab_helper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "chrome/browser/favicon/favicon_handler.h" 6 #include "chrome/browser/favicon/favicon_handler.h"
7 #include "chrome/browser/profiles/profile.h" 7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "content/public/browser/invalidate_type.h" 9 #include "content/public/browser/invalidate_type.h"
10 #include "content/public/browser/navigation_entry.h" 10 #include "content/public/browser/navigation_entry.h"
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 } 184 }
185 185
186 virtual void NotifyFaviconUpdated() { 186 virtual void NotifyFaviconUpdated() {
187 tab_contents_->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); 187 tab_contents_->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
188 } 188 }
189 189
190 private: 190 private:
191 WebContents* tab_contents_; // weak 191 WebContents* tab_contents_; // weak
192 }; 192 };
193 193
194 // This class provides a callback for FaviconHandler::DownloadImage() and
195 // provides an accessor to test the pixel size of the downloaded bitmap.
196 class BitmapDownloader {
197 public:
198 BitmapDownloader() {
199 }
200
201 ~BitmapDownloader() {
202 }
203
204 void OnBitmapDownloaded(int id, bool error, const SkBitmap& bitmap) {
205 downloaded_bitmap_size_ = gfx::Size(bitmap.width(), bitmap.height());
206 }
207
208 const gfx::Size& downloaded_bitmap_size() {
209 return downloaded_bitmap_size_;
210 }
211
212 private:
213 // The size of the downloaded bitmap.
214 gfx::Size downloaded_bitmap_size_;
215
216 DISALLOW_COPY_AND_ASSIGN(BitmapDownloader);
217 };
218
194 // This class is used to catch the FaviconHandler's download and history 219 // This class is used to catch the FaviconHandler's download and history
195 // request, and also provide the methods to access the FaviconHandler internal. 220 // request, and also provide the methods to access the FaviconHandler
221 // internals.
196 class TestFaviconHandler : public FaviconHandler { 222 class TestFaviconHandler : public FaviconHandler {
197 public: 223 public:
198 TestFaviconHandler(const GURL& page_url, 224 TestFaviconHandler(const GURL& page_url,
199 Profile* profile, 225 Profile* profile,
200 FaviconHandlerDelegate* delegate, 226 FaviconHandlerDelegate* delegate,
201 Type type) 227 Type type)
202 : FaviconHandler(profile, delegate, type), 228 : FaviconHandler(profile, delegate, type),
203 entry_(NavigationEntry::Create()), 229 entry_(NavigationEntry::Create()),
204 download_id_(0) { 230 download_id_(0) {
205 entry_->SetURL(page_url); 231 entry_->SetURL(page_url);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 335
310 namespace { 336 namespace {
311 337
312 void HistoryRequestHandler::InvokeCallback() { 338 void HistoryRequestHandler::InvokeCallback() {
313 if (!callback_.is_null()) 339 if (!callback_.is_null())
314 callback_.Run(0, history_results_, history::IconURLSizesMap()); 340 callback_.Run(0, history_results_, history::IconURLSizesMap());
315 } 341 }
316 342
317 void DownloadHandler::InvokeCallback() { 343 void DownloadHandler::InvokeCallback() {
318 SkBitmap bitmap; 344 SkBitmap bitmap;
319 int bitmap_size = (download_->image_size > 0) ? 345 const int kRequestedSize = gfx::kFaviconSize;
346 int downloaded_size = (download_->image_size > 0) ?
320 download_->image_size : gfx::kFaviconSize; 347 download_->image_size : gfx::kFaviconSize;
321 FillDataToBitmap(bitmap_size, bitmap_size, &bitmap); 348 FillDataToBitmap(downloaded_size, downloaded_size, &bitmap);
322 gfx::Image image(bitmap); 349 std::vector<SkBitmap> bitmaps;
350 bitmaps.push_back(bitmap);
323 favicon_helper_->OnDidDownloadFavicon( 351 favicon_helper_->OnDidDownloadFavicon(
324 download_->download_id, download_->image_url, failed_, image, 352 download_->download_id, download_->image_url, failed_,
325 download_->image_size == gfx::kFaviconSize); 353 kRequestedSize, bitmaps);
326 } 354 }
327 355
328 class FaviconHandlerTest : public ChromeRenderViewHostTestHarness { 356 class FaviconHandlerTest : public ChromeRenderViewHostTestHarness {
357 public:
358 FaviconHandlerTest() {
359 }
360
361 virtual ~FaviconHandlerTest() {
362 }
363
364 virtual void SetUp() {
365 // The score computed by SelectFaviconFrames() is dependent on the supported
366 // scale factors of the platform. It is used for determining the goodness of
367 // a downloaded bitmap in FaviconHandler::OnDidDownloadFavicon().
368 // Force the values of the scale factors so that the tests produce the same
369 // results on all platforms.
370 std::vector<ui::ScaleFactor> scale_factors;
371 scale_factors.push_back(ui::SCALE_FACTOR_100P);
372 ui::test::SetSupportedScaleFactors(scale_factors);
373
374 ChromeRenderViewHostTestHarness::SetUp();
375 }
376
377 private:
378 DISALLOW_COPY_AND_ASSIGN(FaviconHandlerTest);
329 }; 379 };
330 380
331 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) { 381 TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
332 const GURL page_url("http://www.google.com"); 382 const GURL page_url("http://www.google.com");
333 const GURL icon_url("http://www.google.com/favicon"); 383 const GURL icon_url("http://www.google.com/favicon");
334 384
335 TestFaviconHandlerDelegate delegate(contents()); 385 TestFaviconHandlerDelegate delegate(contents());
336 Profile* profile = Profile::FromBrowserContext( 386 Profile* profile = Profile::FromBrowserContext(
337 contents()->GetBrowserContext()); 387 contents()->GetBrowserContext());
338 TestFaviconHandler helper(page_url, profile, 388 TestFaviconHandler helper(page_url, profile,
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after
918 EXPECT_EQ(0U, handler.image_urls().size()); 968 EXPECT_EQ(0U, handler.image_urls().size());
919 969
920 // Verify correct icon size chosen. 970 // Verify correct icon size chosen.
921 EXPECT_EQ(icon_url_preferred1, handler.GetEntry()->GetFavicon().url); 971 EXPECT_EQ(icon_url_preferred1, handler.GetEntry()->GetFavicon().url);
922 EXPECT_TRUE(handler.GetEntry()->GetFavicon().valid); 972 EXPECT_TRUE(handler.GetEntry()->GetFavicon().valid);
923 EXPECT_FALSE(handler.GetEntry()->GetFavicon().image.IsEmpty()); 973 EXPECT_FALSE(handler.GetEntry()->GetFavicon().image.IsEmpty());
924 EXPECT_EQ(gfx::kFaviconSize, 974 EXPECT_EQ(gfx::kFaviconSize,
925 handler.GetEntry()->GetFavicon().image.ToSkBitmap()->width()); 975 handler.GetEntry()->GetFavicon().image.ToSkBitmap()->width());
926 } 976 }
927 977
978 // Test that DownloadImage() returns an unresized bitmap.
979 TEST_F(FaviconHandlerTest, DownloadImage) {
980 const GURL page_url("http://www.google.com");
981 const GURL icon_url("http://www.google.com/favicon");
982
983 TestFaviconHandlerDelegate delegate(contents());
984 Profile* profile = Profile::FromBrowserContext(
985 contents()->GetBrowserContext());
986 TestFaviconHandler handler(page_url, profile,
987 &delegate, FaviconHandler::FAVICON);
988
989 BitmapDownloader downloader;
990 FaviconTabHelper::ImageDownloadCallback callback = base::Bind(
991 &BitmapDownloader::OnBitmapDownloaded, base::Unretained(&downloader));
992
993 handler.DownloadImage(icon_url, gfx::kFaviconSize, history::FAVICON,
994 callback);
995
996 DownloadHandler* download_handler = handler.download_handler();
997 ASSERT_TRUE(download_handler->HasDownload());
998 EXPECT_EQ(icon_url, download_handler->GetImageUrl());
999
1000 // Set the downloaded bitmap size to something different than the requested
1001 // size of gfx::kFaviconSize;
1002 const int kLargeSize = gfx::kFaviconSize * 2;
1003 download_handler->SetImageSize(kLargeSize);
1004 download_handler->InvokeCallback();
1005
1006 // Check that the callback was invoked with the unresized bitmap.
1007 const gfx::Size& downloaded_bitmap_size = downloader.downloaded_bitmap_size();
1008 EXPECT_EQ(kLargeSize, downloaded_bitmap_size.width());
1009 EXPECT_EQ(kLargeSize, downloaded_bitmap_size.height());
1010 }
1011
928 } // namespace. 1012 } // namespace.
OLDNEW
« no previous file with comments | « chrome/browser/favicon/favicon_handler.cc ('k') | chrome/browser/favicon/favicon_tab_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698