Skip to content

Commit

Permalink
feat: add content support to dashboard widget (#6618)
Browse files Browse the repository at this point in the history
* feat: add content related api to dashboard widget

* refactor: make set get content separate from slotted elements

* test: update testbench content to check for all slots

* feat: add content related api to dashboard widget

* refactor: make set get content separate from slotted elements

* test: update testbench content to check for all slots

* chore: run formatter

* test: move tests into appropriate classes
  • Loading branch information
ugur-vaadin committed Oct 15, 2024
1 parent 929e9f7 commit f64e51e
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,36 +116,6 @@ public DashboardPage() {
.ifPresent(dashboard::remove));
removeFirstSection.setId("remove-first-section");

NativeButton addWidgetToFirstSection = new NativeButton(
"Add widget to first section");
addWidgetToFirstSection.addClickListener(
click -> getFirstSection(dashboard).ifPresent(section -> {
DashboardWidget newWidget = new DashboardWidget();
newWidget.setTitle("New widget");
section.add(newWidget);
}));
addWidgetToFirstSection.setId("add-widget-to-first-section");

NativeButton removeFirstWidgetFromFirstSection = new NativeButton(
"Remove first widget from first section");
removeFirstWidgetFromFirstSection.addClickListener(
click -> getFirstSection(dashboard).ifPresent(section -> {
List<DashboardWidget> currentWidgets = section.getWidgets();
if (currentWidgets.isEmpty()) {
return;
}
section.remove(currentWidgets.get(0));
}));
removeFirstWidgetFromFirstSection
.setId("remove-first-widget-from-first-section");

NativeButton removeAllFromFirstSection = new NativeButton(
"Remove all from first section");
removeAllFromFirstSection
.addClickListener(click -> getFirstSection(dashboard)
.ifPresent(DashboardSection::removeAll));
removeAllFromFirstSection.setId("remove-all-from-first-section");

NativeButton setMaximumColumnCount1 = new NativeButton(
"Set maximum column count 1");
setMaximumColumnCount1
Expand All @@ -158,24 +128,9 @@ public DashboardPage() {
click -> dashboard.setMaximumColumnCount(null));
setMaximumColumnCountNull.setId("set-maximum-column-count-null");

NativeButton increaseAllColspansBy1 = new NativeButton(
"Increase all colspans by 1");
increaseAllColspansBy1.addClickListener(click -> dashboard.getWidgets()
.forEach(widget -> widget.setColspan(widget.getColspan() + 1)));
increaseAllColspansBy1.setId("increase-all-colspans-by-1");

NativeButton decreaseAllColspansBy1 = new NativeButton(
"Decrease all colspans by 1");
decreaseAllColspansBy1.addClickListener(click -> dashboard.getWidgets()
.forEach(widget -> widget.setColspan(widget.getColspan() - 1)));
decreaseAllColspansBy1.setId("decrease-all-colspans-by-1");

add(addMultipleWidgets, removeFirstAndLastWidgets, removeAll,
addSectionWithMultipleWidgets, removeFirstSection,
addWidgetToFirstSection, removeFirstWidgetFromFirstSection,
removeAllFromFirstSection, setMaximumColumnCount1,
setMaximumColumnCountNull, increaseAllColspansBy1,
decreaseAllColspansBy1, dashboard);
setMaximumColumnCount1, setMaximumColumnCountNull, dashboard);
}

