Files
Terminal.Gui/Tests/UnitTests/TestsAllViews.cs
Tig 47bcf1bf57 Partial on #2975 - Replaces Menu v1 in many places with v2 (#4040)
* touching publish.yml

* Fixed UICatalog bugs. Added fluent tests.

* marked v1 menu stuff as obsolte

* Tweaks.
Added View.GetSubMenus<type>().

* fixed unit tests

* general messing around

* general messing around

* Playing with Fluent

* ColorScheme tweaks

* WIP: ColorScheme tweaks

* Playing with Fluent

* Merged from laptop2

* Hacky-ish fixes to:
- #4016
- #4014

* Fixed Region bug preventing menus without borders from working

* Tweaks

* Fixed a bunch of CM issues

* Fixed OoptionSelector

* ip

* FixedCM issues

* Fixed CM issues2

* Revert "FixedCM issues"

This reverts commit dd6c6a70a3.

* Reverted stuff

* Found and fixed bug in AllViews_Center_Properly

* Fixed CM issues2

* removed menuv2 onapplied.
Changed how UICatalog Applys CM

* changed test time out to see if it helkps with ubuntu fails

* reset app on fail?

* back to 1500ms

* Made StatusBar nullable.

* Code Cleanup.

* HexEditor Code Cleanup.

* HexEditor Code Cleanup.

* Back to 3000ms. Sigh.

* Trying different logic

* Trying different logic2

* Fixed potential crash in runlop

* Fixed potential crash in runlop2

* Tweaked Spinner stuff

* Removed TabView from TextEffects scenario. Not needed and possible culprit.

* back to 2000ms

* WIP: Revamping menu scenarios

* Menu Scenario refinements.
Fixed a few bugs.
Code cleanup.

* fixed unit test

* Fixed warnings

* Fixed warnings2

* Fixed File.Exit

* WIP: Dealing with QuitKey struggles

* WIP: Dealing with QuitKey struggles 2

* WIP: Dealing with QuitKey struggles 3

* Fixed ListView collection nav bug

* Fixed a bunch of menu stuff.
Fixed Appv2 stuff.

* Lots of refactoring and fixing

* Lots of unit test issues

* Fixed DebugIDisposable issues

* Fixed release build issue

* Fixed release build issue 2

* DebugIDisposable -> EnableDebugIDisposableAsserts and more

* DebugIDisposable -> EnableDebugIDisposableAsserts and more 2

* Fixed Menus scenario - context menu

* Added @bdisp suggested assert. Commented it out as it breaks tests.

* Code cleanup

* Fixed disposed but

* Fixed UICatalog exit

* Fixed Unit test I broke.
Added 'Minimal' Theme that turns off all borders etc...
2025-04-24 05:17:58 -06:00

196 lines
5.7 KiB
C#

#nullable enable
using System.Drawing;
using System.Reflection;
using Terminal.Gui;
namespace UnitTests;
/// <summary>
/// Base class for tests that need to test all views.
/// </summary>
public class TestsAllViews
{
/// <summary>
/// Gets all view types.
/// </summary>
public static IEnumerable<object []> AllViewTypes =>
typeof (View).Assembly
.GetTypes ()
.Where (
type => type is { IsClass: true, IsAbstract: false, IsPublic: true }
&& (type.IsSubclassOf (typeof (View)) || type == typeof (View)))
.Select (type => new object [] { type });
/// <summary>
/// Creates an instance of a view if it is not a generic type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static View? CreateInstanceIfNotGeneric (Type type)
{
if (type.IsGenericType)
{
// Return null for generic types
return null;
}
return Activator.CreateInstance (type) as View;
}
/// <summary>
/// Gets a list of all view classes.
/// </summary>
/// <returns></returns>
public static List<Type> GetAllViewClasses ()
{
return typeof (View).Assembly.GetTypes ()
.Where (
myType => myType is { IsClass: true, IsAbstract: false, IsPublic: true }
&& myType.IsSubclassOf (typeof (View))
)
.ToList ();
}
/// <summary>
/// Creates a view from a type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="ctor">The constructor to call.</param>
/// <returns></returns>
public static View? CreateViewFromType (Type type, ConstructorInfo ctor)
{
View? viewType = null;
if (type is { IsGenericType: true, IsTypeDefinition: true })
{
List<Type> typeArguments = new ();
// use <object> or the original type if applicable
foreach (Type arg in type.GetGenericArguments ())
{
if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
{
typeArguments.Add (arg);
}
else
{
typeArguments.Add (typeof (object));
}
}
type = type.MakeGenericType (typeArguments.ToArray ());
// Ensure the type does not contain any generic parameters
if (type.ContainsGenericParameters)
{
Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
//throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
return null;
}
Assert.IsType (type, (View)Activator.CreateInstance (type)!);
}
else
{
ParameterInfo [] paramsInfo = ctor.GetParameters ();
Type paramType;
List<object> pTypes = new ();
if (type.IsGenericType)
{
foreach (Type args in type.GetGenericArguments ())
{
paramType = args.GetType ();
if (args.Name == "T")
{
pTypes.Add (typeof (object));
}
else
{
AddArguments (paramType, pTypes);
}
}
}
foreach (ParameterInfo p in paramsInfo)
{
paramType = p.ParameterType;
if (p.HasDefaultValue)
{
pTypes.Add (p.DefaultValue!);
}
else
{
AddArguments (paramType, pTypes);
}
}
if (type is { IsGenericType: true, IsTypeDefinition: false })
{
viewType = Activator.CreateInstance (type) as View;
}
else
{
viewType = (View)ctor.Invoke (pTypes.ToArray ());
}
Assert.IsType (type, viewType);
}
return viewType;
}
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!);
}
}
}