| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- using System.Globalization;
- using TerminalGuiFluentTesting;
- using TerminalGuiFluentTestingXunit;
- using Xunit.Abstractions;
- namespace IntegrationTests.FluentTests;
- /// <summary>
- /// Tests for the PopoverMenu class
- /// </summary>
- public class PopoverMenuTests
- {
- private readonly TextWriter _out;
- public PopoverMenuTests (ITestOutputHelper outputHelper)
- {
- CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
- _out = new TestOutputWriter (outputHelper);
- }
- [Theory]
- [ClassData (typeof (V2TestDrivers))]
- public void EnableForDesign_CreatesMenuItems (V2TestDriver d)
- {
- using GuiTestContext c = With.A<Window> (80, 25, d)
- .Then (
- () =>
- {
- var popoverMenu = new PopoverMenu ();
- Application.Top!.Add (popoverMenu);
- // Call EnableForDesign
- Toplevel top = Application.Top;
- bool result = popoverMenu.EnableForDesign (ref top);
- // Should return true
- Assert.True (result);
- // Should have created menu items
- Assert.NotNull (popoverMenu.Root);
- Assert.Equal (7, popoverMenu.Root.SubViews.Count);
- // Should have Cut menu item
- View? cutMenuItem = popoverMenu.GetMenuItemsOfAllSubMenus ().FirstOrDefault (v => v?.Title == "Cu_t");
- Assert.NotNull (cutMenuItem);
- })
- .Stop ();
- }
- private static object o = new ();
- [Theory]
- [ClassData (typeof (V2TestDrivers))]
- public void Activate_Sets_Application_Navigation_Correctly (V2TestDriver d)
- {
- lock (o)
- {
- using GuiTestContext c = With.A<Window> (50, 20, d)
- .Then (
- () =>
- {
- var popoverMenu = new PopoverMenu ();
- // Call EnableForDesign
- Toplevel top = Application.Top!;
- popoverMenu.EnableForDesign (ref top);
- View? view = new View ()
- {
- CanFocus = true,
- Height = Dim.Auto (),
- Width = Dim.Auto (),
- Id = "focusableView",
- Text = "View",
- };
- Application.Top!.Add (view);
- // EnableForDesign sets to true; undo that
- popoverMenu.Visible = false;
- Application.Popover!.Register (popoverMenu);
- view.SetFocus ();
- })
- .WaitIteration ()
- .AssertFalse (Application.Popover?.GetActivePopover () is PopoverMenu)
- .AssertIsNotType<MenuItemv2> (Application.Navigation!.GetFocused ())
- .ScreenShot ("PopoverMenu initial state", _out)
- .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
- .WaitIteration ()
- .ScreenShot ($"After Show", _out)
- .AssertTrue (Application.Popover?.GetActivePopover () is PopoverMenu)
- .AssertEqual ("Cu_t", Application.Navigation!.GetFocused ()!.Title)
- .WriteOutLogs (_out)
- .Stop ();
- }
-
- }
- [Theory]
- [ClassData (typeof (V2TestDrivers))]
- public void QuitKey_Hides (V2TestDriver d)
- {
- using GuiTestContext c = With.A<Window> (50, 20, d)
- .Then (
- () =>
- {
- var popoverMenu = new PopoverMenu ();
- // Call EnableForDesign
- Toplevel top = Application.Top!;
- bool result = popoverMenu.EnableForDesign (ref top);
- View? view = new View ()
- {
- CanFocus = true,
- Height = Dim.Auto (),
- Width = Dim.Auto (),
- Id = "focusableView",
- Text = "View",
- };
- Application.Top!.Add (view);
- // EnableForDesign sets to true; undo that
- popoverMenu.Visible = false;
- Application.Popover!.Register (popoverMenu);
- view.SetFocus ();
- })
- .WaitIteration ()
- .ScreenShot ("PopoverMenu initial state", _out)
- .AssertFalse (Application.Popover?.GetActivePopover () is PopoverMenu)
- .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
- .WaitIteration ()
- .ScreenShot ($"After Show", _out)
- .AssertTrue (Application.Popover?.GetActivePopover () is PopoverMenu)
- .RaiseKeyDownEvent (Application.QuitKey)
- .WaitIteration ()
- .WriteOutLogs (_out)
- .ScreenShot ($"After {Application.QuitKey}", _out)
- .AssertFalse (Application.Popover!.Popovers.Cast<PopoverMenu> ().FirstOrDefault()!.Visible)
- .AssertNull (Application.Popover!.GetActivePopover())
- .AssertTrue (Application.Top!.Running)
- .WriteOutLogs (_out)
- .Stop ();
- }
- [Theory]
- [ClassData (typeof (V2TestDrivers))]
- public void QuitKey_Restores_Focus_Correctly (V2TestDriver d)
- {
- using GuiTestContext c = With.A<Window> (50, 20, d)
- .Then (
- () =>
- {
- var popoverMenu = new PopoverMenu ();
- // Call EnableForDesign
- Toplevel top = Application.Top!;
- bool result = popoverMenu.EnableForDesign (ref top);
- View? view = new View ()
- {
- CanFocus = true,
- Height = Dim.Auto (),
- Width = Dim.Auto (),
- Id = "focusableView",
- Text = "View",
- };
- Application.Top!.Add (view);
- // EnableForDesign sets to true; undo that
- popoverMenu.Visible = false;
- Application.Popover!.Register (popoverMenu);
- view.SetFocus ();
- })
- .WaitIteration ()
- .ScreenShot ("PopoverMenu initial state", _out)
- .AssertFalse (Application.Popover?.GetActivePopover () is PopoverMenu)
- .AssertIsNotType<MenuItemv2>(Application.Navigation!.GetFocused())
- .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
- .WaitIteration ()
- .ScreenShot ($"After Show", _out)
- .AssertTrue (Application.Popover?.GetActivePopover () is PopoverMenu)
- .AssertIsType<MenuItemv2>(Application.Navigation!.GetFocused())
- .RaiseKeyDownEvent (Application.QuitKey)
- .ScreenShot ($"After {Application.QuitKey}", _out)
- .AssertFalse (Application.Popover?.GetActivePopover () is PopoverMenu)
- .AssertIsNotType<MenuItemv2>(Application.Navigation!.GetFocused())
- .WriteOutLogs (_out)
- .Stop ();
- }
- [Theory]
- [ClassData (typeof (V2TestDrivers))]
- public void MenuBarItem_With_QuitKey_Open_QuitKey_Does_Not_Quit_App (V2TestDriver d)
- {
- using GuiTestContext c = With.A<Window> (50, 20, d)
- .Then (
- () =>
- {
- var popoverMenu = new PopoverMenu ();
- // Call EnableForDesign
- Toplevel top = Application.Top!;
- bool result = popoverMenu.EnableForDesign (ref top);
- View? view = new View ()
- {
- CanFocus = true,
- Height = Dim.Auto (),
- Width = Dim.Auto (),
- Id = "focusableView",
- Text = "View",
- };
- Application.Top!.Add (view);
- // EnableForDesign sets to true; undo that
- popoverMenu.Visible = false;
- Application.Popover!.Register (popoverMenu);
- view.SetFocus ();
- })
- .WaitIteration ()
- .AssertIsNotType<MenuItemv2>(Application.Navigation!.GetFocused())
- .ScreenShot ("PopoverMenu initial state", _out)
- .Then (() => Application.Popover!.Show (Application.Popover.Popovers.First ()))
- .WaitIteration ()
- .ScreenShot ("PopoverMenu after Show", _out)
- .AssertEqual ("Cu_t", Application.Navigation!.GetFocused ()!.Title)
- .AssertTrue (Application.Top!.Running)
- .RaiseKeyDownEvent (Application.QuitKey)
- .WaitIteration ()
- .ScreenShot ($"After {Application.QuitKey}", _out)
- .AssertFalse (Application.Popover?.GetActivePopover () is PopoverMenu)
- .AssertTrue (Application.Top!.Running)
- .WriteOutLogs (_out)
- .Stop ();
- }
- [Theory]
- [ClassData (typeof (V2TestDrivers))]
- public void Not_Active_DoesNotEat_Space (V2TestDriver d)
- {
- int spaceKeyDownCount = 0;
- View testView = new View ()
- {
- CanFocus = true,
- Id = "testView",
- };
- testView.KeyDown += (sender, key) =>
- {
- if (key == Key.Space)
- {
- spaceKeyDownCount++;
- }
- };
- using GuiTestContext c = With.A<Window> (50, 20, d)
- .Then (
- () =>
- {
- var popoverMenu = new PopoverMenu();
- Toplevel top = Application.Top!;
- popoverMenu.EnableForDesign (ref top);
- Application.Popover!.Register (popoverMenu);
- })
- .Add (testView)
- .WaitIteration ()
- .Focus (testView)
- .RaiseKeyDownEvent (Key.Space)
- .AssertEqual (1, spaceKeyDownCount)
- .WriteOutLogs (_out)
- .Stop ();
- }
- [Theory]
- [ClassData (typeof (V2TestDrivers))]
- public void Not_Active_DoesNotEat_Enter (V2TestDriver d)
- {
- int enterKeyDownCount = 0;
- View testView = new View ()
- {
- CanFocus = true,
- Id = "testView",
- };
- testView.KeyDown += (sender, key) =>
- {
- if (key == Key.Enter)
- {
- enterKeyDownCount++;
- }
- };
- using GuiTestContext c = With.A<Window> (50, 20, d)
- .Then (
- () =>
- {
- var popoverMenu = new PopoverMenu ();
- Toplevel top = Application.Top!;
- popoverMenu.EnableForDesign (ref top);
- Application.Popover!.Register (popoverMenu);
- })
- .Add (testView)
- .WaitIteration ()
- .Focus (testView)
- .RaiseKeyDownEvent (Key.Enter)
- .AssertEqual (1, enterKeyDownCount)
- .WriteOutLogs (_out)
- .Stop ();
- }
- [Theory]
- [ClassData (typeof (V2TestDrivers))]
- public void Not_Active_DoesNotEat_QuitKey (V2TestDriver d)
- {
- int quitKeyDownCount = 0;
- View testView = new View ()
- {
- CanFocus = true,
- Id = "testView",
- };
- testView.KeyDown += (sender, key) =>
- {
- if (key == Application.QuitKey)
- {
- quitKeyDownCount++;
- }
- };
- using GuiTestContext c = With.A<Window> (50, 20, d)
- .Then (
- () =>
- {
- var popoverMenu = new PopoverMenu ();
- Toplevel top = Application.Top!;
- popoverMenu.EnableForDesign (ref top);
- Application.Popover!.Register (popoverMenu);
- })
- .Add (testView)
- .WaitIteration ()
- .Focus (testView)
- .RaiseKeyDownEvent (Application.QuitKey)
- .AssertEqual (1, quitKeyDownCount)
- .WriteOutLogs (_out)
- .Stop ();
- }
- }
|