- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / thumbnails / simple_thumbnail_crop_unittest.cc
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.
4
5 #include "chrome/browser/thumbnails/simple_thumbnail_crop.h"
6
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/surface/transport_dib.h"
20
21 using content::WebContents;
22 using thumbnails::SimpleThumbnailCrop;
23
24 typedef testing::Test SimpleThumbnailCropTest;
25
26 TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_Empty) {
27   SkBitmap bitmap;
28   EXPECT_DOUBLE_EQ(1.0, SimpleThumbnailCrop::CalculateBoringScore(bitmap));
29 }
30
31 TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_SingleColor) {
32   const gfx::Size kSize(20, 10);
33   gfx::Canvas canvas(kSize, 1.0f, true);
34   // Fill all pixels in black.
35   canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK);
36
37   SkBitmap bitmap =
38       skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
39   // The thumbnail should deserve the highest boring score.
40   EXPECT_DOUBLE_EQ(1.0, SimpleThumbnailCrop::CalculateBoringScore(bitmap));
41 }
42
43 TEST_F(SimpleThumbnailCropTest, CalculateBoringScore_TwoColors) {
44   const gfx::Size kSize(20, 10);
45
46   gfx::Canvas canvas(kSize, 1.0f, true);
47   // Fill all pixels in black.
48   canvas.FillRect(gfx::Rect(kSize), SK_ColorBLACK);
49   // Fill the left half pixels in white.
50   canvas.FillRect(gfx::Rect(0, 0, kSize.width() / 2, kSize.height()),
51                   SK_ColorWHITE);
52
53   SkBitmap bitmap =
54       skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
55   ASSERT_EQ(kSize.width(), bitmap.width());
56   ASSERT_EQ(kSize.height(), bitmap.height());
57   // The thumbnail should be less boring because two colors are used.
58   EXPECT_DOUBLE_EQ(0.5, SimpleThumbnailCrop::CalculateBoringScore(bitmap));
59 }
60
61 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_TallerThanWide) {
62   // The input bitmap is vertically long.
63   gfx::Canvas canvas(gfx::Size(40, 90), 1.0f, true);
64   SkBitmap bitmap =
65       skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
66
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 taller than wide.
75   EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result);
76 }
77
78 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_WiderThanTall) {
79   // The input bitmap is horizontally long.
80   gfx::Canvas canvas(gfx::Size(70, 40), 1.0f, true);
81   SkBitmap bitmap =
82       skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
83
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   // The input was wider than tall.
92   EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL, clip_result);
93 }
94
95 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_TooWiderThanTall) {
96   // The input bitmap is horizontally very long.
97   gfx::Canvas canvas(gfx::Size(90, 40), 1.0f, true);
98   SkBitmap bitmap =
99       skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
100
101   // The desired size is square.
102   thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED;
103   SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap(
104       bitmap, 10, 10, &clip_result);
105   // The clipped bitmap should be square.
106   EXPECT_EQ(40, clipped_bitmap.width());
107   EXPECT_EQ(40, clipped_bitmap.height());
108   // The input was wider than tall.
109   EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL, clip_result);
110 }
111
112 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_NotClipped) {
113   // The input bitmap is square.
114   gfx::Canvas canvas(gfx::Size(40, 40), 1.0f, true);
115   SkBitmap bitmap =
116       skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
117
118   // The desired size is square.
119   thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED;
120   SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap(
121       bitmap, 10, 10, &clip_result);
122   // The clipped bitmap should be square.
123   EXPECT_EQ(40, clipped_bitmap.width());
124   EXPECT_EQ(40, clipped_bitmap.height());
125   // There was no need to clip.
126   EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED, clip_result);
127 }
128
129 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_NonSquareOutput) {
130   // The input bitmap is square.
131   gfx::Canvas canvas(gfx::Size(40, 40), 1.0f, true);
132   SkBitmap bitmap =
133       skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
134
135   // The desired size is horizontally long.
136   thumbnails::ClipResult clip_result = thumbnails::CLIP_RESULT_NOT_CLIPPED;
137   SkBitmap clipped_bitmap = SimpleThumbnailCrop::GetClippedBitmap(
138       bitmap, 20, 10, &clip_result);
139   // The clipped bitmap should have the same aspect ratio of the desired size.
140   EXPECT_EQ(40, clipped_bitmap.width());
141   EXPECT_EQ(20, clipped_bitmap.height());
142   // The input was taller than wide.
143   EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result);
144 }
145
146 TEST_F(SimpleThumbnailCropTest, GetCanvasCopyInfo) {
147   gfx::Size thumbnail_size(200, 120);
148   float desired_aspect =
149       static_cast<float>(thumbnail_size.width()) / thumbnail_size.height();
150   scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm(
151       new SimpleThumbnailCrop(thumbnail_size));
152   gfx::Rect clipping_rect_result;
153   gfx::Size target_size_result;
154
155   thumbnails::ClipResult clip_result = algorithm->GetCanvasCopyInfo(
156       gfx::Size(400, 210),
157       ui::SCALE_FACTOR_200P,
158       &clipping_rect_result,
159       &target_size_result);
160   gfx::Size clipping_size = clipping_rect_result.size();
161   float clip_aspect =
162       static_cast<float>(clipping_size.width()) / clipping_size.height();
163   EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL, clip_result);
164   EXPECT_EQ(thumbnail_size, target_size_result);
165   EXPECT_NEAR(desired_aspect, clip_aspect, 0.01);
166
167   clip_result = algorithm->GetCanvasCopyInfo(
168       gfx::Size(600, 200),
169       ui::SCALE_FACTOR_200P,
170       &clipping_rect_result,
171       &target_size_result);
172   clipping_size = clipping_rect_result.size();
173   clip_aspect =
174       static_cast<float>(clipping_size.width()) / clipping_size.height();
175   EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL, clip_result);
176   EXPECT_EQ(thumbnail_size, target_size_result);
177   EXPECT_NEAR(desired_aspect, clip_aspect, 0.01);
178
179   clip_result = algorithm->GetCanvasCopyInfo(
180       gfx::Size(300, 600),
181       ui::SCALE_FACTOR_200P,
182       &clipping_rect_result,
183       &target_size_result);
184   clipping_size = clipping_rect_result.size();
185   clip_aspect =
186       static_cast<float>(clipping_size.width()) / clipping_size.height();
187   EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result);
188   EXPECT_EQ(thumbnail_size, target_size_result);
189   EXPECT_NEAR(desired_aspect, clip_aspect, 0.01);
190
191   clip_result = algorithm->GetCanvasCopyInfo(
192       gfx::Size(200, 100),
193       ui::SCALE_FACTOR_200P,
194       &clipping_rect_result,
195       &target_size_result);
196   EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER, clip_result);
197   EXPECT_EQ(thumbnail_size, target_size_result);
198 }
199
200 TEST_F(SimpleThumbnailCropTest, GetClippingRect) {
201   const gfx::Size desired_size(300, 200);
202   thumbnails::ClipResult clip_result;
203   // Try out 'microsource'.
204   gfx::Rect clip_rect = SimpleThumbnailCrop::GetClippingRect(
205       gfx::Size(300, 199), desired_size, &clip_result);
206   EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER, clip_result);
207   EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString());
208   EXPECT_EQ(desired_size.ToString(), clip_rect.size().ToString());
209
210   // Portrait source.
211   clip_rect = SimpleThumbnailCrop::GetClippingRect(
212       gfx::Size(500, 1200), desired_size, &clip_result);
213   EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result);
214   EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString());
215   EXPECT_EQ(500, clip_rect.width());
216   EXPECT_GE(1200, clip_rect.height());
217
218   clip_rect = SimpleThumbnailCrop::GetClippingRect(
219       gfx::Size(2000, 800), desired_size, &clip_result);
220   EXPECT_TRUE(clip_result == thumbnails::CLIP_RESULT_WIDER_THAN_TALL ||
221               clip_result == thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL);
222   EXPECT_EQ(0, clip_rect.y());
223   EXPECT_LT(0, clip_rect.x());
224   EXPECT_GE(2000, clip_rect.width());
225   EXPECT_EQ(800, clip_rect.height());
226
227   clip_rect = SimpleThumbnailCrop::GetClippingRect(
228       gfx::Size(900, 600), desired_size, &clip_result);
229   EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED, clip_result);
230   EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString());
231   EXPECT_EQ(gfx::Size(900, 600).ToString(), clip_rect.size().ToString());
232 }