Files
Terminal.Gui/UnitTests/View/Layout/DimTests.cs
Tig 4430fe2cc6 Fixes #3192. Improve correctness / clarity of existing View.AutoSize functionality/unit tests (#3202)
* Removed resharper settings from editorconfig

* Remove constructors with frame parameters from Button class.

* Remove constructors with frame parameters from CheckBox class.

* Cleanup code.

* Remove constructors with frame parameters from ComboBox class.

* @BDisp
Remove constructors with frame parameters from FrameView class.

* Remove constructors with frame parameters from Label class.

* Remove constructors with frame parameters from ListView class.

* Remove constructors with frame parameters from ScrollBarView class.

* Remove constructors with frame parameters from ScrollView class.

* Remove namespace braces.

* Cleanup code.

* Cleanup code.

* Cleanup code.

* Remove constructors with frame parameters from TextField class.

* Remove constructors with frame parameters from TimeField class.

* Fixes #3182. OnResizeNeeded returns int.MaxValue and int.MaxValue when Application.Top is null, should return Size.Empty.

* Remove constructors with frame parameters from Toplevel class.

* Remove constructors with frame parameters from Window class.

* Fix merge errors.

* Revert "Fixes #3182. OnResizeNeeded returns int.MaxValue and int.MaxValue when Application.Top is null, should return Size.Empty."

This reverts commit cf9c24b846.

* Revert unit test.

* Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame (#2980)

* Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame

* Trying fix this unit test that sometimes fail.

* Fixes #2983. View need a alternative DrawFrame for the v2.

* Use new DrawFrame method.

* Change _lines field to Lines property.

* Add TabWindow unit test.

* Add DrawIncompleteFrame method and unit tests.

* Add more unit tests to LineCanvas.

* Fix newline conflict errors.

* Revert "Change _lines field to Lines property."

This reverts commit ab6c5f3094.

* Add DrawIncompleteFrame method and unit tests.

* Add more unit tests to LineCanvas.

* Fix newline conflict errors.

* Force render immediately instead of join.

* I will never rely on zero-location-based unit test again.

* Fix TestTreeViewColor unit test fail.

* Using location of 3 to avoid be divisible by 2 and so avoiding bugs.

* Revert "Using location of 3 to avoid be divisible by 2 and so avoiding bugs."

This reverts commit dd3df135d8.

* Revert "I will never rely on zero-location-based unit test again."

This reverts commit 62adf6f285.

* Revert "Fix newline conflict errors."

This reverts commit 4acf72612d.

* Revert "Add more unit tests to LineCanvas."

This reverts commit 66bc6f514e.

* Revert "Add DrawIncompleteFrame method and unit tests."

This reverts commit 680ba264e1.

* Resolving merge conflicts.

* Revert "Use new DrawFrame method."

This reverts commit 69a7f17f19.

* Revert "Fixes #2983. View need a alternative DrawFrame for the v2."

This reverts commit dade9fd767.

* Reverting this changes to start a new one.

* Add horizontal and vertical support for combining glyphs.

* Fix text and auto size behavior.

* Add TabWidth property.

* Add unit test for WordWrap.

* Fixes #3017. View TextDirection returns incorrect size on a vertical direction instance with AutoSize as false.

* Using Frame to force read from the get method.

* Fix some issues with AutoSize and ForceValidatePosDim.

* Fixing broken unit tests.

* Restoring code I've broken.

* Removing forgotten code.

* Only LayoutStyle.Computed can change the Frame.

* DateField and TimeField depends on LayoutStyle.Computed.

* Fix unit tests related with LayoutStyle.

* Implements tabs, left and right arrows as View.

* Draws a minimum full border.

* Adds missing XML parameter.

* Adds assert tests for Frame.

* Removes duplicates InlineData.

* Adds more unit tests for minimum full border without Left and Right thickness.

* Trying to fix the TestTreeViewColor unit test fail.

* Prevents a user to set TextDirection to -1.

* Prevents any invalid TextDirection value.

* Removes (TextDirection)(-1).

* Removes unnecessary TextDirection initialization.

* Removes LayoutStyle.

* Fixing unit tests with border.

* Trying to fix TestTreeViewColor again.

* Revert "Trying to fix TestTreeViewColor again."

This reverts commit c2efa8e42e.

* Trying to fix TestTreeViewColor again.

* Fix merge errors.

* Fix merge errors.

* Restoring unit test.

* Restores the right XML comment.

* Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes.

* Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes.

* Only call OnResizeNeeded if it's LayoutStyle.Computed.

* Fix merge errors.

* Fix merge errors.

* Fix unit tests fail.

* Reformat.

* Again.

* Rename to OnDrawAdornments.

* Fix failing unit tests.

* Reduces indentation and cleanup code.

* Cleanup code.

* Fix bug done when cleanup.

* Replace FrameHandledMouseEvent to AdornmentHandledMouseEvent.

* Removes Tab constructor parameters.

---------

Co-authored-by: Tig <tig@users.noreply.github.com>

* Fix merge errors.

* Remove constructors with parameters from Button.

* Remove parenthesis on objects initializers from Button.

* Remove constructors with parameters from CheckBox.

* Remove parenthesis on objects initializers from CheckBox.

* Remove constructors with parameters from ComboBox.

* Remove constructors with parameters from FrameView.

* Remove parenthesis on objects initializers from FrameView.

* Initial commit

* Renamed Direction enum for clarity in refactoring unit tests

* Moved nav tests to NavigationTests

* Moved view tests around

* Cleaning up TextFormatter and View.AutoSize code

* Fixed latent TextFormatter bug with \n

* removed Application dependency on some autosize unit tests

* Fixed Label tests to deal with auotsize overriding height/width

* Fixed more label tests. WIP

* Fixed all places where AutoSize = happend after setting Dims

* Started adding new primitive View.Text tests

* Code comments

* WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match.

* WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match.

* Remove frame set from the View constructor and prevent SetRelativeLayout running if not yet initialized.

* Changes needed for unit tests pass on remove parameters constructors from the Label class.

* Remove constructors with parameters from Label.

* Remove parenthesis on objects initializers from Label.

* Prefix private fields with underscore.

* Renamed to MaxLength.

* Remove constructors with parameters from ListView.

* MakeWrapper not needed anymore.

* Remove parenthesis on objects initializers from ListView.

* WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match.

* Massive code cleanup - use parameterless constructors and ensure AutoSize is set properly. Code reformat.

* Massive code cleanup - use parameterless constructors and ensure AutoSize is set properly. Code reformat.

* Fixed messagebox

* Remove constructors with parameters from ScrollBarView and ScrollView.

* Remove parenthesis on objects initializers from ScrollBarView and ScrollView.

* Cleanup code.

* Fix merge errors.

* Add empty dotsettings for solution and projects.

* Set ReSharper language analysis level for projects to  C#12

* Make ReSharper consider itself the boss for style

* Add rule to enforce property backing fields above the property

* Disable auto-detection of naming rules so ReSharper doesn't change them by itself

* Don't let someone's VS settings override the indent settings

* Explicitly set tab width to 4 spaces and force spaces.

* Rules to keep various multi-line constructs aligned within themselves

* Curly brace rules (Using K&R style, per current project spec)

* Blank line rules

Mostly to add breaks in various situations, and also to enforce max of 1 blank line.

* Increase auto-wrap to 160 from default of 120

* Line break at end of all files, to make Unixy systems happy

* Keep attributes on their own lines except for methods and records that are themselves single-line

* Increase attribute auto-wrap to 60 from default of 38

* Wrap/chop rules for long method signatures and record declarations

Chop if either already multi-line or if over 8 parameters.
Chop AFTER the left paren and BEFORE the first parameter.

* Chop rules for generics

For generics with multi-line type parameters or multiple type parameter constraints, chop in a way that has consistent alignment.

* Always enforce enum members on their own lines

* One-line functions completely on one line

They're usually expression-bodied anyway, here, so this is almost irrelevant

* Keep control flow statements on their own lines

* Follow same chop rules for method invocation as method declaration

* Chop long or multi-line method chains or patterns

* Wrap rules for binary operators

Operator at beginning of new lines
Auto-chop if long or already multi-line

* Spaces between keywords and their opening parentheses

* Add file layout rules for organization of reorderable items like fields, properties, etc.

Ugly XML, so load it up in the UI to look at it
It's MOSTLY the ReSharper defaults, but with more aggressive sorting, generally by access modifier and name, within each grouping.

* Deconstructors use per-member types

* Use keywords for built-in types and increase severity of inspection for violations

Also apply to IntPtr and similar, which should now be nint and similar

* Apply syntax style on completion.

* Use var when evident for built-in and simple types, but not elsewhere

* Increase severity for some minor redundancy and clarity inspections

* Enforce braces always required for blocks, and treat as error

* Warn if a local function is not statement-bodied

* Increase various inspection severities

IF Condition => Severity:
Attributes not wrapped property => Suggestion
Constructors expression-bodied => Error
default doesn't have type when it isn't clear => suggestion
Namespaces not file-scoped => error
Methods not statement-bodied => hint
Null check pattern not the object property pattern => Error

* Adjust preference order of null check patterns to make object pattern highest

* XmlDoc rules to keep tags and contents aligned and wrapped

* Add a few custom profiles for code cleanup and make the full profile default

* Remove parts for languages not used in this solution

* copied in v2_develop changes

* Merged v2_develop

* Added Begin/Init unit tests. Removed Application dependencey from AutoSizeFalse tests

* TextFormatter.Lines -> GetLines ()

* Let ReSharper know we intend to localize things

* TextFormatter code cleanup

* copied in v2_develop changes

* Merged v2_develop

* Spaces aren't wanted here either.

* Fix merge errors.

* Fixes ContentBottomRightCorner related with https://github.com/gui-cs/Terminal.Gui/issues/3211#issue-2098878820

* Remove constructors with parameters from ContextMenu.

* Remove commented code.

* Remove constructors with parameters from OpenDialog.

* Remove constructors with parameters from SaveDialog.

* Remove constructors with parameters from TextField.

* Remove constructors with parameters from TimeField.

* Fix unit test.

* Remove unnecessary SetInitialProperties method.

* Remove unnecessary SetInitialProperties method.

* Remove parenthesis on objects initializers from Toplevel and Window.

* Remove constructors with parameters from RadioGroup.

* Remove constructors with parameters from TextView.

* Remove constructors with parameters from MenuBar.

* TEMPORARY: Turn everything that was set to error down to warning or lower

* Fixes #3219. MenuBar is opened by call OpenMenu even it's disabled.

* Remove constructors with parameters from Menu.

* Remove constructors with parameters from View.

* Change constructor to internal because is mainly useful for testing.

* ReSharper Cleanup Code.

* Added format only r# config

* Fixes #3224. TextFormatter.Lines should return a single string.Empty list even with Width or Height equal to zero.

* Remove constructors with parameters from Dialog.

* Remove constructors with parameters from TextValidateField.

* Fixes https://github.com/gui-cs/Terminal.Gui/issues/3224#issuecomment-1924096038

* Fixes #3225. Press CursorDown on TabView doesn't move to the next view.

* Fixes #3229. TextFormatter should have a FillRemaining property.

* Testing formatting merge in bdisp

* Testing formatting merge

* Testing formatting merge bdisp

* Testing formatting merge 2

* xmldoc format

* R# Full Code Cleanup

* R# Full Code Cleanup2

* R# Full Code Cleanup2

* Merged! But broke tests

* Refixing...

* Refixed DrawTests

* Refixed ViewTests

* Refixed Text Tests

* Refixed more unit tests

* Refixed scenarios

* Refixed rest of scenarios

* Refixed ViewsTests

* Refixed rest of tests. All unit tests pass again!

* Fixed warnings

* Updated R# version. Added new code cleanup settings

* Applied latest code cleanup to solution

* Another code cleanup pass

* Tweaked r# settings. .editorconfig now matches.

* r# else on separate line

* r# - update

* r# - full solution

* test commit

* test commit

* test commit

* Removed extra profiles

* Full cleanup following cleaning up profiles

* Null checking pattern rules/inspections

* Tabs and extra whitespace are evil

* Attributes on their own lines

* Code layout rules to put fields first and to put backing fields with their properties

* Merged v2_develop

* Full cleanup following dodexahedron's PR

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
Co-authored-by: Brandon Thetford <github@snapsinazfs.com>
2024-02-11 15:54:03 -07:00

977 lines
33 KiB
C#

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;
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 { X = 0, Y = view.Bounds.Height, /*Width = 20,*/ Text = field.Text };
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 { Width = 10, Height = 10, Text = "super" };
var view = new View
{
Width = Dim.Width (super), // this is allowed
Height = Dim.Height (super), // this is allowed
Text = "view"
};
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<Label> listLabels = new ();
for (var i = 0; i < count; i++)
{
field.Text = $"Label {i}";
var label = new Label { X = 0, Y = view.Bounds.Height, /*Width = 20,*/ Text = field.Text };
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 += (s, k) =>
{
if (k.KeyCode == KeyCode.Enter)
{
Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
view.Remove (listLabels [count - 1]);
listLabels [count - 1].Dispose ();
Assert.Equal ($"Absolute({count})", view.Height.ToString ());
view.Height -= 1;
count--;
Assert.Equal ($"Absolute({count})", view.Height.ToString ());
}
};
Application.Iteration += (s, a) =>
{
while (count > 0)
{
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 (0, 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_SyperView_Referencing_SubView_Throws ()
{
var super = new View { Width = 10, Height = 10, Text = "super" };
var view2 = new View { Width = 10, Height = 10, Text = "view2" };
var view = new View
{
Width = Dim.Width (view2), // this is not allowed
Height = Dim.Height (view2), // this is not allowed
Text = "view"
};
view.Add (view2);
super.Add (view);
super.BeginInit ();
super.EndInit ();
Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
super.Dispose ();
}
// 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_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
{
var t = new View { Width = 80, Height = 25, Text = "top" };
var w = new Window { Width = Dim.Fill (), Height = Dim.Sized (10) };
var v = new View { Width = Dim.Width (w) - 2, Height = Dim.Percent (10), Text = "v" };
w.Add (v);
t.Add (w);
Assert.Equal (LayoutStyle.Absolute, t.LayoutStyle);
Assert.Equal (LayoutStyle.Computed, w.LayoutStyle);
Assert.Equal (LayoutStyle.Computed, v.LayoutStyle);
t.LayoutSubviews ();
Assert.Equal (2, v.Width = 2);
Assert.Equal (2, v.Height = 2);
// Force v to be LayoutStyle.Absolute;
v.Frame = new Rect (0, 1, 3, 4);
Assert.Equal (LayoutStyle.Absolute, v.LayoutStyle);
t.LayoutSubviews ();
Assert.Equal (2, v.Width = 2);
Assert.Equal (2, v.Height = 2);
t.Dispose ();
}
// 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_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
{
var t = new View { Width = 80, Height = 25, Text = "top" };
var w = new Window
{
X = 1,
Y = 2,
Width = 4,
Height = 5,
Title = "w"
};
t.Add (w);
t.LayoutSubviews ();
Assert.Equal (3, w.Width = 3);
Assert.Equal (4, w.Height = 4);
t.Dispose ();
}
// 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 DimCombine_ObtuseScenario_Does_Not_Throw_If_Two_SubViews_Refs_The_Same_SuperView ()
{
var t = new View { Width = 80, Height = 25, Text = "top" };
var w = new Window
{
Width = Dim.Width (t) - 2, // 78
Height = Dim.Height (t) - 2 // 23
};
var f = new FrameView ();
var v1 = new View
{
Width = Dim.Width (w) - 2, // 76
Height = Dim.Height (w) - 2 // 21
};
var v2 = new View
{
Width = Dim.Width (v1) - 2, // 74
Height = Dim.Height (v1) - 2 // 19
};
f.Add (v1, v2);
w.Add (f);
t.Add (w);
t.BeginInit ();
t.EndInit ();
f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
// BUGBUG: v2 - f references t and w here; t is f's super-superview and w is f's superview. This is supported!
Exception exception = Record.Exception (t.LayoutSubviews);
Assert.Null (exception);
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);
t.Dispose ();
}
// See #2461
//[Fact]
//public void Dim_Referencing_SuperView_Throws ()
//{
// var super = new View ("super") {
// Width = 10,
// Height = 10
// };
// var view = new View ("view") {
// Width = Dim.Width (super), // this is not allowed
// Height = Dim.Height (super), // this is not allowed
// };
// super.Add (view);
// super.BeginInit ();
// super.EndInit ();
// Assert.Throws<InvalidOperationException> (() => super.LayoutSubviews ());
//}
// TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
// TODO: A new test that calls SetRelativeLayout directly is needed.
/// <summary>This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461</summary>
[Fact]
[TestRespondersDisposed]
public void DimCombine_ObtuseScenario_Throw_If_SuperView_Refs_SubView ()
{
var t = new View { Width = 80, Height = 25 };
var w = new Window
{
Width = Dim.Width (t) - 2, // 78
Height = Dim.Height (t) - 2 // 23
};
var f = new FrameView ();
var v1 = new View
{
Width = Dim.Width (w) - 2, // 76
Height = Dim.Height (w) - 2 // 21
};
var v2 = new View
{
Width = Dim.Width (v1) - 2, // 74
Height = Dim.Height (v1) - 2 // 19
};
f.Add (v1, v2);
w.Add (f);
t.Add (w);
t.BeginInit ();
t.EndInit ();
f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
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);
t.Dispose ();
}
// TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
// TODO: A new test that calls SetRelativeLayout directly is needed.
[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
{
AutoSize = false,
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.BeginInit ();
Application.Top.EndInit ();
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);
}
}
[Fact]
public void Fill_Equal ()
{
var margin1 = 0;
var margin2 = 0;
Dim dim1 = Dim.Fill (margin1);
Dim dim2 = Dim.Fill (margin2);
Assert.Equal (dim1, dim2);
}
// TODO: Other Dim.Height tests (e.g. Equal?)
[Fact]
public void Fill_SetsValue ()
{
var testMargin = 0;
Dim 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 Function_Equal ()
{
Func<int> f1 = () => 0;
Func<int> f2 = () => 0;
Dim dim1 = Dim.Function (f1);
Dim dim2 = Dim.Function (f2);
Assert.Equal (dim1, dim2);
f2 = () => 1;
dim2 = Dim.Function (f2);
Assert.NotEqual (dim1, dim2);
}
[Fact]
public void Function_SetsValue ()
{
var text = "Test";
Dim 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 Height_Set_To_Null_Throws ()
{
Dim dim = Dim.Height (null);
Assert.Throws<NullReferenceException> (() => dim.ToString ());
}
[Fact]
[TestRespondersDisposed]
public void Height_SetsValue ()
{
var testVal = Rect.Empty;
var testValview = new View { Frame = testVal };
Dim dim = Dim.Height (testValview);
Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
testValview.Dispose ();
testVal = new Rect (1, 2, 3, 4);
testValview = new View { Frame = testVal };
dim = Dim.Height (testValview);
Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
testValview.Dispose ();
}
[Fact]
[TestRespondersDisposed]
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 { Frame = 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));
view.Dispose ();
}
[Fact]
public void New_Works ()
{
var dim = new Dim ();
Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
}
// 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 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.
Toplevel t = Application.Top;
var w = new Window { Width = 100, Height = 100 };
var f1 = new FrameView
{
X = 0,
Y = 0,
Width = Dim.Percent (50),
Height = 5,
Title = "f1"
};
var f2 = new FrameView
{
X = Pos.Right (f1),
Y = 0,
Width = Dim.Fill (),
Height = 5,
Title = "f2"
};
var v1 = new Button
{
AutoSize = false,
X = Pos.X (f1) + 2,
Y = Pos.Bottom (f1) + 2,
Width = Dim.Width (f1) - 2,
Height = Dim.Fill () - 2,
ValidatePosDim = true,
Text = "v1"
};
var v2 = new Button
{
AutoSize = false,
X = Pos.X (f2) + 2,
Y = Pos.Bottom (f2) + 2,
Width = Dim.Width (f2) - 2,
Height = Dim.Fill () - 2,
ValidatePosDim = true,
Text = "v2"
};
var v3 = new Button
{
AutoSize = false,
Width = Dim.Percent (10),
Height = Dim.Percent (10),
ValidatePosDim = true,
Text = "v3"
};
var v4 = new Button
{
AutoSize = false,
Width = Dim.Sized (50),
Height = Dim.Sized (50),
ValidatePosDim = true,
Text = "v4"
};
var v5 = new Button
{
AutoSize = false,
Width = Dim.Width (v1) - Dim.Width (v3),
Height = Dim.Height (v1) - Dim.Height (v3),
ValidatePosDim = true,
Text = "v5"
};
var v6 = new Button
{
AutoSize = false,
X = Pos.X (f2),
Y = Pos.Bottom (f2) + 2,
Width = Dim.Percent (20, true),
Height = Dim.Percent (20, true),
ValidatePosDim = true,
Text = "v6"
};
w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
t.Add (w);
t.Ready += (s, e) =>
{
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(View(Width,FrameView(f1)(0,0,49,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(View(Width,FrameView(f2)(49,0,49,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(View(Width,Button(v1)(2,7,47,89))-View(Width,Button(v3)(0,0,9,9)))",
v5.Width.ToString ()
);
Assert.Equal (
"Combine(View(Height,Button(v1)(2,7,47,89))-View(Height,Button(v3)(0,0,9,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;
Assert.True (t.LayoutNeeded);
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 (99, f1.Frame.Width); // 100-1=99
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 (99, f2.Frame.Width); // 100-1=99
Assert.Equal (5, f2.Frame.Height);
v1.Text = "Button1";
Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,99,5))-Absolute(2))", v1.Width.ToString ());
Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
Assert.Equal (97, v1.Frame.Width); // 99-2=97
Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
v2.Text = "Button2";
Assert.Equal (
"Combine(View(Width,FrameView(f2)(99,0,99,5))-Absolute(2))",
v2.Width.ToString ()
);
Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
Assert.Equal (97, v2.Frame.Width); // 99-2=97
Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
v3.Text = "Button3";
Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
Assert.Equal (
19,
v3.Frame
.Width
); // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
Assert.Equal (19, v3.Frame.Height); // 199*10%=19
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(View(Width,Button(v1)(2,7,97,189))-View(Width,Button(v3)(0,0,19,19)))",
v5.Width.ToString ()
);
Assert.Equal (
"Combine(View(Height,Button(v1)(2,7,97,189))-View(Height,Button(v3)(0,0,19,19)))",
v5.Height.ToString ()
);
Assert.Equal (78, v5.Frame.Width); // 97-9=78
Assert.Equal (170, v5.Frame.Height); // 189-19=170
v6.Text = "Button6";
Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
Assert.Equal (19, v6.Frame.Width); // 99*20%=19
Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
};
Application.Iteration += (s, a) => Application.RequestStop ();
Application.Run ();
}
[Fact]
public void Percent_Equals ()
{
float n1 = 0;
float n2 = 0;
Dim dim1 = Dim.Percent (n1);
Dim 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_Invalid_Throws ()
{
Dim 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 Percent_SetsValue ()
{
float f = 0;
Dim 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 ());
}
// 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 PosCombine_View_Not_Added_Throws ()
{
var t = new View { Width = 80, Height = 50 };
var super = new View { Width = Dim.Width (t) - 2, Height = Dim.Height (t) - 2 };
t.Add (super);
var sub = new View ();
super.Add (sub);
var v1 = new View { Width = Dim.Width (super) - 2, Height = Dim.Height (super) - 2 };
var v2 = new View { Width = Dim.Width (v1) - 2, Height = Dim.Height (v1) - 2 };
sub.Add (v1);
// v2 not added to sub; should cause exception on Layout since it's referenced by sub.
sub.Width = Dim.Fill () - Dim.Width (v2);
sub.Height = Dim.Fill () - Dim.Height (v2);
t.BeginInit ();
t.EndInit ();
Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
t.Dispose ();
v2.Dispose ();
}
[Fact]
[TestRespondersDisposed]
public void SetsValue ()
{
var testVal = Rect.Empty;
var testValView = new View { Frame = testVal };
Dim dim = Dim.Width (testValView);
Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
testValView.Dispose ();
testVal = new Rect (1, 2, 3, 4);
testValView = new View { Frame = testVal };
dim = Dim.Width (testValView);
Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
testValView.Dispose ();
}
[Fact]
public void Sized_Equals ()
{
var n1 = 0;
var n2 = 0;
Dim dim1 = Dim.Sized (n1);
Dim 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 Sized_SetsValue ()
{
Dim dim = Dim.Sized (0);
Assert.Equal ("Absolute(0)", dim.ToString ());
var 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]
[TestRespondersDisposed]
public void Width_Equals ()
{
var testRect1 = Rect.Empty;
var view1 = new View { Frame = testRect1 };
var testRect2 = Rect.Empty;
var view2 = new View { Frame = testRect2 };
Dim dim1 = Dim.Width (view1);
Dim 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 { Frame = 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 { Frame = 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 { Frame = testRect1 };
testRect2 = Rect.Empty;
view2 = new View { Frame = testRect2 };
dim1 = Dim.Width (view1);
dim2 = Dim.Width (view2);
Assert.NotEqual (dim1, dim2);
#if DEBUG_IDISPOSABLE
// HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
Responder.Instances.Clear ();
Assert.Empty (Responder.Instances);
#endif
}
[Fact]
public void Width_Set_To_Null_Throws ()
{
Dim dim = Dim.Width (null);
Assert.Throws<NullReferenceException> (() => dim.ToString ());
}
}