private static Optional<DashboardSection> getFirstSection(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright 2000-2024 Vaadin Ltd.
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full
* license.
*/
package com.vaadin.flow.component.dashboard.tests;

import java.util.List;
import java.util.Optional;

import com.vaadin.flow.component.dashboard.Dashboard;
import com.vaadin.flow.component.dashboard.DashboardSection;
import com.vaadin.flow.component.dashboard.DashboardWidget;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.router.Route;

/**
* @author Vaadin Ltd
*/
@Route("vaadin-dashboard-section")
public class DashboardSectionPage extends Div {

public DashboardSectionPage() {
Dashboard dashboard = new Dashboard();

DashboardWidget widget1InSection1 = new DashboardWidget();
widget1InSection1.setTitle("Widget 1 in Section 1");
widget1InSection1.setId("widget-1-in-section-1");

DashboardWidget widget2InSection1 = new DashboardWidget();
widget2InSection1.setTitle("Widget 2 in Section 1");
widget2InSection1.setId("widget-2-in-section-1");

DashboardWidget widget1InSection2 = new DashboardWidget();
widget1InSection2.setTitle("Widget 1 in Section 2");
widget1InSection2.setId("widget-1-in-section-2");

DashboardSection section1 = new DashboardSection("Section 1");
section1.add(widget1InSection1, widget2InSection1);
dashboard.addSection(section1);

DashboardSection section2 = dashboard.addSection("Section 2");
section2.add(widget1InSection2);

NativeButton addWidgetToFirstSection = new NativeButton(
"Add widget to first section");
addWidgetToFirstSection.addClickListener(
click -> getFirstSection(dashboard).ifPresent(section -> {
DashboardWidget newWidget = new DashboardWidget();
newWidget.setTitle("New widget");
section.add(newWidget);
}));
addWidgetToFirstSection.setId("add-widget-to-first-section");

NativeButton removeFirstWidgetFromFirstSection = new NativeButton(
"Remove first widget from first section");
removeFirstWidgetFromFirstSection.addClickListener(
click -> getFirstSection(dashboard).ifPresent(section -> {
List<DashboardWidget> currentWidgets = section.getWidgets();
if (currentWidgets.isEmpty()) {
return;
}
section.remove(currentWidgets.get(0));
}));
removeFirstWidgetFromFirstSection
.setId("remove-first-widget-from-first-section");

NativeButton removeAllFromFirstSection = new NativeButton(
"Remove all from first section");
removeAllFromFirstSection
.addClickListener(click -> getFirstSection(dashboard)
.ifPresent(DashboardSection::removeAll));
removeAllFromFirstSection.setId("remove-all-from-first-section");

add(addWidgetToFirstSection, removeFirstWidgetFromFirstSection,
removeAllFromFirstSection, dashboard);
}

private static Optional<DashboardSection> getFirstSection(
Dashboard dashboard) {
return dashboard.getChildren()
.filter(DashboardSection.class::isInstance)
.map(DashboardSection.class::cast).findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
*/
package com.vaadin.flow.component.dashboard.tests;

import java.util.List;

import com.vaadin.flow.component.dashboard.Dashboard;
import com.vaadin.flow.component.dashboard.DashboardWidget;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.router.Route;

/**
Expand All @@ -19,8 +24,54 @@
public class DashboardWidgetPage extends Div {

public DashboardWidgetPage() {
DashboardWidget widget = new DashboardWidget();
widget.setTitle("Widget");
add(widget);
Dashboard dashboard = new Dashboard();

DashboardWidget widget1 = new DashboardWidget();
widget1.setTitle("Widget 1");
widget1.setContent(new Div("Some content"));
widget1.setId("widget-1");

DashboardWidget widget2 = new DashboardWidget();
widget2.setTitle("Widget 2");
widget2.setId("widget-2");

dashboard.add(widget1, widget2);

NativeButton increaseAllColspansBy1 = new NativeButton(
"Increase all colspans by 1");
increaseAllColspansBy1.addClickListener(click -> dashboard.getWidgets()
.forEach(widget -> widget.setColspan(widget.getColspan() + 1)));
increaseAllColspansBy1.setId("increase-all-colspans-by-1");

NativeButton decreaseAllColspansBy1 = new NativeButton(
"Decrease all colspans by 1");
decreaseAllColspansBy1.addClickListener(click -> dashboard.getWidgets()
.forEach(widget -> widget.setColspan(widget.getColspan() - 1)));
decreaseAllColspansBy1.setId("decrease-all-colspans-by-1");

NativeButton updateContentOfTheFirstWidget = new NativeButton(
"Update content of the first widget");
updateContentOfTheFirstWidget.addClickListener(click -> {
List<DashboardWidget> widgets = dashboard.getWidgets();
if (!widgets.isEmpty()) {
widgets.get(0).setContent(new Span("Updated content"));
}
});
updateContentOfTheFirstWidget
.setId("update-content-of-the-first-widget");

NativeButton removeContentOfTheFirstWidget = new NativeButton(
"Remove content of the first widget");
removeContentOfTheFirstWidget.addClickListener(click -> {
List<DashboardWidget> widgets = dashboard.getWidgets();
if (!widgets.isEmpty()) {
widgets.get(0).setContent(null);
}
});
removeContentOfTheFirstWidget
.setId("remove-content-of-the-first-widget");

add(updateContentOfTheFirstWidget, removeContentOfTheFirstWidget,
increaseAllColspansBy1, decreaseAllColspansBy1, dashboard);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,6 @@ public void removeFirstSection_sectionIsRemoved() {
assertSectionWidgetsByTitle(firstSection, "Widget 1 in Section 2");
}

@Test
public void addWidgetToFirstSection_widgetsAreAdded() {
clickElementWithJs("add-widget-to-first-section");
List<DashboardSectionElement> sections = dashboardElement.getSections();
DashboardSectionElement firstSection = sections.get(0);
Assert.assertEquals("Section 1", firstSection.getTitle());
assertSectionWidgetsByTitle(firstSection, "Widget 1 in Section 1",
"Widget 2 in Section 1", "New widget");
}

@Test
public void removeFirstWidgetFromFirstSection_widgetIsRemoved() {
clickElementWithJs("remove-first-widget-from-first-section");
List<DashboardSectionElement> sections = dashboardElement.getSections();
DashboardSectionElement firstSection = sections.get(0);
Assert.assertEquals("Section 1", firstSection.getTitle());
assertSectionWidgetsByTitle(firstSection, "Widget 2 in Section 1");
}

@Test
public void removeAllFromFirstSection_widgetsAreRemoved() {
clickElementWithJs("remove-all-from-first-section");
List<DashboardSectionElement> sections = dashboardElement.getSections();
DashboardSectionElement firstSection = sections.get(0);
Assert.assertEquals("Section 1", firstSection.getTitle());
assertSectionWidgetsByTitle(firstSection);
}

@Test
public void changeMaximumColumnCountTo1_widgetsShouldBeOnTheSameColumn() {
List<DashboardWidgetElement> widgets = dashboardElement.getWidgets();
Expand Down Expand Up @@ -155,24 +127,6 @@ public void changeMaximumColumnCountToNull_widgetsShouldBeOnTheSameRow() {
Assert.assertEquals(yOfWidget1, widgets.get(1).getLocation().getY());
}

@Test
public void defaultWidgetColspanIsCorrect() {
List<DashboardWidgetElement> widgets = dashboardElement.getWidgets();
widgets.forEach(widget -> Assert.assertEquals(Integer.valueOf(1),
widget.getColspan()));
}

@Test
public void updateColspans_colspansForAllWidgetsUpdated() {
clickElementWithJs("increase-all-colspans-by-1");
List<DashboardWidgetElement> widgets = dashboardElement.getWidgets();
widgets.forEach(widget -> Assert.assertEquals(Integer.valueOf(2),
widget.getColspan()));
clickElementWithJs("decrease-all-colspans-by-1");
widgets.forEach(widget -> Assert.assertEquals(Integer.valueOf(1),
widget.getColspan()));
}

private void assertDashboardWidgetsByTitle(String... expectedWidgetTitles) {
assertWidgetsByTitle(dashboardElement.getWidgets(),
expectedWidgetTitles);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright 2000-2024 Vaadin Ltd.
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full
* license.
*/
package com.vaadin.flow.component.dashboard.tests;

import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.component.dashboard.testbench.DashboardElement;
import com.vaadin.flow.component.dashboard.testbench.DashboardSectionElement;
import com.vaadin.flow.component.dashboard.testbench.DashboardWidgetElement;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.tests.AbstractComponentIT;

/**
* @author Vaadin Ltd
*/
@TestPath("vaadin-dashboard-section")
public class DashboardSectionIT extends AbstractComponentIT {

private DashboardElement dashboardElement;

@Before
public void init() {
open();
dashboardElement = $(DashboardElement.class).waitForFirst();
}

@Test
public void addWidgetToFirstSection_widgetsAreAdded() {
clickElementWithJs("add-widget-to-first-section");
List<DashboardSectionElement> sections = dashboardElement.getSections();
DashboardSectionElement firstSection = sections.get(0);
Assert.assertEquals("Section 1", firstSection.getTitle());
assertSectionWidgetsByTitle(firstSection, "Widget 1 in Section 1",
"Widget 2 in Section 1", "New widget");
}

@Test
public void removeFirstWidgetFromFirstSection_widgetIsRemoved() {
clickElementWithJs("remove-first-widget-from-first-section");
List<DashboardSectionElement> sections = dashboardElement.getSections();
DashboardSectionElement firstSection = sections.get(0);
Assert.assertEquals("Section 1", firstSection.getTitle());
assertSectionWidgetsByTitle(firstSection, "Widget 2 in Section 1");
}

@Test
public void removeAllFromFirstSection_widgetsAreRemoved() {
clickElementWithJs("remove-all-from-first-section");
List<DashboardSectionElement> sections = dashboardElement.getSections();
DashboardSectionElement firstSection = sections.get(0);
Assert.assertEquals("Section 1", firstSection.getTitle());
assertSectionWidgetsByTitle(firstSection);
}

private static void assertSectionWidgetsByTitle(
DashboardSectionElement section, String... expectedWidgetTitles) {
assertWidgetsByTitle(section.getWidgets(), expectedWidgetTitles);
}

private static void assertWidgetsByTitle(
List<DashboardWidgetElement> actualWidgets,
String... expectedWidgetTitles) {
List<String> widgetTitles = actualWidgets.stream()
.map(DashboardWidgetElement::getTitle).toList();
Assert.assertEquals(Arrays.asList(expectedWidgetTitles), widgetTitles);
}
}
Loading

0 comments on commit f64e51e

Please sign in to comment.