Files
Terminal.Gui/UnitTests/Views/ButtonTests.cs
Tig dcb3b359ad Fixes #2926 - Refactor KeyEvent and KeyEventEventArgs to simplify (#2927)
* Adds basic MainLoop unit tests

* Remove WinChange action from Curses

* Remove WinChange action from Curses

* Remove ProcessInput action from Windows MainLoop

* Simplified MainLoop/ConsoleDriver by making MainLoop internal and moving impt fns to Application

* Modernized Terminal resize events

* Modernized Terminal resize events

* Removed un used property

* for _isWindowsTerminal devenv->wininit; not sure what changed

* Modernized mouse/keyboard events (Action->EventHandler)

* Updated OnMouseEvent API docs

* Using WT_SESSION to detect WT

* removes hacky GetParentProcess

* Updates to fix #2634 (clear last line)

* removes hacky GetParentProcess2

* Addressed mac resize issue

* Addressed mac resize issue

* Removes ConsoleDriver.PrepareToRun, has Init return MainLoop

* Removes unneeded Attribute methods

* Removed GetProcesssName

* Removed GetProcesssName

* Refactored KeyEvent and KeyEventEventArgs into a single class

* Revert "Refactored KeyEvent and KeyEventEventArgs into a single class"

This reverts commit 88a00658db.

* Fixed key repeat issue; reverted stupidity on 1049/1047 confusion

* Updated CSI API Docs

* merge

* Rearranged Event.cs to Keyboard.cs and Mouse.cs

* Renamed KeyEventEventArgs KeyEventArgs

* temp renamed KeyEvent OldKeyEvent

* Merged KeyEvent into KeyEventArgs

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Added Responder.KeyPressed

* Removed unused references

* Fixed arg naming

* InvokeKeybindings->InvokeKeyBindings

* InvokeKeybindings->InvokeKeyBindings

* Fixed unit tests fail

* More progress on refactoring key input; still broken and probably wrong

* Moved OnKeyPressed out of Responder and made ProcessKeyPrssed non-virtual

* Updated API docs

* Moved key handling from Responder to View

* Updated API docs

* Updated HotKey API docs

* Updated shortcut API docs

* Fixed responder unit tests

* Removed Shortcut from View as it is not used

* Removed unneeded OnHotKey override from Button

* Fixed BackTab logic

* Button now uses Key Bindings exclusively

* Button now uses Key Bindings exclusively

* Updated keyboard.md docs

* Fixed unit tests to account for Toplevel handling default button

* Added View.InvokeCommand API

* Modernized RadioGroup

* Removed ColdKey

* Modernized (partially) StatusBar

* Worked around FileDialog issue with Ctrl-F

* Fixed driver unit test; view must be focused to reciev key pressed

* Application code cleanup

* Start on updaing menu

* Menu now mostly works

* Menu Select refinement

* Fixed known menu bugs!

* Enabled HotKey to cause focus- experimental

* Fixes #3022 & adds unit test to prove it

* Actually Fixes #3022 & adds unit test to prove it

* Working through hotkey issues

* Misc fixes

* removed hot/cold key stuff from Keys scenario

* Fixed scenarios

* Simplified shortcut string handling

* Modernized Checkbox

* Modernized TileView

* Updated API docs

* Updated API docs

* attempting to publish v2 docs

* Revert "attempting to publish v2 docs"

This reverts commit 59dcec111b.

* Playing with api docs

* Removed Key.BackTab

* Removed Caps/Scroll/Numlock

* Partial removal of keymodifiers - unit tests pass

* Partial removal of keymodifiers - broke netdriver somewhere

* WindowsDriver & added KeyEventArgsTests

* Fixing menu shortcut/hotkeys - broke Menu.cs into separate files

* Fixed MenuBar!

* Finished modernizing Menu/MenuBar

* Removed Key.a-z. Broke lots of stuff

* checkout@v4

* progress on key mapping and formatting

* VK tests are still failing

* Fixed some unit tests

* Added Hotkey and Keybinding unit tests

* fixed unit test

* All unit tests pass again...

* Fixed broken unit tests

* KeyEventArgs.KeyValue -> AsRune

* Fixed bugs. Still some broken

