OLD | NEW |
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 Loading... |
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 Loading... |
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 various aspect ratios and sizes on the sample family. |
| 176 |
| 177 // Targeting an image with width and/or height of 0 results in empty image. |
| 178 EXPECT_TRUE(image_family_.CreateExact(0, 0).IsEmpty()); |
| 179 EXPECT_TRUE(image_family_.CreateExact(0, 64).IsEmpty()); |
| 180 EXPECT_TRUE(image_family_.CreateExact(64, 0).IsEmpty()); |
| 181 |
| 182 // Thinner than thinnest image. |
| 183 EXPECT_IMAGE_SIZE(image_family_.CreateExact(2, 12), 2, 12); |
| 184 |
| 185 // Exact match aspect ratio. |
| 186 // Exact match size. |
| 187 EXPECT_IMAGE_SIZE(image_family_.CreateExact(32, 32), 32, 32); |
| 188 // Much smaller. |
| 189 EXPECT_IMAGE_SIZE(image_family_.CreateExact(17, 17), 17, 17); |
| 190 // Smaller than any image. |
| 191 EXPECT_IMAGE_SIZE(image_family_.CreateExact(3, 3), 3, 3); |
| 192 // Larger than any image. |
| 193 EXPECT_IMAGE_SIZE(image_family_.CreateExact(512, 512), 512, 512); |
| 194 |
| 195 // Wider than widest image. |
| 196 EXPECT_IMAGE_SIZE(image_family_.CreateExact(255, 51), 255, 51); |
| 197 |
| 198 // CreateExact on an image family with a non-N32 color bitmap results in an |
| 199 // empty image. |
| 200 gfx::ImageFamily a8_family; |
| 201 SkBitmap a8_bitmap; |
| 202 SkImageInfo a8_info = SkImageInfo::MakeA8(64, 64); |
| 203 a8_bitmap.allocPixels(a8_info); |
| 204 a8_family.Add(gfx::Image::CreateFrom1xBitmap(a8_bitmap)); |
| 205 EXPECT_TRUE(a8_family.CreateExact(32, 32).IsEmpty()); |
| 206 } |
| 207 |
154 // Test adding and looking up images with 0 width and height. | 208 // Test adding and looking up images with 0 width and height. |
155 TEST_F(ImageFamilyTest, ZeroWidthAndHeight) { | 209 TEST_F(ImageFamilyTest, ZeroWidthAndHeight) { |
156 // An empty Image. Should be considered to have 0 width and height. | 210 // An empty Image. Should be considered to have 0 width and height. |
157 image_family_.Add(gfx::Image()); | 211 image_family_.Add(gfx::Image()); |
158 // Images with 0 width OR height should be treated the same as an image with 0 | 212 // 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). | 213 // width AND height (in fact, the ImageSkias should be indistinguishable). |
160 image_family_.Add(gt::CreateImageSkia(32, 0)); | 214 image_family_.Add(gt::CreateImageSkia(32, 0)); |
161 image_family_.Add(gt::CreateImageSkia(0, 32)); | 215 image_family_.Add(gt::CreateImageSkia(0, 32)); |
162 | 216 |
163 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(0, 0), 0, 0); | 217 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); | 218 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); | 219 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(32, 32), 32, 32); |
166 | 220 |
167 // GetBest(0, N) or GetBest(N, 0) should be treated the same as GetBest(0, 0). | 221 // 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); | 222 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); | 223 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); | 224 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); | 225 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(32, 0), 0, 0); |
172 | 226 |
173 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(1, 32), 12, 48); | 227 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); | 228 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_.GetBest(32, 1), 256, 64); |
175 } | 229 } |
176 | 230 |
177 } // namespace | 231 } // namespace |
OLD | NEW |