using System.Globalization;
using System.Reflection;
using TerminalGuiFluentTesting;
using TerminalGuiFluentTestingXunit;
using Xunit.Abstractions;
namespace IntegrationTests.FluentTests;
///
/// Tests for the MenuBar class
///
public class MenuBarTests
{
private readonly TextWriter _out;
public MenuBarTests (ITestOutputHelper outputHelper)
{
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
_out = new TestOutputWriter (outputHelper);
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void Initializes_WithNoItems (TestDriver d)
{
using GuiTestContext c = With.A (80, 25, d, _out)
.Then ((_) =>
{
// Create a menu bar with no items
var menuBar = new MenuBar ();
Assert.Empty (menuBar.SubViews);
Assert.False (menuBar.CanFocus);
Assert.Equal (Orientation.Horizontal, menuBar.Orientation);
Assert.Equal (Key.F9, MenuBar.DefaultKey);
});
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void Initializes_WithItems (TestDriver d)
{
MenuBarItem [] menuItems = [];
using GuiTestContext c = With.A (80, 25, d, _out)
.Then ((_) =>
{
// Create items for the menu bar
menuItems =
[
new (
"_File",
[
new MenuItem ("_Open", "Opens a file", () => { })
]),
new (
"_Edit",
[
new MenuItem ("_Copy", "Copies selection", () => { })
])
];
var menuBar = new MenuBar (menuItems);
Assert.Equal (2, menuBar.SubViews.Count);
// First item should be the File menu
var fileMenu = menuBar.SubViews.ElementAt (0) as MenuBarItem;
Assert.NotNull (fileMenu);
Assert.Equal ("_File", fileMenu.Title);
// Second item should be the Edit menu
var editMenu = menuBar.SubViews.ElementAt (1) as MenuBarItem;
Assert.NotNull (editMenu);
Assert.Equal ("_Edit", editMenu.Title);
});
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void AddsItems_WithMenusProperty (TestDriver d)
{
using GuiTestContext c = With.A (80, 25, d, _out)
.Then ((_) =>
{
var menuBar = new MenuBar ();
// Set items through Menus property
menuBar.Menus =
[
new ("_File"),
new ("_Edit"),
new ("_View")
];
Assert.Equal (3, menuBar.SubViews.Count);
});
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void ChangesKey_RaisesEvent (TestDriver d)
{
using GuiTestContext c = With.A (80, 25, d, _out)
.Then ((_) =>
{
var menuBar = new MenuBar ();
var oldKeyValue = Key.Empty;
var newKeyValue = Key.Empty;
var eventRaised = false;
menuBar.KeyChanged += (_, args) =>
{
eventRaised = true;
oldKeyValue = args.OldKey;
newKeyValue = args.NewKey;
};
// Default key should be F9
Assert.Equal (Key.F9, menuBar.Key);
// Change key to F1
menuBar.Key = Key.F1;
// Verify event was raised
Assert.True (eventRaised);
Assert.Equal (Key.F9, oldKeyValue);
Assert.Equal (Key.F1, newKeyValue);
// Verify key was changed
Assert.Equal (Key.F1, menuBar.Key);
});
}
[Theory]
[ClassData (typeof (TestDrivers))]
public void DefaultKey_Activates (TestDriver d)
{
MenuBar? menuBar = null;
Toplevel? top = null;
using GuiTestContext c = With.A (50, 20, d, _out)
.Then ((app) =>
{
menuBar = new MenuBar ();
top = app.TopRunnable!;
top.Add (
new View ()
{
CanFocus = true,
Id = "focusableView",
});
menuBar.EnableForDesign (ref top);
app.TopRunnable!.Add (menuBar);
})
.WaitIteration ()
.AssertIsNotType