Skip to content

Commit

Permalink
Ignore component removal if the component has no parent (#4537)
Browse files Browse the repository at this point in the history
Fixes #4528
  • Loading branch information
Denis authored and caalador committed Aug 28, 2018
1 parent db73c7b commit f47daba
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
Expand Up @@ -17,6 +17,8 @@

import java.util.Objects;

import org.slf4j.LoggerFactory;

import com.vaadin.flow.dom.Element;

/**
Expand Down Expand Up @@ -62,7 +64,13 @@ default void remove(Component... components) {
for (Component component : components) {
Objects.requireNonNull(component,
"Component to remove cannot be null");
if (getElement().equals(component.getElement().getParent())) {
Element parent = component.getElement().getParent();
if (parent == null) {
LoggerFactory.getLogger(HasComponents.class).debug(
"Remove of a component with no parent does nothing.");
return;
}
if (getElement().equals(parent)) {
getElement().removeChild(component.getElement());
} else {
throw new IllegalArgumentException("The given component ("
Expand All @@ -84,7 +92,7 @@ default void removeAll() {
/**
* Adds the given component as child of this component at the specific
* index.
*
*
* @param index
* the index, where the component will be added. If {@code index}
* is negative, the component will be added as the first child of
Expand All @@ -109,7 +117,7 @@ default void addComponentAtIndex(int index, Component component) {

/**
* Adds the given component as the first child of this component.
*
*
* @param component
* the component to add, value should not be null
*/
Expand Down
Expand Up @@ -44,9 +44,8 @@ public void insertComponentAtIndex() {
innerComponent.setId("insert-component-index");
component.addComponentAtIndex(2, innerComponent);
checkChildren(4, component);
Assert.assertEquals(innerComponent.getId(),
component.getElement().getChild(2).getComponent().get()
.getId());
Assert.assertEquals(innerComponent.getId(), component.getElement()
.getChild(2).getComponent().get().getId());
}

@Test
Expand All @@ -67,9 +66,45 @@ public void insertComponentIndexGreaterThanChildrenNumber() {
innerComponent.setId("insert-component-index-greater");
component.addComponentAtIndex(100, innerComponent);
checkChildren(4, component);
Assert.assertEquals(innerComponent.getId(), component.getElement()
.getChild((int) (component.getChildren().count() - 1))
.getComponent().get().getId());
Assert.assertEquals(innerComponent.getId(),
component.getElement()
.getChild((int) (component.getChildren().count() - 1))
.getComponent().get().getId());
}

@Test
public void remove_removeComponentWithNoParent() {
TestComponent component = createTestStructure();
TestComponent innerComponent = new TestComponent();

// No any exception is thrown
component.remove(innerComponent);
}

@Test
public void remove_removeComponentWithCorrectParent() {
TestComponent component = createTestStructure();
TestComponent innerComponent = new TestComponent();

long size = component.getChildren().count();

component.add(innerComponent);

component.remove(innerComponent);

Assert.assertEquals(size, component.getChildren().count());
}

@Test(expected = IllegalArgumentException.class)
public void remove_removeComponentWithDifferentParent() {
TestComponent component = createTestStructure();

TestComponent another = createTestStructure();
TestComponent innerComponent = new TestComponent();

another.add(innerComponent);

component.remove(innerComponent);
}

private TestComponent createTestStructure() {
Expand Down
Expand Up @@ -307,13 +307,6 @@ public void removeComponent() {
ComponentTest.assertChildren(ui);
}

@Test(expected = IllegalArgumentException.class)
public void removeNotChildComponent() {
UI ui = new UI();
Text text = new Text("foo");
ui.remove(text);
}

@Test
public void setSession_attachEventIsFired()
throws InvalidRouteConfigurationException {
Expand Down

0 comments on commit f47daba

Please sign in to comment.