@@ -51,6 +51,45 @@ public async Task DispatcherQueueTimer_Debounce_Trailing()
51
51
Assert . AreEqual ( 1 , triggeredCount , "Expected to run once." ) ;
52
52
}
53
53
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
+
54
93
[ TestCategory ( "DispatcherQueueTimerExtensions" ) ]
55
94
[ UIThreadTestMethod ]
56
95
public async Task DispatcherQueueTimer_Debounce_Trailing_Interrupt ( )
0 commit comments