123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using Terminal.Gui;
- using Xunit;
- using Xunit.Abstractions;
- // Alias Console to MockConsole so we don't accidentally use Console
- using Console = Terminal.Gui.FakeConsole;
- namespace Terminal.Gui.TypeTests {
- public class DimTests {
- readonly ITestOutputHelper output;
- public DimTests (ITestOutputHelper output)
- {
- this.output = output;
- Console.OutputEncoding = System.Text.Encoding.Default;
- // Change current culture
- var culture = CultureInfo.CreateSpecificCulture ("en-US");
- Thread.CurrentThread.CurrentCulture = culture;
- Thread.CurrentThread.CurrentUICulture = culture;
- }
- [Fact]
- public void New_Works ()
- {
- var dim = new Dim ();
- Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
- }
- [Fact]
- public void Sized_SetsValue ()
- {
- var dim = Dim.Sized (0);
- Assert.Equal ("Absolute(0)", dim.ToString ());
- int testVal = 5;
- dim = Dim.Sized (testVal);
- Assert.Equal ($"Absolute({testVal})", dim.ToString ());
- testVal = -1;
- dim = Dim.Sized (testVal);
- Assert.Equal ($"Absolute({testVal})", dim.ToString ());
- }
- [Fact]
- public void Sized_Equals ()
- {
- int n1 = 0;
- int n2 = 0;
- var dim1 = Dim.Sized (n1);
- var dim2 = Dim.Sized (n2);
- Assert.Equal (dim1, dim2);
- n1 = n2 = 1;
- dim1 = Dim.Sized (n1);
- dim2 = Dim.Sized (n2);
- Assert.Equal (dim1, dim2);
- n1 = n2 = -1;
- dim1 = Dim.Sized (n1);
- dim2 = Dim.Sized (n2);
- Assert.Equal (dim1, dim2);
- n1 = 0;
- n2 = 1;
- dim1 = Dim.Sized (n1);
- dim2 = Dim.Sized (n2);
- Assert.NotEqual (dim1, dim2);
- }
- [Fact]
- public void Width_SetsValue ()
- {
- var dim = Dim.Width (null);
- Assert.Throws<NullReferenceException> (() => dim.ToString ());
- var testVal = Rect.Empty;
- testVal = Rect.Empty;
- dim = Dim.Width (new View (testVal));
- Assert.Equal ($"DimView(Width,View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
- testVal = new Rect (1, 2, 3, 4);
- dim = Dim.Width (new View (testVal));
- Assert.Equal ($"DimView(Width,View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
- }
- [Fact]
- public void Width_Equals ()
- {
- var testRect1 = Rect.Empty;
- var view1 = new View (testRect1);
- var testRect2 = Rect.Empty;
- var view2 = new View (testRect2);
- var dim1 = Dim.Width (view1);
- var dim2 = Dim.Width (view1);
- // FIXED: Dim.Width should support Equals() and this should change to Equal.
- Assert.Equal (dim1, dim2);
- dim2 = Dim.Width (view2);
- Assert.NotEqual (dim1, dim2);
- testRect1 = new Rect (0, 1, 2, 3);
- view1 = new View (testRect1);
- testRect2 = new Rect (0, 1, 2, 3);
- dim1 = Dim.Width (view1);
- dim2 = Dim.Width (view1);
- // FIXED: Dim.Width should support Equals() and this should change to Equal.
- Assert.Equal (dim1, dim2);
- Assert.Throws<ArgumentException> (() => new Rect (0, -1, -2, -3));
- testRect1 = new Rect (0, -1, 2, 3);
- view1 = new View (testRect1);
- testRect2 = new Rect (0, -1, 2, 3);
- dim1 = Dim.Width (view1);
- dim2 = Dim.Width (view1);
- // FIXED: Dim.Width should support Equals() and this should change to Equal.
- Assert.Equal (dim1, dim2);
- testRect1 = new Rect (0, -1, 2, 3);
- view1 = new View (testRect1);
- testRect2 = Rect.Empty;
- view2 = new View (testRect2);
- dim1 = Dim.Width (view1);
- dim2 = Dim.Width (view2);
- Assert.NotEqual (dim1, dim2);
- }
- [Fact]
- public void Height_SetsValue ()
- {
- var dim = Dim.Height (null);
- Assert.Throws<NullReferenceException> (() => dim.ToString ());
- var testVal = Rect.Empty;
- testVal = Rect.Empty;
- dim = Dim.Height (new View (testVal));
- Assert.Equal ($"DimView(Height,View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
- testVal = new Rect (1, 2, 3, 4);
- dim = Dim.Height (new View (testVal));
- Assert.Equal ($"DimView(Height,View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
- }
- // TODO: Other Dim.Height tests (e.g. Equal?)
- [Fact]
- public void Fill_SetsValue ()
- {
- var testMargin = 0;
- var dim = Dim.Fill ();
- Assert.Equal ($"Fill({testMargin})", dim.ToString ());
- testMargin = 0;
- dim = Dim.Fill (testMargin);
- Assert.Equal ($"Fill({testMargin})", dim.ToString ());
- testMargin = 5;
- dim = Dim.Fill (testMargin);
- Assert.Equal ($"Fill({testMargin})", dim.ToString ());
- }
- [Fact]
- public void Fill_Equal ()
- {
- var margin1 = 0;
- var margin2 = 0;
- var dim1 = Dim.Fill (margin1);
- var dim2 = Dim.Fill (margin2);
- Assert.Equal (dim1, dim2);
- }
- [Fact]
- public void Percent_SetsValue ()
- {
- float f = 0;
- var dim = Dim.Percent (f);
- Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
- f = 0.5F;
- dim = Dim.Percent (f);
- Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
- f = 100;
- dim = Dim.Percent (f);
- Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
- }
- [Fact]
- public void Percent_Equals ()
- {
- float n1 = 0;
- float n2 = 0;
- var dim1 = Dim.Percent (n1);
- var dim2 = Dim.Percent (n2);
- Assert.Equal (dim1, dim2);
- n1 = n2 = 1;
- dim1 = Dim.Percent (n1);
- dim2 = Dim.Percent (n2);
- Assert.Equal (dim1, dim2);
- n1 = n2 = 0.5f;
- dim1 = Dim.Percent (n1);
- dim2 = Dim.Percent (n2);
- Assert.Equal (dim1, dim2);
- n1 = n2 = 100f;
- dim1 = Dim.Percent (n1);
- dim2 = Dim.Percent (n2);
- Assert.Equal (dim1, dim2);
- n1 = n2 = 0.3f;
- dim1 = Dim.Percent (n1, true);
- dim2 = Dim.Percent (n2, true);
- Assert.Equal (dim1, dim2);
- n1 = n2 = 0.3f;
- dim1 = Dim.Percent (n1);
- dim2 = Dim.Percent (n2, true);
- Assert.NotEqual (dim1, dim2);
- n1 = 0;
- n2 = 1;
- dim1 = Dim.Percent (n1);
- dim2 = Dim.Percent (n2);
- Assert.NotEqual (dim1, dim2);
- n1 = 0.5f;
- n2 = 1.5f;
- dim1 = Dim.Percent (n1);
- dim2 = Dim.Percent (n2);
- Assert.NotEqual (dim1, dim2);
- }
- [Fact]
- public void Percent_ThrowsOnIvalid ()
- {
- var dim = Dim.Percent (0);
- Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
- Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
- Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
- Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
- }
- [Fact]
- public void ForceValidatePosDim_True_Dim_Validation_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type ()
- {
- Application.Init (new FakeDriver ());
- var t = Application.Top;
- var w = new Window ("w") {
- Width = Dim.Fill (0),
- Height = Dim.Sized (10)
- };
- var v = new View ("v") {
- Width = Dim.Width (w) - 2,
- Height = Dim.Percent (10),
- ForceValidatePosDim = true
- };
- w.Add (v);
- t.Add (w);
- t.Ready += () => {
- Assert.Equal (2, w.Width = 2);
- Assert.Equal (2, w.Height = 2);
- Assert.Throws<ArgumentException> (() => v.Width = 2);
- Assert.Throws<ArgumentException> (() => v.Height = 2);
- v.ForceValidatePosDim = false;
- var exception = Record.Exception (() => v.Width = 2);
- Assert.Null (exception);
- Assert.Equal (2, v.Width);
- exception = Record.Exception (() => v.Height = 2);
- Assert.Null (exception);
- Assert.Equal (2, v.Height);
- };
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Application.Shutdown ();
- }
- [Fact]
- public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
- {
- Application.Init (new FakeDriver ());
- var t = Application.Top;
- var w = new Window (new Rect (1, 2, 4, 5), "w");
- t.Add (w);
- t.Ready += () => {
- Assert.Equal (3, w.Width = 3);
- Assert.Equal (4, w.Height = 4);
- };
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Application.Shutdown ();
- }
- [Fact]
- public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
- {
- Application.Init (new FakeDriver ());
- var t = Application.Top;
- var w = new Window ("w") {
- Width = Dim.Fill (0),
- Height = Dim.Sized (10)
- };
- var v = new View ("v") {
- Width = Dim.Width (w) - 2,
- Height = Dim.Percent (10)
- };
- w.Add (v);
- t.Add (w);
- t.Ready += () => {
- v.LayoutStyle = LayoutStyle.Absolute;
- Assert.Equal (2, v.Width = 2);
- Assert.Equal (2, v.Height = 2);
- };
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Application.Shutdown ();
- }
- [Fact]
- public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
- {
- // Testing with the Button because it properly handles the Dim class.
- Application.Init (new FakeDriver ());
- var t = Application.Top;
- var w = new Window ("w") {
- Width = 100,
- Height = 100
- };
- var f1 = new FrameView ("f1") {
- X = 0,
- Y = 0,
- Width = Dim.Percent (50),
- Height = 5
- };
- var f2 = new FrameView ("f2") {
- X = Pos.Right (f1),
- Y = 0,
- Width = Dim.Fill (),
- Height = 5
- };
- var v1 = new Button ("v1") {
- AutoSize = false,
- X = Pos.X (f1) + 2,
- Y = Pos.Bottom (f1) + 2,
- Width = Dim.Width (f1) - 2,
- Height = Dim.Fill () - 2,
- ForceValidatePosDim = true
- };
- var v2 = new Button ("v2") {
- AutoSize = false,
- X = Pos.X (f2) + 2,
- Y = Pos.Bottom (f2) + 2,
- Width = Dim.Width (f2) - 2,
- Height = Dim.Fill () - 2,
- ForceValidatePosDim = true
- };
- var v3 = new Button ("v3") {
- AutoSize = false,
- Width = Dim.Percent (10),
- Height = Dim.Percent (10),
- ForceValidatePosDim = true
- };
- var v4 = new Button ("v4") {
- AutoSize = false,
- Width = Dim.Sized (50),
- Height = Dim.Sized (50),
- ForceValidatePosDim = true
- };
- var v5 = new Button ("v5") {
- AutoSize = false,
- Width = Dim.Width (v1) - Dim.Width (v3),
- Height = Dim.Height (v1) - Dim.Height (v3),
- ForceValidatePosDim = true
- };
- var v6 = new Button ("v6") {
- AutoSize = false,
- X = Pos.X (f2),
- Y = Pos.Bottom (f2) + 2,
- Width = Dim.Percent (20, true),
- Height = Dim.Percent (20, true),
- ForceValidatePosDim = true
- };
- w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
- t.Add (w);
- t.Ready += () => {
- Assert.Equal ("Absolute(100)", w.Width.ToString ());
- Assert.Equal ("Absolute(100)", w.Height.ToString ());
- Assert.Equal (100, w.Frame.Width);
- Assert.Equal (100, w.Frame.Height);
- Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
- Assert.Equal ("Absolute(5)", f1.Height.ToString ());
- Assert.Equal (49, f1.Frame.Width); // 50-1=49
- Assert.Equal (5, f1.Frame.Height);
- Assert.Equal ("Fill(0)", f2.Width.ToString ());
- Assert.Equal ("Absolute(5)", f2.Height.ToString ());
- Assert.Equal (49, f2.Frame.Width); // 50-1=49
- Assert.Equal (5, f2.Frame.Height);
-
- Assert.Equal ("Combine(DimView(Width,FrameView()({X=0,Y=0,Width=49,Height=5}))-Absolute(2))", v1.Width.ToString ());
- Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
- Assert.Equal (47, v1.Frame.Width); // 49-2=47
- Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
- Assert.Equal ("Combine(DimView(Width,FrameView()({X=49,Y=0,Width=49,Height=5}))-Absolute(2))", v2.Width.ToString ());
- Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
- Assert.Equal (47, v2.Frame.Width); // 49-2=47
- Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
- Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
- Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
- Assert.Equal (9, v3.Frame.Width); // 98*10%=9
- Assert.Equal (9, v3.Frame.Height); // 98*10%=9
- Assert.Equal ("Absolute(50)", v4.Width.ToString ());
- Assert.Equal ("Absolute(50)", v4.Height.ToString ());
- Assert.Equal (50, v4.Frame.Width);
- Assert.Equal (50, v4.Frame.Height);
- Assert.Equal ("Combine(DimView(Width,Button()({X=2,Y=7,Width=47,Height=89}))-DimView(Width,Button()({X=0,Y=0,Width=9,Height=9})))", v5.Width.ToString ());
- Assert.Equal ("Combine(DimView(Height,Button()({X=2,Y=7,Width=47,Height=89}))-DimView(Height,Button()({X=0,Y=0,Width=9,Height=9})))", v5.Height.ToString ());
- Assert.Equal (38, v5.Frame.Width); // 47-9=38
- Assert.Equal (80, v5.Frame.Height); // 89-9=80
- Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
- Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
- Assert.Equal (9, v6.Frame.Width); // 47*20%=9
- Assert.Equal (18, v6.Frame.Height); // 89*20%=18
- w.Width = 200;
- w.Height = 200;
- t.LayoutSubviews ();
- Assert.Equal ("Absolute(200)", w.Width.ToString ());
- Assert.Equal ("Absolute(200)", w.Height.ToString ());
- Assert.Equal (200, w.Frame.Width);
- Assert.Equal (200, w.Frame.Height);
- f1.Text = "Frame1";
- Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
- Assert.Equal ("Absolute(5)", f1.Height.ToString ());
- Assert.Equal (49, f1.Frame.Width); // 100*0.5=49
- Assert.Equal (5, f1.Frame.Height);
- f2.Text = "Frame2";
- Assert.Equal ("Fill(0)", f2.Width.ToString ());
- Assert.Equal ("Absolute(5)", f2.Height.ToString ());
- Assert.Equal (49, f2.Frame.Width); // f2.X = Pos.Right(f1), thus 50-1=49
- Assert.Equal (5, f2.Frame.Height);
- v1.Text = "Button1";
- Assert.Equal ("Combine(DimView(Width,FrameView()({X=0,Y=0,Width=49,Height=5}))-Absolute(2))", v1.Width.ToString ());
- Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
- Assert.Equal (47, v1.Frame.Width); // 49-2=47
- Assert.Equal (89, v1.Frame.Height); // 98-2-7=89
- v2.Text = "Button2";
- Assert.Equal ("Combine(DimView(Width,FrameView()({X=49,Y=0,Width=49,Height=5}))-Absolute(2))", v2.Width.ToString ());
- Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
- Assert.Equal (47, v2.Frame.Width); // 49-2=47
- Assert.Equal (89, v2.Frame.Height); // 98-2-7=89
- v3.Text = "Button3";
- Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
- Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
- Assert.Equal (9, v3.Frame.Width); // 98*10%=9 * Percent is related to the super-view if it isn't null otherwise the view width
- Assert.Equal (9, v3.Frame.Height); // 99*10%=9
- v4.Text = "Button4";
- v4.AutoSize = false;
- Assert.Equal ("Absolute(50)", v4.Width.ToString ());
- Assert.Equal ("Absolute(50)", v4.Height.ToString ());
- Assert.Equal (50, v4.Frame.Width);
- Assert.Equal (50, v4.Frame.Height);
- v4.AutoSize = true;
- Assert.Equal ("Absolute(11)", v4.Width.ToString ());
- Assert.Equal ("Absolute(1)", v4.Height.ToString ());
- Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
- Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
- v5.Text = "Button5";
- Assert.Equal ("Combine(DimView(Width,Button()({X=2,Y=7,Width=47,Height=89}))-DimView(Width,Button()({X=0,Y=0,Width=9,Height=9})))", v5.Width.ToString ());
- Assert.Equal ("Combine(DimView(Height,Button()({X=2,Y=7,Width=47,Height=89}))-DimView(Height,Button()({X=0,Y=0,Width=9,Height=9})))", v5.Height.ToString ());
- Assert.Equal (38, v5.Frame.Width); // 47-9=38
- Assert.Equal (80, v5.Frame.Height); // 89-9=80
- v6.Text = "Button6";
- Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
- Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
- Assert.Equal (9, v6.Frame.Width); // 49*20%=9
- Assert.Equal (18, v6.Frame.Height); // 98-7*20=18
- };
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Application.Shutdown ();
- }
- // DONE: Test operators
- [Fact]
- public void DimCombine_Do_Not_Throws ()
- {
- Application.Init (new FakeDriver ());
- var t = Application.Top;
- var w = new Window ("w") {
- Width = Dim.Width (t) - 2,
- Height = Dim.Height (t) - 2
- };
- var f = new FrameView ("f");
- var v1 = new View ("v1") {
- Width = Dim.Width (w) - 2,
- Height = Dim.Height (w) - 2
- };
- var v2 = new View ("v2") {
- Width = Dim.Width (v1) - 2,
- Height = Dim.Height (v1) - 2
- };
- f.Add (v1, v2);
- w.Add (f);
- t.Add (w);
- f.Width = Dim.Width (t) - Dim.Width (v2);
- f.Height = Dim.Height (t) - Dim.Height (v2);
- t.Ready += () => {
- Assert.Equal (80, t.Frame.Width);
- Assert.Equal (25, t.Frame.Height);
- Assert.Equal (78, w.Frame.Width);
- Assert.Equal (23, w.Frame.Height);
- Assert.Equal (6, f.Frame.Width);
- Assert.Equal (6, f.Frame.Height);
- Assert.Equal (76, v1.Frame.Width);
- Assert.Equal (21, v1.Frame.Height);
- Assert.Equal (74, v2.Frame.Width);
- Assert.Equal (19, v2.Frame.Height);
- };
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Application.Shutdown ();
- }
- [Fact]
- public void PosCombine_Will_Throws ()
- {
- Application.Init (new FakeDriver ());
- var t = Application.Top;
- var w = new Window ("w") {
- Width = Dim.Width (t) - 2,
- Height = Dim.Height (t) - 2
- };
- var f = new FrameView ("f");
- var v1 = new View ("v1") {
- Width = Dim.Width (w) - 2,
- Height = Dim.Height (w) - 2
- };
- var v2 = new View ("v2") {
- Width = Dim.Width (v1) - 2,
- Height = Dim.Height (v1) - 2
- };
- f.Add (v1); // v2 not added
- w.Add (f);
- t.Add (w);
- f.Width = Dim.Width (t) - Dim.Width (v2);
- f.Height = Dim.Height (t) - Dim.Height (v2);
- Assert.Throws<InvalidOperationException> (() => Application.Run ());
- Application.Shutdown ();
- }
- [Fact]
- public void Dim_Add_Operator ()
- {
- Application.Init (new FakeDriver ());
- var top = Application.Top;
- var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
- var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
- var count = 0;
- field.KeyDown += (k) => {
- if (k.KeyEvent.Key == Key.Enter) {
- field.Text = $"Label {count}";
- var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
- view.Add (label);
- Assert.Equal ($"Label {count}", label.Text);
- Assert.Equal ($"Absolute({count})", label.Y.ToString ());
- Assert.Equal ($"Absolute({count})", view.Height.ToString ());
- view.Height += 1;
- count++;
- Assert.Equal ($"Absolute({count})", view.Height.ToString ());
- }
- };
- Application.Iteration += () => {
- while (count < 20) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
- Application.RequestStop ();
- };
- var win = new Window ();
- win.Add (view);
- win.Add (field);
- top.Add (win);
- Application.Run (top);
- Assert.Equal (20, count);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- private string [] expecteds = new string [21] {
- @"
- ┌────────────────────┐
- │View with long text │
- │ │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 0 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 1 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 2 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 3 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 4 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 5 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 6 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 7 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 8 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 9 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 10 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 11 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 12 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 13 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 14 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 15 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 16 │
- │Label 16 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 16 │
- │Label 17 │
- │Label 17 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 16 │
- │Label 17 │
- │Label 18 │
- │Label 18 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 16 │
- │Label 17 │
- │Label 18 │
- │Label 19 │
- │Label 19 │
- └────────────────────┘",
- };
- [Fact]
- public void Dim_Add_Operator_With_Text ()
- {
- Application.Init (new FakeDriver ());
- var top = Application.Top;
- // Although view height is zero the text it's draw due the SetMinWidthHeight method
- var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 0 };
- var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
- var count = 0;
- var listLabels = new List<Label> ();
- field.KeyDown += (k) => {
- if (k.KeyEvent.Key == Key.Enter) {
- ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
- Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
- if (count < 20) {
- field.Text = $"Label {count}";
- var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
- view.Add (label);
- Assert.Equal ($"Label {count}", label.Text);
- Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
- listLabels.Add (label);
- if (count == 0) {
- Assert.Equal ($"Absolute({count})", view.Height.ToString ());
- view.Height += 2;
- } else {
- Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
- view.Height += 1;
- }
- count++;
- }
- Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
- }
- };
- Application.Iteration += () => {
- while (count < 21) {
- field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
- if (count == 20) {
- field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
- break;
- }
- }
- Application.RequestStop ();
- };
- var win = new Window ();
- win.Add (view);
- win.Add (field);
- top.Add (win);
- Application.Run (top);
- Assert.Equal (20, count);
- Assert.Equal (count, listLabels.Count);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void Dim_Subtract_Operator ()
- {
- Application.Init (new FakeDriver ());
- var top = Application.Top;
- var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
- var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
- var count = 20;
- var listLabels = new List<Label> ();
- for (int i = 0; i < count; i++) {
- field.Text = $"Label {i}";
- var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
- view.Add (label);
- Assert.Equal ($"Label {i}", label.Text);
- Assert.Equal ($"Absolute({i})", label.Y.ToString ());
- listLabels.Add (label);
- Assert.Equal ($"Absolute({i})", view.Height.ToString ());
- view.Height += 1;
- Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
- }
- field.KeyDown += (k) => {
- if (k.KeyEvent.Key == Key.Enter) {
- Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
- view.Remove (listLabels [count - 1]);
- Assert.Equal ($"Absolute({count})", view.Height.ToString ());
- view.Height -= 1;
- count--;
- Assert.Equal ($"Absolute({count})", view.Height.ToString ());
- }
- };
- Application.Iteration += () => {
- while (count > 0) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
- Application.RequestStop ();
- };
- var win = new Window ();
- win.Add (view);
- win.Add (field);
- top.Add (win);
- Application.Run (top);
- Assert.Equal (0, count);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void Dim_Subtract_Operator_With_Text ()
- {
- Application.Init (new FakeDriver ());
- var top = Application.Top;
- // Although view height is zero the text it's draw due the SetMinWidthHeight method
- var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 0 };
- var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
- var count = 20;
- var listLabels = new List<Label> ();
- for (int i = 0; i < count; i++) {
- field.Text = $"Label {i}";
- var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
- view.Add (label);
- Assert.Equal ($"Label {i}", label.Text);
- Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
- listLabels.Add (label);
- if (i == 0) {
- Assert.Equal ($"Absolute({i})", view.Height.ToString ());
- view.Height += 2;
- Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
- } else {
- Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
- view.Height += 1;
- Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
- }
- }
- field.KeyDown += (k) => {
- if (k.KeyEvent.Key == Key.Enter) {
- ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
- Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
- if (count > 0) {
- Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
- view.Remove (listLabels [count - 1]);
- listLabels.RemoveAt (count - 1);
- Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
- view.Height -= 1;
- count--;
- if (listLabels.Count > 0)
- field.Text = listLabels [count - 1].Text;
- else
- field.Text = NStack.ustring.Empty;
- }
- Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
- }
- };
- Application.Iteration += () => {
- while (count > -1) {
- field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
- if (count == 0) {
- field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
- break;
- }
- }
- Application.RequestStop ();
- };
- var win = new Window ();
- win.Add (view);
- win.Add (field);
- top.Add (win);
- Application.Run (top);
- Assert.Equal (0, count);
- Assert.Equal (count, listLabels.Count);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void Internal_Tests ()
- {
- var dimFactor = new Dim.DimFactor (0.10F);
- Assert.Equal (10, dimFactor.Anchor (100));
- var dimAbsolute = new Dim.DimAbsolute (10);
- Assert.Equal (10, dimAbsolute.Anchor (0));
- var dimFill = new Dim.DimFill (1);
- Assert.Equal (99, dimFill.Anchor (100));
- var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
- Assert.Equal (dimCombine.left, dimFactor);
- Assert.Equal (dimCombine.right, dimAbsolute);
- Assert.Equal (20, dimCombine.Anchor (100));
- var view = new View (new Rect (20, 10, 20, 1));
- var dimViewHeight = new Dim.DimView (view, 0);
- Assert.Equal (1, dimViewHeight.Anchor (0));
- var dimViewWidth = new Dim.DimView (view, 1);
- Assert.Equal (20, dimViewWidth.Anchor (0));
- }
- [Fact]
- public void Function_SetsValue ()
- {
- var text = "Test";
- var dim = Dim.Function (() => text.Length);
- Assert.Equal ("DimFunc(4)", dim.ToString ());
- text = "New Test";
- Assert.Equal ("DimFunc(8)", dim.ToString ());
- text = "";
- Assert.Equal ("DimFunc(0)", dim.ToString ());
- }
- [Fact]
- public void Function_Equal ()
- {
- var f1 = () => 0;
- var f2 = () => 0;
- var dim1 = Dim.Function (f1);
- var dim2 = Dim.Function (f2);
- Assert.Equal (dim1, dim2);
- f2 = () => 1;
- dim2 = Dim.Function (f2);
- Assert.NotEqual (dim1, dim2);
- }
- [Theory, AutoInitShutdown]
- [InlineData (0, true)]
- [InlineData (0, false)]
- [InlineData (50, true)]
- [InlineData (50, false)]
- public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
- {
- var container = new View {
- Width = 100,
- Height = 100,
- };
- var label = new Label {
- X = testHorizontal ? startingDistance : 0,
- Y = testHorizontal ? 0 : startingDistance,
- Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
- Height = testHorizontal ? 1 : Dim.Percent (50) + 1,
- };
- container.Add (label);
- Application.Top.Add (container);
- Application.Top.LayoutSubviews ();
- Assert.Equal (100, container.Frame.Width);
- Assert.Equal (100, container.Frame.Height);
- if (testHorizontal) {
- Assert.Equal (51, label.Frame.Width);
- Assert.Equal (1, label.Frame.Height);
- } else {
- Assert.Equal (1, label.Frame.Width);
- Assert.Equal (51, label.Frame.Height);
- }
- }
- }
- }
|