Java Code Examples for com.vaadin.flow.dom.Element#getNode()

The following examples show how to use com.vaadin.flow.dom.Element#getNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapSyncRpcHandlerTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void propertyIsNotExplicitlyAllowed_subproperty_throwsWithComponentInfo() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage(CoreMatchers.allOf(
            CoreMatchers.containsString(
                    "Component " + TestComponent.class.getName()),
            CoreMatchers.containsString("'" + TEST_PROPERTY + "'")));
    TestComponent component = new TestComponent();
    Element element = component.getElement();

    StateNode node = element.getNode();
    ElementPropertyMap propertyMap = node
            .getFeature(ElementPropertyMap.class)
            .resolveModelMap("foo.bar");

    new MapSyncRpcHandler().handleNode(propertyMap.getNode(),
            createSyncPropertyInvocation(propertyMap.getNode(),
                    TEST_PROPERTY, NEW_VALUE));
}
 
Example 2
Source File: StateNodeTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void collectChanges_initiallyInactiveElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive() {
    Element element = ElementFactory.createAnchor();

    StateNode stateNode = element.getNode();

    ElementData visibility = stateNode.getFeature(ElementData.class);
    ElementPropertyMap properties = stateNode
            .getFeature(ElementPropertyMap.class);

    TestStateTree tree = new TestStateTree();

    // attach the node to be able to get changes
    tree.getRootNode().getFeature(ElementChildrenList.class).add(0,
            stateNode);

    assertCollectChanges_initiallyInactive(stateNode, properties,
            isVisible -> {
                visibility.setVisible(isVisible);
                stateNode.updateActiveState();
            });
}
 
Example 3
Source File: FlowClassesSerializableTest.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a serialization bug (probably located in JVM ) when serialized
 * {@link Command} is deserialized as some internal lambda and produces
 * {@link ClassCastException}
 *
 * see the workaround in ElementAttributeMap#deferRegistration
 */
@Test
public void streamResource() throws Throwable {
    UI ui = new UI();
    UI.setCurrent(ui);
    try {
        Element element = new Element("dummy-element");
        StreamReceiver streamReceiver = new StreamReceiver(
                element.getNode(), "upload", new MyStreamVariable());
        Assert.assertEquals(ui, UI.getCurrent());
        element.setAttribute("target", streamReceiver);
        serializeAndDeserialize(element);
        assertTrue("Basic smoke test with ",
                element.getAttribute("target").length() > 10);

    } finally {
        UI.setCurrent(null);
    }
}
 
Example 4
Source File: HierarchicalCommunicatorDataTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    ui = new MockUI();
    Element element = new Element("div");
    ui.getElement().appendChild(element);

    treeData = new TreeData<>();
    treeData.addItems(null, ROOT);
    treeData.addItems(ROOT, FOLDER);
    treeData.addItems(FOLDER, LEAF);
    dataProvider = new TreeDataProvider<>(treeData);
    communicator = new HierarchicalDataCommunicator<>(
            Mockito.mock(CompositeDataGenerator.class), arrayUpdater,
            json -> {
            }, element.getNode(),
            () -> (ValueProvider<Item, String>) item -> String
                    .valueOf(item.id));
    communicator.setDataProvider(dataProvider, null);
}
 
Example 5
Source File: WebComponentConfigurationRegistry.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a partial copy of the element sub-tree, with the given
 * {@code rootElement} as the root element of the created tree. The copy
 * cares only about the HTML structure of the element and by-passes state
 * information where possible. The copying is done on element-level: tags,
 * attributes, and contents.
 * <p>
 * This is used create copies from elements which should be moved from
 * document head to each embedded web component on the page.
 * <p>
 * Copies the following
 * {@link com.vaadin.flow.internal.nodefeature.NodeFeature}:
 * <ul>
 * <li>{@link com.vaadin.flow.internal.nodefeature.ElementData}</li>
 * </ul>
 *
 * @param rootElement
 *            element to copy and make the root node of the new element tree
 * @return copy of the given {@code rootElement} with copied child hierarchy
 * @see com.vaadin.flow.dom.ElementUtil#fromJsoup(org.jsoup.nodes.Node) for
 *      the source of the elements being copied
 */
private static Element copyElementTree(Element rootElement) {
    // exception case for text node
    if (rootElement.getNode().hasFeature(TextNodeMap.class)) {
        return Element.createText(rootElement.getText());
    }

    StateNode copyNode = new StateNode(rootElement.getNode());
    // copy ElementData
    ElementData originalData = rootElement.getNode()
            .getFeature(ElementData.class);
    ElementData copyData = copyNode.getFeature(ElementData.class);
    copyData.setTag(originalData.getTag());
    copyData.setPayload(originalData.getPayload());
    copyData.setVisible(originalData.isVisible());

    Element copyElement = Element.get(copyNode);

    // copy relevant attributes
    rootElement.getAttributeNames().forEach(name -> copyElement
            .setAttribute(name, rootElement.getAttribute(name)));
    rootElement.getChildren().forEach(
            child -> copyElement.appendChild(copyElementTree(child)));

    // Element created from the copied StateNode
    return copyElement;
}
 
Example 6
Source File: MapSyncRpcHandlerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void syncJSON_jsonIsPropertyValueOfStateNode_propertySetToNode()
        throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);

    StateNode node = element.getNode();

    // Set model value directly via ElementPropertyMap
    ElementPropertyMap propertyMap = node
            .getFeature(ElementPropertyMap.class);
    propertyMap.setUpdateFromClientFilter(name -> true);

    ElementPropertyMap modelMap = propertyMap.resolveModelMap("foo");
    // fake StateNode has been created for the model
    StateNode model = modelMap.getNode();
    modelMap.setProperty("bar", "baz");

    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", model.getId());

    // send sync request
    sendSynchronizePropertyEvent(element, ui, "foo", json);

    Serializable testPropertyValue = propertyMap.getProperty("foo");

    Assert.assertTrue(testPropertyValue instanceof StateNode);

    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertSame(model, newNode);
}
 
Example 7
Source File: MapSyncRpcHandlerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void syncJSON_jsonIsNotListItemAndNotPropertyValue_propertySetToJSON()
        throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);

    StateNode node = element.getNode();

    TestComponent anotherComonent = new TestComponent();
    StateNode anotherNode = anotherComonent.getElement().getNode();

    ElementPropertyMap.getModel(node)
            .setUpdateFromClientFilter(name -> true);

    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", anotherNode.getId());

    // send sync request
    sendSynchronizePropertyEvent(element, ui, "foo", json);

    Serializable testPropertyValue = node
            .getFeature(ElementPropertyMap.class).getProperty("foo");

    Assert.assertNotSame(anotherNode, testPropertyValue);
    Assert.assertTrue(testPropertyValue instanceof JsonValue);
}
 
Example 8
Source File: EventRpcHandlerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private static JsonObject createElementEventInvocation(Element element,
        String eventType, JsonObject eventData) {
    StateNode node = element.getNode();
    // Copied from ServerConnector
    JsonObject message = Json.createObject();
    message.put(JsonConstants.RPC_NODE, node.getId());
    message.put(JsonConstants.RPC_EVENT_TYPE, eventType);

    if (eventData != null) {
        message.put(JsonConstants.RPC_EVENT_DATA, eventData);
    }

    return message;
}
 
Example 9
Source File: StateNodeTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void collectChanges_initiallyInactiveViaParentElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive() {
    Element element = ElementFactory.createAnchor();

    StateNode stateNode = element.getNode();

    StateNode parent = createTestNode("Parent node",
            ElementPropertyMap.class, ElementData.class,
            ElementChildrenList.class);

    parent.getFeature(ElementChildrenList.class).add(0, stateNode);

    ElementData visibility = parent.getFeature(ElementData.class);

    ElementPropertyMap properties = stateNode
            .getFeature(ElementPropertyMap.class);

    TestStateTree tree = new TestStateTree();

    // attach the node to be able to get changes
    tree.getRootNode().getFeature(ElementChildrenList.class).add(0, parent);

    assertCollectChanges_initiallyInactive(stateNode, properties,
            isVisible -> {
                visibility.setVisible(isVisible);
                parent.updateActiveState();
            });
}
 
Example 10
Source File: DataCommunicatorTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    ui = new MockUI();
    element = new Element("div");
    ui.getElement().appendChild(element);

    lastClear = null;
    lastSet = null;
    lastUpdateId = -1;

    update = new ArrayUpdater.Update() {

        @Override
        public void clear(int start, int length) {
            lastClear = Range.withLength(start, length);
        }

        @Override
        public void set(int start, List<JsonValue> items) {
            lastSet = Range.withLength(start, items.size());
        }

        @Override
        public void commit(int updateId) {
            lastUpdateId = updateId;
        }
    };

    Mockito.when(arrayUpdater.startUpdate(Mockito.anyInt()))
            .thenReturn(update);

    dataCommunicator = new DataCommunicator<>(dataGenerator, arrayUpdater,
            data -> {
            }, element.getNode());
    pageSize = 50;
    dataCommunicator.setPageSize(pageSize);
}
 
Example 11
Source File: MapSyncRpcHandlerTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Test
public void syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy()
        throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);

    StateNode node = element.getNode();

    // Set model value directly via ElementPropertyMap
    ElementPropertyMap propertyMap = node
            .getFeature(ElementPropertyMap.class);
    propertyMap.setUpdateFromClientFilter(name -> true);
    ModelList modelList = propertyMap.resolveModelList("foo");
    // fake StateNode has been created for the model
    StateNode item = new StateNode(ElementPropertyMap.class);
    modelList.add(item);
    item.getFeature(ElementPropertyMap.class).setProperty("bar", "baz");

    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", item.getId());

    // send sync request
    sendSynchronizePropertyEvent(element, ui, TEST_PROPERTY, json);

    // Now the model node should be copied and available as the
    // TEST_PROPERTY value
    Serializable testPropertyValue = propertyMap.getProperty(TEST_PROPERTY);

    Assert.assertTrue(testPropertyValue instanceof StateNode);

    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertNotEquals(item.getId(), newNode.getId());

    Assert.assertEquals("baz", newNode.getFeature(ElementPropertyMap.class)
            .getProperty("bar"));
}