Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / ui / uikit_ui_util_unittest.mm
blob7795a3ebeababebfb8659508f800a55b443473c1
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/uikit_ui_util.h"
7 #include "base/basictypes.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "ios/chrome/browser/ui/ui_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #import "third_party/ocmock/OCMock/OCMock.h"
14 namespace {
16 void ExpectInterpolatedColor(UIColor* firstColor,
17                              UIColor* secondColor,
18                              CGFloat percentage,
19                              CGFloat expectedValue) {
20   UIColor* interpolatedColor =
21       InterpolateFromColorToColor(firstColor, secondColor, percentage);
22   CGFloat r, g, b, a;
23   [interpolatedColor getRed:&r green:&g blue:&b alpha:&a];
24   EXPECT_FLOAT_EQ(expectedValue, r);
25   EXPECT_FLOAT_EQ(expectedValue, g);
26   EXPECT_FLOAT_EQ(expectedValue, b);
27   EXPECT_FLOAT_EQ(1.0, a);
30 // Verify the assumption about UIViewController that on iPad all orientations
31 // are supported, and all orientations but Portrait Upside-Down on iPhone and
32 // iPod Touch.
33 TEST(UIKitUIUtilTest, UIViewControllerSupportedOrientationsTest) {
34   base::scoped_nsobject<UIViewController> viewController(
35       [[UIViewController alloc] initWithNibName:nil bundle:nil]);
36   if (IsIPadIdiom()) {
37     EXPECT_EQ(UIInterfaceOrientationMaskAll,
38               [viewController supportedInterfaceOrientations]);
39   } else {
40     EXPECT_EQ(UIInterfaceOrientationMaskAllButUpsideDown,
41               [viewController supportedInterfaceOrientations]);
42   }
45 TEST(UIKitUIUtilTest, TestGetUiFont) {
46   EXPECT_TRUE(GetUIFont(FONT_HELVETICA, false, 15.0));
47   EXPECT_TRUE(GetUIFont(FONT_HELVETICA_NEUE, true, 15.0));
50 // Verifies that greyImage never returns retina-scale images.
51 TEST(UIKitUIUtilTest, TestGreyImage) {
52   // Create an image using the device's scale factor.
53   const CGSize kSize = CGSizeMake(100, 100);
54   UIGraphicsBeginImageContextWithOptions(kSize, NO, 0.0);
55   UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
56   UIGraphicsEndImageContext();
58   // Verify the grey image's size and scale.
59   UIImage* greyImage = GreyImage(image);
60   EXPECT_EQ(kSize.width, greyImage.size.width);
61   EXPECT_EQ(kSize.height, greyImage.size.height);
62   EXPECT_EQ(1.0, greyImage.scale);
65 // Returns an image of random color in the same scale as the device main
66 // screen.
67 UIImage* testImage(CGSize imageSize) {
68   UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
69   CGContextRef context = UIGraphicsGetCurrentContext();
70   CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
71   CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
72   CGContextFillRect(context,
73                     CGRectMake(0.0, 0.0, imageSize.width, imageSize.height));
74   UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
75   UIGraphicsEndImageContext();
76   return image;
79 TEST(UIKitUIUtilTest, TestResizeImageInvalidInput) {
80   UIImage* actual;
81   UIImage* image = testImage(CGSizeMake(100, 50));
82   actual = ResizeImage(image, CGSizeZero, ProjectionMode::kAspectFit);
83   EXPECT_FALSE(actual);
85   actual = ResizeImage(image, CGSizeMake(0.1, 0.1), ProjectionMode::kAspectFit);
86   EXPECT_FALSE(actual);
88   actual =
89       ResizeImage(image, CGSizeMake(-100, -100), ProjectionMode::kAspectFit);
90   EXPECT_FALSE(actual);
93 TEST(UIKitUIUtilTest, TestInterpolateFromColorToColor) {
94   CGFloat colorOne = 50.0f / 255.0f;
95   CGFloat colorTwo = 100.0f / 255.0f;
96   CGFloat expectedOne = 50.0f / 255.0f;
97   CGFloat expectedTwo = 55.0f / 255.0f;
98   CGFloat expectedThree = 75.0f / 255.0f;
99   CGFloat expectedFour = 100.0f / 255.0f;
101   UIColor* firstColor =
102       [UIColor colorWithRed:colorOne green:colorOne blue:colorOne alpha:1.0];
103   UIColor* secondColor =
104       [UIColor colorWithRed:colorTwo green:colorTwo blue:colorTwo alpha:1.0];
105   ExpectInterpolatedColor(firstColor, secondColor, 0.0f, expectedOne);
106   ExpectInterpolatedColor(firstColor, secondColor, 0.1f, expectedTwo);
107   ExpectInterpolatedColor(firstColor, secondColor, 0.5f, expectedThree);
108   ExpectInterpolatedColor(firstColor, secondColor, 1.0f, expectedFour);
111 // Tests that InterpolateFromColorToColor() works for monochrome colors.
112 TEST(UIKitUIUtilTest, TestInterpolateFromColorToColorMonochrome) {
113   CGFloat kRGBComponent = 0.2;
114   UIColor* rgb = [UIColor colorWithRed:kRGBComponent
115                                  green:kRGBComponent
116                                   blue:kRGBComponent
117                                  alpha:1.0];
118   ASSERT_EQ(kCGColorSpaceModelRGB,
119             CGColorSpaceGetModel(CGColorGetColorSpace(rgb.CGColor)));
121   UIColor* white = [UIColor whiteColor];
122   ASSERT_EQ(kCGColorSpaceModelMonochrome,
123             CGColorSpaceGetModel(CGColorGetColorSpace(white.CGColor)));
125   UIColor* black = [UIColor blackColor];
126   ASSERT_EQ(kCGColorSpaceModelMonochrome,
127             CGColorSpaceGetModel(CGColorGetColorSpace(black.CGColor)));
129   // Interpolate between monochrome and rgb.
130   ExpectInterpolatedColor(black, rgb, 0.5, 0.1);
131   // Interpolate between two monochrome colors.
132   ExpectInterpolatedColor(black, white, 0.3, 0.3);
135 }  // namespace