Search in sources :

Example 21 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class RouterLink method getRouter.

private Router getRouter() {
    Router router = null;
    if (getElement().getNode().isAttached()) {
        StateTree tree = (StateTree) getElement().getNode().getOwner();
        router = tree.getUI().getInternals().getRouter();
    }
    if (router == null) {
        router = VaadinService.getCurrent().getRouter();
    }
    if (router == null) {
        throw new IllegalStateException("Implicit router instance is not available. " + "Use overloaded method with explicit router parameter.");
    }
    return router;
}
Also used : StateTree(com.vaadin.flow.internal.StateTree)

Example 22 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class JavaScriptBootstrapUITest method should_update_pushState_when_navigationHasBeenAlreadyStarted.

@Test
public void should_update_pushState_when_navigationHasBeenAlreadyStarted() {
    ui = Mockito.spy(ui);
    Page page = mockPage();
    UIInternals internals = mockUIInternals();
    Mockito.when(internals.hasLastHandledLocation()).thenReturn(true);
    Location lastLocation = new Location("clean");
    Mockito.when(internals.getLastHandledLocation()).thenReturn(lastLocation);
    StateTree stateTree = Mockito.mock(StateTree.class);
    Mockito.when(internals.getStateTree()).thenReturn(stateTree);
    Mockito.when(internals.getTitle()).thenReturn("");
    StateNode stateNode = BasicElementStateProvider.createStateNode("foo-element");
    Mockito.when(stateTree.getRootNode()).thenReturn(stateNode);
    ArgumentCaptor<String> execJs = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> execArg = ArgumentCaptor.forClass(String.class);
    ui.navigate("clean/1");
    Mockito.verify(page).executeJs(execJs.capture(), execArg.capture());
    assertEquals(CLIENT_PUSHSTATE_TO, execJs.getValue());
    final List<String> execValues = execArg.getAllValues();
    assertEquals(2, execValues.size());
    assertNull(execValues.get(0));
    assertEquals("clean/1", execArg.getValue());
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) StateNode(com.vaadin.flow.internal.StateNode) Page(com.vaadin.flow.component.page.Page) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 23 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class AttachExistingElementRpcHandlerTest method handleNode_error.

@Test
public void handleNode_error() {
    AttachExistingElementRpcHandler handler = new AttachExistingElementRpcHandler();
    int requestedId = 1;
    JsonObject object = Json.createObject();
    object.put(JsonConstants.RPC_ATTACH_REQUESTED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_ASSIGNED_ID, -1);
    object.put(JsonConstants.RPC_ATTACH_TAG_NAME, "div");
    object.put(JsonConstants.RPC_ATTACH_INDEX, -1);
    StateNode node = Mockito.mock(StateNode.class);
    StateNode requested = Mockito.mock(StateNode.class);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(requested);
    AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
    Node<?> parentNode = Mockito.mock(Node.class);
    ChildElementConsumer consumer = Mockito.mock(ChildElementConsumer.class);
    Element sibling = Mockito.mock(Element.class);
    feature.register(parentNode, sibling, requested, consumer);
    Mockito.when(node.getFeature(AttachExistingElementFeature.class)).thenReturn(feature);
    handler.handleNode(node, object);
    Mockito.verify(consumer).onError(parentNode, "div", sibling);
    assertNodeIsUnregistered(node, requested, feature);
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature) Test(org.junit.Test)

Example 24 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class AttachExistingElementRpcHandlerTest method handleNode_requestedIdEqualsAssignedId.

@Test
public void handleNode_requestedIdEqualsAssignedId() {
    AttachExistingElementRpcHandler handler = new AttachExistingElementRpcHandler();
    int requestedId = 1;
    int index = 2;
    JsonObject object = Json.createObject();
    object.put(JsonConstants.RPC_ATTACH_REQUESTED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_ASSIGNED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_TAG_NAME, "div");
    object.put(JsonConstants.RPC_ATTACH_INDEX, index);
    StateNode node = Mockito.mock(StateNode.class);
    StateNode requested = Mockito.mock(StateNode.class);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(requested);
    Mockito.when(requested.hasFeature(Mockito.any())).thenReturn(true);
    AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
    Node<?> parentNode = Mockito.mock(Node.class);
    ChildElementConsumer consumer = Mockito.mock(ChildElementConsumer.class);
    Element sibling = Mockito.mock(Element.class);
    feature.register(parentNode, sibling, requested, consumer);
    Mockito.when(node.getFeature(AttachExistingElementFeature.class)).thenReturn(feature);
    handler.handleNode(node, object);
    assertNodeIsUnregistered(node, requested, feature);
    Mockito.verify(parentNode).insertChild(index, Element.get(requested));
    Mockito.verify(consumer).accept(Element.get(requested));
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature) Test(org.junit.Test)

Example 25 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class ServerRpcHandlerTest method setup.

@Before
public void setup() {
    request = Mockito.mock(VaadinRequest.class);
    service = Mockito.mock(VaadinService.class);
    session = Mockito.mock(VaadinSession.class);
    ui = Mockito.mock(UI.class);
    uiInternals = Mockito.mock(UIInternals.class);
    dependencyList = Mockito.mock(DependencyList.class);
    Mockito.when(request.getService()).thenReturn(service);
    Mockito.when(session.getService()).thenReturn(service);
    Mockito.when(ui.getInternals()).thenReturn(uiInternals);
    Mockito.when(ui.getSession()).thenReturn(session);
    Mockito.when(ui.getCsrfToken()).thenReturn(csrfToken);
    DeploymentConfiguration deploymentConfiguration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(deploymentConfiguration);
    uiTree = new StateTree(uiInternals);
    Mockito.when(uiInternals.getStateTree()).thenReturn(uiTree);
    Mockito.when(uiInternals.getDependencyList()).thenReturn(dependencyList);
    serverRpcHandler = new ServerRpcHandler();
}
Also used : DependencyList(com.vaadin.flow.component.internal.DependencyList) StateTree(com.vaadin.flow.internal.StateTree) VaadinSession(com.vaadin.flow.server.VaadinSession) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) UIInternals(com.vaadin.flow.component.internal.UIInternals) VaadinRequest(com.vaadin.flow.server.VaadinRequest) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Before(org.junit.Before)

Aggregations

StateTree (com.vaadin.flow.internal.StateTree)32 Test (org.junit.Test)21 UI (com.vaadin.flow.component.UI)20 StateNode (com.vaadin.flow.internal.StateNode)18 JsonObject (elemental.json.JsonObject)10 Element (com.vaadin.flow.dom.Element)8 ArrayList (java.util.ArrayList)7 NodeChange (com.vaadin.flow.internal.change.NodeChange)6 UIInternals (com.vaadin.flow.component.internal.UIInternals)4 ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)4 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Component (com.vaadin.flow.component.Component)3 DomEvent (com.vaadin.flow.dom.DomEvent)3 DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)3 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)3 JsonValue (elemental.json.JsonValue)3 NodeOwner (com.vaadin.flow.internal.NodeOwner)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2