-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathTestSceneCompositeDrawable.cs
209 lines (183 loc) · 7.07 KB
/
TestSceneCompositeDrawable.cs
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
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Utils;
using osuTK;
using osuTK.Graphics;
namespace osu.Framework.Tests.Visual.Containers
{
public partial class TestSceneCompositeDrawable : FrameworkTestScene
{
[Test]
public void TestSortWithComparerChange()
{
SortableComposite composite = null;
Drawable firstItem = null;
Drawable lastItem = null;
AddStep("add composite", () => Child = composite = new SortableComposite());
AddStep("reverse comparer", () =>
{
firstItem = composite.InternalChildren[0];
lastItem = composite.InternalChildren[^1];
for (int i = 0; i < composite.InternalChildren.Count; i++)
((SortableBox)composite.InternalChildren[i]).Id = composite.InternalChildren.Count - 1 - i;
composite.Sort();
});
AddAssert("children reversed", () => composite.InternalChildren[0] == lastItem && composite.InternalChildren[^1] == firstItem);
}
[Test]
public void TestChangeChildDepthFailsIfNotCalledOnDirectChild()
{
Container parent = null;
Container nestedChild = null;
AddStep("create hierarchy", () => Child = parent = new Container
{
Child = new Container
{
Child = nestedChild = new Container()
}
});
AddStep("bad change child depth call fails", () => Assert.Throws<InvalidOperationException>(() => parent.ChangeChildDepth(nestedChild, 10)));
}
/// <summary>
/// Ensures that <see cref="Framework.Graphics.Transforms.Transformable.ClearTransforms"/> does not discard auto-sizing indefinitely.
/// </summary>
[Test]
public void TestClearTransformsOnDelayedAutoSize()
{
Container container = null;
bool clearedTransforms = false;
AddStep("create hierarchy", () =>
{
Child = container = new Container
{
Masking = true,
AutoSizeAxes = Axes.Both,
AutoSizeDuration = 1000,
Child = new Box { Size = new Vector2(100) },
};
clearedTransforms = false;
container.OnAutoSize += () => Schedule(() =>
{
if (clearedTransforms)
return;
container.ClearTransforms();
clearedTransforms = true;
});
});
AddAssert("transforms cleared", () => clearedTransforms);
AddUntilStep("container still autosized", () => container.Size == new Vector2(100));
}
[Test]
public void TestAutoSizeDuration()
{
Container parent = null;
Drawable child = null;
AddStep("create hierarchy", () =>
{
Child = parent = new Container
{
Masking = true,
AutoSizeAxes = Axes.Both,
AutoSizeDuration = 500,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Yellow,
},
new Container
{
Padding = new MarginPadding(50),
AutoSizeAxes = Axes.Both,
Child = child = new Box
{
Size = new Vector2(100),
Colour = Color4.Red,
}
}
}
};
});
AddSliderStep("AutoSizeDuration", 0f, 1500f, 500f, value =>
{
if (parent != null) parent.AutoSizeDuration = value;
});
AddSliderStep("Width", 0f, 300f, 100f, value =>
{
if (child != null) child.Width = value;
});
AddSliderStep("Height", 0f, 300f, 100f, value =>
{
if (child != null) child.Height = value;
});
}
[Test]
public void TestFinishAutoSizeTransforms()
{
Container parent = null;
Drawable child = null;
AddStep("create hierarchy", () =>
{
Child = parent = new Container
{
Masking = true,
AutoSizeAxes = Axes.Both,
AutoSizeDuration = 1000,
Name = "Parent",
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Yellow,
},
child = new Box
{
Size = new Vector2(100),
Colour = Color4.Red,
Alpha = 0.5f,
}
}
};
});
AddAssert("size matches child", () => Precision.AlmostEquals(parent.ChildSize, child.LayoutSize));
AddStep("resize child", () => child.Size = new Vector2(200));
AddAssert("size doesn't match child", () => !Precision.AlmostEquals(parent.ChildSize, child.LayoutSize));
AddStep("finish autosize transform", () => parent.FinishAutoSizeTransforms());
AddAssert("size matches child", () => Precision.AlmostEquals(parent.ChildSize, child.LayoutSize));
}
private partial class SortableComposite : CompositeDrawable
{
public SortableComposite()
{
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
AutoSizeAxes = Axes.Both;
for (int i = 0; i < 128; i++)
{
AddInternal(new SortableBox
{
Id = i,
Colour = new Color4(i / 255f, i / 255f, i / 255f, 1.0f),
Position = new Vector2(3 * i),
Size = new Vector2(50)
});
}
}
public void Sort() => SortInternal();
protected override int Compare(Drawable x, Drawable y)
=> ((SortableBox)x).Id.CompareTo(((SortableBox)y).Id);
}
private partial class SortableBox : Box
{
public int Id;
}
}
}