-
Notifications
You must be signed in to change notification settings - Fork 100
Add sample/tests for FrameworkElementExtensions.Ancestor/Type #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b2b95a6
Add sample for FrameworkElementExtensions.Ancestor/Type
michael-hawker 6468ade
Fixes #106 to change binding type of AncestorType in FrameworkElement…
michael-hawker 6854bb9
Add initial test case for FrameworkElementExtension.RelativeAncestor
michael-hawker 05ad5d1
Add more test cases to try and track issue for #107
michael-hawker 022b4bf
Add unloaded event, but didn't see impact tests pass before and after…
michael-hawker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
components/Extensions/samples/FrameworkElementAncestorSample.xaml
This file contains hidden or 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,22 @@ | ||
<Page x:Class="ExtensionsExperiment.Samples.FrameworkElementAncestorSample" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="using:ExtensionsExperiment.Samples" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:ui="using:CommunityToolkit.WinUI" | ||
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" | ||
mc:Ignorable="d"> | ||
|
||
<StackPanel Padding="16" | ||
Spacing="{x:Bind ValueSlider.Value, Mode=OneWay}"> | ||
<Slider x:Name="ValueSlider" | ||
Maximum="16" | ||
Minimum="4" | ||
StepFrequency="4" | ||
Value="8" /> | ||
<TextBlock Text="This is some text in a StackPanel with Spacing:" /> | ||
<TextBlock ui:FrameworkElementExtensions.AncestorType="StackPanel" | ||
Text="{Binding (ui:FrameworkElementExtensions.Ancestor).Spacing, RelativeSource={RelativeSource Self}}" /> | ||
</StackPanel> | ||
</Page> |
17 changes: 17 additions & 0 deletions
17
components/Extensions/samples/FrameworkElementAncestorSample.xaml.cs
This file contains hidden or 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,17 @@ | ||
// 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. | ||
|
||
namespace ExtensionsExperiment.Samples; | ||
|
||
/// <summary> | ||
/// An empty page that can be used on its own or navigated to within a Frame. | ||
/// </summary> | ||
[ToolkitSample(id: nameof(FrameworkElementAncestorSample), nameof(FrameworkElementAncestorSample), description: $"A sample for showing how to use the FrameworkElementExtensions.Ancestor attached property.")] | ||
public sealed partial class FrameworkElementAncestorSample : Page | ||
{ | ||
public FrameworkElementAncestorSample() | ||
{ | ||
this.InitializeComponent(); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
154 changes: 154 additions & 0 deletions
154
components/Extensions/tests/Element/FrameworkElementExtensionsTests.RelativeAncestor.cs
This file contains hidden or 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,154 @@ | ||
// 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 CommunityToolkit.Tests; | ||
using CommunityToolkit.Tooling.TestGen; | ||
|
||
namespace ExtensionsComponent.Tests; | ||
|
||
[TestClass] | ||
public partial class FrameworkElementExtensionsTests : VisualUITestBase | ||
{ | ||
[TestCategory("FrameworkElementExtension")] | ||
[UIThreadTestMethod] | ||
public void FrameworkElementExtension_RelativeAncestor_InDataTemplate(FrameworkElementRelativeAncestorDataTemplateTestPage page) | ||
{ | ||
var list = page.FindDescendant<ListView>(); | ||
|
||
Assert.IsNotNull(list, "Couldn't find listview"); | ||
|
||
int count = 0; | ||
foreach (var item in list.FindDescendants().OfType<TextBlock>()) | ||
{ | ||
count++; | ||
Assert.AreEqual("Hello", item.Text, "Text didn't match binding of ancestor tag property"); | ||
} | ||
|
||
Assert.AreEqual(3, count, "Didn't find three textblocks"); | ||
} | ||
|
||
[TestCategory("FrameworkElementExtension")] | ||
[UIThreadTestMethod] | ||
public async Task FrameworkElementExtension_RelativeAncestor_FreeParentBaseline(FrameworkElementRelativeAncestorDataTemplateTestPage page) | ||
{ | ||
var text = page.FindDescendant<TextBox>(); | ||
|
||
Assert.IsNotNull(text, "Couldn't find TextBox"); | ||
|
||
// Grab a hold of a weak reference for TextBox so we can detect when it unloads. | ||
WeakReference textRef = new(text); | ||
text = null; | ||
|
||
var parent = page.FindDescendant<Grid>(); | ||
|
||
Assert.IsNotNull(parent, "Couldn't find parent Grid"); | ||
|
||
// Remove all the children from the grid to simulate it unloading. | ||
VisualTreeHelper.DisconnectChildrenRecursive(parent); | ||
parent.Children.Clear(); | ||
parent = null; | ||
|
||
// Wait for the Visual Tree to perform removals and clean-up | ||
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); | ||
|
||
// Wait for the .NET Garbage Collector to clean up references | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
Assert.IsFalse(textRef.IsAlive, "TextBox is still alive..."); | ||
} | ||
|
||
[TestCategory("FrameworkElementExtension")] | ||
[UIThreadTestMethod] | ||
public async Task FrameworkElementExtension_RelativeAncestor_FreeParent(FrameworkElementRelativeAncestorDataTemplateTestPage page) | ||
{ | ||
var list = page.FindDescendant<ListView>(); | ||
|
||
Assert.IsNotNull(list, "Couldn't find listview"); | ||
|
||
// Grab a hold of a weak reference for ListView so we can detect when it unloads. | ||
WeakReference listRef = new(list); | ||
list = null; | ||
|
||
// Remove all the children from the grid to simulate it unloading. | ||
VisualTreeHelper.DisconnectChildrenRecursive(page); | ||
page.Content = null; | ||
|
||
// Wait for the Visual Tree to perform removals and clean-up | ||
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); | ||
|
||
// Wait for the .NET Garbage Collector to clean up references | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
Assert.IsFalse(listRef.IsAlive, "ListView is still alive..."); | ||
} | ||
|
||
[TestCategory("FrameworkElementExtension")] | ||
[UIThreadTestMethod] | ||
public async Task FrameworkElementExtension_RelativeAncestor_FreePageNavigation() | ||
{ | ||
TaskCompletionSource<bool?> taskCompletionSource = new(); | ||
var frame = new Frame(); | ||
frame.Navigated += OnNavigated; | ||
|
||
await LoadTestContentAsync(frame); | ||
|
||
// Navigate to the new page. | ||
frame.Navigate(typeof(FrameworkElementRelativeAncestorDataTemplateTestPage), null, new SuppressNavigationTransitionInfo()); | ||
|
||
async void OnNavigated(object sender, NavigationEventArgs e) | ||
{ | ||
frame.Navigated -= OnNavigated; | ||
|
||
// Wait for first Render pass | ||
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); | ||
|
||
taskCompletionSource.SetResult(true); | ||
} | ||
|
||
// Wait for frame to navigate/load | ||
var result = await taskCompletionSource.Task; | ||
Assert.IsTrue(result, "Navigation didn't complete"); | ||
|
||
// Find the ListView we want to track | ||
|
||
var list = frame.FindDescendant<ListView>(); | ||
|
||
Assert.IsNotNull(list, "Couldn't find listview"); | ||
|
||
// Grab a hold of a weak reference for ListView so we can detect when it unloads. | ||
WeakReference listRef = new(list); | ||
list = null; | ||
|
||
TaskCompletionSource<bool?> taskCompletionSource2 = new(); | ||
frame.Navigated += OnNavigated2; | ||
|
||
async void OnNavigated2(object sender, NavigationEventArgs e) | ||
{ | ||
frame.Navigated -= OnNavigated2; | ||
|
||
// Wait for first Render pass | ||
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); | ||
|
||
taskCompletionSource2.SetResult(true); | ||
} | ||
|
||
// Navigate to any other page to unload our other one | ||
frame.Navigate(typeof(BitmapIconExtensionTestPage), null, new SuppressNavigationTransitionInfo()); | ||
|
||
// Wait for navigation to complete | ||
result = await taskCompletionSource2.Task; | ||
Assert.IsTrue(result, "Navigation didn't complete 2"); | ||
|
||
// Wait for the Visual Tree to perform removals and clean-up | ||
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); | ||
|
||
// Wait for the .NET Garbage Collector to clean up references | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
Assert.IsFalse(listRef.IsAlive, "ListView is still alive..."); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...onents/Extensions/tests/Element/FrameworkElementRelativeAncestorDataTemplateTestPage.xaml
This file contains hidden or 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,25 @@ | ||
<Page x:Class="ExtensionsComponent.Tests.FrameworkElementRelativeAncestorDataTemplateTestPage" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:ui="using:CommunityToolkit.WinUI" | ||
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" | ||
mc:Ignorable="d"> | ||
|
||
<Grid> | ||
<ListView Tag="Hello"> | ||
<ListView.ItemTemplate> | ||
<DataTemplate> | ||
<TextBlock ui:FrameworkElementExtensions.AncestorType="ListView" | ||
Text="{Binding (ui:FrameworkElementExtensions.Ancestor).Tag, RelativeSource={RelativeSource Self}}" /> | ||
</DataTemplate> | ||
</ListView.ItemTemplate> | ||
<x:String>1</x:String> | ||
<x:String>2</x:String> | ||
<x:String>3</x:String> | ||
</ListView> | ||
<!-- Another child to test clearing GC --> | ||
<TextBox /> | ||
</Grid> | ||
</Page> |
16 changes: 16 additions & 0 deletions
16
...nts/Extensions/tests/Element/FrameworkElementRelativeAncestorDataTemplateTestPage.xaml.cs
This file contains hidden or 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,16 @@ | ||
// 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. | ||
|
||
namespace ExtensionsComponent.Tests; | ||
|
||
/// <summary> | ||
/// An empty page that can be used on its own or navigated to within a Frame. | ||
/// </summary> | ||
public sealed partial class FrameworkElementRelativeAncestorDataTemplateTestPage : Page | ||
{ | ||
public FrameworkElementRelativeAncestorDataTemplateTestPage() | ||
{ | ||
this.InitializeComponent(); | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.