Skip to content

Commit

Permalink
Ensure views have size in Android tests so we know they've been laid out
Browse files Browse the repository at this point in the history
  • Loading branch information
hartez committed Dec 20, 2023
1 parent 9fbc6d3 commit 388088b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Controls/tests/DeviceTests/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.Maui.DeviceTests
public static class Extensions
{
public static Task WaitUntilLoaded(this Image image, int timeout = 1000) =>
AssertEventually(() => !image.IsLoading, timeout);
AssertEventually(() => !image.IsLoading, timeout, message: $"Timed out loading image {image}");

public static void SetupShellHandlers(this MauiAppBuilder builder)
{
Expand Down
10 changes: 9 additions & 1 deletion src/TestUtils/src/DeviceTests/AssertionExtensions.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using MColor = Microsoft.Maui.Graphics.Color;
using SearchView = AndroidX.AppCompat.Widget.SearchView;
using static Microsoft.Maui.DeviceTests.AssertHelpers;
using AndroidX.Core.View;

namespace Microsoft.Maui.DeviceTests
{
Expand Down Expand Up @@ -217,7 +218,7 @@ public static async Task WaitForKeyboardToHide(this AView view, int timeout = 10
public static Task WaitForLayoutOrNonZeroSize(this AView view, int timeout = 1000) =>
Task.WhenAll(
view.WaitForLayout(timeout),
AssertEventually(() => view.Width > 0 && view.Height > 0, timeout,
AssertEventually(() => ViewCompat.IsLaidOut(view), timeout,
message: $"Timed out waiting for {view} to have {nameof(view.Width)} and {nameof(view.Height)} greater than zero"));

public static Task<bool> WaitForLayout(this AView view, int timeout = 1000)
Expand Down Expand Up @@ -333,6 +334,13 @@ public static async Task<T> AttachAndRun<T>(this AView view, Func<Task<T>> actio
Gravity = GravityFlags.Center
};

// Ensure that the view gets _some_ width/height; the combination of WrapContent and Center above means that
// the default Fill layout options will have no effect, so unless the root test object or its Content has a
// WidthRequest/HeightRequest, the actual layout height and width may be zero

view.SetMinimumWidth(40);
view.SetMinimumHeight(40);

var act = context.GetActivity()!;
var rootView = act.FindViewById<FrameLayout>(Android.Resource.Id.Content)!;

Expand Down

0 comments on commit 388088b

Please sign in to comment.