From 30d2917e9ddb00d97166f91848f2ac9e6e5227f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20In=C3=A1cio?= Date: Sat, 2 Oct 2021 19:12:44 -0300 Subject: [PATCH 1/5] Fixed the debounce extension in order to update to the last action passed. --- .../Extensions/DispatcherQueueTimerExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs index 52a1b6e9ae6..ffb273c3d07 100644 --- a/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs +++ b/Microsoft.Toolkit.Uwp/Extensions/DispatcherQueueTimerExtensions.cs @@ -62,7 +62,7 @@ public static void Debounce(this DispatcherQueueTimer timer, Action action, Time timer.Tick += Timer_Tick; // Store/Update function - _debounceInstances.AddOrUpdate(timer, action, (k, v) => v); + _debounceInstances.AddOrUpdate(timer, action, (k, v) => action); } // Start the timer to keep track of the last call here. From b87504c8a1264d525895c5e4c9ee474e959de59b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20In=C3=A1cio?= Date: Mon, 4 Oct 2021 13:16:55 -0300 Subject: [PATCH 2/5] Implemented test to the debounce extension for the DispatcherQueueTimer. --- .../Test_DispatcherQueueTimerExtensions.cs | 47 +++++++++++++++++++ UnitTests/UnitTests.UWP/UnitTests.UWP.csproj | 1 + 2 files changed, 48 insertions(+) create mode 100644 UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs diff --git a/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs new file mode 100644 index 00000000000..91729eefe67 --- /dev/null +++ b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs @@ -0,0 +1,47 @@ +using Microsoft.Toolkit.Uwp.UI; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Threading.Tasks; + +namespace UnitTests.Extensions +{ + [TestClass] + public class Test_DispatcherQueueTimerExtensions + { + [TestCategory("DispatcherQueueTimerExtensions")] + [TestMethod] + public async Task Test_DispatcherQueueTimerExtensions_Debounce() + { + var debounceTimer = App.DispatcherQueue.CreateTimer(); + + var triggeredCount = 0; + string triggeredValue = null; + + var value = "He"; + debounceTimer.Debounce( + () => + { + triggeredCount++; + triggeredValue = value; + }, + TimeSpan.FromMilliseconds(60)); + + await Task.Delay(TimeSpan.FromMilliseconds(10)); + + value = "Hello"; + debounceTimer.Debounce( + () => + { + triggeredCount++; + triggeredValue = value; + }, + TimeSpan.FromMilliseconds(60)); + + await Task.Delay(TimeSpan.FromMilliseconds(110)); + + Assert.AreEqual(false, debounceTimer.IsRunning, "Expected to stop the timer."); + Assert.AreEqual(value, triggeredValue, "Expected to execute the last action."); + Assert.AreEqual(1, triggeredCount, "Expected to postpone execution."); + } + } +} diff --git a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj index a93621cfa61..cdbb101c62a 100644 --- a/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj +++ b/UnitTests/UnitTests.UWP/UnitTests.UWP.csproj @@ -165,6 +165,7 @@ + From 9881a23a1a880992becd30374938c0127781e186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20In=C3=A1cio?= Date: Mon, 4 Oct 2021 18:18:14 -0300 Subject: [PATCH 3/5] Added missing file header. --- .../Extensions/Test_DispatcherQueueTimerExtensions.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs index 91729eefe67..3af086d3afc 100644 --- a/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs +++ b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs @@ -1,3 +1,7 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + using Microsoft.Toolkit.Uwp.UI; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; From 011a67afc77972389b0516600faf626e3d801205 Mon Sep 17 00:00:00 2001 From: "Michael Hawker MSFT (XAML Llama)" <24302614+michael-hawker@users.noreply.github.com> Date: Thu, 7 Oct 2021 13:22:46 -0700 Subject: [PATCH 4/5] Update UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs --- .../Extensions/Test_DispatcherQueueTimerExtensions.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs index 3af086d3afc..da990d15bb4 100644 --- a/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs +++ b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs @@ -30,21 +30,20 @@ public async Task Test_DispatcherQueueTimerExtensions_Debounce() }, TimeSpan.FromMilliseconds(60)); - await Task.Delay(TimeSpan.FromMilliseconds(10)); + var value2 = "Hello"; - value = "Hello"; debounceTimer.Debounce( () => { triggeredCount++; - triggeredValue = value; + triggeredValue = value2; }, TimeSpan.FromMilliseconds(60)); await Task.Delay(TimeSpan.FromMilliseconds(110)); Assert.AreEqual(false, debounceTimer.IsRunning, "Expected to stop the timer."); - Assert.AreEqual(value, triggeredValue, "Expected to execute the last action."); + Assert.AreEqual(value2, triggeredValue, "Expected to execute the last action."); Assert.AreEqual(1, triggeredCount, "Expected to postpone execution."); } } From 9419c7e0abfa76d0a885a1e5572b8a918f4243ba Mon Sep 17 00:00:00 2001 From: "Michael Hawker MSFT (XAML Llama)" <24302614+michael-hawker@users.noreply.github.com> Date: Thu, 7 Oct 2021 13:23:23 -0700 Subject: [PATCH 5/5] Update UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs --- .../Extensions/Test_DispatcherQueueTimerExtensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs index da990d15bb4..b77b54e4e3f 100644 --- a/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs +++ b/UnitTests/UnitTests.UWP/Extensions/Test_DispatcherQueueTimerExtensions.cs @@ -31,7 +31,6 @@ public async Task Test_DispatcherQueueTimerExtensions_Debounce() TimeSpan.FromMilliseconds(60)); var value2 = "Hello"; - debounceTimer.Debounce( () => {