|
11 | 11 | import org.junit.Assert; |
12 | 12 | import org.junit.Before; |
13 | 13 | import org.junit.Test; |
| 14 | +import org.mockito.ArgumentMatchers; |
14 | 15 | import org.mockito.Mock; |
15 | 16 | import org.mockito.Mockito; |
16 | 17 | import org.mockito.MockitoAnnotations; |
|
24 | 25 | import com.vaadin.flow.di.DefaultInstantiator; |
25 | 26 | import com.vaadin.flow.dom.Element; |
26 | 27 | import com.vaadin.flow.function.DeploymentConfiguration; |
| 28 | +import com.vaadin.flow.internal.JsonCodec; |
| 29 | +import com.vaadin.flow.internal.StateNode; |
| 30 | +import com.vaadin.flow.internal.nodefeature.ElementChildrenList; |
| 31 | +import com.vaadin.flow.internal.nodefeature.ElementData; |
27 | 32 | import com.vaadin.flow.router.Location; |
28 | 33 | import com.vaadin.flow.router.ParentLayout; |
29 | 34 | import com.vaadin.flow.router.Route; |
@@ -274,6 +279,150 @@ public void showRouteTarget_navigateToAnotherLayoutHierarchy_detachedLayoutHiera |
274 | 279 | subLayout.getElement().getChildren().count()); |
275 | 280 | } |
276 | 281 |
|
| 282 | + @Test |
| 283 | + public void dumpPendingJavaScriptInvocations_detachListenerRegisteredOnce() { |
| 284 | + StateNode node = Mockito.spy(new StateNode(ElementData.class)); |
| 285 | + node.getFeature(ElementData.class).setVisible(false); |
| 286 | + internals.getStateTree().getRootNode() |
| 287 | + .getFeature(ElementChildrenList.class).add(0, node); |
| 288 | + |
| 289 | + internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node, |
| 290 | + new UIInternals.JavaScriptInvocation(""))); |
| 291 | + internals.dumpPendingJavaScriptInvocations(); |
| 292 | + internals.dumpPendingJavaScriptInvocations(); |
| 293 | + internals.dumpPendingJavaScriptInvocations(); |
| 294 | + |
| 295 | + Mockito.verify(node, Mockito.times(1)) |
| 296 | + .addDetachListener(ArgumentMatchers.any()); |
| 297 | + } |
| 298 | + |
| 299 | + @Test |
| 300 | + public void dumpPendingJavaScriptInvocations_multipleInvocationPerNode_onlyOneDetachListenerRegistered() { |
| 301 | + StateNode node = Mockito.spy(new StateNode(ElementData.class)); |
| 302 | + node.getFeature(ElementData.class).setVisible(false); |
| 303 | + internals.getStateTree().getRootNode() |
| 304 | + .getFeature(ElementChildrenList.class).add(0, node); |
| 305 | + |
| 306 | + internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node, |
| 307 | + new UIInternals.JavaScriptInvocation("1"))); |
| 308 | + internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node, |
| 309 | + new UIInternals.JavaScriptInvocation("2"))); |
| 310 | + internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node, |
| 311 | + new UIInternals.JavaScriptInvocation("3"))); |
| 312 | + internals.dumpPendingJavaScriptInvocations(); |
| 313 | + |
| 314 | + Mockito.verify(node, Mockito.times(1)) |
| 315 | + .addDetachListener(ArgumentMatchers.any()); |
| 316 | + } |
| 317 | + |
| 318 | + @Test |
| 319 | + public void dumpPendingJavaScriptInvocations_registerOneDetachListenerPerNode() { |
| 320 | + StateNode node1 = Mockito.spy(new StateNode(ElementData.class)); |
| 321 | + node1.getFeature(ElementData.class).setVisible(false); |
| 322 | + internals.getStateTree().getRootNode() |
| 323 | + .getFeature(ElementChildrenList.class).add(0, node1); |
| 324 | + internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node1, |
| 325 | + new UIInternals.JavaScriptInvocation("1"))); |
| 326 | + |
| 327 | + StateNode node2 = Mockito.spy(new StateNode(ElementData.class)); |
| 328 | + node2.getFeature(ElementData.class).setVisible(false); |
| 329 | + internals.getStateTree().getRootNode() |
| 330 | + .getFeature(ElementChildrenList.class).add(0, node2); |
| 331 | + internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node2, |
| 332 | + new UIInternals.JavaScriptInvocation("1"))); |
| 333 | + |
| 334 | + internals.dumpPendingJavaScriptInvocations(); |
| 335 | + |
| 336 | + Mockito.verify(node1, Mockito.times(1)) |
| 337 | + .addDetachListener(ArgumentMatchers.any()); |
| 338 | + Mockito.verify(node2, Mockito.times(1)) |
| 339 | + .addDetachListener(ArgumentMatchers.any()); |
| 340 | + } |
| 341 | + |
| 342 | + @Test |
| 343 | + public void dumpPendingJavaScriptInvocations_invocationCompletes_pendingListPurged() { |
| 344 | + StateNode node = Mockito.spy(new StateNode(ElementData.class)); |
| 345 | + node.getFeature(ElementData.class).setVisible(false); |
| 346 | + internals.getStateTree().getRootNode() |
| 347 | + .getFeature(ElementChildrenList.class).add(0, node); |
| 348 | + |
| 349 | + PendingJavaScriptInvocation invocation = new PendingJavaScriptInvocation( |
| 350 | + node, new UIInternals.JavaScriptInvocation("")); |
| 351 | + internals.addJavaScriptInvocation(invocation); |
| 352 | + internals.dumpPendingJavaScriptInvocations(); |
| 353 | + |
| 354 | + Mockito.verify(node, Mockito.times(1)) |
| 355 | + .addDetachListener(ArgumentMatchers.any()); |
| 356 | + |
| 357 | + invocation.complete(JsonCodec.encodeWithTypeInfo("OK")); |
| 358 | + |
| 359 | + Assert.assertEquals(0, |
| 360 | + internals.getPendingJavaScriptInvocations().count()); |
| 361 | + } |
| 362 | + |
| 363 | + @Test |
| 364 | + public void dumpPendingJavaScriptInvocations_invocationFails_pendingListPurged() { |
| 365 | + StateNode node = Mockito.spy(new StateNode(ElementData.class)); |
| 366 | + node.getFeature(ElementData.class).setVisible(false); |
| 367 | + internals.getStateTree().getRootNode() |
| 368 | + .getFeature(ElementChildrenList.class).add(0, node); |
| 369 | + |
| 370 | + PendingJavaScriptInvocation invocation = new PendingJavaScriptInvocation( |
| 371 | + node, new UIInternals.JavaScriptInvocation("")); |
| 372 | + internals.addJavaScriptInvocation(invocation); |
| 373 | + internals.dumpPendingJavaScriptInvocations(); |
| 374 | + |
| 375 | + Mockito.verify(node, Mockito.times(1)) |
| 376 | + .addDetachListener(ArgumentMatchers.any()); |
| 377 | + |
| 378 | + invocation.completeExceptionally(JsonCodec.encodeWithTypeInfo("ERROR")); |
| 379 | + |
| 380 | + Assert.assertEquals(0, |
| 381 | + internals.getPendingJavaScriptInvocations().count()); |
| 382 | + } |
| 383 | + |
| 384 | + @Test |
| 385 | + public void dumpPendingJavaScriptInvocations_invocationCanceled_pendingListPurged() { |
| 386 | + StateNode node = Mockito.spy(new StateNode(ElementData.class)); |
| 387 | + node.getFeature(ElementData.class).setVisible(false); |
| 388 | + internals.getStateTree().getRootNode() |
| 389 | + .getFeature(ElementChildrenList.class).add(0, node); |
| 390 | + |
| 391 | + PendingJavaScriptInvocation invocation = new PendingJavaScriptInvocation( |
| 392 | + node, new UIInternals.JavaScriptInvocation("")); |
| 393 | + internals.addJavaScriptInvocation(invocation); |
| 394 | + internals.dumpPendingJavaScriptInvocations(); |
| 395 | + |
| 396 | + Mockito.verify(node, Mockito.times(1)) |
| 397 | + .addDetachListener(ArgumentMatchers.any()); |
| 398 | + |
| 399 | + invocation.cancelExecution(); |
| 400 | + |
| 401 | + Assert.assertEquals(0, |
| 402 | + internals.getPendingJavaScriptInvocations().count()); |
| 403 | + } |
| 404 | + |
| 405 | + @Test |
| 406 | + public void dumpPendingJavaScriptInvocations_nodeDetached_pendingListPurged() { |
| 407 | + StateNode node = Mockito.spy(new StateNode(ElementData.class)); |
| 408 | + node.getFeature(ElementData.class).setVisible(false); |
| 409 | + internals.getStateTree().getRootNode() |
| 410 | + .getFeature(ElementChildrenList.class).add(0, node); |
| 411 | + |
| 412 | + PendingJavaScriptInvocation invocation = new PendingJavaScriptInvocation( |
| 413 | + node, new UIInternals.JavaScriptInvocation("")); |
| 414 | + internals.addJavaScriptInvocation(invocation); |
| 415 | + internals.dumpPendingJavaScriptInvocations(); |
| 416 | + |
| 417 | + Mockito.verify(node, Mockito.times(1)) |
| 418 | + .addDetachListener(ArgumentMatchers.any()); |
| 419 | + |
| 420 | + node.setParent(null); |
| 421 | + |
| 422 | + Assert.assertEquals(0, |
| 423 | + internals.getPendingJavaScriptInvocations().count()); |
| 424 | + } |
| 425 | + |
277 | 426 | private PushConfiguration setUpInitialPush() { |
278 | 427 | DeploymentConfiguration config = Mockito |
279 | 428 | .mock(DeploymentConfiguration.class); |
|
0 commit comments