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

Commit ef2c3d8

Browse files
committed
Clean up Android pixel checking code; add more CornerRadius tests
1 parent 116cd9f commit ef2c3d8

File tree

6 files changed

+191
-97
lines changed

6 files changed

+191
-97
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using AView = Android.Views.View;
2+
using AColor = Android.Graphics.Color;
3+
using Android.Graphics;
4+
using System;
5+
using NUnit.Framework;
6+
using System.IO;
7+
8+
#if __ANDROID_29__
9+
#else
10+
using Android.Support.V7.Widget;
11+
#endif
12+
13+
namespace Xamarin.Forms.ControlGallery.Android.Tests
14+
{
15+
internal static class AssertionExtensions
16+
{
17+
public static string CreateColorAtPointError(this Bitmap bitmap, AColor expectedColor, int x, int y)
18+
{
19+
using (var ms = new MemoryStream())
20+
{
21+
bitmap.Compress(Bitmap.CompressFormat.Png, 0, ms);
22+
var imageAsString = Convert.ToBase64String(ms.ToArray());
23+
return $"Expected {expectedColor} at point {x},{y} in renderered view. This is what it looked like:<img>{imageAsString}</img>";
24+
}
25+
}
26+
27+
public static AColor ColorAtPoint(this Bitmap bitmap, int x, int y)
28+
{
29+
int pixel = bitmap.GetPixel(x, y);
30+
31+
int red = AColor.GetRedComponent(pixel);
32+
int blue = AColor.GetBlueComponent(pixel);
33+
int green = AColor.GetGreenComponent(pixel);
34+
35+
return AColor.Rgb(red, green, blue);
36+
}
37+
38+
public static Bitmap ToBitmap(this AView view)
39+
{
40+
var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888);
41+
var canvas = new Canvas(bitmap);
42+
canvas.Save();
43+
canvas.Translate(0, 0);
44+
view.Draw(canvas);
45+
canvas.Restore();
46+
47+
return bitmap;
48+
}
49+
50+
public static Bitmap AssertColorAtPoint(this Bitmap bitmap, AColor expectedColor, int x, int y)
51+
{
52+
Assert.That(bitmap.ColorAtPoint(x, y), Is.EqualTo(expectedColor),
53+
() => bitmap.CreateColorAtPointError(expectedColor, x, y));
54+
55+
return bitmap;
56+
}
57+
58+
public static Bitmap AssertColorAtCenter(this Bitmap bitmap, AColor expectedColor)
59+
{
60+
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width / 2, bitmap.Height / 2);
61+
}
62+
63+
public static Bitmap AssertColorAtBottomLeft(this Bitmap bitmap, AColor expectedColor)
64+
{
65+
return bitmap.AssertColorAtPoint(expectedColor, 0, 0);
66+
}
67+
68+
public static Bitmap AssertColorAtBottomRight(this Bitmap bitmap, AColor expectedColor)
69+
{
70+
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width - 1, 0);
71+
}
72+
73+
public static Bitmap AssertColorAtTopLeft(this Bitmap bitmap, AColor expectedColor)
74+
{
75+
return bitmap.AssertColorAtPoint(expectedColor, 0, bitmap.Height - 1);
76+
}
77+
78+
public static Bitmap AssertColorAtTopRight(this Bitmap bitmap, AColor expectedColor)
79+
{
80+
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width - 1, bitmap.Height - 1);
81+
}
82+
83+
public static Bitmap AssertColorAtPoint(this AView view, AColor expectedColor, int x, int y)
84+
{
85+
var bitmap = view.ToBitmap();
86+
Assert.That(bitmap.ColorAtPoint(x, y), Is.EqualTo(expectedColor),
87+
() => bitmap.CreateColorAtPointError(expectedColor, x, y));
88+
89+
return bitmap;
90+
}
91+
92+
public static Bitmap AssertColorAtCenter(this AView view, AColor expectedColor)
93+
{
94+
var bitmap = view.ToBitmap();
95+
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width / 2, bitmap.Height / 2);
96+
}
97+
98+
public static Bitmap AssertColorAtBottomLeft(this AView view, AColor expectedColor)
99+
{
100+
var bitmap = view.ToBitmap();
101+
return bitmap.AssertColorAtPoint(expectedColor, 0, 0);
102+
}
103+
104+
public static Bitmap AssertColorAtBottomRight(this AView view, AColor expectedColor)
105+
{
106+
var bitmap = view.ToBitmap();
107+
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width - 1, 0);
108+
}
109+
110+
public static Bitmap AssertColorAtTopLeft(this AView view, AColor expectedColor)
111+
{
112+
var bitmap = view.ToBitmap();
113+
return bitmap.AssertColorAtPoint(expectedColor, 0, bitmap.Height - 1);
114+
}
115+
116+
public static Bitmap AssertColorAtTopRight(this AView view, AColor expectedColor)
117+
{
118+
var bitmap = view.ToBitmap();
119+
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width - 1, bitmap.Height - 1);
120+
}
121+
}
122+
}

