service_worker_utils.py (2527B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import os 6 7 from marionette_driver import Wait 8 from marionette_harness import MarionetteTestCase 9 10 11 class MarionetteServiceWorkerTestCase(MarionetteTestCase): 12 def install_service_worker(self, path): 13 install_url = self.marionette.absolute_url(path) 14 self.marionette.navigate(install_url) 15 Wait(self.marionette).until( 16 lambda _: self.is_service_worker_registered, 17 message="Service worker not successfully installed", 18 ) 19 20 # Wait for the registered service worker to be stored in the Firefox 21 # profile before restarting the instance to prevent intermittent 22 # failures (Bug 1665184). 23 Wait(self.marionette, timeout=10).until( 24 lambda _: self.profile_serviceworker_txt_exists, 25 message="Service worker not stored in profile", 26 ) 27 28 # self.marionette.restart(in_app=True) will restore service workers if 29 # we don't navigate away before restarting. 30 self.marionette.navigate("about:blank") 31 32 # Using @property helps avoid the case where missing parens at the call site 33 # yields an unvarying 'true' value. 34 @property 35 def profile_serviceworker_txt_exists(self): 36 return "serviceworker.txt" in os.listdir(self.marionette.profile_path) 37 38 @property 39 def is_service_worker_registered(self): 40 with self.marionette.using_context("chrome"): 41 return self.marionette.execute_script( 42 """ 43 let swm = Cc["@mozilla.org/serviceworkers/manager;1"].getService( 44 Ci.nsIServiceWorkerManager 45 ); 46 let ssm = Services.scriptSecurityManager; 47 48 let principal = ssm.createContentPrincipalFromOrigin(arguments[0]); 49 50 let serviceWorkers = swm.getAllRegistrations(); 51 for (let i = 0; i < serviceWorkers.length; i++) { 52 let sw = serviceWorkers.queryElementAt( 53 i, 54 Ci.nsIServiceWorkerRegistrationInfo 55 ); 56 if (sw.principal.origin == principal.origin) { 57 return true; 58 } 59 } 60 return false; 61 """, 62 script_args=(self.marionette.absolute_url(""),), 63 )