diff options
author | Artur <artur@vaadin.com> | 2018-01-09 13:11:02 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2018-01-09 13:11:02 +0200 |
commit | 59a2dafdcb1333b278484bdd65b3ca3cc7bb87f8 (patch) | |
tree | 9222a32b504433b787bafca805637d21e5f3bddf /uitest | |
parent | 531320c5051b7f72c9d96c7826b5cd4f9dcaae67 (diff) | |
download | vaadin-framework-59a2dafdcb1333b278484bdd65b3ca3cc7bb87f8.tar.gz vaadin-framework-59a2dafdcb1333b278484bdd65b3ca3cc7bb87f8.zip |
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
Diffstat (limited to 'uitest')
2 files changed, 80 insertions, 4 deletions
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<Notification> 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()); + } +} |