Tons of unit test updates

This commit is contained in:
Tig
2024-04-18 09:59:07 -06:00
parent a89aa040ca
commit a48146af82
14 changed files with 508 additions and 467 deletions

View File

@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text;
@@ -521,64 +522,56 @@ internal partial class TestHelpers
return sb.ToString ();
}
// TODO: Update all tests that use GetALlViews to use GetAllViewsTheoryData instead
/// <summary>Gets a list of instances of all classes derived from View.</summary>
/// <returns>List of View objects</returns>
public static List<View> GetAllViews ()
{
return typeof (View).Assembly.GetTypes ()
.Where (
type => type.IsClass
&& !type.IsAbstract
&& type.IsPublic
&& type.IsSubclassOf (typeof (View))
)
.Select (type => CreateView (type, type.GetConstructor (Array.Empty<Type> ())))
.ToList ();
}
//// TODO: Update all tests that use GetALlViews to use GetAllViewsTheoryData instead
///// <summary>Gets a list of instances of all classes derived from View.</summary>
///// <returns>List of View objects</returns>
//public static List<View> GetAllViews ()
//{
// return typeof (View).Assembly.GetTypes ()
// .Where (
// type => type.IsClass
// && !type.IsAbstract
// && type.IsPublic
// && type.IsSubclassOf (typeof (View))
// )
// .Select (type => CreateView (type, type.GetConstructor (Array.Empty<Type> ())))
// .ToList ();
//}
public static TheoryData<View, string> GetAllViewsTheoryData ()
{
// TODO: Figure out how to simplify this. I couldn't figure out how to not have to iterate over ret.
(View view, string name)[] ret =
typeof (View).Assembly
.GetTypes ()
.Where (
type => type.IsClass
&& !type.IsAbstract
&& type.IsPublic
&& type.IsSubclassOf (typeof (View))
)
.Select (
type => (
view: CreateView (
type, type.GetConstructor (Array.Empty<Type> ())),
name: type.Name)
).ToArray();
//public class AllViewsData : IEnumerable<object []>
//{
// private Lazy<List<object []>> data;
TheoryData<View, string> td = new ();
foreach ((View view, string name) in ret)
{
td.Add(view, name);
}
// public AllViewsData ()
// {
// data = new Lazy<List<object []>> (GetTestData);
// }
return td;
}
// public IEnumerator<object []> GetEnumerator ()
// {
// return data.Value.GetEnumerator ();
// }
// IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
public static TheoryData<Scenario, string> GetAllScenarioTheoryData ()
{
// TODO: Figure out how to simplify this. I couldn't figure out how to not have to iterate over ret.
var scenarios = Scenario.GetScenarios ();
(Scenario scenario, string name) [] ret = scenarios.Select (s => (scenario: s, name: s.GetName ())).ToArray();
TheoryData<Scenario, string> td = new ();
foreach ((Scenario scenario, string name) in ret)
{
td.Add (scenario, name);
}
// private List<object []> GetTestData ()
// {
// var viewTypes = typeof (View).Assembly
// .GetTypes ()
// .Where (type => type.IsClass && !type.IsAbstract && type.IsPublic && type.IsSubclassOf (typeof (View)));
// var testData = new List<object []> ();
// foreach (var type in viewTypes)
// {
// var view = CreateView (type, type.GetConstructor (Array.Empty<Type> ()));
// testData.Add (new object [] { view, type.Name });
// }
// return testData;
// }
//}
return td;
}
/// <summary>
/// Verifies the console used all the <paramref name="expectedColors"/> when rendering. If one or more of the
@@ -851,3 +844,25 @@ internal partial class TestHelpers
[GeneratedRegex ("\\s+$", RegexOptions.Multiline)]
private static partial Regex TrailingWhiteSpaceRegEx ();
}
public class TestsAllViews
{
public static IEnumerable<object []> AllViewTypes =>
typeof (View).Assembly
.GetTypes ()
.Where (type => type.IsClass && !type.IsAbstract && type.IsPublic && type.IsSubclassOf (typeof (View)))
.Select (type => new object [] { type });
public static View CreateInstanceIfNotGeneric (Type type)
{
if (type.IsGenericType)
{
// Return null for generic types
return null;
}
return Activator.CreateInstance (type) as View;
}
}

View File

