|
| 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