-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
238 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions</AssemblyName> | ||
<RootNamespace>Microsoft.VisualStudio.TestTools.UnitTesting</RootNamespace> | ||
<Company>Microsoft Corporation</Company> | ||
<Product>Extension.WinUI</Product> | ||
<AssemblyTitle>Extension.WinUI</AssemblyTitle> | ||
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright> | ||
|
||
<TargetFramework>net5.0-windows10.0.18362.0</TargetFramework> | ||
<TargetPlatformVersion>10.0.18362.0</TargetPlatformVersion> | ||
<TargetPlatformMinVersion>10.0.18362.0</TargetPlatformMinVersion> | ||
<SupportedOSPlatformVersion>10.0.18362.0</SupportedOSPlatformVersion> | ||
|
||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<TestFxRoot Condition="$(TestFxRoot) == ''">..\..\..\</TestFxRoot> | ||
<FrameworkIdentifier>NetCore</FrameworkIdentifier> | ||
</PropertyGroup> | ||
|
||
<Import Project="..\Extension.Shared\Extension.Shared.projitems" Label="Shared" /> | ||
<Import Project="$(TestFxRoot)scripts\build\TestFx.Settings.targets" /> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.WinUI" Version="3.0.0-preview4.210210.4" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MSTest.Core\MSTest.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<DocumentationFile>$(OutputPath)\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.XML</DocumentationFile> | ||
<LocDocumentationSubPath>Extensions\WinUI</LocDocumentationSubPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(TestFxRoot)scripts\build\TestFx.targets" /> | ||
|
||
</Project> |
85 changes: 85 additions & 0 deletions
85
src/TestFramework/Extension.WinUI/UITestMethodAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer | ||
{ | ||
using global::System; | ||
using global::System.Runtime.CompilerServices; | ||
|
||
/// <summary> | ||
/// Execute test code in UI thread for Windows store apps. | ||
/// </summary> | ||
public class UITestMethodAttribute : TestMethodAttribute | ||
{ | ||
/// <summary> | ||
/// Gets or sets the <see cref="Microsoft.System.DispatcherQueue"/> that should be used to invoke the UITestMethodAttribute. | ||
/// If none is provided, it will try to use the Microsoft.UI.Xaml.Window.Current.DispatcherQueue, which only works on UWP. | ||
/// </summary> | ||
public static Microsoft.System.DispatcherQueue DispatcherQueue { get; set; } | ||
|
||
/// <summary> | ||
/// Executes the test method on the UI Thread. | ||
/// </summary> | ||
/// <param name="testMethod"> | ||
/// The test method. | ||
/// </param> | ||
/// <returns> | ||
/// An array of <see cref="TestResult"/> instances. | ||
/// </returns> | ||
/// Throws <exception cref="NotSupportedException"> when run on an async test method. | ||
/// </exception> | ||
public override TestResult[] Execute(ITestMethod testMethod) | ||
{ | ||
var attrib = testMethod.GetAttributes<AsyncStateMachineAttribute>(false); | ||
if (attrib.Length > 0) | ||
{ | ||
throw new NotSupportedException(FrameworkMessages.AsyncUITestMethodNotSupported); | ||
} | ||
|
||
TestResult result = null; | ||
|
||
var dispatcher = DispatcherQueue ?? global::Microsoft.UI.Xaml.Window.Current?.DispatcherQueue; | ||
if (dispatcher == null) | ||
{ | ||
throw new InvalidOperationException(FrameworkMessages.AsyncUITestMethodWithNoDispatcherQueue); | ||
} | ||
|
||
if (dispatcher.HasThreadAccess) | ||
{ | ||
try | ||
{ | ||
result = testMethod.Invoke(Array.Empty<object>()); | ||
} | ||
catch (Exception e) | ||
{ | ||
return new TestResult[] { new TestResult { TestFailureException = e } }; | ||
} | ||
} | ||
else | ||
{ | ||
var taskCompletionSource = new global::System.Threading.Tasks.TaskCompletionSource<object>(); | ||
|
||
if (!dispatcher.TryEnqueue(System.DispatcherQueuePriority.Normal, () => | ||
{ | ||
try | ||
{ | ||
result = testMethod.Invoke(Array.Empty<object>()); | ||
taskCompletionSource.SetResult(null); | ||
} | ||
catch (Exception e) | ||
{ | ||
result = new TestResult { TestFailureException = e }; | ||
taskCompletionSource.SetException(e); | ||
} | ||
})) | ||
{ | ||
taskCompletionSource.SetResult(null); | ||
} | ||
|
||
taskCompletionSource.Task.GetAwaiter().GetResult(); | ||
} | ||
|
||
return new TestResult[] { result }; | ||
} | ||
} | ||
} |
Oops, something went wrong.