* Added KeyEventArgs.IsAlpha. Added KeyEventArgs.cast ops. Fixed bugs. Unit tests pass

* Fixed WindowsDriver

* Oops.

* Refactoring based on bdisp's help. Not complete!

* removed calling into subviews from OnKeyBindings

* removed calling into subviews from OnKeyBindings

* Improved View KeyEvent unit tests

* More hotkey unit tests

* BIg change - Got rid of KeyPress w/in Application/Drivers

* Unit tests now pass again

* Refreshed API docs

* Better HotKey logic. More progress. Getting close.

* Fixed handling of shifted chars like ö

* Minor code cleanup

* Minor code cleanup2

* Why is build Action failing?

* Why is build Action failing??

* upgraded to .net8 to try to fix weird CI/CD build errors

* upgraded to .net8 to try to fix weird CI/CD build errors2

* Disabling TextViewTests to diagnose build errors

* reenable TextViewTests to diagnose build errors

* Arrrrrrg

* Merged v2_develop

* Fixed uppercase accented keys in WindowsDriver

* Fixed key binding api docs

* Experimental impl of CommandScope.SubViews for MenuBar

* Removed dead code from application.cs

* Removed dead code from application.cs

* Removed dead code from ConsoleDriver.cs

* Cleaned up some key binding stuff

* Disabled Alt to activate menu for now

* Updated label commands

* Fixed menu bugs. Upgraded menu unit tests

* Fixed unit tests

* Working on NetDriver

* fixed netdriver

* Fixed issues called out by @bdisp CR

* fixed CursesDriver

* added todo to netdriver

* Cherry picked treeview test fix 1b415e5

* Fix NetDriver.

* CommandScope->KeyBindingScope

* Address some tznind feedback

* Refactored KeyBindings big time!

* Added key consts to KeyEventArgs and renamed Key to ConsoleDriverKey

* Fixed some API docs

* Moved ConsoleDriverKey to ConsoleDriver.cs

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* renamed file I forgot to rename before

* Updated name and API docs of KeyEventArgs.isAlpha

* Fixed issues with OnKeyUp not doing the right thing.

* Fixed MainLoop.Running never being used

* Fixed MainLoop.Running never being used - unit tests

* Claned up BUGBUG comments

* Disabled a unit test to see why ci/cd tests are failing

* Removed defunct commented code

* Removed more defunct commented code

* Re-eanbled unit test; jsut removing one test case...

* Disabled more...

* Renambed Global->Applicaton and updated scope API docs

* Disabled more unit tests...

* Removed dead code

* Disabled more unit tests...2

* Disabled more unit tests...3

* Renambed Global->Applicaton and updated scope API docs 2

* Added more KeyBinding scope tests

* Added more KeyBinding scope tests2

* ConsoleDriverKey too long. Key too ambiguous. Settled on KeyCode. (Partialy because eventually I want to intro a class named Key).

* KeyEventArgs improvements. cast to Rune must be explicit as it's lossy

* Fixed warnings

* Renamed KeyEventArgs to Key... progress on fixing broken stuff that resulted

* Fix ConsoleKeyMapping bugs.

* Fix NetDriver issue from converting a lower case to a upper case.

* Started migration to Key from KeyCode - e.g. made HotKeys all consistent.

* Fixed build warnings

* Added key defns to Key

* KeyBindings now uses Key vs. KeyCode

* Verified by tweaking UICatalog

* Fixed treeview test ... again

* Renamed ProcessKeyDown/Up to NewKeyDown/Up and OnKeyPressed to OnProcessKeyDown to make things more clear

* Added test AllViews_KeyDown_All_EventsFire unit tests and fixed a few Views that were wrong

* fixed stupid KeyUp event bug

* If key not handled, return false for datefield

* dotnet test --no-restore --verbosity diag

* dotnet test --blame

* run tests on windows

* Fix TestVKPacket unit test and move it to ConsoleKeyMappingTests.cs file.

* Remove unnecessary commented code.

* Tweaked unit tests and removed Key.BareKey

* Fixed little details and updated api docs

* updated api docs

* AddKeyBindingsForHotKey: KeyCode->Key

* Cleaned up more old KeyCode usages. Added TODOs

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2023-12-16 12:04:23 -07:00

653 lines
20 KiB
C#

