| OLD | NEW |
| 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/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/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 typedef testing::Test ThumbnailGeneratorTest; | 23 typedef testing::Test ThumbnailGeneratorTest; |
| 24 | 24 |
| 25 TEST_F(ThumbnailGeneratorTest, CalculateBoringScore_Empty) { | 25 TEST_F(ThumbnailGeneratorTest, CalculateBoringScore_Empty) { |
| 26 SkBitmap bitmap; | 26 SkBitmap bitmap; |
| 27 EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(bitmap)); | 27 EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(bitmap)); |
| 28 } | 28 } |
| 29 | 29 |
| 30 TEST_F(ThumbnailGeneratorTest, CalculateBoringScore_SingleColor) { | 30 TEST_F(ThumbnailGeneratorTest, CalculateBoringScore_SingleColor) { |
| 31 const gfx::Size kSize(20, 10); | 31 const gfx::Size kSize(20, 10); |
| 32 gfx::Canvas canvas(kSize, true); | 32 gfx::Canvas canvas(kSize, ui::SCALE_FACTOR_100P, true); |
| 33 // Fill all pixesl in black. | 33 // Fill all pixesl in black. |
| 34 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); | 34 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); |
| 35 | 35 |
| 36 SkBitmap bitmap = | 36 SkBitmap bitmap = |
| 37 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 37 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| 38 // The thumbnail should deserve the highest boring score. | 38 // The thumbnail should deserve the highest boring score. |
| 39 EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(bitmap)); | 39 EXPECT_DOUBLE_EQ(1.0, ThumbnailGenerator::CalculateBoringScore(bitmap)); |
| 40 } | 40 } |
| 41 | 41 |
| 42 TEST_F(ThumbnailGeneratorTest, CalculateBoringScore_TwoColors) { | 42 TEST_F(ThumbnailGeneratorTest, CalculateBoringScore_TwoColors) { |
| 43 const gfx::Size kSize(20, 10); | 43 const gfx::Size kSize(20, 10); |
| 44 | 44 |
| 45 gfx::Canvas canvas(kSize, true); | 45 gfx::Canvas canvas(kSize, ui::SCALE_FACTOR_100P, true); |
| 46 // Fill all pixesl in black. | 46 // Fill all pixesl in black. |
| 47 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); | 47 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); |
| 48 // Fill the left half pixels in white. | 48 // Fill the left half pixels in white. |
| 49 canvas.FillRect(gfx::Rect(0, 0, kSize.width() / 2, kSize.height()), | 49 canvas.FillRect(gfx::Rect(0, 0, kSize.width() / 2, kSize.height()), |
| 50 SK_ColorWHITE); | 50 SK_ColorWHITE); |
| 51 | 51 |
| 52 SkBitmap bitmap = | 52 SkBitmap bitmap = |
| 53 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 53 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| 54 ASSERT_EQ(kSize.width(), bitmap.width()); | 54 ASSERT_EQ(kSize.width(), bitmap.width()); |
| 55 ASSERT_EQ(kSize.height(), bitmap.height()); | 55 ASSERT_EQ(kSize.height(), bitmap.height()); |
| 56 // The thumbnail should be less boring because two colors are used. | 56 // The thumbnail should be less boring because two colors are used. |
| 57 EXPECT_DOUBLE_EQ(0.5, ThumbnailGenerator::CalculateBoringScore(bitmap)); | 57 EXPECT_DOUBLE_EQ(0.5, ThumbnailGenerator::CalculateBoringScore(bitmap)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_TallerThanWide) { | 60 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_TallerThanWide) { |
| 61 // The input bitmap is vertically long. | 61 // The input bitmap is vertically long. |
| 62 gfx::Canvas canvas(gfx::Size(40, 90), true); | 62 gfx::Canvas canvas(gfx::Size(40, 90), ui::SCALE_FACTOR_100P, true); |
| 63 SkBitmap bitmap = | 63 SkBitmap bitmap = |
| 64 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 64 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| 65 | 65 |
| 66 // The desired size is square. | 66 // The desired size is square. |
| 67 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; | 67 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; |
| 68 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( | 68 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( |
| 69 bitmap, 10, 10, &clip_result); | 69 bitmap, 10, 10, &clip_result); |
| 70 // The clipped bitmap should be square. | 70 // The clipped bitmap should be square. |
| 71 EXPECT_EQ(40, clipped_bitmap.width()); | 71 EXPECT_EQ(40, clipped_bitmap.width()); |
| 72 EXPECT_EQ(40, clipped_bitmap.height()); | 72 EXPECT_EQ(40, clipped_bitmap.height()); |
| 73 // The input was taller than wide. | 73 // The input was taller than wide. |
| 74 EXPECT_EQ(ThumbnailGenerator::kTallerThanWide, clip_result); | 74 EXPECT_EQ(ThumbnailGenerator::kTallerThanWide, clip_result); |
| 75 } | 75 } |
| 76 | 76 |
| 77 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_WiderThanTall) { | 77 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_WiderThanTall) { |
| 78 // The input bitmap is horizontally long. | 78 // The input bitmap is horizontally long. |
| 79 gfx::Canvas canvas(gfx::Size(70, 40), true); | 79 gfx::Canvas canvas(gfx::Size(70, 40), ui::SCALE_FACTOR_100P, true); |
| 80 SkBitmap bitmap = | 80 SkBitmap bitmap = |
| 81 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 81 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| 82 | 82 |
| 83 // The desired size is square. | 83 // The desired size is square. |
| 84 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; | 84 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; |
| 85 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( | 85 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( |
| 86 bitmap, 10, 10, &clip_result); | 86 bitmap, 10, 10, &clip_result); |
| 87 // The clipped bitmap should be square. | 87 // The clipped bitmap should be square. |
| 88 EXPECT_EQ(40, clipped_bitmap.width()); | 88 EXPECT_EQ(40, clipped_bitmap.width()); |
| 89 EXPECT_EQ(40, clipped_bitmap.height()); | 89 EXPECT_EQ(40, clipped_bitmap.height()); |
| 90 // The input was wider than tall. | 90 // The input was wider than tall. |
| 91 EXPECT_EQ(ThumbnailGenerator::kWiderThanTall, clip_result); | 91 EXPECT_EQ(ThumbnailGenerator::kWiderThanTall, clip_result); |
| 92 } | 92 } |
| 93 | 93 |
| 94 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_TooWiderThanTall) { | 94 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_TooWiderThanTall) { |
| 95 // The input bitmap is horizontally very long. | 95 // The input bitmap is horizontally very long. |
| 96 gfx::Canvas canvas(gfx::Size(90, 40), true); | 96 gfx::Canvas canvas(gfx::Size(90, 40), ui::SCALE_FACTOR_100P, true); |
| 97 SkBitmap bitmap = | 97 SkBitmap bitmap = |
| 98 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 98 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| 99 | 99 |
| 100 // The desired size is square. | 100 // The desired size is square. |
| 101 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; | 101 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; |
| 102 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( | 102 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( |
| 103 bitmap, 10, 10, &clip_result); | 103 bitmap, 10, 10, &clip_result); |
| 104 // The clipped bitmap should be square. | 104 // The clipped bitmap should be square. |
| 105 EXPECT_EQ(40, clipped_bitmap.width()); | 105 EXPECT_EQ(40, clipped_bitmap.width()); |
| 106 EXPECT_EQ(40, clipped_bitmap.height()); | 106 EXPECT_EQ(40, clipped_bitmap.height()); |
| 107 // The input was wider than tall. | 107 // The input was wider than tall. |
| 108 EXPECT_EQ(ThumbnailGenerator::kTooWiderThanTall, clip_result); | 108 EXPECT_EQ(ThumbnailGenerator::kTooWiderThanTall, clip_result); |
| 109 } | 109 } |
| 110 | 110 |
| 111 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_NotClipped) { | 111 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_NotClipped) { |
| 112 // The input bitmap is square. | 112 // The input bitmap is square. |
| 113 gfx::Canvas canvas(gfx::Size(40, 40), true); | 113 gfx::Canvas canvas(gfx::Size(40, 40), ui::SCALE_FACTOR_100P, true); |
| 114 SkBitmap bitmap = | 114 SkBitmap bitmap = |
| 115 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 115 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| 116 | 116 |
| 117 // The desired size is square. | 117 // The desired size is square. |
| 118 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; | 118 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; |
| 119 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( | 119 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( |
| 120 bitmap, 10, 10, &clip_result); | 120 bitmap, 10, 10, &clip_result); |
| 121 // The clipped bitmap should be square. | 121 // The clipped bitmap should be square. |
| 122 EXPECT_EQ(40, clipped_bitmap.width()); | 122 EXPECT_EQ(40, clipped_bitmap.width()); |
| 123 EXPECT_EQ(40, clipped_bitmap.height()); | 123 EXPECT_EQ(40, clipped_bitmap.height()); |
| 124 // There was no need to clip. | 124 // There was no need to clip. |
| 125 EXPECT_EQ(ThumbnailGenerator::kNotClipped, clip_result); | 125 EXPECT_EQ(ThumbnailGenerator::kNotClipped, clip_result); |
| 126 } | 126 } |
| 127 | 127 |
| 128 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_NonSquareOutput) { | 128 TEST_F(ThumbnailGeneratorTest, GetClippedBitmap_NonSquareOutput) { |
| 129 // The input bitmap is square. | 129 // The input bitmap is square. |
| 130 gfx::Canvas canvas(gfx::Size(40, 40), true); | 130 gfx::Canvas canvas(gfx::Size(40, 40), ui::SCALE_FACTOR_100P, true); |
| 131 SkBitmap bitmap = | 131 SkBitmap bitmap = |
| 132 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 132 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| 133 | 133 |
| 134 // The desired size is horizontally long. | 134 // The desired size is horizontally long. |
| 135 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; | 135 ThumbnailGenerator::ClipResult clip_result = ThumbnailGenerator::kNotClipped; |
| 136 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( | 136 SkBitmap clipped_bitmap = ThumbnailGenerator::GetClippedBitmap( |
| 137 bitmap, 20, 10, &clip_result); | 137 bitmap, 20, 10, &clip_result); |
| 138 // The clipped bitmap should have the same aspect ratio of the desired size. | 138 // The clipped bitmap should have the same aspect ratio of the desired size. |
| 139 EXPECT_EQ(40, clipped_bitmap.width()); | 139 EXPECT_EQ(40, clipped_bitmap.width()); |
| 140 EXPECT_EQ(20, clipped_bitmap.height()); | 140 EXPECT_EQ(20, clipped_bitmap.height()); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 good_score.good_clipping = true; | 232 good_score.good_clipping = true; |
| 233 good_score.boring_score = 0.0; | 233 good_score.boring_score = 0.0; |
| 234 good_score.load_completed = true; | 234 good_score.load_completed = true; |
| 235 top_sites->AddKnownURL(kGoodURL, good_score); | 235 top_sites->AddKnownURL(kGoodURL, good_score); |
| 236 | 236 |
| 237 // Should be false, as the existing thumbnail is good enough (i.e. don't | 237 // Should be false, as the existing thumbnail is good enough (i.e. don't |
| 238 // need to replace the existing thumbnail which is new and good). | 238 // need to replace the existing thumbnail which is new and good). |
| 239 EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail( | 239 EXPECT_FALSE(ThumbnailGenerator::ShouldUpdateThumbnail( |
| 240 &profile, top_sites.get(), kGoodURL)); | 240 &profile, top_sites.get(), kGoodURL)); |
| 241 } | 241 } |
| OLD | NEW |