Xamarin.Forms.ControlGallery.Android/Tests/BackgroundColorTests.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public void ButtonBackgroundColorConsistent()
4444
{
4545
var expectedColor = button.BackgroundColor.ToAndroid();
4646
Layout(button, nativeButton);
47-
var nativeColor = GetColorAtCenter(nativeButton);
48-
Assert.That(nativeColor, Is.EqualTo(expectedColor));
47+
nativeButton.AssertColorAtCenter(expectedColor);
4948
}
5049
}
5150

@@ -64,8 +63,7 @@ public void ImageButtonBackgroundColorConsistent()
6463
{
6564
var expectedColor = button.BackgroundColor.ToAndroid();
6665
Layout(button, nativeButton);
67-
var nativeColor = GetColorAtCenter(nativeButton);
68-
Assert.That(nativeColor, Is.EqualTo(expectedColor));
66+
nativeButton.AssertColorAtCenter(expectedColor);
6967
}
7068
}
7169

@@ -85,8 +83,7 @@ public void FrameBackgroundColorConsistent()
8583
var expectedColor = frame.BackgroundColor.ToAndroid();
8684
var view = renderer.View;
8785
Layout(frame, view);
88-
var nativeColor = GetColorAtCenter(view);
89-
Assert.That(nativeColor, Is.EqualTo(expectedColor));
86+
view.AssertColorAtCenter(expectedColor);
9087
}
9188
}
9289

@@ -103,27 +100,5 @@ public void BackgroundColorConsistent(VisualElement element)
103100
Assert.That(nativeColor, Is.EqualTo(expectedColor));
104101
}
105102
}
106-
107-
//[Test, Category("BackgroundColor")]
108-
//public void FakeBGTest()
109-
//{
110-
// var ve = new BoxView
111-
// {
112-
// HeightRequest = 100,
113-
// WidthRequest = 300,
114-
// CornerRadius = 15,
115-
// BackgroundColor = Color.Red,
116-
// };
117-
118-
// using (var renderer = GetRenderer(ve))
119-
// {
120-
// var view = renderer.View;
121-
// Layout(ve, view);
122-
// var bitmap = ToBitmap(view);
123-
124-
125-
// AssertColorAtPoint(bitmap, Color.Bisque.ToAndroid(), 15, 15);
126-
// }
127-
//}
128103
}
129104
}

Xamarin.Forms.ControlGallery.Android/Tests/CornerRadiusTests.cs

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using NUnit.Framework.Internal;
33
using Xamarin.Forms.CustomAttributes;
44
using Xamarin.Forms.Platform.Android;
5-
using AColor = Android.Graphics.Color;
65

