Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2836. v1 RequestStop while a ContextMenu is open can throw an exception. #2837

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void Container_Closing (ToplevelClosingEventArgs obj)
/// </summary>
public void Hide ()
{
menuBar.CleanUp ();
menuBar?.CleanUp ();
Dispose ();
}

Expand Down
73 changes: 69 additions & 4 deletions UnitTests/Menus/ContextMenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,21 @@ public void ContextMenu_Constructors ()
Assert.NotNull (cm.Host);
}

[Fact]
[AutoInitShutdown]
public void Show_Hide_IsShow ()
private ContextMenu Create_ContextMenu_With_Two_MenuItem (int x, int y)
{
var cm = new ContextMenu (10, 5,
return new ContextMenu (x, y,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
}

[Fact]
[AutoInitShutdown]
public void Show_Hide_IsShow ()
{
var cm = Create_ContextMenu_With_Two_MenuItem (10, 5);

cm.Show ();
Assert.True (ContextMenu.IsShow);
Expand Down Expand Up @@ -902,5 +907,65 @@ public void Key_Open_And_Close_The_ContextMenu ()
Assert.True (top.Subviews [1].ProcessKey (new KeyEvent (Key.F10 | Key.ShiftMask, new KeyModifiers ())));
Assert.Null (tf.ContextMenu.MenuBar);
}

[Fact, AutoInitShutdown]
public void RequestStop_While_ContextMenu_Is_Open_Does_Not_Throws ()
{
var cm = Create_ContextMenu_With_Two_MenuItem (10, 5);
var top = Application.Top;
var isMenuAllClosed = false;
MenuBarItem mi = null;
var iterations = -1;
Application.Iteration += () => {
iterations++;
if (iterations == 0) {
cm.Show ();
Assert.True (ContextMenu.IsShow);
mi = cm.MenuBar.Menus [0];
mi.Action = () => {
var dialog1 = new Dialog ();
Application.Run (dialog1);

Assert.False (ContextMenu.IsShow);
Assert.True (isMenuAllClosed);
};

cm.MenuBar.MenuAllClosed += () => isMenuAllClosed = true;

} else if (iterations == 1) {
mi.Action ();
} else if (iterations == 2) {
Application.RequestStop ();
} else if (iterations == 3) {
isMenuAllClosed = false;
cm.Show ();
Assert.True (ContextMenu.IsShow);

cm.MenuBar.MenuAllClosed += () => isMenuAllClosed = true;
} else if (iterations == 4) {
var exception = Record.Exception (() => Application.RequestStop ());
Assert.Null (exception);
} else {
Application.RequestStop ();
}
};

var isTopClosed = false;
top.Closing += (_) => {
var dialog2 = new Dialog ();
Application.Run (dialog2);

Assert.False (ContextMenu.IsShow);
Assert.True (isMenuAllClosed);

isTopClosed = true;
};

Application.Run ();

Assert.True (isTopClosed);
Assert.False (ContextMenu.IsShow);
Assert.True (isMenuAllClosed);
}
}
}
Loading