Skip to content

Conversation

@tig
Copy link
Collaborator

@tig tig commented Nov 12, 2025

Summary

This PR adds comprehensive parallelizable unit tests for WindowsKeyConverter to ensure proper keyboard input conversion between Windows native InputRecord format and Terminal.Gui's Key representation.

Changes

  • Added 118 parallelizable unit tests in Tests/UnitTestsParallelizable/Drivers/WindowsKeyConverterTests.cs
  • Comprehensive coverage of ToKey and ToKeyInfo methods
  • Full bidirectional conversion testing with round-trip validation
  • Tests for all key categories including edge cases

Test Coverage

Test Categories (118 tests total)

  1. ToKey Tests - Basic Characters (7 tests)

    • Letter keys (A-Z) with and without Shift
    • Number keys (0-9)
  2. ToKey Tests - Modifiers (5 tests)

    • Ctrl, Alt, Shift combinations
    • Multiple modifier combinations (Ctrl+Alt, Ctrl+Shift+Alt, etc.)
  3. ToKey Tests - Special Keys (26 tests)

    • Navigation keys (Enter, Esc, Tab, Backspace, Delete, Insert, Home, End, PageUp/PageDown)
    • Arrow keys (Up, Down, Left, Right)
    • Function keys (F1-F12)
  4. ToKey Tests - VK_PACKET (Unicode/IME) (7 tests)

    • International characters (Chinese, Japanese, Korean, Greek)
    • Accented characters (é, etc.)
    • Special symbols (€, etc.)
    • Null character handling
  5. ToKey Tests - OEM Keys (12 tests)

    • Keyboard layout-specific keys (semicolon, slash, comma, period, plus, minus)
    • Shifted and unshifted variations
    • US keyboard layout mappings
  6. ToKey Tests - NumPad (9 tests)

    • NumPad number keys (NumPad0-9)
    • NumPad operators (*, +, -, ., /)
  7. ToKey Tests - Null/Empty (1 test)

    • Null key handling
  8. ToKeyInfo Tests - Basic Keys (7 tests)

    • Letter and number keys reverse conversion
    • EventType, VirtualKeyCode, UnicodeChar, KeyDown, RepeatCount validation
  9. ToKeyInfo Tests - Special Keys (15 tests)

    • Special character keys with proper unicode chars
    • Navigation keys
    • Function keys
  10. ToKeyInfo Tests - Modifiers (5 tests)

    • ControlKeyState verification for all modifier combinations
    • ShiftPressed, LeftControlPressed, LeftAltPressed flags
  11. ToKeyInfo Tests - Scan Codes (8 tests)

    • Hardware scan code generation validation
    • Common keys (A, Enter, Esc, Space, F1, F10, CursorUp, Home)
  12. Round-Trip Tests (13 tests)

    • ToKey → ToKeyInfo → ToKey preservation
    • ToKeyInfo → ToKey → ToKeyInfo preservation
    • Modifier state preservation validation
  13. CapsLock/NumLock Tests (2 tests)

    • CapsLock state handling
    • Lock state interaction with Shift key

Test Results

All 118 tests passing successfully:

tig added 3 commits November 12, 2025 09:04
- 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 gui-cs#4389
@codecov
Copy link

codecov bot commented Nov 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.74%. Comparing base (1bd5e37) to head (2126d69).
⚠️ Report is 1 commits behind head on v2_develop.

Additional details and impacted files
@@              Coverage Diff               @@
##           v2_develop    #4390      +/-   ##
==============================================
+ Coverage       68.64%   74.74%   +6.09%     
==============================================
  Files             388      388              
  Lines           46692    46690       -2     
  Branches         6641     6641              
==============================================
+ Hits            32052    34898    +2846     
+ Misses          12708     9898    -2810     
+ Partials         1932     1894      -38     
Files with missing lines Coverage Δ
Terminal.Gui/App/ApplicationImpl.Lifecycle.cs 86.29% <ø> (+9.30%) ⬆️
Terminal.Gui/Views/Color/ColorBar.cs 86.60% <100.00%> (ø)
Terminal.Gui/Views/Color/HueBar.cs 100.00% <ø> (ø)

... and 89 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1bd5e37...2126d69. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot finished reviewing on behalf of tig November 12, 2025 19:40
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive unit tests for the WindowsKeyConverter class, which handles bidirectional conversion between Windows native InputRecord format and Terminal.Gui's Key representation. The tests provide extensive coverage with 118 parallelizable test cases covering basic character keys, modifiers, special keys, Unicode/IME input, OEM keys, NumPad keys, function keys, scan codes, round-trip conversions, and lock state handling.

Key Changes:

  • Added 118 parallelizable unit tests organized into 13 test categories
  • Comprehensive testing of ToKey and ToKeyInfo conversion methods
  • Full bidirectional round-trip conversion validation

tig added 5 commits November 12, 2025 12:48
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.
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.
Created custom xUnit attributes SkipOnNonWindowsFact and SkipOnNonWindowsTheory that automatically skip tests on non-Windows platforms. This prevents CI failures on macOS and Linux.
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.
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.
@tig tig force-pushed the feature/windowskeyconverter-tests branch from 43b0373 to 8ebf25a Compare November 12, 2025 20:26
tig added 12 commits November 12, 2025 15:03
Added --filter 'Platform!=Windows' for Linux and macOS runners to exclude WindowsKeyConverterTests which require Windows Console APIs. Windows runner runs all tests normally.
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.
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.
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.
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.
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.
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.
@tig tig requested review from BDisp and Copilot November 20, 2025 02:48
Copilot finished reviewing on behalf of tig November 20, 2025 02:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 8 out of 11 changed files in this pull request and generated 4 comments.

Copy link
Collaborator

@BDisp BDisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good to me. I might think of a solution to make unit tests pass on non-Windows operating systems as well.

@tig tig merged commit 726b15d into gui-cs:v2_develop Nov 20, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants