|
20 | 20 | import org.junit.Assert; |
21 | 21 | import org.junit.Test; |
22 | 22 |
|
| 23 | +import com.vaadin.flow.component.FocusOption.FocusVisible; |
| 24 | +import com.vaadin.flow.component.FocusOption.PreventScroll; |
23 | 25 | import com.vaadin.flow.component.internal.PendingJavaScriptInvocation; |
24 | 26 | import com.vaadin.tests.util.MockUI; |
25 | 27 |
|
@@ -78,4 +80,168 @@ private void assertPendingInvocationCount(String message, int expected) { |
78 | 80 | .dumpPendingJsInvocations(); |
79 | 81 | Assert.assertEquals(message, expected, invocations.size()); |
80 | 82 | } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void focus_withFocusVisible_generatesCorrectJS() { |
| 86 | + ui.add(component); |
| 87 | + component.focus(FocusVisible.VISIBLE); |
| 88 | + |
| 89 | + List<PendingJavaScriptInvocation> invocations = ui |
| 90 | + .dumpPendingJsInvocations(); |
| 91 | + Assert.assertEquals(1, invocations.size()); |
| 92 | + |
| 93 | + String expression = invocations.get(0).getInvocation().getExpression(); |
| 94 | + Assert.assertTrue("Should contain setTimeout wrapper", |
| 95 | + expression.contains("setTimeout")); |
| 96 | + Assert.assertTrue("Should contain focus call with parameter", |
| 97 | + expression.contains(".focus($1)")); |
| 98 | + |
| 99 | + // Check the parameters |
| 100 | + List<Object> params = invocations.get(0).getInvocation() |
| 101 | + .getParameters(); |
| 102 | + // First param is element, second param is the options object |
| 103 | + Assert.assertTrue("Should have at least 2 parameters", |
| 104 | + params.size() >= 2); |
| 105 | + String paramJson = params.get(1).toString(); |
| 106 | + Assert.assertTrue("Should set focusVisible to true", |
| 107 | + paramJson.contains("\"focusVisible\":true")); |
| 108 | + Assert.assertFalse("Should not contain preventScroll", |
| 109 | + paramJson.contains("preventScroll")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void focus_withFocusNotVisible_generatesCorrectJS() { |
| 114 | + ui.add(component); |
| 115 | + component.focus(FocusVisible.NOT_VISIBLE); |
| 116 | + |
| 117 | + List<PendingJavaScriptInvocation> invocations = ui |
| 118 | + .dumpPendingJsInvocations(); |
| 119 | + Assert.assertEquals(1, invocations.size()); |
| 120 | + |
| 121 | + String expression = invocations.get(0).getInvocation().getExpression(); |
| 122 | + Assert.assertTrue("Should contain setTimeout wrapper", |
| 123 | + expression.contains("setTimeout")); |
| 124 | + Assert.assertTrue("Should contain focus call with parameter", |
| 125 | + expression.contains(".focus($1)")); |
| 126 | + |
| 127 | + // Check the parameters |
| 128 | + List<Object> params = invocations.get(0).getInvocation() |
| 129 | + .getParameters(); |
| 130 | + // First param is element, second param is the options object |
| 131 | + Assert.assertTrue("Should have at least 2 parameters", |
| 132 | + params.size() >= 2); |
| 133 | + String paramJson = params.get(1).toString(); |
| 134 | + Assert.assertTrue("Should set focusVisible to false", |
| 135 | + paramJson.contains("\"focusVisible\":false")); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void focus_withPreventScrollEnabled_generatesCorrectJS() { |
| 140 | + ui.add(component); |
| 141 | + component.focus(PreventScroll.ENABLED); |
| 142 | + |
| 143 | + List<PendingJavaScriptInvocation> invocations = ui |
| 144 | + .dumpPendingJsInvocations(); |
| 145 | + Assert.assertEquals(1, invocations.size()); |
| 146 | + |
| 147 | + String expression = invocations.get(0).getInvocation().getExpression(); |
| 148 | + Assert.assertTrue("Should contain setTimeout wrapper", |
| 149 | + expression.contains("setTimeout")); |
| 150 | + Assert.assertTrue("Should contain focus call with parameter", |
| 151 | + expression.contains(".focus($1)")); |
| 152 | + |
| 153 | + // Check the parameters |
| 154 | + List<Object> params = invocations.get(0).getInvocation() |
| 155 | + .getParameters(); |
| 156 | + // First param is element, second param is the options object |
| 157 | + Assert.assertTrue("Should have at least 2 parameters", |
| 158 | + params.size() >= 2); |
| 159 | + String paramJson = params.get(1).toString(); |
| 160 | + Assert.assertTrue("Should set preventScroll to true", |
| 161 | + paramJson.contains("\"preventScroll\":true")); |
| 162 | + Assert.assertFalse("Should not contain focusVisible", |
| 163 | + paramJson.contains("focusVisible")); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void focus_withPreventScrollDisabled_generatesCorrectJS() { |
| 168 | + ui.add(component); |
| 169 | + component.focus(PreventScroll.DISABLED); |
| 170 | + |
| 171 | + List<PendingJavaScriptInvocation> invocations = ui |
| 172 | + .dumpPendingJsInvocations(); |
| 173 | + Assert.assertEquals(1, invocations.size()); |
| 174 | + |
| 175 | + String expression = invocations.get(0).getInvocation().getExpression(); |
| 176 | + Assert.assertTrue("Should contain setTimeout wrapper", |
| 177 | + expression.contains("setTimeout")); |
| 178 | + Assert.assertTrue("Should contain focus call with parameter", |
| 179 | + expression.contains(".focus($1)")); |
| 180 | + |
| 181 | + // Check the parameters |
| 182 | + List<Object> params = invocations.get(0).getInvocation() |
| 183 | + .getParameters(); |
| 184 | + // First param is element, second param is the options object |
| 185 | + Assert.assertTrue("Should have at least 2 parameters", |
| 186 | + params.size() >= 2); |
| 187 | + String paramJson = params.get(1).toString(); |
| 188 | + Assert.assertTrue("Should set preventScroll to false", |
| 189 | + paramJson.contains("\"preventScroll\":false")); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void focus_withBothOptions_generatesCorrectJS() { |
| 194 | + ui.add(component); |
| 195 | + component.focus(FocusVisible.VISIBLE, PreventScroll.ENABLED); |
| 196 | + |
| 197 | + List<PendingJavaScriptInvocation> invocations = ui |
| 198 | + .dumpPendingJsInvocations(); |
| 199 | + Assert.assertEquals(1, invocations.size()); |
| 200 | + |
| 201 | + String expression = invocations.get(0).getInvocation().getExpression(); |
| 202 | + Assert.assertTrue("Should contain setTimeout wrapper", |
| 203 | + expression.contains("setTimeout")); |
| 204 | + Assert.assertTrue("Should contain focus call with parameter", |
| 205 | + expression.contains(".focus($1)")); |
| 206 | + |
| 207 | + // Check the parameters |
| 208 | + List<Object> params = invocations.get(0).getInvocation() |
| 209 | + .getParameters(); |
| 210 | + // First param is element, second param is the options object |
| 211 | + Assert.assertTrue("Should have at least 2 parameters", |
| 212 | + params.size() >= 2); |
| 213 | + String paramJson = params.get(1).toString(); |
| 214 | + Assert.assertTrue("Should set preventScroll to true", |
| 215 | + paramJson.contains("\"preventScroll\":true")); |
| 216 | + Assert.assertTrue("Should set focusVisible to true", |
| 217 | + paramJson.contains("\"focusVisible\":true")); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void focus_withBothOptionsFalse_generatesCorrectJS() { |
| 222 | + ui.add(component); |
| 223 | + component.focus(FocusVisible.NOT_VISIBLE, PreventScroll.DISABLED); |
| 224 | + |
| 225 | + List<PendingJavaScriptInvocation> invocations = ui |
| 226 | + .dumpPendingJsInvocations(); |
| 227 | + Assert.assertEquals(1, invocations.size()); |
| 228 | + |
| 229 | + String expression = invocations.get(0).getInvocation().getExpression(); |
| 230 | + Assert.assertTrue("Should contain setTimeout wrapper", |
| 231 | + expression.contains("setTimeout")); |
| 232 | + Assert.assertTrue("Should contain focus call with parameter", |
| 233 | + expression.contains(".focus($1)")); |
| 234 | + |
| 235 | + // Check the parameters |
| 236 | + List<Object> params = invocations.get(0).getInvocation() |
| 237 | + .getParameters(); |
| 238 | + // First param is element, second param is the options object |
| 239 | + Assert.assertTrue("Should have at least 2 parameters", |
| 240 | + params.size() >= 2); |
| 241 | + String paramJson = params.get(1).toString(); |
| 242 | + Assert.assertTrue("Should set preventScroll to false", |
| 243 | + paramJson.contains("\"preventScroll\":false")); |
| 244 | + Assert.assertTrue("Should set focusVisible to false", |
| 245 | + paramJson.contains("\"focusVisible\":false")); |
| 246 | + } |
81 | 247 | } |
0 commit comments