| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- ๏ปฟusing System.Text;
- using UnitTests;
- using Xunit.Abstractions;
- namespace ViewBaseTests.Adornments;
- [Collection ("Global Test Setup")]
- public class ShadowTests (ITestOutputHelper output)
- {
- private readonly ITestOutputHelper _output = output;
- [Fact]
- public void Default_None ()
- {
- var view = new View ();
- Assert.Equal (ShadowStyle.None, view.ShadowStyle);
- Assert.Equal (ShadowStyle.None, view.Margin!.ShadowStyle);
- view.Dispose ();
- }
- [Theory]
- [InlineData (ShadowStyle.None)]
- [InlineData (ShadowStyle.Opaque)]
- [InlineData (ShadowStyle.Transparent)]
- public void Set_View_Sets_Margin (ShadowStyle style)
- {
- var view = new View ();
- view.ShadowStyle = style;
- Assert.Equal (style, view.ShadowStyle);
- Assert.Equal (style, view.Margin!.ShadowStyle);
- view.Dispose ();
- }
- [Theory]
- [InlineData (ShadowStyle.None, 0, 0, 0, 0)]
- [InlineData (ShadowStyle.Opaque, 0, 0, 1, 1)]
- [InlineData (ShadowStyle.Transparent, 0, 0, 1, 1)]
- public void ShadowStyle_Margin_Thickness (ShadowStyle style, int expectedLeft, int expectedTop, int expectedRight, int expectedBottom)
- {
- var superView = new View
- {
- Height = 10, Width = 10
- };
- View view = new ()
- {
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- Text = "0123",
- HighlightStates = MouseState.Pressed,
- ShadowStyle = style,
- CanFocus = true
- };
- superView.Add (view);
- superView.BeginInit ();
- superView.EndInit ();
- Assert.Equal (new (expectedLeft, expectedTop, expectedRight, expectedBottom), view.Margin!.Thickness);
- }
- [Theory]
- [InlineData (ShadowStyle.None, 3)]
- [InlineData (ShadowStyle.Opaque, 4)]
- [InlineData (ShadowStyle.Transparent, 4)]
- public void Style_Changes_Margin_Thickness (ShadowStyle style, int expected)
- {
- var view = new View ();
- view.Margin!.Thickness = new (3);
- view.ShadowStyle = style;
- Assert.Equal (new (3, 3, expected, expected), view.Margin.Thickness);
- view.ShadowStyle = ShadowStyle.None;
- Assert.Equal (new (3), view.Margin.Thickness);
- view.Dispose ();
- }
- [Theory]
- [InlineData (ShadowStyle.None)]
- [InlineData (ShadowStyle.Opaque)]
- [InlineData (ShadowStyle.Transparent)]
- public void ShadowWidth_ShadowHeight_Defaults (ShadowStyle style)
- {
- View view = new () { ShadowStyle = style };
- if (view.ShadowStyle == ShadowStyle.None)
- {
- Assert.Equal (new (0, 0), view.Margin!.ShadowSize);
- }
- else
- {
- Assert.Equal (new (1, 1), view.Margin!.ShadowSize);
- }
- }
- [Fact]
- public void ShadowStyle_Opaque_Margin_ShadowWidth_ShadowHeight_Cannot_Be_Set_Different_Of_One ()
- {
- View view = new () { ShadowStyle = ShadowStyle.Opaque };
- view.Margin!.ShadowSize = new (3, 4);
- Assert.Equal (1, view.Margin.ShadowSize.Width);
- Assert.Equal (1, view.Margin.ShadowSize.Height);
- }
- [Theory]
- [InlineData (ShadowStyle.None, 0)]
- [InlineData (ShadowStyle.Opaque, 1)]
- [InlineData (ShadowStyle.Transparent, 1)]
- public void Margin_ShadowWidth_ShadowHeight_Cannot_Be_Set_Less_Than_Zero (ShadowStyle style, int expectedLength)
- {
- View view = new () { ShadowStyle = style };
- view.Margin!.ShadowSize = new (-1, -1);
- Assert.Equal (expectedLength, view.Margin!.ShadowSize.Width);
- Assert.Equal (expectedLength, view.Margin!.ShadowSize.Height);
- }
- [Fact]
- public void Changing_ShadowStyle_Correctly_Set_ShadowWidth_ShadowHeight_Thickness ()
- {
- View view = new () { ShadowStyle = ShadowStyle.Transparent };
- view.Margin!.ShadowSize = new (2, 2);
- Assert.Equal (new (2, 2), view.Margin!.ShadowSize);
- Assert.Equal (new (0, 0, 2, 2), view.Margin.Thickness);
- view.ShadowStyle = ShadowStyle.None;
- Assert.Equal (new (2, 2), view.Margin!.ShadowSize);
- Assert.Equal (new (0, 0, 0, 0), view.Margin.Thickness);
- view.ShadowStyle = ShadowStyle.Opaque;
- Assert.Equal (new (1, 1), view.Margin!.ShadowSize);
- Assert.Equal (new (0, 0, 1, 1), view.Margin.Thickness);
- }
- [Theory]
- [InlineData (ShadowStyle.None, 2, 1, 3, 0, 0, 0)]
- [InlineData (ShadowStyle.Opaque, 1, 1, 1, 1, 1, 1)]
- [InlineData (ShadowStyle.Transparent, 2, 1, 3, 2, 2, 3)]
- public void Changing_ShadowWidth_ShadowHeight_Correctly_Set_Thickness (
- ShadowStyle style,
- int expectedLength1,
- int expectedLength2,
- int expectedLength3,
- int expectedThickness1,
- int expectedThickness2,
- int expectedThickness3
- )
- {
- View view = new () { ShadowStyle = style };
- view.Margin!.ShadowSize = new (2, 2);
- Assert.Equal (expectedLength1, view.Margin!.ShadowSize.Width);
- Assert.Equal (expectedLength1, view.Margin.ShadowSize.Height);
- Assert.Equal (new (0, 0, expectedThickness1, expectedThickness1), view.Margin.Thickness);
- view.Margin!.ShadowSize = new (1, 1);
- Assert.Equal (expectedLength2, view.Margin!.ShadowSize.Width);
- Assert.Equal (expectedLength2, view.Margin.ShadowSize.Height);
- Assert.Equal (new (0, 0, expectedThickness2, expectedThickness2), view.Margin.Thickness);
- view.Margin!.ShadowSize = new (3, 3);
- Assert.Equal (expectedLength3, view.Margin!.ShadowSize.Width);
- Assert.Equal (expectedLength3, view.Margin.ShadowSize.Height);
- Assert.Equal (new (0, 0, expectedThickness3, expectedThickness3), view.Margin.Thickness);
- view.ShadowStyle = ShadowStyle.None;
- Assert.Equal (expectedLength3, view.Margin!.ShadowSize.Width);
- Assert.Equal (expectedLength3, view.Margin.ShadowSize.Height);
- Assert.Equal (new (0, 0, 0, 0), view.Margin.Thickness);
- }
- [Theory]
- [InlineData (ShadowStyle.None, 0, 1)]
- [InlineData (ShadowStyle.Opaque, 1, 1)]
- [InlineData (ShadowStyle.Transparent, 1, 1)]
- public void Changing_Thickness_Correctly_Set_Thickness (ShadowStyle style, int expectedLength, int expectedThickness)
- {
- View view = new () { ShadowStyle = style };
- Assert.Equal (new (0, 0, expectedLength, expectedLength), view.Margin!.Thickness);
- view.Margin!.Thickness = new (0, 0, 1, 1);
- Assert.Equal (expectedLength, view.Margin!.ShadowSize.Width);
- Assert.Equal (expectedLength, view.Margin.ShadowSize.Height);
- Assert.Equal (new (0, 0, expectedThickness, expectedThickness), view.Margin.Thickness);
- }
- [Fact]
- public void ShadowStyle_Transparent_Handles_Wide_Glyphs_Correctly ()
- {
- IApplication app = Application.Create ();
- app.Init ("fake");
- app.Driver?.SetScreenSize (6, 5);
- // Using a replacement char to make sure wide glyphs are handled correctly
- // in the shadow area, to not confusing with a space char.
- app.Driver?.GetOutputBuffer ().SetWideGlyphReplacement (Rune.ReplacementChar);
- Runnable superview = new () { Width = Dim.Fill (), Height = Dim.Fill () };
- superview.Text = """
- ๐๐๐
- ๐๐๐
- ๐๐๐
- ๐๐๐
- ๐๐๐
- """;
- View view = new () { Width = Dim.Fill (), Height = Dim.Fill (), BorderStyle = LineStyle.Single, ShadowStyle = ShadowStyle.Transparent };
- view.Margin!.ShadowSize = view.Margin!.ShadowSize with { Width = 2 };
- superview.Add (view);
- app.Begin (superview);
- Assert.Equal (new (2, 1), view.Margin!.ShadowSize);
- Assert.Equal (new (0, 0, 2, 1), view.Margin!.Thickness);
- DriverAssert.AssertDriverContentsAre (
- """
- โโโโ๐
- โ โ๐
- โ โ๐
- โโโโ๐
- ๏ฟฝ ๐๐
- """,
- output,
- app.Driver);
- view.Margin!.ShadowSize = new (1, 2);
- app.LayoutAndDraw ();
- Assert.Equal (new (1, 2), view.Margin!.ShadowSize);
- Assert.Equal (new (0, 0, 2, 2), view.Margin!.Thickness);
- DriverAssert.AssertDriverContentsAre (
- """
- โโโโ๐
- โ โ๏ฟฝ
- โโโโ๏ฟฝ
- ๏ฟฝ ๐๐
- ๏ฟฝ ๐๐
- """,
- output,
- app.Driver);
- view.Width = Dim.Fill (1);
- app.LayoutAndDraw ();
- Assert.Equal (new (1, 2), view.Margin!.ShadowSize);
- Assert.Equal (new (0, 0, 2, 2), view.Margin!.Thickness);
- DriverAssert.AssertDriverContentsAre (
- """
- โโโ ๐
- โ โ ๏ฟฝ
- โโโ ๏ฟฝ
- ๏ฟฝ ๐๏ฟฝ
- ๏ฟฝ ๐๏ฟฝ
- """,
- output,
- app.Driver);
- }
- [Fact]
- public void ShadowStyle_Opaque_Change_Thickness_On_Mouse_Pressed_Released ()
- {
- IApplication app = Application.Create ();
- app.Init ("fake");
- app.Driver?.SetScreenSize (10, 4);
- Runnable superview = new () { Width = Dim.Fill (), Height = Dim.Fill () };
- View view = new () { Width = 7, Height = 2, ShadowStyle = ShadowStyle.Opaque, Text = "| Hi |", HighlightStates = MouseState.Pressed };
- superview.Add (view);
- app.Begin (superview);
- DriverAssert.AssertDriverContentsAre (
- """
- | Hi |โ
- โโโโโโโ
- """,
- output,
- app.Driver);
- app.Mouse.RaiseMouseEvent (new () { ScreenPosition = new (2, 0), Flags = MouseFlags.Button1Pressed });
- app.LayoutAndDraw ();
- DriverAssert.AssertDriverContentsAre (
- """
- | Hi |
- """,
- output,
- app.Driver);
- app.Mouse.RaiseMouseEvent (new () { ScreenPosition = new (2, 0), Flags = MouseFlags.Button1Released });
- app.LayoutAndDraw ();
- DriverAssert.AssertDriverContentsAre (
- """
- | Hi |โ
- โโโโโโโ
- """,
- output,
- app.Driver);
- }
- [Fact]
- public void ShadowStyle_Transparent_Never_Throws_Navigating_Outside_Bounds ()
- {
- IApplication app = Application.Create ();
- app.Init ("fake");
- app.Driver?.SetScreenSize (6, 5);
- Runnable superview = new () { Width = Dim.Fill (), Height = Dim.Fill () };
- superview.Text = """
- ๐๐๐
- ๐๐๐
- ๐๐๐
- ๐๐๐
- ๐๐๐
- """;
- View view = new ()
- {
- Width = Dim.Fill (), Height = Dim.Fill (), BorderStyle = LineStyle.Single, ShadowStyle = ShadowStyle.Transparent,
- Arrangement = ViewArrangement.Movable, CanFocus = true
- };
- view.Margin!.ShadowSize = view.Margin!.ShadowSize with { Width = 2 };
- superview.Add (view);
- app.Begin (superview);
- Assert.Equal (new (0, 0), view.Frame.Location);
- Assert.True (app.Keyboard.RaiseKeyDownEvent (Key.F5.WithCtrl));
- int i = 0;
- DecrementValue (-10, Key.CursorLeft);
- Assert.Equal (-10, i);
- IncrementValue (0, Key.CursorRight);
- Assert.Equal (0, i);
- DecrementValue (-10, Key.CursorUp);
- Assert.Equal (-10, i);
- IncrementValue (20, Key.CursorDown);
- Assert.Equal (20, i);
- DecrementValue (0, Key.CursorUp);
- Assert.Equal (0, i);
- IncrementValue (20, Key.CursorRight);
- Assert.Equal (20, i);
- return;
- void DecrementValue (int count, Key key)
- {
- for (; i > count; i--)
- {
- Assert.True (app.Keyboard.RaiseKeyDownEvent (key));
- app.LayoutAndDraw ();
- CheckAssertion (new (i - 1, 0), new (0, i - 1), key);
- }
- }
- void IncrementValue (int count, Key key)
- {
- for (; i < count; i++)
- {
- Assert.True (app.Keyboard.RaiseKeyDownEvent (key));
- app.LayoutAndDraw ();
- CheckAssertion (new (i + 1, 0), new (0, i + 1), key);
- }
- }
- bool? IsColumn (Key key)
- {
- if (key == Key.CursorLeft || key == Key.CursorRight)
- {
- return true;
- }
- if (key == Key.CursorUp || key == Key.CursorDown)
- {
- return false;
- }
- return null;
- }
- void CheckAssertion (Point colLocation, Point rowLocation, Key key)
- {
- bool? isCol = IsColumn (key);
- switch (isCol)
- {
- case true:
- Assert.Equal (colLocation, view.Frame.Location);
- break;
- case false:
- Assert.Equal (rowLocation, view.Frame.Location);
- break;
- default:
- throw new InvalidOperationException ();
- }
- }
- }
- [Theory]
- [InlineData (ShadowStyle.None, 3, 4, 4)]
- [InlineData (ShadowStyle.Opaque, 4, 5, 4)]
- [InlineData (ShadowStyle.Transparent, 4, 5, 4)]
- public void Margin_Thickness_Changes_Adjust_Correctly (ShadowStyle style, int expectedThickness, int expectedThicknessAdjust, int expectedThicknessNone)
- {
- var view = new View ();
- view.Margin!.Thickness = new (3);
- view.ShadowStyle = style;
- Assert.Equal (new (3, 3, expectedThickness, expectedThickness), view.Margin.Thickness);
- view.Margin.Thickness = new (3, 3, expectedThickness + 1, expectedThickness + 1);
- Assert.Equal (new (3, 3, expectedThicknessAdjust, expectedThicknessAdjust), view.Margin.Thickness);
- view.ShadowStyle = ShadowStyle.None;
- Assert.Equal (new (3, 3, expectedThicknessNone, expectedThicknessNone), view.Margin.Thickness);
- view.Dispose ();
- }
- [Fact]
- public void Runnable_View_Overlap_Other_Runnables ()
- {
- IApplication app = Application.Create ();
- app.Init ("fake");
- app.Driver?.SetScreenSize (10, 5);
- Runnable superview = new () { Width = Dim.Fill (), Height = Dim.Fill (), Text = "๐".Repeat (25)! };
- View view = new () { Width = 7, Height = 2, ShadowStyle = ShadowStyle.Opaque, Text = "| Hi |" };
- superview.Add (view);
- app.Begin (superview);
- DriverAssert.AssertDriverContentsAre (
- """
- | Hi |โ ๐
- โโโโโโโ ๐
- ๐๐๐๐๐
- ๐๐๐๐๐
- ๐๐๐๐๐
- """,
- output,
- app.Driver);
- Runnable modalSuperview = new () { Y = 1, Width = Dim.Fill (), Height = 4, BorderStyle = LineStyle.Single };
- View view1 = new () { Width = 8, Height = 2, ShadowStyle = ShadowStyle.Opaque, Text = "| Hey |" };
- modalSuperview.Add (view1);
- app.Begin (modalSuperview);
- Assert.True (modalSuperview.IsModal);
- DriverAssert.AssertDriverContentsAre (
- """
- | Hi |โ ๐
- โโโโโโโโโโ
- โ| Hey |โโ
- โโโโโโโโโโ
- โโโโโโโโโโ
- """,
- output,
- app.Driver);
- app.Dispose ();
- }
- [Fact]
- public void TransparentShadow_Draws_Transparent_At_Driver_Output ()
- {
- // Arrange
- using IApplication app = Application.Create ();
- app.Init ("fake");
- app.Driver!.SetScreenSize (2, 1);
- app.Driver.Force16Colors = true;
- using Runnable superView = new ();
- superView.Width = Dim.Fill ();
- superView.Height = Dim.Fill ();
- superView.Text = "AB";
- superView.TextFormatter.WordWrap = true;
- superView.SetScheme (new (new Attribute (Color.Black, Color.White)));
- // Create view with transparent shadow
- View viewWithShadow = new ()
- {
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- Text = "*",
- ShadowStyle = ShadowStyle.Transparent
- };
- // Make it so the margin is only on the right for simplicity
- viewWithShadow.Margin!.Thickness = new (0, 0, 1, 0);
- viewWithShadow.SetScheme (new (new Attribute (Color.Black, Color.White)));
- superView.Add (viewWithShadow);
- // Act
- app.Begin (superView);
- app.LayoutAndDraw ();
- app.Driver.Refresh ();
- // Assert
- Assert.Equal (new (0, 0, 2, 2), viewWithShadow.Frame);
- Assert.Equal (new (0, 0, 1, 1), viewWithShadow.Viewport);
- _output.WriteLine ("Actual driver contents:");
- _output.WriteLine (app.Driver.ToString ());
- _output.WriteLine ("\nActual driver output:");
- string? output = app.Driver.GetOutput ().GetLastOutput ();
- _output.WriteLine (output);
- // Printed with bright black (dark gray) text on bright black (dark gray) background making it invisible
- DriverAssert.AssertDriverOutputIs ("""
- \x1b[30m\x1b[107m*\x1b[90m\x1b[100mB
- """, _output, app.Driver);
- }
- [Fact]
- public void TransparentShadow_OverWide_Draws_Transparent_At_Driver_Output ()
- {
- // Arrange
- using IApplication app = Application.Create ();
- app.Init ("fake");
- app.Driver!.SetScreenSize (2, 3);
- app.Driver.Force16Colors = true;
- using Runnable superView = new ();
- superView.Width = Dim.Fill ();
- superView.Height = Dim.Fill ();
- superView.Text = "๐๐๐๐";
- superView.TextFormatter.WordWrap = true;
- superView.SetScheme (new (new Attribute (Color.Black, Color.White)));
- // Create view with transparent shadow
- View viewWithShadow = new ()
- {
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- Text = "*",
- ShadowStyle = ShadowStyle.Transparent
- };
- // Make it so the margin is only on the bottom for simplicity
- viewWithShadow.Margin!.Thickness = new (0, 0, 0, 1);
- viewWithShadow.SetScheme (new (new Attribute (Color.Black, Color.White)));
- superView.Add (viewWithShadow);
- // Act
- app.Begin (superView);
- app.LayoutAndDraw ();
- app.Driver.Refresh ();
- // Assert
- _output.WriteLine ("Actual driver contents:");
- _output.WriteLine (app.Driver.ToString ());
- _output.WriteLine ("\nActual driver output:");
- string? output = app.Driver.GetOutput ().GetLastOutput ();
- _output.WriteLine (output);
- DriverAssert.AssertDriverOutputIs ("""
- \x1b[30m\x1b[107m*\x1b[90m\x1b[103m \x1b[97m\x1b[40m \x1b[90m\x1b[100m \x1b[97m\x1b[40m๐
- """, _output, app.Driver);
- }
- }
|