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"));
}
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"));
}
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));
}
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));
}
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());
}
Aggregations