Search in sources :

Example 1 with SerializableConsumer

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

the class ShortcutRegistrationTest method clientResponse.

/**
 * Works only with the {@code registration} member variable, but allows
 * configuring the {@code listenOn} component
 *
 * Simulates a "beforeClientResponse" callback for the given
 * {@link ShortcutRegistration}
 */
private void clientResponse(Component[] listenOnMock) {
    for (Component component : listenOnMock) {
        when(component.getElement()).thenReturn(new Element("tag"));
        when(component.getEventBus()).thenReturn(new ComponentEventBus(component));
    }
    ArgumentCaptor<SerializableConsumer> captor = ArgumentCaptor.forClass(SerializableConsumer.class);
    verify(ui, atLeastOnce()).beforeClientResponse(eq(lifecycleOwner), captor.capture());
    SerializableConsumer consumer = captor.getValue();
    // Fake beforeClientExecution call.
    consumer.accept(mock(ExecutionContext.class));
}
Also used : ExecutionContext(com.vaadin.flow.internal.ExecutionContext) Element(com.vaadin.flow.dom.Element) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer)

Example 2 with SerializableConsumer

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

the class ShortcutRegistrationTest method reattachComponent_detachListenerIsAddedOnEveryAttach_listenOnUIIsClosing_eventIsPopulatedForANewUI.

@Test
public void reattachComponent_detachListenerIsAddedOnEveryAttach_listenOnUIIsClosing_eventIsPopulatedForANewUI() {
    UI ui = Mockito.spy(UI.class);
    Component owner = new FakeComponent();
    Registration registration = Mockito.mock(Registration.class);
    AtomicInteger count = new AtomicInteger();
    Mockito.when(ui.addDetachListener(any())).thenAnswer(invocation -> {
        count.incrementAndGet();
        return registration;
    });
    Component[] components = new Component[] { ui };
    ui.add(owner);
    new ShortcutRegistration(owner, () -> components, event -> {
    }, Key.KEY_A);
    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));
    Assert.assertEquals(1, count.get());
    ui.remove(owner);
    // reattach
    ui.add(owner);
    // Fake beforeClientExecution call.
    consumer.accept(mock(ExecutionContext.class));
    Assert.assertEquals(2, count.get());
    UI newUI = Mockito.spy(UI.class);
    // close the previous UI
    ui.close();
    components[0] = newUI;
    owner.getElement().removeFromTree();
    newUI.add(owner);
    verify(newUI, atLeastOnce()).beforeClientResponse(eq(owner), captor.capture());
    // Fake beforeClientExecution call.
    captor.getValue().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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Registration(com.vaadin.flow.shared.Registration) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) Test(org.junit.Test)

Example 3 with SerializableConsumer

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

the class PageTest method retrieveExtendedClientDetails_twice_jsOnceAndCallbackTwice.

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

        @Override
        public PendingJavaScriptResult executeJs(String expression, Serializable... params) {
            super.executeJs(expression, params);
            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) {
                    final HashMap<String, String> params = new HashMap<>();
                    params.put("v-sw", "2560");
                    params.put("v-sh", "1450");
                    params.put("v-tzo", "-270");
                    params.put("v-rtzo", "-210");
                    params.put("v-dstd", "60");
                    params.put("v-dston", "true");
                    params.put("v-tzid", "Asia/Tehran");
                    params.put("v-curdate", "1555000000000");
                    params.put("v-td", "false");
                    params.put("v-wn", "ROOT-1234567-0.1234567");
                    resultHandler.accept(JsonUtils.createObject(params, Json::create));
                }
            };
        }
    };
    final AtomicInteger callbackInvocations = new AtomicInteger();
    final Page.ExtendedClientDetailsReceiver receiver = details -> {
        callbackInvocations.incrementAndGet();
    };
    // when
    page.retrieveExtendedClientDetails(receiver);
    page.retrieveExtendedClientDetails(receiver);
    // then
    final int jsInvocations = mockUI.getInternals().dumpPendingJavaScriptInvocations().size();
    Assert.assertEquals(1, jsInvocations);
    Assert.assertEquals(2, callbackInvocations.get());
}
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) Serializable(java.io.Serializable) HashMap(java.util.HashMap) MockUI(com.vaadin.tests.util.MockUI) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) Test(org.junit.Test)

