refactoring menutests

This commit is contained in:
Charlie Kindel
2022-10-20 08:15:54 -06:00
parent ddf78ce0f6
commit c15d9bcc57
5 changed files with 109 additions and 90 deletions

View File

@@ -1024,6 +1024,8 @@ namespace Terminal.Gui {
isCleaning = false;
}
static int leftPadding = 1;
static int rightPadding = 1;
///<inheritdoc/>
public override void Redraw (Rect bounds)
{
@@ -1033,7 +1035,7 @@ namespace Terminal.Gui {
Driver.AddRune (' ');
Move (1, 0);
int pos = 1;
int pos = 0;
for (int i = 0; i < Menus.Length; i++) {
var menu = Menus [i];
@@ -1050,8 +1052,8 @@ namespace Terminal.Gui {
hotColor = GetNormalColor ();
normalColor = GetNormalColor ();
}
DrawHotString (menu.Help.IsEmpty ? $" {menu.Title} " : $" {menu.Title} {menu.Help} ", hotColor, normalColor);
pos += 1 + menu.TitleLength + (menu.Help.ConsoleWidth > 0 ? menu.Help.ConsoleWidth + 2 : 0) + 2;
DrawHotString (menu.Help.IsEmpty ? $" {menu.Title} " : $" {menu.Title} ({menu.Help}) ", hotColor, normalColor);
pos += leftPadding + menu.TitleLength + (menu.Help.ConsoleWidth > 0 ? menu.Help.ConsoleWidth + 3 : 0) + rightPadding;
}
PositionCursor ();
}
@@ -1073,7 +1075,7 @@ namespace Terminal.Gui {
}
return;
} else {
pos += 1 + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + 2 : 0) + 2;
pos += leftPadding + Menus [i].TitleLength + (Menus [i].Help.ConsoleWidth > 0 ? Menus [i].Help.ConsoleWidth + 3 : 0) + rightPadding;
}
}
}

View File

