Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit a27e58f

Browse files
committed
Add tolerance for color comparisons
1 parent e136700 commit a27e58f

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

Xamarin.Forms.ControlGallery.iOS/Tests/AssertionExtensions.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections;
23
using CoreGraphics;
34
using NUnit.Framework;
5+
using NUnit.Framework.Constraints;
46
using UIKit;
57

68
namespace Xamarin.Forms.ControlGallery.iOS.Tests
@@ -68,12 +70,46 @@ public static byte[] GetPixel(this UIImage bitmap, int x, int y)
6870
return pixel;
6971
}
7072

73+
static bool ARGBEquivalent(UIColor color1, UIColor color2)
74+
{
75+
color1.GetRGBA(out nfloat red1, out nfloat green1, out nfloat blue1, out nfloat alpha1);
76+
color2.GetRGBA(out nfloat red2, out nfloat green2, out nfloat blue2, out nfloat alpha2);
77+
78+
const double tolerance = 0.000001;
79+
80+
return Equal(red1, red2, tolerance)
81+
&& Equal(green1, green2, tolerance)
82+
&& Equal(blue1, blue2, tolerance)
83+
&& Equal(alpha1, alpha2, tolerance);
84+
}
85+
86+
static bool Equal(nfloat v1, nfloat v2, double tolerance)
87+
{
88+
return Math.Abs(v1 - v2) <= tolerance;
89+
}
90+
7191
public static UIImage AssertColorAtPoint(this UIImage bitmap, UIColor expectedColor, int x, int y)
7292
{
73-
Assert.That(bitmap.ColorAtPoint(x, y), Is.EqualTo(expectedColor),
74-
() => bitmap.CreateColorAtPointError(expectedColor, x, y));
93+
try
94+
{
95+
var cap = bitmap.ColorAtPoint(x, y);
7596

76-
return bitmap;
97+
if (!ARGBEquivalent(cap, expectedColor))
98+
{
99+
System.Diagnostics.Debug.WriteLine("Here");
100+
}
101+
102+
Assert.That(cap, Is.EqualTo(expectedColor).Using<UIColor>(ARGBEquivalent),
103+
() => bitmap.CreateColorAtPointError(expectedColor, x, y));
104+
105+
return bitmap;
106+
}
107+
catch (Exception ex)
108+
{
109+
System.Diagnostics.Debug.WriteLine(ex);
110+
}
111+
112+
return null;
77113
}
78114

79115
public static UIImage AssertColorAtCenter(this UIImage bitmap, UIColor expectedColor)

0 commit comments

Comments
 (0)