Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: ios/chrome/browser/ui/uikit_ui_util_unittest.mm

Issue 802633007: Upstream iOS UI utilities. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/uikit_ui_util.h"
6
7 #include "base/basictypes.h"
8 #include "base/ios/ios_util.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/strings/sys_string_conversions.h"
11 #import "ios/chrome/browser/ui/ui_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #import "third_party/ocmock/OCMock/OCMock.h"
14
15 namespace {
16
17 void ExpectInterpolatedColor(UIColor* firstColor,
18 UIColor* secondColor,
19 CGFloat percentage,
20 CGFloat expectedValue) {
21 UIColor* interpolatedColor =
22 InterpolateFromColorToColor(firstColor, secondColor, percentage);
23 CGFloat r, g, b, a;
24 [interpolatedColor getRed:&r green:&g blue:&b alpha:&a];
25 EXPECT_FLOAT_EQ(expectedValue, r);
26 EXPECT_FLOAT_EQ(expectedValue, g);
27 EXPECT_FLOAT_EQ(expectedValue, b);
28 EXPECT_FLOAT_EQ(1.0, a);
29 }
30
31 // Verify the assumption about UIViewController that on iPad all orientations
32 // are supported, and all orientations but Portrait Upside-Down on iPhone and
33 // iPod Touch.
34 TEST(UIKitUIUtilTest, UIViewControllerSupportedOrientationsTest) {
35 base::scoped_nsobject<UIViewController> viewController(
36 [[UIViewController alloc] initWithNibName:nil bundle:nil]);
37 if (IsIPadIdiom()) {
38 EXPECT_EQ(UIInterfaceOrientationMaskAll,
39 [viewController supportedInterfaceOrientations]);
40 } else {
41 EXPECT_EQ(UIInterfaceOrientationMaskAllButUpsideDown,
42 [viewController supportedInterfaceOrientations]);
43 }
44 }
45
46 TEST(UIKitUIUtilTest, TestGetUiFont) {
47 EXPECT_TRUE(GetUIFont(FONT_HELVETICA, false, 15.0));
48 EXPECT_TRUE(GetUIFont(FONT_HELVETICA_NEUE, true, 15.0));
49 }
50
51 // Verifies that greyImage never returns retina-scale images.
52 TEST(UIKitUIUtilTest, TestGreyImage) {
53 // Create an image using the device's scale factor.
54 const CGSize kSize = CGSizeMake(100, 100);
55 UIGraphicsBeginImageContextWithOptions(kSize, NO, 0.0);
56 UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
57 UIGraphicsEndImageContext();
58
59 // Verify the grey image's size and scale.
60 UIImage* greyImage = GreyImage(image);
61 EXPECT_EQ(kSize.width, greyImage.size.width);
62 EXPECT_EQ(kSize.height, greyImage.size.height);
63 EXPECT_EQ(1.0, greyImage.scale);
64 }
65
66 // Returns an image of random color in the same scale as the device main
67 // screen.
68 UIImage* testImage(CGSize imageSize) {
69 CGFloat r = rand() / CGFloat(RAND_MAX);
sdefresne 2015/01/13 15:19:25 nit: why the random, using a fixed color would wor
70 CGFloat g = rand() / CGFloat(RAND_MAX);
71 CGFloat b = rand() / CGFloat(RAND_MAX);
72 UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
73 CGContextRef context = UIGraphicsGetCurrentContext();
74 CGContextSetRGBStrokeColor(context, r, g, b, 1.0);
75 CGContextSetRGBFillColor(context, r, g, b, 1.0);
76 CGContextFillRect(context,
77 CGRectMake(0.0, 0.0, imageSize.width, imageSize.height));
78 UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
79 UIGraphicsEndImageContext();
80 return image;
81 }
82
83 TEST(UIKitUIUtilTest, TestResizeImageInvalidInput) {
84 UIImage* actual;
85 UIImage* image = testImage(CGSizeMake(100, 50));
86 actual = ResizeImage(image, CGSizeZero,
87 YES, // preserveAspectRatio
88 NO); // trimToFit
89 EXPECT_FALSE(actual);
90
91 actual = ResizeImage(image, CGSizeMake(0.1, 0.1),
92 YES, // preserveAspectRatio
93 NO); // trimToFit
94 EXPECT_FALSE(actual);
95
96 actual = ResizeImage(image, CGSizeMake(-100, -100),
97 YES, // preserveAspectRatio
98 NO); // trimToFit
99 EXPECT_FALSE(actual);
100 }
101
102 TEST(UIKitUIUtilTest, TestInterpolateFromColorToColor) {
103 CGFloat colorOne = 50.0f / 255.0f;
104 CGFloat colorTwo = 100.0f / 255.0f;
105 CGFloat expectedOne = 50.0f / 255.0f;
106 CGFloat expectedTwo = 55.0f / 255.0f;
107 CGFloat expectedThree = 75.0f / 255.0f;
108 CGFloat expectedFour = 100.0f / 255.0f;
109
110 UIColor* firstColor =
111 [UIColor colorWithRed:colorOne green:colorOne blue:colorOne alpha:1.0];
112 UIColor* secondColor =
113 [UIColor colorWithRed:colorTwo green:colorTwo blue:colorTwo alpha:1.0];
114 ExpectInterpolatedColor(firstColor, secondColor, 0.0f, expectedOne);
115 ExpectInterpolatedColor(firstColor, secondColor, 0.1f, expectedTwo);
116 ExpectInterpolatedColor(firstColor, secondColor, 0.5f, expectedThree);
117 ExpectInterpolatedColor(firstColor, secondColor, 1.0f, expectedFour);
118 }
119
120 // Tests that InterpolateFromColorToColor() works for monochrome colors.
121 TEST(UIKitUIUtilTest, TestInterpolateFromColorToColorMonochrome) {
122 CGFloat kRGBComponent = 0.2;
123 UIColor* rgb = [UIColor colorWithRed:kRGBComponent
124 green:kRGBComponent
125 blue:kRGBComponent
126 alpha:1.0];
127 ASSERT_EQ(kCGColorSpaceModelRGB,
128 CGColorSpaceGetModel(CGColorGetColorSpace(rgb.CGColor)));
129
130 UIColor* white = [UIColor whiteColor];
131 ASSERT_EQ(kCGColorSpaceModelMonochrome,
132 CGColorSpaceGetModel(CGColorGetColorSpace(white.CGColor)));
133
134 UIColor* black = [UIColor blackColor];
135 ASSERT_EQ(kCGColorSpaceModelMonochrome,
136 CGColorSpaceGetModel(CGColorGetColorSpace(black.CGColor)));
137
138 // Interpolate between monochrome and rgb.
139 ExpectInterpolatedColor(black, rgb, 0.5, 0.1);
140 // Interpolate between two monochrome colors.
141 ExpectInterpolatedColor(black, white, 0.3, 0.3);
142 }
143
144 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698