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
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "ui/gfx/image/image.h"
7 #include "ui/gfx/image/image_family.h"
8 #include "ui/gfx/image/image_skia.h"
9 #include "ui/gfx/image/image_unittest_util.h"
13 namespace gt
= gfx::test
;
15 // 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
17 // reported when a test fails.
18 #define EXPECT_IMAGE_NON_NULL_AND_SIZE(image, expected_width, expected_height) \
20 const gfx::Image* image_ = image; \
21 EXPECT_TRUE(image_); \
22 EXPECT_EQ(expected_width, image_->Width()); \
23 EXPECT_EQ(expected_height, image_->Height()); \
26 class ImageFamilyTest
: public testing::Test
{
28 // Construct an ImageFamily. Implicitly tests Add and Empty.
29 void SetUp() override
{
30 EXPECT_TRUE(image_family_
.empty());
33 image_family_
.Add(gt::CreateImageSkia(32, 32));
34 EXPECT_FALSE(image_family_
.empty());
35 image_family_
.Add(gt::CreateImageSkia(16, 16));
36 image_family_
.Add(gt::CreateImageSkia(64, 64));
37 // Duplicate (should override previous one).
38 // Insert an Image directly, instead of an ImageSkia.
39 gfx::Image
image(gt::CreateImageSkia(32, 32));
40 image_family_
.Add(image
);
42 image_family_
.Add(gt::CreateImageSkia(3, 12));
43 image_family_
.Add(gt::CreateImageSkia(12, 48));
45 image_family_
.Add(gt::CreateImageSkia(512, 128));
46 image_family_
.Add(gt::CreateImageSkia(256, 64));
48 EXPECT_FALSE(image_family_
.empty());
51 gfx::ImageFamily image_family_
;
54 TEST_F(ImageFamilyTest
, Clear
) {
55 image_family_
.clear();
56 EXPECT_TRUE(image_family_
.empty());
59 // Tests iteration over an ImageFamily.
60 TEST_F(ImageFamilyTest
, Iteration
) {
61 gfx::ImageFamily::const_iterator it
= image_family_
.begin();
62 gfx::ImageFamily::const_iterator end
= image_family_
.end();
64 // Expect iteration in order of aspect ratio (from thinnest to widest), then
66 EXPECT_TRUE(it
!= end
);
67 EXPECT_EQ(gfx::Size(3, 12), it
->Size());
69 EXPECT_TRUE(it
!= end
);
70 EXPECT_EQ(gfx::Size(12, 48), it
->Size());
71 it
++; // Test post-increment.
72 EXPECT_TRUE(it
!= end
);
73 EXPECT_EQ(gfx::Size(16, 16), it
->Size());
75 EXPECT_TRUE(it
!= end
);
76 EXPECT_EQ(gfx::Size(32, 32), it
->Size());
77 --it
; // Test decrement
78 EXPECT_TRUE(it
!= end
);
79 EXPECT_EQ(gfx::Size(16, 16), it
->Size());
82 EXPECT_TRUE(it
!= end
);
83 EXPECT_EQ(gfx::Size(64, 64), (*it
).Size()); // Test operator*.
85 EXPECT_TRUE(it
!= end
);
86 EXPECT_EQ(gfx::Size(256, 64), it
->Size());
88 EXPECT_TRUE(it
!= end
);
89 EXPECT_EQ(gfx::Size(512, 128), it
->Size());
92 EXPECT_TRUE(it
== end
);
95 TEST_F(ImageFamilyTest
, Get
) {
96 // Get on an empty family.
97 gfx::ImageFamily empty_family
;
98 EXPECT_TRUE(empty_family
.empty());
99 EXPECT_FALSE(empty_family
.GetBest(32, 32));
100 EXPECT_FALSE(empty_family
.GetBest(0, 32));
101 EXPECT_FALSE(empty_family
.GetBest(32, 0));
103 // Get various aspect ratios and sizes on the sample family.
105 // 0x0 (expect the smallest square image).
106 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(0, 0), 16, 16);
107 // GetBest(0, N) or GetBest(N, 0) should be treated the same as GetBest(0, 0).
108 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(0, 16), 16, 16);
109 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(0, 64), 16, 16);
110 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(16, 0), 16, 16);
111 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(64, 0), 16, 16);
113 // Thinner than thinnest image.
114 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(2, 12), 3, 12);
115 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(2, 13), 12, 48);
116 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(10, 60), 12, 48);
118 // Between two images' aspect ratio.
119 // Note: Testing the boundary around 1:2 and 2:1, half way to 1:4 and 4:1.
120 // Ties are broken by favouring the thinner aspect ratio.
121 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(63, 32), 64, 64);
122 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(64, 32), 64, 64);
123 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(65, 32), 256, 64);
124 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(32, 63), 64, 64);
125 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(32, 64), 12, 48);
126 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(32, 65), 12, 48);
128 // Exact match aspect ratio.
130 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(32, 32), 32, 32);
132 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(31, 31), 32, 32);
134 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(17, 17), 32, 32);
136 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(16, 16), 16, 16);
137 // Smaller than any image.
138 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(3, 3), 16, 16);
139 // Larger than any image.
140 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(512, 512), 64, 64);
142 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(16, 64), 12, 48);
143 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(2, 8), 3, 12);
145 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);
148 // Wider than widest image.
149 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);
151 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(654, 129), 512, 128);
154 // Test adding and looking up images with 0 width and height.
155 TEST_F(ImageFamilyTest
, ZeroWidthAndHeight
) {
156 // An empty Image. Should be considered to have 0 width and height.
157 image_family_
.Add(gfx::Image());
158 // 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).
160 image_family_
.Add(gt::CreateImageSkia(32, 0));
161 image_family_
.Add(gt::CreateImageSkia(0, 32));
163 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);
165 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(32, 32), 32, 32);
167 // 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);
169 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);
171 EXPECT_IMAGE_NON_NULL_AND_SIZE(image_family_
.GetBest(32, 0), 0, 0);
173 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);