| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- #nullable enable
- using UnitTests;
- using Xunit.Abstractions;
- namespace ViewsTests;
- /// <summary>
- /// Pure unit tests for <see cref="Label"/> that don't require Application.Driver or Application context.
- /// These tests can run in parallel without interference.
- /// </summary>
- public class LabelTests (ITestOutputHelper output) : FakeDriverBase
- {
- [Fact]
- public void Text_Mirrors_Title ()
- {
- var label = new Label ();
- label.Title = "Hello";
- Assert.Equal ("Hello", label.Title);
- Assert.Equal ("Hello", label.TitleTextFormatter.Text);
- Assert.Equal ("Hello", label.Text);
- Assert.Equal ("Hello", label.TextFormatter.Text);
- }
- [Fact]
- public void Title_Mirrors_Text ()
- {
- var label = new Label ();
- label.Text = "Hello";
- Assert.Equal ("Hello", label.Text);
- Assert.Equal ("Hello", label.TextFormatter.Text);
- Assert.Equal ("Hello", label.Title);
- Assert.Equal ("Hello", label.TitleTextFormatter.Text);
- }
- [Theory]
- [CombinatorialData]
- public void HotKey_Command_SetsFocus_OnNextSubView (bool hasHotKey)
- {
- var superView = new View { CanFocus = true };
- var label = new Label ();
- label.HotKey = hasHotKey ? Key.A.WithAlt : Key.Empty;
- var nextSubView = new View { CanFocus = true };
- superView.Add (label, nextSubView);
- superView.BeginInit ();
- superView.EndInit ();
- Assert.False (label.HasFocus);
- Assert.False (nextSubView.HasFocus);
- label.InvokeCommand (Command.HotKey);
- Assert.False (label.HasFocus);
- Assert.Equal (hasHotKey, nextSubView.HasFocus);
- }
- [Theory]
- [CombinatorialData]
- public void MouseClick_SetsFocus_OnNextSubView (bool hasHotKey)
- {
- var superView = new View { CanFocus = true, Height = 1, Width = 15 };
- var focusedView = new View { CanFocus = true, Width = 1, Height = 1 };
- var label = new Label { X = 2 };
- label.HotKey = hasHotKey ? Key.X.WithAlt : Key.Empty;
- var nextSubView = new View { CanFocus = true, X = 4, Width = 4, Height = 1 };
- superView.Add (focusedView, label, nextSubView);
- superView.BeginInit ();
- superView.EndInit ();
- Assert.False (focusedView.HasFocus);
- Assert.False (label.HasFocus);
- Assert.False (nextSubView.HasFocus);
- label.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked });
- Assert.False (label.HasFocus);
- Assert.Equal (hasHotKey, nextSubView.HasFocus);
- }
- [Fact]
- public void HotKey_Command_Does_Not_Accept ()
- {
- var label = new Label ();
- var accepted = false;
- label.Accepting += LabelOnAccept;
- label.InvokeCommand (Command.HotKey);
- Assert.False (accepted);
- return;
- void LabelOnAccept (object? sender, CommandEventArgs e) { accepted = true; }
- }
- [Fact]
- public void Constructors_Defaults ()
- {
- var label = new Label ();
- Assert.Equal (string.Empty, label.Text);
- Assert.Equal (Alignment.Start, label.TextAlignment);
- Assert.False (label.CanFocus);
- Assert.Equal (new (0, 0, 0, 0), label.Frame);
- Assert.Equal (KeyCode.Null, label.HotKey);
- }
- [Fact]
- public void Label_HotKeyChanged_EventFires ()
- {
- var label = new Label ();
- var fired = false;
- Key oldKey = Key.Empty;
- Key newKey = Key.Empty;
- label.HotKeyChanged += (s, e) =>
- {
- fired = true;
- oldKey = e.OldKey;
- newKey = e.NewKey;
- };
- label.HotKey = Key.A.WithAlt;
- Assert.True (fired);
- Assert.Equal (Key.Empty, oldKey);
- Assert.Equal (Key.A.WithAlt, newKey);
- }
- [Fact]
- public void Label_HotKeyChanged_EventFires_WithNone ()
- {
- var label = new Label { HotKey = Key.A.WithAlt };
- var fired = false;
- Key oldKey = Key.Empty;
- Key newKey = Key.Empty;
- label.HotKeyChanged += (s, e) =>
- {
- fired = true;
- oldKey = e.OldKey;
- newKey = e.NewKey;
- };
- label.HotKey = Key.Empty;
- Assert.True (fired);
- Assert.Equal (Key.A.WithAlt, oldKey);
- Assert.Equal (Key.Empty, newKey);
- }
- [Fact]
- public void TestAssignTextToLabel ()
- {
- var label = new Label ();
- label.Text = "Test";
- Assert.Equal ("Test", label.Text);
- }
- [Fact]
- public void CanFocus_False_HotKey_SetsFocus_Next ()
- {
- View otherView = new ()
- {
- Text = "otherView",
- CanFocus = true
- };
- Label label = new ()
- {
- Text = "_label"
- };
- View nextView = new ()
- {
- Text = "nextView",
- CanFocus = true
- };
- IApplication app = Application.Create ();
- Runnable<bool> runnable = new ();
- app.Begin (runnable);
- runnable.Add (otherView, label, nextView);
- otherView.SetFocus ();
- // runnable.SetFocus ();
- Assert.True (otherView.HasFocus);
- Assert.True (app.Keyboard.RaiseKeyDownEvent (label.HotKey));
- Assert.False (otherView.HasFocus);
- Assert.False (label.HasFocus);
- Assert.True (nextView.HasFocus);
- }
- [Fact]
- public void CanFocus_False_MouseClick_SetsFocus_Next ()
- {
- View otherView = new () { X = 0, Y = 0, Width = 1, Height = 1, Id = "otherView", CanFocus = true };
- Label label = new () { X = 0, Y = 1, Text = "_label" };
- View nextView = new ()
- {
- X = Pos.Right (label), Y = Pos.Top (label), Width = 1, Height = 1, Id = "nextView", CanFocus = true
- };
- IApplication app = Application.Create ();
- Runnable<bool> runnable = new ();
- app.Begin (runnable);
- runnable.Add (otherView, label, nextView);
- otherView.SetFocus ();
- // click on label
- app.Mouse.RaiseMouseEvent (new () { ScreenPosition = label.Frame.Location, Flags = MouseFlags.Button1Clicked });
- Assert.False (label.HasFocus);
- Assert.True (nextView.HasFocus);
- }
- [Fact]
- public void CanFocus_True_HotKey_SetsFocus ()
- {
- Label label = new ()
- {
- Text = "_label",
- CanFocus = true
- };
- View view = new ()
- {
- Text = "view",
- CanFocus = true
- };
- IApplication app = Application.Create ();
- Runnable<bool> runnable = new ();
- app.Begin (runnable);
- runnable.Add (label, view);
- view.SetFocus ();
- Assert.True (label.CanFocus);
- Assert.False (label.HasFocus);
- Assert.True (view.CanFocus);
- Assert.True (view.HasFocus);
- // No focused view accepts Tab, and there's no other view to focus, so OnKeyDown returns false
- Assert.True (app.Keyboard.RaiseKeyDownEvent (label.HotKey));
- Assert.True (label.HasFocus);
- Assert.False (view.HasFocus);
- }
- [Fact]
- public void CanFocus_True_MouseClick_Focuses ()
- {
- Label label = new ()
- {
- Text = "label",
- X = 0,
- Y = 0,
- CanFocus = true
- };
- View otherView = new ()
- {
- Text = "view",
- X = 0,
- Y = 1,
- Width = 4,
- Height = 1,
- CanFocus = true
- };
- IApplication app = Application.Create ();
- Runnable<bool> runnable = new ()
- {
- Width = 10,
- Height = 10
- }; ;
- app.Begin (runnable);
- runnable.Add (label, otherView);
- label.SetFocus ();
- Assert.True (label.CanFocus);
- Assert.True (label.HasFocus);
- Assert.True (otherView.CanFocus);
- Assert.False (otherView.HasFocus);
- otherView.SetFocus ();
- Assert.True (otherView.HasFocus);
- // label can focus, so clicking on it set focus
- app.Mouse.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Clicked });
- Assert.True (label.HasFocus);
- Assert.False (otherView.HasFocus);
- // click on view
- app.Mouse.RaiseMouseEvent (new () { ScreenPosition = new (0, 1), Flags = MouseFlags.Button1Clicked });
- Assert.False (label.HasFocus);
- Assert.True (otherView.HasFocus);
- }
- [Fact]
- public void With_Top_Margin_Without_Top_Border ()
- {
- IApplication app = Application.Create ();
- app.Init ("Fake");
- Runnable<bool> runnable = new ()
- {
- Width = 10,
- Height = 10
- }; ;
- app.Begin (runnable);
- var label = new Label { Text = "Test", /*Width = 6, Height = 3,*/ BorderStyle = LineStyle.Single };
- label.Margin!.Thickness = new (0, 1, 0, 0);
- label.Border!.Thickness = new (1, 0, 1, 1);
- runnable.Add (label);
- app.LayoutAndDraw ();
- Assert.Equal (new (0, 0, 6, 3), label.Frame);
- Assert.Equal (new (0, 0, 4, 1), label.Viewport);
- DriverAssert.AssertDriverContentsWithFrameAre (
- @"
- │Test│
- └────┘",
- output,
- app.Driver
- );
- }
- [Fact]
- public void Without_Top_Border ()
- {
- IApplication app = Application.Create ();
- app.Init ("Fake");
- Runnable<bool> runnable = new ()
- {
- Width = 10,
- Height = 10
- }; ;
- app.Begin (runnable);
- var label = new Label { Text = "Test", /* Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
- label.Border!.Thickness = new (1, 0, 1, 1);
- runnable.Add (label);
- app.LayoutAndDraw ();
- Assert.Equal (new (0, 0, 6, 2), label.Frame);
- Assert.Equal (new (0, 0, 4, 1), label.Viewport);
- DriverAssert.AssertDriverContentsWithFrameAre (
- @"
- │Test│
- └────┘",
- output,
- app.Driver
- );
- }
- }
|