| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #import "ios/chrome/browser/ui/ui_util.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "testing/gtest_mac.h" |
| 12 |
| 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); |
| 25 |
| 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 } |
| 34 } |
| 35 |
| 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)) |
| 40 |
| 41 TEST(UIUtilTest, TestProjectionWithoutPreservingAspectRatio) { |
| 42 CGSize originalSize, targetSize, expectedRevisedSize, revisedSize; |
| 43 CGRect expectedProjection, projection; |
| 44 |
| 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, NO, NO, revisedSize, |
| 51 projection); |
| 52 EXPECT_EQ_RECT(expectedProjection, projection); |
| 53 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 54 |
| 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, NO, NO, revisedSize, |
| 61 projection); |
| 62 EXPECT_EQ_RECT(expectedProjection, projection); |
| 63 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 64 |
| 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, NO, NO, revisedSize, |
| 71 projection); |
| 72 EXPECT_EQ_RECT(expectedProjection, projection); |
| 73 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 74 } |
| 75 |
| 76 TEST(UIUtilTest, TestProjectionPreservingAspectRatioWithoutClip) { |
| 77 CGSize originalSize, targetSize, expectedRevisedSize, revisedSize; |
| 78 CGRect expectedProjection, projection; |
| 79 |
| 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, YES, NO, revisedSize, |
| 86 projection); |
| 87 EXPECT_EQ_RECT(expectedProjection, projection); |
| 88 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 89 |
| 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, YES, NO, revisedSize, |
| 96 projection); |
| 97 EXPECT_EQ_RECT(expectedProjection, projection); |
| 98 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 99 |
| 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, YES, NO, revisedSize, |
| 106 projection); |
| 107 EXPECT_EQ_RECT(expectedProjection, projection); |
| 108 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 109 |
| 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, YES, NO, revisedSize, |
| 116 projection); |
| 117 EXPECT_EQ_RECT(expectedProjection, projection); |
| 118 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 119 |
| 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, YES, NO, revisedSize, |
| 126 projection); |
| 127 EXPECT_EQ_RECT(expectedProjection, projection); |
| 128 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 129 |
| 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, YES, NO, revisedSize, |
| 136 projection); |
| 137 EXPECT_EQ_RECT(expectedProjection, projection); |
| 138 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 139 } |
| 140 |
| 141 TEST(UIUtilTest, TestProjectionPreservingAspectRatioWithClip) { |
| 142 CGSize originalSize, targetSize, expectedRevisedSize, revisedSize; |
| 143 CGRect expectedProjection, projection; |
| 144 |
| 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, YES, YES, revisedSize, |
| 151 projection); |
| 152 EXPECT_EQ_RECT(expectedProjection, projection); |
| 153 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 154 |
| 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, YES, YES, revisedSize, |
| 161 projection); |
| 162 EXPECT_EQ_RECT(expectedProjection, projection); |
| 163 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 164 |
| 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, YES, YES, revisedSize, |
| 171 projection); |
| 172 EXPECT_EQ_RECT(expectedProjection, projection); |
| 173 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 174 |
| 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, YES, YES, revisedSize, |
| 181 projection); |
| 182 EXPECT_EQ_RECT(expectedProjection, projection); |
| 183 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 184 |
| 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, YES, YES, revisedSize, |
| 191 projection); |
| 192 EXPECT_EQ_RECT(expectedProjection, projection); |
| 193 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 194 |
| 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, YES, YES, revisedSize, |
| 201 projection); |
| 202 EXPECT_EQ_RECT(expectedProjection, projection); |
| 203 EXPECT_EQ_SIZE(expectedRevisedSize, revisedSize); |
| 204 } |
| OLD | NEW |