xamarin-macios/tests/monotouch-test/CoreGraphics/GradientTest.cs

100 строки
3.4 KiB
C#

//
// Unit tests for CGGradient
//
// Authors:
// Sebastien Pouliot <sebastien@xamarin.com>
//
// Copyright 2012, 2015 Xamarin Inc. All rights reserved.
//
using System;
using System.Runtime.InteropServices;
using Foundation;
#if MONOMAC
using AppKit;
using UIColor = AppKit.NSColor;
#else
using UIKit;
#endif
using CoreGraphics;
using ObjCRuntime;
using NUnit.Framework;
namespace MonoTouchFixtures.CoreGraphics {
[TestFixture]
[Preserve (AllMembers = true)]
public class GradientTest {
static CGColor [] array = {
TestRuntime.GetCGColor (UIColor.Black),
TestRuntime.GetCGColor (UIColor.Clear),
TestRuntime.GetCGColor (UIColor.Blue),
};
[Test]
public void Colorspace_Null ()
{
using (var g = new CGGradient (null, array)) {
Assert.That (g.Handle, Is.Not.EqualTo (IntPtr.Zero), "null,CGColor[]");
}
using (var g = new CGGradient (null, array, new nfloat [3] { 0f, 1f, 0.5f })) {
Assert.That (g.Handle, Is.Not.EqualTo (IntPtr.Zero), "null,CGColor[],float[]");
}
using (var g = new CGGradient (null, array, new nfloat [3] { 0f, 1f, 0.5f })) {
Assert.That (g.Handle, Is.Not.EqualTo (IntPtr.Zero), "null,CGColor[],float[]");
}
}
static CGColorSpace [] spaces = {
CGColorSpace.CreateDeviceGray (),
CGColorSpace.CreateDeviceRGB (),
#if !XAMCORE_3_0
CGColorSpace.Null
#endif
};
[Test]
public void Colorspaces ()
{
foreach (var cs in spaces) {
using (var g = new CGGradient (null, array, new nfloat [3] { 0f, 1f, 0.5f })) {
Assert.That (g.Handle, Is.Not.EqualTo (IntPtr.Zero), cs.ToString ());
}
}
}
[DllImport(Constants.CoreGraphicsLibrary)]
extern static /* CGGradientRef */ IntPtr CGGradientCreateWithColorComponents (/* CGColorSpaceRef */ IntPtr colorspace, /* CGFloat[] */ nfloat [] components, /* CGFloat[] */ nfloat [] locations, /* size_t */ nint count);
[DllImport(Constants.CoreGraphicsLibrary)]
extern static /* CGGradientRef */ IntPtr CGGradientCreateWithColors (/* CGColorSpaceRef */ IntPtr space, /* CFArrayRef */ IntPtr colors, /* CGFloat[] */ nfloat [] locations);
[Test]
public void Nullable ()
{
// either a null CGColorSpace or a null CGFloat* array will return nil, i.e. not a valid instance
using (var cs = CGColorSpace.CreateDeviceGray ())
Assert.That (CGGradientCreateWithColorComponents (cs.Handle, null, null, 0), Is.EqualTo (IntPtr.Zero), "CGGradientCreateWithColorComponents-1");
Assert.That (CGGradientCreateWithColorComponents (IntPtr.Zero, new nfloat [3] { 0f, 1f, 0.5f }, null, 0), Is.EqualTo (IntPtr.Zero), "CGGradientCreateWithColorComponents-2");
// a null CFArray won't return a valid instance
using (var cs = CGColorSpace.CreateDeviceGray ())
Assert.That (CGGradientCreateWithColors (cs.Handle, IntPtr.Zero, null), Is.EqualTo (IntPtr.Zero), "CGGradientCreateWithColors-1");
// a null CGColorSpace can return a valid instance
using (var a = NSArray.FromNSObjects (array))
Assert.That (CGGradientCreateWithColors (IntPtr.Zero, a.Handle, null), Is.Not.EqualTo (IntPtr.Zero), "CGGradientCreateWithColors-2");
}
[Test]
public void GradientDrawingOptions ()
{
var gdo = CGGradientDrawingOptions.DrawsAfterEndLocation | CGGradientDrawingOptions.DrawsBeforeStartLocation;
// this would be "3" without a [Flags] attribute
Assert.That (gdo.ToString (), Is.EqualTo ("DrawsBeforeStartLocation, DrawsAfterEndLocation"), "ToString/Flags");
}
}
}