Search in sources :

Example 1 with Registration

use of com.vaadin.flow.shared.Registration in project flow by vaadin.

the class BootstrapHandlerTest method uiInitialization_allRegisteredListenersAreNotified.

// UIInitListeners
@Test
public void uiInitialization_allRegisteredListenersAreNotified() {
    BootstrapHandler bootstrapHandler = new BootstrapHandler();
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    AtomicReference<UI> uiReference = new AtomicReference<>();
    Registration registration = service.addUIInitListener(event -> Assert.assertTrue("Atomic reference was not empty.", uiReference.compareAndSet(null, event.getUI())));
    final BootstrapContext context = bootstrapHandler.createAndInitUI(TestUI.class, createVaadinRequest(), response, session);
    Assert.assertEquals("Event UI didn't match initialized UI instance.", context.getUI(), uiReference.get());
    // unregister listener
    registration.remove();
    AtomicReference<UI> secondListenerReference = new AtomicReference<>();
    service.addUIInitListener(event -> Assert.assertTrue("Atomic reference did not contain previous UI.", uiReference.compareAndSet(context.getUI(), event.getUI())));
    service.addUIInitListener(event -> Assert.assertTrue("Atomic reference was not empty.", secondListenerReference.compareAndSet(null, event.getUI())));
    final BootstrapContext secondInit = bootstrapHandler.createAndInitUI(TestUI.class, createVaadinRequest(), response, session);
    Assert.assertEquals("Event UI didn't match initialized UI instance.", secondInit.getUI(), uiReference.get());
    Assert.assertEquals("Second event UI didn't match initialized UI instance.", secondInit.getUI(), secondListenerReference.get());
}
Also used : UI(com.vaadin.flow.component.UI) Registration(com.vaadin.flow.shared.Registration) AtomicReference(java.util.concurrent.atomic.AtomicReference) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Test(org.junit.Test)

Example 2 with Registration

use of com.vaadin.flow.shared.Registration in project flow by vaadin.

the class BootstrapHandlerTest method uiInitialization_changingListenersOnEventWorks.

// UIInitListeners
@Test
public void uiInitialization_changingListenersOnEventWorks() {
    BootstrapHandler bootstrapHandler = new BootstrapHandler();
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    AtomicReference<UI> uiReference = new AtomicReference<>();
    Registration registration = service.addUIInitListener(event -> service.addUIInitListener(laterEvent -> uiReference.compareAndSet(null, laterEvent.getUI())));
    bootstrapHandler.createAndInitUI(TestUI.class, createVaadinRequest(), response, session);
    Assert.assertEquals("Event UI didn't match initialized UI instance.", null, uiReference.get());
    // unregister listener
    registration.remove();
    service.addUIInitListener(event -> registration.remove());
    final BootstrapContext secondInit = bootstrapHandler.createAndInitUI(TestUI.class, createVaadinRequest(), response, session);
    Assert.assertEquals("Event UI didn't match initialized UI instance.", secondInit.getUI(), uiReference.get());
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) Inline(com.vaadin.flow.component.page.Inline) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) TargetElement(com.vaadin.flow.component.page.TargetElement) Registration(com.vaadin.flow.shared.Registration) PageTitle(com.vaadin.flow.router.PageTitle) Router(com.vaadin.flow.router.Router) Route(com.vaadin.flow.router.Route) RouteAlias(com.vaadin.flow.router.RouteAlias) Theme(com.vaadin.flow.theme.Theme) Assert.assertThat(org.junit.Assert.assertThat) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) After(org.junit.After) UI(com.vaadin.flow.component.UI) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) BodySize(com.vaadin.flow.component.page.BodySize) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Matchers(org.mockito.Matchers) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) Text(com.vaadin.flow.component.Text) RouterLayout(com.vaadin.flow.router.RouterLayout) Html(com.vaadin.flow.component.Html) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) InlineTemplate(com.vaadin.flow.template.angular.InlineTemplate) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Mockito(org.mockito.Mockito) Assert(org.junit.Assert) Collections(java.util.Collections) Viewport(com.vaadin.flow.component.page.Viewport) ParentLayout(com.vaadin.flow.router.ParentLayout) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) UI(com.vaadin.flow.component.UI) Registration(com.vaadin.flow.shared.Registration) AtomicReference(java.util.concurrent.atomic.AtomicReference) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Test(org.junit.Test)

