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
3 // found in the LICENSE file.
5 #include "chrome/browser/thumbnails/simple_thumbnail_crop.h"
7 #include "base/basictypes.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/thumbnails/thumbnailing_context.h"
10 #include "chrome/common/render_messages.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/browser/notification_types.h"
13 #include "content/public/test/mock_render_process_host.h"
14 #include "content/public/test/test_renderer_host.h"
15 #include "skia/ext/platform_canvas.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkColorPriv.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/geometry/size_conversions.h"
20 #include "ui/surface/transport_dib.h"
22 using content::WebContents
;
23 using thumbnails::SimpleThumbnailCrop
;
25 typedef testing::Test SimpleThumbnailCropTest
;
27 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_TallerThanWide
) {
28 // The input bitmap is vertically long.
29 gfx::Canvas
canvas(gfx::Size(40, 90), 1.0f
, true);
31 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
33 // The desired size is square.
34 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
35 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
36 bitmap
, 10, 10, &clip_result
);
37 // The clipped bitmap should be square.
38 EXPECT_EQ(40, clipped_bitmap
.width());
39 EXPECT_EQ(40, clipped_bitmap
.height());
40 // The input was taller than wide.
41 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE
, clip_result
);
44 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_WiderThanTall
) {
45 // The input bitmap is horizontally long.
46 gfx::Canvas
canvas(gfx::Size(70, 40), 1.0f
, true);
48 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
50 // The desired size is square.
51 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
52 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
53 bitmap
, 10, 10, &clip_result
);
54 // The clipped bitmap should be square.
55 EXPECT_EQ(40, clipped_bitmap
.width());
56 EXPECT_EQ(40, clipped_bitmap
.height());
57 // The input was wider than tall.
58 EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL
, clip_result
);
61 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_TooWiderThanTall
) {
62 // The input bitmap is horizontally very long.
63 gfx::Canvas
canvas(gfx::Size(90, 40), 1.0f
, true);
65 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
67 // The desired size is square.
68 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
69 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
70 bitmap
, 10, 10, &clip_result
);
71 // The clipped bitmap should be square.
72 EXPECT_EQ(40, clipped_bitmap
.width());
73 EXPECT_EQ(40, clipped_bitmap
.height());
74 // The input was wider than tall.
75 EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL
, clip_result
);
78 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_NotClipped
) {
79 // The input bitmap is square.
80 gfx::Canvas
canvas(gfx::Size(40, 40), 1.0f
, true);
82 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
84 // The desired size is square.
85 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
86 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
87 bitmap
, 10, 10, &clip_result
);
88 // The clipped bitmap should be square.
89 EXPECT_EQ(40, clipped_bitmap
.width());
90 EXPECT_EQ(40, clipped_bitmap
.height());
91 // There was no need to clip.
92 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED
, clip_result
);
95 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_NonSquareOutput
) {
96 // The input bitmap is square.
97 gfx::Canvas
canvas(gfx::Size(40, 40), 1.0f
, true);
99 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
101 // The desired size is horizontally long.
102 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
103 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
104 bitmap
, 20, 10, &clip_result
);
105 // The clipped bitmap should have the same aspect ratio of the desired size.
106 EXPECT_EQ(40, clipped_bitmap
.width());
107 EXPECT_EQ(20, clipped_bitmap
.height());
108 // The input was taller than wide.
109 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE
, clip_result
);
112 TEST_F(SimpleThumbnailCropTest
, GetCanvasCopyInfo
) {
113 gfx::Size
thumbnail_size(200, 120);
114 gfx::Size expected_2x_size
=
115 gfx::ToFlooredSize(gfx::ScaleSize(thumbnail_size
, 2.0));
116 float desired_aspect
=
117 static_cast<float>(thumbnail_size
.width()) / thumbnail_size
.height();
118 scoped_refptr
<thumbnails::ThumbnailingAlgorithm
> algorithm(
119 new SimpleThumbnailCrop(thumbnail_size
));
120 gfx::Rect clipping_rect_result
;
121 gfx::Size target_size_result
;
123 thumbnails::ClipResult clip_result
= algorithm
->GetCanvasCopyInfo(
125 ui::SCALE_FACTOR_200P
,
126 &clipping_rect_result
,
127 &target_size_result
);
128 gfx::Size clipping_size
= clipping_rect_result
.size();
130 static_cast<float>(clipping_size
.width()) / clipping_size
.height();
131 EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL
, clip_result
);
132 EXPECT_EQ(expected_2x_size
, target_size_result
);
133 EXPECT_NEAR(desired_aspect
, clip_aspect
, 0.01);
135 clip_result
= algorithm
->GetCanvasCopyInfo(
137 ui::SCALE_FACTOR_200P
,
138 &clipping_rect_result
,
139 &target_size_result
);
140 clipping_size
= clipping_rect_result
.size();
142 static_cast<float>(clipping_size
.width()) / clipping_size
.height();
143 EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL
, clip_result
);
144 EXPECT_EQ(expected_2x_size
, target_size_result
);
145 EXPECT_NEAR(desired_aspect
, clip_aspect
, 0.01);
147 clip_result
= algorithm
->GetCanvasCopyInfo(
149 ui::SCALE_FACTOR_200P
,
150 &clipping_rect_result
,
151 &target_size_result
);
152 clipping_size
= clipping_rect_result
.size();
154 static_cast<float>(clipping_size
.width()) / clipping_size
.height();
155 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE
, clip_result
);
156 EXPECT_EQ(expected_2x_size
, target_size_result
);
157 EXPECT_NEAR(desired_aspect
, clip_aspect
, 0.01);
159 clip_result
= algorithm
->GetCanvasCopyInfo(
161 ui::SCALE_FACTOR_200P
,
162 &clipping_rect_result
,
163 &target_size_result
);
164 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER
, clip_result
);
165 EXPECT_EQ(expected_2x_size
, target_size_result
);
168 TEST_F(SimpleThumbnailCropTest
, GetCanvasCopyInfoDifferentScales
) {
169 gfx::Size
thumbnail_size(200, 120);
170 scoped_refptr
<thumbnails::ThumbnailingAlgorithm
> algorithm(
171 new SimpleThumbnailCrop(thumbnail_size
));
173 gfx::Rect clipping_rect_result
;
174 gfx::Size target_size_result
;
176 gfx::Size expected_2x_size
=
177 gfx::ToFlooredSize(gfx::ScaleSize(thumbnail_size
, 2.0));
179 // Test at 1x scale. Expect a 2x thumbnail (we do this for quality).
180 algorithm
->GetCanvasCopyInfo(gfx::Size(400, 210), ui::SCALE_FACTOR_100P
,
181 &clipping_rect_result
, &target_size_result
);
182 EXPECT_EQ(expected_2x_size
, target_size_result
);
185 algorithm
->GetCanvasCopyInfo(gfx::Size(400, 210), ui::SCALE_FACTOR_200P
,
186 &clipping_rect_result
, &target_size_result
);
187 EXPECT_EQ(expected_2x_size
, target_size_result
);
190 gfx::Size expected_3x_size
=
191 gfx::ToFlooredSize(gfx::ScaleSize(thumbnail_size
, 3.0));
192 algorithm
->GetCanvasCopyInfo(gfx::Size(400, 210), ui::SCALE_FACTOR_300P
,
193 &clipping_rect_result
, &target_size_result
);
194 EXPECT_EQ(expected_3x_size
, target_size_result
);
197 TEST_F(SimpleThumbnailCropTest
, GetClippingRect
) {
198 const gfx::Size
desired_size(300, 200);
199 thumbnails::ClipResult clip_result
;
200 // Try out 'microsource'.
201 gfx::Rect clip_rect
= SimpleThumbnailCrop::GetClippingRect(
202 gfx::Size(300, 199), desired_size
, &clip_result
);
203 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER
, clip_result
);
204 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect
.origin().ToString());
205 EXPECT_EQ(desired_size
.ToString(), clip_rect
.size().ToString());
208 clip_rect
= SimpleThumbnailCrop::GetClippingRect(
209 gfx::Size(500, 1200), desired_size
, &clip_result
);
210 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE
, clip_result
);
211 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect
.origin().ToString());
212 EXPECT_EQ(500, clip_rect
.width());
213 EXPECT_GE(1200, clip_rect
.height());
215 clip_rect
= SimpleThumbnailCrop::GetClippingRect(
216 gfx::Size(2000, 800), desired_size
, &clip_result
);
217 EXPECT_TRUE(clip_result
== thumbnails::CLIP_RESULT_WIDER_THAN_TALL
||
218 clip_result
== thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL
);
219 EXPECT_EQ(0, clip_rect
.y());
220 EXPECT_LT(0, clip_rect
.x());
221 EXPECT_GE(2000, clip_rect
.width());
222 EXPECT_EQ(800, clip_rect
.height());
224 clip_rect
= SimpleThumbnailCrop::GetClippingRect(
225 gfx::Size(900, 600), desired_size
, &clip_result
);
226 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED
, clip_result
);
227 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect
.origin().ToString());
228 EXPECT_EQ(gfx::Size(900, 600).ToString(), clip_rect
.size().ToString());