aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2018-01-10 10:30:57 +0200
committerTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2018-01-15 11:43:19 +0200
commit9e54760b68b7682566a41827b3e2ef235da895ab (patch)
treec8fc60189e8e0781587a67d7ccd77982b8f5e0fd
parent531320c5051b7f72c9d96c7826b5cd4f9dcaae67 (diff)
downloadvaadin-framework-9e54760b68b7682566a41827b3e2ef235da895ab.tar.gz
vaadin-framework-9e54760b68b7682566a41827b3e2ef235da895ab.zip
Backport fix for removing Notifications on close (#10504)
-rw-r--r--client/src/main/java/com/vaadin/client/ui/notification/NotificationConnector.java32
-rw-r--r--server/src/main/java/com/vaadin/ui/Notification.java26
-rw-r--r--uitest/src/main/java/com/vaadin/tests/elements/notification/NotificationGetTypeAndDescription.java18
-rw-r--r--uitest/src/test/java/com/vaadin/tests/elements/notification/NotificationCloseEventTest.java46
4 files changed, 106 insertions, 16 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/notification/NotificationConnector.java b/client/src/main/java/com/vaadin/client/ui/notification/NotificationConnector.java
index 038d70dbf3..a0c49af8a3 100644
--- a/client/src/main/java/com/vaadin/client/ui/notification/NotificationConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/notification/NotificationConnector.java
@@ -36,6 +36,8 @@ import com.vaadin.ui.Notification;
@Connect(value = Notification.class)
public class NotificationConnector extends AbstractExtensionConnector {
+ private VNotification notification;
+
@Override
public NotificationState getState() {
return (NotificationState) super.getState();
@@ -44,21 +46,33 @@ public class NotificationConnector extends AbstractExtensionConnector {
@Override
protected void extend(ServerConnector target) {
NotificationState state = getState();
- VNotification n = VNotification.showNotification(
- target.getConnection(),
- state.caption, state.description,
- state.htmlContentAllowed, getResourceUrl("icon"),
- state.styleName, state.position, state.delay);
+ notification = VNotification.showNotification(target.getConnection(),
+ state.caption, state.description, state.htmlContentAllowed,
+ getResourceUrl("icon"), state.styleName, state.position,
+ state.delay);
- n.addCloseHandler(new CloseHandler<PopupPanel>() {
+ notification.addCloseHandler(new CloseHandler<PopupPanel>() {
@Override
public void onClose(CloseEvent<PopupPanel> event) {
- NotificationServerRpc rpc =
- getRpcProxy(NotificationServerRpc.class);
- rpc.closed();
+ if (getParent() == null) {
+ // Unregistered already
+ return;
+ }
+ NotificationServerRpc rpc = getRpcProxy(
+ NotificationServerRpc.class);
+ rpc.closed();
+ notification = null;
}
});
}
+ @Override
+ public void onUnregister() {
+ super.onUnregister();
+ if (notification != null) {
+ notification.hide();
+ notification = null;
+ }
+ }
}
diff --git a/server/src/main/java/com/vaadin/ui/Notification.java b/server/src/main/java/com/vaadin/ui/Notification.java
index 24327a8cc4..b7735edd4a 100644
--- a/server/src/main/java/com/vaadin/ui/Notification.java
+++ b/server/src/main/java/com/vaadin/ui/Notification.java
@@ -20,6 +20,7 @@ import java.io.Serializable;
import java.lang.reflect.Method;
import com.vaadin.event.ConnectorEvent;
+import com.vaadin.event.HasUserOriginated;
import com.vaadin.server.AbstractExtension;
import com.vaadin.server.Page;
import com.vaadin.server.Resource;
@@ -125,8 +126,9 @@ public class Notification extends AbstractExtension {
*
* @since 8.2
*/
- private NotificationServerRpc rpc = () -> fireEvent(
- new CloseEvent(Notification.this));
+ private NotificationServerRpc rpc = () -> {
+ close();
+ };
/**
* Creates a "humanized" notification message.
@@ -391,6 +393,25 @@ public class Notification extends AbstractExtension {
extend(page.getUI());
}
+ /**
+ * Closes (hides) the notification.
+ * <p>
+ * If the notification is not shown, does nothing.
+ *
+ * @param userOriginated
+ * <code>true</code> if the notification was closed because the
+ * user clicked on it, <code>false</code> if the notification was
+ * closed from the server
+ */
+ private void close() {
+ if (!isAttached()) {
+ return;
+ }
+
+ remove();
+ fireEvent(new CloseEvent(this));
+ }
+
@Override
protected NotificationState getState() {
return (NotificationState) super.getState();
@@ -499,7 +520,6 @@ public class Notification extends AbstractExtension {
* @since 8.2
*/
public static class CloseEvent extends ConnectorEvent {
-
/**
* @param source
*/
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..6a70679e04 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,
@@ -55,8 +62,11 @@ 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");
+ });
}
}
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..3c462aa6d4
--- /dev/null
+++ b/uitest/src/test/java/com/vaadin/tests/elements/notification/NotificationCloseEventTest.java
@@ -0,0 +1,46 @@
+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", getLogRow(0));
+ }
+
+ @Test
+ public void notificationsStayAwayAfterRefresh() {
+ openTestURL();
+ Assert.assertFalse(isElementPresent(NotificationElement.class));
+ ButtonElement warning = $(ButtonElement.class).caption("warning")
+ .first();
+ warning.click();
+ Assert.assertTrue("Notification should open",
+ isElementPresent(NotificationElement.class));
+ openTestURL();
+ Assert.assertTrue("Notification should still be present.",
+ isElementPresent(NotificationElement.class));
+ $(NotificationElement.class).first().close();
+ Assert.assertEquals("1. Notification (warning) closed", getLogRow(0));
+ Assert.assertFalse("No notification should be present",
+ isElementPresent(NotificationElement.class));
+ openTestURL();
+ Assert.assertFalse("Reloading should not open notifications",
+ isElementPresent(NotificationElement.class));
+ }
+}