Files
Terminal.Gui/Tests/UnitTests/Dialogs/MessageBoxTests.cs
Tig 47bcf1bf57 Partial on #2975 - Replaces Menu v1 in many places with v2 (#4040)
* touching publish.yml

* Fixed UICatalog bugs. Added fluent tests.

* marked v1 menu stuff as obsolte

* Tweaks.
Added View.GetSubMenus<type>().

* fixed unit tests

* general messing around

* general messing around

* Playing with Fluent

* ColorScheme tweaks

* WIP: ColorScheme tweaks

* Playing with Fluent

* Merged from laptop2

* Hacky-ish fixes to:
- #4016
- #4014

* Fixed Region bug preventing menus without borders from working

* Tweaks

* Fixed a bunch of CM issues

* Fixed OoptionSelector

* ip

* FixedCM issues

* Fixed CM issues2

* Revert "FixedCM issues"

This reverts commit dd6c6a70a3.

* Reverted stuff

* Found and fixed bug in AllViews_Center_Properly

* Fixed CM issues2

* removed menuv2 onapplied.
Changed how UICatalog Applys CM

* changed test time out to see if it helkps with ubuntu fails

* reset app on fail?

* back to 1500ms

* Made StatusBar nullable.

* Code Cleanup.

* HexEditor Code Cleanup.

* HexEditor Code Cleanup.

* Back to 3000ms. Sigh.

* Trying different logic

* Trying different logic2

* Fixed potential crash in runlop

* Fixed potential crash in runlop2

* Tweaked Spinner stuff

* Removed TabView from TextEffects scenario. Not needed and possible culprit.

* back to 2000ms

* WIP: Revamping menu scenarios

* Menu Scenario refinements.
Fixed a few bugs.
Code cleanup.

* fixed unit test

* Fixed warnings

* Fixed warnings2

* Fixed File.Exit

* WIP: Dealing with QuitKey struggles

* WIP: Dealing with QuitKey struggles 2

* WIP: Dealing with QuitKey struggles 3

* Fixed ListView collection nav bug

* Fixed a bunch of menu stuff.
Fixed Appv2 stuff.

* Lots of refactoring and fixing

* Lots of unit test issues

* Fixed DebugIDisposable issues

* Fixed release build issue

* Fixed release build issue 2

* DebugIDisposable -> EnableDebugIDisposableAsserts and more

* DebugIDisposable -> EnableDebugIDisposableAsserts and more 2

* Fixed Menus scenario - context menu

* Added @bdisp suggested assert. Commented it out as it breaks tests.

* Code cleanup

* Fixed disposed but

* Fixed UICatalog exit

* Fixed Unit test I broke.
Added 'Minimal' Theme that turns off all borders etc...
2025-04-24 05:17:58 -06:00

526 lines
22 KiB
C#

