using System.Globalization; using System.Text; using Xunit.Abstractions; // Alias Console to MockConsole so we don't accidentally use Console using Console = Terminal.Gui.FakeConsole; namespace Terminal.Gui.ViewTests; public class DimTests { private readonly ITestOutputHelper _output; private readonly 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 │ └────────────────────┘" }; public DimTests (ITestOutputHelper output) { _output = output; Console.OutputEncoding = Encoding.Default; // Change current culture var culture = CultureInfo.CreateSpecificCulture ("en-US"); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; } // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved // A new test that does not depend on Application is needed. [Fact] [AutoInitShutdown] public void Dim_Add_Operator () { Toplevel 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 += (s, k) => { if (k.KeyCode == KeyCode.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 += (s, a) => { while (count < 20) { field.NewKeyDownEvent (new Key (KeyCode.Enter)); } Application.RequestStop (); }; var win = new Window (); win.Add (view); win.Add (field); top.Add (win); Application.Run (top); Assert.Equal (20, count); } // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved // TODO: A new test that calls SetRelativeLayout directly is needed. [Fact] [TestRespondersDisposed] public void Dim_Referencing_SuperView_Does_Not_Throw () { var super = new View ("super") { Width = 10, Height = 10 }; var view = new View ("view") { Width = Dim.Width (super), // this is allowed Height = Dim.Height (super) // this is allowed }; super.Add (view); super.BeginInit (); super.EndInit (); Exception exception = Record.Exception (super.LayoutSubviews); Assert.Null (exception); super.Dispose (); } // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved // TODO: A new test that calls SetRelativeLayout directly is needed. [Fact] [AutoInitShutdown] public void Dim_Subtract_Operator () { Toplevel 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; List