diff options
Diffstat (limited to 'dom/quota/test/marionette/quota_test_case.py')
-rw-r--r-- | dom/quota/test/marionette/quota_test_case.py | 180 |
1 files changed, 136 insertions, 44 deletions
diff --git a/dom/quota/test/marionette/quota_test_case.py b/dom/quota/test/marionette/quota_test_case.py index c31edcaaf7..7d161cf4ff 100644 --- a/dom/quota/test/marionette/quota_test_case.py +++ b/dom/quota/test/marionette/quota_test_case.py @@ -10,6 +10,51 @@ from marionette_harness import MarionetteTestCase class QuotaTestCase(MarionetteTestCase): + def executeAsyncScript(self, script, script_args): + res = self.marionette.execute_async_script( + """ + const resolve = arguments[arguments.length - 1]; + + class RequestError extends Error { + constructor(resultCode, resultName) { + super(`Request failed (code: ${resultCode}, name: ${resultName})`); + this.name = "RequestError"; + this.resultCode = resultCode; + this.resultName = resultName; + } + } + + async function requestFinished(request) { + await new Promise(function (resolve) { + request.callback = function () { + resolve(); + }; + }); + + if (request.resultCode !== Cr.NS_OK) { + throw new RequestError(request.resultCode, request.resultName); + } + + return request.result; + } + """ + + script + + """ + main() + .then(function(result) { + resolve(result); + }) + .catch(function() { + resolve(null); + });; + """, + script_args=script_args, + new_sandbox=False, + ) + + assert res is not None + return res + def ensureInvariantHolds(self, op): maxWaitTime = 60 Wait(self.marionette, timeout=maxWaitTime).until( @@ -24,27 +69,23 @@ class QuotaTestCase(MarionetteTestCase): return None def getFullOriginMetadata(self, persistenceType, origin): - with self.marionette.using_context("chrome"): - res = self.marionette.execute_async_script( + with self.marionette.using_context(self.marionette.CONTEXT_CHROME): + res = self.executeAsyncScript( """ - const [persistenceType, origin, resolve] = arguments; + const [persistenceType, origin] = arguments; - const principal = Services.scriptSecurityManager. - createContentPrincipalFromOrigin(origin); - - const request = Services.qms.getFullOriginMetadata( - persistenceType, principal); - - request.callback = function() { - if (request.resultCode != Cr.NS_OK) { - resolve(null); - } else { - resolve(request.result); - } - } + async function main() { + const principal = Services.scriptSecurityManager. + createContentPrincipalFromOrigin(origin); + + const request = Services.qms.getFullOriginMetadata( + persistenceType, principal); + const metadata = await requestFinished(request); + + return metadata; + } """, script_args=(persistenceType, origin), - new_sandbox=False, ) assert res is not None @@ -57,41 +98,91 @@ class QuotaTestCase(MarionetteTestCase): sanitizedStorageOrigin = storageOrigin.replace(":", "+").replace("/", "+") return os.path.join( - profilePath, "storage", persistenceType, sanitizedStorageOrigin, client + self.getRepositoryPath(persistenceType), sanitizedStorageOrigin, client ) + def getRepositoryPath(self, persistenceType): + profilePath = self.marionette.instance.profile.profile + assert profilePath is not None + + return os.path.join(profilePath, "storage", persistenceType) + + def initStorage(self): + with self.marionette.using_context(self.marionette.CONTEXT_CHROME): + return self.executeAsyncScript( + """ + async function main() { + let req = Services.qms.init(); + await requestFinished(req); + + return true; + } + """, + script_args=(), + ) + + def initTemporaryOrigin(self, persistenceType, origin): + with self.marionette.using_context(self.marionette.CONTEXT_CHROME): + return self.executeAsyncScript( + """ + const [persistenceType, origin] = arguments; + async function main() { + const principal = Services.scriptSecurityManager. + createContentPrincipalFromOrigin(origin); + + let req = Services.qms.initializeTemporaryOrigin(persistenceType, principal); + await requestFinished(req) + + return true; + } + """, + script_args=( + persistenceType, + origin, + ), + ) + + def initTemporaryStorage(self): + with self.marionette.using_context(self.marionette.CONTEXT_CHROME): + return self.executeAsyncScript( + """ + async function main() { + const req = Services.qms.initTemporaryStorage(); + await requestFinished(req); + + return true; + } + """, + script_args=(), + ) + def resetStoragesForPrincipal(self, origin, persistenceType, client): # This method is used to force sqlite to write journal file contents to # main sqlite database file - script = """ - const [resolve] = arguments - - let origin = '%s'; - let persistenceType = '%s'; - let client = '%s'; - let principal = Services.scriptSecurityManager. - createContentPrincipalFromOrigin(origin); - - let req = Services.qms.resetStoragesForPrincipal(principal, persistenceType, client); - req.callback = () => { - if (req.resultCode == Cr.NS_OK) { - resolve(true); - } else { - resolve(false); + with self.marionette.using_context(self.marionette.CONTEXT_CHROME): + res = self.executeAsyncScript( + """ + const [origin, persistenceType, client] = arguments; + + async function main() { + const principal = Services.scriptSecurityManager. + createContentPrincipalFromOrigin(origin); + + const request = Services.qms.resetStoragesForPrincipal(principal, persistenceType, client); + await requestFinished(request); + + return true; } - } - """ % ( - origin, - persistenceType, - client, - ) + """, + script_args=(origin, persistenceType, client), + ) - with self.marionette.using_context(self.marionette.CONTEXT_CHROME): - return self.marionette.execute_async_script(script) + assert res is not None + return res @contextmanager - def using_new_window(self, path, private=False): + def using_new_window(self, path, private=False, skipCleanup=False): """ This helper method is created to ensure that a temporary window required inside the test scope is lifetime'd properly @@ -109,5 +200,6 @@ class QuotaTestCase(MarionetteTestCase): yield (origin, "private" if private else "default") finally: - self.marionette.close() - self.marionette.switch_to_window(oldWindow) + if not skipCleanup: + self.marionette.close() + self.marionette.switch_to_window(oldWindow) |