-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathExpanderTests.cs
More file actions
265 lines (229 loc) · 8.16 KB
/
ExpanderTests.cs
File metadata and controls
265 lines (229 loc) · 8.16 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
using System.ComponentModel;
using System.Text;
using CommunityToolkit.Maui.Behaviors;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Views;
using FluentAssertions;
using Xunit;
namespace CommunityToolkit.Maui.UnitTests.Views;
public class ExpanderTests : BaseViewTest
{
readonly Maui.Views.Expander expander = new();
[Fact]
public void ExpanderShouldBeAssignedToIExpander()
{
expander.Should().BeAssignableTo<IExpander>();
}
[Fact]
public void CheckDefaultValues()
{
Assert.Equal(ExpandDirection.Down, expander.Direction);
Assert.False(expander.IsExpanded);
Assert.Null(expander.Content);
Assert.Null(expander.Header);
Assert.Null(expander.Command);
Assert.Null(expander.CommandParameter);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ExpandedChangedIsExpandedPassedWithEvent(bool expectedIsExpanded)
{
bool? isExpanded = null;
var action = new EventHandler<ExpandedChangedEventArgs>((_, e) => isExpanded = e.IsExpanded);
expander.ExpandedChanged += action;
((IExpander)expander).ExpandedChanged(expectedIsExpanded);
expander.ExpandedChanged -= action;
isExpanded.Should().Be(expectedIsExpanded);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ExpandedChangedCommandExecutedWithParams(bool expectedIsExpanded)
{
bool? isExpanded = null;
expander.Command = new Command<bool>(parameter => isExpanded = parameter);
expander.CommandParameter = expectedIsExpanded;
((IExpander)expander).ExpandedChanged(expectedIsExpanded);
isExpanded.Should().Be(expectedIsExpanded);
}
[Theory]
[InlineData((ExpandDirection)(-1))]
[InlineData((ExpandDirection)2)]
public void ExpanderDirectionThrowsInvalidEnumArgumentException(ExpandDirection direction)
{
Assert.Throws<InvalidEnumArgumentException>(() => expander.Direction = direction);
}
[Fact]
public void EnsureExpandedChanged()
{
var isExpanded_Initial = expander.IsExpanded;
var header = new View();
expander.Header = header;
expander.HeaderTapGestureRecognizer.SendTapped(header);
var isExpanded_Final = expander.IsExpanded;
Assert.True(isExpanded_Final);
Assert.False(isExpanded_Initial);
Assert.NotEqual(isExpanded_Initial, isExpanded_Final);
expander.HeaderTapGestureRecognizer.SendTapped(header);
Assert.False(expander.IsExpanded);
}
[Fact]
public void EnsureHandleHeaderTappedExecutesWhenHeaderTapped()
{
int handleHeaderTappedCount = 0;
bool didHandleHeaderTappedExecute = false;
var header = new View();
expander.Header = new View();
expander.HandleHeaderTapped = HandleHeaderTapped;
expander.HeaderTapGestureRecognizer.SendTapped(header);
Assert.True(didHandleHeaderTappedExecute);
Assert.Equal(1, handleHeaderTappedCount);
expander.HandleHeaderTapped = null;
expander.HeaderTapGestureRecognizer.SendTapped(header);
Assert.True(didHandleHeaderTappedExecute);
Assert.Equal(1, handleHeaderTappedCount);
void HandleHeaderTapped(TappedEventArgs tappedEventArgs)
{
handleHeaderTappedCount++;
didHandleHeaderTappedExecute = true;
}
}
[Fact]
public void EnsureDefaults()
{
var expander = new Maui.Views.Expander();
Assert.Equal(ExpanderDefaults.Direction, expander.Direction);
}
[Fact(Timeout = (int)TestDuration.Short)]
public async Task ExpanderRaisesEventsInCorrectOrderWhenExpanding()
{
TaskCompletionSource tcs = new();
var expander = new Maui.Views.Expander();
StringBuilder callOrder = new();
expander.ExpandedChanging += (_, _) => callOrder.Append("changing,");
expander.ExpandedChanged += (_, _) => { callOrder.Append("changed,"); tcs.TrySetResult(); };
var controller = new MockExpansionController();
controller.Expanding += (_, _) => callOrder.Append("controllerExpanding,");
controller.Collapsing += (_, _) => callOrder.Append("controllerCollapsing,");
expander.ExpansionController = controller;
expander.Header = new Label { Text = "Header" };
expander.Content = new Label { Text = "Hello" };
expander.IsExpanded = true;
await tcs.Task;
Assert.Equal("changing,controllerExpanding,changed,", callOrder.ToString());
}
[Fact(Timeout = (int)TestDuration.Short)]
public async Task ExpanderCallsCorrectControllerMethod()
{
TaskCompletionSource tcs = new();
var expander = new Maui.Views.Expander();
expander.ExpandedChanged += (_, _) => tcs.TrySetResult();
var controller = new MockExpansionController();
expander.ExpansionController = controller;
expander.Header = new Label { Text = "Header" };
expander.Content = new Label { Text = "Hello" };
expander.IsExpanded = true;
await tcs.Task;
Assert.Equal(1, controller.ExpandingCount);
Assert.Equal(0, controller.CollapsingCount);
}
[Fact(Timeout = (int)TestDuration.Short)]
public async Task ExpanderExpandsContentBecomesVisibleHeightRestored()
{
var tcs = new TaskCompletionSource();
var expander = new Maui.Views.Expander();
expander.ExpandedChanged += (_, _) => tcs.TrySetResult();
expander.Header = new Label { Text = "Header" };
expander.Content = new Label { Text = "Hello" };
expander.IsExpanded = true;
await tcs.Task;
var element = expander.Content as VisualElement;
Assert.NotNull(expander.ContentHost);
Assert.NotNull(expander.Content);
Assert.NotNull(element);
Assert.True(element.IsVisible);
Assert.Equal(-1, expander.ContentHost.HeightRequest);
}
[Fact(Timeout = (int)TestDuration.Short)]
public async Task ExpanderCollapseContentHiddenHeightZero()
{
var expander = new Maui.Views.Expander();
expander.IsExpanded = true;
var tcs = new TaskCompletionSource();
expander.ExpandedChanged += (_, _) => tcs.TrySetResult();
expander.Header = new Label { Text = "Header" };
expander.Content = new Label { Text = "Hello" };
expander.IsExpanded = false;
await tcs.Task;
var element = expander.Content as VisualElement;
Assert.NotNull(expander.ContentHost);
Assert.NotNull(expander.Content);
Assert.NotNull(element);
Assert.False(element.IsVisible);
Assert.Equal(0, expander.ContentHost.HeightRequest);
}
[Fact(Timeout = (int)TestDuration.Short)]
public async Task ExpanderIsExpandedSetBeforeContentAssignedInitialStateCorrect()
{
var expander = new Maui.Views.Expander();
expander.IsExpanded = true;
expander.Header = new Label { Text = "Header" };
expander.Content = new Label { Text = "Hello" };
var element = expander.Content as VisualElement;
Assert.NotNull(expander.ContentHost);
Assert.NotNull(element);
Assert.True(element.IsVisible);
Assert.Equal(-1, expander.ContentHost.HeightRequest);
}
[Fact]
public void AttachingAnimationBehaviorSetsExpansionController()
{
var expander = new Maui.Views.Expander();
var behavior = new ExpanderAnimationBehavior();
expander.Behaviors.Add(behavior);
Assert.IsType<ExpanderAnimationBehavior>(expander.Behaviors[0]);
Assert.IsType<ExpanderAnimationBehavior>(behavior);
Assert.IsType<ExpanderAnimationBehavior>(expander.ExpansionController);
Assert.Same(behavior, expander.ExpansionController);
}
[Fact(Timeout = (int)TestDuration.Short)]
public async Task OnExpandingAsyncAnimatesHeightCorrectly()
{
var tcs = new TaskCompletionSource();
var expander = new Maui.Views.Expander();
expander.ExpandedChanged += (_, _) => tcs.TrySetResult();
var behavior = new ExpanderAnimationBehavior();
expander.Behaviors.Add(behavior);
expander.Header = new Label { Text = "Header" };
expander.Content = new Label { Text = "Hello" };
Assert.NotNull(expander.ContentHost);
expander.ContentHost.HeightRequest = 0;
expander.IsExpanded = true;
await tcs.Task;
var element = expander.Content as VisualElement;
Assert.True(expander.IsExpanded);
Assert.Equal(-1, expander.ContentHost.HeightRequest);
Assert.NotNull(element);
Assert.True(element.IsVisible);
}
class MockExpansionController : IExpansionController
{
public event EventHandler? Expanding;
public event EventHandler? Collapsing;
public int ExpandingCount { get; private set; } = 0;
public int CollapsingCount { get; private set; } = 0;
public async Task OnExpandingAsync(Expander expander)
{
await Task.Yield();
ExpandingCount++;
Expanding?.Invoke(this, EventArgs.Empty);
}
public async Task OnCollapsingAsync(Expander expander)
{
await Task.Yield();
CollapsingCount++;
Collapsing?.Invoke(this, EventArgs.Empty);
}
}
}