/
AssertionExtensions.cs
155 lines (122 loc) · 4.49 KB
/
AssertionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using CoreGraphics;
using NUnit.Framework;
using UIKit;
namespace Xamarin.Forms.Platform.iOS.UnitTests
{
internal static class AssertionExtensions
{
public static string CreateColorAtPointError(this UIImage bitmap, UIColor expectedColor, int x, int y)
{
var data = bitmap.AsPNG();
var imageAsString = data.GetBase64EncodedString(Foundation.NSDataBase64EncodingOptions.None);
return $"Expected {expectedColor} at point {x},{y} in renderered view. This is what it looked like:<img>{imageAsString}</img>";
}
public static UIImage ToBitmap(this UIView view)
{
var imageRect = new CGRect(0, 0, view.Frame.Width, view.Frame.Height);
UIGraphics.BeginImageContext(imageRect.Size);
var context = UIGraphics.GetCurrentContext();
view.Layer.RenderInContext(context);
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return image;
}
public static UIColor ColorAtPoint(this UIImage bitmap, int x, int y)
{
var pixel = bitmap.GetPixel(x, y);
// Returned pixel data is B, G, R, A (ARGB little endian byte order)
var color = new UIColor(pixel[2] / 255.0f, pixel[1] / 255.0f, pixel[0] / 255.0f, pixel[3] / 255.0f);
return color;
}
public static byte[] GetPixel(this UIImage bitmap, int x, int y)
{
var cgImage = bitmap.CGImage.WithColorSpace(CGColorSpace.CreateDeviceRGB());
// Grab the raw image data
var nsData = cgImage.DataProvider.CopyData();
// Copy the data into a buffer
var dataBytes = new byte[nsData.Length];
System.Runtime.InteropServices.Marshal.Copy(nsData.Bytes, dataBytes, 0, (int)nsData.Length);
// Figure out where the pixel we care about is
var pixelLocation = (cgImage.BytesPerRow * y) + (4 * x);
var pixel = new byte[4]
{
dataBytes[pixelLocation],
dataBytes[pixelLocation + 1],
dataBytes[pixelLocation + 2],
dataBytes[pixelLocation + 3],
};
return pixel;
}
public static UIImage AssertColorAtPoint(this UIImage bitmap, UIColor expectedColor, int x, int y)
{
try
{
var cap = bitmap.ColorAtPoint(x, y);
if (!ColorComparison.ARGBEquivalent(cap, expectedColor))
{
System.Diagnostics.Debug.WriteLine("Here");
}
Assert.That(cap, Is.EqualTo(expectedColor).Using<UIColor>(ColorComparison.ARGBEquivalent),
() => bitmap.CreateColorAtPointError(expectedColor, x, y));
return bitmap;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
return null;
}
public static UIImage AssertColorAtCenter(this UIImage bitmap, UIColor expectedColor)
{
AssertColorAtPoint(bitmap, expectedColor, (int)bitmap.Size.Width / 2, (int)bitmap.Size.Height / 2);
return bitmap;
}
public static UIImage AssertColorAtBottomLeft(this UIImage bitmap, UIColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, 0, 0);
}
public static UIImage AssertColorAtBottomRight(this UIImage bitmap, UIColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, (int)bitmap.Size.Width - 1, 0);
}
public static UIImage AssertColorAtTopLeft(this UIImage bitmap, UIColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, 0, (int)bitmap.Size.Height - 1);
}
public static UIImage AssertColorAtTopRight(this UIImage bitmap, UIColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, (int)bitmap.Size.Width - 1, (int)bitmap.Size.Height - 1);
}
public static UIImage AssertColorAtPoint(this UIView view, UIColor expectedColor, int x, int y)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtPoint(expectedColor, x, y);
}
public static UIImage AssertColorAtCenter(this UIView view, UIColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtCenter(expectedColor);
}
public static UIImage AssertColorAtBottomLeft(this UIView view, UIColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtBottomLeft(expectedColor);
}
public static UIImage AssertColorAtBottomRight(this UIView view, UIColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtBottomRight(expectedColor);
}
public static UIImage AssertColorAtTopLeft(this UIView view, UIColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtTopLeft(expectedColor);
}
public static UIImage AssertColorAtTopRight(this UIView view, UIColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtTopRight(expectedColor);
}
}
}