Skip to content

Commit

Permalink
#7911 Implement Component.isAttached(). (#8810)
Browse files Browse the repository at this point in the history
Closes #7911
# Conflicts:
#	flow-server/src/test/java/com/vaadin/flow/component/ComponentTest.java
  • Loading branch information
mvysny authored and caalador committed Aug 19, 2021
1 parent 232df8b commit b1ad691
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 @@ -431,6 +431,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 @@ -56,6 +56,7 @@

import elemental.json.Json;
import net.jcip.annotations.NotThreadSafe;
import org.mockito.Mockito;

@NotThreadSafe
public class ComponentTest {
Expand Down Expand Up @@ -758,6 +759,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 b1ad691

Please sign in to comment.