Search in sources :

Example 21 with StreamResource

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

the class ElementTest method setResourceAttribute_detachElement_resourceIsUnregistered.

@Test
public void setResourceAttribute_detachElement_resourceIsUnregistered() throws URISyntaxException, InterruptedException {
    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");
    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;
    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());
    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertNotNull(element.getAttribute("foo"));
    Assert.assertTrue(element.getAttribute("foo").endsWith(resName));
    element.setAttribute("foo", "bar");
    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertEquals("bar", element.getAttribute("foo"));
    TestUtil.isGarbageCollected(ref);
}
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 22 with StreamResource

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

the class ElementTest method setResourceAttribute_attachElement_setAnotherResource.

@Test
public void setResourceAttribute_attachElement_setAnotherResource() 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;
    String resName = "resource2";
    element.setAttribute("foo", createEmptyResource(resName));
    ui.getElement().appendChild(element);
    Assert.assertTrue(element.hasAttribute("foo"));
    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));
    // allow GC to collect element and all its (detach) listeners
    element = null;
    TestUtil.isGarbageCollected(ref);
}
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 23 with StreamResource

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

the class ElementTest method setResourceAttribute_attachElement_removeAttribute.

@Test
public void setResourceAttribute_attachElement_removeAttribute() 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;
    element.removeAttribute("foo");
    ui.getElement().appendChild(element);
    TestUtil.isGarbageCollected(ref);
    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 24 with StreamResource

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

the class ElementTest method setResourceAttribute_attachAndDetachAndReattachElement_resourceReregistered.

@Test
public void setResourceAttribute_attachAndDetachAndReattachElement_resourceReregistered() throws URISyntaxException {
    UI ui = createUI();
    UI.setCurrent(ui);
    Element element = ElementFactory.createDiv();
    String resName = "resource";
    StreamResource resource = createEmptyResource(resName);
    element.setAttribute("foo", resource);
    String attribute = element.getAttribute("foo");
    ui.getElement().appendChild(element);
    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)

Example 25 with StreamResource

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

the class ElementTest method setResourceAttributeSeveralTimes_elementIsNotAttached_elementHasAttribute.

@Test
public void setResourceAttributeSeveralTimes_elementIsNotAttached_elementHasAttribute() {
    UI.setCurrent(createUI());
    Element element = ElementFactory.createDiv();
    String resName = "resource";
    StreamResource resource = createEmptyResource(resName);
    element.setAttribute("foo", resource);
    Assert.assertTrue(element.hasAttribute("foo"));
    resName = "resource1";
    resource = createEmptyResource(resName);
    element.setAttribute("foo", resource);
    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertTrue(element.getAttribute("foo").endsWith(resName));
}
Also used : StreamResource(com.vaadin.flow.server.StreamResource) Element(com.vaadin.flow.dom.Element) 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