76
namespace Xamarin.Forms.ControlGallery.Android.Tests
87
{
@@ -12,38 +11,78 @@ public class CornerRadiusTests : PlatformTestFixture
1211
[Test, Category("CornerRadius"), Category("BoxView")]
1312
public void BoxviewCornerRadius()
1413
{
15-
var nativeBackground = new AColor(0, 0, 0, 255);
16-
var nativeForeground = Color.Red.ToAndroid();
17-
int height = 100;
18-
int width = 300;
14+
var boxView = new BoxView
15+
{
16+
HeightRequest = 100,
17+
WidthRequest = 200,
18+
CornerRadius = 15,
19+
BackgroundColor = Color.Red,
20+
};
1921

20-
var ve = new BoxView
22+
CheckCornerRadius(boxView);
23+
}
24+
25+
[Test, Category("CornerRadius"), Category("Button")]
26+
public void ButtonCornerRadius()
27+
{
28+
var backgroundColor = Color.Red;
29+
30+
var button = new Button
2131
{
22-
HeightRequest = height,
23-
WidthRequest = width,
32+
HeightRequest = 100,
33+
WidthRequest = 200,
2434
CornerRadius = 15,
25-
Color = Color.Red,
35+
BackgroundColor = backgroundColor,
2636
};
2737

28-
using (var renderer = GetRenderer(ve))
38+
CheckCornerRadius(button);
39+
}
40+
41+
[Test, Category("CornerRadius"), Category("Frame")]
42+
public void FrameCornerRadius()
43+
{
44+
var backgroundColor = Color.Red;
45+
46+
var frame = new Frame
2947
{
30-
var view = renderer.View;
31-
Layout(ve, view);
48+
HeightRequest = 100,
49+
WidthRequest = 200,
50+
CornerRadius = 15,
51+
BackgroundColor = backgroundColor,
52+
};
3253

33-
// TODO Provide a way to give a view and a set of point/color expected values
34-
// so that we don't need bitmap at this level
35-
// maybe even give the values as Forms colors
54+
CheckCornerRadius(frame);
55+
}
3656

37-
var bitmap = ToBitmap(view);
57+
[Test, Category("CornerRadius"), Category("ImageButton")]
58+
public void ImageButtonCornerRadius()
59+
{
60+
var backgroundColor = Color.Red;
3861

39-
// The corners should show the background color
40-
AssertColorAtPoint(bitmap, nativeBackground, 0, 0);
41-
AssertColorAtPoint(bitmap, nativeBackground, width - 1, 0);
42-
AssertColorAtPoint(bitmap, nativeBackground, width - 1, height - 1);
43-
AssertColorAtPoint(bitmap, nativeBackground, 0, height - 1);
62+
var button = new ImageButton
63+
{
64+
HeightRequest = 100,
65+
WidthRequest = 200,
66+
CornerRadius = 15,
67+
BackgroundColor = backgroundColor,
68+
};
4469

45-
// The center should be the foreground color
46-
AssertColorAtPoint(bitmap, nativeForeground, width/2, height/2);
70+
CheckCornerRadius(button);
71+
}
72+
73+
public void CheckCornerRadius(VisualElement visualElement)
74+
{
75+
using (var renderer = GetRenderer(visualElement))
76+
{
77+
var view = renderer.View;
78+
Layout(visualElement, view);
79+
80+
// The corners should show the background color
81+
view.AssertColorAtTopLeft(EmptyBackground)
82+
.AssertColorAtTopRight(EmptyBackground)
83+
.AssertColorAtBottomLeft(EmptyBackground)
84+
.AssertColorAtBottomRight(EmptyBackground)
85+
.AssertColorAtCenter(visualElement.BackgroundColor.ToAndroid());
4786
}
4887
}
4988
}

Xamarin.Forms.ControlGallery.Android/Tests/PlatformTestFixture.cs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
using Xamarin.Forms.Platform.Android;
55
using AView = Android.Views.View;
66
using AColor = Android.Graphics.Color;
7-
using Android.Graphics;
87
using Android.Views;
98
using System;
109
using AProgressBar = Android.Widget.ProgressBar;
1110
using ASearchView = Android.Widget.SearchView;
1211
using System.Collections.Generic;
1312
using NUnit.Framework;
14-
using System.IO;
1513

