@@ -185,10 +185,7 @@ public void setText_notificationHasAddedComponents_innerHtmlIsTextValue() {
185
185
186
186
notification .open ();
187
187
188
- UIInternals internals = ui .getInternals ();
189
- VaadinSession session = Mockito .mock (VaadinSession .class );
190
- internals .setSession (session );
191
- internals .getStateTree ().runExecutionsBeforeClientResponse ();
188
+ flushBeforeClientResponse ();
192
189
193
190
Element templateElement = notification .getElement ().getChildren ()
194
191
.findFirst ().get ();
@@ -206,10 +203,7 @@ public void add_notificationHasText_innerHtmlIsTemplateValue() {
206
203
207
204
notification .open ();
208
205
209
- UIInternals internals = ui .getInternals ();
210
- VaadinSession session = Mockito .mock (VaadinSession .class );
211
- internals .setSession (session );
212
- internals .getStateTree ().runExecutionsBeforeClientResponse ();
206
+ flushBeforeClientResponse ();
213
207
214
208
Element templateElement = notification .getElement ().getChildren ()
215
209
.findFirst ().get ();
@@ -241,4 +235,87 @@ public void hasStyle() {
241
235
Notification notification = new Notification ();
242
236
Assert .assertTrue (notification instanceof HasStyle );
243
237
}
238
+
239
+ @ Test
240
+ public void createNotificationh_closeOnParentDetach () {
241
+ // Create a Notification manually and add it to a parent container
242
+ Notification notification = new Notification ();
243
+ notification .open ();
244
+ Div parent = new Div (notification );
245
+ // Add the parent to the UI
246
+ ui .add (parent );
247
+
248
+ // The notification was opened manually. Check that it's still open.
249
+ Assert .assertTrue (notification .isOpened ());
250
+
251
+ // Remove the parent container from the UI
252
+ ui .remove (parent );
253
+
254
+ // The notification should have been closed on detach, even if it was
255
+ // the parent that was removed
256
+ Assert .assertFalse (notification .isOpened ());
257
+ // The parent reference should not have changed
258
+ Assert .assertEquals (notification .getParent ().get (), parent );
259
+ }
260
+
261
+ @ Test
262
+ public void showNotification_closeAndDetachOnParentDetach () {
263
+ // Create a modal parent container and add it to the UI
264
+ Div parent = new Div ();
265
+ ui .add (parent );
266
+ ui .setChildComponentModal (parent , true );
267
+
268
+ // Use Notification.show() helper to create a notification.
269
+ // It will be automatically added to the modal parent container (before
270
+ // client response)
271
+ Notification notification = Notification .show ("foo" );
272
+ flushBeforeClientResponse ();
273
+
274
+ // Check that the notification is opened and attached to the parent
275
+ // container
276
+ Assert .assertTrue (notification .isOpened ());
277
+ Assert .assertTrue (parent .getChildren ().collect (Collectors .toList ())
278
+ .contains (notification ));
279
+
280
+ // Remove the modal parent container from the UI
281
+ ui .remove (parent );
282
+
283
+ // The notification should have been closed on detach, even if it was
284
+ // the parent that was removed
285
+ Assert .assertFalse (notification .isOpened ());
286
+ // The notification should have been automatically removed from the
287
+ // parent container
288
+ Assert .assertFalse (parent .getChildren ().collect (Collectors .toList ())
289
+ .contains (notification ));
290
+ }
291
+
292
+ @ Test
293
+ public void showNotification_addManually_dontDetachOnParentDetach () {
294
+ // Use Notification.show() helper to create a notification.
295
+ Notification notification = Notification .show ("foo" );
296
+
297
+ // Manually add the notification to a parent
298
+ Div parent = new Div (notification );
299
+ ui .add (parent );
300
+ // Flush
301
+ flushBeforeClientResponse ();
302
+
303
+ // Check that the notification is attached to the parent container
304
+ Assert .assertEquals (notification .getParent ().get (), parent );
305
+
306
+ // Remove the modal parent container from the UI
307
+ ui .remove (parent );
308
+
309
+ // Even though the notification was created using Notification.show(),
310
+ // it got was manually added to the parent container so it should not
311
+ // have been automatically removed from it.
312
+ Assert .assertEquals (notification .getParent ().get (), parent );
313
+ }
314
+
315
+ private void flushBeforeClientResponse () {
316
+ UIInternals internals = ui .getInternals ();
317
+ VaadinSession session = Mockito .mock (VaadinSession .class );
318
+ internals .setSession (session );
319
+ internals .getStateTree ().runExecutionsBeforeClientResponse ();
320
+ }
244
321
}
0 commit comments