Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 1, 2025

This PR adds a fireShortcut() method to the ComponentTester class to enable testing of keyboard shortcuts attached directly to components through the KeyNotifier interface methods.

Problem

The existing fireShortcut() method in BaseUIUnitTest only works with shortcuts attached to the UI (or modal components) created with the Shortcuts API. It does not work with key down listeners attached to individual components through the KeyNotifier interface methods like addKeyDownListener().

Solution

Added a new fireShortcut(Key key, KeyModifier... modifiers) method to ComponentTester that:

  • Uses ShortcutsKt._fireShortcut() with the wrapped component as specified in the issue
  • Validates component usability before firing the shortcut using ensureComponentIsUsable()
  • Follows the same method signature and pattern as the existing UI-level fireShortcut() method
  • Includes comprehensive JavaDoc explaining when to use this vs the UI-level method

Usage Example

@Test
void testComponentKeyboardShortcut() {
    MyComponent component = new MyComponent();
    component.addKeyDownListener(event -> {
        if (event.getKey() == Key.KEY_S && event.getModifiers().contains(Key.CONTROL)) {
            // Handle Ctrl+S shortcut
        }
    });
    
    getCurrentView().add(component);
    ComponentTester<MyComponent> tester = test(component);
    
    // Fire shortcut directly on the component
    tester.fireShortcut(Key.KEY_S, KeyModifier.CONTROL);
    
    // Verify the shortcut was handled...
}

Implementation Details

  • Minimal changes: Only added necessary imports and the single method to ComponentTester
  • Consistent API: Same method signature as BaseUIUnitTest.fireShortcut()
  • Proper validation: Ensures component is usable (enabled, attached, visible, not behind modal)
  • Clear documentation: Explains the difference between component-level and UI-level shortcuts

Tests Added

  • Unit tests for method behavior and validation
  • Integration tests demonstrating DOM listener interaction
  • Example tests showing practical usage patterns

Fixes #1748.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • maven.vaadin.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.10/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.10/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.10 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.10/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/testbench/testbench org.codehaus.plexus.classworlds.launcher.Launcher clean compile -q (dns block)
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.10/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.10/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.10 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.10/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/testbench/testbench org.codehaus.plexus.classworlds.launcher.Launcher clean compile -q -pl vaadin-testbench-unit-shared (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI and others added 3 commits July 1, 2025 12:28
…tests

Co-authored-by: mshabarov <61410877+mshabarov@users.noreply.github.com>
Co-authored-by: mshabarov <61410877+mshabarov@users.noreply.github.com>
Co-authored-by: mshabarov <61410877+mshabarov@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Allow firing shortcut on components Add fireShortcut method to ComponentTester for component-level keyboard shortcuts Jul 1, 2025
Copilot finished work on behalf of mshabarov July 1, 2025 12:31
@Copilot Copilot AI requested a review from mshabarov July 1, 2025 12:31
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.

Allow firing shortcut on components
3 participants