Skip to content

Commit

Permalink
Added tests for the DispatcherHelper type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Mar 24, 2020
1 parent 1cebda0 commit 899d500
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 0 deletions.
221 changes: 221 additions & 0 deletions UnitTests/Helpers/Test_DispatcherHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
// 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 System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Microsoft.Toolkit.Uwp.Helpers;

namespace UnitTests.Helpers
{
[TestClass]
public class Test_DispatcherHelper
{
[TestCategory("Helpers")]
[UITestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Test_DispatcherHelper_Action_Null()
{
DispatcherHelper.ExecuteOnUIThreadAsync(default(Action));
}

[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherHelper_Action_Ok_UIThread()
{
DispatcherHelper.ExecuteOnUIThreadAsync(() =>
{
var textBlock = new TextBlock { Text = nameof(Test_DispatcherHelper_Action_Ok_NonUIThread) };

Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherHelper_Action_Ok_NonUIThread));
}).Wait();
}

[TestCategory("Helpers")]
[TestMethod]
public async Task Test_DispatcherHelper_Action_Ok_NonUIThread()
{
await DispatcherHelper.ExecuteOnUIThreadAsync(() =>
{
var textBlock = new TextBlock { Text = nameof(Test_DispatcherHelper_Action_Ok_NonUIThread) };

Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherHelper_Action_Ok_NonUIThread));
});
}

[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherHelper_Action_Exception()
{
var task = DispatcherHelper.ExecuteOnUIThreadAsync(() =>
{
throw new ArgumentException(nameof(this.Test_DispatcherHelper_Action_Exception));
});

Assert.IsNotNull(task);
Assert.AreEqual(task.Status, TaskStatus.Faulted);
Assert.IsNotNull(task.Exception);
Assert.IsInstanceOfType(task.Exception.InnerExceptions.FirstOrDefault(), typeof(ArgumentException));
}

[TestCategory("Helpers")]
[UITestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Test_DispatcherHelper_FuncOfT_Null()
{
DispatcherHelper.ExecuteOnUIThreadAsync(default(Func<int>));
}

[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherHelper_FuncOfT_Ok_UIThread()
{
var textBlock = DispatcherHelper.ExecuteOnUIThreadAsync(() =>
{
return new TextBlock { Text = nameof(Test_DispatcherHelper_FuncOfT_Ok_NonUIThread) };
}).Result;

Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherHelper_FuncOfT_Ok_NonUIThread));
}

[TestCategory("Helpers")]
[TestMethod]
public async Task Test_DispatcherHelper_FuncOfT_Ok_NonUIThread()
{
var textBlock = await DispatcherHelper.ExecuteOnUIThreadAsync(() =>
{
return new TextBlock { Text = nameof(Test_DispatcherHelper_FuncOfT_Ok_NonUIThread) };
});

await DispatcherHelper.ExecuteOnUIThreadAsync(() =>
{
Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherHelper_FuncOfT_Ok_NonUIThread));
});
}

[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherHelper_FuncOfT_Exception()
{
var task = DispatcherHelper.ExecuteOnUIThreadAsync(new Func<int>(() =>
{
throw new ArgumentException(nameof(this.Test_DispatcherHelper_FuncOfT_Exception));
}));

Assert.IsNotNull(task);
Assert.AreEqual(task.Status, TaskStatus.Faulted);
Assert.IsNotNull(task.Exception);
Assert.IsInstanceOfType(task.Exception.InnerExceptions.FirstOrDefault(), typeof(ArgumentException));
}

[TestCategory("Helpers")]
[UITestMethod]
[ExpectedException(typeof(ArgumentNullException))]
[SuppressMessage("Style", "IDE0034", Justification = "Explicit overload for clarity")]
public void Test_DispatcherHelper_FuncOfTask_Null()
{
DispatcherHelper.ExecuteOnUIThreadAsync(default(Func<Task>));
}

[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherHelper_FuncOfTask_Ok_UIThread()
{
DispatcherHelper.ExecuteOnUIThreadAsync(() =>
{
var textBlock = new TextBlock { Text = nameof(Test_DispatcherHelper_FuncOfTask_Ok_NonUIThread) };

Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherHelper_FuncOfTask_Ok_NonUIThread));

return Task.CompletedTask;
}).Wait();
}

[TestCategory("Helpers")]
[TestMethod]
public async Task Test_DispatcherHelper_FuncOfTask_Ok_NonUIThread()
{
await DispatcherHelper.ExecuteOnUIThreadAsync(async () =>
{
await Task.Yield();

var textBlock = new TextBlock { Text = nameof(Test_DispatcherHelper_FuncOfTask_Ok_NonUIThread) };

Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherHelper_FuncOfTask_Ok_NonUIThread));
});
}

[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherHelper_FuncOfTask_Exception()
{
var task = DispatcherHelper.ExecuteOnUIThreadAsync(new Func<Task>(() =>
{
throw new ArgumentException(nameof(this.Test_DispatcherHelper_FuncOfTask_Exception));
}));

Assert.IsNotNull(task);
Assert.AreEqual(task.Status, TaskStatus.Faulted);
Assert.IsNotNull(task.Exception);
Assert.IsInstanceOfType(task.Exception.InnerExceptions.FirstOrDefault(), typeof(ArgumentException));
}

[TestCategory("Helpers")]
[UITestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Test_DispatcherHelper_FuncOfTaskOfT_Null()
{
DispatcherHelper.ExecuteOnUIThreadAsync(default(Func<Task<int>>));
}

[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherHelper_FuncOfTaskOfT_Ok_UIThread()
{
DispatcherHelper.ExecuteOnUIThreadAsync(() =>
{
var textBlock = new TextBlock { Text = nameof(Test_DispatcherHelper_FuncOfTaskOfT_Ok_NonUIThread) };

Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherHelper_FuncOfTaskOfT_Ok_NonUIThread));

return Task.FromResult(1);
}).Wait();
}

[TestCategory("Helpers")]
[TestMethod]
public async Task Test_DispatcherHelper_FuncOfTaskOfT_Ok_NonUIThread()
{
await DispatcherHelper.ExecuteOnUIThreadAsync(async () =>
{
await Task.Yield();

var textBlock = new TextBlock { Text = nameof(Test_DispatcherHelper_FuncOfTaskOfT_Ok_NonUIThread) };

Assert.AreEqual(textBlock.Text, nameof(Test_DispatcherHelper_FuncOfTaskOfT_Ok_NonUIThread));

return textBlock;
});
}

[TestCategory("Helpers")]
[UITestMethod]
public void Test_DispatcherHelper_FuncOfTaskOfT_Exception()
{
var task = DispatcherHelper.ExecuteOnUIThreadAsync(new Func<Task<int>>(() =>
{
throw new ArgumentException(nameof(this.Test_DispatcherHelper_FuncOfTaskOfT_Exception));
}));

Assert.IsNotNull(task);
Assert.AreEqual(task.Status, TaskStatus.Faulted);
Assert.IsNotNull(task.Exception);
Assert.IsInstanceOfType(task.Exception.InnerExceptions.FirstOrDefault(), typeof(ArgumentException));
}
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<Compile Include="Extensions\Test_ArrayExtensions.cs" />
<Compile Include="Extensions\Test_NullableBoolMarkupExtension.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Helpers\Test_DispatcherHelper.cs" />
<Compile Include="Helpers\Test_AdvancedCollectionView.cs" />
<Compile Include="Helpers\Test_BackgroundTaskHelper.cs" />
<Compile Include="Helpers\Test_ColorHelper.cs" />
Expand Down

0 comments on commit 899d500

Please sign in to comment.