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

Unified 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: Remove rand() 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ios/chrome/browser/ui/uikit_ui_util.mm ('k') | ios/chrome/ios_chrome.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ios/chrome/browser/ui/uikit_ui_util_unittest.mm
diff --git a/ios/chrome/browser/ui/uikit_ui_util_unittest.mm b/ios/chrome/browser/ui/uikit_ui_util_unittest.mm
new file mode 100644
index 0000000000000000000000000000000000000000..c6400712f8aab22de7d789b81ce15919e2f73899
--- /dev/null
+++ b/ios/chrome/browser/ui/uikit_ui_util_unittest.mm
@@ -0,0 +1,141 @@
+// 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/basictypes.h"
+#include "base/ios/ios_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, TestResizeImageInvalidInput) {
+ UIImage* actual;
+ UIImage* image = testImage(CGSizeMake(100, 50));
+ actual = ResizeImage(image, CGSizeZero,
+ YES, // preserveAspectRatio
+ NO); // trimToFit
+ EXPECT_FALSE(actual);
+
+ actual = ResizeImage(image, CGSizeMake(0.1, 0.1),
+ YES, // preserveAspectRatio
+ NO); // trimToFit
+ EXPECT_FALSE(actual);
+
+ actual = ResizeImage(image, CGSizeMake(-100, -100),
+ YES, // preserveAspectRatio
+ NO); // trimToFit
+ 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
« no previous file with comments | « ios/chrome/browser/ui/uikit_ui_util.mm ('k') | ios/chrome/ios_chrome.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698