-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackPanelTests.cs
More file actions
358 lines (302 loc) · 9.33 KB
/
StackPanelTests.cs
File metadata and controls
358 lines (302 loc) · 9.33 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (c) ktsu.dev
// All rights reserved.
// Licensed under the MIT license.
namespace ktsu.TUI.Test;
using ktsu.TUI.Core.Contracts;
using ktsu.TUI.Core.Elements.Layouts;
using ktsu.TUI.Core.Elements.Primitives;
using ktsu.TUI.Core.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
/// <summary>
/// Tests for StackPanel layout functionality
/// </summary>
[TestClass]
public sealed class StackPanelTests
{
/// <summary>
/// Tests that StackPanel initializes with correct default values
/// </summary>
[TestMethod]
public void StackPanelDefaultConstructorInitializesCorrectly()
{
// Arrange & Act
StackPanel stackPanel = [];
// Assert
Assert.AreEqual(Orientation.Vertical, stackPanel.Orientation);
Assert.AreEqual(0, stackPanel.Spacing);
Assert.IsNotNull(stackPanel.Children);
Assert.IsEmpty(stackPanel.Children);
Assert.IsTrue(stackPanel.IsVisible, "Default IsVisible should be true");
}
/// <summary>
/// Tests that StackPanel properly sets orientation
/// </summary>
[TestMethod]
public void StackPanelSetOrientationUpdatesOrientationProperty()
{
// Arrange
StackPanel stackPanel = [];
Orientation expectedOrientation = Orientation.Horizontal;
// Act
stackPanel.Orientation = expectedOrientation;
// Assert
Assert.AreEqual(expectedOrientation, stackPanel.Orientation);
}
/// <summary>
/// Tests that StackPanel invalidates when orientation changes
/// </summary>
[TestMethod]
public void StackPanelChangeOrientationTriggersInvalidation()
{
// Arrange
StackPanel stackPanel = new()
{ Orientation = Orientation.Vertical };
bool invalidated = false;
stackPanel.Invalidated += (sender, args) => invalidated = true;
// Act
stackPanel.Orientation = Orientation.Horizontal;
// Assert
Assert.IsTrue(invalidated, "Changing orientation should trigger invalidation event");
}
/// <summary>
/// Tests that StackPanel properly sets spacing
/// </summary>
[TestMethod]
public void StackPanelSetSpacingUpdatesSpacingProperty()
{
// Arrange
StackPanel stackPanel = [];
int expectedSpacing = 5;
// Act
stackPanel.Spacing = expectedSpacing;
// Assert
Assert.AreEqual(expectedSpacing, stackPanel.Spacing);
}
/// <summary>
/// Tests that StackPanel invalidates when spacing changes
/// </summary>
[TestMethod]
public void StackPanelChangeSpacingTriggersInvalidation()
{
// Arrange
StackPanel stackPanel = new()
{ Spacing = 0 };
bool invalidated = false;
stackPanel.Invalidated += (sender, args) => invalidated = true;
// Act
stackPanel.Spacing = 3;
// Assert
Assert.IsTrue(invalidated, "Changing spacing should trigger invalidation event");
}
/// <summary>
/// Tests that StackPanel can add child elements
/// </summary>
[TestMethod]
public void StackPanelAddChildAddsChildToCollection()
{
// Arrange
StackPanel stackPanel = [];
TextElement child = new("Test child");
// Act
stackPanel.AddChild(child);
// Assert
Assert.HasCount(1, stackPanel.Children);
Assert.AreEqual(child, stackPanel.Children.First());
Assert.AreEqual(stackPanel, child.Parent);
}
/// <summary>
/// Tests that StackPanel invalidates when child is added
/// </summary>
[TestMethod]
public void StackPanelAddChildTriggersInvalidation()
{
// Arrange
StackPanel stackPanel = [];
bool invalidated = false;
stackPanel.Invalidated += (sender, args) => invalidated = true;
// Act
stackPanel.AddChild(new TextElement("Test child"));
// Assert
Assert.IsTrue(invalidated, "Adding a child should trigger invalidation event");
}
/// <summary>
/// Tests that StackPanel can remove child elements
/// </summary>
[TestMethod]
public void StackPanelRemoveChildRemovesChildFromCollection()
{
// Arrange
StackPanel stackPanel = [];
TextElement child = new("Test child");
stackPanel.AddChild(child);
// Act
bool result = stackPanel.RemoveChild(child);
// Assert
Assert.IsTrue(result, "RemoveChild should return true when child is successfully removed");
Assert.IsEmpty(stackPanel.Children);
Assert.IsNull(child.Parent);
}
/// <summary>
/// Tests that StackPanel invalidates when child is removed
/// </summary>
[TestMethod]
public void StackPanelRemoveChildTriggersInvalidation()
{
// Arrange
StackPanel stackPanel = [];
TextElement child = new("Test child");
stackPanel.AddChild(child);
bool invalidated = false;
stackPanel.Invalidated += (sender, args) => invalidated = true;
// Act
stackPanel.RemoveChild(child);
// Assert
Assert.IsTrue(invalidated, "Removing a child should trigger invalidation event");
}
/// <summary>
/// Tests that StackPanel remove returns false for non-existent child
/// </summary>
[TestMethod]
public void StackPanelRemoveNonExistentChildReturnsFalse()
{
// Arrange
StackPanel stackPanel = [];
TextElement child = new("Test child");
// Act
bool result = stackPanel.RemoveChild(child);
// Assert
Assert.IsFalse(result, "RemoveChild should return false when child is not in the collection");
}
/// <summary>
/// Tests that StackPanel can clear all children
/// </summary>
[TestMethod]
public void StackPanelClearChildrenRemovesAllChildren()
{
// Arrange
StackPanel stackPanel = [];
TextElement child1 = new("Child 1");
TextElement child2 = new("Child 2");
stackPanel.AddChild(child1);
stackPanel.AddChild(child2);
// Act
stackPanel.ClearChildren();
// Assert
Assert.IsEmpty(stackPanel.Children);
Assert.IsNull(child1.Parent);
Assert.IsNull(child2.Parent);
}
/// <summary>
/// Tests that StackPanel invalidates when children are cleared
/// </summary>
[TestMethod]
public void StackPanelClearChildrenTriggersInvalidation()
{
// Arrange
StackPanel stackPanel = [];
stackPanel.AddChild(new TextElement("Test child"));
bool invalidated = false;
stackPanel.Invalidated += (sender, args) => invalidated = true;
// Act
stackPanel.ClearChildren();
// Assert
Assert.IsTrue(invalidated, "Clearing children should trigger invalidation event");
}
/// <summary>
/// Tests that StackPanel handles null child gracefully
/// </summary>
[TestMethod]
public void StackPanelAddNullChildThrowsArgumentNullException()
{
// Arrange
StackPanel stackPanel = [];
// Act & Assert
Assert.ThrowsExactly<ArgumentNullException>(() => stackPanel.AddChild(null!));
}
/// <summary>
/// Tests that StackPanel handles input by checking children in reverse order
/// </summary>
[TestMethod]
public void StackPanelHandleInputChecksChildrenInReverseOrder()
{
// Arrange
StackPanel stackPanel = [];
Mock<IUIElement> mockChild1 = new();
Mock<IUIElement> mockChild2 = new();
mockChild1.Setup(c => c.HandleInput(It.IsAny<InputResult>())).Returns(false);
mockChild1.Setup(c => c.IsVisible).Returns(true);
mockChild2.Setup(c => c.HandleInput(It.IsAny<InputResult>())).Returns(true);
mockChild2.Setup(c => c.IsVisible).Returns(true);
stackPanel.AddChild(mockChild1.Object);
stackPanel.AddChild(mockChild2.Object);
InputResult input = new()
{ Key = ConsoleKey.Enter, Modifiers = InputModifiers.None };
// Act
bool result = stackPanel.HandleInput(input);
// Assert
Assert.IsTrue(result, "HandleInput should return true when a child handles the input");
// mockChild2 (added last) should be called first and handle the input
mockChild2.Verify(c => c.HandleInput(input), Times.Once);
// mockChild1 should not be called because mockChild2 handled the input
mockChild1.Verify(c => c.HandleInput(input), Times.Never);
}
/// <summary>
/// Tests that StackPanel checks all children when none handle input
/// </summary>
[TestMethod]
public void StackPanelHandleInputNoChildrenHandleItChecksAllChildren()
{
// Arrange
StackPanel stackPanel = [];
Mock<IUIElement> mockChild1 = new();
Mock<IUIElement> mockChild2 = new();
mockChild1.Setup(c => c.HandleInput(It.IsAny<InputResult>())).Returns(false);
mockChild1.Setup(c => c.IsVisible).Returns(true);
mockChild2.Setup(c => c.HandleInput(It.IsAny<InputResult>())).Returns(false);
mockChild2.Setup(c => c.IsVisible).Returns(true);
stackPanel.AddChild(mockChild1.Object);
stackPanel.AddChild(mockChild2.Object);
InputResult input = new()
{ Key = ConsoleKey.Enter, Modifiers = InputModifiers.None };
// Act
bool result = stackPanel.HandleInput(input);
// Assert
Assert.IsFalse(result, "HandleInput should return false when no child handles the input");
// Both children should be called since neither handles the input
mockChild1.Verify(c => c.HandleInput(input), Times.Once);
mockChild2.Verify(c => c.HandleInput(input), Times.Once);
}
/// <summary>
/// Tests that StackPanel with no children returns false for input
/// </summary>
[TestMethod]
public void StackPanelHandleInputWithNoChildrenReturnsFalse()
{
// Arrange
StackPanel stackPanel = [];
InputResult input = new()
{ Key = ConsoleKey.Enter, Modifiers = InputModifiers.None };
// Act
bool result = stackPanel.HandleInput(input);
// Assert
Assert.IsFalse(result, "HandleInput should return false when there are no children");
}
/// <summary>
/// Tests orientation enum values
/// </summary>
[TestMethod]
[DataRow(Orientation.Vertical)]
[DataRow(Orientation.Horizontal)]
public void StackPanelSetOrientationValuesAcceptsAllValidValues(Orientation orientation)
{
// Arrange
StackPanel stackPanel = new()
{
// Act
Orientation = orientation
};
// Assert
Assert.AreEqual(orientation, stackPanel.Orientation);
}
}