Skip to content

Commit b87504c

Browse files
author
Matheus Inácio
committed
Implemented test to the debounce extension for the DispatcherQueueTimer.
1 parent 30d2917 commit b87504c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.Toolkit.Uwp.UI;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace UnitTests.Extensions
7+
{
8+
[TestClass]
9+
public class Test_DispatcherQueueTimerExtensions
10+
{
11+
[TestCategory("DispatcherQueueTimerExtensions")]
12+
[TestMethod]
13+
public async Task Test_DispatcherQueueTimerExtensions_Debounce()
14+
{
15+
var debounceTimer = App.DispatcherQueue.CreateTimer();
16+
17+
var triggeredCount = 0;
18+
string triggeredValue = null;
19+
20+
var value = "He";
21+
debounceTimer.Debounce(
22+
() =>
23+
{
24+
triggeredCount++;
25+
triggeredValue = value;
26+
},
27+
TimeSpan.FromMilliseconds(60));
28+
29+
await Task.Delay(TimeSpan.FromMilliseconds(10));
30+
31+
value = "Hello";
32+
debounceTimer.Debounce(
33+
() =>
34+
{
35+
triggeredCount++;
36+
triggeredValue = value;
37+
},
38+
TimeSpan.FromMilliseconds(60));
39+
40+
await Task.Delay(TimeSpan.FromMilliseconds(110));
41+
42+
Assert.AreEqual(false, debounceTimer.IsRunning, "Expected to stop the timer.");
43+
Assert.AreEqual(value, triggeredValue, "Expected to execute the last action.");
44+
Assert.AreEqual(1, triggeredCount, "Expected to postpone execution.");
45+
}
46+
}
47+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
<Compile Include="Converters\Test_StringFormatConverter.cs" />
166166
<Compile Include="Converters\Test_TypeToObjectConverter.cs" />
167167
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
168+
<Compile Include="Extensions\Test_DispatcherQueueTimerExtensions.cs" />
168169
<Compile Include="Extensions\Test_StringExtensions.cs" />
169170
<Compile Include="Extensions\Test_UIElementExtensions_Coordinates.cs" />
170171
<Compile Include="Extensions\Test_VisualTreeExtensions.cs" />

0 commit comments

Comments
 (0)