|
1 | 1 | using System;
|
| 2 | +using System.Collections; |
2 | 3 | using CoreGraphics;
|
3 | 4 | using NUnit.Framework;
|
| 5 | +using NUnit.Framework.Constraints; |
4 | 6 | using UIKit;
|
5 | 7 |
|
6 | 8 | namespace Xamarin.Forms.ControlGallery.iOS.Tests
|
@@ -68,12 +70,46 @@ public static byte[] GetPixel(this UIImage bitmap, int x, int y)
|
68 | 70 | return pixel;
|
69 | 71 | }
|
70 | 72 |
|
| 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 | + |
71 | 91 | public static UIImage AssertColorAtPoint(this UIImage bitmap, UIColor expectedColor, int x, int y)
|
72 | 92 | {
|
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); |
75 | 96 |
|
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; |
77 | 113 | }
|
78 | 114 |
|
79 | 115 | public static UIImage AssertColorAtCenter(this UIImage bitmap, UIColor expectedColor)
|
|
0 commit comments