using System.Text;
using UICatalog;
using UnitTests;
using Xunit.Abstractions;
namespace Terminal.Gui.DialogTests;
public class MessageBoxTests
{
private readonly ITestOutputHelper _output;
public MessageBoxTests (ITestOutputHelper output) { _output = output; }
[Fact]
[AutoInitShutdown]
public void KeyBindings_Enter_Causes_Focused_Button_Click_No_Accept ()
{
int result = -1;
var iteration = 0;
var btnAcceptCount = 0;
Application.Iteration += (s, a) =>
{
iteration++;
switch (iteration)
{
case 1:
result = MessageBox.Query (string.Empty, string.Empty, 0, false, "btn0", "btn1");
Application.RequestStop ();
break;
case 2:
// Tab to btn2
Application.RaiseKeyDownEvent (Key.Tab);
var btn = Application.Navigation!.GetFocused () as Button;
btn.Accepting += (sender, e) => { btnAcceptCount++; };
// Click
Application.RaiseKeyDownEvent (Key.Enter);
break;
default:
Assert.Fail ();
break;
}
};
Application.Run ().Dispose ();
Assert.Equal (1, result);
Assert.Equal (1, btnAcceptCount);
}
[Fact]
[AutoInitShutdown]
public void KeyBindings_Esc_Closes ()
{
var result = 999;
var iteration = 0;
Application.Iteration += (s, a) =>
{
iteration++;
switch (iteration)
{
case 1:
result = MessageBox.Query (string.Empty, string.Empty, 0, false, "btn0", "btn1");
Application.RequestStop ();
break;
case 2:
Application.RaiseKeyDownEvent (Key.Esc);
break;
default:
Assert.Fail ();
break;
}
};
Application.Run ().Dispose ();
Assert.Equal (-1, result);
}
[Fact]
[AutoInitShutdown]
public void KeyBindings_Space_Causes_Focused_Button_Click_No_Accept ()
{
int result = -1;
var iteration = 0;
var btnAcceptCount = 0;
Application.Iteration += (s, a) =>
{
iteration++;
switch (iteration)
{
case 1:
result = MessageBox.Query (string.Empty, string.Empty, 0, false, "btn0", "btn1");
Application.RequestStop ();
break;
case 2:
// Tab to btn2
Application.RaiseKeyDownEvent (Key.Tab);
var btn = Application.Navigation!.GetFocused () as Button;
btn.Accepting += (sender, e) => { btnAcceptCount++; };
Application.RaiseKeyDownEvent (Key.Space);
break;
default:
Assert.Fail ();
break;
}
};
Application.Run ().Dispose ();
Assert.Equal (1, result);
Assert.Equal (1, btnAcceptCount);
}
[Theory]
[InlineData (@"", false, false, 6, 6, 2, 2)]
[InlineData (@"", false, true, 3, 6, 9, 3)]
[InlineData (@"01234\n-----\n01234", false, false, 1, 6, 13, 3)]
[InlineData (@"01234\n-----\n01234", true, false, 1, 5, 13, 4)]
[InlineData (@"0123456789", false, false, 1, 6, 12, 3)]
[InlineData (@"0123456789", false, true, 1, 5, 12, 4)]
[InlineData (@"01234567890123456789", false, true, 1, 5, 13, 4)]
[InlineData (@"01234567890123456789", true, true, 1, 5, 13, 5)]
[InlineData (@"01234567890123456789\n01234567890123456789", false, true, 1, 5, 13, 4)]
[InlineData (@"01234567890123456789\n01234567890123456789", true, true, 1, 4, 13, 7)]
[AutoInitShutdown]
public void Location_And_Size_Correct (string message, bool wrapMessage, bool hasButton, int expectedX, int expectedY, int expectedW, int expectedH)
{
int iterations = -1;
((FakeDriver)Application.Driver!).SetBufferSize (15, 15); // 15 x 15 gives us enough room for a button with one char (9x1)
Dialog.DefaultShadow = ShadowStyle.None;
Button.DefaultShadow = ShadowStyle.None;
var mbFrame = Rectangle.Empty;
Application.Iteration += (s, a) =>
{
iterations++;
if (iterations == 0)
{
MessageBox.Query (string.Empty, message, 0, wrapMessage, hasButton ? ["0"] : []);
Application.RequestStop ();
}
else if (iterations == 1)
{
mbFrame = Application.Top!.Frame;
Application.RequestStop ();
}
};
Application.Run ().Dispose ();
Assert.Equal (new (expectedX, expectedY, expectedW, expectedH), mbFrame);
}
[Fact]
[AutoInitShutdown]
public void Message_With_Spaces_WrapMessage_False ()
{
int iterations = -1;
var top = new Toplevel ();
top.BorderStyle = LineStyle.None;
((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
var btn =
$"{Glyphs.LeftBracket}{Glyphs.LeftDefaultIndicator} btn {Glyphs.RightDefaultIndicator}{Glyphs.RightBracket}";
// Override CM
MessageBox.DefaultButtonAlignment = Alignment.End;
MessageBox.DefaultBorderStyle = LineStyle.Double;
Dialog.DefaultShadow = ShadowStyle.None;
Button.DefaultShadow = ShadowStyle.None;
Application.Iteration += (s, a) =>
{
iterations++;
if (iterations == 0)
{
var sb = new StringBuilder ();
for (var i = 0; i < 17; i++)
{
sb.Append ("ff ");
}
MessageBox.Query (string.Empty, sb.ToString (), 0, false, "btn");
Application.RequestStop ();
}
else if (iterations == 1)
{
Application.LayoutAndDraw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
╔════════════════╗
║ ff ff ff ff ff ║
║ ⟦► btn ◄⟧║
╚════════════════╝",
_output
);
Application.RequestStop ();
// Really long text
MessageBox.Query (string.Empty, new ('f', 500), 0, false, "btn");
}
else if (iterations == 2)
{
Application.LayoutAndDraw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
╔════════════════╗
║ffffffffffffffff║
║ ⟦► btn ◄⟧║
╚════════════════╝",
_output
);
Application.RequestStop ();
}
};
Application.Run (top);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Message_With_Spaces_WrapMessage_True ()
{
int iterations = -1;
var top = new Toplevel ();
top.BorderStyle = LineStyle.None;
((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
var btn =
$"{Glyphs.LeftBracket}{Glyphs.LeftDefaultIndicator} btn {Glyphs.RightDefaultIndicator}{Glyphs.RightBracket}";
// Override CM
MessageBox.DefaultButtonAlignment = Alignment.End;
MessageBox.DefaultBorderStyle = LineStyle.Double;
Dialog.DefaultShadow = ShadowStyle.None;
Button.DefaultShadow = ShadowStyle.None;
Application.Iteration += (s, a) =>
{
iterations++;
if (iterations == 0)
{
var sb = new StringBuilder ();
for (var i = 0; i < 17; i++)
{
sb.Append ("ff ");
}
MessageBox.Query (string.Empty, sb.ToString (), 0, true, "btn");
Application.RequestStop ();
}
else if (iterations == 1)
{
Application.LayoutAndDraw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
╔══════════════╗
║ff ff ff ff ff║
║ff ff ff ff ff║
║ff ff ff ff ff║
║ ff ff ║
║ ⟦► btn ◄⟧║
╚══════════════╝",
_output
);
Application.RequestStop ();
// Really long text
MessageBox.Query (string.Empty, new ('f', 500), 0, true, "btn");
}
else if (iterations == 2)
{
Application.LayoutAndDraw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
╔════════════════╗
║ffffffffffffffff║
║ffffffffffffffff║
║ffffffffffffffff║
║ffffffffffffffff║
║ffffffffffffffff║
║ffffffffffffffff║
║fffffff⟦► btn ◄⟧║
╚════════════════╝",
_output
);
Application.RequestStop ();
}
};
Application.Run (top);
top.Dispose ();
}
[Theory]
[InlineData (0, 0, "1")]
[InlineData (1, 1, "1")]
[InlineData (7, 5, "1")]
[InlineData (50, 50, "1")]
[InlineData (0, 0, "message")]
[InlineData (1, 1, "message")]
[InlineData (7, 5, "message")]
[InlineData (50, 50, "message")]
[AutoInitShutdown]
public void Size_Not_Default_Message (int height, int width, string message)
{
int iterations = -1;
((FakeDriver)Application.Driver!).SetBufferSize (100, 100);
Application.Iteration += (s, a) =>
{
iterations++;
if (iterations == 0)
{
MessageBox.Query (height, width, string.Empty, message, null);
Application.RequestStop ();
}
else if (iterations == 1)
{
Application.LayoutAndDraw ();
Assert.IsType<Dialog> (Application.Top);
Assert.Equal (new (height, width), Application.Top.Frame.Size);
Application.RequestStop ();
}
};
}
[Theory]
[InlineData (0, 0, "1")]
[InlineData (1, 1, "1")]
[InlineData (7, 5, "1")]
[InlineData (50, 50, "1")]
[InlineData (0, 0, "message")]
[InlineData (1, 1, "message")]
[InlineData (7, 5, "message")]
[InlineData (50, 50, "message")]
[AutoInitShutdown]
public void Size_Not_Default_Message_Button (int height, int width, string message)
{
int iterations = -1;
((FakeDriver)Application.Driver!).SetBufferSize (100, 100);
Application.Iteration += (s, a) =>
{
iterations++;
if (iterations == 0)
{
MessageBox.Query (height, width, string.Empty, message, "_Ok");
Application.RequestStop ();
}
else if (iterations == 1)
{
Application.LayoutAndDraw ();
Assert.IsType<Dialog> (Application.Top);
Assert.Equal (new (height, width), Application.Top.Frame.Size);
Application.RequestStop ();
}
};
}
[Theory]
[InlineData (0, 0)]
[InlineData (1, 1)]
[InlineData (7, 5)]
[InlineData (50, 50)]
[AutoInitShutdown]
public void Size_Not_Default_No_Message (int height, int width)
{
int iterations = -1;
((FakeDriver)Application.Driver!).SetBufferSize (100, 100);
Application.Iteration += (s, a) =>
{
iterations++;
if (iterations == 0)
{
MessageBox.Query (height, width, string.Empty, string.Empty, null);
Application.RequestStop ();
}
else if (iterations == 1)
{
Application.LayoutAndDraw ();
Assert.IsType<Dialog> (Application.Top);
Assert.Equal (new (height, width), Application.Top.Frame.Size);
Application.RequestStop ();
}
};
}
[Fact]
[AutoInitShutdown]
public void UICatalog_AboutBox ()
{
int iterations = -1;
((FakeDriver)Application.Driver).SetBufferSize (70, 15);
// Override CM
MessageBox.DefaultButtonAlignment = Alignment.End;
MessageBox.DefaultBorderStyle = LineStyle.Double;
Dialog.DefaultShadow = ShadowStyle.None;
Button.DefaultShadow = ShadowStyle.None;
Application.Iteration += (s, a) =>
{
iterations++;
if (iterations == 0)
{
MessageBox.Query (
"",
UICatalog.UICatalogTop.GetAboutBoxMessage (),
wrapMessage: false,
buttons: "_Ok"
);
Application.RequestStop ();
}
else if (iterations == 1)
{
Application.LayoutAndDraw ();
var expectedText = """
UI Catalog: A comprehensive sample library and test app for
_______ _ _ _____ _
|__ __| (_) | | / ____| (_)
| | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _
| |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | |
| | __/ | | | | | | | | | | | (_| | || |__| | |_| | |
|_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_|
v2 - Pre-Alpha
Ok
""";
DriverAssert.AssertDriverContentsAre (expectedText, _output);
Application.RequestStop ();
}
};
var top = new Toplevel ();
top.BorderStyle = LineStyle.Single;
Application.Run (top);
top.Dispose ();
}
[Theory]
[MemberData (nameof (AcceptingKeys))]
public void Button_IsDefault_True_Return_His_Index_On_Accepting (Key key)
{
Application.Init (new FakeDriver ());
Application.Iteration += (_, _) => Assert.True (Application.RaiseKeyDownEvent (key));
int res = MessageBox.Query ("hey", "IsDefault", "Yes", "No");
Assert.Equal (0, res);
Application.Shutdown ();
}
public static IEnumerable<object []> AcceptingKeys ()
{
yield return [Key.Enter];
yield return [Key.Space];
}
}