forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBorderTests.cs
More file actions
317 lines (267 loc) · 9.71 KB
/
BorderTests.cs
File metadata and controls
317 lines (267 loc) · 9.71 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.DeviceTests.ImageAnalysis;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Xunit;
namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.Border)]
public partial class BorderTests : ControlsHandlerTestBase
{
void SetupBuilder()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<Border, BorderHandler>();
handlers.AddHandler<Grid, LayoutHandler>();
handlers.AddHandler<Label, LabelHandler>();
});
});
}
#if ANDROID
[Fact("Checks that the default background is transparent")]
public async Task DefaultBackgroundIsTransparent ()
{
// We use a Grid container to set a background color and then make sure that the Border background
// is transparent by making sure that the parent Grid's Blue background shows through.
var grid = new Grid
{
ColumnDefinitions = new ColumnDefinitionCollection()
{
new ColumnDefinition(GridLength.Star)
},
RowDefinitions = new RowDefinitionCollection()
{
new RowDefinition(GridLength.Star)
},
BackgroundColor = Colors.Blue,
WidthRequest = 200,
HeightRequest = 200
};
var border = new Border {
WidthRequest = 200,
HeightRequest = 200,
Stroke = Colors.Red,
StrokeThickness = 10
};
grid.Add(border, 0, 0);
await CreateHandlerAsync<BorderHandler>(border);
await CreateHandlerAsync<LayoutHandler>(grid);
var bitmap = await GetRawBitmap(grid, typeof(LayoutHandler));
Assert.Equal(200, bitmap.Width, 2d);
Assert.Equal(200, bitmap.Height, 2d);
// Analyze red border - we expect it to fill a 200x200 area
var redBlob = ConnectedComponentAnalysis.FindConnectedPixels(bitmap, (c) => c.Red > .5).Single();
Assert.Equal(200, redBlob.Width, 2d);
Assert.Equal(200, redBlob.Height, 2d);
// Analyze the blue blob - it should fill the inside of the border (minus the stroke thickness)
var blueBlobs = ConnectedComponentAnalysis.FindConnectedPixels(bitmap, (c) => c.Blue > .5);
// Note: There is a 1px blue border around the red border, so we need to find the one
// that represents the inner bounds of the border.
var innerBlob = blueBlobs[0];
for (int i = 1; i < blueBlobs.Count; i++)
{
if (blueBlobs[i].MinColumn > innerBlob.MinColumn && blueBlobs[i].MaxColumn < innerBlob.MaxColumn)
innerBlob = blueBlobs[i];
}
Assert.Equal(180, innerBlob.Width, 2d);
Assert.Equal(180, innerBlob.Height, 2d);
}
#endif
[Fact(DisplayName = "Rounded Rectangle Border occupies correct space")]
public async Task RoundedRectangleBorderLayoutIsCorrect()
{
Color stroke = Colors.Black;
const int strokeThickness = 4;
const int radius = 20;
var grid = new Grid()
{
ColumnDefinitions = new ColumnDefinitionCollection()
{
new ColumnDefinition(GridLength.Star),
new ColumnDefinition(GridLength.Star)
},
RowDefinitions = new RowDefinitionCollection()
{
new RowDefinition(GridLength.Star),
new RowDefinition(GridLength.Star)
},
BackgroundColor = Colors.White
};
var shape = new RoundRectangle()
{
CornerRadius = new CornerRadius(radius),
};
var border = new Border()
{
StrokeShape = shape,
Stroke = stroke,
StrokeThickness = strokeThickness,
BackgroundColor = Colors.Red,
};
grid.Add(border, 0, 0);
grid.WidthRequest = 200;
grid.HeightRequest = 200;
await CreateHandlerAsync<BorderHandler>(border);
await CreateHandlerAsync<LayoutHandler>(grid);
Point[] corners = new Point[4]
{
new Point(0, 0), // upper-left corner
new Point(100, 0), // upper-right corner
new Point(0, 100), // lower-left corner
new Point(100, 100) // lower-right corner
};
var points = new Point[16];
var colors = new Color[16];
int index = 0;
// To calculate the x and y offsets (from the center) for a 45-45-90 triangle, we can use the radius as the hypotenuse
// which means that the x and y offsets would be radius / sqrt(2).
var xy = radius - (radius / Math.Sqrt(2));
for (int i = 0; i < corners.Length; i++)
{
int xdir = i == 0 || i == 2 ? 1 : -1;
int ydir = i == 0 || i == 1 ? 1 : -1;
// This marks the outside edge of the rounded corner.
var outerX = corners[i].X + (xdir * xy);
var outerY = corners[i].Y + (ydir * xy);
// Add stroke thickness to find the inner edge of the rounded corner.
var innerX = outerX + (xdir * strokeThickness);
var innerY = outerY + (ydir * strokeThickness);
// Verify that the color outside of the rounded corner is the parent's color (White)
points[index] = new Point(outerX - (xdir * 0.25), outerY - (ydir * 0.25));
colors[index] = Colors.White;
index++;
// Verify that the rounded corner stroke is where we expect it to be
points[index] = new Point(outerX + (xdir * 1.25), outerY + (ydir * 1.25));
colors[index] = stroke;
index++;
points[index] = new Point(innerX - (xdir * 1.25), innerY - (ydir * 1.25));
colors[index] = stroke;
index++;
// Verify that the background color starts where we'd expect it to start
points[index] = new Point(innerX + (xdir * 0.25), innerY + (ydir * 0.25));
colors[index] = border.BackgroundColor;
index++;
}
await AssertColorsAtPoints(grid, typeof(LayoutHandler), colors, points);
}
[Fact(DisplayName = "StrokeThickness does not inset stroke path")]
public async Task BorderStrokeThicknessDoesNotInsetStrokePath()
{
var grid = new Grid()
{
ColumnDefinitions = new ColumnDefinitionCollection()
{
new ColumnDefinition(GridLength.Star),
new ColumnDefinition(GridLength.Star)
},
RowDefinitions = new RowDefinitionCollection()
{
new RowDefinition(GridLength.Star),
new RowDefinition(GridLength.Star)
},
BackgroundColor = Colors.White
};
var border = new Border()
{
Stroke = Colors.Black,
StrokeThickness = 10,
BackgroundColor = Colors.Red
};
grid.Add(border, 0, 0);
grid.WidthRequest = 200;
grid.HeightRequest = 200;
await CreateHandlerAsync<BorderHandler>(border);
await CreateHandlerAsync<LayoutHandler>(grid);
var points = new Point[2];
var colors = new Color[2];
#if IOS
// FIXME: iOS seems to have a white boarder around the Border stroke
int offset = 1;
#else
int offset = 0;
#endif
// Verify that the stroke is where we expect
points[0] = new Point(offset + 1, offset + 1);
colors[0] = Colors.Black;
// Verify that the stroke is only as thick as we expect
points[1] = new Point(offset + border.StrokeThickness + 1, offset + border.StrokeThickness + 1);
colors[1] = Colors.Red;
await AssertColorsAtPoints(grid, typeof(LayoutHandler), colors, points);
}
// NOTE: this test is slightly different than MemoryTests.HandlerDoesNotLeak
// It adds a child to the Border, which is a worthwhile test case.
[Fact("Border Does Not Leak")]
public async Task DoesNotLeak()
{
SetupBuilder();
WeakReference platformViewReference = null;
WeakReference handlerReference = null;
await InvokeOnMainThreadAsync(() =>
{
var layout = new Grid();
var border = new Border();
var label = new Label();
border.Content = label;
layout.Add(border);
var handler = CreateHandler<LayoutHandler>(layout);
handlerReference = new WeakReference(label.Handler);
platformViewReference = new WeakReference(label.Handler.PlatformView);
});
await AssertionExtensions.WaitForGC(handlerReference, platformViewReference);
}
[Fact(DisplayName = "Border With Stroke Shape And Name Does Not Leak")]
public async Task DoesNotLeakWithStrokeShape()
{
SetupBuilder();
WeakReference platformViewReference = null;
WeakReference handlerReference = null;
await InvokeOnMainThreadAsync(() =>
{
var layout = new Grid();
var border = new Border();
var rect = new RoundRectangle();
border.StrokeShape = rect;
layout.Add(border);
var nameScope = new NameScope();
((INameScope)nameScope).RegisterName("Border", border);
layout.transientNamescope = nameScope;
border.transientNamescope = nameScope;
rect.transientNamescope = nameScope;
var handler = CreateHandler<LayoutHandler>(layout);
handlerReference = new WeakReference(border.Handler);
platformViewReference = new WeakReference(border.Handler.PlatformView);
});
await AssertionExtensions.WaitForGC(handlerReference, platformViewReference);
}
[Fact("Ensures the border renders the expected size - Issue 15339")]
public async Task BorderAndStrokeIsCorrectSize()
{
double borderThickness = 10;
Border border = new Border() { WidthRequest = 200, HeightRequest = 100 };
border.BackgroundColor = Colors.Red;
border.Stroke = Colors.Blue;
border.StrokeThickness = borderThickness;
// This is randomly failing on iOS, so let's add a timeout to avoid device tests running for hours
var bitmap = await GetRawBitmap(border, typeof(BorderHandler)).WaitAsync(TimeSpan.FromSeconds(5));
Assert.Equal(200, bitmap.Width, 2d);
Assert.Equal(100, bitmap.Height, 2d);
// Analyze blue border - we expect it to fill the 200x100 area
var blueBlob = ConnectedComponentAnalysis.FindConnectedPixels(bitmap, (c) => c.Blue > .5).Single();
Assert.Equal(200, blueBlob.Width, 2d);
Assert.Equal(100, blueBlob.Height, 2d);
// Analyze red inside- we expect it to fill the area minus the stroke thickness
var redBlob = ConnectedComponentAnalysis.FindConnectedPixels(bitmap, (c) => c.Red > .5).Single();
Assert.Equal(180, redBlob.Width, 2d);
Assert.Equal(80, redBlob.Height, 2d);
}
}
}