Example 3 with Registration

use of com.vaadin.flow.shared.Registration in project flow by vaadin.

the class StateNodeTest method testDetachListener_listenerRemoved_listenerNotTriggered.

@Test
public void testDetachListener_listenerRemoved_listenerNotTriggered() {
    StateNode root = new RootStateNode();
    TestStateNode child = new TestStateNode();
    setParent(child, root);
    Assert.assertTrue(child.isAttached());
    AtomicBoolean triggered = new AtomicBoolean(false);
    Registration registrationHandle = child.addDetachListener(() -> triggered.set(true));
    registrationHandle.remove();
    setParent(child, null);
    Assert.assertFalse("Detach listener was triggered even though handler was removed.", triggered.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Registration(com.vaadin.flow.shared.Registration) Test(org.junit.Test)

Example 4 with Registration

use of com.vaadin.flow.shared.Registration in project flow by vaadin.

the class ElementListenersTest method testEventNameInClientData.

@Test
public void testEventNameInClientData() {
    Assert.assertFalse(ns.contains("foo"));
    Registration handle = ns.add("foo", noOp, new String[0]);
    Assert.assertEquals(0, getExpressions("foo").size());
    handle.remove();
    Assert.assertFalse(ns.contains("foo"));
}
Also used : Registration(com.vaadin.flow.shared.Registration) Test(org.junit.Test)

Example 5 with Registration

use of com.vaadin.flow.shared.Registration in project flow by vaadin.

the class ElementPropertyMapTest method removePropertyChangeListener_fireEvent_listenerIsNotNotified.

@Test
public void removePropertyChangeListener_fireEvent_listenerIsNotNotified() {
    ElementPropertyMap map = createSimplePropertyMap();
    PropertyChangeListener listener = ev -> {
        Assert.fail();
    };
    Registration registration = map.addPropertyChangeListener("foo", listener);
    registration.remove();
    // listener is not called. Otherwise its assertion fails.
    map.setProperty("foo", "bar", true);
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) BasicElementStateProvider(com.vaadin.flow.dom.impl.BasicElementStateProvider) PropertyChangeListener(com.vaadin.flow.dom.PropertyChangeListener) Registration(com.vaadin.flow.shared.Registration) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Serializable(java.io.Serializable) HashSet(java.util.HashSet) Element(com.vaadin.flow.dom.Element) PropertyChangeEvent(com.vaadin.flow.dom.PropertyChangeEvent) Assert(org.junit.Assert) PropertyChangeListener(com.vaadin.flow.dom.PropertyChangeListener) Registration(com.vaadin.flow.shared.Registration) Test(org.junit.Test)

Aggregations

Registration (com.vaadin.flow.shared.Registration)25 Test (org.junit.Test)20 UI (com.vaadin.flow.component.UI)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 TestComponent (com.vaadin.flow.component.ComponentTest.TestComponent)4 Element (com.vaadin.flow.dom.Element)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 StreamRegistration (com.vaadin.flow.server.StreamRegistration)3 JsonObject (elemental.json.JsonObject)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 BootstrapContext (com.vaadin.flow.server.BootstrapHandler.BootstrapContext)2 Set (java.util.Set)2 Assert (org.junit.Assert)2 Component (com.vaadin.flow.component.Component)1 Html (com.vaadin.flow.component.Html)1 Tag (com.vaadin.flow.component.Tag)1 Text (com.vaadin.flow.component.Text)1 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)1