Skip to content

Commit

Permalink
chore: fix drawer flyout tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Nov 9, 2023
1 parent bd3625f commit 1392331
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions src/Uno.Toolkit.RuntimeTests/Tests/DrawerFlyoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ namespace Uno.Toolkit.RuntimeTests.Tests;
[RunsOnUIThread]
internal class DrawerFlyoutTests
{
#if !DEBUG
[TestCleanup] // note that this is only run, after each [TestMethod], **not** between [DataRow].
public void CloseOpenPopup()
{
// killing the host, will close the associated popup.
UnitTestsUIContentHelper.Content = null;
}
#endif

[TestMethod]
public async Task Can_Open()
Expand All @@ -58,7 +56,7 @@ public async Task Can_Open()
GetOpenPopup();
}

#if false // uno#14372: can't use "xmlns'd attached property style setter" with XamlReader
#if !HAS_UNO // uno#14372: can't use "xmlns'd attached property style setter" with XamlReader
[TestMethod]
#endif
public async Task AttachedProperty_Inheritance()
Expand All @@ -68,7 +66,7 @@ public async Task AttachedProperty_Inheritance()
<Flyout.FlyoutPresenterStyle>
<Style BasedOn="{StaticResource DrawerFlyoutPresenterStyle}" TargetType="FlyoutPresenter">
<Setter Property="utu:DrawerFlyoutPresenter.OpenDirection" Value="Left" />
<Setter Property="utu:DrawerFlyoutPresenter.DrawerDepth" Value="3.12*" />
<Setter Property="utu:DrawerFlyoutPresenter.DrawerDepth" Value="312*" />
<Setter Property="utu:DrawerFlyoutPresenter.IsGestureEnabled" Value="True" />
<Setter Property="utu:DrawerFlyoutPresenter.LightDismissOverlayBackground" Value="Pink" />
</Style>
Expand All @@ -82,13 +80,18 @@ public async Task AttachedProperty_Inheritance()
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
SUT.Flyout.ShowAt(SUT);

#if !HAS_UNO
await UnitTestUIContentHelperEx.WaitFor(() => GetOpenPopupsCompat().Any(), message: "Timeout waiting on flyout open");
#endif

var popup = GetOpenPopup();
var presenter = popup.Child.GetFirstDescendant<DrawerFlyoutPresenter>() ?? throw new InvalidOperationException("DrawerFlyoutPresenter not found");
var child = popup.Child as FlyoutPresenter ?? throw new InvalidOperationException("FlyoutPresenter not found");
await UnitTestsUIContentHelper.WaitForLoaded(child);

// note: don't need to wait for DrawerFlyoutPresenter to be loaded here, since we don't care about its measurements...
var presenter = popup.Child.GetFirstDescendant<DrawerFlyoutPresenter>() ?? throw new InvalidOperationException("DrawerFlyoutPresenter not found");

