12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.IO;
- using System.Linq;
- 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 PosTests {
- readonly ITestOutputHelper output;
- public PosTests (ITestOutputHelper output)
- {
- this.output = output;
- }
- [Fact]
- public void New_Works ()
- {
- var pos = new Pos ();
- Assert.Equal ("Terminal.Gui.Pos", pos.ToString ());
- }
- [Fact]
- public void AnchorEnd_SetsValue ()
- {
- var n = 0;
- var pos = Pos.AnchorEnd ();
- Assert.Equal ($"Pos.AnchorEnd(margin={n})", pos.ToString ());
- n = 5;
- pos = Pos.AnchorEnd (n);
- Assert.Equal ($"Pos.AnchorEnd(margin={n})", pos.ToString ());
- }
- [Fact]
- public void AnchorEnd_Equal ()
- {
- var n1 = 0;
- var n2 = 0;
- var pos1 = Pos.AnchorEnd (n1);
- var pos2 = Pos.AnchorEnd (n2);
- Assert.Equal (pos1, pos2);
- // Test inequality
- n2 = 5;
- pos2 = Pos.AnchorEnd (n2);
- Assert.NotEqual (pos1, pos2);
- }
- [Fact]
- [AutoInitShutdown]
- public void AnchorEnd_Equal_Inside_Window ()
- {
- var viewWidth = 10;
- var viewHeight = 1;
- var tv = new TextView () {
- X = Pos.AnchorEnd (viewWidth),
- Y = Pos.AnchorEnd (viewHeight),
- Width = viewWidth,
- Height = viewHeight
- };
- var win = new Window ();
- win.Add (tv);
- var top = Application.Top;
- top.Add (win);
- Application.Begin (top);
- Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
- Assert.Equal (new Rect (0, 0, 80, 25), win.Frame);
- Assert.Equal (new Rect (1, 1, 78, 23), win.Subviews [0].Frame);
- Assert.Equal ("ContentView()({X=1,Y=1,Width=78,Height=23})", win.Subviews [0].ToString ());
- Assert.Equal (new Rect (1, 1, 79, 24), new Rect (
- win.Subviews [0].Frame.Left, win.Subviews [0].Frame.Top,
- win.Subviews [0].Frame.Right, win.Subviews [0].Frame.Bottom));
- Assert.Equal (new Rect (68, 22, 10, 1), tv.Frame);
- }
- [Fact]
- [AutoInitShutdown]
- public void AnchorEnd_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
- {
- var viewWidth = 10;
- var viewHeight = 1;
- var tv = new TextView () {
- X = Pos.AnchorEnd (viewWidth),
- Y = Pos.AnchorEnd (viewHeight),
- Width = viewWidth,
- Height = viewHeight
- };
- var win = new Window ();
- win.Add (tv);
- var menu = new MenuBar ();
- var status = new StatusBar ();
- var top = Application.Top;
- top.Add (win, menu, status);
- Application.Begin (top);
- Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
- Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
- Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
- Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
- Assert.Equal (new Rect (1, 1, 78, 21), win.Subviews [0].Frame);
- Assert.Equal (new Rect (1, 1, 79, 22), new Rect (
- win.Subviews [0].Frame.Left, win.Subviews [0].Frame.Top,
- win.Subviews [0].Frame.Right, win.Subviews [0].Frame.Bottom));
- Assert.Equal (new Rect (68, 20, 10, 1), tv.Frame);
- }
- [Fact]
- [AutoInitShutdown]
- public void Bottom_Equal_Inside_Window ()
- {
- var win = new Window ();
- var label = new Label ("This should be the last line.") {
- TextAlignment = Terminal.Gui.TextAlignment.Centered,
- ColorScheme = Colors.Menu,
- Width = Dim.Fill (),
- X = Pos.Center (),
- Y = Pos.Bottom (win) - 3 // two lines top and bottom borders more one line above the bottom border
- };
- win.Add (label);
- var top = Application.Top;
- top.Add (win);
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
- Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
- Assert.Equal (new Rect (1, 1, 38, 8), win.Subviews [0].Frame);
- Assert.Equal ("ContentView()({X=1,Y=1,Width=38,Height=8})", win.Subviews [0].ToString ());
- Assert.Equal (new Rect (0, 0, 40, 10), new Rect (
- win.Frame.Left, win.Frame.Top,
- win.Frame.Right, win.Frame.Bottom));
- Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
- var expected = @"
- ┌──────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ This should be the last line. │
- └──────────────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact]
- [AutoInitShutdown]
- public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
- {
- var win = new Window ();
- var label = new Label ("This should be the last line.") {
- TextAlignment = Terminal.Gui.TextAlignment.Centered,
- ColorScheme = Colors.Menu,
- Width = Dim.Fill (),
- X = Pos.Center (),
- Y = Pos.AnchorEnd (1)
- };
- win.Add (label);
- var top = Application.Top;
- top.Add (win);
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
- Assert.True (label.AutoSize);
- Assert.Equal (29, label.Text.Length);
- Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
- Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
- Assert.Equal (new Rect (1, 1, 38, 8), win.Subviews [0].Frame);
- Assert.Equal ("ContentView()({X=1,Y=1,Width=38,Height=8})", win.Subviews [0].ToString ());
- Assert.Equal (new Rect (0, 0, 40, 10), new Rect (
- win.Frame.Left, win.Frame.Top,
- win.Frame.Right, win.Frame.Bottom));
- Assert.Equal (new Rect (0, 7, 38, 1), label.Frame);
- var expected = @"
- ┌──────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ This should be the last line. │
- └──────────────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact]
- [AutoInitShutdown]
- public void Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
- {
- var win = new Window ();
- var label = new Label ("This should be the last line.") {
- TextAlignment = Terminal.Gui.TextAlignment.Centered,
- ColorScheme = Colors.Menu,
- Width = Dim.Fill (),
- X = Pos.Center (),
- Y = Pos.Bottom (win) - 4 // two lines top and bottom borders more two lines above border
- };
- win.Add (label);
- var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
- var status = new StatusBar (new StatusItem [] { new (Key.F1, "~F1~ Help", null) });
- var top = Application.Top;
- top.Add (win, menu, status);
- Application.Begin (top);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
- Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
- Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
- Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
- Assert.Equal (new Rect (1, 1, 78, 21), win.Subviews [0].Frame);
- Assert.Equal (new Rect (0, 1, 80, 24), new Rect (
- win.Frame.Left, win.Frame.Top,
- win.Frame.Right, win.Frame.Bottom));
- Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
- var expected = @"
- Menu
- ┌──────────────────────────────────────────────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ This should be the last line. │
- └──────────────────────────────────────────────────────────────────────────────┘
- F1 Help
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact]
- [AutoInitShutdown]
- public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
- {
- var win = new Window ();
- var label = new Label ("This should be the last line.") {
- TextAlignment = Terminal.Gui.TextAlignment.Centered,
- ColorScheme = Colors.Menu,
- Width = Dim.Fill (),
- X = Pos.Center (),
- Y = Pos.AnchorEnd (1)
- };
- win.Add (label);
- var menu = new MenuBar (new MenuBarItem [] { new ("Menu", "", null) });
- var status = new StatusBar (new StatusItem [] { new (Key.F1, "~F1~ Help", null) });
- var top = Application.Top;
- top.Add (win, menu, status);
- Application.Begin (top);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
- Assert.Equal (new Rect (0, 0, 80, 1), menu.Frame);
- Assert.Equal (new Rect (0, 24, 80, 1), status.Frame);
- Assert.Equal (new Rect (0, 1, 80, 23), win.Frame);
- Assert.Equal (new Rect (1, 1, 78, 21), win.Subviews [0].Frame);
- Assert.Equal (new Rect (0, 1, 80, 24), new Rect (
- win.Frame.Left, win.Frame.Top,
- win.Frame.Right, win.Frame.Bottom));
- Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
- var expected = @"
- Menu
- ┌──────────────────────────────────────────────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ This should be the last line. │
- └──────────────────────────────────────────────────────────────────────────────┘
- F1 Help
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact]
- public void AnchorEnd_Negative_Throws ()
- {
- Pos pos;
- var n = -1;
- Assert.Throws<ArgumentException> (() => pos = Pos.AnchorEnd (n));
- }
- [Fact]
- public void At_SetsValue ()
- {
- var pos = Pos.At (0);
- Assert.Equal ("Pos.Absolute(0)", pos.ToString ());
- pos = Pos.At (5);
- Assert.Equal ("Pos.Absolute(5)", pos.ToString ());
- pos = Pos.At (-1);
- Assert.Equal ("Pos.Absolute(-1)", pos.ToString ());
- }
- [Fact]
- public void At_Equal ()
- {
- var n1 = 0;
- var n2 = 0;
- var pos1 = Pos.At (n1);
- var pos2 = Pos.At (n2);
- Assert.Equal (pos1, pos2);
- }
- [Fact]
- public void SetSide_Null_Throws ()
- {
- var pos = Pos.Left (null);
- Assert.Throws<NullReferenceException> (() => pos.ToString ());
- pos = Pos.X (null);
- Assert.Throws<NullReferenceException> (() => pos.ToString ());
- pos = Pos.Top (null);
- Assert.Throws<NullReferenceException> (() => pos.ToString ());
- pos = Pos.Y (null);
- Assert.Throws<NullReferenceException> (() => pos.ToString ());
- pos = Pos.Bottom (null);
- Assert.Throws<NullReferenceException> (() => pos.ToString ());
- pos = Pos.Right (null);
- Assert.Throws<NullReferenceException> (() => pos.ToString ());
- }
- // TODO: Test Left, Top, Right bottom Equal
- /// <summary>
- /// Tests Pos.Left, Pos.X, Pos.Top, Pos.Y, Pos.Right, and Pos.Bottom set operations
- /// </summary>
- [Fact]
- public void PosSide_SetsValue ()
- {
- string side; // used in format string
- var testRect = Rect.Empty;
- var testInt = 0;
- Pos pos;
- // Pos.Left
- side = "x";
- testInt = 0;
- testRect = Rect.Empty;
- pos = Pos.Left (new View ());
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- pos = Pos.Left (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testRect = new Rect (1, 2, 3, 4);
- pos = Pos.Left (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- // Pos.Left(win) + 0
- pos = Pos.Left (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = 1;
- // Pos.Left(win) +1
- pos = Pos.Left (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = -1;
- // Pos.Left(win) -1
- pos = Pos.Left (new View (testRect)) - testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- // Pos.X
- side = "x";
- testInt = 0;
- testRect = Rect.Empty;
- pos = Pos.X (new View ());
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- pos = Pos.X (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testRect = new Rect (1, 2, 3, 4);
- pos = Pos.X (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- // Pos.X(win) + 0
- pos = Pos.X (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = 1;
- // Pos.X(win) +1
- pos = Pos.X (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = -1;
- // Pos.X(win) -1
- pos = Pos.X (new View (testRect)) - testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- // Pos.Top
- side = "y";
- testInt = 0;
- testRect = Rect.Empty;
- pos = Pos.Top (new View ());
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- pos = Pos.Top (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testRect = new Rect (1, 2, 3, 4);
- pos = Pos.Top (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- // Pos.Top(win) + 0
- pos = Pos.Top (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = 1;
- // Pos.Top(win) +1
- pos = Pos.Top (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = -1;
- // Pos.Top(win) -1
- pos = Pos.Top (new View (testRect)) - testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- // Pos.Y
- side = "y";
- testInt = 0;
- testRect = Rect.Empty;
- pos = Pos.Y (new View ());
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- pos = Pos.Y (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testRect = new Rect (1, 2, 3, 4);
- pos = Pos.Y (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- // Pos.Y(win) + 0
- pos = Pos.Y (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = 1;
- // Pos.Y(win) +1
- pos = Pos.Y (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = -1;
- // Pos.Y(win) -1
- pos = Pos.Y (new View (testRect)) - testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- // Pos.Bottom
- side = "bottom";
- testRect = Rect.Empty;
- testInt = 0;
- pos = Pos.Bottom (new View ());
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- pos = Pos.Bottom (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testRect = new Rect (1, 2, 3, 4);
- pos = Pos.Bottom (new View (testRect));
- Assert.Equal ($"Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}})){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- // Pos.Bottom(win) + 0
- pos = Pos.Bottom (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = 1;
- // Pos.Bottom(win) +1
- pos = Pos.Bottom (new View (testRect)) + testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- testInt = -1;
- // Pos.Bottom(win) -1
- pos = Pos.Bottom (new View (testRect)) - testInt;
- Assert.Equal ($"Pos.Combine(Pos.Combine(Pos.View(side={side}, target=View()({{X={testRect.X},Y={testRect.Y},Width={testRect.Width},Height={testRect.Height}}}))+Pos.Absolute(0)){(testInt < 0 ? '-' : '+')}Pos.Absolute({testInt}))", pos.ToString ());
- }
- // See: https://github.com/gui-cs/Terminal.Gui/issues/504
- [Fact]
- public void LeftTopBottomRight_Win_ShouldNotThrow ()
- {
- // Setup Fake driver
- (Window win, Button button) setup ()
- {
- Application.Init (new FakeDriver ());
- Application.Iteration = () => {
- Application.RequestStop ();
- };
- var win = new Window ("window") {
- X = 0,
- Y = 0,
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- };
- Application.Top.Add (win);
- var button = new Button ("button") {
- X = Pos.Center (),
- };
- win.Add (button);
- return (win, button);
- }
- Application.RunState rs;
- void cleanup (Application.RunState rs)
- {
- // Cleanup
- Application.End (rs);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- // Test cases:
- var app = setup ();
- app.button.Y = Pos.Left (app.win);
- rs = Application.Begin (Application.Top);
- // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
- Application.RunLoop (rs);
- cleanup (rs);
- app = setup ();
- app.button.Y = Pos.X (app.win);
- rs = Application.Begin (Application.Top);
- // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
- Application.RunLoop (rs);
- cleanup (rs);
- app = setup ();
- app.button.Y = Pos.Top (app.win);
- rs = Application.Begin (Application.Top);
- // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
- Application.RunLoop (rs);
- cleanup (rs);
- app = setup ();
- app.button.Y = Pos.Y (app.win);
- rs = Application.Begin (Application.Top);
- // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
- Application.RunLoop (rs);
- cleanup (rs);
- app = setup ();
- app.button.Y = Pos.Bottom (app.win);
- rs = Application.Begin (Application.Top);
- // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
- Application.RunLoop (rs);
- cleanup (rs);
- app = setup ();
- app.button.Y = Pos.Right (app.win);
- rs = Application.Begin (Application.Top);
- // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
- Application.RunLoop (rs);
- cleanup (rs);
- }
- [Fact]
- public void Center_SetsValue ()
- {
- var pos = Pos.Center ();
- Assert.Equal ("Pos.Center", pos.ToString ());
- }
- [Fact]
- public void Percent_SetsValue ()
- {
- float f = 0;
- var pos = Pos.Percent (f);
- Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
- f = 0.5F;
- pos = Pos.Percent (f);
- Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
- f = 100;
- pos = Pos.Percent (f);
- Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
- }
- [Fact]
- public void Percent_Equal ()
- {
- float n1 = 0;
- float n2 = 0;
- var pos1 = Pos.Percent (n1);
- var pos2 = Pos.Percent (n2);
- Assert.Equal (pos1, pos2);
- n1 = n2 = 1;
- pos1 = Pos.Percent (n1);
- pos2 = Pos.Percent (n2);
- Assert.Equal (pos1, pos2);
- n1 = n2 = 0.5f;
- pos1 = Pos.Percent (n1);
- pos2 = Pos.Percent (n2);
- Assert.Equal (pos1, pos2);
- n1 = n2 = 100f;
- pos1 = Pos.Percent (n1);
- pos2 = Pos.Percent (n2);
- Assert.Equal (pos1, pos2);
- n1 = 0;
- n2 = 1;
- pos1 = Pos.Percent (n1);
- pos2 = Pos.Percent (n2);
- Assert.NotEqual (pos1, pos2);
- n1 = 0.5f;
- n2 = 1.5f;
- pos1 = Pos.Percent (n1);
- pos2 = Pos.Percent (n2);
- Assert.NotEqual (pos1, pos2);
- }
- [Fact]
- public void Percent_ThrowsOnIvalid ()
- {
- var pos = Pos.Percent (0);
- Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
- Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
- Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
- Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
- }
- [Fact]
- public void ForceValidatePosDim_True_Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
- {
- Application.Init (new FakeDriver ());
- var t = Application.Top;
- var w = new Window ("w") {
- X = Pos.Left (t) + 2,
- Y = Pos.At (2)
- };
- var v = new View ("v") {
- X = Pos.Center (),
- Y = Pos.Percent (10),
- ForceValidatePosDim = true
- };
- w.Add (v);
- t.Add (w);
- t.Ready += () => {
- Assert.Equal (2, w.X = 2);
- Assert.Equal (2, w.Y = 2);
- Assert.Throws<ArgumentException> (() => v.X = 2);
- Assert.Throws<ArgumentException> (() => v.Y = 2);
- };
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Application.Shutdown ();
- }
- [Fact]
- public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_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 (2, w.X = 2);
- Assert.Equal (2, w.Y = 2);
- };
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Application.Shutdown ();
- }
- [Fact]
- public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
- {
- Application.Init (new FakeDriver ());
- var t = Application.Top;
- var w = new Window ("w") {
- X = Pos.Left (t) + 2,
- Y = Pos.At (2)
- };
- var v = new View ("v") {
- X = Pos.Center (),
- Y = Pos.Percent (10)
- };
- w.Add (v);
- t.Add (w);
- t.Ready += () => {
- v.LayoutStyle = LayoutStyle.Absolute;
- Assert.Equal (2, v.X = 2);
- Assert.Equal (2, v.Y = 2);
- };
- Application.Iteration += () => Application.RequestStop ();
- Application.Run ();
- Application.Shutdown ();
- }
- // DONE: Test PosCombine
- // DONE: Test operators
- [Fact]
- public void PosCombine_Do_Not_Throws ()
- {
- Application.Init (new FakeDriver ());
- var t = Application.Top;
- var w = new Window ("w") {
- X = Pos.Left (t) + 2,
- Y = Pos.Top (t) + 2
- };
- var f = new FrameView ("f");
- var v1 = new View ("v1") {
- X = Pos.Left (w) + 2,
- Y = Pos.Top (w) + 2
- };
- var v2 = new View ("v2") {
- X = Pos.Left (v1) + 2,
- Y = Pos.Top (v1) + 2
- };
- f.Add (v1, v2);
- w.Add (f);
- t.Add (w);
- f.X = Pos.X (t) + Pos.X (v2) - Pos.X (v1);
- f.Y = Pos.Y (t) + Pos.Y (v2) - Pos.Y (v1);
- t.Ready += () => {
- Assert.Equal (0, t.Frame.X);
- Assert.Equal (0, t.Frame.Y);
- Assert.Equal (2, w.Frame.X);
- Assert.Equal (2, w.Frame.Y);
- Assert.Equal (2, f.Frame.X);
- Assert.Equal (2, f.Frame.Y);
- Assert.Equal (4, v1.Frame.X);
- Assert.Equal (4, v1.Frame.Y);
- Assert.Equal (6, v2.Frame.X);
- Assert.Equal (6, v2.Frame.Y);
- };
- 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") {
- X = Pos.Left (t) + 2,
- Y = Pos.Top (t) + 2
- };
- var f = new FrameView ("f");
- var v1 = new View ("v1") {
- X = Pos.Left (w) + 2,
- Y = Pos.Top (w) + 2
- };
- var v2 = new View ("v2") {
- X = Pos.Left (v1) + 2,
- Y = Pos.Top (v1) + 2
- };
- f.Add (v1); // v2 not added
- w.Add (f);
- t.Add (w);
- f.X = Pos.X (v2) - Pos.X (v1);
- f.Y = Pos.Y (v2) - Pos.Y (v1);
- Assert.Throws<InvalidOperationException> (() => Application.Run ());
- Application.Shutdown ();
- }
- [Fact]
- public void Pos_Add_Operator ()
- {
- Application.Init (new FakeDriver ());
- var top = Application.Top;
- var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
- var field = new TextField () { X = 0, Y = 0, 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 = field.Y, Width = 20 };
- view.Add (label);
- Assert.Equal ($"Label {count}", label.Text);
- Assert.Equal ($"Pos.Absolute({count})", label.Y.ToString ());
- Assert.Equal ($"Pos.Absolute({count})", field.Y.ToString ());
- field.Y += 1;
- count++;
- Assert.Equal ($"Pos.Absolute({count})", field.Y.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 ();
- }
- [Fact]
- public void Pos_Subtract_Operator ()
- {
- Application.Init (new FakeDriver ());
- var top = Application.Top;
- var view = new View () { X = 0, Y = 0, Width = 20, Height = 20 };
- var field = new TextField () { X = 0, Y = 0, 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 = field.Y, Width = 20 };
- view.Add (label);
- Assert.Equal ($"Label {i}", label.Text);
- Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
- listLabels.Add (label);
- Assert.Equal ($"Pos.Absolute({i})", field.Y.ToString ());
- field.Y += 1;
- Assert.Equal ($"Pos.Absolute({i + 1})", field.Y.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 ($"Pos.Absolute({count})", field.Y.ToString ());
- field.Y -= 1;
- count--;
- Assert.Equal ($"Pos.Absolute({count})", field.Y.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 Internal_Tests ()
- {
- var posFactor = new Pos.PosFactor (0.10F);
- Assert.Equal (10, posFactor.Anchor (100));
- var posAnchorEnd = new Pos.PosAnchorEnd (1);
- Assert.Equal (99, posAnchorEnd.Anchor (100));
- var posCenter = new Pos.PosCenter ();
- Assert.Equal (50, posCenter.Anchor (100));
- var posAbsolute = new Pos.PosAbsolute (10);
- Assert.Equal (10, posAbsolute.Anchor (0));
- var posCombine = new Pos.PosCombine (true, posFactor, posAbsolute);
- Assert.Equal (posCombine.left, posFactor);
- Assert.Equal (posCombine.right, posAbsolute);
- Assert.Equal (20, posCombine.Anchor (100));
- var view = new View (new Rect (20, 10, 20, 1));
- var posViewX = new Pos.PosView (view, 0);
- Assert.Equal (20, posViewX.Anchor (0));
- var posViewY = new Pos.PosView (view, 1);
- Assert.Equal (10, posViewY.Anchor (0));
- var posRight = new Pos.PosView (view, 2);
- Assert.Equal (40, posRight.Anchor (0));
- var posViewBottom = new Pos.PosView (view, 3);
- Assert.Equal (11, posViewBottom.Anchor (0));
- }
- [Fact]
- public void Function_SetsValue ()
- {
- var text = "Test";
- var pos = Pos.Function (() => text.Length);
- Assert.Equal ("Pos.PosFunc(4)", pos.ToString ());
- text = "New Test";
- Assert.Equal ("Pos.PosFunc(8)", pos.ToString ());
- text = "";
- Assert.Equal ("Pos.PosFunc(0)", pos.ToString ());
- }
- [Fact]
- public void Function_Equal ()
- {
- var f1 = () => 0;
- var f2 = () => 0;
- var pos1 = Pos.Function (f1);
- var pos2 = Pos.Function (f2);
- Assert.Equal (pos1, pos2);
- f2 = () => 1;
- pos2 = Pos.Function (f2);
- Assert.NotEqual (pos1, pos2);
- }
- [Theory, AutoInitShutdown]
- [InlineData (true)]
- [InlineData (false)]
- public void PosPercentPlusOne (bool testHorizontal)
- {
- var container = new View {
- Width = 100,
- Height = 100,
- };
- var label = new Label {
- X = testHorizontal ? Pos.Percent (50) + Pos.Percent (10) + 1 : 1,
- Y = testHorizontal ? 1 : Pos.Percent (50) + Pos.Percent (10) + 1,
- Width = 10,
- Height = 10,
- };
- 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 (61, label.Frame.X);
- Assert.Equal (1, label.Frame.Y);
- } else {
- Assert.Equal (1, label.Frame.X);
- Assert.Equal (61, label.Frame.Y);
- }
- }
- }
- }
|