Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / ui / ui_util_unittest.mm
blob4ca6928be6e93cd02bfd700bd47f1404432ef71d
1 // Copyright 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 #import "ios/chrome/browser/ui/ui_util.h"
7 #import <UIKit/UIKit.h>
9 #include "base/basictypes.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/gtest_mac.h"
13 TEST(UIUtilTest, AlignToPixel) {
14   CGFloat scale = [[UIScreen mainScreen] scale];
15   // Pick a few interesting values: already aligned, aligned on retina, and
16   // some unaligned values that would round differently. Ensure that all are
17   // "integer" values within <1 of the original value in the scaled space.
18   CGFloat test_values[] = {10.0, 55.5, 3.14159, 2.71828};
19   const CGFloat kMaxAlignDelta = 0.9999;
20   size_t value_count = arraysize(test_values);
21   for (unsigned int i = 0; i < value_count; ++i) {
22     CGFloat aligned = AlignValueToPixel(test_values[i]);
23     EXPECT_FLOAT_EQ(aligned * scale, floor(aligned * scale));
24     EXPECT_NEAR(aligned * scale, test_values[i] * scale, kMaxAlignDelta);
26     CGFloat x = test_values[i];
27     CGFloat y = test_values[(i + 1) % value_count];
28     CGPoint alignedPoint = AlignPointToPixel(CGPointMake(x, y));
29     EXPECT_FLOAT_EQ(floor(alignedPoint.x * scale), alignedPoint.x * scale);
30     EXPECT_FLOAT_EQ(floor(alignedPoint.y * scale), alignedPoint.y * scale);
31     EXPECT_NEAR(x * scale, alignedPoint.x * scale, kMaxAlignDelta);
32     EXPECT_NEAR(y * scale, alignedPoint.y * scale, kMaxAlignDelta);
33   }
36 #define EXPECT_EQ_RECT(a, b) \
37   EXPECT_NSEQ(NSStringFromCGRect(a), NSStringFromCGRect(b))
38 #define EXPECT_EQ_SIZE(a, b) \
39   EXPECT_NSEQ(NSStringFromCGSize(a), NSStringFromCGSize(b))
41 TEST(UIUtilTest, TestProjectionFill) {
42   CGSize originalSize, targetSize, expectedRevisedSize, revisedSize;
43   CGRect expectedProjection, projection;
45   // Resize with same aspect ratio.
46   originalSize = CGSizeMake(100, 100);
47   targetSize = CGSizeMake(50, 50);
48   expectedRevisedSize = targetSize;
49   expectedProjection = CGRectMake(0, 0, 50, 50);
50   CalculateProjection(originalSize, targetSize, ProjectionMode::kFill,
51                       revisedSize, projection);
52   EXPECT_EQ_RECT(expectedProjection, projection);
53   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
55   // Resize with different aspect ratio.
56   originalSize = CGSizeMake(100, 100);
57   targetSize = CGSizeMake(60, 40);
58   expectedRevisedSize = targetSize;
59   expectedProjection = CGRectMake(0, 0, 60, 40);
60   CalculateProjection(originalSize, targetSize, ProjectionMode::kFill,
61                       revisedSize, projection);
62   EXPECT_EQ_RECT(expectedProjection, projection);
63   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
65   // Stretch the other way.
66   originalSize = CGSizeMake(100, 100);
67   targetSize = CGSizeMake(40, 60);
68   expectedRevisedSize = targetSize;
69   expectedProjection = CGRectMake(0, 0, 40, 60);
70   CalculateProjection(originalSize, targetSize, ProjectionMode::kFill,
71                       revisedSize, projection);
72   EXPECT_EQ_RECT(expectedProjection, projection);
73   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
76 TEST(UIUtilTest, TestProjectionFit) {
77   CGSize originalSize, targetSize, expectedRevisedSize, revisedSize;
78   CGRect expectedProjection, projection;
80   // Landscape resize to 50x50, but squeezed into 50x25.
81   originalSize = CGSizeMake(100, 50);
82   targetSize = CGSizeMake(50, 50);
83   expectedRevisedSize = CGSizeMake(50, 25);
84   expectedProjection = CGRectMake(0, 0, 50, 25);
85   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFit,
86                       revisedSize, projection);
87   EXPECT_EQ_RECT(expectedProjection, projection);
88   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
90   // Landscape resize to 60x40, but squeezed into 60x30.
91   originalSize = CGSizeMake(100, 50);
92   targetSize = CGSizeMake(60, 40);
93   expectedRevisedSize = CGSizeMake(60, 30);
94   expectedProjection = CGRectMake(0, 0, 60, 30);
95   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFit,
96                       revisedSize, projection);
97   EXPECT_EQ_RECT(expectedProjection, projection);
98   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
100   // Landscape resize to 40x60, but squeezed into 40x20.
101   originalSize = CGSizeMake(100, 50);
102   targetSize = CGSizeMake(40, 60);
103   expectedRevisedSize = CGSizeMake(40, 20);
104   expectedProjection = CGRectMake(0, 0, 40, 20);
105   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFit,
106                       revisedSize, projection);
107   EXPECT_EQ_RECT(expectedProjection, projection);
108   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
110   // Portrait resize to 50x50, but squeezed into 25x50.
111   originalSize = CGSizeMake(50, 100);
112   targetSize = CGSizeMake(50, 50);
113   expectedRevisedSize = CGSizeMake(25, 50);
114   expectedProjection = CGRectMake(0, 0, 25, 50);
115   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFit,
116                       revisedSize, projection);
117   EXPECT_EQ_RECT(expectedProjection, projection);
118   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
120   // Portrait resize to 60x40, but squeezed into 20x40.
121   originalSize = CGSizeMake(50, 100);
122   targetSize = CGSizeMake(60, 40);
123   expectedRevisedSize = CGSizeMake(20, 40);
124   expectedProjection = CGRectMake(0, 0, 20, 40);
125   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFit,
126                       revisedSize, projection);
127   EXPECT_EQ_RECT(expectedProjection, projection);
128   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
130   // Portrait resize to 40x60, but squeezed into 30x60.
131   originalSize = CGSizeMake(50, 100);
132   targetSize = CGSizeMake(40, 60);
133   expectedRevisedSize = CGSizeMake(30, 60);
134   expectedProjection = CGRectMake(0, 0, 30, 60);
135   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFit,
136                       revisedSize, projection);
137   EXPECT_EQ_RECT(expectedProjection, projection);
138   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
141 TEST(UIUtilTest, TestProjectionAspectFill) {
142   CGSize originalSize, targetSize, expectedRevisedSize, revisedSize;
143   CGRect expectedProjection, projection;
145   // Landscape resize to 50x50
146   originalSize = CGSizeMake(100, 50);
147   targetSize = CGSizeMake(50, 50);
148   expectedRevisedSize = targetSize;
149   expectedProjection = CGRectMake(-25, 0, 100, 50);
150   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFill,
151                       revisedSize, projection);
152   EXPECT_EQ_RECT(expectedProjection, projection);
153   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
155   // Landscape resize to 60x40, but squeezed into 60x30.
156   originalSize = CGSizeMake(100, 50);
157   targetSize = CGSizeMake(60, 40);
158   expectedRevisedSize = targetSize;
159   expectedProjection = CGRectMake(-10, 0, 80, 40);
160   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFill,
161                       revisedSize, projection);
162   EXPECT_EQ_RECT(expectedProjection, projection);
163   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
165   // Landscape resize to 40x60, but squeezed into 40x20.
166   originalSize = CGSizeMake(100, 50);
167   targetSize = CGSizeMake(40, 60);
168   expectedRevisedSize = targetSize;
169   expectedProjection = CGRectMake(-40, 0, 120, 60);
170   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFill,
171                       revisedSize, projection);
172   EXPECT_EQ_RECT(expectedProjection, projection);
173   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
175   // Portrait resize to 50x50, but squeezed into 25x50.
176   originalSize = CGSizeMake(50, 100);
177   targetSize = CGSizeMake(50, 50);
178   expectedRevisedSize = targetSize;
179   expectedProjection = CGRectMake(0, -25, 50, 100);
180   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFill,
181                       revisedSize, projection);
182   EXPECT_EQ_RECT(expectedProjection, projection);
183   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
185   // Portrait resize to 60x40, but clipped to 20x40.
186   originalSize = CGSizeMake(50, 100);
187   targetSize = CGSizeMake(60, 40);
188   expectedRevisedSize = targetSize;
189   expectedProjection = CGRectMake(0, -40, 60, 120);
190   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFill,
191                       revisedSize, projection);
192   EXPECT_EQ_RECT(expectedProjection, projection);
193   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
195   // Portrait resize to 40x60, but clipped to 30x60.
196   originalSize = CGSizeMake(50, 100);
197   targetSize = CGSizeMake(40, 60);
198   expectedRevisedSize = targetSize;
199   expectedProjection = CGRectMake(0, -10, 40, 80);
200   CalculateProjection(originalSize, targetSize, ProjectionMode::kAspectFill,
201                       revisedSize, projection);
202   EXPECT_EQ_RECT(expectedProjection, projection);
203   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
206 TEST(UIUtilTest, TestProjectionAspectFillNoClipping) {
207   CGSize originalSize, targetSize, expectedRevisedSize, revisedSize;
208   CGRect expectedProjection, projection;
210   // Landscape resize to 50x50
211   originalSize = CGSizeMake(100, 50);
212   targetSize = CGSizeMake(50, 50);
213   expectedProjection = CGRectMake(0, 0, 100, 50);
214   expectedRevisedSize = expectedProjection.size;
215   CalculateProjection(originalSize, targetSize,
216                       ProjectionMode::kAspectFillNoClipping, revisedSize,
217                       projection);
218   EXPECT_EQ_RECT(expectedProjection, projection);
219   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
221   // Landscape resize to 60x40.
222   originalSize = CGSizeMake(100, 50);
223   targetSize = CGSizeMake(60, 40);
224   expectedProjection = CGRectMake(0, 0, 80, 40);
225   expectedRevisedSize = expectedProjection.size;
226   CalculateProjection(originalSize, targetSize,
227                       ProjectionMode::kAspectFillNoClipping, revisedSize,
228                       projection);
229   EXPECT_EQ_RECT(expectedProjection, projection);
230   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
232   // Landscape resize to 40x60.
233   originalSize = CGSizeMake(100, 50);
234   targetSize = CGSizeMake(40, 60);
235   expectedProjection = CGRectMake(0, 0, 120, 60);
236   expectedRevisedSize = expectedProjection.size;
237   CalculateProjection(originalSize, targetSize,
238                       ProjectionMode::kAspectFillNoClipping, revisedSize,
239                       projection);
240   EXPECT_EQ_RECT(expectedProjection, projection);
241   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
243   // Portrait resize to 50x50.
244   originalSize = CGSizeMake(50, 100);
245   targetSize = CGSizeMake(50, 50);
246   expectedProjection = CGRectMake(0, 0, 50, 100);
247   expectedRevisedSize = expectedProjection.size;
248   CalculateProjection(originalSize, targetSize,
249                       ProjectionMode::kAspectFillNoClipping, revisedSize,
250                       projection);
251   EXPECT_EQ_RECT(expectedProjection, projection);
252   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
254   // Portrait resize to 60x40.
255   originalSize = CGSizeMake(50, 100);
256   targetSize = CGSizeMake(60, 40);
257   expectedProjection = CGRectMake(0, 0, 60, 120);
258   expectedRevisedSize = expectedProjection.size;
259   CalculateProjection(originalSize, targetSize,
260                       ProjectionMode::kAspectFillNoClipping, revisedSize,
261                       projection);
262   EXPECT_EQ_RECT(expectedProjection, projection);
263   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);
265   // Portrait resize to 40x60.
266   originalSize = CGSizeMake(50, 100);
267   targetSize = CGSizeMake(40, 60);
268   expectedProjection = CGRectMake(0, 0, 40, 80);
269   expectedRevisedSize = expectedProjection.size;
270   CalculateProjection(originalSize, targetSize,
271                       ProjectionMode::kAspectFillNoClipping, revisedSize,
272                       projection);
273   EXPECT_EQ_RECT(expectedProjection, projection);
274   EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize);