use of com.vaadin.flow.dom.ChildElementConsumer in project flow by vaadin.
the class AttachExistingElementFeatureTest method register_dataIsAvailaleByNode.
@Test
public void register_dataIsAvailaleByNode() {
StateNode node = new StateNode();
AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
Element element = Mockito.mock(Element.class);
StateNode child = Mockito.mock(StateNode.class);
ChildElementConsumer callback = Mockito.mock(ChildElementConsumer.class);
Node<?> parent = Mockito.mock(Node.class);
feature.register(parent, element, child, callback);
Mockito.verify(child).setParent(node);
Assert.assertEquals(callback, feature.getCallback(child));
Assert.assertEquals(parent, feature.getParent(child));
Assert.assertEquals(element, feature.getPreviousSibling(child));
}
use of com.vaadin.flow.dom.ChildElementConsumer 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);
}
use of com.vaadin.flow.dom.ChildElementConsumer 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));
}
use of com.vaadin.flow.dom.ChildElementConsumer in project flow by vaadin.
the class AbstractNodeStateProvider method attachExistingElement.
@Override
public void attachExistingElement(StateNode node, String tagName, Element previousSibling, ChildElementConsumer callback) {
if (tagName == null) {
throw new IllegalArgumentException("Tag name parameter cannot be null");
}
if (callback == null) {
throw new IllegalArgumentException("Callback parameter cannot be null");
}
/*
* create a node that should represent the client-side element. This
* node won't be available anywhere and will be removed if there is no
* appropriate element on the client-side. This node will be used after
* client-side roundtrip for the appropriate element.
*/
StateNode proposedNode = BasicElementStateProvider.createStateNode(tagName);
node.runWhenAttached(ui -> ui.getInternals().getStateTree().beforeClientResponse(node, context -> {
node.getFeature(AttachExistingElementFeature.class).register(getNode(node), previousSibling, proposedNode, callback);
ui.getPage().executeJs("this.attachExistingElement($0, $1, $2, $3);", getNode(node), previousSibling, tagName, proposedNode.getId());
}));
}
Aggregations