@@ -43,12 +43,14 @@ namespace UICatalog.Scenarios {
Height = Dim.Fill (1),
};
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("_File", new MenuItem [] {
var fileMenu = new MenuBarItem ("_File", new MenuItem [] {
new MenuItem ("_Open CSV", "", () => Open()),
new MenuItem ("_Save", "", () => Save()),
new MenuItem ("_Quit", "", () => Quit()),
}),
new MenuItem ("_Quit", "Quits The App", () => Quit()),
});
//fileMenu.Help = "Help";
var menu = new MenuBar (new MenuBarItem [] {
fileMenu,
new MenuBarItem ("_Edit", new MenuItem [] {
new MenuItem ("_New Column", "", () => AddColumn()),
new MenuItem ("_New Row", "", () => AddRow()),

View File

@@ -592,7 +592,7 @@ namespace Terminal.Gui.Core {
Assert.Equal (new Point (9, 3), tf.ContextMenu.Position);
Application.Top.Redraw (Application.Top.Bounds);
var expected = @"
File Edit
File Edit
Label: TextField
@@ -612,7 +612,7 @@ namespace Terminal.Gui.Core {
";
var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 32, 17), pos);
Assert.Equal (new Rect (1, 0, 32, 17), pos);
}
[Fact, AutoInitShutdown]
@@ -656,7 +656,7 @@ namespace Terminal.Gui.Core {
Assert.Equal (new Point (10, 5), tf.ContextMenu.Position);
Application.Top.Redraw (Application.Top.Bounds);
var expected = @"
File Edit
File Edit
┌ Window ──────────────────────────────────┐
│ │
│ │
@@ -676,7 +676,7 @@ namespace Terminal.Gui.Core {
";
var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 44, 17), pos);
Assert.Equal (new Rect (1, 0, 44, 17), pos);
}
[Fact, AutoInitShutdown]

View File

@@ -1074,11 +1074,11 @@ Edit
Assert.True (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
var expected = @"
File Edit
File Edit
";
var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 13, 1), pos);
Assert.Equal (new Rect (1, 0, 11, 1), pos);
Assert.True (menu.ProcessKey (new (Key.N, null)));
Application.MainLoop.MainIteration ();
@@ -1088,11 +1088,11 @@ Edit
Assert.True (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit
File Edit
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 13, 1), pos);
Assert.Equal (new Rect (1, 0, 11, 1), pos);
Assert.True (menu.ProcessKey (new (Key.CursorRight, null)));
Assert.True (menu.ProcessKey (new (Key.C, null)));
@@ -1124,14 +1124,14 @@ Edit
Assert.True (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
var expected = @"
File Edit
┌──────┐
│ New │
└──────┘
File Edit
┌──────┐
│ New │
└──────┘
";
var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 13, 4), pos);
Assert.Equal (new Rect (1, 0, 11, 4), pos);
Assert.True (Application.Top.Subviews [1].ProcessKey (new (Key.N, null)));
Application.MainLoop.MainIteration ();
@@ -1141,7 +1141,7 @@ Edit
Assert.True (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit
File Edit
┌───────┐
│ Copy │
└───────┘
@@ -1158,89 +1158,104 @@ Edit
[Fact, AutoInitShutdown]
public void MenuBar_Position_And_Size_With_HotKeys_Is_The_Same_As_Without_HotKeys ()
{
// With HotKeys
var firstMenuBarText = "File";
var firstMenuBarItemText = "New";
var secondMenuBarText = "Edit";
var secondMenuBarItemText = "Copy";
// Define expected MenuBar
// " File New "
var expectedMenuBarText = " " + firstMenuBarText + " " + " " + secondMenuBarText + " ";
// Define expected menu frame
// "┌──────┐"
// "│ New │"
// "└──────┘"
var d = ((FakeDriver)Application.Driver);
// BUGBUG: The extra 4 spaces on these should not be there
var expectedFirstTopRow = $"{d.ULCorner}{new String (d.HLine.ToString () [0], firstMenuBarItemText.Length + 3)}{d.URCorner} ";
var expectedFirstBottomRow = $"{d.LLCorner}{new String (d.HLine.ToString () [0], firstMenuBarItemText.Length + 3)}{d.LRCorner} ";
var expectedSecondTopRow = $"{d.ULCorner}{new String (d.HLine.ToString () [0], secondMenuBarItemText.Length + 3)}{d.URCorner}";
var expectedSecondBottomRow = $"{d.LLCorner}{new String (d.HLine.ToString () [0], secondMenuBarItemText.Length + 3)}{d.LRCorner}";
var expectedClosed = " " + firstMenuBarText + " " + " " + secondMenuBarText + "" + "\n";
var expectedFirstMenuOpen = " " + firstMenuBarText + " " + " " + secondMenuBarText + "" + "\n" +
expectedFirstTopRow + "\n" +
$"{d.VLine} {firstMenuBarItemText} {d.VLine}" + " \n" +
expectedFirstBottomRow + "\n";
var expectedSecondMenuOpen = " " + firstMenuBarText + " " + " " + secondMenuBarText + " " + "\n" +
new String (' ', firstMenuBarItemText.Length + 4) + expectedSecondTopRow + "\n" +
new String (' ', firstMenuBarItemText.Length + 4) + $"{d.VLine} {secondMenuBarItemText} {d.VLine}" + "\n" +
new String (' ', firstMenuBarItemText.Length + 4) + expectedSecondBottomRow + "\n";
// Test without HotKeys first
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("_File", new MenuItem [] {
new MenuItem ("_New", "", null)
new MenuBarItem (firstMenuBarText, new MenuItem [] {
new MenuItem (firstMenuBarItemText, "", null)
}),
new MenuBarItem ("_Edit", new MenuItem [] {
new MenuItem ("_Copy", "", null)
new MenuBarItem (secondMenuBarText, new MenuItem [] {
new MenuItem (secondMenuBarItemText, "", null)
})
});
Application.Top.Add (menu);
// Open first
Assert.True (menu.ProcessHotKey (new (Key.F9, new KeyModifiers ())));
Assert.True (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
var expected = @"
File Edit
┌──────┐
│ New │
└──────┘
";
var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 13, 4), pos);
var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expectedFirstMenuOpen, output);
Assert.Equal (1, pos.X);
Assert.Equal (0, pos.Y);
// Open second
Assert.True (Application.Top.Subviews [1].ProcessKey (new (Key.CursorRight, null)));
Assert.True (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit
┌───────┐
│ Copy │
└───────┘
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 16, 4), pos);
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expectedSecondMenuOpen, output);
// Close menu
Assert.True (menu.ProcessHotKey (new (Key.F9, new KeyModifiers ())));
Assert.False (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expectedClosed, output);
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 13, 1), pos);
Application.Top.Remove (menu);
// Without HotKeys
// Now test WITH HotKeys
menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("File", new MenuItem [] {
new MenuItem ("New", "", null)
new MenuBarItem ("_" + firstMenuBarText, new MenuItem [] {
new MenuItem ("_" + firstMenuBarItemText, "", null)
}),
new MenuBarItem ("Edit", new MenuItem [] {
new MenuItem ("Copy", "", null)
new MenuBarItem ("_" + secondMenuBarText, new MenuItem [] {
new MenuItem ("_" + secondMenuBarItemText, "", null)
})
});
Application.Top.Add (menu);
// Open first
Assert.True (menu.ProcessHotKey (new (Key.F9, new KeyModifiers ())));
Assert.True (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit
┌──────┐
│ New │
└──────┘
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 13, 4), pos);
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expectedFirstMenuOpen, output);
Assert.Equal (1, pos.X);
Assert.Equal (0, pos.Y);
// Open second
Assert.True (Application.Top.Subviews [1].ProcessKey (new (Key.CursorRight, null)));
Assert.True (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit
┌───────┐
│ Copy │
└───────┘
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expectedSecondMenuOpen, output);
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 16, 4), pos);
// Close menu
Assert.True (menu.ProcessHotKey (new (Key.F9, new KeyModifiers ())));
Assert.False (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expectedClosed, output);
}
[Fact, AutoInitShutdown]
@@ -1261,24 +1276,24 @@ Edit
Assert.True (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
var expected = @"
File Edit
File Edit
┌──────┐
│ New │
└──────┘
";
var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 13, 4), pos);
Assert.Equal (new Rect (1, 0, 11, 4), pos);
Assert.True (menu.MouseEvent (new MouseEvent () { X = 1, Y = 0, Flags = MouseFlags.Button1Pressed, View = menu }));
Assert.False (menu.IsMenuOpen);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit
File Edit
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new Rect (2, 0, 13, 1), pos);
Assert.Equal (new Rect (1, 0, 11, 1), pos);
}
[Fact]
@@ -1320,7 +1335,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
var expected = @"
File Edit Format
File Edit Format
┌──────┐
│ New │
└──────┘
@@ -1334,7 +1349,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
@@ -1345,7 +1360,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
┌───────┐
│ Wrap │
└───────┘
@@ -1359,7 +1374,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
@@ -1370,7 +1385,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
┌──────┐
│ New │
└──────┘
@@ -1384,7 +1399,7 @@ Edit
Assert.True (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
@@ -1414,7 +1429,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
var expected = @"
File Edit Format
File Edit Format
┌──────┐
│ New │
└──────┘
@@ -1428,7 +1443,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
@@ -1439,7 +1454,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
┌───────┐
│ Wrap │
└───────┘
@@ -1453,7 +1468,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
@@ -1464,7 +1479,7 @@ Edit
Assert.False (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
┌──────┐
│ New │
└──────┘
@@ -1478,7 +1493,7 @@ Edit
Assert.True (tf.HasFocus);
Application.Top.Redraw (Application.Top.Bounds);
expected = @"
File Edit Format
File Edit Format
";
pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);

View File

@@ -247,7 +247,7 @@ namespace Terminal.Gui.Core {
win.Frame.Right, win.Frame.Bottom));
Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
var expected = @"
Menu
Menu
┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ │
@@ -310,7 +310,7 @@ namespace Terminal.Gui.Core {
win.Frame.Right, win.Frame.Bottom));
Assert.Equal (new Rect (0, 20, 78, 1), label.Frame);
var expected = @"
Menu
Menu
┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ │