michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const {Cc: Cc, Ci: Ci, Cr: Cr, Cu: Cu} = SpecialPowers; michael@0: michael@0: let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise; michael@0: michael@0: let _pendingEmulatorCmdCount = 0; michael@0: michael@0: /** michael@0: * Send emulator command with safe guard. michael@0: * michael@0: * We should only call |finish()| after all emulator command transactions michael@0: * end, so here comes with the pending counter. Resolve when the emulator michael@0: * gives positive response, and reject otherwise. michael@0: * michael@0: * Fulfill params: michael@0: * result -- an array of emulator response lines. michael@0: * Reject params: michael@0: * result -- an array of emulator response lines. michael@0: * michael@0: * @return A deferred promise. michael@0: */ michael@0: function runEmulatorCmdSafe(aCommand) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: ++_pendingEmulatorCmdCount; michael@0: runEmulatorCmd(aCommand, function(aResult) { michael@0: --_pendingEmulatorCmdCount; michael@0: michael@0: ok(true, "Emulator response: " + JSON.stringify(aResult)); michael@0: if (Array.isArray(aResult) && michael@0: aResult[aResult.length - 1] === "OK") { michael@0: deferred.resolve(aResult); michael@0: } else { michael@0: deferred.reject(aResult); michael@0: } michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: /** michael@0: * Get emulator sensor values of a named sensor. michael@0: * michael@0: * Fulfill params: michael@0: * result -- an array of emulator sensor values. michael@0: * Reject params: (none) michael@0: * michael@0: * @param aSensorName michael@0: * A string name of the sensor. Availables are: "acceleration" michael@0: * "magnetic-field", "orientation", "temperature", "proximity". michael@0: * michael@0: * @return A deferred promise. michael@0: */ michael@0: function getEmulatorSensorValues(aSensorName) { michael@0: return runEmulatorCmdSafe("sensor get " + aSensorName) michael@0: .then(function(aResult) { michael@0: // aResult = ["orientation = 0:0:0", "OK"] michael@0: return aResult[0].split(" ")[2].split(":").map(function(aElement) { michael@0: return parseInt(aElement, 10); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Convenient alias function for getting orientation sensor values. michael@0: */ michael@0: function getEmulatorOrientationValues() { michael@0: return getEmulatorSensorValues("orientation"); michael@0: } michael@0: michael@0: /** michael@0: * Set emulator orientation sensor values. michael@0: * michael@0: * Fulfill params: (none) michael@0: * Reject params: (none) michael@0: * michael@0: * @param aAzimuth michael@0: * @param aPitch michael@0: * @param aRoll michael@0: * michael@0: * @return A deferred promise. michael@0: */ michael@0: function setEmulatorOrientationValues(aAzimuth, aPitch, aRoll) { michael@0: let cmd = "sensor set orientation " + aAzimuth + ":" + aPitch + ":" + aRoll; michael@0: return runEmulatorCmdSafe(cmd); michael@0: } michael@0: michael@0: /** michael@0: * Wait for a named window event. michael@0: * michael@0: * Resolve if that named event occurs. Never reject. michael@0: * michael@0: * Forfill params: the DOMEvent passed. michael@0: * michael@0: * @param aEventName michael@0: * A string event name. michael@0: * michael@0: * @return A deferred promise. michael@0: */ michael@0: function waitForWindowEvent(aEventName) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: window.addEventListener(aEventName, function onevent(aEvent) { michael@0: window.removeEventListener(aEventName, onevent); michael@0: michael@0: ok(true, "Window event '" + aEventName + "' got."); michael@0: deferred.resolve(aEvent); michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: /** michael@0: * Flush permission settings and call |finish()|. michael@0: */ michael@0: function cleanUp() { michael@0: waitFor(function() { michael@0: SpecialPowers.flushPermissions(function() { michael@0: // Use ok here so that we have at least one test run. michael@0: ok(true, "permissions flushed"); michael@0: michael@0: finish(); michael@0: }); michael@0: }, function() { michael@0: return _pendingEmulatorCmdCount === 0; michael@0: }); 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(cleanUp, function() { michael@0: ok(false, 'promise rejects during test.'); michael@0: cleanUp(); michael@0: }); michael@0: }