Skip to content

Commit 368f496

Browse files
committed
fix: Ensure ShowAsync returns if popup is forcibly closed
1 parent 5b2e1b8 commit 368f496

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_ContentDialog.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,56 @@ public async Task When_BackButton_Pressed(bool isCloseButtonEnabled)
327327
SUT.Hide();
328328
}
329329
}
330+
331+
[TestMethod]
332+
#if __MACOS__
333+
[Ignore("Currently fails on macOS, part of #9282 epic")]
334+
#endif
335+
public async Task When_Popup_Closed()
336+
{
337+
var closedEvent = new Event();
338+
var openedEvent = new Event();
339+
var closedRegistration = new SafeEventRegistration<ContentDialog, TypedEventHandler<ContentDialog, ContentDialogClosedEventArgs>>("Closed");
340+
var openedRegistration = new SafeEventRegistration<ContentDialog, TypedEventHandler<ContentDialog, ContentDialogOpenedEventArgs>>("Opened");
341+
342+
var SUT = new MyContentDialog
343+
{
344+
Title = "Dialog title",
345+
Content = "Dialog content",
346+
CloseButtonText = "Close",
347+
};
348+
349+
closedRegistration.Attach(SUT, (s, e) => closedEvent.Set());
350+
openedRegistration.Attach(SUT, (s, e) => openedEvent.Set());
351+
352+
try
353+
{
354+
var showAsyncResult = SUT.ShowAsync().AsTask();
355+
356+
await openedEvent.WaitForDefault();
357+
358+
SUT._popup.IsOpen = false;
359+
360+
await closedEvent.WaitForDefault();
361+
362+
VERIFY_IS_TRUE(closedEvent.HasFired());
363+
VERIFY_IS_FALSE(SUT._popup.IsOpen);
364+
365+
if (await Task.WhenAny(showAsyncResult, Task.Delay(2000)) == showAsyncResult)
366+
{
367+
var dialogResult = showAsyncResult.Result;
368+
VERIFY_ARE_EQUAL(ContentDialogResult.None, dialogResult);
369+
}
370+
else
371+
{
372+
Assert.Fail("Timed out waiting for ShowAsync");
373+
}
374+
}
375+
finally
376+
{
377+
SUT.Hide();
378+
}
379+
}
330380
#endif
331381

332382
#if __ANDROID__
@@ -379,6 +429,7 @@ void OnShowing(InputPane sender, InputPaneVisibilityEventArgs args)
379429
inputPane.Showing -= OnShowing;
380430
}
381431
await WindowHelper.WaitForIdle();
432+
await WindowHelper.WaitForIdle();
382433
}
383434

384435
private static async Task ShowDialog(MyContentDialog dialog)

src/Uno.UI/UI/Xaml/Controls/ContentDialog/ContentDialog.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ public ContentDialog() : base()
7070
that.UpdateVisualState();
7171
}
7272
};
73+
74+
_popup.Closed += (s, e) =>
75+
{
76+
if (thisRef.Target is ContentDialog that)
77+
{
78+
that.Hide();
79+
}
80+
};
81+
7382
this.KeyDown += OnPopupKeyDown;
7483
var inputPane = InputPane.GetForCurrentView();
7584
inputPane.Showing += (o, e) =>

0 commit comments

Comments
 (0)