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