Example 4 with SerializableConsumer

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

the class PageTest method retrieveExtendedClientDetails_twice_theSecondResultComesDifferentBeforeCachedValueIsSet.

@Test
public void retrieveExtendedClientDetails_twice_theSecondResultComesDifferentBeforeCachedValueIsSet() {
    // given
    final UI mockUI = new MockUI();
    List<Runnable> invocations = new ArrayList<>();
    final Page page = new Page(mockUI) {

        @Override
        public PendingJavaScriptResult executeJs(String expression, Serializable... params) {
            super.executeJs(expression, params);
            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) {
                    final HashMap<String, String> params = new HashMap<>();
                    params.put("v-sw", "2560");
                    params.put("v-sh", "1450");
                    params.put("v-tzo", "-270");
                    params.put("v-rtzo", "-210");
                    params.put("v-dstd", "60");
                    params.put("v-dston", "true");
                    params.put("v-tzid", "Asia/Tehran");
                    params.put("v-curdate", "1555000000000");
                    params.put("v-td", "false");
                    if (invocations.isEmpty()) {
                        params.put("v-wn", "ROOT-1234567-0.1234567");
                    } else {
                        params.put("v-wn", "foo");
                    }
                    invocations.add(() -> resultHandler.accept(JsonUtils.createObject(params, Json::create)));
                }
            };
        }
    };
    final AtomicInteger callbackInvocations = new AtomicInteger();
    final Page.ExtendedClientDetailsReceiver receiver = details -> {
        callbackInvocations.incrementAndGet();
    };
    // when
    page.retrieveExtendedClientDetails(receiver);
    page.retrieveExtendedClientDetails(receiver);
    // then : before cached value is set the second retrieve is requested
    invocations.forEach(Runnable::run);
    Assert.assertEquals(2, callbackInvocations.get());
    Assert.assertEquals("ROOT-1234567-0.1234567", mockUI.getInternals().getExtendedClientDetails().getWindowName());
}
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) Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Json(elemental.json.Json) MockUI(com.vaadin.tests.util.MockUI) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) Test(org.junit.Test)

Example 5 with SerializableConsumer

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

the class ShortcutRegistrationTest method listenOnComponentIsChanged_eventIsPopulatedForANewListenOnComponent.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void listenOnComponentIsChanged_eventIsPopulatedForANewListenOnComponent() {
    UI ui = Mockito.spy(UI.class);
    Component owner = new FakeComponent();
    Component initialComponentToListenOn = new FakeComponent();
    Component[] components = new Component[] { initialComponentToListenOn };
    ui.add(owner);
    ui.add(initialComponentToListenOn);
    new ShortcutRegistration(owner, () -> components, event -> {
    }, Key.KEY_A);
    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));
    // Once the shortcut listener is registered the expression should
    // contain KeyA
    Assert.assertTrue(hasKeyAInKeyDownExpression(initialComponentToListenOn));
    Component replacementComponentToListenOn = new FakeComponent();
    components[0] = replacementComponentToListenOn;
    ui.add(components[0]);
    // detach the original "listen on" component: the new one replaces the
    // old one
    ui.remove(initialComponentToListenOn);
    // now re-attach the owner
    ui.remove(owner);
    ui.add(owner);
    consumer.accept(mock(ExecutionContext.class));
    // the new component should now also have expression with KeyA
    Assert.assertTrue(hasKeyAInKeyDownExpression(replacementComponentToListenOn));
}
Also used : ExecutionContext(com.vaadin.flow.internal.ExecutionContext) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) 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