Skip to content

Commit ecdb1e6

Browse files
committed
Add unit tests for DependencyObject extension methods
1 parent 5d68adf commit ecdb1e6

3 files changed

Lines changed: 153 additions & 33 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Microsoft.UI.Xaml.Controls;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using WinUI.TableView.Extensions;
7+
8+
namespace WinUI.TableView.Tests;
9+
10+
[TestClass]
11+
public class DependencyObjectExtensionsTests
12+
{
13+
private static Grid BuildVisualTree()
14+
{
15+
var root = new Grid();
16+
var child1 = new StackPanel();
17+
var child2 = new Button();
18+
var grandChild = new TextBlock();
19+
child1.Children.Add(grandChild);
20+
root.Children.Add(child1);
21+
root.Children.Add(child2);
22+
return root;
23+
}
24+
25+
[UITestMethod]
26+
public void FindDescendant_FindsFirstMatchingType()
27+
{
28+
var root = BuildVisualTree();
29+
var result = root.FindDescendant<TextBlock>();
30+
Assert.IsNotNull(result);
31+
Assert.IsInstanceOfType<TextBlock>(result);
32+
}
33+
34+
[UITestMethod]
35+
public void FindDescendant_WithPredicate_FindsCorrectDescendant()
36+
{
37+
var root = BuildVisualTree();
38+
var result = root.FindDescendant<StackPanel>(sp => sp.Children.Count == 1);
39+
Assert.IsNotNull(result);
40+
Assert.IsInstanceOfType<StackPanel>(result);
41+
}
42+
43+
[UITestMethod]
44+
public void FindDescendants_EnumeratesAllDescendants()
45+
{
46+
var root = BuildVisualTree();
47+
var descendants = root.FindDescendants().ToList();
48+
49+
Assert.IsTrue(descendants.OfType<StackPanel>().Any());
50+
Assert.IsTrue(descendants.OfType<Button>().Any());
51+
Assert.IsTrue(descendants.OfType<TextBlock>().Any());
52+
}
53+
54+
[UITestMethod]
55+
public async Task FindAscendant_FindsFirstMatchingAscendant()
56+
{
57+
var root = BuildVisualTree();
58+
await UnitTestApp.Current.MainWindow.LoadTestContentAsync(root);
59+
60+
var grandChild = root.FindDescendant<TextBlock>();
61+
var ascendant = grandChild?.FindAscendant<Grid>();
62+
Assert.AreEqual(root, ascendant);
63+
64+
await UnitTestApp.Current.MainWindow.UnloadTestContentAsync(root);
65+
}
66+
67+
[UITestMethod]
68+
public async Task FindAscendants_EnumeratesAllAscendants()
69+
{
70+
var root = BuildVisualTree();
71+
await UnitTestApp.Current.MainWindow.LoadTestContentAsync(root);
72+
73+
var grandChild = root.FindDescendant<TextBlock>();
74+
var ascendants = grandChild?.FindAscendants().ToList();
75+
Assert.IsTrue(ascendants?.OfType<StackPanel>().Any() ?? false);
76+
Assert.IsTrue(ascendants?.OfType<Grid>().Any() ?? false);
77+
78+
await UnitTestApp.Current.MainWindow.UnloadTestContentAsync(root);
79+
}
80+
}

tests/UnitTestApp.xaml.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
using Microsoft.UI.Xaml;
2-
using Microsoft.UI.Xaml.Controls;
3-
using Microsoft.UI.Xaml.Controls.Primitives;
4-
using Microsoft.UI.Xaml.Data;
5-
using Microsoft.UI.Xaml.Input;
6-
using Microsoft.UI.Xaml.Media;
7-
using Microsoft.UI.Xaml.Navigation;
8-
using Microsoft.UI.Xaml.Shapes;
2+
using Microsoft.VisualStudio.TestPlatform.TestExecutor;
93
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
104
using System;
11-
using System.Collections.Generic;
12-
using System.IO;
13-
using System.Linq;
14-
using System.Runtime.InteropServices.WindowsRuntime;
15-
using Windows.ApplicationModel;
16-
using Windows.ApplicationModel.Activation;
17-
using Windows.Foundation;
18-
using Windows.Foundation.Collections;
195

