From 312a73ba458f358b5593c6e400c00652061ad374 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 9 Jun 2022 13:04:27 -0500 Subject: [PATCH 01/16] Fix incorrect scaling units for Visual Tree methods --- .../Elements/VisualElementTreeTests.cs | 86 +++++++++++++++++++ .../tests/DeviceTests/HandlerTestBase.cs | 14 ++- .../tests/DeviceTests/TestCategory.cs | 1 + .../Extensions/VisualTreeElementExtensions.cs | 2 +- .../src/Platform/Android/ViewExtensions.cs | 12 ++- .../Handlers/HandlerTestBaseOfT.Tests.cs | 8 ++ 6 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs diff --git a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs new file mode 100644 index 000000000000..4c9d3877ceb1 --- /dev/null +++ b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs @@ -0,0 +1,86 @@ +using System.Threading.Tasks; +using Microsoft.Maui.Controls; +using Microsoft.Maui.Controls.Handlers; +using Microsoft.Maui.Handlers; +using Microsoft.Maui.Hosting; +using Microsoft.Maui.Platform; +using Xunit; +#if ANDROID || IOS +using ShellHandler = Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer; +#endif + +namespace Microsoft.Maui.DeviceTests +{ + [Category(TestCategory.VisualElementTree)] +#if ANDROID || IOS + [Collection(HandlerTestBase.RunInNewWindowCollection)] +#endif + public partial class VisualElementTreeTests : HandlerTestBase + { + void SetupBuilder() + { + EnsureHandlerCreated(builder => + { + builder.ConfigureMauiHandlers(handlers => + { + handlers.AddHandler(typeof(Controls.Shell), typeof(ShellHandler)); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); +#if WINDOWS + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); +#endif + }); + }); + } + + [Fact] + public async Task GetVisualTreeElements() + { + SetupBuilder(); + var label = new Label() { Text = "Find Me" }; + var page = new ContentPage() { Title = "Title Page" }; + page.Content = new VerticalStackLayout() + { + label + }; + + var shell = await InvokeOnMainThreadAsync(() => + new Shell() { CurrentItem = new FlyoutItem() { Items = { page } } } + ); + + await CreateHandlerAndAddToWindow(shell, async handler => + { + await OnFrameSetToNotEmpty(label); + var locationOnScreen = label.GetLocationOnScreen().Value; + var labelFrame = label.Frame; + var window = shell.Window; + + // Find label at the top left corner + Assert.Contains(label, window.GetVisualTreeElements(locationOnScreen.X, locationOnScreen.Y)); + + // find label at the bottom right corner + Assert.Contains(label, window.GetVisualTreeElements( + locationOnScreen.X + labelFrame.Width - 0.1, + locationOnScreen.Y + labelFrame.Height - 0.1 + )); + + // Ensure that the point directly outside the bounds of the label doesn't + // return the label + Assert.DoesNotContain(label, window.GetVisualTreeElements( + locationOnScreen.X + labelFrame.Width + 0.1, + locationOnScreen.Y + labelFrame.Height + 0.1 + )); + + }); + } + } +} \ No newline at end of file diff --git a/src/Controls/tests/DeviceTests/HandlerTestBase.cs b/src/Controls/tests/DeviceTests/HandlerTestBase.cs index b11b65aee26e..dd14c695d673 100644 --- a/src/Controls/tests/DeviceTests/HandlerTestBase.cs +++ b/src/Controls/tests/DeviceTests/HandlerTestBase.cs @@ -11,6 +11,7 @@ using Microsoft.Maui.Handlers; using Microsoft.Maui.Hosting; using Microsoft.Maui.LifecycleEvents; +using Microsoft.Maui.Platform; using Microsoft.Maui.TestUtils.DeviceTests.Runners; namespace Microsoft.Maui.DeviceTests @@ -209,6 +210,9 @@ await SetupWindowForTests(window, async () => content = pc.CurrentPage; await OnLoadedAsync(content as VisualElement); + + if (content is Page page) + await OnNavigatedToAsync(page); #if WINDOWS await Task.Delay(10); @@ -310,19 +314,23 @@ void NavigatedTo(object sender, NavigatedToEventArgs e) } } - protected Task OnFrameSetToNotEmpty(VisualElement frameworkElement, TimeSpan? timeOut = null) + protected async Task OnFrameSetToNotEmpty(VisualElement frameworkElement, TimeSpan? timeOut = null) { if (frameworkElement.Frame.Height > 0 && frameworkElement.Frame.Width > 0) { - return Task.CompletedTask; + return; } timeOut = timeOut ?? TimeSpan.FromSeconds(2); TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); frameworkElement.BatchCommitted += OnBatchCommitted; - return taskCompletionSource.Task.WaitAsync(timeOut.Value); + await taskCompletionSource.Task.WaitAsync(timeOut.Value); + + // Wait for the layout to propagate to the platform + if (frameworkElement.GetBoundingBox().Equals(new Rect())) + await Task.Delay(10); void OnBatchCommitted(object sender, Controls.Internals.EventArg e) { diff --git a/src/Controls/tests/DeviceTests/TestCategory.cs b/src/Controls/tests/DeviceTests/TestCategory.cs index e382fd9daef2..7a688563b7f2 100644 --- a/src/Controls/tests/DeviceTests/TestCategory.cs +++ b/src/Controls/tests/DeviceTests/TestCategory.cs @@ -24,6 +24,7 @@ public static class TestCategory public const string TabbedPage = "TabbedPage"; public const string TemplatedView = "TemplatedView"; public const string VisualElement = "VisualElement"; + public const string VisualElementTree = "VisualElementTree"; public const string Window = "Window"; } } diff --git a/src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs b/src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs index 905fbd88901c..23728841e493 100644 --- a/src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs +++ b/src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs @@ -149,7 +149,7 @@ static void Impl(IVisualTreeElement visualElement, Predicate intersectElem { if (visualElement is IView view) { - Rect bounds = view.GetPlatformViewBounds(); + Rect bounds = view.GetBoundingBox(); if (intersectElementBounds(bounds)) elements.Add(visualElement); } diff --git a/src/Core/src/Platform/Android/ViewExtensions.cs b/src/Core/src/Platform/Android/ViewExtensions.cs index e3258ca8b22a..6f003d85bf17 100644 --- a/src/Core/src/Platform/Android/ViewExtensions.cs +++ b/src/Core/src/Platform/Android/ViewExtensions.cs @@ -193,7 +193,7 @@ public static void UpdateBackground(this AView platformView, Paint? background) platformView.Background = drawable; } } - else if(platformView is LayoutViewGroup) + else if (platformView is LayoutViewGroup) { platformView.Background = null; } @@ -408,12 +408,18 @@ internal static Graphics.Rect GetBoundingBox(this IView view) internal static Graphics.Rect GetBoundingBox(this View? platformView) { - if (platformView == null) + if (platformView?.Context == null) return new Rect(); + var context = platformView.Context; var rect = new Android.Graphics.Rect(); platformView.GetGlobalVisibleRect(rect); - return new Rect(rect.ExactCenterX() - (rect.Width() / 2), rect.ExactCenterY() - (rect.Height() / 2), (float)rect.Width(), (float)rect.Height()); + + return new Rect( + context.FromPixels(rect.ExactCenterX() - (rect.Width() / 2)), + context.FromPixels(rect.ExactCenterY() - (rect.Height() / 2)), + context.FromPixels((float)rect.Width()), + context.FromPixels((float)rect.Height())); } internal static bool IsLoaded(this View frameworkElement) => diff --git a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs index 1026b0be1040..5404d0137727 100644 --- a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs +++ b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs @@ -189,6 +189,14 @@ public async Task ReturnsNonEmptyNativeBoundingBounds(int size) var nativeBoundingBox = await GetValueAsync(view, handler => GetBoundingBox(handler)); Assert.NotEqual(nativeBoundingBox, new Graphics.Rect()); + + if (!CloseEnough(size, nativeBoundingBox.Size.Height) || !CloseEnough(size, nativeBoundingBox.Size.Width)) + Assert.Equal(new Size(size, size), nativeBoundingBox.Size); + + bool CloseEnough(double value1, double value2) + { + return System.Math.Abs(value2 - value1) < 0.1; + } } From dc9d6ebbf9cfb88838fd80c6957afe19ead0f946 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 9 Jun 2022 14:05:49 -0500 Subject: [PATCH 02/16] Update HandlerTestBaseOfT.Tests.cs --- src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs index 5404d0137727..b9e6010664f4 100644 --- a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs +++ b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs @@ -195,7 +195,7 @@ public async Task ReturnsNonEmptyNativeBoundingBounds(int size) bool CloseEnough(double value1, double value2) { - return System.Math.Abs(value2 - value1) < 0.1; + return System.Math.Abs(value2 - value1) < 0.2; } } From be2fa4b6c9abd0ea1397006a8e91467d00885b49 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 9 Jun 2022 21:19:11 -0500 Subject: [PATCH 03/16] - fix tests --- src/Controls/tests/DeviceTests/HandlerTestBase.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Controls/tests/DeviceTests/HandlerTestBase.cs b/src/Controls/tests/DeviceTests/HandlerTestBase.cs index dd14c695d673..91c952c4caa7 100644 --- a/src/Controls/tests/DeviceTests/HandlerTestBase.cs +++ b/src/Controls/tests/DeviceTests/HandlerTestBase.cs @@ -210,14 +210,9 @@ await SetupWindowForTests(window, async () => content = pc.CurrentPage; await OnLoadedAsync(content as VisualElement); - - if (content is Page page) - await OnNavigatedToAsync(page); #if WINDOWS - await Task.Delay(10); #endif - if (typeof(THandler).IsAssignableFrom(window.Handler.GetType())) await action((THandler)window.Handler); else if (typeof(THandler).IsAssignableFrom(window.Content.Handler.GetType())) From 98bc753b852b14522f53c94a3d938fa16a2da196 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 9 Jun 2022 21:38:05 -0500 Subject: [PATCH 04/16] - increase delta --- .../tests/DeviceTests/Elements/VisualElementTreeTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs index 4c9d3877ceb1..cee08ad32cbb 100644 --- a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs @@ -69,15 +69,15 @@ await CreateHandlerAndAddToWindow(shell, async handler => // find label at the bottom right corner Assert.Contains(label, window.GetVisualTreeElements( - locationOnScreen.X + labelFrame.Width - 0.1, - locationOnScreen.Y + labelFrame.Height - 0.1 + locationOnScreen.X + labelFrame.Width - 0.2, + locationOnScreen.Y + labelFrame.Height - 0.2 )); // Ensure that the point directly outside the bounds of the label doesn't // return the label Assert.DoesNotContain(label, window.GetVisualTreeElements( - locationOnScreen.X + labelFrame.Width + 0.1, - locationOnScreen.Y + labelFrame.Height + 0.1 + locationOnScreen.X + labelFrame.Width + 0.2, + locationOnScreen.Y + labelFrame.Height + 0.2 )); }); From f6dacf6a5af512ec67255395a4cc3ab39d635946 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 07:46:28 -0500 Subject: [PATCH 05/16] - shift pixels a little more --- .../DeviceTests/Elements/VisualElementTreeTests.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs index cee08ad32cbb..52f06946e34a 100644 --- a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs @@ -65,19 +65,19 @@ await CreateHandlerAndAddToWindow(shell, async handler => var window = shell.Window; // Find label at the top left corner - Assert.Contains(label, window.GetVisualTreeElements(locationOnScreen.X, locationOnScreen.Y)); + Assert.Contains(label, window.GetVisualTreeElements(locationOnScreen.X + 0.1, locationOnScreen.Y + 0.1)); // find label at the bottom right corner Assert.Contains(label, window.GetVisualTreeElements( - locationOnScreen.X + labelFrame.Width - 0.2, - locationOnScreen.Y + labelFrame.Height - 0.2 + locationOnScreen.X + labelFrame.Width - 1, + locationOnScreen.Y + labelFrame.Height - 1 )); // Ensure that the point directly outside the bounds of the label doesn't // return the label Assert.DoesNotContain(label, window.GetVisualTreeElements( - locationOnScreen.X + labelFrame.Width + 0.2, - locationOnScreen.Y + labelFrame.Height + 0.2 + locationOnScreen.X + labelFrame.Width + 1, + locationOnScreen.Y + labelFrame.Height + 1 )); }); From e535e290e113ddfe7ff42ca822a2ac0d53bb8637 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 07:47:51 -0500 Subject: [PATCH 06/16] - navpage --- .../tests/DeviceTests/Elements/VisualElementTreeTests.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs index 52f06946e34a..6a9593fbde05 100644 --- a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs @@ -24,6 +24,7 @@ void SetupBuilder() builder.ConfigureMauiHandlers(handlers => { handlers.AddHandler(typeof(Controls.Shell), typeof(ShellHandler)); + handlers.AddHandler(typeof(Controls.NavigationPage), typeof(NavigationViewHandler)); handlers.AddHandler(); handlers.AddHandler(); handlers.AddHandler(); @@ -53,16 +54,16 @@ public async Task GetVisualTreeElements() label }; - var shell = await InvokeOnMainThreadAsync(() => - new Shell() { CurrentItem = new FlyoutItem() { Items = { page } } } + var rootPage = await InvokeOnMainThreadAsync(() => + new NavigationPage(page) ); - await CreateHandlerAndAddToWindow(shell, async handler => + await CreateHandlerAndAddToWindow(rootPage, async handler => { await OnFrameSetToNotEmpty(label); var locationOnScreen = label.GetLocationOnScreen().Value; var labelFrame = label.Frame; - var window = shell.Window; + var window = rootPage.Window; // Find label at the top left corner Assert.Contains(label, window.GetVisualTreeElements(locationOnScreen.X + 0.1, locationOnScreen.Y + 0.1)); From 18bb12728d6a1434321c95122d8d19dd85efa21b Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 08:38:35 -0500 Subject: [PATCH 07/16] - try longer await --- src/Controls/tests/DeviceTests/HandlerTestBase.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Controls/tests/DeviceTests/HandlerTestBase.cs b/src/Controls/tests/DeviceTests/HandlerTestBase.cs index 91c952c4caa7..605be32d3233 100644 --- a/src/Controls/tests/DeviceTests/HandlerTestBase.cs +++ b/src/Controls/tests/DeviceTests/HandlerTestBase.cs @@ -324,8 +324,7 @@ protected async Task OnFrameSetToNotEmpty(VisualElement frameworkElement, TimeSp await taskCompletionSource.Task.WaitAsync(timeOut.Value); // Wait for the layout to propagate to the platform - if (frameworkElement.GetBoundingBox().Equals(new Rect())) - await Task.Delay(10); + await Task.Delay(1000); void OnBatchCommitted(object sender, Controls.Internals.EventArg e) { From c1e2dcfb178d33be5ea6d32166166e92acd3de06 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 10:47:50 -0500 Subject: [PATCH 08/16] - Add Api to iOS --- src/Core/src/Platform/iOS/ViewExtensions.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Core/src/Platform/iOS/ViewExtensions.cs b/src/Core/src/Platform/iOS/ViewExtensions.cs index 53adda5ee399..0fbe30e9a144 100644 --- a/src/Core/src/Platform/iOS/ViewExtensions.cs +++ b/src/Core/src/Platform/iOS/ViewExtensions.cs @@ -388,6 +388,17 @@ internal static Matrix4x4 GetViewTransform(this IView view) internal static Matrix4x4 GetViewTransform(this UIView view) => view.Layer.GetViewTransform(); + internal static Point GetLocationOnScreen(this UIView view) => + view.GetPlatformViewBounds().Location; + + internal static Point? GetLocationOnScreen(this IElement element) + { + if (element.Handler?.MauiContext == null) + return null; + + return (element.ToPlatform())?.GetLocationOnScreen(); + } + internal static Graphics.Rect GetBoundingBox(this IView view) => view.ToPlatform().GetBoundingBox(); From cc63c190f6630864465fe33df6b5a584581c7a13 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 11:14:14 -0500 Subject: [PATCH 09/16] - improve failure message --- .../Elements/VisualElementTreeTests.cs | 17 ++++++++++++----- .../src/DeviceTests/AssertionExtensions.cs | 13 +++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs index 6a9593fbde05..48db9da6c5ee 100644 --- a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs @@ -66,13 +66,20 @@ await CreateHandlerAndAddToWindow(rootPage, async handler => var window = rootPage.Window; // Find label at the top left corner - Assert.Contains(label, window.GetVisualTreeElements(locationOnScreen.X + 0.1, locationOnScreen.Y + 0.1)); + var topLeft = new Graphics.Point(locationOnScreen.X, locationOnScreen.Y); + + AssertionExtensions.AssertWithMessage( + () => Assert.Contains(label, window.GetVisualTreeElements(topLeft)), + $"Unable to find label using top left coordinate: {topLeft} with label frame of: {labelFrame}"); // find label at the bottom right corner - Assert.Contains(label, window.GetVisualTreeElements( - locationOnScreen.X + labelFrame.Width - 1, - locationOnScreen.Y + labelFrame.Height - 1 - )); + var bottomRight = new Graphics.Point( + locationOnScreen.X + labelFrame.Width - 1, + locationOnScreen.Y + labelFrame.Height - 1); + + AssertionExtensions.AssertWithMessage( + () => Assert.Contains(label, window.GetVisualTreeElements(bottomRight)), + $"Unable to find label using bottom right coordinate: {bottomRight} with label frame of: {labelFrame}"); // Ensure that the point directly outside the bounds of the label doesn't // return the label diff --git a/src/TestUtils/src/DeviceTests/AssertionExtensions.cs b/src/TestUtils/src/DeviceTests/AssertionExtensions.cs index 4ddcadb272e3..970e0e223bf4 100644 --- a/src/TestUtils/src/DeviceTests/AssertionExtensions.cs +++ b/src/TestUtils/src/DeviceTests/AssertionExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.ExceptionServices; using System.Threading.Tasks; using Xunit; using Xunit.Sdk; @@ -29,5 +30,17 @@ public static void AssertHasFlag(this Enum self, Enum flag) if (!hasFlag) throw new ContainsException(flag, self); } + + public static void AssertWithMessage(Action assertion, string message) + { + try + { + assertion(); + } + catch (Exception e) + { + Assert.True(false, $"Message: {message} Failure: {e}"); + } + } } } \ No newline at end of file From ef5cb90d910ac8c0949c6e46bd294e4bd036398f Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 11:58:43 -0500 Subject: [PATCH 10/16] - even better logging messages --- .../tests/DeviceTests/Elements/VisualElementTreeTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs index 48db9da6c5ee..cbd9b92fccba 100644 --- a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs @@ -70,7 +70,7 @@ await CreateHandlerAndAddToWindow(rootPage, async handler => AssertionExtensions.AssertWithMessage( () => Assert.Contains(label, window.GetVisualTreeElements(topLeft)), - $"Unable to find label using top left coordinate: {topLeft} with label frame of: {labelFrame}"); + $"Unable to find label using top left coordinate: {topLeft} with label location: {label.GetBoundingBox()}"); // find label at the bottom right corner var bottomRight = new Graphics.Point( @@ -79,7 +79,7 @@ await CreateHandlerAndAddToWindow(rootPage, async handler => AssertionExtensions.AssertWithMessage( () => Assert.Contains(label, window.GetVisualTreeElements(bottomRight)), - $"Unable to find label using bottom right coordinate: {bottomRight} with label frame of: {labelFrame}"); + $"Unable to find label using bottom right coordinate: {bottomRight} with label location: {label.GetBoundingBox()}"); // Ensure that the point directly outside the bounds of the label doesn't // return the label From 5ef8f248db1c6c799d0edaaaf6be8ff465213781 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 12:18:26 -0500 Subject: [PATCH 11/16] - offset top left corner --- .../tests/DeviceTests/Elements/VisualElementTreeTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs index cbd9b92fccba..e53d1feb1d3a 100644 --- a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs @@ -66,7 +66,7 @@ await CreateHandlerAndAddToWindow(rootPage, async handler => var window = rootPage.Window; // Find label at the top left corner - var topLeft = new Graphics.Point(locationOnScreen.X, locationOnScreen.Y); + var topLeft = new Graphics.Point(locationOnScreen.X + 1, locationOnScreen.Y + 1); AssertionExtensions.AssertWithMessage( () => Assert.Contains(label, window.GetVisualTreeElements(topLeft)), From 82f28198b1d62f087e8e06222c1920d9053de930 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 12:26:48 -0500 Subject: [PATCH 12/16] Update VisualElementTreeTests.cs --- .../tests/DeviceTests/Elements/VisualElementTreeTests.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs index e53d1feb1d3a..3b964fb0b1ad 100644 --- a/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs @@ -24,7 +24,11 @@ void SetupBuilder() builder.ConfigureMauiHandlers(handlers => { handlers.AddHandler(typeof(Controls.Shell), typeof(ShellHandler)); +#if IOS + handlers.AddHandler(typeof(Controls.NavigationPage), typeof(Controls.Handlers.Compatibility.NavigationRenderer)); +#else handlers.AddHandler(typeof(Controls.NavigationPage), typeof(NavigationViewHandler)); +#endif handlers.AddHandler(); handlers.AddHandler(); handlers.AddHandler(); @@ -91,4 +95,4 @@ await CreateHandlerAndAddToWindow(rootPage, async handler => }); } } -} \ No newline at end of file +} From 0c39aecc93e6dd46d1a325ec767d591da722512c Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 13:15:22 -0500 Subject: [PATCH 13/16] - workaround label/progress bugs --- .../Handlers/HandlerTestBaseOfT.Tests.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs index b9e6010664f4..ff8973770394 100644 --- a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs +++ b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Tests.cs @@ -190,8 +190,20 @@ public async Task ReturnsNonEmptyNativeBoundingBounds(int size) var nativeBoundingBox = await GetValueAsync(view, handler => GetBoundingBox(handler)); Assert.NotEqual(nativeBoundingBox, new Graphics.Rect()); - if (!CloseEnough(size, nativeBoundingBox.Size.Height) || !CloseEnough(size, nativeBoundingBox.Size.Width)) - Assert.Equal(new Size(size, size), nativeBoundingBox.Size); + + // Currently there's an issue with label/progress where they don't set the frame size to + // the explicit Width and Height values set + // https://github.com/dotnet/maui/issues/7935 + if (view is ILabel || view is IProgress) + { + if (!CloseEnough(size, nativeBoundingBox.Size.Width)) + Assert.Equal(new Size(size, size), nativeBoundingBox.Size); + } + else + { + if (!CloseEnough(size, nativeBoundingBox.Size.Height) || !CloseEnough(size, nativeBoundingBox.Size.Width)) + Assert.Equal(new Size(size, size), nativeBoundingBox.Size); + } bool CloseEnough(double value1, double value2) { From bd5bde0451a9c734dd70198954da487816e9328c Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 13:19:19 -0500 Subject: [PATCH 14/16] Update HandlerTestBase.cs --- src/Controls/tests/DeviceTests/HandlerTestBase.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Controls/tests/DeviceTests/HandlerTestBase.cs b/src/Controls/tests/DeviceTests/HandlerTestBase.cs index 605be32d3233..91c952c4caa7 100644 --- a/src/Controls/tests/DeviceTests/HandlerTestBase.cs +++ b/src/Controls/tests/DeviceTests/HandlerTestBase.cs @@ -324,7 +324,8 @@ protected async Task OnFrameSetToNotEmpty(VisualElement frameworkElement, TimeSp await taskCompletionSource.Task.WaitAsync(timeOut.Value); // Wait for the layout to propagate to the platform - await Task.Delay(1000); + if (frameworkElement.GetBoundingBox().Equals(new Rect())) + await Task.Delay(10); void OnBatchCommitted(object sender, Controls.Internals.EventArg e) { From a76a4014f4f8532a4b6ee3ee5a5586fc69158761 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 13:29:01 -0500 Subject: [PATCH 15/16] - use toplatform for bounding boxes --- .../tests/DeviceTests/Handlers/HandlerTestBaseOfT.Android.cs | 4 ++-- .../tests/DeviceTests/Handlers/HandlerTestBaseOfT.Windows.cs | 4 ++-- src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.iOS.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Android.cs b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Android.cs index cdeb73dfdb80..221b93784af1 100644 --- a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Android.cs @@ -301,10 +301,10 @@ protected Visibility GetVisibility(IViewHandler viewHandler) } protected Maui.Graphics.Rect GetPlatformViewBounds(IViewHandler viewHandler) => - ((View)viewHandler.PlatformView).GetPlatformViewBounds(); + viewHandler.VirtualView.ToPlatform().GetPlatformViewBounds(); protected Maui.Graphics.Rect GetBoundingBox(IViewHandler viewHandler) => - ((View)viewHandler.PlatformView).GetBoundingBox(); + viewHandler.VirtualView.ToPlatform().GetBoundingBox(); protected Matrix4x4 GetViewTransform(IViewHandler viewHandler) => ((View)viewHandler.PlatformView).GetViewTransform(); diff --git a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Windows.cs b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Windows.cs index 3e2ce280af2a..8308a300e5be 100644 --- a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Windows.cs +++ b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.Windows.cs @@ -138,7 +138,7 @@ public async Task InputTransparencyInitializesCorrectly(bool inputTransparent) protected Task GetPlatformViewBounds(IViewHandler viewHandler) { - var fe = (FrameworkElement)viewHandler.PlatformView; + var fe = viewHandler.VirtualView.ToPlatform(); return fe.AttachAndRun(() => fe.GetPlatformViewBounds()); } @@ -147,7 +147,7 @@ protected System.Numerics.Matrix4x4 GetViewTransform(IViewHandler viewHandler) = protected Task GetBoundingBox(IViewHandler viewHandler) { - var fe = (FrameworkElement)viewHandler.PlatformView; + var fe = viewHandler.VirtualView.ToPlatform(); return fe.AttachAndRun(() => fe.GetBoundingBox()); } diff --git a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.iOS.cs b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.iOS.cs index fd026ac14d97..67cab60eaa6a 100644 --- a/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.iOS.cs +++ b/src/Core/tests/DeviceTests/Handlers/HandlerTestBaseOfT.iOS.cs @@ -173,10 +173,10 @@ protected bool GetIsAccessibilityElement(IViewHandler viewHandler) } protected Maui.Graphics.Rect GetPlatformViewBounds(IViewHandler viewHandler) => - ((UIView)viewHandler.PlatformView).GetPlatformViewBounds(); + viewHandler.VirtualView.ToPlatform().GetPlatformViewBounds(); protected Maui.Graphics.Rect GetBoundingBox(IViewHandler viewHandler) => - ((UIView)viewHandler.PlatformView).GetBoundingBox(); + viewHandler.VirtualView.ToPlatform().GetBoundingBox(); protected System.Numerics.Matrix4x4 GetViewTransform(IViewHandler viewHandler) => ((UIView)viewHandler.PlatformView).GetViewTransform(); From 529a4dcbf21a8ace50e54d7f059cc52bea604b92 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Fri, 10 Jun 2022 14:26:57 -0500 Subject: [PATCH 16/16] - refine tests --- .../Elements/Compatibility/VisualElementRendererTests.cs | 4 +++- src/Controls/tests/DeviceTests/HandlerTestBase.cs | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/Compatibility/VisualElementRendererTests.cs b/src/Controls/tests/DeviceTests/Elements/Compatibility/VisualElementRendererTests.cs index 268b6a8286c3..0cd6a6c3f4a3 100644 --- a/src/Controls/tests/DeviceTests/Elements/Compatibility/VisualElementRendererTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Compatibility/VisualElementRendererTests.cs @@ -47,9 +47,11 @@ void SetupBuilder() public async Task CompatibilityRendererWorksWithNoInnerContrlSpecified() { SetupBuilder(); + var renderer = await InvokeOnMainThreadAsync(() => new LegacyComponent().ToPlatform(MauiContext)); - Assert.Equal(renderer, (renderer as IPlatformViewHandler).PlatformView); + await InvokeOnMainThreadAsync(() => Assert.Equal(renderer, (renderer as IPlatformViewHandler).PlatformView)); + Assert.NotNull(renderer); } } diff --git a/src/Controls/tests/DeviceTests/HandlerTestBase.cs b/src/Controls/tests/DeviceTests/HandlerTestBase.cs index 91c952c4caa7..91556289be5d 100644 --- a/src/Controls/tests/DeviceTests/HandlerTestBase.cs +++ b/src/Controls/tests/DeviceTests/HandlerTestBase.cs @@ -324,8 +324,9 @@ protected async Task OnFrameSetToNotEmpty(VisualElement frameworkElement, TimeSp await taskCompletionSource.Task.WaitAsync(timeOut.Value); // Wait for the layout to propagate to the platform - if (frameworkElement.GetBoundingBox().Equals(new Rect())) - await Task.Delay(10); + await AssertionExtensions.Wait( + () => !frameworkElement.GetBoundingBox().Size.Equals(Size.Zero) + ); void OnBatchCommitted(object sender, Controls.Internals.EventArg e) {