summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cleardata/tests/marionette/test_service_worker_at_shutdown.py
blob: 9738c0ea1761fa4290b16c950ec393b9fbf4667c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from marionette_driver import Wait
from marionette_harness import MarionetteTestCase


class ServiceWorkerAtShutdownTestCase(MarionetteTestCase):
    def setUp(self):
        super(ServiceWorkerAtShutdownTestCase, self).setUp()
        self.install_service_worker()
        self.set_pref_to_delete_site_data_on_shutdown()

    def tearDown(self):
        self.marionette.restart(in_app=False, clean=True)
        super(ServiceWorkerAtShutdownTestCase, self).tearDown()

    def install_service_worker(self):
        install_url = self.marionette.absolute_url(
            "serviceworker/install_serviceworker.html"
        )
        self.marionette.navigate(install_url)
        # Make sure 'install_url' is not loaded on startup, it would reinstall the service worker
        dummy_url = self.marionette.absolute_url("dummy.html")
        self.marionette.navigate(dummy_url)
        Wait(self.marionette).until(lambda _: self.is_service_worker_registered)

    def set_pref_to_delete_site_data_on_shutdown(self):
        self.marionette.set_pref("privacy.sanitize.sanitizeOnShutdown", True)
        self.marionette.set_pref("privacy.clearOnShutdown.offlineApps", True)

    def test_unregistering_service_worker_when_clearing_data(self):
        self.marionette.restart(clean=False, in_app=True)
        self.assertFalse(self.is_service_worker_registered)

    @property
    def is_service_worker_registered(self):
        with self.marionette.using_context("chrome"):
            return self.marionette.execute_script(
                """
                let serviceWorkerManager = Cc["@mozilla.org/serviceworkers/manager;1"].getService(
                    Ci.nsIServiceWorkerManager
                );

                let principal =
                    Services.scriptSecurityManager.createContentPrincipalFromOrigin(arguments[0]);

                let serviceWorkers = serviceWorkerManager.getAllRegistrations();
                for (let i = 0; i < serviceWorkers.length; i++) {
                    let sw = serviceWorkers.queryElementAt(
                        i,
                        Ci.nsIServiceWorkerRegistrationInfo
                    );
                    if (sw.principal.origin == principal.origin) {
                        return true;
                    }
                }
                return false;
            """,
                script_args=(self.marionette.absolute_url(""),),
            )