Search in sources :

Example 16 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_attachElement_removeAttributeAfterAttaching.

@Test
public void setResourceAttribute_attachElement_removeAttributeAfterAttaching() throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);
    StreamResource resource = createEmptyResource("resource");
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", resource);
    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;
    ui.getElement().appendChild(element);
    element.removeAttribute("foo");
    TestUtil.isGarbageCollected(ref);
    Assert.assertNull(ref.get());
    Assert.assertFalse(element.hasAttribute("foo"));
    Assert.assertNull(element.getAttribute("foo"));
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) Element(com.vaadin.flow.dom.Element) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Example 17 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_attachElement_setRawAttributeAfterAttaching.

@Test
public void setResourceAttribute_attachElement_setRawAttributeAfterAttaching() throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);
    StreamResource resource = createEmptyResource("resource");
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", resource);
    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;
    ui.getElement().appendChild(element);
    element.setAttribute("foo", "bar");
    TestUtil.isGarbageCollected(ref);
    Assert.assertNull(ref.get());
    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertEquals("bar", element.getAttribute("foo"));
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) Element(com.vaadin.flow.dom.Element) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Example 18 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_elementIsAttached_elementHasAttribute.

@Test
public void setResourceAttribute_elementIsAttached_elementHasAttribute() {
    UI ui = createUI();
    UI.setCurrent(ui);
    String resName = "resource";
    StreamResource resource = createEmptyResource(resName);
    ui.getElement().setAttribute("foo", resource);
    Assert.assertTrue(ui.getElement().hasAttribute("foo"));
    Assert.assertTrue(ui.getElement().getAttribute("foo").endsWith(resName));
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) Test(org.junit.Test)

Example 19 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_attachElement_setAnotherResourceAfterAttaching.

@Test
public void setResourceAttribute_attachElement_setAnotherResourceAfterAttaching() throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);
    StreamResource resource = createEmptyResource("resource1");
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", resource);
    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;
    ui.getElement().appendChild(element);
    String resName = "resource2";
    element.setAttribute("foo", createEmptyResource(resName));
    Assert.assertTrue(element.hasAttribute("foo"));
    TestUtil.isGarbageCollected(ref);
    Assert.assertNull(ref.get());
    String uri = element.getAttribute("foo");
    Optional<StreamResource> res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, new URI(uri));
    Assert.assertTrue(res.isPresent());
    Assert.assertTrue(uri.endsWith(resName));
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) Element(com.vaadin.flow.dom.Element) WeakReference(java.lang.ref.WeakReference) URI(java.net.URI) Test(org.junit.Test)

Example 20 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_detachAndReattachElement_resourceReregistered.

@Test
public void setResourceAttribute_detachAndReattachElement_resourceReregistered() throws URISyntaxException {
    UI ui = createUI();
    UI.setCurrent(ui);
    Element element = ElementFactory.createDiv();
    ui.getElement().appendChild(element);
    String resName = "resource";
    StreamResource resource = createEmptyResource(resName);
    element.setAttribute("foo", resource);
    String attribute = element.getAttribute("foo");
    URI uri = new URI(attribute);
    Optional<StreamResource> res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, uri);
    Assert.assertTrue(res.isPresent());
    ui.getElement().removeAllChildren();
    res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, uri);
    Assert.assertFalse(res.isPresent());
    ui.getElement().appendChild(element);
    res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, uri);
    Assert.assertTrue(res.isPresent());
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) Element(com.vaadin.flow.dom.Element) URI(java.net.URI) Test(org.junit.Test)

Aggregations

StreamResource (com.vaadin.flow.server.StreamResource)26 Test (org.junit.Test)24 UI (com.vaadin.flow.component.UI)14 Element (com.vaadin.flow.dom.Element)13 URI (java.net.URI)12 WeakReference (java.lang.ref.WeakReference)9 StreamRegistration (com.vaadin.flow.server.StreamRegistration)4 StreamResourceRegistry (com.vaadin.flow.server.StreamResourceRegistry)4 Anchor (com.vaadin.flow.component.html.Anchor)1 ContentTypeResolver (com.vaadin.flow.function.ContentTypeResolver)1 AbstractStreamResource (com.vaadin.flow.server.AbstractStreamResource)1 StreamReceiver (com.vaadin.flow.server.StreamReceiver)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1