diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java index 5467af9a69d..98cd25c1fe2 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java @@ -479,6 +479,9 @@ void removeChild(Component child) { private void onItemReorderEnd( DashboardItemReorderEndEvent dashboardItemReorderEndEvent) { + if (!isEditable()) { + return; + } JsonArray orderedItemsFromClient = dashboardItemReorderEndEvent .getItems(); reorderItems(orderedItemsFromClient); @@ -487,6 +490,9 @@ private void onItemReorderEnd( private void onItemResizeEnd( DashboardItemResizeEndEvent dashboardItemResizeEndEvent) { + if (!isEditable()) { + return; + } DashboardWidget resizedWidget = dashboardItemResizeEndEvent .getResizedWidget(); resizedWidget.setRowspan(dashboardItemResizeEndEvent.getRowspan()); @@ -495,6 +501,9 @@ private void onItemResizeEnd( private void onItemRemoved( DashboardItemRemovedEvent dashboardItemRemovedEvent) { + if (!isEditable()) { + return; + } dashboardItemRemovedEvent.getRemovedItem().removeFromParent(); } diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardDragReorderTest.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardDragReorderTest.java index fd39b0d2fdb..bdddead6019 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardDragReorderTest.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardDragReorderTest.java @@ -37,6 +37,7 @@ public void setup() { super.setup(); dashboard = new Dashboard(); dashboard.add(new DashboardWidget(), new DashboardWidget()); + dashboard.setEditable(true); DashboardSection section = dashboard.addSection(); section.add(new DashboardWidget(), new DashboardWidget()); getUi().add(dashboard); @@ -59,6 +60,15 @@ public void reorderWidgetInSection_orderIsUpdated() { assertSectionWidgetReorder(2, 0, 1); } + @Test + public void setDashboardNotEditable_reorderWidget_orderIsNotUpdated() { + dashboard.setEditable(false); + List expectedRootLevelNodeIds = getRootLevelNodeIds(); + reorderRootLevelItem(0, 1); + fireItemReorderEndEvent(); + Assert.assertEquals(expectedRootLevelNodeIds, getRootLevelNodeIds()); + } + private void fireItemReorderEndEvent() { ComponentUtil.fireEvent(dashboard, new DashboardItemReorderEndEvent(dashboard, false, itemsArray)); diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardDragResizeTest.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardDragResizeTest.java index 3a8771341c2..8a901a0f961 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardDragResizeTest.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardDragResizeTest.java @@ -27,6 +27,7 @@ public void setup() { super.setup(); dashboard = new Dashboard(); dashboard.add(new DashboardWidget()); + dashboard.setEditable(true); DashboardSection section = dashboard.addSection(); section.add(new DashboardWidget()); getUi().add(dashboard); @@ -63,6 +64,15 @@ public void resizeWidgetInSectionBothHorizontallyAndVertically_sizeIsUpdated() { assertWidgetResized(1, 2, 2); } + @Test + public void setDashboardNotEditable_resizeWidget_sizeIsNotUpdated() { + dashboard.setEditable(false); + DashboardWidget widgetToResize = dashboard.getWidgets().get(0); + fireItemResizeEndEvent(widgetToResize, 2, 2); + Assert.assertEquals(1, widgetToResize.getColspan()); + Assert.assertEquals(1, widgetToResize.getRowspan()); + } + private void assertWidgetResized(int widgetIndexToResize, int targetColspan, int targetRowspan) { DashboardWidget widgetToResize = dashboard.getWidgets() diff --git a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java index babd21ee882..3c50d285c7a 100644 --- a/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java +++ b/vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/tests/DashboardTest.java @@ -8,11 +8,16 @@ */ package com.vaadin.flow.component.dashboard.tests; +import java.util.Set; +import java.util.stream.Collectors; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import com.vaadin.flow.component.ComponentUtil; import com.vaadin.flow.component.dashboard.Dashboard; +import com.vaadin.flow.component.dashboard.DashboardItemRemovedEvent; import com.vaadin.flow.component.dashboard.DashboardSection; import com.vaadin.flow.component.dashboard.DashboardWidget; import com.vaadin.flow.component.html.Div; @@ -815,4 +820,40 @@ public void detachDashboard_addWidget_reattachDashboard_widgetIsAdded() { fakeClientCommunication(); assertChildComponents(dashboard, widget); } + + @Test + public void dashboardNotEditable_removeWidget_widgetIsNotRemoved() { + DashboardWidget widgetToRemove = new DashboardWidget(); + dashboard.add(widgetToRemove); + fakeClientCommunication(); + int expectedWidgetCount = dashboard.getWidgets().size(); + int expectedNodeId = widgetToRemove.getElement().getNode().getId(); + fireItemRemovedEvent(expectedNodeId); + Assert.assertEquals(expectedWidgetCount, dashboard.getWidgets().size()); + Set actualNodeIds = dashboard.getWidgets().stream() + .map(widget -> widget.getElement().getNode().getId()) + .collect(Collectors.toSet()); + Assert.assertTrue(actualNodeIds.contains(expectedNodeId)); + } + + @Test + public void setDashboardEditable_removeWidget_widgetIsRemoved() { + DashboardWidget widgetToRemove = new DashboardWidget(); + dashboard.add(widgetToRemove); + dashboard.setEditable(true); + fakeClientCommunication(); + int expectedWidgetCount = dashboard.getWidgets().size() - 1; + int nodeIdToBeRemoved = widgetToRemove.getElement().getNode().getId(); + fireItemRemovedEvent(nodeIdToBeRemoved); + Assert.assertEquals(expectedWidgetCount, dashboard.getWidgets().size()); + Set actualNodeIds = dashboard.getWidgets().stream() + .map(widget -> widget.getElement().getNode().getId()) + .collect(Collectors.toSet()); + Assert.assertFalse(actualNodeIds.contains(nodeIdToBeRemoved)); + } + + private void fireItemRemovedEvent(int nodeId) { + ComponentUtil.fireEvent(dashboard, + new DashboardItemRemovedEvent(dashboard, false, nodeId)); + } }