1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/uikit_ui_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/chrome/browser/ui/ui_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "third_party/ocmock/OCMock/OCMock.h"
namespace {
void ExpectInterpolatedColor(UIColor* firstColor,
UIColor* secondColor,
CGFloat percentage,
CGFloat expectedValue) {
UIColor* interpolatedColor =
InterpolateFromColorToColor(firstColor, secondColor, percentage);
CGFloat r, g, b, a;
[interpolatedColor getRed:&r green:&g blue:&b alpha:&a];
EXPECT_FLOAT_EQ(expectedValue, r);
EXPECT_FLOAT_EQ(expectedValue, g);
EXPECT_FLOAT_EQ(expectedValue, b);
EXPECT_FLOAT_EQ(1.0, a);
}
// Verify the assumption about UIViewController that on iPad all orientations
// are supported, and all orientations but Portrait Upside-Down on iPhone and
// iPod Touch.
TEST(UIKitUIUtilTest, UIViewControllerSupportedOrientationsTest) {
base::scoped_nsobject<UIViewController> viewController(
[[UIViewController alloc] initWithNibName:nil bundle:nil]);
if (IsIPadIdiom()) {
EXPECT_EQ(UIInterfaceOrientationMaskAll,
[viewController supportedInterfaceOrientations]);
} else {
EXPECT_EQ(UIInterfaceOrientationMaskAllButUpsideDown,
[viewController supportedInterfaceOrientations]);
}
}
TEST(UIKitUIUtilTest, TestGetUiFont) {
EXPECT_TRUE(GetUIFont(FONT_HELVETICA, false, 15.0));
EXPECT_TRUE(GetUIFont(FONT_HELVETICA_NEUE, true, 15.0));
}
// Verifies that greyImage never returns retina-scale images.
TEST(UIKitUIUtilTest, TestGreyImage) {
// Create an image using the device's scale factor.
const CGSize kSize = CGSizeMake(100, 100);
UIGraphicsBeginImageContextWithOptions(kSize, NO, 0.0);
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Verify the grey image's size and scale.
UIImage* greyImage = GreyImage(image);
EXPECT_EQ(kSize.width, greyImage.size.width);
EXPECT_EQ(kSize.height, greyImage.size.height);
EXPECT_EQ(1.0, greyImage.scale);
}
// Returns an image of random color in the same scale as the device main
// screen.
UIImage* testImage(CGSize imageSize) {
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
CGContextFillRect(context,
CGRectMake(0.0, 0.0, imageSize.width, imageSize.height));
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
TEST(UIKitUIUtilTest, TestResizeImageOpacity) {
UIImage* actual;
UIImage* image = testImage(CGSizeMake(100, 100));
actual =
ResizeImage(image, CGSizeMake(50, 50), ProjectionMode::kAspectFit, YES);
EXPECT_TRUE(actual);
EXPECT_FALSE(ImageHasAlphaChannel(actual));
actual =
ResizeImage(image, CGSizeMake(50, 50), ProjectionMode::kAspectFit, NO);
EXPECT_TRUE(actual);
EXPECT_TRUE(ImageHasAlphaChannel(actual));
}
TEST(UIKitUIUtilTest, TestResizeImageInvalidInput) {
UIImage* actual;
UIImage* image = testImage(CGSizeMake(100, 50));
actual = ResizeImage(image, CGSizeZero, ProjectionMode::kAspectFit);
EXPECT_FALSE(actual);
actual = ResizeImage(image, CGSizeMake(0.1, 0.1), ProjectionMode::kAspectFit);
EXPECT_FALSE(actual);
actual =
ResizeImage(image, CGSizeMake(-100, -100), ProjectionMode::kAspectFit);
EXPECT_FALSE(actual);
}
TEST(UIKitUIUtilTest, TestInterpolateFromColorToColor) {
CGFloat colorOne = 50.0f / 255.0f;
CGFloat colorTwo = 100.0f / 255.0f;
CGFloat expectedOne = 50.0f / 255.0f;
CGFloat expectedTwo = 55.0f / 255.0f;
CGFloat expectedThree = 75.0f / 255.0f;
CGFloat expectedFour = 100.0f / 255.0f;
UIColor* firstColor =
[UIColor colorWithRed:colorOne green:colorOne blue:colorOne alpha:1.0];
UIColor* secondColor =
[UIColor colorWithRed:colorTwo green:colorTwo blue:colorTwo alpha:1.0];
ExpectInterpolatedColor(firstColor, secondColor, 0.0f, expectedOne);
ExpectInterpolatedColor(firstColor, secondColor, 0.1f, expectedTwo);
ExpectInterpolatedColor(firstColor, secondColor, 0.5f, expectedThree);
ExpectInterpolatedColor(firstColor, secondColor, 1.0f, expectedFour);
}
// Tests that InterpolateFromColorToColor() works for monochrome colors.
TEST(UIKitUIUtilTest, TestInterpolateFromColorToColorMonochrome) {
CGFloat kRGBComponent = 0.2;
UIColor* rgb = [UIColor colorWithRed:kRGBComponent
green:kRGBComponent
blue:kRGBComponent
alpha:1.0];
ASSERT_EQ(kCGColorSpaceModelRGB,
CGColorSpaceGetModel(CGColorGetColorSpace(rgb.CGColor)));
UIColor* white = [UIColor whiteColor];
ASSERT_EQ(kCGColorSpaceModelMonochrome,
CGColorSpaceGetModel(CGColorGetColorSpace(white.CGColor)));
UIColor* black = [UIColor blackColor];
ASSERT_EQ(kCGColorSpaceModelMonochrome,
CGColorSpaceGetModel(CGColorGetColorSpace(black.CGColor)));
// Interpolate between monochrome and rgb.
ExpectInterpolatedColor(black, rgb, 0.5, 0.1);
// Interpolate between two monochrome colors.
ExpectInterpolatedColor(black, white, 0.3, 0.3);
}
} // namespace
|