Skip to content

Commit 726b15d

Browse files
authored
Fixes #4389 - Add comprehensive unit tests for WindowsKeyConverter (#4390)
* 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 * Add test documenting VK_PACKET surrogate pair limitation VK_PACKET sends surrogate pairs (e.g., emoji) as two separate events. Current WindowsKeyConverter processes each independently without combining. Test documents this limitation for future fix at InputProcessor level. * Mark WindowsKeyConverterTests as Windows-only Tests now skip on non-Windows platforms using SkipException in constructor. This prevents CI failures on macOS and Linux where Windows Console API is not available. * Properly mark WindowsKeyConverter tests as Windows-only Created custom xUnit attributes SkipOnNonWindowsFact and SkipOnNonWindowsTheory that automatically skip tests on non-Windows platforms. This prevents CI failures on macOS and Linux. * Refactor and enhance test coverage for input processors Refactored `ApplicationImpl.cs` to simplify its structure by removing the `_stopAfterFirstIteration` field. Reintroduced and modernized test files with improved structure and coverage: - `NetInputProcessorTests.cs`: Added tests for `ConsoleKeyInfo` to `Rune`/`Key` conversion and queue processing. - `WindowSizeMonitorTests.cs`: Added tests for size change event handling. - `WindowsInputProcessorTests.cs`: Added tests for keyboard and mouse input processing, including mouse flag mapping. - `WindowsKeyConverterTests.cs`: Added comprehensive tests for `InputRecord` to `Key` conversion, covering OEM keys, modifiers, Unicode, and round-trip integrity. Improved test coverage for edge cases, introduced parameterized tests, and documented known limitations for future improvements. * Use Trait-based platform filtering for WindowsKeyConverter tests Added [Trait('Platform', 'Windows')] and [Collection('Global Test Setup')] attributes. Tests will run on Windows but can be filtered in CI on other platforms using --filter 'Platform!=Windows' if needed. This approach doesn't interfere with GlobalTestSetup and works correctly with xUnit. * Filter Windows-specific tests on non-Windows CI platforms Added --filter 'Platform!=Windows' for Linux and macOS runners to exclude WindowsKeyConverterTests which require Windows Console APIs. Windows runner runs all tests normally. * Fix log path typo and remove Codecov upload step Corrected a typo in the log directory path from `logs/UnitTestsParallelable/` to `logs/UnitTestsParallelizable/`. Removed the "Upload Parallelizable UnitTests Coverage to Codecov" step, which was conditional on `matrix.os == 'ubuntu-latest'` and used the `codecov/codecov-action@v4` action. This change improves log handling and removes the Codecov integration. * Refactor application reset logic Replaced `Application.ResetState(true)` with a more explicit reset mechanism. Introduced `ApplicationImpl.SetInstance(null)` to clear the application instance and added `CM.Disable(true)` to disable specific components. This change improves control over the reset process and ensures a more granular approach to application state management. * Improve null safety with ?. and ! operators Enhanced null safety across the codebase by introducing the null-conditional operator (`?.`) and null-forgiving operator (`!`) where appropriate. - Updated `app` and `driver` method calls to use `?.` to prevent potential `NullReferenceException` errors. - Added `!` to assert non-nullability in cases like `e.Value!.ToString()` and `app.Driver?.Contents!`. - Modified `lv.SelectedItemChanged` event handler to ensure safe handling of nullable values. - Updated `app.Shutdown()`, `app.LayoutAndDraw()`, and mouse event handling to use `?.`. - Ensured `driver.SetScreenSize` is invoked only when `driver` is not null. - Improved string concatenation logic with null-forgiving operator for `Contents`. - General improvements to null safety to make the code more robust and resilient to null references. * Improve Unicode tests and clarify surrogate pair handling Updated `WindowsKeyConverterTests` to enhance readability, improve test data, and clarify comments. Key changes include: - Reformatted `[Collection]` and `[Trait]` attributes for consistency. - Replaced placeholder Unicode characters with meaningful examples: - Chinese: `中`, Japanese: `日`, Korean: `한`, Accented: `é`, Euro: `€`, Greek: `Ω`. - Updated comments to replace placeholder emojis (`??`) with `😀` (U+1F600) for clarity. - Adjusted surrogate pair test data to accurately reflect `😀`. - Improved documentation of current limitations and future fixes for surrogate pair handling. These changes ensure more accurate and meaningful test cases while improving code clarity. * Ensure platform-specific test execution on Windows Added `System.Runtime.InteropServices` and `Xunit.Sdk` namespaces to `WindowsKeyConverterTests.cs` to support platform checks and test setup. Marked the test class with `[Collection("Global Test Setup")]` to enable shared test setup. Updated the `ToKey_NumPadKeys_ReturnsExpectedKeyCode` method to include a platform check, ensuring the test only runs on Windows platforms. * Integrate TrueColor support into ColorPicker Merged TrueColors functionality into ColorPicker, enhancing the scenario with TrueColor demonstration and gradient features. Updated `ColorPicker.cs` to include driver information, TrueColor support indicators, and a toggle for `Force16Colors`. Removed `TrueColors.cs` as its functionality is now consolidated. Refined `ColorBar` to use dynamic height with `Dim.Auto` for better flexibility. Added documentation to `HueBar` to clarify its role in representing the Hue component in HSL color space. * Revert workflow change. * Reverted attribute that didn't actualy work.
1 parent 1bd5e37 commit 726b15d

