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

Side by Side Diff: ui/gfx/image/image_family_unittest.cc

Issue 1424913007: Added ImageFamily::CreateExact, which scales the best image to size. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master-plus
Patch Set: IconUtil: Rewrite comment. Created 5 years, 1 month 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
« no previous file with comments | « ui/gfx/image/image_family.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "third_party/skia/include/core/SkBitmap.h"
7 #include "third_party/skia/include/core/SkImageInfo.h"
6 #include "ui/gfx/image/image.h" 8 #include "ui/gfx/image/image.h"
7 #include "ui/gfx/image/image_family.h" 9 #include "ui/gfx/image/image_family.h"
8 #include "ui/gfx/image/image_skia.h" 10 #include "ui/gfx/image/image_skia.h"
9 #include "ui/gfx/image/image_unittest_util.h" 11 #include "ui/gfx/image/image_unittest_util.h"
10 12
11 namespace { 13 namespace {
12 14
13 namespace gt = gfx::test; 15 namespace gt = gfx::test;
14 16
15 // Tests that |image| != NULL, and has the given width and height. 17 // Tests that |image| != NULL, and has the given width and height.
16 // This is a macro instead of a function, so that the correct line numbers are 18 // This is a macro instead of a function, so that the correct line numbers are
17 // reported when a test fails. 19 // reported when a test fails.
18 #define EXPECT_IMAGE_NON_NULL_AND_SIZE(image, expected_width, expected_height) \ 20 #define EXPECT_IMAGE_NON_NULL_AND_SIZE(image, expected_width, expected_height) \
19 do { \ 21 do { \
20 const gfx::Image* image_ = image; \ 22 const gfx::Image* image_ = image; \
21 EXPECT_TRUE(image_); \ 23 EXPECT_TRUE(image_); \
22 EXPECT_EQ(expected_width, image_->Width()); \ 24 EXPECT_EQ(expected_width, image_->Width()); \
23 EXPECT_EQ(expected_height, image_->Height()); \ 25 EXPECT_EQ(expected_height, image_->Height()); \
24 } while(0) 26 } while(0)
25 27
28 // Tests that |image| has the given width and height.
29 // This is a macro instead of a function, so that the correct line numbers are
30 // reported when a test fails.
31 #define EXPECT_IMAGE_SIZE(image, expected_width, expected_height) \
32 do { \
33 const gfx::Image& image_ = image; \
34 EXPECT_FALSE(image_.IsEmpty()); \
35 EXPECT_EQ(expected_width, image_.Width()); \
36 EXPECT_EQ(expected_height, image_.Height()); \
37 } while(0)
38
26 class ImageFamilyTest : public testing::Test { 39 class ImageFamilyTest : public testing::Test {
27 public: 40 public:
28 // Construct an ImageFamily. Implicitly tests Add and Empty. 41 // Construct an ImageFamily. Implicitly tests Add and Empty.
29 void SetUp() override { 42 void SetUp() override {
30 EXPECT_TRUE(image_family_.empty()); 43 EXPECT_TRUE(image_family_.empty());
31 44
32 // Aspect ratio 1:1. 45 // Aspect ratio 1:1.
33 image_family_.Add(gt::CreateImageSkia(32, 32)); 46 image_family_.Add(gt::CreateImageSkia(32, 32));
34 EXPECT_FALSE(image_family_.empty()); 47 EXPECT_FALSE(image_family_.empty());
35 image_family_.Add(gt::CreateImageSkia(16, 16)); 48 image_family_.Add(gt::CreateImageSkia(16, 16));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 EXPECT_TRUE(it != end); 98 EXPECT_TRUE(it != end);
86 EXPECT_EQ(gfx::Size(256, 64), it->Size()); 99 EXPECT_EQ(gfx::Size(256, 64), it->Size());
87 ++it; 100 ++it;
88 EXPECT_TRUE(it != end); 101 EXPECT_TRUE(it != end);
89 EXPECT_EQ(gfx::Size(512, 128), it->Size()); 102 EXPECT_EQ(gfx::Size(512, 128), it->Size());
90 ++it; 103 ++it;
91 104
92 EXPECT_TRUE(it == end); 105 EXPECT_TRUE(it == end);
93 } 106 }
94 107
95 TEST_F(ImageFamilyTest, Get) { 108 TEST_F(ImageFamilyTest, GetBest) {
96 // Get on an empty family. 109 // Get on an empty family.
97 gfx::ImageFamily empty_family; 110 gfx::ImageFamily empty_family;
98 EXPECT_TRUE(empty_family.empty()); 111 EXPECT_TRUE(empty_family.empty());
99 EXPECT_FALSE(empty_family.GetBest(32, 32)); 112 EXPECT_FALSE(empty_family.GetBest(32, 32));
100 EXPECT_FALSE(empty_family.GetBest(0, 32)); 113 EXPECT_FALSE(empty_family.GetBest(0, 32));
101 EXPECT_FALSE(empty_family.GetBest(32, 0)); 114 EXPECT_FALSE(empty_family.GetBest(32, 0));
102 115
103 // Get various aspect ratios and sizes on the sample family. 116 // Get various aspect ratios and sizes on the sample family.
104 117
105 // 0x0 (expect the smallest square image). 118 // 0x0 (expect the smallest square image).
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 // 4:1 aspect ratio. 157 // 4:1 aspect ratio.
145 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(64, 16), 256, 64); 158 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(64, 16), 256, 64);
146 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(260, 65), 512, 128); 159 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(260, 65), 512, 128);
147 160
148 // Wider than widest image. 161 // Wider than widest image.
149 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(255, 51), 256, 64); 162 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(255, 51), 256, 64);
150 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(260, 52), 512, 128); 163 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(260, 52), 512, 128);
151 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(654, 129), 512, 128); 164 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(654, 129), 512, 128);
152 } 165 }
153 166
167 TEST_F(ImageFamilyTest, CreateExact) {
168 // CreateExact on an empty family.
169 gfx::ImageFamily empty_family;
170 EXPECT_TRUE(empty_family.empty());
171 EXPECT_TRUE(empty_family.CreateExact(32, 32).IsEmpty());
172 EXPECT_TRUE(empty_family.CreateExact(0, 32).IsEmpty());
173 EXPECT_TRUE(empty_family.CreateExact(32, 0).IsEmpty());
174
175 // CreateExact on a family with only empty images results in an empty image,
176 // despite the requested image size.
177 gfx::ImageFamily family_with_empty_image;
178 family_with_empty_image.Add(gfx::Image());
179 EXPECT_FALSE(family_with_empty_image.empty());
180 EXPECT_TRUE(family_with_empty_image.CreateExact(32, 32).IsEmpty());
181
182 // CreateExact on various aspect ratios and sizes on the sample family.
183
184 // Targeting an image with width and/or height of 0 results in empty image.
185 EXPECT_TRUE(image_family_.CreateExact(0, 0).IsEmpty());
186 EXPECT_TRUE(image_family_.CreateExact(0, 64).IsEmpty());
187 EXPECT_TRUE(image_family_.CreateExact(64, 0).IsEmpty());
188
189 // Thinner than thinnest image.
190 EXPECT_IMAGE_SIZE(image_family_.CreateExact(2, 12), 2, 12);
191
192 // Exact match aspect ratio.
193 // Exact match size.
194 EXPECT_IMAGE_SIZE(image_family_.CreateExact(32, 32), 32, 32);
195 // Much smaller.
196 EXPECT_IMAGE_SIZE(image_family_.CreateExact(17, 17), 17, 17);
197 // Smaller than any image.
198 EXPECT_IMAGE_SIZE(image_family_.CreateExact(3, 3), 3, 3);
199 // Larger than any image.
200 EXPECT_IMAGE_SIZE(image_family_.CreateExact(512, 512), 512, 512);
201
202 // Wider than widest image.
203 EXPECT_IMAGE_SIZE(image_family_.CreateExact(255, 51), 255, 51);
204
205 // CreateExact on an image family with a non-N32 color bitmap results in an
206 // empty image.
207 gfx::ImageFamily a8_family;
208 SkBitmap a8_bitmap;
209 SkImageInfo a8_info = SkImageInfo::MakeA8(64, 64);
210 a8_bitmap.allocPixels(a8_info);
211 a8_family.Add(gfx::Image::CreateFrom1xBitmap(a8_bitmap));
212 EXPECT_TRUE(a8_family.CreateExact(32, 32).IsEmpty());
213 }
214
154 // Test adding and looking up images with 0 width and height. 215 // Test adding and looking up images with 0 width and height.
155 TEST_F(ImageFamilyTest, ZeroWidthAndHeight) { 216 TEST_F(ImageFamilyTest, ZeroWidthAndHeight) {
156 // An empty Image. Should be considered to have 0 width and height. 217 // An empty Image. Should be considered to have 0 width and height.
157 image_family_.Add(gfx::Image()); 218 image_family_.Add(gfx::Image());
158 // Images with 0 width OR height should be treated the same as an image with 0 219 // Images with 0 width OR height should be treated the same as an image with 0
159 // width AND height (in fact, the ImageSkias should be indistinguishable). 220 // width AND height (in fact, the ImageSkias should be indistinguishable).
160 image_family_.Add(gt::CreateImageSkia(32, 0)); 221 image_family_.Add(gt::CreateImageSkia(32, 0));
161 image_family_.Add(gt::CreateImageSkia(0, 32)); 222 image_family_.Add(gt::CreateImageSkia(0, 32));
162 223
163 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(0, 0), 0, 0); 224 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(0, 0), 0, 0);
164 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(1, 1), 16, 16); 225 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(1, 1), 16, 16);
165 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(32, 32), 32, 32); 226 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(32, 32), 32, 32);
166 227
167 // GetBest(0, N) or GetBest(N, 0) should be treated the same as GetBest(0, 0). 228 // GetBest(0, N) or GetBest(N, 0) should be treated the same as GetBest(0, 0).
168 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(0, 1), 0, 0); 229 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(0, 1), 0, 0);
169 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(0, 32), 0, 0); 230 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(0, 32), 0, 0);
170 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(1, 0), 0, 0); 231 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(1, 0), 0, 0);
171 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(32, 0), 0, 0); 232 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(32, 0), 0, 0);
172 233
173 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(1, 32), 12, 48); 234 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(1, 32), 12, 48);
174 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(32, 1), 256, 64); 235 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(32, 1), 256, 64);
175 } 236 }
176 237
177 } // namespace 238 } // namespace
OLDNEW
« no previous file with comments | « ui/gfx/image/image_family.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698