Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(667)

Side by Side Diff: chrome/browser/thumbnails/simple_thumbnail_crop_unittest.cc

Issue 1028393003: [Thumbnails] Specify copy size in Pixels, not DIPs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: correct rebase Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "content/public/browser/notification_service.h" 11 #include "content/public/browser/notification_service.h"
12 #include "content/public/browser/notification_types.h" 12 #include "content/public/browser/notification_types.h"
13 #include "content/public/test/mock_render_process_host.h" 13 #include "content/public/test/mock_render_process_host.h"
14 #include "content/public/test/test_renderer_host.h" 14 #include "content/public/test/test_renderer_host.h"
15 #include "skia/ext/platform_canvas.h" 15 #include "skia/ext/platform_canvas.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/core/SkColorPriv.h" 17 #include "third_party/skia/include/core/SkColorPriv.h"
18 #include "ui/gfx/canvas.h" 18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/geometry/size_conversions.h"
19 #include "ui/surface/transport_dib.h" 20 #include "ui/surface/transport_dib.h"
20 21
21 using content::WebContents; 22 using content::WebContents;
22 using thumbnails::SimpleThumbnailCrop; 23 using thumbnails::SimpleThumbnailCrop;
23 24
24 typedef testing::Test SimpleThumbnailCropTest; 25 typedef testing::Test SimpleThumbnailCropTest;
25 26
26 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_TallerThanWide) { 27 TEST_F(SimpleThumbnailCropTest, GetClippedBitmap_TallerThanWide) {
27 // The input bitmap is vertically long. 28 // The input bitmap is vertically long.
28 gfx::Canvas canvas(gfx::Size(40, 90), 1.0f, true); 29 gfx::Canvas canvas(gfx::Size(40, 90), 1.0f, true);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 bitmap, 20, 10, &clip_result); 104 bitmap, 20, 10, &clip_result);
104 // The clipped bitmap should have the same aspect ratio of the desired size. 105 // The clipped bitmap should have the same aspect ratio of the desired size.
105 EXPECT_EQ(40, clipped_bitmap.width()); 106 EXPECT_EQ(40, clipped_bitmap.width());
106 EXPECT_EQ(20, clipped_bitmap.height()); 107 EXPECT_EQ(20, clipped_bitmap.height());
107 // The input was taller than wide. 108 // The input was taller than wide.
108 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result); 109 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result);
109 } 110 }
110 111
111 TEST_F(SimpleThumbnailCropTest, GetCanvasCopyInfo) { 112 TEST_F(SimpleThumbnailCropTest, GetCanvasCopyInfo) {
112 gfx::Size thumbnail_size(200, 120); 113 gfx::Size thumbnail_size(200, 120);
114 gfx::Size expected_2x_size =
115 gfx::ToFlooredSize(gfx::ScaleSize(thumbnail_size, 2.0));
113 float desired_aspect = 116 float desired_aspect =
114 static_cast<float>(thumbnail_size.width()) / thumbnail_size.height(); 117 static_cast<float>(thumbnail_size.width()) / thumbnail_size.height();
115 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm( 118 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm(
116 new SimpleThumbnailCrop(thumbnail_size)); 119 new SimpleThumbnailCrop(thumbnail_size));
117 gfx::Rect clipping_rect_result; 120 gfx::Rect clipping_rect_result;
118 gfx::Size target_size_result; 121 gfx::Size target_size_result;
119 122
120 thumbnails::ClipResult clip_result = algorithm->GetCanvasCopyInfo( 123 thumbnails::ClipResult clip_result = algorithm->GetCanvasCopyInfo(
121 gfx::Size(400, 210), 124 gfx::Size(400, 210),
122 ui::SCALE_FACTOR_200P, 125 ui::SCALE_FACTOR_200P,
123 &clipping_rect_result, 126 &clipping_rect_result,
124 &target_size_result); 127 &target_size_result);
125 gfx::Size clipping_size = clipping_rect_result.size(); 128 gfx::Size clipping_size = clipping_rect_result.size();
126 float clip_aspect = 129 float clip_aspect =
127 static_cast<float>(clipping_size.width()) / clipping_size.height(); 130 static_cast<float>(clipping_size.width()) / clipping_size.height();
128 EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL, clip_result); 131 EXPECT_EQ(thumbnails::CLIP_RESULT_WIDER_THAN_TALL, clip_result);
129 EXPECT_EQ(thumbnail_size, target_size_result); 132 EXPECT_EQ(expected_2x_size, target_size_result);
130 EXPECT_NEAR(desired_aspect, clip_aspect, 0.01); 133 EXPECT_NEAR(desired_aspect, clip_aspect, 0.01);
131 134
132 clip_result = algorithm->GetCanvasCopyInfo( 135 clip_result = algorithm->GetCanvasCopyInfo(
133 gfx::Size(600, 200), 136 gfx::Size(600, 200),
134 ui::SCALE_FACTOR_200P, 137 ui::SCALE_FACTOR_200P,
135 &clipping_rect_result, 138 &clipping_rect_result,
136 &target_size_result); 139 &target_size_result);
137 clipping_size = clipping_rect_result.size(); 140 clipping_size = clipping_rect_result.size();
138 clip_aspect = 141 clip_aspect =
139 static_cast<float>(clipping_size.width()) / clipping_size.height(); 142 static_cast<float>(clipping_size.width()) / clipping_size.height();
140 EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL, clip_result); 143 EXPECT_EQ(thumbnails::CLIP_RESULT_MUCH_WIDER_THAN_TALL, clip_result);
141 EXPECT_EQ(thumbnail_size, target_size_result); 144 EXPECT_EQ(expected_2x_size, target_size_result);
142 EXPECT_NEAR(desired_aspect, clip_aspect, 0.01); 145 EXPECT_NEAR(desired_aspect, clip_aspect, 0.01);
143 146
144 clip_result = algorithm->GetCanvasCopyInfo( 147 clip_result = algorithm->GetCanvasCopyInfo(
145 gfx::Size(300, 600), 148 gfx::Size(300, 600),
146 ui::SCALE_FACTOR_200P, 149 ui::SCALE_FACTOR_200P,
147 &clipping_rect_result, 150 &clipping_rect_result,
148 &target_size_result); 151 &target_size_result);
149 clipping_size = clipping_rect_result.size(); 152 clipping_size = clipping_rect_result.size();
150 clip_aspect = 153 clip_aspect =
151 static_cast<float>(clipping_size.width()) / clipping_size.height(); 154 static_cast<float>(clipping_size.width()) / clipping_size.height();
152 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result); 155 EXPECT_EQ(thumbnails::CLIP_RESULT_TALLER_THAN_WIDE, clip_result);
153 EXPECT_EQ(thumbnail_size, target_size_result); 156 EXPECT_EQ(expected_2x_size, target_size_result);
154 EXPECT_NEAR(desired_aspect, clip_aspect, 0.01); 157 EXPECT_NEAR(desired_aspect, clip_aspect, 0.01);
155 158
156 clip_result = algorithm->GetCanvasCopyInfo( 159 clip_result = algorithm->GetCanvasCopyInfo(
157 gfx::Size(200, 100), 160 gfx::Size(200, 100),
158 ui::SCALE_FACTOR_200P, 161 ui::SCALE_FACTOR_200P,
159 &clipping_rect_result, 162 &clipping_rect_result,
160 &target_size_result); 163 &target_size_result);
161 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER, clip_result); 164 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER, clip_result);
162 EXPECT_EQ(thumbnail_size, target_size_result); 165 EXPECT_EQ(expected_2x_size, target_size_result);
166 }
167
168 TEST_F(SimpleThumbnailCropTest, GetCanvasCopyInfoDifferentScales) {
169 gfx::Size thumbnail_size(200, 120);
170 scoped_refptr<thumbnails::ThumbnailingAlgorithm> algorithm(
171 new SimpleThumbnailCrop(thumbnail_size));
172
173 gfx::Rect clipping_rect_result;
174 gfx::Size target_size_result;
175
176 gfx::Size expected_2x_size =
177 gfx::ToFlooredSize(gfx::ScaleSize(thumbnail_size, 2.0));
178
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);
183
184 // Test at 2x scale.
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);
188
189 // Test at 3x scale.
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);
163 } 195 }
164 196
165 TEST_F(SimpleThumbnailCropTest, GetClippingRect) { 197 TEST_F(SimpleThumbnailCropTest, GetClippingRect) {
166 const gfx::Size desired_size(300, 200); 198 const gfx::Size desired_size(300, 200);
167 thumbnails::ClipResult clip_result; 199 thumbnails::ClipResult clip_result;
168 // Try out 'microsource'. 200 // Try out 'microsource'.
169 gfx::Rect clip_rect = SimpleThumbnailCrop::GetClippingRect( 201 gfx::Rect clip_rect = SimpleThumbnailCrop::GetClippingRect(
170 gfx::Size(300, 199), desired_size, &clip_result); 202 gfx::Size(300, 199), desired_size, &clip_result);
171 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER, clip_result); 203 EXPECT_EQ(thumbnails::CLIP_RESULT_SOURCE_IS_SMALLER, clip_result);
172 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString()); 204 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString());
(...skipping 15 matching lines...) Expand all
188 EXPECT_LT(0, clip_rect.x()); 220 EXPECT_LT(0, clip_rect.x());
189 EXPECT_GE(2000, clip_rect.width()); 221 EXPECT_GE(2000, clip_rect.width());
190 EXPECT_EQ(800, clip_rect.height()); 222 EXPECT_EQ(800, clip_rect.height());
191 223
192 clip_rect = SimpleThumbnailCrop::GetClippingRect( 224 clip_rect = SimpleThumbnailCrop::GetClippingRect(
193 gfx::Size(900, 600), desired_size, &clip_result); 225 gfx::Size(900, 600), desired_size, &clip_result);
194 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED, clip_result); 226 EXPECT_EQ(thumbnails::CLIP_RESULT_NOT_CLIPPED, clip_result);
195 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString()); 227 EXPECT_EQ(gfx::Point(0, 0).ToString(), clip_rect.origin().ToString());
196 EXPECT_EQ(gfx::Size(900, 600).ToString(), clip_rect.size().ToString()); 228 EXPECT_EQ(gfx::Size(900, 600).ToString(), clip_rect.size().ToString());
197 } 229 }
OLDNEW
« no previous file with comments | « chrome/browser/thumbnails/simple_thumbnail_crop.cc ('k') | chrome/browser/thumbnails/thumbnailing_algorithm.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698