12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading.Tasks;
- using Terminal.Gui;
- using Xunit;
- using System.Globalization;
- using Xunit.Abstractions;
- using NStack;
- using static Terminal.Gui.Application;
- namespace Terminal.Gui.DialogTests {
- public class DialogTests {
- readonly ITestOutputHelper output;
- public DialogTests (ITestOutputHelper output)
- {
- this.output = output;
- }
- //[Fact]
- //[AutoInitShutdown]
- //public void Default_Has_Border ()
- //{
- // var d = (FakeDriver)Application.Driver;
- // d.SetBufferSize (20, 5);
- // Application.RunState runstate = null;
- // var title = "Title";
- // var btnText = "ok";
- // var buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
- // var width = buttonRow.Length;
- // var topRow = $"┌┤{title} {new string (d.HLine.ToString () [0], width - title.Length - 2)}├┐";
- // var bottomRow = $"└{new string (d.HLine.ToString () [0], width - 2)}┘";
- // var dlg = new Dialog (title, new Button (btnText));
- // Application.Begin (dlg);
- // TestHelpers.AssertDriverContentsWithFrameAre ($"{topRow}\n{buttonRow}\n{bottomRow}", output);
- // Application.End (runstate);
- //}
- private (Application.RunState, Dialog) RunButtonTestDialog (string title, int width, Dialog.ButtonAlignments align, params Button [] btns)
- {
- var dlg = new Dialog (btns) {
- Title = title,
- X = 0,
- Y = 0,
- Width = width,
- Height = 1,
- ButtonAlignment = align,
- };
- // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
- dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
- return (Application.Begin (dlg), dlg);
- }
- [Fact]
- [AutoInitShutdown]
- public void Size_Default ()
- {
- var d = new Dialog () {
- };
- Application.Begin (d);
- ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
- // Default size is Percent(85)
- Assert.Equal (new Size ((int)(100 * .85), (int)(100 * .85)), d.Frame.Size);
- }
- [Fact]
- [AutoInitShutdown]
- public void Location_Default ()
- {
- var d = new Dialog () {
- };
- Application.Begin (d);
- ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
- // Default location is centered, so 100 / 2 - 85 / 2 = 7
- var expected = 7;
- Assert.Equal (new Point (expected, expected), d.Frame.Location);
- }
- [Fact]
- [AutoInitShutdown]
- public void Size_Not_Default ()
- {
- var d = new Dialog () {
- Width = 50,
- Height = 50,
- };
- Application.Begin (d);
- ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
- // Default size is Percent(85)
- Assert.Equal (new Size (50, 50), d.Frame.Size);
- }
- [Fact]
- [AutoInitShutdown]
- public void Location_Not_Default ()
- {
- var d = new Dialog () {
- X = 1,
- Y = 1,
- };
- Application.Begin (d);
- ((FakeDriver)Application.Driver).SetBufferSize (100, 100);
- // Default location is centered, so 100 / 2 - 85 / 2 = 7
- var expected = 1;
- Assert.Equal (new Point (expected, expected), d.Frame.Location);
- }
- [Fact]
- [AutoInitShutdown]
- public void Location_When_Application_Top_Not_Default ()
- {
- var expected = 5;
- var d = new Dialog () {
- X = expected,
- Y = expected,
- Height = 5,
- Width = 5
- };
- Application.Begin (d);
- ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
- // Default location is centered, so 100 / 2 - 85 / 2 = 7
- Assert.Equal (new Point (expected, expected), d.Frame.Location);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌───┐
- │ │
- │ │
- │ │
- └───┘", output);
- }
- [Fact]
- [AutoInitShutdown]
- public void Location_When_Not_Application_Top_Not_Default ()
- {
- Application.Top.BorderStyle = LineStyle.Double;
- var iterations = -1;
- Application.Iteration += () => {
- iterations++;
- if (iterations == 0) {
- var d = new Dialog () {
- X = 5,
- Y = 5,
- Height = 3,
- Width = 5
- };
- Application.Begin (d);
- Assert.Equal (new Point (5, 5), d.Frame.Location);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╔══════════════════╗
- ║ ║
- ║ ║
- ║ ║
- ║ ║
- ║ ┌───┐ ║
- ║ │ │ ║
- ║ └───┘ ║
- ║ ║
- ╚══════════════════╝", output);
- d = new Dialog () {
- X = 5,
- Y = 5,
- };
- Application.Begin (d);
- // This is because of PostionTopLevels and EnsureVisibleBounds
- Assert.Equal (new Point (3, 2), d.Frame.Location);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╔══════════════════╗
- ║ ║
- ║ ┌───────────────┐
- ║ │ │
- ║ │ │
- ║ │ │
- ║ │ │
- ║ │ │
- ║ │ │
- ╚══└───────────────┘", output);
- } else if (iterations > 0) {
- Application.RequestStop ();
- }
- };
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
- Application.Run ();
- }
- [Fact]
- [AutoInitShutdown]
- public void ButtonAlignment_One ()
- {
- var d = (FakeDriver)Application.Driver;
- Application.RunState runstate = null;
- var title = "1234";
- // E.g "|[ ok ]|"
- var btnText = "ok";
- var buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
- var width = buttonRow.Length;
- d.SetBufferSize (width, 1);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
- // Center
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btnText));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Right
- buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btnText));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Left
- buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btnText));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Wider
- buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
- width = buttonRow.Length;
- d.SetBufferSize (width, 1);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btnText));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Right
- buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btnText));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Left
- buttonRow = $"{d.VLine}{d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btnText));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void ButtonAlignment_Two ()
- {
- Application.RunState runstate = null;
- var d = (FakeDriver)Application.Driver;
- var title = "1234";
- // E.g "|[ yes ][ no ]|"
- var btn1Text = "yes";
- var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
- var btn2Text = "no";
- var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
- var buttonRow = $@"{d.VLine} {btn1} {btn2} {d.VLine}";
- var width = buttonRow.Length;
- d.SetBufferSize (buttonRow.Length, 3);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- buttonRow = $@"{d.VLine}{btn1} {btn2}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Right
- buttonRow = $@"{d.VLine} {btn1} {btn2}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Left
- buttonRow = $@"{d.VLine}{btn1} {btn2} {d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void ButtonAlignment_Two_Hidden ()
- {
- Application.RunState runstate = null;
- bool firstIteration = false;
- var d = (FakeDriver)Application.Driver;
- var title = "1234";
- // E.g "|[ yes ][ no ]|"
- var btn1Text = "yes";
- var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
- var btn2Text = "no";
- var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
- var buttonRow = $@"{d.VLine} {btn1} {btn2} {d.VLine}";
- var width = buttonRow.Length;
- d.SetBufferSize (buttonRow.Length, 3);
- Dialog dlg = null;
- Button button1, button2;
- // Default (Center)
- button1 = new Button (btn1Text);
- button2 = new Button (btn2Text);
- (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, button1, button2);
- button1.Visible = false;
- Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
- buttonRow = $@"{d.VLine} {btn2} {d.VLine}";
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- Assert.Equal (width, buttonRow.Length);
- button1 = new Button (btn1Text);
- button2 = new Button (btn2Text);
- (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, button1, button2);
- button1.Visible = false;
- Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
- buttonRow = $@"{d.VLine} {btn2}{d.VLine}";
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Right
- Assert.Equal (width, buttonRow.Length);
- button1 = new Button (btn1Text);
- button2 = new Button (btn2Text);
- (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, button1, button2);
- button1.Visible = false;
- Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Left
- Assert.Equal (width, buttonRow.Length);
- button1 = new Button (btn1Text);
- button2 = new Button (btn2Text);
- (runstate, dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, button1, button2);
- button1.Visible = false;
- Application.RunMainLoopIteration (ref runstate, true, ref firstIteration);
- buttonRow = $@"{d.VLine} {btn2} {d.VLine}";
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void ButtonAlignment_Three ()
- {
- Application.RunState runstate = null;
- var d = (FakeDriver)Application.Driver;
- var title = "1234";
- // E.g "|[ yes ][ no ][ maybe ]|"
- var btn1Text = "yes";
- var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
- var btn2Text = "no";
- var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
- var btn3Text = "maybe";
- var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
- var buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3} {d.VLine}";
- var width = buttonRow.Length;
- d.SetBufferSize (buttonRow.Length, 3);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Right
- buttonRow = $@"{d.VLine} {btn1} {btn2} {btn3}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Left
- buttonRow = $@"{d.VLine}{btn1} {btn2} {btn3} {d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void ButtonAlignment_Four ()
- {
- Application.RunState runstate = null;
- var d = (FakeDriver)Application.Driver;
- var title = "1234";
- // E.g "|[ yes ][ no ][ maybe ]|"
- var btn1Text = "yes";
- var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
- var btn2Text = "no";
- var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
- var btn3Text = "maybe";
- var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
- var btn4Text = "never";
- var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
- var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
- var width = buttonRow.Length;
- d.SetBufferSize (buttonRow.Length, 3);
- // Default - Center
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Right
- buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Left
- buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void ButtonAlignment_Four_On_Too_Small_Width ()
- {
- Application.RunState runstate = null;
- var d = (FakeDriver)Application.Driver;
- var title = "1234";
- // E.g "|[ yes ][ no ][ maybe ][ never ]|"
- var btn1Text = "yes";
- var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
- var btn2Text = "no";
- var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
- var btn3Text = "maybe";
- var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
- var btn4Text = "never";
- var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
- var buttonRow = string.Empty;
- var width = 30;
- d.SetBufferSize (width, 1);
- // Default - Center
- buttonRow = $"{d.VLine}es ] {btn2} {btn3} [ neve{d.VLine}";
- (runstate, var dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- Assert.Equal (new Size (width, 1), dlg.Frame.Size);
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- buttonRow = $"{d.VLine}[ yes [ no [ maybe [ never ]{d.VLine}";
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
- // Right
- buttonRow = $"{d.VLine}] {btn2} {btn3} {btn4}{d.VLine}";
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
- // Left
- buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} [ n{d.VLine}";
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output); Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void ButtonAlignment_Four_Wider ()
- {
- Application.RunState runstate = null;
- var d = (FakeDriver)Application.Driver;
- var title = "1234";
- // E.g "|[ yes ][ no ][ maybe ]|"
- var btn1Text = "yes";
- var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
- var btn2Text = "no";
- var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
- var btn3Text = "你你你你你"; // This is a wide char
- var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
- // Requires a Nerd Font
- var btn4Text = "\uE36E\uE36F\uE370\uE371\uE372\uE373";
- var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
- // Note extra spaces to make dialog even wider
- // 123456 123456
- var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
- var width = ustring.Make (buttonRow).ConsoleWidth;
- d.SetBufferSize (width, 3);
- // Default - Center
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
- Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Right
- buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
- Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Left
- buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
- Assert.Equal (width, ustring.Make (buttonRow).ConsoleWidth);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void ButtonAlignment_Four_WideOdd ()
- {
- Application.RunState runstate = null;
- var d = (FakeDriver)Application.Driver;
- var title = "1234";
- // E.g "|[ yes ][ no ][ maybe ]|"
- var btn1Text = "really long button 1";
- var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
- var btn2Text = "really long button 2";
- var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
- var btn3Text = "really long button 3";
- var btn3 = $"{d.LeftBracket} {btn3Text} {d.RightBracket}";
- var btn4Text = "really long button 44"; // 44 is intentional to make length different than rest
- var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
- // Note extra spaces to make dialog even wider
- // 123456 1234567
- var buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4} {d.VLine}";
- var width = buttonRow.Length;
- d.SetBufferSize (buttonRow.Length, 1);
- // Default - Center
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Justify, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Right
- buttonRow = $"{d.VLine} {btn1} {btn2} {btn3} {btn4}{d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Right, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Left
- buttonRow = $"{d.VLine}{btn1} {btn2} {btn3} {btn4} {d.VLine}";
- Assert.Equal (width, buttonRow.Length);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Left, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void Zero_Buttons_Works ()
- {
- Application.RunState runstate = null;
- var d = (FakeDriver)Application.Driver;
- var title = "1234";
- var buttonRow = $"{d.VLine} {d.VLine}";
- var width = buttonRow.Length;
- d.SetBufferSize (buttonRow.Length, 3);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, null);
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void One_Button_Works ()
- {
- Application.RunState runstate = null;
- var d = (FakeDriver)Application.Driver;
- var title = "";
- var btnText = "ok";
- var buttonRow = $"{d.VLine} {d.LeftBracket} {btnText} {d.RightBracket} {d.VLine}";
- var width = buttonRow.Length;
- d.SetBufferSize (buttonRow.Length, 10);
- (runstate, var _) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btnText));
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void Add_Button_Works ()
- {
- Application.RunState runstate = null;
- var d = (FakeDriver)Application.Driver;
- var title = "1234";
- var btn1Text = "yes";
- var btn1 = $"{d.LeftBracket} {btn1Text} {d.RightBracket}";
- var btn2Text = "no";
- var btn2 = $"{d.LeftBracket} {btn2Text} {d.RightBracket}";
- // We test with one button first, but do this to get the width right for 2
- var width = $@"{d.VLine} {btn1} {btn2} {d.VLine}".Length;
- d.SetBufferSize (width, 1);
- // Default (center)
- var dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Center };
- // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
- dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
- runstate = Application.Begin (dlg);
- var buttonRow = $"{d.VLine} {btn1} {d.VLine}";
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- // Now add a second button
- buttonRow = $"{d.VLine} {btn1} {btn2} {d.VLine}";
- dlg.AddButton (new Button (btn2Text));
- bool first = false;
- Application.RunMainLoopIteration (ref runstate, true, ref first);
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Justify
- dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Justify };
- // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
- dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
- runstate = Application.Begin (dlg);
- buttonRow = $"{d.VLine} {btn1}{d.VLine}";
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- // Now add a second button
- buttonRow = $"{d.VLine}{btn1} {btn2}{d.VLine}";
- dlg.AddButton (new Button (btn2Text));
- first = false;
- Application.RunMainLoopIteration (ref runstate, true, ref first);
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Right
- dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Right };
- // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
- dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
- runstate = Application.Begin (dlg);
- buttonRow = $"{d.VLine}{new string (' ', width - btn1.Length - 2)}{btn1}{d.VLine}";
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- // Now add a second button
- buttonRow = $"{d.VLine} {btn1} {btn2}{d.VLine}";
- dlg.AddButton (new Button (btn2Text));
- first = false;
- Application.RunMainLoopIteration (ref runstate, true, ref first);
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- // Left
- dlg = new Dialog (new Button (btn1Text)) { Title = title, Width = width, Height = 1, ButtonAlignment = Dialog.ButtonAlignments.Left };
- // Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
- dlg.Border.Thickness = new Thickness (1, 0, 1, 0);
- runstate = Application.Begin (dlg);
- buttonRow = $"{d.VLine}{btn1}{new string (' ', width - btn1.Length - 2)}{d.VLine}";
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- // Now add a second button
- buttonRow = $"{d.VLine}{btn1} {btn2} {d.VLine}";
- dlg.AddButton (new Button (btn2Text));
- first = false;
- Application.RunMainLoopIteration (ref runstate, true, ref first);
- TestHelpers.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
- Application.End (runstate);
- }
- [Fact]
- [AutoInitShutdown]
- public void FileDialog_FileSystemWatcher ()
- {
- for (int i = 0; i < 8; i++) {
- var fd = new FileDialog ();
- fd.Ready += (s, e) => Application.RequestStop ();
- Application.Run (fd);
- }
- }
- [Fact, AutoInitShutdown]
- public void Dialog_Opened_From_Another_Dialog ()
- {
- var btn1 = new Button ("press me 1");
- Button btn2 = null;
- Button btn3 = null;
- string expected = null;
- btn1.Clicked += (s, e) => {
- btn2 = new Button ("Show Sub");
- btn3 = new Button ("Close");
- btn3.Clicked += (s, e) => Application.RequestStop ();
- btn2.Clicked += (s, e) => { MessageBox.Query (string.Empty, "ya", "ok"); };
- var dlg = new Dialog (btn2, btn3);
- Application.Run (dlg);
- };
- var iterations = -1;
- Application.Iteration += () => {
- iterations++;
- if (iterations == 0) {
- Assert.True (btn1.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
- } else if (iterations == 1) {
- expected = @"
- ┌──────────────────────────────────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ [ Show Sub ] [ Close ] │
- └──────────────────────────────────────────────────────────────────┘";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.True (btn2.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
- } else if (iterations == 2) {
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────────────────────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ ┌──────────────────────────────────────────────┐ │
- │ │ ya │ │
- │ │ │ │
- │ │ [◦ ok ◦] │ │
- │ └──────────────────────────────────────────────┘ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ [ Show Sub ] [ Close ] │
- └──────────────────────────────────────────────────────────────────┘", output);
- Assert.True (Application.Current.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
- } else if (iterations == 3) {
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.True (btn3.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
- } else if (iterations == 4) {
- TestHelpers.AssertDriverContentsWithFrameAre ("", output);
- Application.RequestStop ();
- }
- };
- Application.Run ();
- Application.Shutdown ();
- Assert.Equal (4, iterations);
- }
- [Fact, AutoInitShutdown]
- public void Dialog_In_Window_With_Size_One_Button_Aligns ()
- {
- ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
- var win = new Window ();
- int iterations = 0;
- Application.Iteration += () => {
- if (++iterations > 2) {
- Application.RequestStop ();
- }
- };
- win.Loaded += (s, a) => {
- var dlg = new Dialog (new Button ("Ok")) { Width = 18, Height = 3 };
- dlg.Loaded += (s, a) => {
- Application.Refresh ();
- var expected = @"
- ┌──────────────────┐
- │┌────────────────┐│
- ││ [ Ok ] ││
- │└────────────────┘│
- └──────────────────┘";
- _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- };
- Application.Run (dlg);
- };
- Application.Run (win);
- }
- // [Theory, AutoInitShutdown]
- // [InlineData (5)]
- // //[InlineData (6)]
- // //[InlineData (7)]
- // //[InlineData (8)]
- // //[InlineData (9)]
- // public void Dialog_In_Window_Without_Size_One_Button_Aligns (int height)
- // {
- // ((FakeDriver)Application.Driver).SetBufferSize (20, height);
- // var win = new Window ();
- // Application.Iteration += () => {
- // var dlg = new Dialog ("Test", new Button ("Ok"));
- // dlg.LayoutComplete += (s, a) => {
- // Application.Refresh ();
- // // BUGBUG: This seems wrong; is it a bug in Dim.Percent(85)??
- // var expected = @"
- //┌┌┤Test├─────────┐─┐
- //││ │ │
- //││ [ Ok ] │ │
- //│└───────────────┘ │
- //└──────────────────┘";
- // _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- // dlg.RequestStop ();
- // win.RequestStop ();
- // };
- // Application.Run (dlg);
- // };
- // Application.Run (win);
- // Application.Shutdown ();
- // }
- [Fact, AutoInitShutdown]
- public void Dialog_In_Window_With_TexxtField_And_Button_AnchorEnd ()
- {
- ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
- var win = new Window ();
- int iterations = 0;
- Application.Iteration += () => {
- if (++iterations > 2) {
- Application.RequestStop ();
- }
- };
- win.Loaded += (s, a) => {
- var dlg = new Dialog () { Width = 18, Height = 3 };
- Button btn = null;
- btn = new Button ("Ok") {
- X = Pos.AnchorEnd () - Pos.Function (Btn_Width)
- };
- int Btn_Width ()
- {
- return (btn?.Bounds.Width) ?? 0;
- }
- var tf = new TextField ("01234567890123456789") {
- Width = Dim.Fill (1) - Dim.Function (Btn_Width)
- };
- dlg.Loaded += (s, a) => {
- Application.Refresh ();
- Assert.Equal (new Rect (10, 0, 6, 1), btn.Frame);
- Assert.Equal (new Rect (0, 0, 6, 1), btn.Bounds);
- var expected = @"
- ┌──────────────────┐
- │┌────────────────┐│
- ││23456789 [ Ok ]││
- │└────────────────┘│
- └──────────────────┘";
- _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- dlg.SetNeedsLayout ();
- dlg.LayoutSubviews ();
- Application.Refresh ();
- Assert.Equal (new Rect (10, 0, 6, 1), btn.Frame);
- Assert.Equal (new Rect (0, 0, 6, 1), btn.Bounds);
- expected = @"
- ┌──────────────────┐
- │┌────────────────┐│
- ││23456789 [ Ok ]││
- │└────────────────┘│
- └──────────────────┘";
- _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- };
- dlg.Add (btn, tf);
- Application.Run (dlg);
- };
- Application.Run (win);
- }
- }
- }
|