|
20 | 20 | import java.util.Arrays; |
21 | 21 | import java.util.Collections; |
22 | 22 | import java.util.List; |
| 23 | +import java.util.Optional; |
23 | 24 | import java.util.Set; |
24 | 25 | import java.util.concurrent.atomic.AtomicBoolean; |
25 | 26 | import java.util.concurrent.atomic.AtomicInteger; |
|
67 | 68 | import com.vaadin.flow.server.MockVaadinSession; |
68 | 69 | import com.vaadin.flow.server.RouteRegistry; |
69 | 70 | import com.vaadin.flow.server.ServiceException; |
| 71 | +import com.vaadin.flow.server.WrappedSession; |
70 | 72 | import com.vaadin.flow.server.startup.ApplicationRouteRegistry; |
71 | 73 | import com.vaadin.tests.util.AlwaysLockedVaadinSession; |
72 | 74 | import com.vaadin.tests.util.MockDeploymentConfiguration; |
@@ -743,4 +745,66 @@ public Page getPage() { |
743 | 745 | "No pushState invocation is expected when navigating to the current location.", |
744 | 746 | pushStateCalled.get()); |
745 | 747 | } |
| 748 | + |
| 749 | + @Test |
| 750 | + public void purgeInactiveUIPreservedChainCache_activeUI_throws() { |
| 751 | + MockVaadinServletService service = createMockServiceWithInstantiator(); |
| 752 | + MockVaadinSession session = new AlwaysLockedVaadinSession(service); |
| 753 | + |
| 754 | + MockUI activeUI = new MockUI(session); |
| 755 | + Component attachedToActiveUI = new PreservedView(); |
| 756 | + activeUI.add(attachedToActiveUI); |
| 757 | + |
| 758 | + Assert.assertThrows(IllegalStateException.class, |
| 759 | + () -> AbstractNavigationStateRenderer |
| 760 | + .purgeInactiveUIPreservedChainCache(activeUI)); |
| 761 | + |
| 762 | + } |
| 763 | + |
| 764 | + @Test |
| 765 | + public void purgeInactiveUIPreservedChainCache_inactiveUI_clearsCache() { |
| 766 | + MockVaadinServletService service = createMockServiceWithInstantiator(); |
| 767 | + WrappedSession wrappedSession = Mockito.mock(WrappedSession.class); |
| 768 | + Mockito.when(wrappedSession.getId()).thenReturn("A-SESSION-ID"); |
| 769 | + MockVaadinSession session = new AlwaysLockedVaadinSession(service) { |
| 770 | + @Override |
| 771 | + public WrappedSession getSession() { |
| 772 | + return wrappedSession; |
| 773 | + } |
| 774 | + }; |
| 775 | + |
| 776 | + MockUI activeUI = new MockUI(session); |
| 777 | + Component attachedToActiveUI = new PreservedView(); |
| 778 | + activeUI.add(attachedToActiveUI); |
| 779 | + |
| 780 | + MockUI inActiveUI = new MockUI(session); |
| 781 | + Component attachedToInactiveUI = new PreservedView(); |
| 782 | + inActiveUI.add(attachedToInactiveUI); |
| 783 | + inActiveUI.close(); |
| 784 | + |
| 785 | + // Simulate two tabs on the same view, but one has been closed |
| 786 | + Location location = new Location("preserved"); |
| 787 | + AbstractNavigationStateRenderer.setPreservedChain(session, "ACTIVE", |
| 788 | + location, |
| 789 | + new ArrayList<>(Collections.singletonList(attachedToActiveUI))); |
| 790 | + AbstractNavigationStateRenderer.setPreservedChain(session, "INACTIVE", |
| 791 | + location, new ArrayList<>( |
| 792 | + Collections.singletonList(attachedToInactiveUI))); |
| 793 | + |
| 794 | + AbstractNavigationStateRenderer |
| 795 | + .purgeInactiveUIPreservedChainCache(inActiveUI); |
| 796 | + |
| 797 | + Optional<ArrayList<HasElement>> active = AbstractNavigationStateRenderer |
| 798 | + .getPreservedChain(session, "ACTIVE", location); |
| 799 | + Assert.assertTrue( |
| 800 | + "Expected preserved chain for active window to be present", |
| 801 | + active.isPresent()); |
| 802 | + |
| 803 | + Optional<ArrayList<HasElement>> inactive = AbstractNavigationStateRenderer |
| 804 | + .getPreservedChain(session, "INACTIVE", location); |
| 805 | + Assert.assertFalse( |
| 806 | + "Expected preserved chain for inactive window to be removed", |
| 807 | + inactive.isPresent()); |
| 808 | + |
| 809 | + } |
746 | 810 | } |
0 commit comments