Files
Terminal.Gui/Tests/UnitTests/View/Draw/ClearViewportTests.cs
Tig b0f32811eb Fixes #3930 - Splits tests to Tests/UnitTests, Tests/IntegrationTests, Tests/StressTests (#3954)
* Tons of API doc updates

* Removed stale test

* Removed stale tests

* Fixed Skipped Shadow test 1

* Fixed Skipped Shadow test 2

* Fixed Skipped Shadow test 3

* Removed stale test

* Removed stale test2

* Explicit unregister of event handler on Application.Driver!.ClearedContents

* Added Toplevels to dict

* code cleanup

* spelling error

* Removed stale test3

* Removed stale test4

* Removed stale test5

* added script

* tweaked script

* tweaked script

* Created StressTests project; moved some tests

* Created IntegrationTests project; moved some tests

* New yml

* made old yml just unit tests

* Tweaked Button_IsDefault_Raises_Accepted_Correctly

* tweaked script

* cleaned up ymls

* tweakled up ymls

* stress tests...

* stress tests on ubuntu only

* Fixed WindowsDriver in InvokeLeakTest

* Fixed WindowsDriver in InvokeLeakTest2

* Added Directory.Packages.props.
Added Directory.Build.props

* Shortened StressTest time

* Removed dupe file.

* DemoFiles

* Moved all tests to ./Tests dir.

* Fixed release build issue

* Fixed .sln file

* Fixed .sl* files

* Fixing ymls

* Fixing interation tests

* Create link to the file TestHelpers.

* Created Tests/UnitTestsParallelizable.
Moved all obviously parallelizable tests.
Updated yml.

* fixing logs

* fixing logs2

* fixing logs3

* don't require stress to pass for PRs

* Fix a failure?

* tweaked script

* Coudl this be it?

* Moved tons of tests to parallelizable

* Fixed some stuff

* Script to find duplicate tests

* Testing workflows

* Updated to v4

* Fix RelativeBasePath issue

* Replace powershell to pwsh

* Add ignore projects.

* Removed dupe unit tests

* Code cleanup of tests

* Cleaned up test warnings

* yml tweak

* Moved setter

* tweak ymls

* just randomly throwing spaghetti at a wall

* Enable runing 5 test runners in par

* Turned off DEBUG_DISPOSABLE for par tests

* RunningUnitTests=true

* code cleanup (forcing more Action runs)

* DISABLE_DEBUG_IDISPOSABLE

* Added View.DebugIDisposable. False by default.

* Remobed bogus tareet

* Remobed bogus tareet2

* fixed warning

* added api doc

* fixed warning

* fixed warning

* fixed warning2

* fixed warning3

* fixed warning4

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2025-03-05 23:44:27 -07:00

414 lines
12 KiB
C#

