From 59a2dafdcb1333b278484bdd65b3ca3cc7bb87f8 Mon Sep 17 00:00:00 2001 From: Artur Date: Tue, 9 Jan 2018 13:11:02 +0200 Subject: Add Notification.close() to hide a notification from the server (#10483) Also fixes the problem that notifications were never removed on the server side Fixes #2114, fixes #10481 --- .../NotificationGetTypeAndDescription.java | 32 +++++++++++-- .../notification/NotificationCloseEventTest.java | 52 ++++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 uitest/src/test/java/com/vaadin/tests/elements/notification/NotificationCloseEventTest.java (limited to 'uitest') diff --git a/uitest/src/main/java/com/vaadin/tests/elements/notification/NotificationGetTypeAndDescription.java b/uitest/src/main/java/com/vaadin/tests/elements/notification/NotificationGetTypeAndDescription.java index 6cd99cb53d..4ac410885c 100644 --- a/uitest/src/main/java/com/vaadin/tests/elements/notification/NotificationGetTypeAndDescription.java +++ b/uitest/src/main/java/com/vaadin/tests/elements/notification/NotificationGetTypeAndDescription.java @@ -1,14 +1,21 @@ package com.vaadin.tests.elements.notification; +import java.util.ArrayList; +import java.util.List; + +import com.vaadin.annotations.PreserveOnRefresh; +import com.vaadin.annotations.Widgetset; import com.vaadin.server.VaadinRequest; -import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.tests.components.AbstractTestUIWithLog; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.Notification; import com.vaadin.ui.Notification.Type; -public class NotificationGetTypeAndDescription extends AbstractTestUI { +@Widgetset("com.vaadin.DefaultWidgetSet") +@PreserveOnRefresh +public class NotificationGetTypeAndDescription extends AbstractTestUIWithLog { private static final Type[] types = { Type.WARNING_MESSAGE, Type.ERROR_MESSAGE, Type.HUMANIZED_MESSAGE, @@ -34,6 +41,19 @@ public class NotificationGetTypeAndDescription extends AbstractTestUI { btn.setId("showid"); btn.addClickListener(event -> Notification.show("test")); addComponent(btn); + + Button hide = new Button("Hide all notifications"); + hide.setId("hide"); + hide.addClickListener(event -> { + List notifications = new ArrayList<>(); + getAllChildrenIterable(getUI()).forEach(conn -> { + if (conn instanceof Notification) { + notifications.add((Notification) conn); + } + }); + notifications.forEach(Notification::close); + }); + addComponent(hide); } @Override @@ -55,8 +75,12 @@ public class NotificationGetTypeAndDescription extends AbstractTestUI { @Override public void buttonClick(ClickEvent event) { - Notification.show(captions[index], descriptions[index], - types[index]); + Notification n = Notification.show(captions[index], + descriptions[index], types[index]); + n.addCloseListener(e -> { + log("Notification (" + descriptions[index] + ") closed " + + (e.isUserOriginated() ? "by user" : "from server")); + }); } } diff --git a/uitest/src/test/java/com/vaadin/tests/elements/notification/NotificationCloseEventTest.java b/uitest/src/test/java/com/vaadin/tests/elements/notification/NotificationCloseEventTest.java new file mode 100644 index 0000000000..e8db79d95c --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/elements/notification/NotificationCloseEventTest.java @@ -0,0 +1,52 @@ +package com.vaadin.tests.elements.notification; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.NotificationElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class NotificationCloseEventTest extends SingleBrowserTest { + + @Override + protected Class getUIClass() { + return NotificationGetTypeAndDescription.class; + } + + @Test + public void testCloseByUser() { + openTestURL(); + ButtonElement error = $(ButtonElement.class).caption("error").first(); + error.click(); + $(NotificationElement.class).get(0).close(); + Assert.assertEquals("1. Notification (error) closed by user", + getLogRow(0)); + } + + @Test + public void testCloseByServer() { + openTestURL(); + ButtonElement warning = $(ButtonElement.class).caption("warning") + .first(); + warning.click(); + ButtonElement close = $(ButtonElement.class) + .caption("Hide all notifications").first(); + close.click(); + + Assert.assertEquals("1. Notification (warning) closed from server", + getLogRow(0)); + } + + @Test + public void notificationsStayAwayAfterRefresh() { + openTestURL(); + ButtonElement warning = $(ButtonElement.class).caption("warning") + .first(); + warning.click(); + $(NotificationElement.class).first().close(); + openTestURL(); + + Assert.assertEquals(0, $(NotificationElement.class).all().size()); + } +} -- cgit v1.2.3