@@ -3,7 +3,7 @@ using Xunit.Abstractions;
namespace UICatalog.Tests;
public class ScenarioTests
public class ScenarioTests : TestsAllViews
{
private readonly ITestOutputHelper _output;
@@ -14,18 +14,25 @@ public class ScenarioTests
#endif
_output = output;
}
public static IEnumerable<object []> AllScenarioTypes =>
typeof (Scenario).Assembly
.GetTypes ()
.Where (type => type.IsClass && !type.IsAbstract && type.IsSubclassOf (typeof (Scenario)))
.Select (type => new object [] { type });
public static TheoryData<Scenario, string> AllScenarios => TestHelpers.GetAllScenarioTheoryData ();
/// <summary>
/// <para>This runs through all Scenarios defined in UI Catalog, calling Init, Setup, and Run.</para>
/// <para>Should find any Scenarios which crash on load or do not respond to <see cref="Application.RequestStop()"/>.</para>
/// </summary>
[Theory]
[MemberData (nameof (AllScenarios))]
public void Run_All_Scenarios (Scenario scenario, string viewName)
[MemberData (nameof (AllScenarioTypes))]
public void Run_All_Scenarios (Type scenarioType)
{
_output.WriteLine ($"Running Scenario '{scenario.GetName ()}'");
_output.WriteLine ($"Running Scenario '{scenarioType}'");
Scenario scenario = (Scenario)Activator.CreateInstance (scenarioType);
Application.Init (new FakeDriver ());
@@ -116,16 +123,13 @@ public class ScenarioTests
TextField _hText;
var _hVal = 0;
List<string> posNames = new () { "Factor", "AnchorEnd", "Center", "Absolute" };
List<string> dimNames = new () { "Factor", "Fill", "Absolute" };
List<string> dimNames = new () { "Auto", "Factor", "Fill", "Absolute" };
Application.Init (new FakeDriver ());
var top = new Toplevel ();
_viewClasses = GetAllViewClassesCollection ()
.OrderBy (t => t.Name)
.Select (t => new KeyValuePair<string, Type> (t.Name, t))
.ToDictionary (t => t.Key, t => t.Value);
_viewClasses = TestHelpers.GetAllViewClasses ().ToDictionary(t => t.Name);
_leftPane = new()
{
@@ -200,7 +204,7 @@ public class ScenarioTests
Title = "Size (Dim)"
};
radioItems = new [] { "Percent(width)", "Fill(width)", "Sized(width)" };
radioItems = new [] { "Auto()", "Percent(width)", "Fill(width)", "Sized(width)" };
label = new() { X = 0, Y = 0, Text = "width:" };
_sizeFrame.Add (label);
_wRadioGroup = new() { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
@@ -208,7 +212,7 @@ public class ScenarioTests
_sizeFrame.Add (_wText);
_sizeFrame.Add (_wRadioGroup);
radioItems = new [] { "Percent(height)", "Fill(height)", "Sized(height)" };
radioItems = new [] { "Auto()", "Percent(height)", "Fill(height)", "Sized(height)" };
label = new() { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "height:" };
_sizeFrame.Add (label);
_hText = new() { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
@@ -452,22 +456,6 @@ public class ScenarioTests
void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
List<Type> GetAllViewClassesCollection ()
{
List<Type> types = new ();
foreach (Type type in typeof (View).Assembly.GetTypes ()
.Where (
myType =>
myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
))
{
types.Add (type);
}
return types;
}
View CreateClass (Type type)
{
// If we are to create a generic Type

View File

@@ -4,26 +4,26 @@
namespace Terminal.Gui.ViewTests;
public class KeyboardEventTests (ITestOutputHelper output)
public class KeyboardEventTests (ITestOutputHelper output) : TestsAllViews
{
public static TheoryData<View, string> AllViews => TestHelpers.GetAllViewsTheoryData ();
/// <summary>
/// This tests that when a new key down event is sent to the view will fire the 3 key-down related
/// events: KeyDown, InvokingKeyBindings, and ProcessKeyDown. Note that KeyUp is independent.
/// </summary>
[Theory]
[MemberData (nameof (AllViews))]
public void AllViews_KeyDown_All_EventsFire (View view, string viewName)
[MemberData (nameof (AllViewTypes))]
public void AllViews_KeyDown_All_EventsFire (Type viewType)
{
var view = CreateInstanceIfNotGeneric (viewType);
if (view == null)
{
output.WriteLine ($"ERROR: Skipping generic view: {viewName}");
output.WriteLine ($"ERROR: Skipping generic view: {viewType}");
return;
}
output.WriteLine ($"Testing {viewName}");
output.WriteLine ($"Testing {viewType}");
var keyDown = false;
@@ -60,32 +60,32 @@ public class KeyboardEventTests (ITestOutputHelper output)
/// This tests that when a new key up event is sent to the view the view will fire the 1 key-up related event:
/// KeyUp
/// </summary>
[Fact]
public void AllViews_KeyUp_All_EventsFire ()
[Theory]
[MemberData (nameof (AllViewTypes))]
public void AllViews_KeyUp_All_EventsFire (Type viewType)
{
foreach (View view in TestHelpers.GetAllViews ())
var view = CreateInstanceIfNotGeneric (viewType);
if (view == null)
{
if (view == null)
{
output.WriteLine ($"ERROR: null view from {nameof (TestHelpers.GetAllViews)}");
output.WriteLine ($"ERROR: Generic view {viewType}");
continue;
}
output.WriteLine ($"Testing {view.GetType ().Name}");
var keyUp = false;
view.KeyUp += (s, a) =>
{
a.Handled = true;
keyUp = true;
};
Assert.True (view.NewKeyUpEvent (Key.A)); // this will be true because the KeyUp event handled it
Assert.True (keyUp);
view.Dispose ();
return;
}
output.WriteLine ($"Testing {view.GetType ().Name}");
var keyUp = false;
view.KeyUp += (s, a) =>
{
a.Handled = true;
keyUp = true;
};
Assert.True (view.NewKeyUpEvent (Key.A)); // this will be true because the KeyUp event handled it
Assert.True (keyUp);
view.Dispose ();
}
[Theory]

View File

@@ -538,5 +538,85 @@ public class DimAutoTests
Assert.Equal (expectedSuperWidth, superView.Frame.Width);
}
[Theory]
[InlineData (0, 1, 1)]
[InlineData (1, 1, 1)]
[InlineData (9, 1, 1)]
[InlineData (10, 1, 1)]
[InlineData (0, 10, 10)]
[InlineData (1, 10, 10)]
[InlineData (9, 10, 10)]
[InlineData (10, 10, 10)]
public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
{
var superView = new View
{
X = 0,
Y = 0,
Width = 10,
Height = 1,
ValidatePosDim = true
};
var subView = new View
{
Text = new string ('*', textLen),
X = subX,
Y = 0,
Width = Dim.Auto (Dim.DimAutoStyle.Text),
Height = 1,
ValidatePosDim = true
};
superView.Add (subView);
superView.BeginInit ();
superView.EndInit ();
superView.SetRelativeLayout (superView.ContentSize);
superView.LayoutSubviews ();
Assert.Equal (expectedSubWidth, subView.Frame.Width);
}
[Theory]
[InlineData (0, 1, 1)]
[InlineData (1, 1, 1)]
[InlineData (9, 1, 1)]
[InlineData (10, 1, 1)]
[InlineData (0, 10, 10)]
[InlineData (1, 10, 10)]
[InlineData (9, 10, 10)]
[InlineData (10, 10, 10)]
public void Width_Auto_Subviews_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
{
var superView = new View
{
X = 0,
Y = 0,
Width = 10,
Height = 1,
ValidatePosDim = true
};
var subView = new View
{
Text = new string ('*', textLen),
X = subX,
Y = 0,
Width = Dim.Auto (Dim.DimAutoStyle.Subviews),
Height = 1,
ValidatePosDim = true
};
superView.Add (subView);
superView.BeginInit ();
superView.EndInit ();
superView.SetRelativeLayout (superView.ContentSize);
superView.LayoutSubviews ();
Assert.Equal (expectedSubWidth, subView.Frame.Width);
}
// Test variations of Frame
}

View File

@@ -620,66 +620,66 @@ public class LayoutTests
sub2.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void TrySetHeight_ForceValidatePosDim ()
{
var top = new View { X = 0, Y = 0, Height = 20 };
//[Fact]
//[AutoInitShutdown]
//public void TrySetHeight_ForceValidatePosDim ()
//{
// var top = new View { X = 0, Y = 0, Height = 20 };
var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
top.Add (v);
// var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
// top.Add (v);
Assert.False (v.TrySetHeight (10, out int rHeight));
Assert.Equal (10, rHeight);
// Assert.False (v.TrySetHeight (10, out int rHeight));
// Assert.Equal (10, rHeight);
v.Height = Dim.Fill (1);
Assert.False (v.TrySetHeight (10, out rHeight));
Assert.Equal (9, rHeight);
// v.Height = Dim.Fill (1);
// Assert.False (v.TrySetHeight (10, out rHeight));
// Assert.Equal (9, rHeight);
v.Height = 0;
Assert.True (v.TrySetHeight (10, out rHeight));
Assert.Equal (10, rHeight);
Assert.False (v.IsInitialized);
// v.Height = 0;
// Assert.True (v.TrySetHeight (10, out rHeight));
// Assert.Equal (10, rHeight);
// Assert.False (v.IsInitialized);
var toplevel = new Toplevel ();
toplevel.Add (top);
Application.Begin (toplevel);
// var toplevel = new Toplevel ();
// toplevel.Add (top);
// Application.Begin (toplevel);
Assert.True (v.IsInitialized);
// Assert.True (v.IsInitialized);
v.Height = 15;
Assert.True (v.TrySetHeight (5, out rHeight));
Assert.Equal (5, rHeight);
}
// v.Height = 15;
// Assert.True (v.TrySetHeight (5, out rHeight));
// Assert.Equal (5, rHeight);
//}
[Fact]
[AutoInitShutdown]
public void TrySetWidth_ForceValidatePosDim ()
{
var top = new View { X = 0, Y = 0, Width = 80 };
//[Fact]
//[AutoInitShutdown]
//public void TrySetWidth_ForceValidatePosDim ()
//{
// var top = new View { X = 0, Y = 0, Width = 80 };
var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
top.Add (v);
// var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
// top.Add (v);
Assert.False (v.TrySetWidth (70, out int rWidth));
Assert.Equal (70, rWidth);
// Assert.False (v.TrySetWidth (70, out int rWidth));
// Assert.Equal (70, rWidth);
v.Width = Dim.Fill (1);
Assert.False (v.TrySetWidth (70, out rWidth));
Assert.Equal (69, rWidth);
// v.Width = Dim.Fill (1);
// Assert.False (v.TrySetWidth (70, out rWidth));
// Assert.Equal (69, rWidth);
v.Width = 0;
Assert.True (v.TrySetWidth (70, out rWidth));
Assert.Equal (70, rWidth);
Assert.False (v.IsInitialized);
// v.Width = 0;
// Assert.True (v.TrySetWidth (70, out rWidth));
// Assert.Equal (70, rWidth);
// Assert.False (v.IsInitialized);
var toplevel = new Toplevel ();
toplevel.Add (top);
Application.Begin (toplevel);
// var toplevel = new Toplevel ();
// toplevel.Add (top);
// Application.Begin (toplevel);
Assert.True (v.IsInitialized);
v.Width = 75;
Assert.True (v.TrySetWidth (60, out rWidth));
Assert.Equal (60, rWidth);
}
// Assert.True (v.IsInitialized);
// v.Width = 75;
// Assert.True (v.TrySetWidth (60, out rWidth));
// Assert.Equal (60, rWidth);
//}
}

View File

@@ -3,7 +3,7 @@ using Xunit.Abstractions;
namespace Terminal.Gui.ViewTests;
public class MouseTests (ITestOutputHelper output)
public class MouseTests (ITestOutputHelper output) : TestsAllViews
{
[Theory]
[InlineData (false, false, false)]
@@ -93,30 +93,29 @@ public class MouseTests (ITestOutputHelper output)
Assert.Equal (mouseFlagsFromEvent, expectedMouseFlagsFromEvent);
}
public static TheoryData<View, string> AllViews => TestHelpers.GetAllViewsTheoryData ();
[Theory]
[MemberData (nameof (AllViews))]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Enter_Leave_Events (View view, string viewName)
public void AllViews_Enter_Leave_Events (Type viewType)
{
var view = CreateInstanceIfNotGeneric (viewType);
if (view == null)
{
output.WriteLine ($"Ignoring {viewName} - It's a Generic");
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
return;
}
if (!view.CanFocus)
{
output.WriteLine ($"Ignoring {viewName} - It can't focus.");
output.WriteLine ($"Ignoring {viewType} - It can't focus.");
return;
}
if (view is Toplevel && ((Toplevel)view).Modal)
{
output.WriteLine ($"Ignoring {viewName} - It's a Modal Toplevel");
output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
return;
}
@@ -189,26 +188,28 @@ public class MouseTests (ITestOutputHelper output)
[Theory]
[MemberData (nameof (AllViews))]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Enter_Leave_Events_Visible_False (View view, string viewName)
public void AllViews_Enter_Leave_Events_Visible_False (Type viewType)
{
var view = CreateInstanceIfNotGeneric (viewType);
if (view == null)
{
output.WriteLine ($"Ignoring {viewName} - It's a Generic");
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
return;
}
if (!view.CanFocus)
{
output.WriteLine ($"Ignoring {viewName} - It can't focus.");
output.WriteLine ($"Ignoring {viewType} - It can't focus.");
return;
}
if (view is Toplevel && ((Toplevel)view).Modal)
{
output.WriteLine ($"Ignoring {viewName} - It's a Modal Toplevel");
output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
return;
}
@@ -304,12 +305,14 @@ public class MouseTests (ITestOutputHelper output)
}
[Theory]
[MemberData (nameof (AllViews))]
public void AllViews_NewMouseEvent_Enabled_False_Does_Not_Set_Handled (View view, string viewName)
[MemberData (nameof (AllViewTypes))]
public void AllViews_NewMouseEvent_Enabled_False_Does_Not_Set_Handled (Type viewType)
{
var view = CreateInstanceIfNotGeneric (viewType);
if (view == null)
{
output.WriteLine ($"Ignoring {viewName} - It's a Generic");
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
return;
}
@@ -321,12 +324,14 @@ public class MouseTests (ITestOutputHelper output)
}
[Theory]
[MemberData (nameof (AllViews))]
public void AllViews_NewMouseEvent_Clicked_Enabled_False_Does_Not_Set_Handled (View view, string viewName)
[MemberData (nameof (AllViewTypes))]
public void AllViews_NewMouseEvent_Clicked_Enabled_False_Does_Not_Set_Handled (Type viewType)
{
var view = CreateInstanceIfNotGeneric (viewType);
if (view == null)
{
output.WriteLine ($"Ignoring {viewName} - It's a Generic");
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
return;
}

View File

@@ -192,10 +192,8 @@ public class AutoSizeFalseTests
Assert.Equal ("Absolute(1)", view.Height.ToString ());
view.AutoSize = true;
// There's no Text, so the view should be sized (0, 0)
Assert.Equal ("Absolute(0)", view.Width.ToString ());
Assert.Equal ("Absolute(0)", view.Height.ToString ());
Assert.Equal (Dim.Auto(Dim.DimAutoStyle.Text), view.Width);
Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view.Height);
view.AutoSize = false;
Assert.Equal ("Absolute(0)", view.Width.ToString ());

View File

@@ -1,3 +1,4 @@
using System.Reflection.Emit;
using System.Text;
using Xunit.Abstractions;
@@ -1123,11 +1124,11 @@ public class AutoSizeTrueTests
{
var text = "Label";
var label = new Label { Text = text };
Assert.Equal ("Absolute(1)", label.Height.ToString ());
Assert.Equal (Dim.Auto(Dim.DimAutoStyle.Text), label.Height);
label.AutoSize = false;
label.Width = Dim.Fill () - text.Length;
label.Height = 1;
Assert.Equal ("Absolute(1)", label.Height.ToString ());
Assert.Equal (Dim.Sized (1), label.Height);
var win = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (label);
@@ -1255,8 +1256,8 @@ public class AutoSizeTrueTests
Text = "Say Hello view4 你",
AutoSize = true,
//Width = 10,
//Height = 5,
//Width = 1,
//Height = 18,
TextDirection = TextDirection.TopBottom_LeftRight,
ValidatePosDim = true
};
@@ -1291,26 +1292,26 @@ public class AutoSizeTrueTests
Assert.False (view5.IsInitialized);
Assert.True (view1.AutoSize);
Assert.Equal (new (0, 0, 18, 1), view1.Frame);
Assert.Equal ("Absolute(18)", view1.Width.ToString ());
Assert.Equal ("Absolute(1)", view1.Height.ToString ());
Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view1.Width);
Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view1.Height);
Assert.True (view2.AutoSize);
Assert.Equal ("Say Hello view2 你".GetColumns (), view2.Width);
Assert.Equal (18, view2.Width);
Assert.Equal (new (0, 0, 18, 5), view2.Frame);
Assert.Equal ("Absolute(18)", view2.Width.ToString ());
Assert.Equal ("Absolute(5)", view2.Height.ToString ());
Assert.Equal ("Say Hello view2 你".GetColumns (), view2.Frame.Width);
Assert.Equal (18, view2.Frame.Width);
Assert.Equal (new (0, 0, 18, 1), view2.Frame);
Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view2.Width);
Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view2.Height);
Assert.True (view3.AutoSize);
Assert.Equal (new (0, 0, 18, 1), view3.Frame); // BUGBUG: AutoSize = true, so the height should be 1.
Assert.Equal ("Absolute(18)", view2.Width.ToString ());
Assert.Equal ("Absolute(1)", view3.Height.ToString ());
Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view3.Width);
Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view3.Height);
// Vertical text
Assert.True (view4.AutoSize);
Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view4.Width);
Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view4.Height);
Assert.Equal ("Say Hello view4 你".GetColumns (), view4.Frame.Height);
Assert.Equal (new (0, 0, 1, 17), view4.Frame);
Assert.Equal ("Say Hello view4 你".GetColumns (), view2.Width);
Assert.Equal (18, view2.Width);
Assert.Equal (new (0, 0, 18, 17), view4.Frame);
Assert.Equal ("Absolute(18)", view4.Width.ToString ());
Assert.Equal ("Absolute(17)", view4.Height.ToString ());
Assert.True (view5.AutoSize);
Assert.Equal (new (0, 0, 18, 17), view5.Frame);
Assert.True (view6.AutoSize);
@@ -1326,29 +1327,17 @@ public class AutoSizeTrueTests
Assert.True (view5.IsInitialized);
Assert.True (view1.AutoSize);
Assert.Equal (new (0, 0, 18, 1), view1.Frame);
Assert.Equal ("Absolute(18)", view1.Width.ToString ());
Assert.Equal ("Absolute(1)", view1.Height.ToString ());
Assert.True (view2.AutoSize);
Assert.Equal (new (0, 0, 18, 5), view2.Frame);
Assert.Equal ("Absolute(18)", view2.Width.ToString ());
Assert.Equal ("Absolute(5)", view2.Height.ToString ());
Assert.True (view3.AutoSize);
Assert.Equal (new (0, 0, 18, 1), view3.Frame); // BUGBUG: AutoSize = true, so the height should be 1.
Assert.Equal ("Absolute(18)", view5.Width.ToString ());
Assert.Equal ("Absolute(1)", view3.Height.ToString ());
Assert.True (view4.AutoSize);
Assert.Equal (new (0, 0, 18, 17), view4.Frame);
Assert.Equal ("Absolute(18)", view5.Width.ToString ());
Assert.Equal ("Absolute(17)", view4.Height.ToString ());
Assert.True (view5.AutoSize);
Assert.Equal (new (0, 0, 18, 17), view5.Frame);
Assert.Equal ("Absolute(18)", view5.Width.ToString ());
Assert.Equal ("Absolute(17)", view5.Height.ToString ());
Assert.True (view6.AutoSize);
Assert.Equal (new (0, 0, 2, 17), view6.Frame); // BUGBUG: AutoSize = true, so the Width should be 2.
Assert.Equal ("Absolute(2)", view6.Width.ToString ());
Assert.Equal ("Absolute(17)", view6.Height.ToString ());
}
[Fact]
@@ -1574,7 +1563,7 @@ Y
label.Text = "Hello";
Application.Refresh ();
Assert.Equal (new (0, 0, 1, 5), label.Frame); // BUGBUG: AutoSize = true, so the Width should be 1.
Assert.Equal (new (0, 0, 1, 5), label.Frame);
var expected = @"
HX
@@ -2307,69 +2296,69 @@ Y
Application.End (rs);
}
[Fact]
public void GetCurrentHeight_TrySetHeight ()
{
var top = new View { X = 0, Y = 0, Height = 20 };
//[Fact]
//public void GetCurrentHeight_TrySetHeight ()
//{
// var top = new View { X = 0, Y = 0, Height = 20 };
var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
top.Add (v);
top.BeginInit ();
top.EndInit ();
top.LayoutSubviews ();
// var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
// top.Add (v);
// top.BeginInit ();
// top.EndInit ();
// top.LayoutSubviews ();
Assert.False (v.AutoSize);
Assert.False (v.TrySetHeight (0, out _));
Assert.Equal (20, v.Frame.Height);
// Assert.False (v.AutoSize);
// Assert.False (v.TrySetHeight (0, out _));
// Assert.Equal (20, v.Frame.Height);
v.Height = Dim.Fill (1);
top.LayoutSubviews ();
// v.Height = Dim.Fill (1);
// top.LayoutSubviews ();
Assert.False (v.TrySetHeight (0, out _));
Assert.True (v.Height is Dim.DimFill);
Assert.Equal (19, v.Frame.Height);
// Assert.False (v.TrySetHeight (0, out _));
// Assert.True (v.Height is Dim.DimFill);
// Assert.Equal (19, v.Frame.Height);
v.AutoSize = true;
top.LayoutSubviews ();
// v.AutoSize = true;
// top.LayoutSubviews ();
Assert.True (v.TrySetHeight (0, out _));
Assert.True (v.Height is Dim.DimAbsolute);
Assert.Equal (0, v.Frame.Height); // No text, so height is 0
top.Dispose ();
}
// Assert.True (v.TrySetHeight (0, out _));
// Assert.True (v.Height is Dim.DimAbsolute);
// Assert.Equal (0, v.Frame.Height); // No text, so height is 0
// top.Dispose ();
//}
[Fact]
[TestRespondersDisposed]
public void GetCurrentWidth_TrySetWidth ()
{
var top = new View { X = 0, Y = 0, Width = 80 };
//[Fact]
//[TestRespondersDisposed]
//public void GetCurrentWidth_TrySetWidth ()
//{
// var top = new View { X = 0, Y = 0, Width = 80 };
var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
top.Add (v);
top.BeginInit ();
top.EndInit ();
top.LayoutSubviews ();
// var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
// top.Add (v);
// top.BeginInit ();
// top.EndInit ();
// top.LayoutSubviews ();
Assert.False (v.AutoSize);
Assert.False (v.TrySetWidth (0, out _));
Assert.True (v.Width is Dim.DimFill);
Assert.Equal (80, v.Frame.Width);
// Assert.False (v.AutoSize);
// Assert.False (v.TrySetWidth (0, out _));
// Assert.True (v.Width is Dim.DimFill);
// Assert.Equal (80, v.Frame.Width);
v.Width = Dim.Fill (1);
top.LayoutSubviews ();
// v.Width = Dim.Fill (1);
// top.LayoutSubviews ();
Assert.False (v.TrySetWidth (0, out _));
Assert.True (v.Width is Dim.DimFill);
Assert.Equal (79, v.Frame.Width);
// Assert.False (v.TrySetWidth (0, out _));
// Assert.True (v.Width is Dim.DimFill);
// Assert.Equal (79, v.Frame.Width);
v.AutoSize = true;
top.LayoutSubviews ();
// v.AutoSize = true;
// top.LayoutSubviews ();
Assert.True (v.TrySetWidth (0, out _));
Assert.True (v.Width is Dim.DimAbsolute);
Assert.Equal (0, v.Frame.Width); // No text, so width is 0
top.Dispose ();
}
// Assert.True (v.TrySetWidth (0, out _));
// Assert.True (v.Width is Dim.DimAbsolute);
// Assert.Equal (0, v.Frame.Width); // No text, so width is 0
// top.Dispose ();
//}
// [Fact]
// [AutoInitShutdown]
@@ -2735,7 +2724,7 @@ Y
view.Text = "01234567890123456789";
Assert.True (view.AutoSize);
Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
Assert.Equal (new (0, 0, 20, 1), view.Frame);
Assert.Equal ("Absolute(0)", view.X.ToString ());
Assert.Equal ("Absolute(0)", view.Y.ToString ());
@@ -2813,68 +2802,68 @@ Y
Application.End (rs);
}
[Fact]
[AutoInitShutdown]
public void TrySetHeight_ForceValidatePosDim ()
{
var top = new View { X = 0, Y = 0, Height = 20 };
//[Fact]
//[AutoInitShutdown]
//public void TrySetHeight_ForceValidatePosDim ()
//{
// var top = new View { X = 0, Y = 0, Height = 20 };
var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
top.Add (v);
// var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
// top.Add (v);
Assert.False (v.TrySetHeight (10, out int rHeight));
Assert.Equal (10, rHeight);
// Assert.False (v.TrySetHeight (10, out int rHeight));
// Assert.Equal (10, rHeight);
v.Height = Dim.Fill (1);
Assert.False (v.TrySetHeight (10, out rHeight));
Assert.Equal (9, rHeight);
// v.Height = Dim.Fill (1);
// Assert.False (v.TrySetHeight (10, out rHeight));
// Assert.Equal (9, rHeight);
v.Height = 0;
Assert.True (v.TrySetHeight (10, out rHeight));
Assert.Equal (10, rHeight);
Assert.False (v.IsInitialized);
// v.Height = 0;
// Assert.True (v.TrySetHeight (10, out rHeight));
// Assert.Equal (10, rHeight);
// Assert.False (v.IsInitialized);
var toplevel = new Toplevel ();
toplevel.Add (top);
Application.Begin (toplevel);
// var toplevel = new Toplevel ();
// toplevel.Add (top);
// Application.Begin (toplevel);
Assert.True (v.IsInitialized);
// Assert.True (v.IsInitialized);
v.Height = 15;
Assert.True (v.TrySetHeight (5, out rHeight));
Assert.Equal (5, rHeight);
}
// v.Height = 15;
// Assert.True (v.TrySetHeight (5, out rHeight));
// Assert.Equal (5, rHeight);
//}
[Fact]
[AutoInitShutdown]
public void TrySetWidth_ForceValidatePosDim ()
{
var top = new View { X = 0, Y = 0, Width = 80 };
//[Fact]
//[AutoInitShutdown]
//public void TrySetWidth_ForceValidatePosDim ()
//{
// var top = new View { X = 0, Y = 0, Width = 80 };
var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
top.Add (v);
// var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
// top.Add (v);
Assert.False (v.TrySetWidth (70, out int rWidth));
Assert.Equal (70, rWidth);
// Assert.False (v.TrySetWidth (70, out int rWidth));
// Assert.Equal (70, rWidth);
v.Width = Dim.Fill (1);
Assert.False (v.TrySetWidth (70, out rWidth));
Assert.Equal (69, rWidth);
// v.Width = Dim.Fill (1);
// Assert.False (v.TrySetWidth (70, out rWidth));
// Assert.Equal (69, rWidth);
v.Width = 0;
Assert.True (v.TrySetWidth (70, out rWidth));
Assert.Equal (70, rWidth);
Assert.False (v.IsInitialized);
// v.Width = 0;
// Assert.True (v.TrySetWidth (70, out rWidth));
// Assert.Equal (70, rWidth);
// Assert.False (v.IsInitialized);
var toplevel = new Toplevel ();
toplevel.Add (top);
Application.Begin (toplevel);
// var toplevel = new Toplevel ();
// toplevel.Add (top);
// Application.Begin (toplevel);
Assert.True (v.IsInitialized);
v.Width = 75;
Assert.True (v.TrySetWidth (60, out rWidth));
Assert.Equal (60, rWidth);
}
// Assert.True (v.IsInitialized);
// v.Width = 75;
// Assert.True (v.TrySetWidth (60, out rWidth));
// Assert.Equal (60, rWidth);
//}
[Theory]
[AutoInitShutdown]