Assert.AreEqual(presenter.OpenDirection, DrawerOpenDirection.Left);
Assert.AreEqual(presenter.DrawerDepth, new GridLength(3.12, GridUnitType.Star));
Assert.AreEqual(presenter.DrawerDepth, new GridLength(312, GridUnitType.Star));
Assert.AreEqual(presenter.IsGestureEnabled, true);
Assert.AreEqual((presenter.LightDismissOverlayBackground as SolidColorBrush)?.Color, Colors.Pink);
}
Expand All @@ -100,22 +103,22 @@ public async Task AttachedProperty_Inheritance()
[DataRow(DrawerOpenDirection.Down)]
public async Task OpenDirection_Layout(DrawerOpenDirection openDirection)
{
#if false // uno#14372: can't use "xmlns'd attached property style setter" with XamlReader
//var SUT = BuildButtonFlyout($$"""
// <Flyout Placement="Full">
// <Flyout.FlyoutPresenterStyle>
// <Style BasedOn="{StaticResource DrawerFlyoutPresenterStyle}" TargetType="FlyoutPresenter">
// <Setter Property="utu:DrawerFlyoutPresenter.OpenDirection" Value="{{openDirection}}" />
// <Setter Property="utu:DrawerFlyoutPresenter.DrawerDepth" Value="0.5*" />
// <Setter Property="Background" Value="SkyBlue" />
// <Setter Property="utu:DrawerFlyoutPresenter.LightDismissOverlayBackground" Value="#80FFC0CB" />
// </Style>
// </Flyout.FlyoutPresenterStyle>
// <Border x:Name="FlyoutContentBorder">
// <TextBlock Text="{{openDirection}}" />
// </Border>
// </Flyout>
//""");
#if !HAS_UNO // uno#14372: can't use "xmlns'd attached property style setter" with XamlReader
var SUT = BuildButtonFlyout($$"""
<Flyout Placement="Full">
<Flyout.FlyoutPresenterStyle>
<Style BasedOn="{StaticResource DrawerFlyoutPresenterStyle}" TargetType="FlyoutPresenter">
<Setter Property="utu:DrawerFlyoutPresenter.OpenDirection" Value="{{openDirection}}" />
<Setter Property="utu:DrawerFlyoutPresenter.DrawerDepth" Value="0.5*" />
<Setter Property="Background" Value="SkyBlue" />
<Setter Property="utu:DrawerFlyoutPresenter.LightDismissOverlayBackground" Value="#80FFC0CB" />
</Style>
</Flyout.FlyoutPresenterStyle>
<Border x:Name="FlyoutContentBorder">
<TextBlock Text="{{openDirection}}" />
</Border>
</Flyout>
""");
#else
var SUT = new Button
{
Expand All @@ -141,7 +144,10 @@ void Setup(DrawerFlyoutPresenter presenter)
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
SUT.Flyout.ShowAt(SUT);

// Flyout.ShowAt guarantees its presence in visual tree, but it is not loaded, and we wont be able to read any measurements.
#if !HAS_UNO
await UnitTestUIContentHelperEx.WaitFor(() => GetOpenPopupsCompat().Any(), message: "Timeout waiting on flyout open");
#endif

var popup = GetOpenPopup();
var child = popup.Child as FlyoutPresenter ?? throw new InvalidOperationException("FlyoutPresenter not found");
await UnitTestsUIContentHelper.WaitForLoaded(child);
Expand All @@ -165,6 +171,10 @@ void Setup(DrawerFlyoutPresenter presenter)
? (AvailableLength: presenter.ActualWidth, ContentLength: content.ActualWidth, PrimaryAxis: nameof(presenter.Width))
: (AvailableLength: presenter.ActualHeight, ContentLength: content.ActualHeight, PrimaryAxis: nameof(presenter.Height));
Assert.AreEqual(ctx.AvailableLength * 0.5, ctx.ContentLength, ctx.AvailableLength * 0.05, $"Invalid content size, expecting it to be half of given: {presenter.ActualWidth}x{presenter.ActualHeight} vs {content.ActualWidth}x{content.ActualHeight}, axis={ctx.PrimaryAxis}, direction={openDirection}");

#if !HAS_UNO
UnitTestsUIContentHelper.Content = null;
#endif
}

private static Button BuildButtonFlyout(string flyoutXaml, string? header = null)
Expand All @@ -184,7 +194,7 @@ private static IReadOnlyList<Popup> GetOpenPopupsCompat()
{
return VisualTreeHelper
#if IS_WINUI
.GetOpenPopupsForXamlRoot(Window.Current.Content.XamlRoot);
.GetOpenPopupsForXamlRoot(UnitTestsUIContentHelper.RootElement?.XamlRoot);
#else
.GetOpenPopups(UnitTestsUIContentHelper.CurrentTestWindow);
#endif
Expand Down

0 comments on commit 1392331

Please sign in to comment.