Files
Terminal.Gui/Tests/UnitTestsParallelizable/View/Draw/NeedsDrawTests.cs
Tig a6258ed398 Updates IListDataSource.Render to rename the start parameter to viewportXOffset (#4392)
* Add comprehensive unit tests for WindowsKeyConverter

- Implement 118 parallelizable unit tests for WindowsKeyConverter

- Cover ToKey and ToKeyInfo methods with full bidirectional testing

- Test basic characters, modifiers, special keys, function keys

- Test VK_PACKET Unicode/IME input

- Test OEM keys, NumPad keys, and lock states

- Include round-trip conversion tests

- All tests passing successfully

Fixes #4389

* Rename `start` parameter to `viewportXOffset` for clarity

The `start` parameter in several methods and interfaces has been
renamed to `viewportXOffset` to better reflect its purpose as the
horizontal offset of the viewport during string rendering.

- Updated method signatures in `ListViewWithSelection` to use
  `viewportXOffset` instead of `start`, including default values.
- Modified the `RenderUstr` method in `ListViewWithSelection` to
  use `viewportXOffset` for calculating the starting index.
- Renamed the `start` parameter to `viewportXOffset` in the
  `IListDataSource` interface and updated its documentation.
- Replaced all occurrences of `start` with `viewportXOffset` in
  the `ListWrapper<T>` class, including method calls and logic.
- Updated the `RenderUstr` method in `ListWrapper<T>` to use
  `viewportXOffset` for substring calculations.
- Adjusted the test method in `ListViewTests.cs` to reflect the
  parameter name change.

These changes improve code readability and make the parameter's
role in rendering logic more explicit.

* Remove WindowsKeyConverterTests class that was added by mistake

* Modernized ListView and IListDataSource - Tons of new unit tests

Refactored `ListView` and `IListDataSource` to improve readability, maintainability, and functionality. Introduced `ListWrapper<T>` as a default implementation of `IListDataSource` for easier integration with standard collections.

Enhanced `ListView` with better handling of marking, selection, and scrolling. Replaced `viewportXOffset` with `viewportX` for horizontal scrolling. Added `EnsureSelectedItemVisible` to maintain visibility of the selected item.

Updated `IListDataSource` with detailed XML documentation and added `SuspendCollectionChangedEvent` for bulk updates. Improved null safety with nullable reference types.

Added comprehensive unit tests for `ListWrapper<T>` and `IListDataSource` to ensure robustness. Modernized the codebase with C# features like expression-bodied members and pattern matching. Fixed bugs related to `SelectedItem` validation and rendering artifacts.

* Improve index validation in ComboBox and ListView

Enhance robustness by adding stricter checks for valid indices
in ComboBox and ListView. Updated conditions in the
`_listview.SelectedItemChanged` event handler to ensure `e.Item`
is non-negative before accessing `_searchSet`. Modified the
`SetValue` method to use `e.Item` instead of `_listview.SelectedItem`.

In ListView, updated the `OnSelectedChanged` method to validate
that `SelectedItem` is non-negative (`>= 0`) before accessing
the `Source` list. These changes prevent potential out-of-range
errors and improve code safety.

* Refactor and enhance test coverage across modules

Refactored and added new tests to improve coverage, readability, and consistency across multiple test files. Key changes include:

- **ShortcutTests.cs**: Added tests for `BindKeyToApplication` and removed redundant tests.
- **SourcesManagerTests.cs**: Renamed `Update_*` tests to `Load_*` for clarity.
- **ArrangementTests.cs**: Reintroduced `MouseGrabHandler` tests, added `ViewArrangement` flag tests, and improved structure.
- **NeedsDrawTests.cs**: Replaced `Application.Screen.Size` with fixed dimensions for better isolation.
- **DimAutoTests.cs**: Updated layout tests to use fixed dimensions.
- **FrameTests.cs**: Standardized object initialization and validated frame behavior.
- **SubViewTests.cs**: Improved formatting and modernized event handling.
- **NumericUpDownTests.cs**: Decoupled layout tests from screen size.

General improvements:
- Enhanced formatting and removed redundant tests.
- Added comments for clarity.
- Introduced `ITestOutputHelper` for better debugging in `ArrangementTests`.

* Refactor to use nullable types for better null safety

Enabled nullable reference types across the codebase to improve null safety and prevent potential null reference issues. Refactored `SelectedItem` and related properties from `int` to `int?` to represent no selection with `null` instead of `-1`. Updated logic, event arguments, and method signatures to handle nullable values consistently.

Simplified object initialization using modern C# syntax and improved code readability with interpolated strings. Added null checks and early returns to prevent runtime errors. Enhanced error handling by throwing `ArgumentOutOfRangeException` for invalid values.

Updated tests to reflect the changes, replacing assertions for `-1` with `null` and ensuring proper handling of nullable values. Cleaned up redundant code and improved formatting for better maintainability.

* on` functionality has been deprecated, refactored, or removed from the `Shortcut` class.

* Refactor: Transition to instance-based architecture

Updated `Run-LocalCoverage.ps1` to increase `--blame-hang-timeout` from 10s to 60s. Improved null safety in `GuiTestContext` by adding null-conditional operators. Commented out problematic code in `SetupFakeApplicationAttribute.cs` to prevent test hangs.

Excluded `ViewBase` files from `UnitTests.Parallelizable.csproj` and removed redundant folder declarations. Simplified event handling in `IListDataSourceTests.cs` and updated `ListViewTests.cs` to use nullable reference types.

Enhanced documentation to emphasize the transition to an instance-based application architecture. Updated examples in `application.md`, `multitasking.md`, and `navigation.md` to reflect the use of `Application.Create()` and `View.App`. Clarified the obsolescence of the static `Application` class.

Revised table of contents in `toc.yml` to include new sections like "Application Deep Dive" and "Scheme Deep Dive." Added `dotnet-tools.json` for tool configuration.

These changes improve maintainability, testability, and alignment with modern C# practices.

* Refactor ListViewTests to use Terminal.Gui framework

The `ListViewTests` class has been refactored to replace the `AutoInitShutdown` attribute with explicit application lifecycle management using `IApplication` and `app.Init()` from the `Terminal.Gui` framework.

Key changes include:
- Rewriting tests to use `Terminal.Gui`'s application lifecycle.
- Adding a private `_output` field for logging test output via `ITestOutputHelper`.
- Updating `DriverAssert.AssertDriverContentsWithFrameAre` to include `app.Driver` for UI verification.
- Rewriting tests like `Clicking_On_Border_Is_Ignored`, `EnsureSelectedItemVisible_SelectedItem`, and others to align with the new framework.
- Adding explicit calls to `app.Shutdown()` for proper cleanup.
- Enabling nullable reference types with `#nullable enable`.
- Updating `using` directives and `namespace` to reflect the new structure.

These changes improve test maintainability, compatibility, and diagnostics.

* Update Terminal.Gui/Views/CollectionNavigation/CollectionNavigatorBase.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Terminal.Gui/Views/CollectionNavigation/CollectionNavigatorBase.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/UICatalogTop.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Terminal.Gui/Views/ListWrapper.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Terminal.Gui/Views/ListWrapper.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Updated the `SetMark` method to return `Source.IsMarked(SelectedItem.Value)` for consistency and removed an outdated comment questioning its correctness.

Enhanced the exception message in the `SelectedItem` property setter to provide clearer guidance when the value is out of range.

* Add comprehensive ListView behavior test coverage

Added multiple test methods to validate `ListView` behavior:
- `Vertical_ScrollBar_Hides_And_Shows_As_Needed`: Ensures the vertical scrollbar auto-hides/shows based on content height.
- `Mouse_Wheel_Scrolls`: Verifies vertical scrolling with the mouse wheel updates `TopItem`.
- `SelectedItem_With_Source_Null_Does_Nothing`: Confirms no exceptions occur when setting `SelectedItem` with a `null` source.
- `Horizontal_Scroll`: Tests horizontal scrolling, including programmatic and mouse wheel interactions, ensuring `LeftItem` updates correctly.
- `SetSourceAsync_SetsSource`: Validates the asynchronous `SetSourceAsync` method updates the source and item count.
- `AllowsMultipleSelection_Set_To_False_Unmarks_All_But_Selected`: Ensures disabling multiple selection unmarks all but the selected item.
- `Source_CollectionChanged_Remove`: Confirms `SelectedItem` and source count update correctly when items are removed from the source collection.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-19 20:39:34 -05:00

315 lines
9.6 KiB
C#

#nullable enable
using UnitTests;
namespace UnitTests_Parallelizable.ViewTests;
[Trait ("Category", "Output")]
public class NeedsDrawTests : FakeDriverBase
{
[Fact]
public void NeedsDraw_False_If_Width_Height_Zero ()
{
View view = new () { Width = 0, Height = 0 };
view.BeginInit ();
view.EndInit ();
Assert.False (view.NeedsDraw);
//Assert.False (view.SubViewNeedsDraw);
}
[Fact]
public void NeedsDraw_True_Initially_If_Width_Height_Not_Zero ()
{
View superView = new () { Driver = CreateFakeDriver (), Width = 1, Height = 1 };
View view1 = new () { Width = 1, Height = 1 };
View view2 = new () { Width = 1, Height = 1 };
superView.Add (view1, view2);
superView.BeginInit ();
superView.EndInit ();
Assert.True (superView.NeedsDraw);
Assert.True (superView.SubViewNeedsDraw);
Assert.True (view1.NeedsDraw);
Assert.True (view2.NeedsDraw);
superView.Layout (); // NeedsDraw is always false if Layout is needed
superView.Draw ();
Assert.False (superView.NeedsDraw);
Assert.False (superView.SubViewNeedsDraw);
Assert.False (view1.NeedsDraw);
Assert.False (view2.NeedsDraw);
superView.SetNeedsDraw ();
Assert.True (superView.NeedsDraw);
Assert.True (superView.SubViewNeedsDraw);
Assert.True (view1.NeedsDraw);
Assert.True (view2.NeedsDraw);
}
[Fact]
public void NeedsDraw_True_After_Constructor ()
{
var view = new View { Width = 2, Height = 2 };
Assert.True (view.NeedsDraw);
view = new () { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
Assert.True (view.NeedsDraw);
}
[Fact]
public void NeedsDraw_True_After_BeginInit ()
{
var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
Assert.True (view.NeedsDraw);
view.BeginInit ();
Assert.True (view.NeedsDraw);
view.NeedsDraw = false;
view.BeginInit ();
Assert.False (view.NeedsDraw); // Because layout is still needed
view.Layout ();
// NeedsDraw is true after layout and NeedsLayout is false if SubViewsLaidOut doesn't call SetNeedsLayout
Assert.True (view.NeedsDraw);
Assert.False (view.NeedsLayout);
}
[Fact]
public void NeedsDraw_True_After_EndInit_Where_Call_Layout ()
{
var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
Assert.True (view.NeedsDraw);
view.BeginInit ();
Assert.True (view.NeedsDraw);
view.EndInit ();
Assert.True (view.NeedsDraw);
view = new () { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
view.BeginInit ();
view.NeedsDraw = false;
view.EndInit ();
Assert.True (view.NeedsDraw);
}
[Fact]
public void NeedsDraw_After_SetLayoutNeeded_And_Layout ()
{
var view = new View { Driver = CreateFakeDriver (), Width = 2, Height = 2 };
Assert.True (view.NeedsDraw);
Assert.False (view.NeedsLayout);
view.Draw ();
Assert.False (view.NeedsDraw);
Assert.False (view.NeedsLayout);
view.SetNeedsLayout ();
Assert.False (view.NeedsDraw);
Assert.True (view.NeedsLayout);
view.Layout ();
Assert.True (view.NeedsDraw);
Assert.False (view.NeedsLayout);
}
[Fact]
public void NeedsDraw_False_After_SetRelativeLayout_Absolute_Dims ()
{
var view = new View { Driver = CreateFakeDriver (), Width = 2, Height = 2 };
Assert.True (view.NeedsDraw);
view.Draw ();
Assert.False (view.NeedsDraw);
Assert.False (view.NeedsLayout);
// SRL won't change anything since the view frame wasn't changed
view.SetRelativeLayout (new (100, 100));
Assert.False (view.NeedsDraw);
view.SetNeedsLayout ();
// SRL won't change anything since the view frame wasn't changed
// SRL doesn't depend on NeedsLayout, but LayoutSubViews does
view.SetRelativeLayout (new (100, 100));
Assert.False (view.NeedsDraw);
Assert.True (view.NeedsLayout);
view.Layout ();
Assert.True (view.NeedsDraw);
Assert.False (view.NeedsLayout);
view.NeedsDraw = false;
// SRL won't change anything since the view frame wasn't changed. However, Layout has not been called
view.SetRelativeLayout (new (10, 10));
Assert.False (view.NeedsDraw);
}
[Fact]
public void NeedsDraw_False_After_SetRelativeLayout_Relative_Dims ()
{
var view = new View { Width = Dim.Percent (50), Height = Dim.Percent (50) };
View superView = new ()
{
Id = "superView",
Width = Dim.Fill (),
Height = Dim.Fill ()
};
// A layout wasn't called yet, so NeedsDraw is still empty
Assert.False (superView.NeedsDraw);
superView.Add (view);
// A layout wasn't called yet, so NeedsDraw is still empty
Assert.False (view.NeedsDraw);
Assert.False (superView.NeedsDraw);
superView.BeginInit ();
Assert.False (view.NeedsDraw);
Assert.False (superView.NeedsDraw);
superView.EndInit (); // Call Layout
Assert.True (view.NeedsDraw);
Assert.True (superView.NeedsDraw);
superView.SetRelativeLayout (new (100, 100));
Assert.True (view.NeedsDraw);
Assert.True (superView.NeedsDraw);
}
[Fact]
public void NeedsDraw_False_After_SetRelativeLayout_10x10 ()
{
View superView = new ()
{
Id = "superView",
Width = Dim.Fill (),
Height = Dim.Fill ()
};
Assert.False (superView.NeedsDraw);
superView.Layout ();
Assert.True (superView.NeedsDraw);
superView.NeedsDraw = false;
superView.SetRelativeLayout (new (10, 10));
Assert.True (superView.NeedsDraw);
}
[Fact]
public void NeedsDraw_True_After_LayoutSubViews ()
{
var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
Assert.True (view.NeedsDraw);
view.BeginInit ();
Assert.True (view.NeedsDraw);
view.EndInit ();
Assert.True (view.NeedsDraw);
view.SetRelativeLayout (new (100, 100));
Assert.True (view.NeedsDraw);
view.LayoutSubViews ();
Assert.True (view.NeedsDraw);
}
[Fact]
public void NeedsDraw_False_After_Draw ()
{
var view = new View { Driver = CreateFakeDriver (), Width = 2, Height = 2, BorderStyle = LineStyle.Single };
Assert.True (view.NeedsDraw);
view.BeginInit ();
Assert.True (view.NeedsDraw);
view.EndInit ();
Assert.True (view.NeedsDraw);
view.SetRelativeLayout (new (100, 100));
Assert.True (view.NeedsDraw);
view.LayoutSubViews ();
Assert.True (view.NeedsDraw);
view.Draw ();
Assert.False (view.NeedsDraw);
}
[Fact]
public void NeedsDrawRect_Is_Viewport_Relative ()
{
View superView = new ()
{
Id = "superView",
Width = 10,
Height = 10
};
Assert.Equal (new (0, 0, 10, 10), superView.Frame);
Assert.Equal (new (0, 0, 10, 10), superView.Viewport);
Assert.Equal (new (0, 0, 10, 10), superView.NeedsDrawRect);
var view = new View
{
Id = "view"
};
view.Frame = new (0, 1, 2, 3);
Assert.Equal (new (0, 1, 2, 3), view.Frame);
Assert.Equal (new (0, 0, 2, 3), view.Viewport);
Assert.Equal (new (0, 0, 2, 3), view.NeedsDrawRect);
superView.Add (view);
Assert.Equal (new (0, 0, 10, 10), superView.Frame);
Assert.Equal (new (0, 0, 10, 10), superView.Viewport);
Assert.Equal (new (0, 0, 10, 10), superView.NeedsDrawRect);
Assert.Equal (new (0, 1, 2, 3), view.Frame);
Assert.Equal (new (0, 0, 2, 3), view.Viewport);
Assert.Equal (new (0, 0, 2, 3), view.NeedsDrawRect);
view.Frame = new (3, 3, 5, 5);
Assert.Equal (new (3, 3, 5, 5), view.Frame);
Assert.Equal (new (0, 0, 5, 5), view.Viewport);
Assert.Equal (new (0, 0, 5, 5), view.NeedsDrawRect);
view.Frame = new (3, 3, 6, 6); // Grow right/bottom 1
Assert.Equal (new (3, 3, 6, 6), view.Frame);
Assert.Equal (new (0, 0, 6, 6), view.Viewport);
Assert.Equal (new (0, 0, 6, 6), view.NeedsDrawRect);
view.Frame = new (3, 3, 5, 5); // Shrink right/bottom 1
Assert.Equal (new (3, 3, 5, 5), view.Frame);
Assert.Equal (new (0, 0, 5, 5), view.Viewport);
Assert.Equal (new (0, 0, 5, 5), view.NeedsDrawRect);
view.SetContentSize (new (10, 10));
Assert.Equal (new (3, 3, 5, 5), view.Frame);
Assert.Equal (new (0, 0, 5, 5), view.Viewport);
Assert.Equal (new (0, 0, 5, 5), view.NeedsDrawRect);
view.Viewport = new (1, 1, 5, 5); // Scroll up/left 1
Assert.Equal (new (3, 3, 5, 5), view.Frame);
Assert.Equal (new (1, 1, 5, 5), view.Viewport);
Assert.Equal (new (0, 0, 5, 5), view.NeedsDrawRect);
view.Frame = new (3, 3, 6, 6); // Grow right/bottom 1
Assert.Equal (new (3, 3, 6, 6), view.Frame);
Assert.Equal (new (1, 1, 6, 6), view.Viewport);
Assert.Equal (new (1, 1, 6, 6), view.NeedsDrawRect);
view.Frame = new (3, 3, 5, 5);
Assert.Equal (new (3, 3, 5, 5), view.Frame);
Assert.Equal (new (1, 1, 5, 5), view.Viewport);
Assert.Equal (new (1, 1, 5, 5), view.NeedsDrawRect);
}
}