Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / ios / chrome / browser / ui / uikit_ui_util_unittest.mm
blobc6400712f8aab22de7d789b81ce15919e2f73899
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/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"
15 namespace {
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);
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   }
46 TEST(UIKitUIUtilTest, TestGetUiFont) {
47   EXPECT_TRUE(GetUIFont(FONT_HELVETICA, false, 15.0));
48   EXPECT_TRUE(GetUIFont(FONT_HELVETICA_NEUE, true, 15.0));
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();
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);
66 // Returns an image of random color in the same scale as the device main
67 // screen.
68 UIImage* testImage(CGSize imageSize) {
69   UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
70   CGContextRef context = UIGraphicsGetCurrentContext();
71   CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
72   CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
73   CGContextFillRect(context,
74                     CGRectMake(0.0, 0.0, imageSize.width, imageSize.height));
75   UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
76   UIGraphicsEndImageContext();
77   return image;
80 TEST(UIKitUIUtilTest, TestResizeImageInvalidInput) {
81   UIImage* actual;
82   UIImage* image = testImage(CGSizeMake(100, 50));
83   actual = ResizeImage(image, CGSizeZero,
84                        YES,  // preserveAspectRatio
85                        NO);  // trimToFit
86   EXPECT_FALSE(actual);
88   actual = ResizeImage(image, CGSizeMake(0.1, 0.1),
89                        YES,  // preserveAspectRatio
90                        NO);  // trimToFit
91   EXPECT_FALSE(actual);
93   actual = ResizeImage(image, CGSizeMake(-100, -100),
94                        YES,  // preserveAspectRatio
95                        NO);  // trimToFit
96   EXPECT_FALSE(actual);
99 TEST(UIKitUIUtilTest, TestInterpolateFromColorToColor) {
100   CGFloat colorOne = 50.0f / 255.0f;
101   CGFloat colorTwo = 100.0f / 255.0f;
102   CGFloat expectedOne = 50.0f / 255.0f;
103   CGFloat expectedTwo = 55.0f / 255.0f;
104   CGFloat expectedThree = 75.0f / 255.0f;
105   CGFloat expectedFour = 100.0f / 255.0f;
107   UIColor* firstColor =
108       [UIColor colorWithRed:colorOne green:colorOne blue:colorOne alpha:1.0];
109   UIColor* secondColor =
110       [UIColor colorWithRed:colorTwo green:colorTwo blue:colorTwo alpha:1.0];
111   ExpectInterpolatedColor(firstColor, secondColor, 0.0f, expectedOne);
112   ExpectInterpolatedColor(firstColor, secondColor, 0.1f, expectedTwo);
113   ExpectInterpolatedColor(firstColor, secondColor, 0.5f, expectedThree);
114   ExpectInterpolatedColor(firstColor, secondColor, 1.0f, expectedFour);
117 // Tests that InterpolateFromColorToColor() works for monochrome colors.
118 TEST(UIKitUIUtilTest, TestInterpolateFromColorToColorMonochrome) {
119   CGFloat kRGBComponent = 0.2;
120   UIColor* rgb = [UIColor colorWithRed:kRGBComponent
121                                  green:kRGBComponent
122                                   blue:kRGBComponent
123                                  alpha:1.0];
124   ASSERT_EQ(kCGColorSpaceModelRGB,
125             CGColorSpaceGetModel(CGColorGetColorSpace(rgb.CGColor)));
127   UIColor* white = [UIColor whiteColor];
128   ASSERT_EQ(kCGColorSpaceModelMonochrome,
129             CGColorSpaceGetModel(CGColorGetColorSpace(white.CGColor)));
131   UIColor* black = [UIColor blackColor];
132   ASSERT_EQ(kCGColorSpaceModelMonochrome,
133             CGColorSpaceGetModel(CGColorGetColorSpace(black.CGColor)));
135   // Interpolate between monochrome and rgb.
136   ExpectInterpolatedColor(black, rgb, 0.5, 0.1);
137   // Interpolate between two monochrome colors.
138   ExpectInterpolatedColor(black, white, 0.3, 0.3);
141 }  // namespace