-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
AssertionExtensions.cs
154 lines (129 loc) · 4.27 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
using AView = Android.Views.View;
using AColor = Android.Graphics.Color;
using Android.Graphics;
using System;
using NUnit.Framework;
using System.IO;
#if __ANDROID_29__
#else
using Android.Support.V7.Widget;
#endif
namespace Xamarin.Forms.Platform.Android.UnitTests
{
internal static class AssertionExtensions
{
public static string CreateColorAtPointError(this Bitmap bitmap, AColor expectedColor, int x, int y)
{
return CreateColorError(bitmap, $"Expected {expectedColor} at point {x},{y} in renderered view.");
}
public static string CreateColorError(this Bitmap bitmap, string message)
{
using (var ms = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, ms);
var imageAsString = Convert.ToBase64String(ms.ToArray());
return $"{message}. This is what it looked like:<img>{imageAsString}</img>";
}
}
public static AColor ColorAtPoint(this Bitmap bitmap, int x, int y, bool includeAlpha = false)
{
int pixel = bitmap.GetPixel(x, y);
int red = AColor.GetRedComponent(pixel);
int blue = AColor.GetBlueComponent(pixel);
int green = AColor.GetGreenComponent(pixel);
if (includeAlpha)
{
int alpha = AColor.GetAlphaComponent(pixel);
return AColor.Argb(alpha, red, green, blue);
}
else
{
return AColor.Rgb(red, green, blue);
}
}
public static Bitmap ToBitmap(this AView view)
{
var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888);
var canvas = new Canvas(bitmap);
canvas.Save();
canvas.Translate(0, 0);
view.Draw(canvas);
canvas.Restore();
return bitmap;
}
public static Bitmap AssertColorAtPoint(this Bitmap bitmap, AColor expectedColor, int x, int y)
{
Assert.That(bitmap.ColorAtPoint(x, y), Is.EqualTo(expectedColor),
() => bitmap.CreateColorAtPointError(expectedColor, x, y));
return bitmap;
}
public static Bitmap AssertColorAtCenter(this Bitmap bitmap, AColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width / 2, bitmap.Height / 2);
}
public static Bitmap AssertColorAtBottomLeft(this Bitmap bitmap, AColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, 0, 0);
}
public static Bitmap AssertColorAtBottomRight(this Bitmap bitmap, AColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width - 1, 0);
}
public static Bitmap AssertColorAtTopLeft(this Bitmap bitmap, AColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, 0, bitmap.Height - 1);
}
public static Bitmap AssertColorAtTopRight(this Bitmap bitmap, AColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width - 1, bitmap.Height - 1);
}
public static Bitmap AssertContainsColor(this AView view, AColor expectedColor)
{
var bitmap = view.ToBitmap();
for(int x = 1; x < view.Width; x++)
{
for(int y = 1; y < view.Height; y++)
{
if(bitmap.ColorAtPoint(x, y, true) == expectedColor)
{
return bitmap;
}
}
}
Assert.Fail(CreateColorError(bitmap, $"Color {expectedColor} not found."));
return bitmap;
}
public static Bitmap AssertColorAtPoint(this AView view, AColor expectedColor, int x, int y)
{
var bitmap = view.ToBitmap();
Assert.That(bitmap.ColorAtPoint(x, y), Is.EqualTo(expectedColor),
() => bitmap.CreateColorAtPointError(expectedColor, x, y));
return bitmap;
}
public static Bitmap AssertColorAtCenter(this AView view, AColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtCenter(expectedColor);
}
public static Bitmap AssertColorAtBottomLeft(this AView view, AColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtBottomLeft(expectedColor);
}
public static Bitmap AssertColorAtBottomRight(this AView view, AColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtBottomRight(expectedColor);
}
public static Bitmap AssertColorAtTopLeft(this AView view, AColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtTopLeft(expectedColor);
}
public static Bitmap AssertColorAtTopRight(this AView view, AColor expectedColor)
{
var bitmap = view.ToBitmap();
return bitmap.AssertColorAtTopRight(expectedColor);
}
}
}