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 "content/public/browser/notification_service.h"
11 #include "content/public/browser/notification_types.h"
12 #include "content/public/test/mock_render_process_host.h"
13 #include "content/public/test/test_renderer_host.h"
14 #include "skia/ext/platform_canvas.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/skia/include/core/SkColorPriv.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/geometry/size_conversions.h"
19 #include "ui/surface/transport_dib.h"
21 using content::WebContents
;
22 using thumbnails::SimpleThumbnailCrop
;
24 typedef testing::Test SimpleThumbnailCropTest
;
26 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_TallerThanWide
) {
27 // The input bitmap is vertically long.
28 gfx::Canvas
canvas(gfx::Size(40, 90), 1.0f
, true);
30 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
32 // The desired size is square.
33 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
34 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
35 bitmap
, 10, 10, &clip_result
);
36 // The clipped bitmap should be square.
37 EXPECT_EQ(40, clipped_bitmap
.width());
38 EXPECT_EQ(40, clipped_bitmap
.height());
39 // The input was taller than wide.
40 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE
, clip_result
);
43 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_WiderThanTall
) {
44 // The input bitmap is horizontally long.
45 gfx::Canvas
canvas(gfx::Size(70, 40), 1.0f
, true);
47 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
49 // The desired size is square.
50 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
51 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
52 bitmap
, 10, 10, &clip_result
);
53 // The clipped bitmap should be square.
54 EXPECT_EQ(40, clipped_bitmap
.width());
55 EXPECT_EQ(40, clipped_bitmap
.height());
56 // The input was wider than tall.
57 EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL
, clip_result
);
60 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_TooWiderThanTall
) {
61 // The input bitmap is horizontally very long.
62 gfx::Canvas
canvas(gfx::Size(90, 40), 1.0f
, true);
64 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
66 // The desired size is square.
67 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
68 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
69 bitmap
, 10, 10, &clip_result
);
70 // The clipped bitmap should be square.
71 EXPECT_EQ(40, clipped_bitmap
.width());
72 EXPECT_EQ(40, clipped_bitmap
.height());
73 // The input was wider than tall.
74 EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL
, clip_result
);
77 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_NotClipped
) {
78 // The input bitmap is square.
79 gfx::Canvas
canvas(gfx::Size(40, 40), 1.0f
, true);
81 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
83 // The desired size is square.
84 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
85 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
86 bitmap
, 10, 10, &clip_result
);
87 // The clipped bitmap should be square.
88 EXPECT_EQ(40, clipped_bitmap
.width());
89 EXPECT_EQ(40, clipped_bitmap
.height());
90 // There was no need to clip.
91 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED
, clip_result
);
94 TEST_F(SimpleThumbnailCropTest
, GetClippedBitmap_NonSquareOutput
) {
95 // The input bitmap is square.
96 gfx::Canvas
canvas(gfx::Size(40, 40), 1.0f
, true);
98 skia::GetTopDevice(*canvas
.sk_canvas())->accessBitmap(false);
100 // The desired size is horizontally long.
101 thumbnails::ClipResult clip_result
= thumbnails::CLIP_RESULT_NOT_CLIPPED
;
102 SkBitmap clipped_bitmap
= SimpleThumbnailCrop::GetClippedBitmap(
103 bitmap
, 20, 10, &clip_result
);
104 // The clipped bitmap should have the same aspect ratio of the desired size.
105 EXPECT_EQ(40, clipped_bitmap
.width());
106 EXPECT_EQ(20, clipped_bitmap
.height());
107 // The input was taller than wide.
108 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE
, clip_result
);
111 TEST_F(SimpleThumbnailCropTest
, GetCanvasCopyInfo
) {
112 gfx::Size
thumbnail_size(200, 120);
113 gfx::Size expected_2x_size
=
114 gfx::ToFlooredSize(gfx::ScaleSize(thumbnail_size
, 2.0));
115 float desired_aspect
=
116 static_cast<float>(thumbnail_size
.width()) / thumbnail_size
.height();
117 scoped_refptr
<thumbnails::ThumbnailingAlgorithm
> algorithm(
118 new SimpleThumbnailCrop(thumbnail_size
));
119 gfx::Rect clipping_rect_result
;
120 gfx::Size target_size_result
;
122 thumbnails::ClipResult clip_result
= algorithm
->GetCanvasCopyInfo(
124 ui::SCALE_FACTOR_200P
,
125 &clipping_rect_result
,
126 &target_size_result
);
127 gfx::Size clipping_size
= clipping_rect_result
.size();
129 static_cast<float>(clipping_size
.width()) / clipping_size
.height();
130 EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL
, clip_result
);
131 EXPECT_EQ(expected_2x_size
, target_size_result
);
132 EXPECT_NEAR(desired_aspect
, clip_aspect
, 0.01);
134 clip_result
= algorithm
->GetCanvasCopyInfo(
136 ui::SCALE_FACTOR_200P
,
137 &clipping_rect_result
,
138 &target_size_result
);
139 clipping_size
= clipping_rect_result
.size();
141 static_cast<float>(clipping_size
.width()) / clipping_size
.height();
142 EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL
, clip_result
);
143 EXPECT_EQ(expected_2x_size
, target_size_result
);
144 EXPECT_NEAR(desired_aspect
, clip_aspect
, 0.01);
146 clip_result
= algorithm
->GetCanvasCopyInfo(
148 ui::SCALE_FACTOR_200P
,
149 &clipping_rect_result
,
150 &target_size_result
);
151 clipping_size
= clipping_rect_result
.size();
153 static_cast<float>(clipping_size
.width()) / clipping_size
.height();
154 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE
, clip_result
);
155 EXPECT_EQ(expected_2x_size
, target_size_result
);
156 EXPECT_NEAR(desired_aspect
, clip_aspect
, 0.01);
158 clip_result
= algorithm
->GetCanvasCopyInfo(
160 ui::SCALE_FACTOR_200P
,
161 &clipping_rect_result
,
162 &target_size_result
);
163 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER
, clip_result
);
164 EXPECT_EQ(expected_2x_size
, target_size_result
);
167 TEST_F(SimpleThumbnailCropTest
, GetCanvasCopyInfoDifferentScales
) {
168 gfx::Size
thumbnail_size(200, 120);
169 scoped_refptr
<thumbnails::ThumbnailingAlgorithm
> algorithm(
170 new SimpleThumbnailCrop(thumbnail_size
));
172 gfx::Rect clipping_rect_result
;
173 gfx::Size target_size_result
;
175 gfx::Size expected_2x_size
=
176 gfx::ToFlooredSize(gfx::ScaleSize(thumbnail_size
, 2.0));
178 // Test at 1x scale. Expect a 2x thumbnail (we do this for quality).
179 algorithm
->GetCanvasCopyInfo(gfx::Size(400, 210), ui::SCALE_FACTOR_100P
,
180 &clipping_rect_result
, &target_size_result
);
181 EXPECT_EQ(expected_2x_size
, target_size_result
);
184 algorithm
->GetCanvasCopyInfo(gfx::Size(400, 210), ui::SCALE_FACTOR_200P
,
185 &clipping_rect_result
, &target_size_result
);
186 EXPECT_EQ(expected_2x_size
, target_size_result
);
189 gfx::Size expected_3x_size
=
190 gfx::ToFlooredSize(gfx::ScaleSize(thumbnail_size
, 3.0));
191 algorithm
->GetCanvasCopyInfo(gfx::Size(400, 210), ui::SCALE_FACTOR_300P
,
192 &clipping_rect_result
, &target_size_result
);
193 EXPECT_EQ(expected_3x_size
, target_size_result
);
196 TEST_F(SimpleThumbnailCropTest
, GetClippingRect
) {
197 const gfx::Size
desired_size(300, 200);
198 thumbnails::ClipResult clip_result
;
199 // Try out 'microsource'.
200 gfx::Rect clip_rect
= SimpleThumbnailCrop::GetClippingRect(
201 gfx::Size(300, 199), desired_size
, &clip_result
);
202 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER
, clip_result
);
203 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect
.origin().ToString());
204 EXPECT_EQ(desired_size
.ToString(), clip_rect
.size().ToString());
207 clip_rect
= SimpleThumbnailCrop::GetClippingRect(
208 gfx::Size(500, 1200), desired_size
, &clip_result
);
209 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE
, clip_result
);
210 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect
.origin().ToString());
211 EXPECT_EQ(500, clip_rect
.width());
212 EXPECT_GE(1200, clip_rect
.height());
214 clip_rect
= SimpleThumbnailCrop::GetClippingRect(
215 gfx::Size(2000, 800), desired_size
, &clip_result
);
216 EXPECT_TRUE(clip_result
== thumbnails::CLIP_RESULT_WIDER_THAN_TALL
||
217 clip_result
== thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL
);
218 EXPECT_EQ(0, clip_rect
.y());
219 EXPECT_LT(0, clip_rect
.x());
220 EXPECT_GE(2000, clip_rect
.width());
221 EXPECT_EQ(800, clip_rect
.height());
223 clip_rect
= SimpleThumbnailCrop::GetClippingRect(
224 gfx::Size(900, 600), desired_size
, &clip_result
);
225 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED
, clip_result
);
226 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect
.origin().ToString());
227 EXPECT_EQ(gfx::Size(900, 600).ToString(), clip_rect
.size().ToString());