123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- using System.Text;
- using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
- 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 ()
- {
- int result = -1;
- 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:
- // Tab to btn2
- Application.OnKeyDown (Key.Tab);
- Application.OnKeyDown (Key.Enter);
- break;
- default:
- Assert.Fail ();
- break;
- }
- };
- Application.Run ().Dispose ();
- Assert.Equal (1, result);
- }
- [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.OnKeyDown (Key.Esc);
- break;
- default:
- Assert.Fail ();
- break;
- }
- };
- Application.Run ().Dispose ();
- Assert.Equal (-1, result);
- }
- [Fact]
- [AutoInitShutdown]
- public void KeyBindings_Space_Causes_Focused_Button_Click ()
- {
- int result = -1;
- 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:
- // Tab to btn2
- Application.OnKeyDown (Key.Tab);
- Application.OnKeyDown (Key.Space);
- break;
- default:
- Assert.Fail ();
- break;
- }
- };
- Application.Run ().Dispose ();
- Assert.Equal (1, result);
- }
- [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)
- Rectangle 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.Current.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 =
- $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} btn {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}";
- 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.Refresh ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╔════════════════╗
- ║ ff ff ff ff ff ║
- ║ ⟦► btn ◄⟧║
- ╚════════════════╝",
- _output
- );
- Application.RequestStop ();
- // Really long text
- MessageBox.Query (string.Empty, new string ('f', 500), 0, false, "btn");
- }
- else if (iterations == 2)
- {
- Application.Refresh ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╔════════════════╗
- ║ffffffffffffffff║
- ║ ⟦► btn ◄⟧║
- ╚════════════════╝",
- _output
- );
- Application.RequestStop ();
- }
- };
- Application.Run (top);
- }
- [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 =
- $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} btn {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}";
- 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.Refresh ();
- TestHelpers.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 string ('f', 500), 0, true, "btn");
- }
- else if (iterations == 2)
- {
- Application.Refresh ();
- TestHelpers.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.Refresh ();
- Assert.IsType<Dialog> (Application.Current);
- Assert.Equal (new (height, width), Application.Current.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.Refresh ();
- Assert.IsType<Dialog> (Application.Current);
- Assert.Equal (new (height, width), Application.Current.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.Refresh ();
- Assert.IsType<Dialog> (Application.Current);
- Assert.Equal (new (height, width), Application.Current.Frame.Size);
- Application.RequestStop ();
- }
- };
- }
- [Fact]
- [AutoInitShutdown]
- public void UICatalog_AboutBox ()
- {
- int iterations = -1;
- ((FakeDriver)Application.Driver).SetBufferSize (70, 15);
- Application.Iteration += (s, a) =>
- {
- iterations++;
- if (iterations == 0)
- {
- MessageBox.Query (
- title: "",
- message: UICatalog.UICatalogApp.GetAboutBoxMessage (),
- wrapMessage: false,
- buttons: "_Ok"
- );
- Application.RequestStop ();
- }
- else if (iterations == 1)
- {
- Application.Refresh ();
- string expectedText = """
- ┌────────────────────────────────────────────────────────────────────┐
- │ ╔══════════════════════════════════════════════════════════╗ │
- │ ║ UI Catalog: A comprehensive sample library for ║ │
- │ ║ ║ │
- │ ║ _______ _ _ _____ _ ║ │
- │ ║|__ __| (_) | | / ____| (_)║ │
- │ ║ | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _ ║ │
- │ ║ | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | |║ │
- │ ║ | | __/ | | | | | | | | | | | (_| | || |__| | |_| | |║ │
- │ ║ |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_|║ │
- │ ║ ║ │
- │ ║ v2 - Pre-Alpha ║ │
- │ ║ ⟦► Ok ◄⟧║ │
- │ ╚══════════════════════════════════════════════════════════╝ │
- └────────────────────────────────────────────────────────────────────┘
- """;
- TestHelpers.AssertDriverContentsAre (expectedText, _output);
- Application.RequestStop ();
- }
- };
- var top = new Toplevel ();
- top.BorderStyle = LineStyle.Single;
- Application.Run (top);
- }
- }
|