Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement Component.isAttached(). (#8810) #11601

Merged
merged 2 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 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,20 @@ 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.
caalador marked this conversation as resolved.
Show resolved Hide resolved
* @since 2.7
*/
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