Files
Terminal.Gui/Tests/UnitTests/View/Draw/ClearViewportTests.cs
Copilot aef88ad4bb Fixes #4332 - Refactor test infrastructure to use modern ApplicationImpl.Coordinator instead of legacy MainLoop (#4335)
* Initial plan

* Refactor tests to use modern ApplicationImpl.Coordinator architecture instead of legacy Application.RunIteration

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Changes before error encountered

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Replace Application.RunIteration implementation to use modern ApplicationImpl.Coordinator architecture

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Simplify Application.RunIteration to directly delegate to ApplicationImpl.Coordinator without legacy infrastructure

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update multitasking.md documentation to use Application.AddTimeout/RemoveTimeout instead of deprecated Application.MainLoop methods

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Refactor contribution guidelines and documentation

- Consolidated all contribution-related instructions into `CONTRIBUTING.md` as the single source of truth.
- Updated `copilot-instructions.md` and `AGENTS.md` to reference `CONTRIBUTING.md`.
- Enhanced build, test, and CI/CD workflow documentation.
- Updated repository structure documentation in `README.md` and `CONTRIBUTING.md`.
- Reinforced coding conventions and streamlined testing requirements.
- Documented GitHub Actions workflows and provided local CI replication steps.
- Refined API documentation requirements and PR guidelines.
- Simplified `.sln` file and removed redundant content.
- Updated links to point to the latest documentation resources.

* Revamp bug report template for clarity and detail

Updated the bug report template to improve structure, readability, and comprehensiveness:
- Specified "Terminal.Gui" in the `about` section.
- Replaced bold headings with Markdown heading syntax.
- Enhanced "To Reproduce" with placeholders for code and behavior details.
- Added an "Environment" section to collect OS, terminal, PowerShell, .NET, and `Terminal.Gui` version details.
- Expanded "Screenshots" to include GIFs and terminal output instructions.
- Removed outdated "Desktop" and "Smartphone" sections, consolidating relevant details.
- Improved "Additional Context" with prompts for consistency, prior behavior, and error messages.
- Streamlined "For Maintainers" instructions for setting project and milestone.

These changes aim to make bug reports more actionable and easier to reproduce.

* Remove [Obsolete] attribute and pragma warnings from Application.RunIteration - method now uses modern architecture internally

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Refactor Application.Run.cs for clarity and cleanup

Removed debug assertions and unused/commented-out code to simplify logic and improve maintainability. Renamed `forceDraw` to `forceRedraw` in `LayoutAndDraw` for better clarity. Removed the internal `OnNotifyStopRunState` method and its associated logic, indicating a refactor of the stop notification mechanism.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Tig <tig@users.noreply.github.com>
Co-authored-by: tig <585482+tig@users.noreply.github.com>
2025-10-26 09:15:46 -06:00

415 lines
12 KiB
C#

#nullable enable
using Moq;
using UnitTests;
using Xunit.Abstractions;
namespace UnitTests.ViewTests;
[Trait ("Category", "Output")]
public class ClearViewportTests (ITestOutputHelper output)
{
public class TestableView : View
{
public bool TestOnClearingViewport () { return OnClearingViewport (); }
public int OnClearingViewportCalled { get; set; }
public bool CancelOnClearingViewport { get; set; }
protected override bool OnClearingViewport ()
{
OnClearingViewportCalled++;
return CancelOnClearingViewport;
}
public int OnClearedViewportCalled { get; set; }
protected override void OnClearedViewport () { OnClearedViewportCalled++; }
}
[Fact]
public void DoClearViewport_ViewportIsTransparent_DoesNotClear ()
{
// Arrange
Mock<TestableView> view = new () { CallBase = true };
view.Object.ViewportSettings = ViewportSettingsFlags.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 () { 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 () { 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 () { CallBase = true };
// Act
view.Object.DoClearViewport ();
// Assert
Assert.Equal (1, view.Object.OnClearedViewportCalled);
}
[Fact]
public void DoClearViewport_RaisesClearingViewportEvent ()
{
// Arrange
Mock<TestableView> view = new () { 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 = ViewportSettingsFlags.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 (), BorderStyle = LineStyle.Single };
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);
AutoInitShutdownAttribute.FakeResize(new Size(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 (), BorderStyle = LineStyle.Single };
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);
AutoInitShutdownAttribute.FakeResize(new Size(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 (Skip = "This test is too fragile; depends on Library Resoruces/Themes which can easily change.")]
[AutoInitShutdown]
[InlineData (true)]
[InlineData (false)]
public void Clear_Does_Not_Spillover_Its_Parent (bool label)
{
ConfigurationManager.Enable (ConfigLocations.LibraryResources);
View root = new () { Width = 20, Height = 10 };
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);
Toplevel top = new ();
top.Add (root);
RunState runState = Application.Begin (top);
AutoInitShutdownAttribute.RunIteration ();
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 =
{
SchemeManager.GetSchemes () ["TopLevel"]!.Normal,
SchemeManager.GetSchemes () ["Base"]!.Normal,
SchemeManager.GetSchemes () ["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 ();
CM.Disable (resetToHardCodedDefaults: true);
}
}