if (self.importScripts) {
    importScripts('/resources/testharness.js');
    importScripts('worker-helpers.js');
}

async_test(function(test) {
    if (Notification.permission != 'granted') {
        assert_unreached('No permission has been granted for displaying notifications.');
        return;
    }

    var notification = new Notification('My Notification', { tag: 'notification-test' });
    notification.addEventListener('show', function() {
        var updatedNotification = new Notification('Second Notification', { tag: 'notification-test' });
        updatedNotification.addEventListener('show', function() {
            test.done();
        });
    });

    // FIXME: Replacing a notification should fire the close event on the
    // notification that's being replaced.

    notification.addEventListener('error', function() {
        assert_unreached('The error event should not be thrown.');
    });

}, 'Replacing a notification will discard the previous notification.');

if (isDedicatedOrSharedWorker())
    done();

if (self.importScripts) {
    importScripts('/resources/testharness.js');
    importScripts('worker-helpers.js');
}

async_test(function(test) {
    if (Notification.permission != 'granted') {
        assert_unreached('No permission has been granted for displaying notifications.');
        return;
    }

    // We require two asynchronous events to happen when a notification gets updated, (1)
    // the old instance should receive the "close" event, and (2) the new notification
    // should receive the "show" event, but only after (1) has happened.
    var closedOriginalNotification = false;

    var notification = new Notification('My Notification', { tag: 'notification-test' });
    notification.addEventListener('show', function() {
        var updatedNotification = new Notification('Second Notification', { tag: 'notification-test' });
        updatedNotification.addEventListener('show', function() {
            assert_true(closedOriginalNotification);
            test.done();
        });
    });

    notification.addEventListener('close', function() {
        closedOriginalNotification = true;
    });

    notification.addEventListener('error', function() {
        assert_unreached('The error event should not be thrown.');
    });

}, 'Replacing a notification will discard the previous notification.');

if (isDedicatedOrSharedWorker())
    done();

Verify in a layout test that replacing a notification closes the old one.
Verify in a layout test that replacing a notification closes the old one.

We do this by listening to the "close" event on the old notification as
well. This test passes with or without the Chrome-sided fix because it's
properly implemented in the LayoutTestNotificationManager, but is a good
way of verifying the fix manually.

BUG=366098

Review URL: https://codereview.chromium.org/750563005

git-svn-id: bf5cd6ccde378db821296732a091cfbcf5285fbd@186033 bbb929c8-8fbe-4397-9dbb-9b2b20218538
