Files
Terminal.Gui/UnitTests/Views/ContextMenuTests.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

1189 lines
35 KiB
C#

using System.Globalization;
using System.Threading;
using Xunit;
using Xunit.Abstractions;
//using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
namespace Terminal.Gui.ViewsTests {
public class ContextMenuTests {
readonly ITestOutputHelper output;
public ContextMenuTests (ITestOutputHelper output)
{
this.output = output;
}
[Fact]
[AutoInitShutdown]
public void ContextMenu_Constructors ()
{
var cm = new ContextMenu ();
Assert.Equal (new Point (0, 0), cm.Position);
Assert.Empty (cm.MenuItems.Children);
Assert.Null (cm.Host);
cm.Position = new Point (20, 10);
cm.MenuItems = new MenuBarItem (new MenuItem [] {
new MenuItem ("First", "", null)
});
Assert.Equal (new Point (20, 10), cm.Position);
Assert.Single (cm.MenuItems.Children);
cm = new ContextMenu (5, 10,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
Assert.Equal (new Point (5, 10), cm.Position);
Assert.Equal (2, cm.MenuItems.Children.Length);
Assert.Null (cm.Host);
cm = new ContextMenu (new View () { X = 5, Y = 10 },
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
Assert.Equal (new Point (5, 10), cm.Position);
Assert.Equal (2, cm.MenuItems.Children.Length);
Assert.NotNull (cm.Host);
}
private ContextMenu Create_ContextMenu_With_Two_MenuItem (int x, int y)
{
return new ContextMenu (x, y,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
}
[Fact]
[AutoInitShutdown]
public void Show_Hide_IsShow ()
{
var cm = Create_ContextMenu_With_Two_MenuItem (10, 5);
cm.Show ();
Assert.True (ContextMenu.IsShow);
Application.Begin (Application.Top);
var expected = @"
┌──────┐
│ One │
│ Two │
└──────┘
";
TestHelpers.AssertDriverContentsAre (expected, output);
cm.Hide ();
Assert.False (ContextMenu.IsShow);
Application.Refresh ();
expected = "";
TestHelpers.AssertDriverContentsAre (expected, output);
}
[Fact]
[AutoInitShutdown]
public void Position_Changing ()
{
var cm = new ContextMenu (10, 5,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
cm.Show ();
Application.Begin (Application.Top);
var expected = @"
┌──────┐
│ One │
│ Two │
└──────┘
";
TestHelpers.AssertDriverContentsAre (expected, output);
cm.Position = new Point (5, 10);
cm.Show ();
Application.Refresh ();
expected = @"
┌──────┐
│ One │
│ Two │
└──────┘
";
TestHelpers.AssertDriverContentsAre (expected, output);
}
[Fact]
[AutoInitShutdown]
public void MenuItens_Changing ()
{
var cm = new ContextMenu (10, 5,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
cm.Show ();
Application.Begin (Application.Top);
var expected = @"
┌──────┐
│ One │
│ Two │
└──────┘
";
TestHelpers.AssertDriverContentsAre (expected, output);
cm.MenuItems = new MenuBarItem (new MenuItem [] {
new MenuItem ("First", "", null),
new MenuItem ("Second", "", null),
new MenuItem ("Third", "", null)
});
cm.Show ();
Application.Refresh ();
expected = @"
┌─────────┐
│ First │
│ Second │
│ Third │
└─────────┘
";
TestHelpers.AssertDriverContentsAre (expected, output);
}
[Fact, AutoInitShutdown]
public void Key_Changing ()
{
var lbl = new Label ("Original");
var cm = new ContextMenu ();
lbl.KeyDown += (s, e) => {
if (e == cm.Key) {
lbl.Text = "Replaced";
e.Handled = true;
}
};
var top = Application.Top;
top.Add (lbl);
Application.Begin (top);
Assert.True (lbl.NewKeyDownEvent (cm.Key));
Assert.Equal ("Replaced", lbl.Text);
lbl.Text = "Original";
cm.Key = KeyCode.Space | KeyCode.CtrlMask;
Assert.True (lbl.NewKeyDownEvent (cm.Key));
Assert.Equal ("Replaced", lbl.Text);
}
[Fact, AutoInitShutdown]
public void MouseFlags_Changing ()
{
var lbl = new Label ("Original");
var cm = new ContextMenu ();
lbl.MouseClick += (s, e) => {
if (e.MouseEvent.Flags == cm.MouseFlags) {
lbl.Text = "Replaced";
e.Handled = true;
}
};
var top = Application.Top;
top.Add (lbl);
Application.Begin (top);
Assert.True (lbl.OnMouseEvent (new MouseEvent () { Flags = cm.MouseFlags }));
Assert.Equal ("Replaced", lbl.Text);
lbl.Text = "Original";
cm.MouseFlags = MouseFlags.Button2Clicked;
Assert.True (lbl.OnMouseEvent (new MouseEvent () { Flags = cm.MouseFlags }));
Assert.Equal ("Replaced", lbl.Text);
}
[Fact, AutoInitShutdown]
public void KeyChanged_Event ()
{
var oldKey = KeyCode.Null;
var cm = new ContextMenu ();
cm.KeyChanged += (s, e) => oldKey = (KeyCode)e.OldKey;
cm.Key = KeyCode.Space | KeyCode.CtrlMask;
Assert.Equal (KeyCode.Space | KeyCode.CtrlMask, cm.Key);
Assert.Equal (KeyCode.F10 | KeyCode.ShiftMask, oldKey);
}
[Fact, AutoInitShutdown]
public void MouseFlagsChanged_Event ()
{
var oldMouseFlags = new MouseFlags ();
var cm = new ContextMenu ();
cm.MouseFlagsChanged += (s, e) => oldMouseFlags = e.OldValue;
cm.MouseFlags = MouseFlags.Button2Clicked;
Assert.Equal (MouseFlags.Button2Clicked, cm.MouseFlags);
Assert.Equal (MouseFlags.Button3Clicked, oldMouseFlags);
}
[Fact, AutoInitShutdown]
public void Show_Ensures_Display_Inside_The_Container_But_Preserves_Position ()
{
var cm = new ContextMenu (80, 25,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
Assert.Equal (new Point (80, 25), cm.Position);
cm.Show ();
Assert.Equal (new Point (80, 25), cm.Position);
Application.Begin (Application.Top);
var expected = @"
┌──────┐
│ One │
│ Two │
└──────┘
";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (72, 21, 80, 4), pos);
cm.Hide ();
Assert.Equal (new Point (80, 25), cm.Position);
}
[Fact, AutoInitShutdown]
public void Show_Ensures_Display_Inside_The_Container_Without_Overlap_The_Host ()
{
var view = new View ("View") {
X = Pos.AnchorEnd (10),
Y = Pos.AnchorEnd (1),
Width = 10,
Height = 1
};
var cm = new ContextMenu (view,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
Application.Top.Add (view);
Application.Begin (Application.Top);
Assert.Equal (new Rect (70, 24, 10, 1), view.Frame);
Assert.Equal (new Point (0, 0), cm.Position);
cm.Show ();
Assert.Equal (new Point (70, 24), cm.Position);
Application.Top.Draw ();
var expected = @"
┌──────┐
│ One │
│ Two │
└──────┘
View
";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (70, 20, 78, 5), pos);
cm.Hide ();
Assert.Equal (new Point (70, 24), cm.Position);
}
[Fact, AutoInitShutdown]
public void Show_Display_Below_The_Bottom_Host_If_Has_Enough_Space ()
{
var view = new View ("View") { X = 10, Y = 5, Width = 10, Height = 1 };
var cm = new ContextMenu (view,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
Application.Top.Add (view);
Application.Begin (Application.Top);
Assert.Equal (new Point (10, 5), cm.Position);
cm.Show ();
Application.Top.Draw ();
Assert.Equal (new Point (10, 5), cm.Position);
var expected = @"
View
┌──────┐
│ One │
│ Two │
└──────┘
";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (10, 5, 18, 5), pos);
cm.Hide ();
Assert.Equal (new Point (10, 5), cm.Position);
cm.Host.X = 5;
cm.Host.Y = 10;
cm.Host.Height = 3;
cm.Show ();
Application.Top.Draw ();
Assert.Equal (new Point (5, 12), cm.Position);
expected = @"
View
┌──────┐
│ One │
│ Two │
└──────┘
";
pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (5, 10, 13, 7), pos);
cm.Hide ();
Assert.Equal (new Point (5, 12), cm.Position);
}
[Fact, AutoInitShutdown]
public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Width ()
{
((FakeDriver)Application.Driver).SetBufferSize (5, 25);
var cm = new ContextMenu (0, 0,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
Assert.Equal (new Point (0, 0), cm.Position);
cm.Show ();
Assert.Equal (new Point (0, 0), cm.Position);
Application.Begin (Application.Top);
var expected = @"
┌────
│ One
│ Two
└────";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (0, 1, 5, 4), pos);
cm.Hide ();
Assert.Equal (new Point (0, 0), cm.Position);
}
[Fact, AutoInitShutdown]
public void Show_Display_At_Zero_If_The_Toplevel_Height_Is_Less_Than_The_Menu_Height ()
{
((FakeDriver)Application.Driver).SetBufferSize (80, 3);
var cm = new ContextMenu (0, 0,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
Assert.Equal (new Point (0, 0), cm.Position);
cm.Show ();
Assert.Equal (new Point (0, 0), cm.Position);
Application.Begin (Application.Top);
var expected = @"
┌──────┐
│ One │
│ Two │";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (0, 0, 8, 3), pos);
cm.Hide ();
Assert.Equal (new Point (0, 0), cm.Position);
}
[Fact, AutoInitShutdown]
public void Hide_Is_Invoke_At_Container_Closing ()
{
var cm = new ContextMenu (80, 25,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
var top = Application.Top;
Application.Begin (top);
top.Running = true;
Assert.False (ContextMenu.IsShow);
cm.Show ();
Assert.True (ContextMenu.IsShow);
top.RequestStop ();
Assert.False (ContextMenu.IsShow);
}
[Fact, AutoInitShutdown]
public void ForceMinimumPosToZero_True_False ()
{
var cm = new ContextMenu (-1, -2,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
Assert.Equal (new Point (-1, -2), cm.Position);
cm.Show ();
Assert.Equal (new Point (-1, -2), cm.Position);
Application.Begin (Application.Top);
var expected = @"
┌──────┐
│ One │
│ Two │
└──────┘
";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (0, 1, 8, 4), pos);
cm.ForceMinimumPosToZero = false;
cm.Show ();
Assert.Equal (new Point (-1, -2), cm.Position);
Application.Refresh ();
expected = @"
One │
Two │
──────┘
";
pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (1, 0, 7, 3), pos);
}
[Fact, AutoInitShutdown]
public void ContextMenu_Is_Closed_If_Another_MenuBar_Is_Open_Or_Vice_Versa ()
{
var cm = new ContextMenu (10, 5,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null)
})
);
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("File", "", null),
new MenuBarItem ("Edit", "", null)
});
Application.Top.Add (menu);
Application.Begin (Application.Top);
Assert.Null (Application.MouseGrabView);
cm.Show ();
Assert.True (ContextMenu.IsShow);
Assert.Equal (cm.MenuBar, Application.MouseGrabView);
Assert.False (menu.IsMenuOpen);
Assert.True (menu.NewKeyDownEvent (menu.Key));
Assert.False (ContextMenu.IsShow);
Assert.Equal (menu, Application.MouseGrabView);
Assert.True (menu.IsMenuOpen);
cm.Show ();
Assert.True (ContextMenu.IsShow);
Assert.Equal (cm.MenuBar, Application.MouseGrabView);
Assert.False (menu.IsMenuOpen);
#if SUPPORT_ALT_TO_ACTIVATE_MENU
Assert.True (Application.Top.ProcessKeyUp (new (Key.AltMask)));
Assert.False (ContextMenu.IsShow);
Assert.Equal (menu, Application.MouseGrabView);
Assert.True (menu.IsMenuOpen);
#endif
cm.Show ();
Assert.True (ContextMenu.IsShow);
Assert.Equal (cm.MenuBar, Application.MouseGrabView);
Assert.False (menu.IsMenuOpen);
Assert.False (menu.MouseEvent (new MouseEvent () { X = 1, Flags = MouseFlags.ReportMousePosition, View = menu }));
Assert.True (ContextMenu.IsShow);
Assert.Equal (cm.MenuBar, Application.MouseGrabView);
Assert.False (menu.IsMenuOpen);
Assert.True (menu.MouseEvent (new MouseEvent () { X = 1, Flags = MouseFlags.Button1Clicked, View = menu }));
Assert.False (ContextMenu.IsShow);
Assert.Equal (menu, Application.MouseGrabView);
Assert.True (menu.IsMenuOpen);
}
[Fact, AutoInitShutdown]
public void ContextMenu_On_Toplevel_With_A_MenuBar_TextField_StatusBar ()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US");
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("File", "", null),
new MenuBarItem ("Edit", "", null)
});
var label = new Label ("Label:") {
X = 2,
Y = 3
};
var tf = new TextField ("TextField") {
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = 20
};
var statusBar = new StatusBar (new StatusItem [] {
new StatusItem(KeyCode.F1, "~F1~ Help", null),
new StatusItem(KeyCode.CtrlMask | KeyCode.Q, "~^Q~ Quit", null)
});
Application.Top.Add (menu, label, tf, statusBar);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (45, 17);
Assert.Equal (new Rect (9, 3, 20, 1), tf.Frame);
Assert.True (tf.HasFocus);
tf.ContextMenu.Show ();
Assert.True (ContextMenu.IsShow);
Assert.Equal (new Point (9, 3), tf.ContextMenu.Position);
Application.Top.Draw ();
var expected = @"
File Edit
Label: TextField
┌─────────────────────┐
│ Select All Ctrl+T │
│ Delete All Ctrl+R │
│ Copy Ctrl+C │
│ Cut Ctrl+X │
│ Paste Ctrl+V │
│ Undo Ctrl+Z │
│ Redo Ctrl+Y │
└─────────────────────┘
F1 Help │ ^Q Quit
";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (1, 0, 32, 17), pos);
}
[Fact, AutoInitShutdown]
public void ContextMenu_On_Toplevel_With_A_MenuBar_Window_TextField_StatusBar ()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US");
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("File", "", null),
new MenuBarItem ("Edit", "", null)
});
var label = new Label ("Label:") {
X = 2,
Y = 3
};
var tf = new TextField ("TextField") {
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = 20
};
var win = new Window ();
win.Add (label, tf);
var statusBar = new StatusBar (new StatusItem [] {
new StatusItem (KeyCode.F1, "~F1~ Help", null),
new StatusItem (KeyCode.CtrlMask | KeyCode.Q, "~^Q~ Quit", null)
});
Application.Top.Add (menu, win, statusBar);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (44, 17);
Assert.Equal (new Rect (9, 3, 20, 1), tf.Frame);
Assert.True (tf.HasFocus);
tf.ContextMenu.Show ();
Assert.True (ContextMenu.IsShow);
Assert.Equal (new Point (10, 5), tf.ContextMenu.Position);
Application.Top.Draw ();
var expected = @"
File Edit
┌──────────────────────────────────────────┐
│ │
│ │
│ │
│ Label: TextField │
│ ┌─────────────────────┐ │
│ │ Select All Ctrl+T │ │
│ │ Delete All Ctrl+R │ │
│ │ Copy Ctrl+C │ │
│ │ Cut Ctrl+X │ │
│ │ Paste Ctrl+V │ │
│ │ Undo Ctrl+Z │ │
│ │ Redo Ctrl+Y │ │
│ └─────────────────────┘ │
└──────────────────────────────────────────┘
F1 Help │ ^Q Quit
";
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (1, 0, 44, 17), pos);
}
[Fact, AutoInitShutdown]
public void Menus_And_SubMenus_Always_Try_To_Be_On_Screen ()
{
var cm = new ContextMenu (-1, -2,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null),
new MenuItem ("Three", "", null),
new MenuBarItem ("Four", new MenuItem [] {
new MenuItem ("SubMenu1", "", null),
new MenuItem ("SubMenu2", "", null),
new MenuItem ("SubMenu3", "", null),
new MenuItem ("SubMenu4", "", null),
new MenuItem ("SubMenu5", "", null),
new MenuItem ("SubMenu6", "", null),
new MenuItem ("SubMenu7", "", null)
}),
new MenuItem ("Five", "", null),
new MenuItem ("Six", "", null)
})
);
Assert.Equal (new Point (-1, -2), cm.Position);
cm.Show ();
Assert.Equal (new Point (-1, -2), cm.Position);
var top = Application.Top;
Application.Begin (top);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
│ One │
│ Two │
│ Three │
│ Four ►│
│ Five │
│ Six │
└────────┘
", output);
Assert.True (top.Subviews [0].MouseEvent (new MouseEvent {
X = 0,
Y = 4,
Flags = MouseFlags.ReportMousePosition,
View = top.Subviews [0]
}));
Application.Refresh ();
Assert.Equal (new Point (-1, -2), cm.Position);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
│ One │
│ Two │
│ Three │
│ Four ►│┌───────────┐
│ Five ││ SubMenu1 │
│ Six ││ SubMenu2 │
└────────┘│ SubMenu3 │
│ SubMenu4 │
│ SubMenu5 │
│ SubMenu6 │
│ SubMenu7 │
└───────────┘
", output);
((FakeDriver)Application.Driver).SetBufferSize (40, 20);
cm.Position = new Point (41, -2);
cm.Show ();
Application.Refresh ();
Assert.Equal (new Point (41, -2), cm.Position);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
│ One │
│ Two │
│ Three │
│ Four ►│
│ Five │
│ Six │
└────────┘
", output);
Assert.True (top.Subviews [0].MouseEvent (new MouseEvent {
X = 30,
Y = 4,
Flags = MouseFlags.ReportMousePosition,
View = top.Subviews [0]
}));
Application.Refresh ();
Assert.Equal (new Point (41, -2), cm.Position);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
│ One │
│ Two │
│ Three │
┌───────────┐│ Four ►│
│ SubMenu1 ││ Five │
│ SubMenu2 ││ Six │
│ SubMenu3 │└────────┘
│ SubMenu4 │
│ SubMenu5 │
│ SubMenu6 │
│ SubMenu7 │
└───────────┘
", output);
cm.Position = new Point (41, 9);
cm.Show ();
Application.Refresh ();
Assert.Equal (new Point (41, 9), cm.Position);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
│ One │
│ Two │
│ Three │
│ Four ►│
│ Five │
│ Six │
└────────┘
", output);
Assert.True (top.Subviews [0].MouseEvent (new MouseEvent {
X = 30,
Y = 4,
Flags = MouseFlags.ReportMousePosition,
View = top.Subviews [0]
}));
Application.Refresh ();
Assert.Equal (new Point (41, 9), cm.Position);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
┌───────────┐│ One │
│ SubMenu1 ││ Two │
│ SubMenu2 ││ Three │
│ SubMenu3 ││ Four ►│
│ SubMenu4 ││ Five │
│ SubMenu5 ││ Six │
│ SubMenu6 │└────────┘
│ SubMenu7 │
└───────────┘
", output);
cm.Position = new Point (41, 22);
cm.Show ();
Application.Refresh ();
Assert.Equal (new Point (41, 22), cm.Position);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
│ One │
│ Two │
│ Three │
│ Four ►│
│ Five │
│ Six │
└────────┘
", output);
Assert.True (top.Subviews [0].MouseEvent (new MouseEvent {
X = 30,
Y = 4,
Flags = MouseFlags.ReportMousePosition,
View = top.Subviews [0]
}));
Application.Refresh ();
Assert.Equal (new Point (41, 22), cm.Position);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌───────────┐
│ SubMenu1 │┌────────┐
│ SubMenu2 ││ One │
│ SubMenu3 ││ Two │
│ SubMenu4 ││ Three │
│ SubMenu5 ││ Four ►│
│ SubMenu6 ││ Five │
│ SubMenu7 ││ Six │
└───────────┘└────────┘
", output);
((FakeDriver)Application.Driver).SetBufferSize (18, 8);
cm.Position = new Point (19, 10);
cm.Show ();
Application.Refresh ();
Assert.Equal (new Point (19, 10), cm.Position);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
│ One │
│ Two │
│ Three │
│ Four ►│
│ Five │
│ Six │
└────────┘
", output);
Assert.True (top.Subviews [0].MouseEvent (new MouseEvent {
X = 30,
Y = 4,
Flags = MouseFlags.ReportMousePosition,
View = top.Subviews [0]
}));
Application.Refresh ();
Assert.Equal (new Point (19, 10), cm.Position);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌───────────┐────┐
│ SubMenu1 │ │
│ SubMenu2 │ │
│ SubMenu3 │ee │
│ SubMenu4 │r ►│
│ SubMenu5 │e │
│ SubMenu6 │ │
│ SubMenu7 │────┘
", output);
}
[Fact, AutoInitShutdown]
public void Key_Open_And_Close_The_ContextMenu ()
{
var tf = new TextField ();
Application.Top.Add (tf);
Application.Begin (Application.Top);
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.F10 | KeyCode.ShiftMask)));
Assert.True (tf.ContextMenu.MenuBar.IsMenuOpen);
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.F10 | KeyCode.ShiftMask)));
Assert.Null (tf.ContextMenu.MenuBar);
}
[Fact, AutoInitShutdown]
public void Draw_A_ContextMenu_Over_A_Dialog ()
{
var top = Application.Top;
var win = new Window ();
top.Add (win);
Application.Begin (top);
((FakeDriver)Application.Driver).SetBufferSize (20, 15);
Assert.Equal (new Rect (0, 0, 20, 15), win.Frame);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌──────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────┘", output);
var dialog = new Dialog () { X = 2, Y = 2, Width = 15, Height = 4 };
dialog.Add (new TextField ("Test") { X = Pos.Center (), Width = 10 });
var rs = Application.Begin (dialog);
Assert.Equal (new Rect (2, 2, 15, 4), dialog.Frame);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌──────────────────┐
│ │
│ ┌─────────────┐ │
│ │ Test │ │
│ │ │ │
│ └─────────────┘ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────┘", output);
Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
X = 9,
Y = 3,
Flags = MouseFlags.Button3Clicked
}));
var firstIteration = false;
Application.RunIteration (ref rs, ref firstIteration);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌──────────────────┐
│ │
│ ┌─────────────┐ │
│ │ Test │ │
┌───────────────────
│ Select All Ctrl+
│ Delete All Ctrl+
│ Copy Ctrl+
│ Cut Ctrl+
│ Paste Ctrl+
│ Undo Ctrl+
│ Redo Ctrl+
└───────────────────
│ │
└──────────────────┘", output);
Application.End (rs);
}
[Fact, AutoInitShutdown]
public void Draw_A_ContextMenu_Over_A_Top_Dialog ()
{
((FakeDriver)Application.Driver).SetBufferSize (20, 15);
Assert.Equal (new Rect (0, 0, 20, 15), Application.Driver.Clip);
TestHelpers.AssertDriverContentsWithFrameAre ("", output);
var dialog = new Dialog () { X = 2, Y = 2, Width = 15, Height = 4 };
dialog.Add (new TextField ("Test") { X = Pos.Center (), Width = 10 });
var rs = Application.Begin (dialog);
Assert.Equal (new Rect (2, 2, 15, 4), dialog.Frame);
Assert.Equal (dialog, Application.Top);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌─────────────┐
│ Test │
│ │
└─────────────┘", output);
Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
X = 9,
Y = 3,
Flags = MouseFlags.Button3Clicked
}));
var firstIteration = false;
Application.RunIteration (ref rs, ref firstIteration);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌─────────────┐
│ Test │
┌───────────────────
│ Select All Ctrl+
│ Delete All Ctrl+
│ Copy Ctrl+
│ Cut Ctrl+
│ Paste Ctrl+
│ Undo Ctrl+
│ Redo Ctrl+
└───────────────────", output);
Application.End (rs);
}
[Fact, AutoInitShutdown]
public void Draw_A_ContextMenu_Over_A_Borderless_Top ()
{
((FakeDriver)Application.Driver).SetBufferSize (20, 15);
Assert.Equal (new Rect (0, 0, 20, 15), Application.Driver.Clip);
TestHelpers.AssertDriverContentsWithFrameAre ("", output);
var top = new Toplevel () { X = 2, Y = 2, Width = 15, Height = 4 };
top.Add (new TextField ("Test") { X = Pos.Center (), Width = 10 });
var rs = Application.Begin (top);
Assert.Equal (new Rect (2, 2, 15, 4), top.Frame);
Assert.Equal (top, Application.Top);
TestHelpers.AssertDriverContentsWithFrameAre (@"
Test", output);
Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
X = 8,
Y = 2,
Flags = MouseFlags.Button3Clicked
}));
var firstIteration = false;
Application.RunIteration (ref rs, ref firstIteration);
TestHelpers.AssertDriverContentsWithFrameAre (@"
Test
┌───────────────────
│ Select All Ctrl+
│ Delete All Ctrl+
│ Copy Ctrl+
│ Cut Ctrl+
│ Paste Ctrl+
│ Undo Ctrl+
│ Redo Ctrl+
└───────────────────", output);
Application.End (rs);
}
[Fact, AutoInitShutdown]
public void UseSubMenusSingleFrame_True_By_Mouse ()
{
var cm = new ContextMenu (5, 10,
new MenuBarItem ("Numbers", new MenuItem [] {
new MenuItem ("One", "", null),
new MenuBarItem ("Two", new MenuItem [] {
new MenuItem ("Sub-Menu 1", "", null),
new MenuItem ("Sub-Menu 2", "", null)
}),
new MenuItem ("Three", "", null),
})
) { UseSubMenusSingleFrame = true };
cm.Show ();
var rs = Application.Begin (Application.Top);
Assert.Equal (new Rect (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
│ One │
│ Two ►│
│ Three │
└────────┘", output);
Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
X = 5,
Y = 13,
Flags = MouseFlags.Button1Clicked
}));
var firstIteration = false;
Application.RunIteration (ref rs, ref firstIteration);
Assert.Equal (new Rect (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
Assert.Equal (new Rect (5, 11, 15, 6), Application.Top.Subviews [1].Frame);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌─────────────┐
│◄ Two │
├─────────────┤
│ Sub-Menu 1 │
│ Sub-Menu 2 │
└─────────────┘", output);
Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
X = 5,
Y = 12,
Flags = MouseFlags.Button1Clicked
}));
firstIteration = false;
Application.RunIteration (ref rs, ref firstIteration);
Assert.Equal (new Rect (5, 11, 10, 5), Application.Top.Subviews [0].Frame);
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌────────┐
│ One │
│ Two ►│
│ Three │
└────────┘", output);
Application.End (rs);
}
[Fact, AutoInitShutdown]
public void RequestStop_While_ContextMenu_Is_Open_Does_Not_Throws ()
{
var cm = Create_ContextMenu_With_Two_MenuItem (10, 5);
var top = Application.Top;
var isMenuAllClosed = false;
MenuBarItem mi = null;
var iterations = -1;
Application.Iteration += (s, a) => {
iterations++;
if (iterations == 0) {
cm.Show ();
Assert.True (ContextMenu.IsShow);
mi = cm.MenuBar.Menus [0];
mi.Action = () => {
var dialog1 = new Dialog ();
Application.Run (dialog1);
Assert.False (ContextMenu.IsShow);
Assert.True (isMenuAllClosed);
};
cm.MenuBar.MenuAllClosed += (_, _) => isMenuAllClosed = true;
} else if (iterations == 1) {
mi.Action ();
} else if (iterations == 2) {
Application.RequestStop ();
} else if (iterations == 3) {
isMenuAllClosed = false;
cm.Show ();
Assert.True (ContextMenu.IsShow);
cm.MenuBar.MenuAllClosed += (_, _) => isMenuAllClosed = true;
} else if (iterations == 4) {
var exception = Record.Exception (() => Application.RequestStop ());
Assert.Null (exception);
} else {
Application.RequestStop ();
}
};
var isTopClosed = false;
top.Closing += (_, _) => {
var dialog2 = new Dialog ();
Application.Run (dialog2);
Assert.False (ContextMenu.IsShow);
Assert.True (isMenuAllClosed);
isTopClosed = true;
};
Application.Run ();
Assert.True (isTopClosed);
Assert.False (ContextMenu.IsShow);
Assert.True (isMenuAllClosed);
}
}
}