Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / thumbnails / simple_thumbnail_crop_unittest.cc
blobfc05f108fb81e164368a45e2be9cbd1b25f22d97
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/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);
29 SkBitmap bitmap =
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);
46 SkBitmap bitmap =
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);
63 SkBitmap bitmap =
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);
80 SkBitmap bitmap =
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);
97 SkBitmap bitmap =
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 float desired_aspect =
114 static_cast<float>(thumbnail_size.width()) / thumbnail_size.height();
115 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm(
116 new SimpleThumbnailCrop(thumbnail_size));
117 gfx::Rect clipping_rect_result;
118 gfx::Size target_size_result;
120 thumbnails::ClipResult clip_result = algorithm->GetCanvasCopyInfo(
121 gfx::Size(400, 210),
122 ui::SCALE_FACTOR_200P,
123 &clipping_rect_result,
124 &target_size_result);
125 gfx::Size clipping_size = clipping_rect_result.size();
126 float clip_aspect =
127 static_cast<float>(clipping_size.width()) / clipping_size.height();
128 EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL, clip_result);
129 EXPECT_EQ(thumbnail_size, target_size_result);
130 EXPECT_NEAR(desired_aspect, clip_aspect, 0.01);
132 clip_result = algorithm->GetCanvasCopyInfo(
133 gfx::Size(600, 200),
134 ui::SCALE_FACTOR_200P,
135 &clipping_rect_result,
136 &target_size_result);
137 clipping_size = clipping_rect_result.size();
138 clip_aspect =
139 static_cast<float>(clipping_size.width()) / clipping_size.height();
140 EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL, clip_result);
141 EXPECT_EQ(thumbnail_size, target_size_result);
142 EXPECT_NEAR(desired_aspect, clip_aspect, 0.01);
144 clip_result = algorithm->GetCanvasCopyInfo(
145 gfx::Size(300, 600),
146 ui::SCALE_FACTOR_200P,
147 &clipping_rect_result,
148 &target_size_result);
149 clipping_size = clipping_rect_result.size();
150 clip_aspect =
151 static_cast<float>(clipping_size.width()) / clipping_size.height();
152 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result);
153 EXPECT_EQ(thumbnail_size, target_size_result);
154 EXPECT_NEAR(desired_aspect, clip_aspect, 0.01);
156 clip_result = algorithm->GetCanvasCopyInfo(
157 gfx::Size(200, 100),
158 ui::SCALE_FACTOR_200P,
159 &clipping_rect_result,
160 &target_size_result);
161 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER, clip_result);
162 EXPECT_EQ(thumbnail_size, target_size_result);
165 TEST_F(SimpleThumbnailCropTest, GetClippingRect) {
166 const gfx::Size desired_size(300, 200);
167 thumbnails::ClipResult clip_result;
168 // Try out 'microsource'.
169 gfx::Rect clip_rect = SimpleThumbnailCrop::GetClippingRect(
170 gfx::Size(300, 199), desired_size, &clip_result);
171 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER, clip_result);
172 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString());
173 EXPECT_EQ(desired_size.ToString(), clip_rect.size().ToString());
175 // Portrait source.
176 clip_rect = SimpleThumbnailCrop::GetClippingRect(
177 gfx::Size(500, 1200), desired_size, &clip_result);
178 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result);
179 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString());
180 EXPECT_EQ(500, clip_rect.width());
181 EXPECT_GE(1200, clip_rect.height());
183 clip_rect = SimpleThumbnailCrop::GetClippingRect(
184 gfx::Size(2000, 800), desired_size, &clip_result);
185 EXPECT_TRUE(clip_result == thumbnails::CLIP_RESULT_WIDER_THAN_TALL ||
186 clip_result == thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL);
187 EXPECT_EQ(0, clip_rect.y());
188 EXPECT_LT(0, clip_rect.x());
189 EXPECT_GE(2000, clip_rect.width());
190 EXPECT_EQ(800, clip_rect.height());
192 clip_rect = SimpleThumbnailCrop::GetClippingRect(
193 gfx::Size(900, 600), desired_size, &clip_result);
194 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED, clip_result);
195 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString());
196 EXPECT_EQ(gfx::Size(900, 600).ToString(), clip_rect.size().ToString());