|
| 1 | +package com.vaadin.flow.component; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.StringUtils; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.lang.reflect.Constructor; |
| 7 | +import java.lang.reflect.InvocationTargetException; |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.util.*; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +import static org.junit.Assert.assertEquals; |
| 13 | +import static org.junit.Assert.assertFalse; |
| 14 | +import static org.junit.Assert.assertTrue; |
| 15 | + |
| 16 | +public class ShortCutRegistrationFilterTest { |
| 17 | + |
| 18 | + /** |
| 19 | + * This method is used to test the generateEventModifierFilter method in the |
| 20 | + * ShortcutRegistration class. This method is private, so we need to use |
| 21 | + * reflection to invoke it. |
| 22 | + * |
| 23 | + * @param list |
| 24 | + * @return |
| 25 | + * @throws NoSuchMethodException |
| 26 | + * @throws InvocationTargetException |
| 27 | + * @throws IllegalAccessException |
| 28 | + */ |
| 29 | + private static String invokeGenerateEventModifierFilter(List<Key> list) |
| 30 | + throws NoSuchMethodException, InvocationTargetException, |
| 31 | + IllegalAccessException { |
| 32 | + Method privateMethod = ShortcutRegistration.class.getDeclaredMethod( |
| 33 | + "generateEventModifierFilter", Collection.class); |
| 34 | + privateMethod.setAccessible(true); |
| 35 | + // invoke the private method for testing |
| 36 | + String result = (String) privateMethod.invoke(null, list); |
| 37 | + return result; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * This method is used to get the HashableKey object from the |
| 42 | + * ShortcutRegistration class. This is needed because the HashableKey class |
| 43 | + * is private, and we need to create an instance of it to test the |
| 44 | + * generateEventModifierFilter method. |
| 45 | + * |
| 46 | + * @param keyModifier |
| 47 | + * @return |
| 48 | + * @throws ClassNotFoundException |
| 49 | + * @throws InvocationTargetException |
| 50 | + * @throws InstantiationException |
| 51 | + * @throws IllegalAccessException |
| 52 | + */ |
| 53 | + private static Object getHashableKey(KeyModifier keyModifier) |
| 54 | + throws ClassNotFoundException, InvocationTargetException, |
| 55 | + InstantiationException, IllegalAccessException { |
| 56 | + final Class<?> nestedClass = Class.forName( |
| 57 | + "com.vaadin.flow.component.ShortcutRegistration$HashableKey"); |
| 58 | + final Constructor<?> ctor = nestedClass.getDeclaredConstructors()[0]; |
| 59 | + ctor.setAccessible(true); |
| 60 | + return ctor.newInstance(keyModifier); |
| 61 | + } |
| 62 | + |
| 63 | + private static ArrayList<String> getModifierFilterConditions( |
| 64 | + String eventModifierFilter, int expectedFilterCount) { |
| 65 | + ArrayList<String> filterConditions = new ArrayList<>(Arrays |
| 66 | + .asList(eventModifierFilter.split(" && ", Integer.MAX_VALUE))); |
| 67 | + assertTrue( |
| 68 | + "Split filter conditions should not contain '&' character or blank strings.", |
| 69 | + filterConditions.stream() |
| 70 | + .filter(c -> c.contains("&") || StringUtils.isBlank(c)) |
| 71 | + .collect(Collectors.toList()).isEmpty()); |
| 72 | + assertEquals(expectedFilterCount, filterConditions.size()); |
| 73 | + return filterConditions; |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testGenerateEventModifierFilterWithModifierKeyAlt() |
| 78 | + throws InvocationTargetException, NoSuchMethodException, |
| 79 | + IllegalAccessException, ClassNotFoundException, |
| 80 | + InstantiationException { |
| 81 | + String result = invokeGenerateEventModifierFilter(Collections |
| 82 | + .singletonList(((Key) getHashableKey(KeyModifier.ALT)))); |
| 83 | + ArrayList<String> conditions = getModifierFilterConditions(result, 4); |
| 84 | + assertTrue(conditions.contains("event.getModifierState('Alt')")); |
| 85 | + assertTrue(conditions.contains("!event.getModifierState('Control')")); |
| 86 | + assertFalse(conditions.contains("event.getModifierState('AltGraph')")); |
| 87 | + assertFalse(conditions.contains("!event.getModifierState('AltGraph')")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testGenerateEventModifierFilterWithModifierKeyAltGr() |
| 92 | + throws InvocationTargetException, NoSuchMethodException, |
| 93 | + IllegalAccessException, ClassNotFoundException, |
| 94 | + InstantiationException { |
| 95 | + String result = invokeGenerateEventModifierFilter(Collections |
| 96 | + .singletonList(((Key) getHashableKey(KeyModifier.ALT_GRAPH)))); |
| 97 | + ArrayList<String> conditions = getModifierFilterConditions(result, 5); |
| 98 | + assertTrue(conditions.contains("event.getModifierState('AltGraph')")); |
| 99 | + assertTrue(conditions.contains("!event.getModifierState('Alt')")); |
| 100 | + assertTrue(conditions.contains("!event.getModifierState('Control')")); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testGenerateEventModifierFilterWithModifierKeyAltAndAltGr() |
| 105 | + throws InvocationTargetException, NoSuchMethodException, |
| 106 | + IllegalAccessException, ClassNotFoundException, |
| 107 | + InstantiationException { |
| 108 | + String result = invokeGenerateEventModifierFilter( |
| 109 | + Arrays.asList((Key) getHashableKey(KeyModifier.ALT), |
| 110 | + (Key) getHashableKey(KeyModifier.ALT_GRAPH))); |
| 111 | + ArrayList<String> conditions = getModifierFilterConditions(result, 5); |
| 112 | + assertTrue(conditions.contains("event.getModifierState('Alt')")); |
| 113 | + assertTrue(conditions.contains("event.getModifierState('AltGraph')")); |
| 114 | + assertTrue(conditions.contains("!event.getModifierState('Control')")); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testGenerateEventModifierFilterWithModifierKeyAltGrAndCtrl() |
| 119 | + throws InvocationTargetException, NoSuchMethodException, |
| 120 | + IllegalAccessException, ClassNotFoundException, |
| 121 | + InstantiationException { |
| 122 | + String result = invokeGenerateEventModifierFilter( |
| 123 | + Arrays.asList((Key) getHashableKey(KeyModifier.ALT_GRAPH), |
| 124 | + (Key) getHashableKey(KeyModifier.CONTROL))); |
| 125 | + ArrayList<String> conditions = getModifierFilterConditions(result, 5); |
| 126 | + assertTrue(conditions.contains("event.getModifierState('AltGraph')")); |
| 127 | + assertTrue(conditions.contains("event.getModifierState('Control')")); |
| 128 | + assertTrue(conditions.contains("!event.getModifierState('Alt')")); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testGenerateEventModifierFilterWithModifierKeyAltAndCtrl() |
| 133 | + throws InvocationTargetException, NoSuchMethodException, |
| 134 | + IllegalAccessException, ClassNotFoundException, |
| 135 | + InstantiationException { |
| 136 | + String result = invokeGenerateEventModifierFilter( |
| 137 | + Arrays.asList((Key) getHashableKey(KeyModifier.ALT), |
| 138 | + (Key) getHashableKey(KeyModifier.CONTROL))); |
| 139 | + ArrayList<String> conditions = getModifierFilterConditions(result, 4); |
| 140 | + assertTrue(conditions.contains("event.getModifierState('Alt')")); |
| 141 | + assertTrue(conditions.contains("event.getModifierState('Control')")); |
| 142 | + assertFalse(conditions.contains("event.getModifierState('AltGraph')")); |
| 143 | + assertFalse(conditions.contains("!event.getModifierState('AltGraph')")); |
| 144 | + } |
| 145 | +} |
0 commit comments