1614
#if __ANDROID_29__
1715
using AndroidX.AppCompat.Widget;
@@ -26,6 +24,8 @@ public class PlatformTestFixture
2624
{
2725
Context _context;
2826

27+
protected static AColor EmptyBackground = new AColor(0, 0, 0, 255);
28+
2929
// Sequence for generating test cases
3030
protected static IEnumerable<VisualElement> BasicElements
3131
{
@@ -261,48 +261,6 @@ protected EditText GetNativeControl(TimePicker timePicker)
261261
return viewRenderer.Control;
262262
}
263263

264-
protected AColor GetColorAtCenter(AView view)
265-
{
266-
var bitmap = ToBitmap(view);
267-
return ColorAtPoint(bitmap, view.Width/2, view.Height/2);
268-
}
269-
270-
protected Bitmap ToBitmap(AView view)
271-
{
272-
var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888);
273-
var canvas = new Canvas(bitmap);
274-
canvas.Save();
275-
canvas.Translate(0, 0);
276-
view.Draw(canvas);
277-
canvas.Restore();
278-
279-
return bitmap;
280-
}
281-
282-
protected AColor ColorAtPoint(Bitmap bitmap, int x, int y)
283-
{
284-
int pixel = bitmap.GetPixel(x, y);
285-
286-
int red = AColor.GetRedComponent(pixel);
287-
int blue = AColor.GetBlueComponent(pixel);
288-
int green = AColor.GetGreenComponent(pixel);
289-
290-
return AColor.Rgb(red, green, blue);
291-
}
292-
293-
protected void AssertColorAtPoint(Bitmap bitmap, AColor expectedColor, int x, int y)
294-
{
295-
Assert.That(ColorAtPoint(bitmap, x, y), Is.EqualTo(expectedColor),
296-
() => CreateColorAtPointError(bitmap, expectedColor, x, y));
297-
}
298-
299-
protected string CreateColorAtPointError(Bitmap bitmap, AColor expectedColor, int x, int y)
300-
{
301-
var ms = new MemoryStream();
302-
bitmap.Compress(Bitmap.CompressFormat.Png, 0, ms);
303-
return $"Expected {expectedColor} at point {x},{y} in renderered view. This is what it looked like:<img>{Convert.ToBase64String(ms.ToArray())}</img>";
304-
}
305-
306264
protected void Layout(VisualElement element, AView nativeView)
307265
{
308266
var size = element.Measure(double.PositiveInfinity, double.PositiveInfinity, MeasureFlags.IncludeMargins);

Xamarin.Forms.ControlGallery.Android/Xamarin.Forms.ControlGallery.Android.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<Compile Include="MainApplication.cs" />
9898
<Compile Include="PerformanceTrackerRenderer.cs" />
9999
<Compile Include="PlatformSpecificCoreGalleryFactory.cs" />
100+
<Compile Include="Tests\AssertionExtensions.cs" />
100101
<Compile Include="Tests\BackgroundColorTests.cs" />
101102
<Compile Include="Tests\CornerRadiusTests.cs" />
102103
<Compile Include="Tests\IsEnabledTests.cs" />

Xamarin.Forms.Controls/GalleryPages/PlatformTestsGallery/PlatformTestsConsole.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ async Task Run()
7171
// e.g. var filter = new TestNameContainsFilter("Bugzilla");
7272
// or var filter = new CategoryFilter("Picker");
7373

74-
var filter = new CategoryFilter("CornerRadius");
75-
await Task.Run(() => _runner.Run(filter)).ConfigureAwait(false);
74+
await Task.Run(() => _runner.Run()).ConfigureAwait(false);
7675
}
7776

7877
void DisplayOverallResult()

0 commit comments

Comments
 (0)