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 "chrome/browser/thumbnails/simple_thumbnail_crop.h" | 5 #include "chrome/browser/thumbnails/simple_thumbnail_crop.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "chrome/browser/thumbnails/thumbnailing_context.h" | 9 #include "chrome/browser/thumbnails/thumbnailing_context.h" |
10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 typedef testing::Test SimpleThumbnailCropTest; | 24 typedef testing::Test SimpleThumbnailCropTest; |
25 | 25 |
26 TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_Empty) { | 26 TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_Empty) { |
27 SkBitmap bitmap; | 27 SkBitmap bitmap; |
28 EXPECT_DOUBLE_EQ(1.0, SimpleThumbnailCrop::CalculateBoringScore(bitmap)); | 28 EXPECT_DOUBLE_EQ(1.0, SimpleThumbnailCrop::CalculateBoringScore(bitmap)); |
29 } | 29 } |
30 | 30 |
31 TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_SingleColor) { | 31 TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_SingleColor) { |
32 const gfx::Size kSize(20, 10); | 32 const gfx::Size kSize(20, 10); |
33 gfx::Canvas canvas(kSize, 1.0f, true); | 33 gfx::Canvas canvas(kSize, ui::SCALE_FACTOR_100P, true); |
34 // Fill all pixels in black. | 34 // Fill all pixels in black. |
35 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); | 35 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); |
36 | 36 |
37 SkBitmap bitmap = | 37 SkBitmap bitmap = |
38 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 38 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
39 // The thumbnail should deserve the highest boring score. | 39 // The thumbnail should deserve the highest boring score. |
40 EXPECT_DOUBLE_EQ(1.0, SimpleThumbnailCrop::CalculateBoringScore(bitmap)); | 40 EXPECT_DOUBLE_EQ(1.0, SimpleThumbnailCrop::CalculateBoringScore(bitmap)); |
41 } | 41 } |
42 | 42 |
43 TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_TwoColors) { | 43 TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_TwoColors) { |
44 const gfx::Size kSize(20, 10); | 44 const gfx::Size kSize(20, 10); |
45 | 45 |
46 gfx::Canvas canvas(kSize, 1.0f, true); | 46 gfx::Canvas canvas(kSize, ui::SCALE_FACTOR_100P, true); |
47 // Fill all pixels in black. | 47 // Fill all pixels in black. |
48 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); | 48 canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK); |
49 // Fill the left half pixels in white. | 49 // Fill the left half pixels in white. |
50 canvas.FillRect(gfx::Rect(0, 0, kSize.width() / 2, kSize.height()), | 50 canvas.FillRect(gfx::Rect(0, 0, kSize.width() / 2, kSize.height()), |
51 SK_ColorWHITE); | 51 SK_ColorWHITE); |
52 | 52 |
53 SkBitmap bitmap = | 53 SkBitmap bitmap = |
54 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 54 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
55 ASSERT_EQ(kSize.width(), bitmap.width()); | 55 ASSERT_EQ(kSize.width(), bitmap.width()); |
56 ASSERT_EQ(kSize.height(), bitmap.height()); | 56 ASSERT_EQ(kSize.height(), bitmap.height()); |
57 // The thumbnail should be less boring because two colors are used. | 57 // The thumbnail should be less boring because two colors are used. |
58 EXPECT_DOUBLE_EQ(0.5, SimpleThumbnailCrop::CalculateBoringScore(bitmap)); | 58 EXPECT_DOUBLE_EQ(0.5, SimpleThumbnailCrop::CalculateBoringScore(bitmap)); |
59 } | 59 } |
60 | 60 |
61 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_TallerThanWide) { | 61 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_TallerThanWide) { |
62 // The input bitmap is vertically long. | 62 // The input bitmap is vertically long. |
63 gfx::Canvas canvas(gfx::Size(40, 90), 1.0f, true); | 63 gfx::Canvas canvas(gfx::Size(40, 90), ui::SCALE_FACTOR_100P, true); |
64 SkBitmap bitmap = | 64 SkBitmap bitmap = |
65 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 65 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
66 | 66 |
67 // The desired size is square. | 67 // The desired size is square. |
68 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; | 68 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; |
69 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( | 69 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( |
70 bitmap, 10, 10, &clip_result); | 70 bitmap, 10, 10, &clip_result); |
71 // The clipped bitmap should be square. | 71 // The clipped bitmap should be square. |
72 EXPECT_EQ(40, clipped_bitmap.width()); | 72 EXPECT_EQ(40, clipped_bitmap.width()); |
73 EXPECT_EQ(40, clipped_bitmap.height()); | 73 EXPECT_EQ(40, clipped_bitmap.height()); |
74 // The input was taller than wide. | 74 // The input was taller than wide. |
75 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result); | 75 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result); |
76 } | 76 } |
77 | 77 |
78 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_WiderThanTall) { | 78 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_WiderThanTall) { |
79 // The input bitmap is horizontally long. | 79 // The input bitmap is horizontally long. |
80 gfx::Canvas canvas(gfx::Size(70, 40), 1.0f, true); | 80 gfx::Canvas canvas(gfx::Size(70, 40), ui::SCALE_FACTOR_100P, true); |
81 SkBitmap bitmap = | 81 SkBitmap bitmap = |
82 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 82 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
83 | 83 |
84 // The desired size is square. | 84 // The desired size is square. |
85 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; | 85 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; |
86 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( | 86 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( |
87 bitmap, 10, 10, &clip_result); | 87 bitmap, 10, 10, &clip_result); |
88 // The clipped bitmap should be square. | 88 // The clipped bitmap should be square. |
89 EXPECT_EQ(40, clipped_bitmap.width()); | 89 EXPECT_EQ(40, clipped_bitmap.width()); |
90 EXPECT_EQ(40, clipped_bitmap.height()); | 90 EXPECT_EQ(40, clipped_bitmap.height()); |
91 // The input was wider than tall. | 91 // The input was wider than tall. |
92 EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL, clip_result); | 92 EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL, clip_result); |
93 } | 93 } |
94 | 94 |
95 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_TooWiderThanTall) { | 95 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_TooWiderThanTall) { |
96 // The input bitmap is horizontally very long. | 96 // The input bitmap is horizontally very long. |
97 gfx::Canvas canvas(gfx::Size(90, 40), 1.0f, true); | 97 gfx::Canvas canvas(gfx::Size(90, 40), ui::SCALE_FACTOR_100P, true); |
98 SkBitmap bitmap = | 98 SkBitmap bitmap = |
99 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 99 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
100 | 100 |
101 // The desired size is square. | 101 // The desired size is square. |
102 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; | 102 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; |
103 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( | 103 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( |
104 bitmap, 10, 10, &clip_result); | 104 bitmap, 10, 10, &clip_result); |
105 // The clipped bitmap should be square. | 105 // The clipped bitmap should be square. |
106 EXPECT_EQ(40, clipped_bitmap.width()); | 106 EXPECT_EQ(40, clipped_bitmap.width()); |
107 EXPECT_EQ(40, clipped_bitmap.height()); | 107 EXPECT_EQ(40, clipped_bitmap.height()); |
108 // The input was wider than tall. | 108 // The input was wider than tall. |
109 EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL, clip_result); | 109 EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL, clip_result); |
110 } | 110 } |
111 | 111 |
112 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_NotClipped) { | 112 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_NotClipped) { |
113 // The input bitmap is square. | 113 // The input bitmap is square. |
114 gfx::Canvas canvas(gfx::Size(40, 40), 1.0f, true); | 114 gfx::Canvas canvas(gfx::Size(40, 40), ui::SCALE_FACTOR_100P, true); |
115 SkBitmap bitmap = | 115 SkBitmap bitmap = |
116 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 116 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
117 | 117 |
118 // The desired size is square. | 118 // The desired size is square. |
119 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; | 119 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; |
120 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( | 120 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( |
121 bitmap, 10, 10, &clip_result); | 121 bitmap, 10, 10, &clip_result); |
122 // The clipped bitmap should be square. | 122 // The clipped bitmap should be square. |
123 EXPECT_EQ(40, clipped_bitmap.width()); | 123 EXPECT_EQ(40, clipped_bitmap.width()); |
124 EXPECT_EQ(40, clipped_bitmap.height()); | 124 EXPECT_EQ(40, clipped_bitmap.height()); |
125 // There was no need to clip. | 125 // There was no need to clip. |
126 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED, clip_result); | 126 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED, clip_result); |
127 } | 127 } |
128 | 128 |
129 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_NonSquareOutput) { | 129 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_NonSquareOutput) { |
130 // The input bitmap is square. | 130 // The input bitmap is square. |
131 gfx::Canvas canvas(gfx::Size(40, 40), 1.0f, true); | 131 gfx::Canvas canvas(gfx::Size(40, 40), ui::SCALE_FACTOR_100P, true); |
132 SkBitmap bitmap = | 132 SkBitmap bitmap = |
133 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); | 133 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
134 | 134 |
135 // The desired size is horizontally long. | 135 // The desired size is horizontally long. |
136 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; | 136 thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED; |
137 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( | 137 SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap( |
138 bitmap, 20, 10, &clip_result); | 138 bitmap, 20, 10, &clip_result); |
139 // The clipped bitmap should have the same aspect ratio of the desired size. | 139 // The clipped bitmap should have the same aspect ratio of the desired size. |
140 EXPECT_EQ(40, clipped_bitmap.width()); | 140 EXPECT_EQ(40, clipped_bitmap.width()); |
141 EXPECT_EQ(20, clipped_bitmap.height()); | 141 EXPECT_EQ(20, clipped_bitmap.height()); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 EXPECT_LT(0, clip_rect.x()); | 223 EXPECT_LT(0, clip_rect.x()); |
224 EXPECT_GE(2000, clip_rect.width()); | 224 EXPECT_GE(2000, clip_rect.width()); |
225 EXPECT_EQ(800, clip_rect.height()); | 225 EXPECT_EQ(800, clip_rect.height()); |
226 | 226 |
227 clip_rect = SimpleThumbnailCrop::GetClippingRect( | 227 clip_rect = SimpleThumbnailCrop::GetClippingRect( |
228 gfx::Size(900, 600), desired_size, &clip_result); | 228 gfx::Size(900, 600), desired_size, &clip_result); |
229 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED, clip_result); | 229 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED, clip_result); |
230 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString()); | 230 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString()); |
231 EXPECT_EQ(gfx::Size(900, 600).ToString(), clip_rect.size().ToString()); | 231 EXPECT_EQ(gfx::Size(900, 600).ToString(), clip_rect.size().ToString()); |
232 } | 232 } |
OLD | NEW |