Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/stringprintf.h" | 6 #include "base/stringprintf.h" |
| 7 #include "chrome/browser/history/top_sites.h" | 7 #include "chrome/browser/history/top_sites.h" |
| 8 #include "chrome/browser/tab_contents/thumbnail_generator.h" | 8 #include "chrome/browser/tab_contents/thumbnail_generator.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/test/testing_browser_process_test.h" | |
| 10 #include "chrome/test/testing_profile.h" | 11 #include "chrome/test/testing_profile.h" |
| 11 #include "content/browser/renderer_host/backing_store_manager.h" | 12 #include "content/browser/renderer_host/backing_store_manager.h" |
| 12 #include "content/browser/renderer_host/backing_store_skia.h" | 13 #include "content/browser/renderer_host/backing_store_skia.h" |
| 13 #include "content/browser/renderer_host/mock_render_process_host.h" | 14 #include "content/browser/renderer_host/mock_render_process_host.h" |
| 14 #include "content/browser/renderer_host/test_render_view_host.h" | 15 #include "content/browser/renderer_host/test_render_view_host.h" |
| 15 #include "content/browser/tab_contents/render_view_host_manager.h" | 16 #include "content/browser/tab_contents/render_view_host_manager.h" |
| 16 #include "content/common/notification_service.h" | 17 #include "content/common/notification_service.h" |
| 17 #include "content/common/view_messages.h" | 18 #include "content/common/view_messages.h" |
| 18 #include "skia/ext/platform_canvas.h" | 19 #include "skia/ext/platform_canvas.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 46 | 47 |
| 47 BackingStore* AllocBackingStore(const gfx::Size& size) { | 48 BackingStore* AllocBackingStore(const gfx::Size& size) { |
| 48 return new BackingStoreSkia(rwh_, size); | 49 return new BackingStoreSkia(rwh_, size); |
| 49 } | 50 } |
| 50 | 51 |
| 51 private: | 52 private: |
| 52 RenderWidgetHost* rwh_; | 53 RenderWidgetHost* rwh_; |
| 53 DISALLOW_COPY_AND_ASSIGN(TestRenderWidgetHostViewWithBackingStoreSkia); | 54 DISALLOW_COPY_AND_ASSIGN(TestRenderWidgetHostViewWithBackingStoreSkia); |
| 54 }; | 55 }; |
| 55 | 56 |
| 56 class ThumbnailGeneratorTest : public testing::Test { | 57 class ThumbnailGeneratorTest : public TestingBrowserProcessTest { |
| 57 public: | 58 public: |
| 58 ThumbnailGeneratorTest() { | 59 ThumbnailGeneratorTest() { |
| 59 profile_.reset(new TestingProfile()); | 60 profile_.reset(new TestingProfile()); |
| 60 process_ = new MockRenderProcessHost(profile_.get()); | 61 process_ = new MockRenderProcessHost(profile_.get()); |
| 61 widget_.reset(new RenderWidgetHost(process_, 1)); | 62 widget_.reset(new RenderWidgetHost(process_, 1)); |
| 62 view_.reset(new TestRenderWidgetHostViewWithBackingStoreSkia( | 63 view_.reset(new TestRenderWidgetHostViewWithBackingStoreSkia( |
| 63 widget_.get())); | 64 widget_.get())); |
| 64 // Paiting will be skipped if there's no view. | 65 // Paiting will be skipped if there's no view. |
| 65 widget_->SetView(view_.get()); | 66 widget_->SetView(view_.get()); |
| 66 | 67 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 ASSERT_FALSE(widget_->GetBackingStore(false)); | 201 ASSERT_FALSE(widget_->GetBackingStore(false)); |
| 201 | 202 |
| 202 // The thumbnail generator should not be able to retrieve a thumbnail, | 203 // The thumbnail generator should not be able to retrieve a thumbnail, |
| 203 // as the backing store is now gone. | 204 // as the backing store is now gone. |
| 204 result = generator_.GetThumbnailForRenderer(widget_.get()); | 205 result = generator_.GetThumbnailForRenderer(widget_.get()); |
| 205 ASSERT_TRUE(result.isNull()); | 206 ASSERT_TRUE(result.isNull()); |
| 206 } | 207 } |
| 207 | 208 |
| 208 #endif // !defined(OS_MAC) | 209 #endif // !defined(OS_MAC) |
| 209 | 210 |
| 210 TEST(ThumbnailGeneratorSimpleTest, CalculateBoringScore_Empty) { | 211 class ThumbnailGeneratorSimpleTest : public TestingBrowserProcessTest { |
|
sky
2011/07/28 00:28:03
nit: typedef TestingBrowserProcessTest ThumbnailGe
| |
| 212 }; | |
| 213 | |
| 214 TEST_F(ThumbnailGeneratorSimpleTest, CalculateBoringScore_Empty) { | |
| 211 SkBitmap bitmap; | 215 SkBitmap bitmap; |
| 212 EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(&bitmap)); | 216 EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(&bitmap)); |
| 213 } | 217 } |
| 214 | 218 |
| 215 TEST(ThumbnailGeneratorSimpleTest, CalculateBoringScore_SingleColor) { | 219 TEST_F(ThumbnailGeneratorSimpleTest, CalculateBoringScore_SingleColor) { |
| 216 const SkColor kBlack = SkColorSetRGB(0, 0, 0); | 220 const SkColor kBlack = SkColorSetRGB(0, 0, 0); |
| 217 const gfx::Size kSize(20, 10); | 221 const gfx::Size kSize(20, 10); |
| 218 gfx::CanvasSkia canvas(kSize.width(), kSize.height(), true); | 222 gfx::CanvasSkia canvas(kSize.width(), kSize.height(), true); |
| 219 // Fill all pixesl in black. | 223 // Fill all pixesl in black. |
| 220 canvas.FillRectInt(kBlack, 0, 0, kSize.width(), kSize.height()); | 224 canvas.FillRectInt(kBlack, 0, 0, kSize.width(), kSize.height()); |
| 221 | 225 |
| 222 SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); | 226 SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); |
| 223 // The thumbnail should deserve the highest boring score. | 227 // The thumbnail should deserve the highest boring score. |
| 224 EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(&bitmap)); | 228 EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(&bitmap)); |
| 225 } | 229 } |
| 226 | 230 |
| 227 TEST(ThumbnailGeneratorSimpleTest, CalculateBoringScore_TwoColors) { | 231 TEST_F(ThumbnailGeneratorSimpleTest, CalculateBoringScore_TwoColors) { |
| 228 const SkColor kBlack = SkColorSetRGB(0, 0, 0); | 232 const SkColor kBlack = SkColorSetRGB(0, 0, 0); |
| 229 const SkColor kWhite = SkColorSetRGB(0xFF, 0xFF, 0xFF); | 233 const SkColor kWhite = SkColorSetRGB(0xFF, 0xFF, 0xFF); |
| 230 const gfx::Size kSize(20, 10); | 234 const gfx::Size kSize(20, 10); |
| 231 | 235 |
| 232 gfx::CanvasSkia canvas(kSize.width(), kSize.height(), true); | 236 gfx::CanvasSkia canvas(kSize.width(), kSize.height(), true); |
| 233 // Fill all pixesl in black. | 237 // Fill all pixesl in black. |
| 234 canvas.FillRectInt(kBlack, 0, 0, kSize.width(), kSize.height()); | 238 canvas.FillRectInt(kBlack, 0, 0, kSize.width(), kSize.height()); |
| 235 // Fill the left half pixels in white. | 239 // Fill the left half pixels in white. |
| 236 canvas.FillRectInt(kWhite, 0, 0, kSize.width() / 2, kSize.height()); | 240 canvas.FillRectInt(kWhite, 0, 0, kSize.width() / 2, kSize.height()); |
| 237 | 241 |
| 238 SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); | 242 SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); |
| 239 ASSERT_EQ(kSize.width(), bitmap.width()); | 243 ASSERT_EQ(kSize.width(), bitmap.width()); |
| 240 ASSERT_EQ(kSize.height(), bitmap.height()); | 244 ASSERT_EQ(kSize.height(), bitmap.height()); |
| 241 // The thumbnail should be less boring because two colors are used. | 245 // The thumbnail should be less boring because two colors are used. |
| 242 EXPECT_DOUBLE_EQ(0.5, ThumbnailGenerator::CalculateBoringScore(&bitmap)); | 246 EXPECT_DOUBLE_EQ(0.5, ThumbnailGenerator::CalculateBoringScore(&bitmap)); |
| 243 } | 247 } |
| 244 | 248 |
| 245 TEST(ThumbnailGeneratorSimpleTest, GetClippedBitmap_TallerThanWide) { | 249 TEST_F(ThumbnailGeneratorSimpleTest, GetClippedBitmap_TallerThanWide) { |
| 246 // The input bitmap is vertically long. | 250 // The input bitmap is vertically long. |
| 247 gfx::CanvasSkia canvas(40, 90, true); | 251 gfx::CanvasSkia canvas(40, 90, true); |
| 248 const SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); | 252 const SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); |
| 249 | 253 |
| 250 // The desired size is square. | 254 // The desired size is square. |
| 251 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; | 255 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; |
| 252 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( | 256 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( |
| 253 bitmap, 10, 10, &clip_result); | 257 bitmap, 10, 10, &clip_result); |
| 254 // The clipped bitmap should be square. | 258 // The clipped bitmap should be square. |
| 255 EXPECT_EQ(40, clipped_bitmap.width()); | 259 EXPECT_EQ(40, clipped_bitmap.width()); |
| 256 EXPECT_EQ(40, clipped_bitmap.height()); | 260 EXPECT_EQ(40, clipped_bitmap.height()); |
| 257 // The input was taller than wide. | 261 // The input was taller than wide. |
| 258 EXPECT_EQ(ThumbnailGenerator::kTallerThanWide, clip_result); | 262 EXPECT_EQ(ThumbnailGenerator::kTallerThanWide, clip_result); |
| 259 } | 263 } |
| 260 | 264 |
| 261 TEST(ThumbnailGeneratorSimpleTest, GetClippedBitmap_WiderThanTall) { | 265 TEST_F(ThumbnailGeneratorSimpleTest, GetClippedBitmap_WiderThanTall) { |
| 262 // The input bitmap is horizontally long. | 266 // The input bitmap is horizontally long. |
| 263 gfx::CanvasSkia canvas(90, 40, true); | 267 gfx::CanvasSkia canvas(90, 40, true); |
| 264 const SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); | 268 const SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); |
| 265 | 269 |
| 266 // The desired size is square. | 270 // The desired size is square. |
| 267 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; | 271 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; |
| 268 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( | 272 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( |
| 269 bitmap, 10, 10, &clip_result); | 273 bitmap, 10, 10, &clip_result); |
| 270 // The clipped bitmap should be square. | 274 // The clipped bitmap should be square. |
| 271 EXPECT_EQ(40, clipped_bitmap.width()); | 275 EXPECT_EQ(40, clipped_bitmap.width()); |
| 272 EXPECT_EQ(40, clipped_bitmap.height()); | 276 EXPECT_EQ(40, clipped_bitmap.height()); |
| 273 // The input was wider than tall. | 277 // The input was wider than tall. |
| 274 EXPECT_EQ(ThumbnailGenerator::kWiderThanTall, clip_result); | 278 EXPECT_EQ(ThumbnailGenerator::kWiderThanTall, clip_result); |
| 275 } | 279 } |
| 276 | 280 |
| 277 TEST(ThumbnailGeneratorSimpleTest, GetClippedBitmap_NotClipped) { | 281 TEST_F(ThumbnailGeneratorSimpleTest, GetClippedBitmap_NotClipped) { |
| 278 // The input bitmap is square. | 282 // The input bitmap is square. |
| 279 gfx::CanvasSkia canvas(40, 40, true); | 283 gfx::CanvasSkia canvas(40, 40, true); |
| 280 const SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); | 284 const SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); |
| 281 | 285 |
| 282 // The desired size is square. | 286 // The desired size is square. |
| 283 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; | 287 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; |
| 284 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( | 288 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( |
| 285 bitmap, 10, 10, &clip_result); | 289 bitmap, 10, 10, &clip_result); |
| 286 // The clipped bitmap should be square. | 290 // The clipped bitmap should be square. |
| 287 EXPECT_EQ(40, clipped_bitmap.width()); | 291 EXPECT_EQ(40, clipped_bitmap.width()); |
| 288 EXPECT_EQ(40, clipped_bitmap.height()); | 292 EXPECT_EQ(40, clipped_bitmap.height()); |
| 289 // There was no need to clip. | 293 // There was no need to clip. |
| 290 EXPECT_EQ(ThumbnailGenerator::kNotClipped, clip_result); | 294 EXPECT_EQ(ThumbnailGenerator::kNotClipped, clip_result); |
| 291 } | 295 } |
| 292 | 296 |
| 293 TEST(ThumbnailGeneratorSimpleTest, GetClippedBitmap_NonSquareOutput) { | 297 TEST_F(ThumbnailGeneratorSimpleTest, GetClippedBitmap_NonSquareOutput) { |
| 294 // The input bitmap is square. | 298 // The input bitmap is square. |
| 295 gfx::CanvasSkia canvas(40, 40, true); | 299 gfx::CanvasSkia canvas(40, 40, true); |
| 296 const SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); | 300 const SkBitmap bitmap = skia::GetTopDevice(canvas)->accessBitmap(false); |
| 297 | 301 |
| 298 // The desired size is horizontally long. | 302 // The desired size is horizontally long. |
| 299 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; | 303 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; |
| 300 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( | 304 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( |
| 301 bitmap, 20, 10, &clip_result); | 305 bitmap, 20, 10, &clip_result); |
| 302 // The clipped bitmap should have the same aspect ratio of the desired size. | 306 // The clipped bitmap should have the same aspect ratio of the desired size. |
| 303 EXPECT_EQ(40, clipped_bitmap.width()); | 307 EXPECT_EQ(40, clipped_bitmap.width()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 void AddKnownURL(const GURL& url, const ThumbnailScore& score) { | 340 void AddKnownURL(const GURL& url, const ThumbnailScore& score) { |
| 337 known_url_map_[url.spec()] = score; | 341 known_url_map_[url.spec()] = score; |
| 338 } | 342 } |
| 339 | 343 |
| 340 private: | 344 private: |
| 341 virtual ~MockTopSites() {} | 345 virtual ~MockTopSites() {} |
| 342 size_t capacity_; | 346 size_t capacity_; |
| 343 std::map<std::string, ThumbnailScore> known_url_map_; | 347 std::map<std::string, ThumbnailScore> known_url_map_; |
| 344 }; | 348 }; |
| 345 | 349 |
| 346 TEST(ThumbnailGeneratorSimpleTest, ShouldUpdateThumbnail) { | 350 TEST_F(ThumbnailGeneratorSimpleTest, ShouldUpdateThumbnail) { |
| 347 const GURL kGoodURL("http://www.google.com/"); | 351 const GURL kGoodURL("http://www.google.com/"); |
| 348 const GURL kBadURL("chrome://newtab"); | 352 const GURL kBadURL("chrome://newtab"); |
| 349 | 353 |
| 350 // Set up the profile. | 354 // Set up the profile. |
| 351 TestingProfile profile; | 355 TestingProfile profile; |
| 352 | 356 |
| 353 // Set up the top sites service. | 357 // Set up the top sites service. |
| 354 ScopedTempDir temp_dir; | 358 ScopedTempDir temp_dir; |
| 355 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 359 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 356 scoped_refptr<MockTopSites> top_sites(new MockTopSites(&profile)); | 360 scoped_refptr<MockTopSites> top_sites(new MockTopSites(&profile)); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 396 good_score.good_clipping = true; | 400 good_score.good_clipping = true; |
| 397 good_score.boring_score = 0.0; | 401 good_score.boring_score = 0.0; |
| 398 good_score.load_completed = true; | 402 good_score.load_completed = true; |
| 399 top_sites->AddKnownURL(kGoodURL, good_score); | 403 top_sites->AddKnownURL(kGoodURL, good_score); |
| 400 | 404 |
| 401 // Should be false, as the existing thumbnail is good enough (i.e. don't | 405 // Should be false, as the existing thumbnail is good enough (i.e. don't |
| 402 // need to replace the existing thumbnail which is new and good). | 406 // need to replace the existing thumbnail which is new and good). |
| 403 EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail( | 407 EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail( |
| 404 &profile, top_sites.get(), kGoodURL)); | 408 &profile, top_sites.get(), kGoodURL)); |
| 405 } | 409 } |
| OLD | NEW |