Skip to content

Commit

Permalink
#7911 Implement Component.isAttached(). (#8810)
Browse files Browse the repository at this point in the history
Closes #7911
  • Loading branch information
mvysny authored Aug 14, 2020
1 parent ba7536f commit f9cfe68
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
13 changes: 13 additions & 0 deletions flow-server/src/main/java/com/vaadin/flow/component/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,19 @@ protected void onDetach(DetachEvent detachEvent) {
// NOOP by default
}

/**
* Checks whether this component is currently attached to a UI.
* <p>
* When {@link UI#close()} is called, the UI and the components are not detached immediately;
* the UI cleanup is performed at the end of the current request which also detaches the
* UI and its components.
*
* @return true if the component is attached to an active UI.
*/
public boolean isAttached() {
return getElement().getNode().isAttached();
}

/**
* Sets the value of the given component property.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.vaadin.tests.util.TestUtil;

import elemental.json.Json;
import org.mockito.Mockito;

@NotThreadSafe
public class ComponentTest {
Expand Down Expand Up @@ -757,6 +758,56 @@ public void testSecondAttach() {
Assert.assertFalse(initialAttach.get());
}

/**
* Tests {@link Component#isAttached}.
*/
@Test
public void testIsAttached() {
UI ui = new UI();
// ui is initially attached
Assert.assertTrue(ui.isAttached());

TestComponentContainer parent = new TestComponentContainer();
TestComponentContainer child = new TestComponentContainer();
TestComponent grandChild = new TestComponent();
child.track();
grandChild.addAttachListener(
event -> Assert.assertTrue(grandChild.isAttached()));
grandChild.addDetachListener(
event -> grandChild.getDetachEvents().incrementAndGet());

parent.add(child);
child.add(grandChild);
Assert.assertFalse(parent.isAttached());
Assert.assertFalse(child.isAttached());
Assert.assertFalse(grandChild.isAttached());

ui.add(parent);
Assert.assertTrue(parent.isAttached());
Assert.assertTrue(child.isAttached());
Assert.assertTrue(grandChild.isAttached());

ui.remove(parent);
Assert.assertFalse(parent.isAttached());
Assert.assertFalse(child.isAttached());
Assert.assertFalse(grandChild.isAttached());

ui.add(parent);
Assert.assertTrue(parent.isAttached());
Assert.assertTrue(child.isAttached());
Assert.assertTrue(grandChild.isAttached());

// Mock closing of UI after request handled
ui.getInternals().setSession(Mockito.mock(VaadinSession.class));
ui.close();
ui.getInternals().setSession(null);

Assert.assertFalse(parent.isAttached());
Assert.assertFalse(child.isAttached());
Assert.assertFalse(grandChild.isAttached());
Assert.assertFalse(ui.isAttached());
}

@Test(expected = IllegalArgumentException.class)
public void wrapNullComponentType() {
new Element("div").as(null);
Expand Down

0 comments on commit f9cfe68

Please sign in to comment.