File tree

11 files changed

+848
-183
lines changed

11 files changed

+848
-183
lines changed

Examples/UICatalog/Scenarios/ColorPicker.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace UICatalog.Scenarios;
55

6-
[ScenarioMetadata ("ColorPicker", "Color Picker.")]
6+
[ScenarioMetadata ("ColorPicker", "Color Picker and TrueColor demonstration.")]
77
[ScenarioCategory ("Colors")]
88
[ScenarioCategory ("Controls")]
99
public class ColorPickers : Scenario
@@ -220,6 +220,33 @@ public override void Main ()
220220
};
221221
app.Add (cbShowName);
222222

223+
var lblDriverName = new Label
224+
{
225+
Y = Pos.Bottom (cbShowName) + 1, Text = $"Driver is `{Application.Driver?.GetName ()}`:"
226+
};
227+
bool canTrueColor = Application.Driver?.SupportsTrueColor ?? false;
228+
229+
var cbSupportsTrueColor = new CheckBox
230+
{
231+
X = Pos.Right (lblDriverName) + 1,
232+
Y = Pos.Top (lblDriverName),
233+
CheckedState = canTrueColor ? CheckState.Checked : CheckState.UnChecked,
234+
CanFocus = false,
235+
Enabled = false,
236+
Text = "SupportsTrueColor"
237+
};
238+
app.Add (cbSupportsTrueColor);
239+
240+
var cbUseTrueColor = new CheckBox
241+
{
242+
X = Pos.Right (cbSupportsTrueColor) + 1,
243+
Y = Pos.Top (lblDriverName),
244+
CheckedState = Application.Force16Colors ? CheckState.Checked : CheckState.UnChecked,
245+
Enabled = canTrueColor,
246+
Text = "Force16Colors"
247+
};
248+
cbUseTrueColor.CheckedStateChanging += (_, evt) => { Application.Force16Colors = evt.Result == CheckState.Checked; };
249+
app.Add (lblDriverName, cbSupportsTrueColor, cbUseTrueColor);
223250
// Set default colors.
224251
foregroundColorPicker.SelectedColor = _demoView.SuperView!.GetAttributeForRole (VisualRole.Normal).Foreground.GetClosestNamedColor16 ();
225252
backgroundColorPicker.SelectedColor = _demoView.SuperView.GetAttributeForRole (VisualRole.Normal).Background.GetClosestNamedColor16 ();

Examples/UICatalog/Scenarios/TrueColors.cs

Lines changed: 0 additions & 157 deletions
This file was deleted.

Terminal.Gui/App/ApplicationImpl.Lifecycle.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,9 @@ private static void AssertNoEventSubscribers (string eventName, Delegate? eventD
145145
}
146146
#endif
147147

148-
private bool _isResetingState;
149-
150148
/// <inheritdoc/>
151149
public void ResetState (bool ignoreDisposed = false)
152150
{
153-
_isResetingState = true;
154-
155151
// Shutdown is the bookend for Init. As such it needs to clean up all resources
156152
// Init created. Apps that do any threading will need to code defensively for this.
157153
// e.g. see Issue #537
@@ -250,8 +246,6 @@ public void ResetState (bool ignoreDisposed = false)
250246
// gui.cs does no longer process any callbacks. See #1084 for more details:
251247
// (https://github.com/gui-cs/Terminal.Gui/issues/1084).
252248
SynchronizationContext.SetSynchronizationContext (null);
253-
254-
_isResetingState = false;
255249
}
256250

257251
/// <summary>

Terminal.Gui/Views/Color/ColorBar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal abstract class ColorBar : View, IColorBar
1515
/// </summary>
1616
protected ColorBar ()
1717
{
18-
Height = 1;
18+
Height = Dim.Auto(minimumContentDim: 1);
1919
Width = Dim.Fill ();
2020
CanFocus = true;
2121

Terminal.Gui/Views/Color/HueBar.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
2-
31
using ColorHelper;
42
using ColorConverter = ColorHelper.ColorConverter;
53

64
namespace Terminal.Gui.Views;
75

6+
/// <summary>
7+
/// A bar representing the Hue component of a <see cref="Color"/> in HSL color space.
8+
/// </summary>
89
internal class HueBar : ColorBar
910
{
1011
/// <inheritdoc/>

Tests/IntegrationTests/UICatalog/ScenarioTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType)
3535
return;
3636
}
3737

38-
Application.ResetState (true);
38+
// Force a complete reset
39+
ApplicationImpl.SetInstance (null);
40+
CM.Disable (true);
3941

4042
_output.WriteLine ($"Running Scenario '{scenarioType}'");
4143
Scenario? scenario = null;

0 commit comments

Comments
 (0)