-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3746 from michael-hawker/tests-visualtree
Setup Unit Test Infrastructure for VisualTree related tests
- Loading branch information
Showing
7 changed files
with
191 additions
and
10 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
66 changes: 66 additions & 0 deletions
66
UnitTests/UnitTests.UWP/Extensions/Test_LogicalTreeExtensions.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,66 @@ | ||
// 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.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Toolkit.Uwp.Extensions; | ||
using Microsoft.Toolkit.Uwp.UI.Extensions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Markup; | ||
|
||
namespace UnitTests.Extensions | ||
{ | ||
[TestClass] | ||
public class Test_LogicalTreeExtensions : VisualUITestBase | ||
{ | ||
[TestCategory("LogicalTree")] | ||
[TestMethod] | ||
public async Task Test_LogicalTree_FindParent_Exists() | ||
{ | ||
await App.DispatcherQueue.EnqueueAsync(async () => | ||
{ | ||
var treeRoot = XamlReader.Load(@"<Page | ||
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | ||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> | ||
<Grid> | ||
<Grid> <!-- Target --> | ||
<Border/> | ||
<Border> | ||
<TextBlock/> <!-- Starting Point --> | ||
</Border> | ||
</Grid> | ||
</Grid> | ||
</Page>") as Page; | ||
|
||
// Test Setup | ||
Assert.IsNotNull(treeRoot, "XAML Failed to Load"); | ||
|
||
// Initialize Visual Tree | ||
await SetTestContentAsync(treeRoot); | ||
|
||
var outerGrid = treeRoot.Content as Grid; | ||
|
||
Assert.IsNotNull(outerGrid, "Couldn't find Page content."); | ||
|
||
var targetGrid = outerGrid.Children.FirstOrDefault() as Grid; | ||
Assert.IsNotNull(targetGrid, "Couldn't find Target Grid"); | ||
Assert.AreEqual(2, targetGrid.Children.Count, "Grid doesn't have right number of children."); | ||
|
||
var secondBorder = targetGrid.Children[1] as Border; | ||
Assert.IsNotNull(secondBorder, "Border not found."); | ||
|
||
var startingPoint = secondBorder.Child as FrameworkElement; | ||
Assert.IsNotNull(startingPoint, "Could not find starting element."); | ||
|
||
// Main Test | ||
var grid = startingPoint.FindParent<Grid>(); | ||
|
||
Assert.IsNotNull(grid, "Expected to find Grid"); | ||
Assert.AreEqual(targetGrid, grid, "Grid didn't match expected."); | ||
}); | ||
} | ||
} | ||
} |
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,77 @@ | ||
// 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.Extensions; | ||
using Microsoft.Toolkit.Uwp.UI.Helpers; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Windows.UI.Xaml; | ||
|
||
namespace UnitTests | ||
{ | ||
/// <summary> | ||
/// Base class to be used in API tests which require UI layout or rendering to occur first. | ||
/// For more E2E scenarios or testing components for user interation, see integration test suite instead. | ||
/// Use this class when an API needs direct access to test functions of the UI itself in more simplistic scenarios (i.e. visual tree helpers). | ||
/// </summary> | ||
public class VisualUITestBase | ||
{ | ||
/// <summary> | ||
/// Sets the content of the test app to a simple <see cref="FrameworkElement"/> to load into the visual tree. | ||
/// Waits for that element to be loaded and rendered before returning. | ||
/// </summary> | ||
/// <param name="content">Content to set in test app.</param> | ||
/// <returns>When UI is loaded.</returns> | ||
protected Task SetTestContentAsync(FrameworkElement content) | ||
{ | ||
return App.DispatcherQueue.EnqueueAsync(() => | ||
{ | ||
var taskCompletionSource = new TaskCompletionSource<bool>(); | ||
|
||
async void Callback(object sender, RoutedEventArgs args) | ||
{ | ||
content.Loaded -= Callback; | ||
|
||
// Wait for first Render pass | ||
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); | ||
|
||
taskCompletionSource.SetResult(true); | ||
} | ||
|
||
// Going to wait for our original content to unload | ||
content.Loaded += Callback; | ||
|
||
// Trigger that now | ||
try | ||
{ | ||
App.ContentRoot = content; | ||
} | ||
catch (Exception e) | ||
{ | ||
taskCompletionSource.SetException(e); | ||
} | ||
|
||
return taskCompletionSource.Task; | ||
}); | ||
} | ||
|
||
[TestCleanup] | ||
public async Task Cleanup() | ||
{ | ||
var taskCompletionSource = new TaskCompletionSource<bool>(); | ||
|
||
await App.DispatcherQueue.EnqueueAsync(() => | ||
{ | ||
// Going to wait for our original content to unload | ||
App.ContentRoot.Unloaded += (_, _) => taskCompletionSource.SetResult(true); | ||
|
||
// Trigger that now | ||
App.ContentRoot = null; | ||
}); | ||
|
||
await taskCompletionSource.Task; | ||
} | ||
} | ||
} |