diff --git a/vaadin-testbench-unit-junit5/src/test/java/com/vaadin/testbench/unit/UIUnitShortcutTest.java b/vaadin-testbench-unit-junit5/src/test/java/com/vaadin/testbench/unit/UIUnitShortcutTest.java index 181b3b5ce..b08f57857 100644 --- a/vaadin-testbench-unit-junit5/src/test/java/com/vaadin/testbench/unit/UIUnitShortcutTest.java +++ b/vaadin-testbench-unit-junit5/src/test/java/com/vaadin/testbench/unit/UIUnitShortcutTest.java @@ -11,7 +11,6 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -28,7 +27,6 @@ @ExtendWith(TreeOnFailureExtension.class) class UIUnitShortcutTest extends UIUnitTest { - @Disabled @Test void fireShortcut_UIListener_invokedForExactMatch() { AtomicInteger eventsCounter = new AtomicInteger(); @@ -52,7 +50,6 @@ void fireShortcut_UIListener_invokedForExactMatch() { Assertions.assertEquals(2, eventsCounter.get()); } - @Disabled @Test void fireShortcut_nestedComponents_listenersInvoked() { AtomicInteger buttonEvents = new AtomicInteger(); diff --git a/vaadin-testbench-unit-junit5/src/test/kotlin/com/vaadin/testbench/unit/internal/AllTests.kt b/vaadin-testbench-unit-junit5/src/test/kotlin/com/vaadin/testbench/unit/internal/AllTests.kt index 4b87734b1..fb8ac4de6 100644 --- a/vaadin-testbench-unit-junit5/src/test/kotlin/com/vaadin/testbench/unit/internal/AllTests.kt +++ b/vaadin-testbench-unit-junit5/src/test/kotlin/com/vaadin/testbench/unit/internal/AllTests.kt @@ -68,9 +68,9 @@ class AllTests : DynaTest({ searchSpecTest() } -// group("shortcuts") { -// shortcutsTestBatch() -// } + group("shortcuts") { + shortcutsTestBatch() + } test("Component.isTemplate does not fail without polymer templates dependency") { expect(false) { Button("foo").isTemplate } diff --git a/vaadin-testbench-unit-shared/src/main/kotlin/com/vaadin/testbench/unit/internal/Shortcuts.kt b/vaadin-testbench-unit-shared/src/main/kotlin/com/vaadin/testbench/unit/internal/Shortcuts.kt index bf28573da..dd40bb1a9 100644 --- a/vaadin-testbench-unit-shared/src/main/kotlin/com/vaadin/testbench/unit/internal/Shortcuts.kt +++ b/vaadin-testbench-unit-shared/src/main/kotlin/com/vaadin/testbench/unit/internal/Shortcuts.kt @@ -2,7 +2,6 @@ package com.vaadin.testbench.unit.internal import com.vaadin.flow.component.Component import com.vaadin.flow.component.Key -import com.vaadin.flow.component.KeyModifier import com.vaadin.flow.component.ShortcutRegistration import elemental.json.impl.JreJsonFactory import elemental.json.impl.JreJsonObject @@ -12,17 +11,30 @@ import elemental.json.impl.JreJsonObject * If this stuff stops working, place a breakpoint into the [getBoolean]/[hasKey] function, * to see what kind of keys you're receiving and whether it matches [filter]. */ -private class MockFilterJsonObject(val key: Key, val modifiers: Set) : JreJsonObject(JreJsonFactory()) { +private class MockFilterJsonObject(val key: Key, val modifiers: Set) : JreJsonObject(JreJsonFactory()) { val filter: String init { + // compute the filter filter = mgenerateEventKeyFilter.invoke(null, key).toString() + " && " + - mgenerateEventModifierFilter.invoke(null, modifiers).toString() + // we call the private method with Hashable keys implementations as the ShortcutRegistration class is + // called by these objects/classes in regular cases. + mgenerateEventModifierFilter.invoke(null, modifiers.map { getHashableKey(it)}).toString() // populate the json object so that KeyDownEvent can be created from it put("event.key", key.keys.first()) } + /** + * Returns a HashableKey instance for the given key modifier. + * @param keyModifier the key modifier + * @return the HashableKey instance + * + */ + private fun getHashableKey(keyModifier: Key): Any? { + return chashableKey.newInstance(keyModifier) + } + override fun hasKey(key: String): Boolean { // the "key" is a JavaScript expression which matches the key pressed. // we need to match it against the 'filter' @@ -54,9 +66,14 @@ private class MockFilterJsonObject(val key: Key, val modifiers: Set companion object { private val mgenerateEventKeyFilter = ShortcutRegistration::class.java.getDeclaredMethod("generateEventKeyFilter", Key::class.java) private val mgenerateEventModifierFilter = ShortcutRegistration::class.java.getDeclaredMethod("generateEventModifierFilter", Collection::class.java) + private val chashableKey = ShortcutRegistration::class.java + .classLoader + .loadClass("com.vaadin.flow.component.ShortcutRegistration\$HashableKey") + .declaredConstructors[0] init { mgenerateEventKeyFilter.isAccessible = true mgenerateEventModifierFilter.isAccessible = true + chashableKey.isAccessible = true } } } @@ -86,8 +103,7 @@ public fun Component._fireShortcut(key: Key, vararg modifiers: Key) { // contains a boolean value with key 'filter'. We need to fake the json object // as if it contained all filters (otherwise `matchesFilter()` would NPE on missing key) // and respond true only to the matching filter. - @Suppress("UNCHECKED_CAST") - val data = MockFilterJsonObject(key, modifiers.toSet() as Set) + val data = MockFilterJsonObject(key, modifiers.toSet()) // the shortcut registration is only updated in [UI.beforeClientResponse]; run the registration code now. MockVaadin.clientRoundtrip() diff --git a/vaadin-testbench-unit/src/test/kotlin/com/vaadin/testbench/unit/internal/AllTests.kt b/vaadin-testbench-unit/src/test/kotlin/com/vaadin/testbench/unit/internal/AllTests.kt index 1cec87307..c2d972efe 100644 --- a/vaadin-testbench-unit/src/test/kotlin/com/vaadin/testbench/unit/internal/AllTests.kt +++ b/vaadin-testbench-unit/src/test/kotlin/com/vaadin/testbench/unit/internal/AllTests.kt @@ -66,9 +66,9 @@ class AllTests : DynaTest({ group("search spec") { searchSpecTest() } -// group("shortcuts") { -// shortcutsTestBatch() -// } + group("shortcuts") { + shortcutsTestBatch() + } })