using System;
using Xunit;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewsTests {
public class ButtonTests {
readonly ITestOutputHelper output;
public ButtonTests (ITestOutputHelper output)
{
this.output = output;
}
[Fact, AutoInitShutdown]
public void Constructors_Defaults ()
{
var btn = new Button ();
Assert.Equal (string.Empty, btn.Text);
Application.Top.Add (btn);
var rs = Application.Begin (Application.Top);
Assert.Equal ($"{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
Assert.False (btn.IsDefault);
Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
Assert.Equal ('_', btn.HotKeySpecifier.Value);
Assert.True (btn.CanFocus);
Assert.Equal (new Rect (0, 0, 4, 1), btn.Bounds);
Assert.Equal (new Rect (0, 0, 4, 1), btn.Frame);
Assert.Equal (string.Empty, btn.Title);
Assert.Equal (KeyCode.Null, btn.HotKey);
var expected = @$"
{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Application.End (rs);
btn = new Button ("ARGS", true) { Text = "Test" };
Assert.Equal ("Test", btn.Text);
Application.Top.Add (btn);
rs = Application.Begin (Application.Top);
Assert.Equal ($"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Test {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
Assert.True (btn.IsDefault);
Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
Assert.Equal ('_', btn.HotKeySpecifier.Value);
Assert.True (btn.CanFocus);
Assert.Equal (new Rect (0, 0, 10, 1), btn.Bounds);
Assert.Equal (new Rect (0, 0, 10, 1), btn.Frame);
Assert.Equal (KeyCode.T, btn.HotKey);
Application.End (rs);
btn = new Button (3, 4, "Test", true);
Assert.Equal ("Test", btn.Text);
Application.Top.Add (btn);
rs = Application.Begin (Application.Top);
Assert.Equal ($"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Test {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
Assert.True (btn.IsDefault);
Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
Assert.Equal ('_', btn.HotKeySpecifier.Value);
Assert.True (btn.CanFocus);
Assert.Equal (new Rect (0, 0, 10, 1), btn.Bounds);
Assert.Equal (new Rect (3, 4, 10, 1), btn.Frame);
Assert.Equal (KeyCode.T, btn.HotKey);
Application.End (rs);
}
[Fact]
[AutoInitShutdown]
public void KeyBindings_Command ()
{
var clicked = false;
Button btn = new Button ("_Test");
btn.Clicked += (s, e) => clicked = true;
Application.Top.Add (btn);
Application.Begin (Application.Top);
Assert.Equal (KeyCode.T, btn.HotKey);
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T | KeyCode.AltMask)));
Assert.True (clicked);
clicked = false;
Assert.False (btn.IsDefault);
Assert.False (btn.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.False (clicked);
btn.IsDefault = true;
Assert.False (btn.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (new (KeyCode.AltMask | KeyCode.T)));
Assert.True (clicked);
clicked = false;
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (new (KeyCode.Space)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (new ((KeyCode)'T')));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (btn.HotKey));
Assert.True (clicked);
btn.Text = "Te_st";
clicked = false;
Assert.True (btn.NewKeyDownEvent (btn.HotKey));
Assert.True (clicked);
}
[Fact]
[AutoInitShutdown]
public void HotKeyChange_Works ()
{
var clicked = false;
Button btn = new Button ("Test");
btn.Clicked += (s, e) => clicked = true;
Application.Top.Add (btn);
Application.Begin (Application.Top);
Assert.Equal (KeyCode.T, btn.HotKey);
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T | KeyCode.AltMask)));
Assert.True (clicked);
clicked = false;
btn.HotKey = KeyCode.E;
Assert.True (btn.NewKeyDownEvent (new (KeyCode.E | KeyCode.AltMask)));
Assert.True (clicked);
}
/// <summary>
/// This test demonstrates how to change the activation key for Button
/// as described in the README.md keyboard handling section
/// </summary>
[Fact]
[AutoInitShutdown]
public void KeyBindingExample ()
{
int pressed = 0;
var btn = new Button ("Press Me");
btn.Clicked += (s, e) => pressed++;
// The Button class supports the Default and Accept command
Assert.Contains (Command.Default, btn.GetSupportedCommands ());
Assert.Contains (Command.Accept, btn.GetSupportedCommands ());
Application.Top.Add (btn);
Application.Begin (Application.Top);
// default keybinding is Space which results in keypress
Application.OnKeyDown (new ((KeyCode)' '));
Assert.Equal (1, pressed);
// remove the default keybinding (Space)
btn.KeyBindings.Clear (Command.Default, Command.Accept);
// After clearing the default keystroke the Space button no longer does anything for the Button
Application.OnKeyDown (new ((KeyCode)' '));
Assert.Equal (1, pressed);
// Set a new binding of b for the click (Accept) event
btn.KeyBindings.Add (KeyCode.B, Command.Default, Command.Accept);
// now pressing B should call the button click event
Application.OnKeyDown (new (KeyCode.B));
Assert.Equal (2, pressed);
// now pressing Shift-B should NOT call the button click event
Application.OnKeyDown (new (KeyCode.ShiftMask | KeyCode.B));
Assert.Equal (2, pressed);
// now pressing Alt-B should NOT call the button click event
Application.OnKeyDown (new (KeyCode.AltMask | KeyCode.B));
Assert.Equal (2, pressed);
// now pressing Shift-Alt-B should NOT call the button click event
Application.OnKeyDown (new (KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.B));
Assert.Equal (2, pressed);
}
[Fact]
public void TestAssignTextToButton ()
{
View b = new Button () { Text = "heya" };
Assert.Equal ("heya", b.Text);
Assert.Contains ("heya", b.TextFormatter.Text);
b.Text = "heyb";
Assert.Equal ("heyb", b.Text);
Assert.Contains ("heyb", b.TextFormatter.Text);
// with cast
Assert.Equal ("heyb", ((Button)b).Text);
}
[Fact]
public void Setting_Empty_Text_Sets_HoKey_To_KeyNull ()
{
var super = new View ();
var btn = new Button ("Test");
super.Add (btn);
super.BeginInit ();
super.EndInit ();
Assert.Equal ("Test", btn.Text);
Assert.Equal (KeyCode.T, btn.HotKey);
btn.Text = string.Empty;
Assert.Equal ("", btn.Text);
Assert.Equal (KeyCode.Null, btn.HotKey);
btn.Text = "Te_st";
Assert.Equal ("Te_st", btn.Text);
Assert.Equal (KeyCode.S, btn.HotKey);
}
[Fact, AutoInitShutdown]
public void Update_Only_On_Or_After_Initialize ()
{
var btn = new Button ("Say Hello 你") {
X = Pos.Center (),
Y = Pos.Center ()
};
var win = new Window () {
Width = Dim.Fill (),
Height = Dim.Fill ()
};
win.Add (btn);
Application.Top.Add (win);
Assert.False (btn.IsInitialized);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
Assert.True (btn.IsInitialized);
Assert.Equal ("Say Hello 你", btn.Text);
Assert.Equal ($"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds);
var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
var expected = @$"
┌────────────────────────────┐
│ │
│ {btnTxt} │
│ │
└────────────────────────────┘
";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (0, 0, 30, 5), pos);
}
[Fact, AutoInitShutdown]
public void Update_Parameterless_Only_On_Or_After_Initialize ()
{
var btn = new Button () {
X = Pos.Center (),
Y = Pos.Center (),
Text = "Say Hello 你"
};
var win = new Window () {
Width = Dim.Fill (),
Height = Dim.Fill (),
};
win.Add (btn);
Application.Top.Add (win);
Assert.False (btn.IsInitialized);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
Assert.True (btn.IsInitialized);
Assert.Equal ("Say Hello 你", btn.Text);
Assert.Equal ($"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds);
var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
var expected = @$"
┌────────────────────────────┐
│ │
│ {btnTxt} │
│ │
└────────────────────────────┘
";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (0, 0, 30, 5), pos);
}
[Fact, AutoInitShutdown]
public void AutoSize_Stays_True_With_EmptyText ()
{
var btn = new Button () {
X = Pos.Center (),
Y = Pos.Center (),
AutoSize = true
};
var win = new Window () {
Width = Dim.Fill (),
Height = Dim.Fill (),
};
win.Add (btn);
Application.Top.Add (win);
Assert.True (btn.AutoSize);
btn.Text = "Say Hello 你";
Assert.True (btn.AutoSize);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
var expected = @$"
┌────────────────────────────┐
│ │
│ {CM.Glyphs.LeftBracket} Say Hello 你 {CM.Glyphs.RightBracket} │
│ │
└────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
}
[Fact, AutoInitShutdown]
public void AutoSize_Stays_True_Center ()
{
var btn = new Button () {
X = Pos.Center (),
Y = Pos.Center (),
Text = "Say Hello 你"
};
var win = new Window () {
Width = Dim.Fill (),
Height = Dim.Fill (),
};
win.Add (btn);
Application.Top.Add (win);
Assert.True (btn.AutoSize);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
var expected = @$"
┌────────────────────────────┐
│ │
│ {CM.Glyphs.LeftBracket} Say Hello 你 {CM.Glyphs.RightBracket} │
│ │
└────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.True (btn.AutoSize);
btn.Text = "Say Hello 你 changed";
Assert.True (btn.AutoSize);
Application.Refresh ();
expected = @$"
┌────────────────────────────┐
│ │
│ {CM.Glyphs.LeftBracket} Say Hello 你 changed {CM.Glyphs.RightBracket} │
│ │
└────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
}
[Fact, AutoInitShutdown]
public void AutoSize_Stays_True_AnchorEnd ()
{
var btn = new Button () {
Y = Pos.Center (),
Text = "Say Hello 你",
AutoSize = true
};
var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
btn.X = Pos.AnchorEnd () - Pos.Function (() => btn.TextFormatter.Text.GetColumns ());
var win = new Window () {
Width = Dim.Fill (),
Height = Dim.Fill (),
};
win.Add (btn);
Application.Top.Add (win);
Assert.True (btn.AutoSize);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
var expected = @$"
┌────────────────────────────┐
│ │
│ {btnTxt}│
│ │
└────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.True (btn.AutoSize);
btn.Text = "Say Hello 你 changed";
btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
Assert.True (btn.AutoSize);
Application.Refresh ();
expected = @$"
┌────────────────────────────┐
│ │
│ {btnTxt}│
│ │
└────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
}
[Fact, AutoInitShutdown]
public void AutoSize_False_With_Fixed_Width ()
{
var tab = new View ();
var lblWidth = 8;
var label = new Label ("Find:") {
Y = 1,
Width = lblWidth,
TextAlignment = TextAlignment.Right,
AutoSize = false
};
tab.Add (label);
var txtToFind = new TextField ("Testing buttons.") {
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = 20
};
tab.Add (txtToFind);
var btnFindNext = new Button ("Find _Next") {
X = Pos.Right (txtToFind) + 1,
Y = Pos.Top (label),
Width = 20,
Enabled = !string.IsNullOrEmpty (txtToFind.Text),
TextAlignment = TextAlignment.Centered,
IsDefault = true,
AutoSize = false
};
tab.Add (btnFindNext);
var btnFindPrevious = new Button ("Find _Previous") {
X = Pos.Right (txtToFind) + 1,
Y = Pos.Top (btnFindNext) + 1,
Width = 20,
Enabled = !string.IsNullOrEmpty (txtToFind.Text),
TextAlignment = TextAlignment.Centered,
AutoSize = false
};
tab.Add (btnFindPrevious);
var btnCancel = new Button ("Cancel") {
X = Pos.Right (txtToFind) + 1,
Y = Pos.Top (btnFindPrevious) + 2,
Width = 20,
TextAlignment = TextAlignment.Centered,
AutoSize = false
};
tab.Add (btnCancel);
var ckbMatchCase = new CheckBox ("Match c_ase") {
X = 0,
Y = Pos.Top (txtToFind) + 2,
Checked = true
};
tab.Add (ckbMatchCase);
var ckbMatchWholeWord = new CheckBox ("Match _whole word") {
X = 0,
Y = Pos.Top (ckbMatchCase) + 1,
Checked = false
};
tab.Add (ckbMatchWholeWord);
var tabView = new TabView () {
Width = Dim.Fill (),
Height = Dim.Fill ()
};
tabView.AddTab (new Tab ("Find", tab), true);
var win = new Window () {
Width = Dim.Fill (),
Height = Dim.Fill ()
};
tab.Width = label.Width + txtToFind.Width + btnFindNext.Width + 2;
tab.Height = btnFindNext.Height + btnFindPrevious.Height + btnCancel.Height + 4;
win.Add (tabView);
Application.Top.Add (win);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (54, 11);
Assert.Equal (new Rect (0, 0, 54, 11), win.Frame);
Assert.Equal (new Rect (0, 0, 52, 9), tabView.Frame);
Assert.Equal (new Rect (0, 0, 50, 7), tab.Frame);
Assert.Equal (new Rect (0, 1, 8, 1), label.Frame);
Assert.Equal (new Rect (9, 1, 20, 1), txtToFind.Frame);
Assert.Equal (0, txtToFind.ScrollOffset);
Assert.Equal (16, txtToFind.CursorPosition);
Assert.Equal (new Rect (30, 1, 20, 1), btnFindNext.Frame);
Assert.Equal (new Rect (30, 2, 20, 1), btnFindPrevious.Frame);
Assert.Equal (new Rect (30, 4, 20, 1), btnCancel.Frame);
Assert.Equal (new Rect (0, 3, 12, 1), ckbMatchCase.Frame);
Assert.Equal (new Rect (0, 4, 18, 1), ckbMatchWholeWord.Frame);
var btn1 = $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Find Next {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}";
var btn2 = $"{CM.Glyphs.LeftBracket} Find Previous {CM.Glyphs.RightBracket}";
var btn3 = $"{CM.Glyphs.LeftBracket} Cancel {CM.Glyphs.RightBracket}";
var expected = @$"
┌────────────────────────────────────────────────────┐
│┌────┐ │
││Find│ │
││ └─────────────────────────────────────────────┐│
││ ││
││ Find: Testing buttons. {btn1} ││
││ {btn2} ││
││{CM.Glyphs.Checked} Match case ││
││{CM.Glyphs.UnChecked} Match whole word {btn3} ││
│└──────────────────────────────────────────────────┘│
└────────────────────────────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
}
[Fact, AutoInitShutdown]
public void Pos_Center_Layout_AutoSize_True ()
{
var button = new Button ("Process keys") {
X = Pos.Center (),
Y = Pos.Center (),
IsDefault = true
};
var win = new Window () {
Width = Dim.Fill (),
Height = Dim.Fill ()
};
win.Add (button);
Application.Top.Add (win);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
Assert.True (button.AutoSize);
Assert.Equal (new Rect (5, 1, 18, 1), button.Frame);
var btn = $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Process keys {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}";
var expected = @$"
┌────────────────────────────┐
│ │
│ {btn} │
│ │
└────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
}
[Fact, AutoInitShutdown]
public void Pos_Center_Layout_AutoSize_False ()
{
var button = new Button ("Process keys") {
X = Pos.Center (),
Y = Pos.Center (),
Width = 20,
IsDefault = true,
AutoSize = false
};
var win = new Window () {
Width = Dim.Fill (),
Height = Dim.Fill ()
};
win.Add (button);
Application.Top.Add (win);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
Assert.False (button.AutoSize);
Assert.Equal (new Rect (4, 1, 20, 1), button.Frame);
var expected = @$"
┌────────────────────────────┐
│ │
│ {CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Process keys {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket} │
│ │
└────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
}
[Fact, AutoInitShutdown]
public void Button_HotKeyChanged_EventFires ()
{
var btn = new Button ("Yar");
object sender = null;
KeyChangedEventArgs args = null;
btn.HotKeyChanged += (s, e) => {
sender = s;
args = e;
};
btn.HotKey = KeyCode.R;
Assert.Same (btn, sender);
Assert.Equal (KeyCode.Y, args.OldKey);
Assert.Equal (KeyCode.R, args.NewKey);
}
[Fact, AutoInitShutdown]
public void Button_HotKeyChanged_EventFires_WithNone ()
{
var btn = new Button ();
object sender = null;
KeyChangedEventArgs args = null;
btn.HotKeyChanged += (s, e) => {
sender = s;
args = e;
};
btn.HotKey = KeyCode.R;
Assert.Same (btn, sender);
Assert.Equal (KeyCode.Null, args.OldKey);
Assert.Equal (KeyCode.R, args.NewKey);
}
}
}