206
// To learn more about WinUI, the WinUI project structure,
217
// and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -26,7 +12,18 @@ namespace WinUI.TableView.Tests;
2612
/// </summary>
2713
public partial class UnitTestApp : Application
2814
{
29-
private Window? _window;
15+
private UnitTestAppWindow? _window;
16+
17+
internal static new UnitTestApp Current => (UnitTestApp)Application.Current;
18+
19+
internal UnitTestAppWindow MainWindow
20+
{
21+
get
22+
{
23+
_window ??= new UnitTestAppWindow();
24+
return _window;
25+
}
26+
}
3027

3128
/// <summary>
3229
/// Initializes the singleton application object. This is the first line of authored code
@@ -43,13 +40,12 @@ public UnitTestApp()
4340
/// <param name="args">Details about the launch request and process.</param>
4441
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
4542
{
46-
Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.CreateDefaultUI();
43+
UnitTestClient.CreateDefaultUI();
4744

48-
_window = new UnitTestAppWindow();
49-
_window.Activate();
45+
MainWindow.Activate();
5046

51-
UITestMethodAttribute.DispatcherQueue = _window.DispatcherQueue;
47+
UITestMethodAttribute.DispatcherQueue = MainWindow.DispatcherQueue;
5248

53-
Microsoft.VisualStudio.TestPlatform.TestExecutor.UnitTestClient.Run(Environment.CommandLine);
49+
UnitTestClient.Run(Environment.CommandLine);
5450
}
5551
}

tests/UnitTestAppWindow.xaml.cs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,70 @@
11
using Microsoft.UI.Xaml;
2-
using Microsoft.UI.Xaml.Controls;
3-
using Microsoft.UI.Xaml.Controls.Primitives;
4-
using Microsoft.UI.Xaml.Data;
5-
using Microsoft.UI.Xaml.Input;
62
using Microsoft.UI.Xaml.Media;
7-
using Microsoft.UI.Xaml.Navigation;
8-
using System;
9-
using System.Collections.Generic;
10-
using System.IO;
11-
using System.Linq;
12-
using System.Runtime.InteropServices.WindowsRuntime;
13-
using Windows.Foundation;
14-
using Windows.Foundation.Collections;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using System.Threading.Tasks;
155

166
// To learn more about WinUI, the WinUI project structure,
177
// and more about our project templates, see: http://aka.ms/winui-project-info.
188

199
namespace WinUI.TableView.Tests;
10+
2011
public sealed partial class UnitTestAppWindow : Window
2112
{
2213
public UnitTestAppWindow()
2314
{
2415
InitializeComponent();
2516
}
17+
18+
internal async Task LoadTestContentAsync(FrameworkElement content)
19+
{
20+
var taskCompletionSource = new TaskCompletionSource<object?>();
21+
22+
content.Loaded += OnLoaded;
23+
Content = content;
24+
25+
await taskCompletionSource.Task;
26+
27+
async void OnLoaded(object sender, RoutedEventArgs args)
28+
{
29+
content.Loaded -= OnLoaded;
30+
31+
// Wait for first Render pass
32+
await ExecuteAfterCompositionRenderingAsync();
33+
34+
taskCompletionSource.SetResult(null);
35+
}
36+
}
37+
38+
private static async Task ExecuteAfterCompositionRenderingAsync()
39+
{
40+
var taskCompletionSource = new TaskCompletionSource<object?>();
41+
42+
void Callback(object? sender, object args)
43+
{
44+
CompositionTarget.Rendering -= Callback;
45+
taskCompletionSource.SetResult(null);
46+
}
47+
48+
CompositionTarget.Rendering += Callback;
49+
50+
await taskCompletionSource.Task;
51+
}
52+
53+
public async Task UnloadTestContentAsync(FrameworkElement element)
54+
{
55+
var taskCompletionSource = new TaskCompletionSource<object?>();
56+
57+
element.Unloaded += OnUnloaded;
58+
59+
Content = null;
60+
61+
await taskCompletionSource.Task;
62+
Assert.IsFalse(element.IsLoaded);
63+
64+
void OnUnloaded(object sender, RoutedEventArgs args)
65+
{
66+
element.Unloaded -= OnUnloaded;
67+
taskCompletionSource.SetResult(null);
68+
}
69+
}
2670
}

0 commit comments

Comments
 (0)