Skip to content

Commit

Permalink
fix: Ensure ShowAsync returns if popup is forcibly closed
Browse files Browse the repository at this point in the history
  • Loading branch information
kazo0 committed Oct 11, 2022
1 parent 5b2e1b8 commit 368f496
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,56 @@ public async Task When_BackButton_Pressed(bool isCloseButtonEnabled)
SUT.Hide();
}
}

[TestMethod]
#if __MACOS__
[Ignore("Currently fails on macOS, part of #9282 epic")]
#endif
public async Task When_Popup_Closed()
{
var closedEvent = new Event();
var openedEvent = new Event();
var closedRegistration = new SafeEventRegistration<ContentDialog, TypedEventHandler<ContentDialog, ContentDialogClosedEventArgs>>("Closed");
var openedRegistration = new SafeEventRegistration<ContentDialog, TypedEventHandler<ContentDialog, ContentDialogOpenedEventArgs>>("Opened");

var SUT = new MyContentDialog
{
Title = "Dialog title",
Content = "Dialog content",
CloseButtonText = "Close",
};

closedRegistration.Attach(SUT, (s, e) => closedEvent.Set());
openedRegistration.Attach(SUT, (s, e) => openedEvent.Set());

try
{
var showAsyncResult = SUT.ShowAsync().AsTask();

await openedEvent.WaitForDefault();

SUT._popup.IsOpen = false;

await closedEvent.WaitForDefault();

VERIFY_IS_TRUE(closedEvent.HasFired());
VERIFY_IS_FALSE(SUT._popup.IsOpen);

if (await Task.WhenAny(showAsyncResult, Task.Delay(2000)) == showAsyncResult)
{
var dialogResult = showAsyncResult.Result;
VERIFY_ARE_EQUAL(ContentDialogResult.None, dialogResult);
}
else
{
Assert.Fail("Timed out waiting for ShowAsync");
}
}
finally
{
SUT.Hide();
}
}
#endif

#if __ANDROID__
Expand Down Expand Up @@ -379,6 +429,7 @@ void OnShowing(InputPane sender, InputPaneVisibilityEventArgs args)
inputPane.Showing -= OnShowing;
}
await WindowHelper.WaitForIdle();
await WindowHelper.WaitForIdle();
}

private static async Task ShowDialog(MyContentDialog dialog)
Expand Down
9 changes: 9 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/ContentDialog/ContentDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public ContentDialog() : base()
that.UpdateVisualState();
}
};

_popup.Closed += (s, e) =>
{
if (thisRef.Target is ContentDialog that)
{
that.Hide();
}
};

this.KeyDown += OnPopupKeyDown;
var inputPane = InputPane.GetForCurrentView();
inputPane.Showing += (o, e) =>
Expand Down

0 comments on commit 368f496

Please sign in to comment.