michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: MARIONETTE_CONTEXT = "chrome"; michael@0: michael@0: let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise; michael@0: michael@0: /** michael@0: * Wrap DOMRequest onsuccess/onerror events to Promise resolve/reject. michael@0: * michael@0: * Fulfill params: A DOMEvent. michael@0: * Reject params: A DOMEvent. michael@0: * michael@0: * @param aRequest michael@0: * A DOMRequest instance. michael@0: * michael@0: * @return A deferred promise. michael@0: */ michael@0: function wrapDomRequestAsPromise(aRequest) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: ok(aRequest instanceof DOMRequest, michael@0: "aRequest is instanceof " + aRequest.constructor); michael@0: michael@0: aRequest.addEventListener("success", function(aEvent) { michael@0: deferred.resolve(aEvent); michael@0: }); michael@0: aRequest.addEventListener("error", function(aEvent) { michael@0: deferred.reject(aEvent); michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: /** michael@0: * Get mozSettings value specified by @aKey. michael@0: * michael@0: * Resolve if that mozSettings value is retrieved successfully, reject michael@0: * otherwise. michael@0: * michael@0: * Fulfill params: The corresponding mozSettings value of the key. michael@0: * Reject params: (none) michael@0: * michael@0: * @param aKey michael@0: * A string. michael@0: * @param aAllowError [optional] michael@0: * A boolean value. If set to true, an error response won't be treated michael@0: * as test failure. Default: false. michael@0: * michael@0: * @return A deferred promise. michael@0: */ michael@0: function getSettings(aKey, aAllowError) { michael@0: let request = window.navigator.mozSettings.createLock().get(aKey); michael@0: return wrapDomRequestAsPromise(request) michael@0: .then(function resolve(aEvent) { michael@0: log("getSettings(" + aKey + ") - success"); michael@0: return aEvent.target.result[aKey]; michael@0: }, function reject(aEvent) { michael@0: ok(aAllowError, "getSettings(" + aKey + ") - error"); michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Set mozSettings values. michael@0: * michael@0: * Resolve if that mozSettings value is set successfully, reject otherwise. michael@0: * michael@0: * Fulfill params: (none) michael@0: * Reject params: (none) michael@0: * michael@0: * @param aKey michael@0: * A string key. michael@0: * @param aValue michael@0: * An object value. michael@0: * @param aAllowError [optional] michael@0: * A boolean value. If set to true, an error response won't be treated michael@0: * as test failure. Default: false. michael@0: * michael@0: * @return A deferred promise. michael@0: */ michael@0: function setSettings(aKey, aValue, aAllowError) { michael@0: let settings = {}; michael@0: settings[aKey] = aValue; michael@0: let request = window.navigator.mozSettings.createLock().set(settings); michael@0: return wrapDomRequestAsPromise(request) michael@0: .then(function resolve() { michael@0: log("setSettings(" + JSON.stringify(settings) + ") - success"); michael@0: }, function reject() { michael@0: ok(aAllowError, "setSettings(" + JSON.stringify(settings) + ") - error"); michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Wait for observer event. michael@0: * michael@0: * Resolve if that topic event occurs. Never reject. michael@0: * michael@0: * Fulfill params: the subject passed. michael@0: * michael@0: * @param aTopic michael@0: * A string topic name. michael@0: * michael@0: * @return A deferred promise. michael@0: */ michael@0: function waitForObserverEvent(aTopic) { michael@0: let obs = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService); michael@0: let deferred = Promise.defer(); michael@0: michael@0: obs.addObserver(function observer(subject, topic, data) { michael@0: if (topic === aTopic) { michael@0: obs.removeObserver(observer, aTopic); michael@0: deferred.resolve(subject); michael@0: } michael@0: }, aTopic, false); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: /** michael@0: * Basic test routine helper. michael@0: * michael@0: * This helper does nothing but clean-ups. michael@0: * michael@0: * @param aTestCaseMain michael@0: * A function that takes no parameter. michael@0: */ michael@0: function startTestBase(aTestCaseMain) { michael@0: Promise.resolve() michael@0: .then(aTestCaseMain) michael@0: .then(finish, function() { michael@0: ok(false, 'promise rejects during test.'); michael@0: finish(); michael@0: }); michael@0: }