dom/system/gonk/tests/marionette/head.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 MARIONETTE_CONTEXT = "chrome";
michael@0 5
michael@0 6 let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise;
michael@0 7
michael@0 8 /**
michael@0 9 * Wrap DOMRequest onsuccess/onerror events to Promise resolve/reject.
michael@0 10 *
michael@0 11 * Fulfill params: A DOMEvent.
michael@0 12 * Reject params: A DOMEvent.
michael@0 13 *
michael@0 14 * @param aRequest
michael@0 15 * A DOMRequest instance.
michael@0 16 *
michael@0 17 * @return A deferred promise.
michael@0 18 */
michael@0 19 function wrapDomRequestAsPromise(aRequest) {
michael@0 20 let deferred = Promise.defer();
michael@0 21
michael@0 22 ok(aRequest instanceof DOMRequest,
michael@0 23 "aRequest is instanceof " + aRequest.constructor);
michael@0 24
michael@0 25 aRequest.addEventListener("success", function(aEvent) {
michael@0 26 deferred.resolve(aEvent);
michael@0 27 });
michael@0 28 aRequest.addEventListener("error", function(aEvent) {
michael@0 29 deferred.reject(aEvent);
michael@0 30 });
michael@0 31
michael@0 32 return deferred.promise;
michael@0 33 }
michael@0 34
michael@0 35 /**
michael@0 36 * Get mozSettings value specified by @aKey.
michael@0 37 *
michael@0 38 * Resolve if that mozSettings value is retrieved successfully, reject
michael@0 39 * otherwise.
michael@0 40 *
michael@0 41 * Fulfill params: The corresponding mozSettings value of the key.
michael@0 42 * Reject params: (none)
michael@0 43 *
michael@0 44 * @param aKey
michael@0 45 * A string.
michael@0 46 * @param aAllowError [optional]
michael@0 47 * A boolean value. If set to true, an error response won't be treated
michael@0 48 * as test failure. Default: false.
michael@0 49 *
michael@0 50 * @return A deferred promise.
michael@0 51 */
michael@0 52 function getSettings(aKey, aAllowError) {
michael@0 53 let request = window.navigator.mozSettings.createLock().get(aKey);
michael@0 54 return wrapDomRequestAsPromise(request)
michael@0 55 .then(function resolve(aEvent) {
michael@0 56 log("getSettings(" + aKey + ") - success");
michael@0 57 return aEvent.target.result[aKey];
michael@0 58 }, function reject(aEvent) {
michael@0 59 ok(aAllowError, "getSettings(" + aKey + ") - error");
michael@0 60 });
michael@0 61 }
michael@0 62
michael@0 63 /**
michael@0 64 * Set mozSettings values.
michael@0 65 *
michael@0 66 * Resolve if that mozSettings value is set successfully, reject otherwise.
michael@0 67 *
michael@0 68 * Fulfill params: (none)
michael@0 69 * Reject params: (none)
michael@0 70 *
michael@0 71 * @param aKey
michael@0 72 * A string key.
michael@0 73 * @param aValue
michael@0 74 * An object value.
michael@0 75 * @param aAllowError [optional]
michael@0 76 * A boolean value. If set to true, an error response won't be treated
michael@0 77 * as test failure. Default: false.
michael@0 78 *
michael@0 79 * @return A deferred promise.
michael@0 80 */
michael@0 81 function setSettings(aKey, aValue, aAllowError) {
michael@0 82 let settings = {};
michael@0 83 settings[aKey] = aValue;
michael@0 84 let request = window.navigator.mozSettings.createLock().set(settings);
michael@0 85 return wrapDomRequestAsPromise(request)
michael@0 86 .then(function resolve() {
michael@0 87 log("setSettings(" + JSON.stringify(settings) + ") - success");
michael@0 88 }, function reject() {
michael@0 89 ok(aAllowError, "setSettings(" + JSON.stringify(settings) + ") - error");
michael@0 90 });
michael@0 91 }
michael@0 92
michael@0 93 /**
michael@0 94 * Wait for observer event.
michael@0 95 *
michael@0 96 * Resolve if that topic event occurs. Never reject.
michael@0 97 *
michael@0 98 * Fulfill params: the subject passed.
michael@0 99 *
michael@0 100 * @param aTopic
michael@0 101 * A string topic name.
michael@0 102 *
michael@0 103 * @return A deferred promise.
michael@0 104 */
michael@0 105 function waitForObserverEvent(aTopic) {
michael@0 106 let obs = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
michael@0 107 let deferred = Promise.defer();
michael@0 108
michael@0 109 obs.addObserver(function observer(subject, topic, data) {
michael@0 110 if (topic === aTopic) {
michael@0 111 obs.removeObserver(observer, aTopic);
michael@0 112 deferred.resolve(subject);
michael@0 113 }
michael@0 114 }, aTopic, false);
michael@0 115
michael@0 116 return deferred.promise;
michael@0 117 }
michael@0 118
michael@0 119 /**
michael@0 120 * Basic test routine helper.
michael@0 121 *
michael@0 122 * This helper does nothing but clean-ups.
michael@0 123 *
michael@0 124 * @param aTestCaseMain
michael@0 125 * A function that takes no parameter.
michael@0 126 */
michael@0 127 function startTestBase(aTestCaseMain) {
michael@0 128 Promise.resolve()
michael@0 129 .then(aTestCaseMain)
michael@0 130 .then(finish, function() {
michael@0 131 ok(false, 'promise rejects during test.');
michael@0 132 finish();
michael@0 133 });
michael@0 134 }

mercurial