Skip to content

Commit b2f8fcb

Browse files
Merge pull request #3886 from michael-hawker/wrap-panel-tests
Add some basic tests for WrapPanel
2 parents ce53551 + 6347f10 commit b2f8fcb

File tree

6 files changed

+662
-0
lines changed

6 files changed

+662
-0
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,5 +708,10 @@ public class GitRefObject
708708
[JsonPropertyName("sha")]
709709
public string Sha { get; set; }
710710
}
711+
712+
public override string ToString()
713+
{
714+
return $"SampleApp.Sample<{CategoryName}.{Subcategory}.{Name}>";
715+
}
711716
}
712717
}

Microsoft.Toolkit.Uwp.UI/Extensions/UIElementExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using Windows.UI.Xaml;
66
using Windows.UI.Xaml.Hosting;
7+
using Point = Windows.Foundation.Point;
78

89
namespace Microsoft.Toolkit.Uwp.UI
910
{
@@ -44,5 +45,31 @@ private static void OnClipToBoundsPropertyChanged(DependencyObject d, Dependency
4445
visual.Clip = clipToBounds ? visual.Compositor.CreateInsetClip() : null;
4546
}
4647
}
48+
49+
/// <summary>
50+
/// Provides the distance in a <see cref="Point"/> from the passed in element to the element being called on.
51+
/// For instance, calling child.CoordinatesFrom(container) will return the position of the child within the container.
52+
/// Helper for <see cref="UIElement.TransformToVisual(UIElement)"/>.
53+
/// </summary>
54+
/// <param name="target">Element to measure distance.</param>
55+
/// <param name="parent">Starting parent element to provide coordinates from.</param>
56+
/// <returns><see cref="Point"/> containing difference in position of elements.</returns>
57+
public static Point CoordinatesFrom(this UIElement target, UIElement parent)
58+
{
59+
return target.TransformToVisual(parent).TransformPoint(default(Point));
60+
}
61+
62+
/// <summary>
63+
/// Provides the distance in a <see cref="Point"/> to the passed in element from the element being called on.
64+
/// For instance, calling container.CoordinatesTo(child) will return the position of the child within the container.
65+
/// Helper for <see cref="UIElement.TransformToVisual(UIElement)"/>.
66+
/// </summary>
67+
/// <param name="parent">Starting parent element to provide coordinates from.</param>
68+
/// <param name="target">Element to measure distance to.</param>
69+
/// <returns><see cref="Point"/> containing difference in position of elements.</returns>
70+
public static Point CoordinatesTo(this UIElement parent, UIElement target)
71+
{
72+
return target.TransformToVisual(parent).TransformPoint(default(Point));
73+
}
4774
}
4875
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Microsoft.Toolkit.Uwp;
8+
using Microsoft.Toolkit.Uwp.UI;
9+
using Microsoft.Toolkit.Uwp.UI.Controls;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
using Windows.UI.Xaml;
12+
using Windows.UI.Xaml.Controls;
13+
using Windows.UI.Xaml.Markup;
14+
15+
namespace UnitTests.Extensions
16+
{
17+
[TestClass]
18+
public class Test_UIElementExtensions_Coordinates : VisualUITestBase
19+
{
20+
[TestCategory("UIElementExtensions")]
21+
[TestMethod]
22+
public async Task Test_UIElement_Extensions_CoordinatesTo()
23+
{
24+
await App.DispatcherQueue.EnqueueAsync(async () =>
25+
{
26+
var treeRoot = XamlReader.Load(@"<Page
27+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
28+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
29+
<Border x:Name=""Target"" Margin=""150,100,0,0""/>
30+
</Page>") as Page;
31+
32+
// Test Setup
33+
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
34+
35+
// Initialize Visual Tree
36+
await SetTestContentAsync(treeRoot);
37+
38+
// Main Test
39+
var border = treeRoot.FindDescendant("Target");
40+
41+
Assert.IsNotNull(border, "Expected to find something.");
42+
Assert.IsInstanceOfType(border, typeof(Border), "Didn't find expected typed element.");
43+
44+
var point = treeRoot.CoordinatesTo(border);
45+
46+
Assert.AreEqual(150, point.X);
47+
Assert.AreEqual(100, point.Y);
48+
49+
// And otherway
50+
point = border.CoordinatesTo(treeRoot);
51+
52+
Assert.AreEqual(-150, point.X);
53+
Assert.AreEqual(-100, point.Y);
54+
});
55+
}
56+
57+
[TestCategory("UIElementExtensions")]
58+
[TestMethod]
59+
public async Task Test_UIElement_Extensions_CoordinatesFrom()
60+
{
61+
await App.DispatcherQueue.EnqueueAsync(async () =>
62+
{
63+
var treeRoot = XamlReader.Load(@"<Page
64+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
65+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
66+
<Border x:Name=""Target"" Margin=""100,150,0,0""/>
67+
</Page>") as Page;
68+
69+
// Test Setup
70+
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
71+
72+
// Initialize Visual Tree
73+
await SetTestContentAsync(treeRoot);
74+
75+
// Main Test
76+
var border = treeRoot.FindDescendant("Target");
77+
78+
Assert.IsNotNull(border, "Expected to find something.");
79+
Assert.IsInstanceOfType(border, typeof(Border), "Didn't find expected typed element.");
80+
81+
var point = border.CoordinatesFrom(treeRoot);
82+
83+
Assert.AreEqual(100, point.X);
84+
Assert.AreEqual(150, point.Y);
85+
86+
// And Backwards
87+
point = treeRoot.CoordinatesFrom(border);
88+
89+
Assert.AreEqual(-100, point.X);
90+
Assert.AreEqual(-150, point.Y);
91+
});
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)