Search in sources :

Example 6 with SerializableConsumer

use of com.vaadin.flow.function.SerializableConsumer in project flow by vaadin.

the class ShortcutRegistrationTest method listenOnUIIsClosing_eventIsPopulatedForANewUI.

@Test
public void listenOnUIIsClosing_eventIsPopulatedForANewUI() {
    UI ui = Mockito.spy(UI.class);
    Component owner = new FakeComponent();
    Component[] components = new Component[] { ui };
    ui.add(owner);
    new ShortcutRegistration(owner, () -> components, event -> {
    }, Key.KEY_A);
    UI newUI = Mockito.spy(UI.class);
    // close the previous UI
    ui.close();
    components[0] = newUI;
    owner.getElement().removeFromTree();
    newUI.add(owner);
    ArgumentCaptor<SerializableConsumer> captor = ArgumentCaptor.forClass(SerializableConsumer.class);
    verify(ui, atLeastOnce()).beforeClientResponse(eq(owner), captor.capture());
    SerializableConsumer consumer = captor.getValue();
    // Fake beforeClientExecution call.
    consumer.accept(mock(ExecutionContext.class));
    // the new UI should now also have expression with KeyA
    Assert.assertTrue(hasKeyAInKeyDownExpression(newUI));
}
Also used : ExecutionContext(com.vaadin.flow.internal.ExecutionContext) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) Test(org.junit.Test)

Example 7 with SerializableConsumer

use of com.vaadin.flow.function.SerializableConsumer in project flow by vaadin.

the class PageTest method fetchCurrentUrl_consumerReceivesCorrectURL.

@Test
public void fetchCurrentUrl_consumerReceivesCorrectURL() {
    // given
    final UI mockUI = new MockUI();
    final Page page = new Page(mockUI) {

        @Override
        public PendingJavaScriptResult executeJs(String expression, Serializable... params) {
            super.executeJs(expression, params);
            Assert.assertEquals("Expected javascript for fetching location is wrong.", "return window.location.href", expression);
            return new PendingJavaScriptResult() {

                @Override
                public boolean cancelExecution() {
                    return false;
                }

                @Override
                public boolean isSentToBrowser() {
                    return false;
                }

                @Override
                public void then(SerializableConsumer<JsonValue> resultHandler, SerializableConsumer<String> errorHandler) {
                    resultHandler.accept(Json.create("http://localhost:8080/home"));
                }
            };
        }
    };
    final AtomicReference<URL> callbackInvocations = new AtomicReference<>();
    final SerializableConsumer<URL> receiver = details -> {
        callbackInvocations.compareAndSet(null, details);
    };
    // when
    page.fetchCurrentURL(receiver);
    // then
    Assert.assertEquals("Returned URL was wrong", "http://localhost:8080/home", callbackInvocations.get().toString());
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) URL(java.net.URL) Registration(com.vaadin.flow.shared.Registration) Json(elemental.json.Json) HashMap(java.util.HashMap) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) Dependency(com.vaadin.flow.shared.ui.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) JsonValue(elemental.json.JsonValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) LinkedList(java.util.LinkedList) Collection(java.util.Collection) Test(org.junit.Test) Serializable(java.io.Serializable) Mockito(org.mockito.Mockito) List(java.util.List) JsonUtils(com.vaadin.flow.internal.JsonUtils) MatcherAssert(org.hamcrest.MatcherAssert) ExecutionCanceler(com.vaadin.flow.component.page.Page.ExecutionCanceler) LoadMode(com.vaadin.flow.shared.ui.LoadMode) Assert(org.junit.Assert) MockUI(com.vaadin.tests.util.MockUI) Serializable(java.io.Serializable) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) URL(java.net.URL) Test(org.junit.Test)

Aggregations

SerializableConsumer (com.vaadin.flow.function.SerializableConsumer)7 Test (org.junit.Test)6 ExecutionContext (com.vaadin.flow.internal.ExecutionContext)4 Registration (com.vaadin.flow.shared.Registration)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Component (com.vaadin.flow.component.Component)3 UI (com.vaadin.flow.component.UI)3 ExecutionCanceler (com.vaadin.flow.component.page.Page.ExecutionCanceler)3 JsonUtils (com.vaadin.flow.internal.JsonUtils)3 Dependency (com.vaadin.flow.shared.ui.Dependency)3 LoadMode (com.vaadin.flow.shared.ui.LoadMode)3 MockUI (com.vaadin.tests.util.MockUI)3 Json (elemental.json.Json)3 JsonValue (elemental.json.JsonValue)3 Serializable (java.io.Serializable)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3