#nullable enable
using Moq;
using UnitTests;
using UnitTests;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewTests;
[Trait ("Category", "Output")]
public class ClearViewportTests (ITestOutputHelper _output)
{
public class TestableView : View
{
public bool TestOnClearingViewport () { return OnClearingViewport (); }
public int OnClearingViewportCalled { get; set; } = 0;
public bool CancelOnClearingViewport { get; set; }
protected override bool OnClearingViewport ()
{
OnClearingViewportCalled++;
return CancelOnClearingViewport;
}
public int OnClearedViewportCalled { get; set; } = 0;
protected override void OnClearedViewport ()
{
OnClearedViewportCalled++;
}
}
[Fact]
public void DoClearViewport_ViewportIsTransparent_DoesNotClear ()
{
// Arrange
Mock<TestableView> view = new Mock<TestableView> { CallBase = true };
view.Object.ViewportSettings = ViewportSettings.Transparent;
// Act
view.Object.DoClearViewport ();
// Assert
Assert.Equal (0, view.Object.OnClearingViewportCalled);
Assert.Equal (0, view.Object.OnClearedViewportCalled);
}
[Fact]
public void DoClearViewport_OnClearingViewportReturnsTrue_DoesNotClear ()
{
// Arrange
Mock<TestableView> view = new Mock<TestableView> { CallBase = true };
view.Object.CancelOnClearingViewport = true;
// Act
view.Object.DoClearViewport ();
// Assert
Assert.Equal (0, view.Object.OnClearedViewportCalled);
}
[Fact]
public void DoClearViewport_ClearingViewportEventCancelled_DoesNotClear ()
{
// Arrange
Mock<TestableView> view = new Mock<TestableView> { CallBase = true };
view.Object.ClearingViewport += (sender, e) => e.Cancel = true;
// Act
view.Object.DoClearViewport ();
// Assert
Assert.Equal (0, view.Object.OnClearedViewportCalled);
}
[Fact]
public void DoClearViewport_ClearsViewport ()
{
// Arrange
Mock<TestableView> view = new Mock<TestableView> { CallBase = true };
// Act
view.Object.DoClearViewport ();
// Assert
Assert.Equal (1, view.Object.OnClearedViewportCalled);
}
[Fact]
public void DoClearViewport_RaisesClearingViewportEvent ()
{
// Arrange
Mock<TestableView> view = new Mock<TestableView> { CallBase = true };
var eventRaised = false;
view.Object.ClearingViewport += (sender, e) => eventRaised = true;
// Act
view.Object.DoClearViewport ();
// Assert
Assert.True (eventRaised);
}
[Fact]
[SetupFakeDriver]
public void Clear_ClearsEntireViewport ()
{
var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
var view = new View
{
Text = "X",
X = 1, Y = 1,
Width = 3, Height = 3,
BorderStyle = LineStyle.Single
};
superView.Add (view);
superView.BeginInit ();
superView.EndInit ();
superView.LayoutSubviews ();
superView.Draw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
┌─┐
│X│
└─┘",
_output);
// On Draw exit the view is excluded from the clip, so this will do nothing.
view.ClearViewport ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
┌─┐
│X│
└─┘",
_output);
View.SetClipToScreen ();
view.ClearViewport ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
┌─┐
│ │
└─┘",
_output);
}
[Fact]
[SetupFakeDriver]
public void Clear_WithClearVisibleContentOnly_ClearsVisibleContentOnly ()
{
var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
var view = new View
{
Text = "X",
X = 1, Y = 1,
Width = 3, Height = 3,
BorderStyle = LineStyle.Single,
ViewportSettings = ViewportSettings.ClearContentOnly
};
superView.Add (view);
superView.BeginInit ();
superView.EndInit ();
superView.LayoutSubviews ();
superView.Draw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
┌─┐
│X│
└─┘",
_output);
View.SetClipToScreen ();
view.ClearViewport ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
┌─┐
│ │
└─┘",
_output);
}
[Fact]
[AutoInitShutdown]
public void Clear_Viewport_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
{
var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
view.DrawingContent += (s, e) =>
{
Region savedClip = view.AddViewportToClip ();
for (var row = 0; row < view.Viewport.Height; row++)
{
Application.Driver?.Move (1, row + 1);
for (var col = 0; col < view.Viewport.Width; col++)
{
Application.Driver?.AddStr ($"{col}");
}
}
View.SetClip (savedClip);
e.Cancel = true;
};
var top = new Toplevel ();
top.Add (view);
Application.Begin (top);
((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
var expected = @"
┌──────────────────┐
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
└──────────────────┘
"
;
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, _output);
Assert.Equal (new (0, 0, 20, 10), pos);
view.FillRect (view.Viewport);
expected = @"
┌──────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────┘
"
;
pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, _output);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
{
var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
view.DrawingContent += (s, e) =>
{
Region savedClip = view.AddViewportToClip ();
for (var row = 0; row < view.Viewport.Height; row++)
{
Application.Driver?.Move (1, row + 1);
for (var col = 0; col < view.Viewport.Width; col++)
{
Application.Driver?.AddStr ($"{col}");
}
}
View.SetClip (savedClip);
e.Cancel = true;
};
var top = new Toplevel ();
top.Add (view);
Application.Begin (top);
((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
var expected = @"
┌──────────────────┐
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
│012345678910111213│
└──────────────────┘
"
;
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, _output);
Assert.Equal (new (0, 0, 20, 10), pos);
view.FillRect (view.Viewport);
expected = @"
┌──────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────┘
";
pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, _output);
top.Dispose ();
}
[Theory]
[AutoInitShutdown (configLocation: ConfigLocations.Default)]
[InlineData (true)]
[InlineData (false)]
public void Clear_Does_Not_Spillover_Its_Parent (bool label)
{
var root = new View { Width = 20, Height = 10, ColorScheme = Colors.ColorSchemes ["Base"] };
string text = new ('c', 100);
View v = label
// Label has Width/Height == AutoSize, so Frame.Size will be (100, 1)
? new Label { Text = text }
// TextView has Width/Height == (Dim.Fill, 1), so Frame.Size will be 20 (width of root), 1
: new TextView { Width = Dim.Fill (), Height = 1, Text = text };
root.Add (v);
var top = new Toplevel ();
top.Add (root);
RunState runState = Application.Begin (top);
Application.RunIteration (ref runState);
if (label)
{
Assert.False (v.CanFocus);
Assert.Equal (new (0, 0, text.Length, 1), v.Frame);
}
else
{
Assert.True (v.CanFocus);
Assert.Equal (new (0, 0, 20, 1), v.Frame);
}
DriverAssert.AssertDriverContentsWithFrameAre (
@"
cccccccccccccccccccc",
_output
);
Attribute [] attributes =
{
Colors.ColorSchemes ["TopLevel"].Normal,
Colors.ColorSchemes ["Base"].Normal,
Colors.ColorSchemes ["Base"].Focus
};
if (label)
{
DriverAssert.AssertDriverAttributesAre (
@"
111111111111111111110
111111111111111111110",
_output,
Application.Driver,
attributes
);
}
else
{
DriverAssert.AssertDriverAttributesAre (
@"
222222222222222222220
111111111111111111110",
_output,
Application.Driver,
attributes
);
}
if (label)
{
root.CanFocus = true;
v.CanFocus = true;
Assert.True (v.HasFocus);
v.SetFocus ();
Assert.True (v.HasFocus);
Application.LayoutAndDraw ();
DriverAssert.AssertDriverAttributesAre (
@"
222222222222222222220
111111111111111111110",
_output,
Application.Driver,
attributes
);
}
Application.End (runState);
top.Dispose ();
}
}