-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDirtyBoundsEdgeRegressionTests.cs
More file actions
282 lines (231 loc) · 9.81 KB
/
DirtyBoundsEdgeRegressionTests.cs
File metadata and controls
282 lines (231 loc) · 9.81 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
using Microsoft.Xna.Framework;
using Xunit;
namespace InkkSlinger.Tests;
public sealed class DirtyBoundsEdgeRegressionTests
{
[Fact]
public void RenderInvalidation_WithNullSource_DoesNotEscalateToFullFrameDirty()
{
var uiRoot = new UiRoot(new Panel());
uiRoot.SetDirtyRegionViewportForTests(new LayoutRect(0f, 0f, 200f, 200f));
uiRoot.ResetDirtyStateForTests();
uiRoot.NotifyInvalidation(UiInvalidationType.Render, null);
Assert.False(uiRoot.IsFullDirtyForTests());
}
[Fact]
public void RenderInvalidation_FromDetachedSource_IsIgnoredByLiveRoot()
{
var uiRoot = new UiRoot(new Panel());
uiRoot.SetDirtyRegionViewportForTests(new LayoutRect(0f, 0f, 200f, 200f));
uiRoot.ResetDirtyStateForTests();
uiRoot.NotifyInvalidation(UiInvalidationType.Render, new Border());
Assert.False(uiRoot.IsFullDirtyForTests());
Assert.Empty(uiRoot.GetDirtyRegionsSnapshotForTests());
Assert.Equal(0, uiRoot.RenderInvalidationCount);
}
[Fact]
public void DetachedElementInvalidations_DoNotMutateLiveRootState()
{
var root = new Panel();
var uiRoot = new UiRoot(root);
uiRoot.SetDirtyRegionViewportForTests(new LayoutRect(0f, 0f, 200f, 200f));
uiRoot.RebuildRenderListForTests();
uiRoot.CompleteDrawStateForTests();
uiRoot.ResetDirtyStateForTests();
root.ClearRenderInvalidationRecursive();
var detached = new Border();
detached.InvalidateMeasure();
detached.InvalidateArrange();
detached.InvalidateVisual();
Assert.False(uiRoot.IsFullDirtyForTests());
Assert.Empty(uiRoot.GetDirtyRegionsSnapshotForTests());
Assert.Equal(0, uiRoot.MeasureInvalidationCount);
Assert.Equal(0, uiRoot.ArrangeInvalidationCount);
Assert.Equal(0, uiRoot.RenderInvalidationCount);
}
[Fact]
public void AttachedElementInvalidations_StillMutateLiveRootState()
{
var root = new Panel();
var child = new Border();
root.AddChild(child);
var uiRoot = new UiRoot(root);
uiRoot.SetDirtyRegionViewportForTests(new LayoutRect(0f, 0f, 200f, 200f));
uiRoot.RebuildRenderListForTests();
uiRoot.CompleteDrawStateForTests();
uiRoot.ResetDirtyStateForTests();
root.ClearRenderInvalidationRecursive();
child.InvalidateMeasure();
Assert.True(uiRoot.MeasureInvalidationCount > 0);
Assert.True(uiRoot.ArrangeInvalidationCount > 0);
Assert.True(uiRoot.RenderInvalidationCount > 0);
}
[Fact]
public void ZeroSizeBounds_DoNotAddDirtyRegions()
{
var root = new Panel();
root.SetLayoutSlot(new LayoutRect(0f, 0f, 200f, 200f));
var child = new Border();
child.SetLayoutSlot(new LayoutRect(10f, 10f, 0f, 0f));
root.AddChild(child);
var uiRoot = new UiRoot(root);
uiRoot.SetDirtyRegionViewportForTests(new LayoutRect(0f, 0f, 200f, 200f));
uiRoot.RebuildRenderListForTests();
uiRoot.ResetDirtyStateForTests();
child.InvalidateVisual();
uiRoot.SynchronizeRetainedRenderListForTests();
Assert.Empty(uiRoot.GetDirtyRegionsSnapshotForTests());
Assert.False(uiRoot.IsFullDirtyForTests());
}
[Fact]
public void TransformMovement_WithDisjointBounds_TracksOldAndNewRegionsSeparately()
{
var root = new Panel();
root.SetLayoutSlot(new LayoutRect(0f, 0f, 240f, 120f));
var child = new Border();
child.SetLayoutSlot(new LayoutRect(10f, 10f, 20f, 20f));
root.AddChild(child);
var uiRoot = new UiRoot(root);
uiRoot.SetDirtyRegionViewportForTests(new LayoutRect(0f, 0f, 240f, 120f));
uiRoot.RebuildRenderListForTests();
uiRoot.ResetDirtyStateForTests();
child.RenderTransform = new TranslateTransform { X = 50f, Y = 0f };
uiRoot.SynchronizeRetainedRenderListForTests();
var regions = uiRoot.GetDirtyRegionsSnapshotForTests();
Assert.Equal(2, regions.Count);
Assert.Equal(10f, regions[0].X);
Assert.Equal(20f, regions[0].Width);
Assert.Equal(60f, regions[1].X);
Assert.Equal(20f, regions[1].Width);
}
[Fact]
public void DirtyRegionTracker_ClipsRegionsToViewportBounds()
{
var tracker = new DirtyRegionTracker(maxRegionCount: 8);
tracker.SetViewport(new LayoutRect(0f, 0f, 100f, 100f));
tracker.AddDirtyRegion(new LayoutRect(-20f, -10f, 60f, 40f));
Assert.False(tracker.IsFullFrameDirty);
Assert.Single(tracker.Regions);
Assert.Equal(0f, tracker.Regions[0].X);
Assert.Equal(0f, tracker.Regions[0].Y);
Assert.Equal(40f, tracker.Regions[0].Width);
Assert.Equal(30f, tracker.Regions[0].Height);
}
[Fact]
public void DirtyRegionTracker_NormalizesNegativeWidthAndHeight()
{
var tracker = new DirtyRegionTracker(maxRegionCount: 8);
tracker.SetViewport(new LayoutRect(0f, 0f, 100f, 100f));
tracker.AddDirtyRegion(new LayoutRect(20f, 20f, -10f, -6f));
Assert.False(tracker.IsFullFrameDirty);
Assert.Single(tracker.Regions);
Assert.Equal(10f, tracker.Regions[0].X);
Assert.Equal(14f, tracker.Regions[0].Y);
Assert.Equal(10f, tracker.Regions[0].Width);
Assert.Equal(6f, tracker.Regions[0].Height);
}
[Fact]
public void VisualStructureChange_WhenElementIsOutsideTree_IsNoOp()
{
var root = new Panel();
var uiRoot = new UiRoot(root);
uiRoot.RebuildRenderListForTests();
uiRoot.CompleteDrawStateForTests();
uiRoot.ResetDirtyStateForTests();
var outside = new Border();
uiRoot.NotifyVisualStructureChanged(outside, oldParent: null, newParent: null);
Assert.False(uiRoot.IsRenderListFullRebuildPendingForTests());
Assert.False(uiRoot.IsFullDirtyForTests());
}
[Fact]
public void ConstructingDetachedSubtree_DoesNotMutateLiveVisualStructureMetrics()
{
var root = new Panel();
root.AddChild(new Border());
var uiRoot = new UiRoot(root);
uiRoot.RebuildRenderListForTests();
uiRoot.CompleteDrawStateForTests();
uiRoot.ResetDirtyStateForTests();
root.ClearRenderInvalidationRecursive();
var beforeMetrics = uiRoot.GetMetricsSnapshot();
var beforePerf = uiRoot.GetPerformanceTelemetrySnapshotForTests();
var detachedRoot = new StackPanel();
for (var i = 0; i < 8; i++)
{
var row = new StackPanel();
row.AddChild(new Label { Content = $"Label {i}" });
row.AddChild(new Button { Content = $"Button {i}" });
detachedRoot.AddChild(row);
}
var afterMetrics = uiRoot.GetMetricsSnapshot();
var afterPerf = uiRoot.GetPerformanceTelemetrySnapshotForTests();
Assert.Equal(beforeMetrics.VisualStructureChangeCount, afterMetrics.VisualStructureChangeCount);
Assert.Equal(beforePerf.VisualIndexVersion, afterPerf.VisualIndexVersion);
Assert.False(uiRoot.IsRenderListFullRebuildPendingForTests());
}
[Fact]
public void VisualStructureChange_WhenOldParentIsInsideTree_TriggersRebuildAndDirty()
{
var root = new Panel();
var attachedParent = new Panel();
root.AddChild(attachedParent);
var uiRoot = new UiRoot(root);
uiRoot.RebuildRenderListForTests();
uiRoot.CompleteDrawStateForTests();
uiRoot.ResetDirtyStateForTests();
root.ClearRenderInvalidationRecursive();
var detachedElement = new Border();
uiRoot.NotifyVisualStructureChanged(detachedElement, oldParent: attachedParent, newParent: null);
Assert.True(uiRoot.IsRenderListFullRebuildPendingForTests());
Assert.True(uiRoot.IsFullDirtyForTests());
}
[Fact]
public void StructuralAndRenderInvalidationSameFrame_RebuildsDeterministically()
{
var root = new Panel();
var first = new Border();
root.AddChild(first);
var uiRoot = new UiRoot(root);
uiRoot.RebuildRenderListForTests();
uiRoot.ResetDirtyStateForTests();
root.ClearRenderInvalidationRecursive();
uiRoot.CompleteDrawStateForTests();
var second = new Border();
root.AddChild(second);
first.InvalidateVisual();
uiRoot.SynchronizeRetainedRenderListForTests();
var order = uiRoot.GetRetainedVisualOrderForTests();
Assert.Collection(
order,
visual => Assert.Same(root, visual),
visual => Assert.Same(first, visual),
visual => Assert.Same(second, visual));
Assert.False(uiRoot.IsRenderListFullRebuildPendingForTests());
}
[Fact]
public void DirtyRegionBudget_AtLimitRemainsPartial_AboveLimitFallsBackOnce()
{
var tracker = new DirtyRegionTracker(maxRegionCount: 12);
tracker.SetViewport(new LayoutRect(0f, 0f, 1200f, 120f));
for (var i = 0; i < 12; i++)
{
tracker.AddDirtyRegion(new LayoutRect(i * 100f, 0f, 10f, 10f));
}
Assert.False(tracker.IsFullFrameDirty);
Assert.Equal(12, tracker.RegionCount);
tracker.AddDirtyRegion(new LayoutRect(1190f, 0f, 10f, 10f));
Assert.True(tracker.IsFullFrameDirty);
Assert.Equal(1, tracker.FullRedrawFallbackCount);
}
[Fact]
public void DirtyCoverage_IsClampedToOneForFullFrameState()
{
var tracker = new DirtyRegionTracker(maxRegionCount: 2);
tracker.SetViewport(new LayoutRect(0f, 0f, 100f, 100f));
tracker.AddDirtyRegion(new LayoutRect(0f, 0f, 40f, 20f));
tracker.AddDirtyRegion(new LayoutRect(50f, 0f, 40f, 20f));
tracker.AddDirtyRegion(new LayoutRect(95f, 0f, 5f, 10f));
Assert.True(tracker.IsFullFrameDirty);
Assert.Equal(1d, tracker.GetDirtyAreaCoverage());
}
}