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

Side by Side Diff: chrome/browser/tab_contents/thumbnail_generator_unittest.cc

Issue 6389001: Add heuristics to skip thumbnail generation when it's unnecessary. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: minor cleanup Created 9 years, 11 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
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "app/surface/transport_dib.h" 5 #include "app/surface/transport_dib.h"
6 #include "base/basictypes.h" 6 #include "base/basictypes.h"
7 #include "base/stringprintf.h"
8 #include "chrome/browser/history/top_sites.h"
7 #include "chrome/browser/renderer_host/backing_store_manager.h" 9 #include "chrome/browser/renderer_host/backing_store_manager.h"
8 #include "chrome/browser/renderer_host/mock_render_process_host.h" 10 #include "chrome/browser/renderer_host/mock_render_process_host.h"
9 #include "chrome/browser/renderer_host/test/test_render_view_host.h" 11 #include "chrome/browser/renderer_host/test/test_render_view_host.h"
10 #include "chrome/browser/tab_contents/thumbnail_generator.h" 12 #include "chrome/browser/tab_contents/thumbnail_generator.h"
11 #include "chrome/common/notification_service.h" 13 #include "chrome/common/notification_service.h"
14 #include "chrome/common/chrome_constants.h"
12 #include "chrome/common/render_messages.h" 15 #include "chrome/common/render_messages.h"
13 #include "chrome/test/testing_profile.h" 16 #include "chrome/test/testing_profile.h"
14 #include "gfx/canvas_skia.h" 17 #include "gfx/canvas_skia.h"
15 #include "skia/ext/platform_canvas.h" 18 #include "skia/ext/platform_canvas.h"
16 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkColorPriv.h" 20 #include "third_party/skia/include/core/SkColorPriv.h"
18 21
19 static const int kBitmapWidth = 100; 22 static const int kBitmapWidth = 100;
20 static const int kBitmapHeight = 100; 23 static const int kBitmapHeight = 100;
21 24
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 // The desired size is horizontally long. 274 // The desired size is horizontally long.
272 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; 275 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped;
273 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( 276 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap(
274 bitmap, 20, 10, &clip_result); 277 bitmap, 20, 10, &clip_result);
275 // The clipped bitmap should have the same aspect ratio of the desired size. 278 // The clipped bitmap should have the same aspect ratio of the desired size.
276 EXPECT_EQ(40, clipped_bitmap.width()); 279 EXPECT_EQ(40, clipped_bitmap.width());
277 EXPECT_EQ(20, clipped_bitmap.height()); 280 EXPECT_EQ(20, clipped_bitmap.height());
278 // The input was taller than wide. 281 // The input was taller than wide.
279 EXPECT_EQ(ThumbnailGenerator::kTallerThanWide, clip_result); 282 EXPECT_EQ(ThumbnailGenerator::kTallerThanWide, clip_result);
280 } 283 }
284
285 // A mock version of TopSites, used for testing ShouldUpdateThumbnail().
286 class MockTopSites : public history::TopSites {
287 public:
288 MockTopSites(Profile* profile)
289 : history::TopSites(profile),
290 capacity_(1) {
291 }
292
293 // history::TopSites overrides.
294 virtual bool IsFull() {
295 return known_url_map_.size() >= capacity_;
296 }
297 virtual bool IsKnownURL(const GURL& url) {
298 return known_url_map_.find(url.spec()) != known_url_map_.end();
299 }
300 virtual bool GetPageThumbnailScore(const GURL& url, ThumbnailScore* score) {
301 std::map<std::string, ThumbnailScore>::const_iterator iter =
302 known_url_map_.find(url.spec());
303 if (iter == known_url_map_.end()) {
304 return false;
305 } else {
306 *score = iter->second;
307 return true;
308 }
309 }
310
311 // Adds a known URL with the associated thumbnail score.
312 void AddKnownURL(const GURL& url, const ThumbnailScore& score) {
313 known_url_map_[url.spec()] = score;
314 }
315
316 private:
317 virtual ~MockTopSites() {}
318 size_t capacity_;
319 std::map<std::string, ThumbnailScore> known_url_map_;
320 };
321
322 TEST(ThumbnailGeneratorSimpleTest, ShouldUpdateThumbnail) {
323 const GURL kGoodURL("http://www.google.com/");
324 const GURL kBadURL("chrome://newtab");
325
326 // Set up the profile.
327 TestingProfile profile;
328
329 // Set up the top sites service.
330 ScopedTempDir temp_dir;
331 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
332 scoped_refptr<MockTopSites> top_sites(new MockTopSites(&profile));
333 FilePath file_name = temp_dir.path().Append(chrome::kTopSitesFilename);
334 top_sites->Init(file_name);
335
336 // Should be false because it's a bad URL.
337 EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail(
338 &profile, top_sites.get(), kBadURL));
339
340 // Should be true, as it's a good URL.
341 EXPECT_TRUE(ThumbnailGenerator::ShouldUpdateThumbnail(
342 &profile, top_sites.get(), kGoodURL));
343
344 // Should be false, if it's in the off-the-record mode.
345 profile.set_off_the_record(true);
346 EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail(
347 &profile, top_sites.get(), kGoodURL));
348
349 // Should be true again, once turning off the off-the-record mode.
350 profile.set_off_the_record(false);
351 EXPECT_TRUE(ThumbnailGenerator::ShouldUpdateThumbnail(
352 &profile, top_sites.get(), kGoodURL));
353
354 // Add a known URL. This makes the top sites data full.
355 ThumbnailScore bad_score;
356 bad_score.time_at_snapshot = base::Time::UnixEpoch(); // Ancient time stamp.
357 top_sites->AddKnownURL(kGoodURL, bad_score);
358 ASSERT_TRUE(top_sites->IsFull());
359
360 // Should be false, as the top sites data is full, and the new URL is
361 // not known.
362 const GURL kAnotherGoodURL("http://www.youtube.com/");
363 EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail(
364 &profile, top_sites.get(), kAnotherGoodURL));
365
366 // Should be true, as the existing thumbnail is bad (i.e need a better one).
367 EXPECT_TRUE(ThumbnailGenerator::ShouldUpdateThumbnail(
368 &profile, top_sites.get(), kGoodURL));
369
370 // Replace the thumbnail score with a really good one.
371 ThumbnailScore good_score;
372 good_score.time_at_snapshot = base::Time::Now(); // Very new.
373 good_score.at_top = true;
374 good_score.good_clipping = true;
375 good_score.boring_score = 0.0;
376 top_sites->AddKnownURL(kGoodURL, good_score);
377
378 // Should be false, as the existing thumbnail is good enough (i.e. don't
379 // need to replace the existing thumbnail which is new and good).
380 EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail(
381 &profile, top_sites.get(), kGoodURL));
382 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698