Skip to content

Commit 387806a

Browse files
Add Debounce test for stopping the timer manually
1 parent df91883 commit 387806a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

components/Extensions/tests/DispatcherQueueTimerExtensionTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,45 @@ public async Task DispatcherQueueTimer_Debounce_Trailing()
5151
Assert.AreEqual(1, triggeredCount, "Expected to run once.");
5252
}
5353

54+
[TestCategory("DispatcherQueueTimerExtensions")]
55+
[UIThreadTestMethod]
56+
public async Task DispatcherQueueTimer_Debounce_Trailing_Stop()
57+
{
58+
var debounceTimer = DispatcherQueue.GetForCurrentThread().CreateTimer();
59+
60+
var triggeredCount = 0;
61+
string? triggeredValue = null;
62+
63+
var value = "He";
64+
debounceTimer.Debounce(
65+
() =>
66+
{
67+
triggeredCount++;
68+
triggeredValue = value;
69+
},
70+
TimeSpan.FromMilliseconds(60));
71+
72+
Assert.AreEqual(true, debounceTimer.IsRunning, "Expected time to be running.");
73+
Assert.AreEqual(0, triggeredCount, "Function shouldn't have run yet.");
74+
Assert.IsNull(triggeredValue, "Function shouldn't have run yet.");
75+
76+
await Task.Delay(TimeSpan.FromMilliseconds(20));
77+
78+
// Stop the timer before it would fire.
79+
debounceTimer.Stop();
80+
81+
Assert.AreEqual(false, debounceTimer.IsRunning, "Expected to stop the timer.");
82+
Assert.IsNull(triggeredValue, "Expected result should be no value set.");
83+
Assert.AreEqual(0, triggeredCount, "Expected not to have code run.");
84+
85+
// Wait until timer would have fired
86+
await Task.Delay(TimeSpan.FromMilliseconds(60));
87+
88+
Assert.AreEqual(false, debounceTimer.IsRunning, "Expected the timer to remain stopped.");
89+
Assert.IsNull(triggeredValue, "Expected result should still be no value set.");
90+
Assert.AreEqual(0, triggeredCount, "Expected not to have code run still.");
91+
}
92+
5493
[TestCategory("DispatcherQueueTimerExtensions")]
5594
[UIThreadTestMethod]
5695
public async Task DispatcherQueueTimer_Debounce_Trailing_Interrupt()

0 commit comments

Comments
 (0)