View File

@@ -160,6 +160,7 @@ public class ViewTests
{
Assert.True (v.AutoSize);
Assert.False (v.CanFocus);
// The text is 100 characters long, but Dim.Auto constrains to SuperView, so it should be truncated.
Assert.Equal (new Rectangle (0, 0, 100, 1), v.Frame);
}
else
@@ -442,7 +443,7 @@ At 0,0
tv.DrawContentComplete += (s, e) => tvCalled = true;
var top = new Toplevel ();
top.Add (view, tv);
top.Add (view, tv);
Application.Begin (top);
Assert.True (viewCalled);
@@ -741,7 +742,7 @@ At 0,0
view.EndInit ();
view.Draw ();
TestHelpers.AssertDriverContentsWithFrameAre ( text, _output);
TestHelpers.AssertDriverContentsWithFrameAre (text, _output);
}
[Fact]
@@ -832,7 +833,7 @@ At 0,0
Text = "Vertical View", TextDirection = TextDirection.TopBottom_LeftRight, AutoSize = true
}; // BUGBUG: AutoSize or Height need be set
Assert.NotNull (r);
Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
// BUGBUG: IsInitialized must be true to process calculation
r.BeginInit ();
@@ -1209,7 +1210,8 @@ At 0,0
Assert.True (acceptInvoked);
return;
void ViewOnAccept (object sender, CancelEventArgs e) {
void ViewOnAccept (object sender, CancelEventArgs e)
{
acceptInvoked = true;
e.Cancel = true;
}

View File

@@ -1,22 +1,24 @@
using System.Reflection;
using System.Collections;
using System.Reflection;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewsTests;
public class AllViewsTests (ITestOutputHelper output)
public class AllViewsTests (ITestOutputHelper output) : TestsAllViews
{
// TODO: Update all these tests to use AllViews like AllViews_Center_Properly does
public static TheoryData<View, string> AllViews => TestHelpers.GetAllViewsTheoryData ();
[Theory]
[MemberData (nameof (AllViews))]
public void AllViews_Center_Properly (View view, string viewName)
[MemberData (nameof (AllViewTypes))]
public void AllViews_Center_Properly (Type viewType)
{
var view = (View)CreateInstanceIfNotGeneric (viewType);
// See https://github.com/gui-cs/Terminal.Gui/issues/3156
if (view == null)
{
output.WriteLine ($"Ignoring {viewName} - It's a Generic");
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
Application.Shutdown ();
return;
@@ -55,102 +57,91 @@ public class AllViewsTests (ITestOutputHelper output)
}
[Fact]
public void AllViews_Enter_Leave_Events ()
[Theory]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Enter_Leave_Events (Type viewType)
{
foreach (Type type in TestHelpers.GetAllViewClasses ())
var vType = (View)CreateInstanceIfNotGeneric (viewType);
if (vType == null)
{
output.WriteLine ($"Testing {type.Name}");
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
Application.Init (new FakeDriver ());
return;
}
Toplevel top = new ();
View vType = TestHelpers.CreateViewFromType (type, type.GetConstructor (Array.Empty<Type> ()));
Application.Init (new FakeDriver ());
if (vType == null)
{
output.WriteLine ($"Ignoring {type} - It's a Generic");
top.Dispose ();
Application.Shutdown ();
Toplevel top = new ();
continue;
}
vType.AutoSize = false;
vType.X = 0;
vType.Y = 0;
vType.Width = 10;
vType.Height = 1;
vType.AutoSize = false;
vType.X = 0;
vType.Y = 0;
vType.Width = 10;
vType.Height = 1;
var view = new View
{
X = 0,
Y = 1,
Width = 10,
Height = 1,
CanFocus = true
};
var vTypeEnter = 0;
var vTypeLeave = 0;
var viewEnter = 0;
var viewLeave = 0;
var view = new View
{
X = 0,
Y = 1,
Width = 10,
Height = 1,
CanFocus = true
};
var vTypeEnter = 0;
var vTypeLeave = 0;
var viewEnter = 0;
var viewLeave = 0;
vType.Enter += (s, e) => vTypeEnter++;
vType.Leave += (s, e) => vTypeLeave++;
view.Enter += (s, e) => viewEnter++;
view.Leave += (s, e) => viewLeave++;
vType.Enter += (s, e) => vTypeEnter++;
vType.Leave += (s, e) => vTypeLeave++;
view.Enter += (s, e) => viewEnter++;
view.Leave += (s, e) => viewLeave++;
top.Add (vType, view);
Application.Begin (top);
top.Add (vType, view);
Application.Begin (top);
if (!vType.CanFocus || (vType is Toplevel && ((Toplevel)vType).Modal))
{
top.Dispose ();
Application.Shutdown ();
if (!vType.CanFocus || (vType is Toplevel && ((Toplevel)vType).Modal))
{
top.Dispose ();
Application.Shutdown ();
return;
}
continue;
}
if (vType is TextView)
if (vType is TextView)
{
top.NewKeyDownEvent (Key.Tab.WithCtrl);
}
else if (vType is DatePicker)
{
for (var i = 0; i < 4; i++)
{
top.NewKeyDownEvent (Key.Tab.WithCtrl);
}
else if (vType is DatePicker)
{
for (var i = 0; i < 4; i++)
{
top.NewKeyDownEvent (Key.Tab.WithCtrl);
}
}
else
{
top.NewKeyDownEvent (Key.Tab);
}
top.NewKeyDownEvent (Key.Tab);
Assert.Equal (2, vTypeEnter);
Assert.Equal (1, vTypeLeave);
Assert.Equal (1, viewEnter);
Assert.Equal (1, viewLeave);
top.Dispose ();
Application.Shutdown ();
}
else
{
top.NewKeyDownEvent (Key.Tab);
}
top.NewKeyDownEvent (Key.Tab);
Assert.Equal (2, vTypeEnter);
Assert.Equal (1, vTypeLeave);
Assert.Equal (1, viewEnter);
Assert.Equal (1, viewLeave);
top.Dispose ();
Application.Shutdown ();
}
[Fact]
public void AllViews_Tests_All_Constructors ()
[Theory]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Tests_All_Constructors (Type viewType)
{
Application.Init (new FakeDriver ());
foreach (Type type in TestHelpers.GetAllViewClasses ())
{
Assert.True (Test_All_Constructors_Of_Type (type));
}
Application.Shutdown ();
Assert.True (Test_All_Constructors_Of_Type (viewType));
}
//[Fact]
@@ -179,57 +170,4 @@ public class AllViewsTests (ITestOutputHelper output)
return true;
}
// BUGBUG: This is a hack. We should figure out how to dynamically
// create the right type of argument for the constructor.
private static void AddArguments (Type paramType, List<object> pTypes)
{
if (paramType == typeof (Rectangle))
{
pTypes.Add (Rectangle.Empty);
}
else if (paramType == typeof (string))
{
pTypes.Add (string.Empty);
}
else if (paramType == typeof (int))
{
pTypes.Add (0);
}
else if (paramType == typeof (bool))
{
pTypes.Add (true);
}
else if (paramType.Name == "IList")
{
pTypes.Add (new List<object> ());
}
else if (paramType.Name == "View")
{
var top = new Toplevel ();
var view = new View ();
top.Add (view);
pTypes.Add (view);
}
else if (paramType.Name == "View[]")
{
pTypes.Add (new View [] { });
}
else if (paramType.Name == "Stream")
{
pTypes.Add (new MemoryStream ());
}
else if (paramType.Name == "String")
{
pTypes.Add (string.Empty);
}
else if (paramType.Name == "TreeView`1[T]")
{
pTypes.Add (string.Empty);
}
else
{
pTypes.Add (null);
}
}
}