Search in sources :

Example 1 with UIInitEvent

use of com.vaadin.flow.server.UIInitEvent in project flow by vaadin.

the class VaadinRouteScopeTest method refresh_uiWithTheSameWindowName_beanInScopeIsDestroyedAfterRefresh.

@Test
public void refresh_uiWithTheSameWindowName_beanInScopeIsDestroyedAfterRefresh() {
    UI ui = mockUI();
    UI anotherUI = makeAnotherUI(ui);
    ExtendedClientDetails details = Mockito.mock(ExtendedClientDetails.class);
    Mockito.when(details.getWindowName()).thenReturn("bar");
    ui.getInternals().setExtendedClientDetails(details);
    anotherUI.getInternals().setExtendedClientDetails(details);
    ui.getSession().addUI(ui);
    ui.getSession().addUI(anotherUI);
    mockServletContext(ui);
    VaadinRouteScope scope = initScope(ui);
    AtomicInteger count = new AtomicInteger();
    scope.registerDestructionCallback("foo", () -> count.getAndIncrement());
    scope.uiInit(new UIInitEvent(ui, ui.getSession().getService()));
    navigateTo(ui, new NavigationTarget());
    putObjectIntoScope(scope);
    // close the first UI
    ui.getSession().removeUI(ui);
    // the bean is not removed since there is a "preserved" UI
    Assert.assertEquals(0, count.get());
    UI.setCurrent(anotherUI);
    scope = initScope(anotherUI);
    // the bean is not removed since there is a "preserved" UI
    Assert.assertEquals(0, count.get());
    navigateTo(anotherUI, new AnotherNavigationTarget());
    // the bean is removed since navigation away from it's owner navigation
    // target
    Assert.assertEquals(1, count.get());
}
Also used : UI(com.vaadin.flow.component.UI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) UIInitEvent(com.vaadin.flow.server.UIInitEvent) Test(org.junit.Test)

Example 2 with UIInitEvent

use of com.vaadin.flow.server.UIInitEvent in project flow by vaadin.

the class VaadinRouteScopeTest method initScope.

private VaadinRouteScope initScope(UI ui) {
    VaadinRouteScope scope = getScope();
    scope.getBeanStore();
    scope.uiInit(new UIInitEvent(ui, ui.getSession().getService()));
    return scope;
}
Also used : UIInitEvent(com.vaadin.flow.server.UIInitEvent)

Example 3 with UIInitEvent

use of com.vaadin.flow.server.UIInitEvent in project flow by vaadin.

the class VaadinRouteScopeTest method detachUI_uiWithDifferentWindowName_beanInScopeIsDestroyedwhenUIIsDetached.

@Test
public void detachUI_uiWithDifferentWindowName_beanInScopeIsDestroyedwhenUIIsDetached() {
    UI ui = mockUI();
    UI anotherUI = makeAnotherUI(ui);
    ExtendedClientDetails details = Mockito.mock(ExtendedClientDetails.class);
    Mockito.when(details.getWindowName()).thenReturn("bar");
    ui.getInternals().setExtendedClientDetails(details);
    ui.getSession().addUI(ui);
    ui.getSession().addUI(anotherUI);
    mockServletContext(ui);
    VaadinRouteScope scope = initScope(ui);
    AtomicInteger count = new AtomicInteger();
    scope.registerDestructionCallback("foo", () -> count.getAndIncrement());
    scope.uiInit(new UIInitEvent(ui, ui.getSession().getService()));
    navigateTo(ui, new NavigationTarget());
    putObjectIntoScope(scope);
    // close the first UI
    ui.getSession().removeUI(ui);
    // the bean is removed since there is no UI with the window name "bar"
    // present.
    Assert.assertEquals(1, count.get());
    count.set(0);
    UI.setCurrent(anotherUI);
    scope = initScope(anotherUI);
    navigateTo(anotherUI, new AnotherNavigationTarget());
    // the bean is not removed since it's already has been removed when the
    // first UI is detached.
    Assert.assertEquals(0, count.get());
}
Also used : UI(com.vaadin.flow.component.UI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) UIInitEvent(com.vaadin.flow.server.UIInitEvent) Test(org.junit.Test)

Aggregations

UIInitEvent (com.vaadin.flow.server.UIInitEvent)3 UI (com.vaadin.flow.component.UI)2 ExtendedClientDetails (com.vaadin.flow.component.page.ExtendedClientDetails)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2