Files
Terminal.Gui/Tests/UnitTestsParallelizable/Views/ShortcutTests.cs
Tig 3e2eebfd2c Fixes #4057 - MASSIVE! Fully implements ColorScheme->Scheme + VisualRole + Colors.->SchemeManager. (#4062)
* touching publish.yml

* ColorScheme->Scheme

* ColorScheme->Scheme 2

* Prototype of GetAttributeForRole

* Badly broke CM

* Further Badly broke CM

* Refactored CM big-time. View still broken

* All unit test pass again. Tons added. CM is still WIP, but Schemes is not mostly refactored and working.

* Actually:
All unit test pass again.
Tons added.
CM is still WIP, but Schemes is not mostly refactored and working.

* Bug fixes.
DeepMemberWiseClone cleanup

* Further cleanup of Scope<T>, ConfigProperty, etc.

* Made ConfigManager thread safe.

* WIP: Broken

* WIP: new deep clone impl

* WIP: new deep clone impl is done. Now fixing CM

* WIP:
- config.md
- Working on AOT clean up
- Core CM is broken; but known.

* WIP

* Merged.
Removed CM from Application.Init

* WIP

* More WIP; Less broke

* All CM unit tests pass... Not sure if it actually works though

* All unit tests pass... Themes are broken though in UI Cat

* CM Ready for review?

* Fixed failures due to TextStyles PR

* Working on Scheme/Attribute

* Working on Scheme/Attribute 2

* Working on Scheme/Attribute 3

* Working on Scheme/Attribute 4

* Working on Scheme/Attribute 5

* Working on Scheme/Attribute 6

* Added test to show how awful memory usage is

* Improved schema. Updated config.json

* Nade Scope<T> concurrentdictionary and added test to prove

* Made Themes ConcrurrentDictionary. Added bunches of tests

* Code cleanup

* Code cleanup 2

* Code cleanup 3

* Tweaking Scheme

* ClearJsonErrors

* ClearJsonErrors2

* Updated Attribute API

* It all (mostly) works!

* Skip odd unit test

* Messed with Themes

* Theme tweaks

* Code reorg. New .md stuff

* Fixed Enabled. Added mock driver

* Fixed a bunch of View.Enabled related issues

* Scheme -> Get/SetScheme()

* Cleanup

* Cleanup2

* Broke something

* Fixed everything

* Made CM.Enable better

* Text Style Scenario

* Added comments

* Fixed UI Catalog Theme Changing

* Fixed more dynamic CM update stuff

* Warning cleanup

* New Default Theme

* fixed unit test

* Refactoring Scheme and Attribute to fix inheritance

* more unit tests

* ConfigProperty is not updating schemes correctly

* All unit tests pass.
Code cleanup

* All unit tests pass.
Code cleanup2

* Fixed unit tests

* Upgraded TextField and TextView

* Fixed TextView !Enabled bug

* More updates to TextView. More unit tests for SchemeManager

* Upgraded CharMap

* API docs

* Fixe HexView API

* upgrade HexView

* Fixed shortcut KeyView

* Fixed more bugs. Added new themes

* updated themes

* upgraded Border

* Fixed themes memory usage...mostly

* Fixed themes memory usage...mostly2

* Fixed themes memory usage...2

* Fixed themes memory usage...3

* Added new colors

* Fixed GetHardCodedConfig bug

* Added Themes Scenario - WIP

* Added Themes Scenario

* Tweaked Themes Scenario

* Code cleanup

* Fixed json schmea

* updated deepdives

* updated deepdives

* Tweaked Themes Scenario

* Made Schemes a concurrent dict

* Test cleanup

* Thread safe ConfigProperty tests

* trying to make things more thread safe

* more trying to make things more thread safe

* Fixing bugs in shadowview

* Fixing bugs in shadowview 2

* Refactored GetViewsUnderMouse to GetViewsUnderLocation etc...

* Fixed dupe unit tests?

* Added better description of layout and coordiantes to deep dive

* Added better description of layout and coordiantes to deep dive

* Modified tests that call v2.AddTimeout; they were returning true which means restart the timer!
This was causing mac/linux unit test failures.
I think

* Fixed auto scheme.
Broke TextView/TextField selection

* Realized Attribute.IsExplicitlySet is stupid; just use nullable

* Fixed Attribute. Simplified. MOre theme testing

* Updated themes again

* GetViewsUnderMouse to GetViewsUnderLocation broke TransparentMouse.

* Fixing mouseunder bugs

* rewriting...

* All working again.
Shadows are now slick as snot.
GetViewsUnderLocation is rewritten to actually work and be readable.
Tons more low-level unit tests.
Margin is now actually ViewportSettings.Transparent.

* Code cleanup

* Code cleanup

* Code cleanup of color apis

* Fixed Hover/Highlight

* Update Examples/UICatalog/Scenarios/AllViewsTester.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/CharacterMap/CharacterMap.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/Clipping.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fixed race condition?

* reverted

* Simplified Attribute API by removing events from SetAttributeForRole

* Removed recursion from GetViewsAtLocation

* Removed unneeded code

* Code clean up.
Fixed Scheme bug.

* reverted temporary disable

* Adjusted scheme algo

* Upgraded TextValidateField

* Fixed TextValidate bugs

* Tweaks

* Frameview rounded border by default

* API doc cleanup

* Readme fix

* Addressed tznind feeback

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true 2

* cleanup

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-29 14:08:48 -06:00

443 lines
12 KiB
C#

using JetBrains.Annotations;
namespace Terminal.Gui.ViewsTests;
[Collection ("Global Test Setup")]
[TestSubject (typeof (Shortcut))]
public class ShortcutTests
{
[Fact]
public void Constructor_Defaults ()
{
var shortcut = new Shortcut ();
Assert.NotNull (shortcut);
Assert.True (shortcut.CanFocus);
Assert.IsType<DimAuto> (shortcut.Width);
Assert.IsType<DimAuto> (shortcut.Height);
}
[Fact]
public void Size_Defaults ()
{
var shortcut = new Shortcut ();
shortcut.Layout ();
Assert.Equal (2, shortcut.Frame.Width);
Assert.Equal (1, shortcut.Frame.Height);
Assert.Equal (2, shortcut.Viewport.Width);
Assert.Equal (1, shortcut.Viewport.Height);
Assert.Equal (0, shortcut.CommandView.Viewport.Width);
Assert.Equal (1, shortcut.CommandView.Viewport.Height);
Assert.Equal (0, shortcut.HelpView.Viewport.Width);
Assert.Equal (0, shortcut.HelpView.Viewport.Height);
Assert.Equal (0, shortcut.KeyView.Viewport.Width);
Assert.Equal (0, shortcut.KeyView.Viewport.Height);
// 0123456789
// " 0 A "
shortcut = new ()
{
Key = Key.A,
HelpText = "0"
};
shortcut.Layout ();
Assert.Equal (8, shortcut.Frame.Width);
Assert.Equal (1, shortcut.Frame.Height);
Assert.Equal (8, shortcut.Viewport.Width);
Assert.Equal (1, shortcut.Viewport.Height);
Assert.Equal (0, shortcut.CommandView.Viewport.Width);
Assert.Equal (1, shortcut.CommandView.Viewport.Height);
Assert.Equal (1, shortcut.HelpView.Viewport.Width);
Assert.Equal (1, shortcut.HelpView.Viewport.Height);
Assert.Equal (1, shortcut.KeyView.Viewport.Width);
Assert.Equal (1, shortcut.KeyView.Viewport.Height);
// 0123456789
// " C 0 A "
shortcut = new ()
{
Title = "C",
Key = Key.A,
HelpText = "0"
};
shortcut.Layout ();
Assert.Equal (9, shortcut.Frame.Width);
Assert.Equal (1, shortcut.Frame.Height);
Assert.Equal (9, shortcut.Viewport.Width);
Assert.Equal (1, shortcut.Viewport.Height);
Assert.Equal (1, shortcut.CommandView.Viewport.Width);
Assert.Equal (1, shortcut.CommandView.Viewport.Height);
Assert.Equal (1, shortcut.HelpView.Viewport.Width);
Assert.Equal (1, shortcut.HelpView.Viewport.Height);
Assert.Equal (1, shortcut.KeyView.Viewport.Width);
Assert.Equal (1, shortcut.KeyView.Viewport.Height);
}
[Theory]
[InlineData ("", "", KeyCode.Null, 2)]
[InlineData ("C", "", KeyCode.Null, 3)]
[InlineData ("", "H", KeyCode.Null, 5)]
[InlineData ("", "", KeyCode.K, 5)]
[InlineData ("C", "", KeyCode.K, 6)]
[InlineData ("C", "H", KeyCode.Null, 6)]
[InlineData ("", "H", KeyCode.K, 8)]
[InlineData ("C", "H", KeyCode.K, 9)]
public void NaturalSize (string command, string help, KeyCode key, int expectedWidth)
{
var shortcut = new Shortcut
{
HelpText = help,
Key = key,
Title = command
};
shortcut.Layout ();
// |0123456789
// | C H K |
Assert.Equal (expectedWidth, shortcut.Frame.Width);
shortcut = new()
{
HelpText = help,
Title = command,
Key = key
};
shortcut.Layout ();
Assert.Equal (expectedWidth, shortcut.Frame.Width);
shortcut = new()
{
HelpText = help,
Key = key,
Title = command
};
shortcut.Layout ();
Assert.Equal (expectedWidth, shortcut.Frame.Width);
shortcut = new()
{
Key = key,
HelpText = help,
Title = command
};
shortcut.Layout ();
Assert.Equal (expectedWidth, shortcut.Frame.Width);
}
[Theory]
[InlineData (0, 0, 3, 3)]
[InlineData (1, 0, 3, 3)]
[InlineData (2, 0, 3, 3)]
[InlineData (3, 0, 3, 3)]
[InlineData (4, 0, 3, 3)]
[InlineData (5, 0, 3, 3)]
[InlineData (6, 0, 3, 3)]
[InlineData (7, 0, 3, 4)]
[InlineData (8, 0, 3, 5)]
[InlineData (9, 0, 3, 6)]
[InlineData (10, 0, 4, 7)]
[InlineData (11, 0, 5, 8)]
public void Set_Width_Layouts_Correctly (int width, int expectedCmdX, int expectedHelpX, int expectedKeyX)
{
var shortcut = new Shortcut
{
Width = width,
Title = "C",
Text = "H",
Key = Key.K
};
shortcut.Layout ();
// 01234
// -C--K
// 012345
// -C--K-
// 0123456
// -C-H-K-
// 01234567
// -C--H-K-
// 012345678
// -C--H--K-
// 0123456789
// -C--H--K-
Assert.Equal (expectedCmdX, shortcut.CommandView.Frame.X);
Assert.Equal (expectedHelpX, shortcut.HelpView.Frame.X);
Assert.Equal (expectedKeyX, shortcut.KeyView.Frame.X);
}
[Fact]
public void CommandView_Text_And_Title_Track ()
{
var shortcut = new Shortcut
{
Title = "T"
};
Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
shortcut = new ();
shortcut.CommandView = new ()
{
Text = "T"
};
Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
}
[Fact]
public void HelpText_And_Text_Are_The_Same ()
{
var shortcut = new Shortcut
{
Text = "H"
};
Assert.Equal (shortcut.Text, shortcut.HelpText);
shortcut = new ()
{
HelpText = "H"
};
Assert.Equal (shortcut.Text, shortcut.HelpText);
}
[Theory]
[InlineData (KeyCode.Null, "")]
[InlineData (KeyCode.F1, "F1")]
public void KeyView_Text_Tracks_Key (KeyCode key, string expected)
{
var shortcut = new Shortcut
{
Key = key
};
Assert.Equal (expected, shortcut.KeyView.Text);
}
// Test Key
[Fact]
public void Key_Defaults_To_Empty ()
{
var shortcut = new Shortcut ();
Assert.Equal (Key.Empty, shortcut.Key);
}
[Fact]
public void Key_Can_Be_Set ()
{
var shortcut = new Shortcut ();
shortcut.Key = Key.F1;
Assert.Equal (Key.F1, shortcut.Key);
}
[Fact]
public void Key_Can_Be_Set_To_Empty ()
{
var shortcut = new Shortcut ();
shortcut.Key = Key.Empty;
Assert.Equal (Key.Empty, shortcut.Key);
}
[Fact]
public void Key_Set_Binds_Key_To_CommandView_Accept ()
{
var shortcut = new Shortcut ();
shortcut.Key = Key.F1;
// TODO:
}
[Fact]
public void Key_Changing_Removes_Previous_Binding ()
{
var shortcut = new Shortcut ();
shortcut.Key = Key.A;
Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
shortcut.Key = Key.B;
Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _));
Assert.True (shortcut.HotKeyBindings.TryGet (Key.B, out _));
}
// Test Key gets bound correctly
[Fact]
public void BindKeyToApplication_Defaults_To_HotKey ()
{
var shortcut = new Shortcut ();
Assert.False (shortcut.BindKeyToApplication);
}
[Fact]
public void BindKeyToApplication_Can_Be_Set ()
{
var shortcut = new Shortcut ();
shortcut.BindKeyToApplication = true;
Assert.True (shortcut.BindKeyToApplication);
}
[Fact]
public void BindKeyToApplication_Changing_Adjusts_KeyBindings ()
{
var shortcut = new Shortcut ();
shortcut.Key = Key.A;
Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
shortcut.BindKeyToApplication = true;
Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _));
Assert.True (Application.KeyBindings.TryGet (Key.A, out _));
shortcut.BindKeyToApplication = false;
Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
Assert.False (Application.KeyBindings.TryGet (Key.A, out _));
}
[Theory]
[InlineData (Orientation.Horizontal)]
[InlineData (Orientation.Vertical)]
public void Orientation_SetsCorrectly (Orientation orientation)
{
var shortcut = new Shortcut
{
Orientation = orientation
};
Assert.Equal (orientation, shortcut.Orientation);
}
[Theory]
[InlineData (AlignmentModes.StartToEnd)]
[InlineData (AlignmentModes.EndToStart)]
public void AlignmentModes_SetsCorrectly (AlignmentModes alignmentModes)
{
var shortcut = new Shortcut
{
AlignmentModes = alignmentModes
};
Assert.Equal (alignmentModes, shortcut.AlignmentModes);
}
[Fact]
public void Action_SetsAndGetsCorrectly ()
{
var actionInvoked = false;
var shortcut = new Shortcut
{
Action = () => { actionInvoked = true; }
};
shortcut.Action.Invoke ();
Assert.True (actionInvoked);
}
[Fact]
public void SubView_Visibility_Controlled_By_Removal ()
{
var shortcut = new Shortcut ();
Assert.True (shortcut.CommandView.Visible);
Assert.Contains (shortcut.CommandView, shortcut.SubViews);
Assert.True (shortcut.HelpView.Visible);
Assert.DoesNotContain (shortcut.HelpView, shortcut.SubViews);
Assert.True (shortcut.KeyView.Visible);
Assert.DoesNotContain (shortcut.KeyView, shortcut.SubViews);
shortcut.HelpText = "help";
Assert.True (shortcut.HelpView.Visible);
Assert.Contains (shortcut.HelpView, shortcut.SubViews);
Assert.True (shortcut.KeyView.Visible);
Assert.DoesNotContain (shortcut.KeyView, shortcut.SubViews);
shortcut.Key = Key.A;
Assert.True (shortcut.HelpView.Visible);
Assert.Contains (shortcut.HelpView, shortcut.SubViews);
Assert.True (shortcut.KeyView.Visible);
Assert.Contains (shortcut.KeyView, shortcut.SubViews);
shortcut.HelpView.Visible = false;
shortcut.ShowHide ();
Assert.False (shortcut.HelpView.Visible);
Assert.DoesNotContain (shortcut.HelpView, shortcut.SubViews);
Assert.True (shortcut.KeyView.Visible);
Assert.Contains (shortcut.KeyView, shortcut.SubViews);
shortcut.KeyView.Visible = false;
shortcut.ShowHide ();
Assert.False (shortcut.HelpView.Visible);
Assert.DoesNotContain (shortcut.HelpView, shortcut.SubViews);
Assert.False (shortcut.KeyView.Visible);
Assert.DoesNotContain (shortcut.KeyView, shortcut.SubViews);
}
[Fact]
public void Focus_CanFocus_Default_Is_True ()
{
Shortcut shortcut = new ();
shortcut.Key = Key.A;
shortcut.Text = "Help";
shortcut.Title = "Command";
Assert.True (shortcut.CanFocus);
Assert.False (shortcut.CommandView.CanFocus);
}
[Fact]
public void Focus_CanFocus_CommandView_Add_Tracks ()
{
Shortcut shortcut = new ();
Assert.True (shortcut.CanFocus);
Assert.False (shortcut.CommandView.CanFocus);
shortcut.CommandView = new () { CanFocus = true };
Assert.True (shortcut.CommandView.CanFocus);
shortcut.CommandView.CanFocus = true;
Assert.True (shortcut.CommandView.CanFocus);
shortcut.CanFocus = false;
Assert.False (shortcut.CanFocus);
Assert.True (shortcut.CommandView.CanFocus);
shortcut.CommandView.CanFocus = false;
Assert.False (shortcut.CanFocus);
Assert.False (shortcut.CommandView.CanFocus);
shortcut.CommandView.CanFocus = true;
Assert.False (shortcut.CanFocus);
Assert.True (shortcut.CommandView.CanFocus);
}
}