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