Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | const {Cc: Cc, Ci: Ci, Cr: Cr, Cu: Cu} = SpecialPowers; |
michael@0 | 5 | |
michael@0 | 6 | let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise; |
michael@0 | 7 | |
michael@0 | 8 | let _pendingEmulatorCmdCount = 0; |
michael@0 | 9 | |
michael@0 | 10 | /** |
michael@0 | 11 | * Send emulator command with safe guard. |
michael@0 | 12 | * |
michael@0 | 13 | * We should only call |finish()| after all emulator command transactions |
michael@0 | 14 | * end, so here comes with the pending counter. Resolve when the emulator |
michael@0 | 15 | * gives positive response, and reject otherwise. |
michael@0 | 16 | * |
michael@0 | 17 | * Fulfill params: |
michael@0 | 18 | * result -- an array of emulator response lines. |
michael@0 | 19 | * Reject params: |
michael@0 | 20 | * result -- an array of emulator response lines. |
michael@0 | 21 | * |
michael@0 | 22 | * @return A deferred promise. |
michael@0 | 23 | */ |
michael@0 | 24 | function runEmulatorCmdSafe(aCommand) { |
michael@0 | 25 | let deferred = Promise.defer(); |
michael@0 | 26 | |
michael@0 | 27 | ++_pendingEmulatorCmdCount; |
michael@0 | 28 | runEmulatorCmd(aCommand, function(aResult) { |
michael@0 | 29 | --_pendingEmulatorCmdCount; |
michael@0 | 30 | |
michael@0 | 31 | ok(true, "Emulator response: " + JSON.stringify(aResult)); |
michael@0 | 32 | if (Array.isArray(aResult) && |
michael@0 | 33 | aResult[aResult.length - 1] === "OK") { |
michael@0 | 34 | deferred.resolve(aResult); |
michael@0 | 35 | } else { |
michael@0 | 36 | deferred.reject(aResult); |
michael@0 | 37 | } |
michael@0 | 38 | }); |
michael@0 | 39 | |
michael@0 | 40 | return deferred.promise; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | /** |
michael@0 | 44 | * Get emulator sensor values of a named sensor. |
michael@0 | 45 | * |
michael@0 | 46 | * Fulfill params: |
michael@0 | 47 | * result -- an array of emulator sensor values. |
michael@0 | 48 | * Reject params: (none) |
michael@0 | 49 | * |
michael@0 | 50 | * @param aSensorName |
michael@0 | 51 | * A string name of the sensor. Availables are: "acceleration" |
michael@0 | 52 | * "magnetic-field", "orientation", "temperature", "proximity". |
michael@0 | 53 | * |
michael@0 | 54 | * @return A deferred promise. |
michael@0 | 55 | */ |
michael@0 | 56 | function getEmulatorSensorValues(aSensorName) { |
michael@0 | 57 | return runEmulatorCmdSafe("sensor get " + aSensorName) |
michael@0 | 58 | .then(function(aResult) { |
michael@0 | 59 | // aResult = ["orientation = 0:0:0", "OK"] |
michael@0 | 60 | return aResult[0].split(" ")[2].split(":").map(function(aElement) { |
michael@0 | 61 | return parseInt(aElement, 10); |
michael@0 | 62 | }); |
michael@0 | 63 | }); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | /** |
michael@0 | 67 | * Convenient alias function for getting orientation sensor values. |
michael@0 | 68 | */ |
michael@0 | 69 | function getEmulatorOrientationValues() { |
michael@0 | 70 | return getEmulatorSensorValues("orientation"); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Set emulator orientation sensor values. |
michael@0 | 75 | * |
michael@0 | 76 | * Fulfill params: (none) |
michael@0 | 77 | * Reject params: (none) |
michael@0 | 78 | * |
michael@0 | 79 | * @param aAzimuth |
michael@0 | 80 | * @param aPitch |
michael@0 | 81 | * @param aRoll |
michael@0 | 82 | * |
michael@0 | 83 | * @return A deferred promise. |
michael@0 | 84 | */ |
michael@0 | 85 | function setEmulatorOrientationValues(aAzimuth, aPitch, aRoll) { |
michael@0 | 86 | let cmd = "sensor set orientation " + aAzimuth + ":" + aPitch + ":" + aRoll; |
michael@0 | 87 | return runEmulatorCmdSafe(cmd); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * Wait for a named window event. |
michael@0 | 92 | * |
michael@0 | 93 | * Resolve if that named event occurs. Never reject. |
michael@0 | 94 | * |
michael@0 | 95 | * Forfill params: the DOMEvent passed. |
michael@0 | 96 | * |
michael@0 | 97 | * @param aEventName |
michael@0 | 98 | * A string event name. |
michael@0 | 99 | * |
michael@0 | 100 | * @return A deferred promise. |
michael@0 | 101 | */ |
michael@0 | 102 | function waitForWindowEvent(aEventName) { |
michael@0 | 103 | let deferred = Promise.defer(); |
michael@0 | 104 | |
michael@0 | 105 | window.addEventListener(aEventName, function onevent(aEvent) { |
michael@0 | 106 | window.removeEventListener(aEventName, onevent); |
michael@0 | 107 | |
michael@0 | 108 | ok(true, "Window event '" + aEventName + "' got."); |
michael@0 | 109 | deferred.resolve(aEvent); |
michael@0 | 110 | }); |
michael@0 | 111 | |
michael@0 | 112 | return deferred.promise; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | /** |
michael@0 | 116 | * Flush permission settings and call |finish()|. |
michael@0 | 117 | */ |
michael@0 | 118 | function cleanUp() { |
michael@0 | 119 | waitFor(function() { |
michael@0 | 120 | SpecialPowers.flushPermissions(function() { |
michael@0 | 121 | // Use ok here so that we have at least one test run. |
michael@0 | 122 | ok(true, "permissions flushed"); |
michael@0 | 123 | |
michael@0 | 124 | finish(); |
michael@0 | 125 | }); |
michael@0 | 126 | }, function() { |
michael@0 | 127 | return _pendingEmulatorCmdCount === 0; |
michael@0 | 128 | }); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * Basic test routine helper. |
michael@0 | 133 | * |
michael@0 | 134 | * This helper does nothing but clean-ups. |
michael@0 | 135 | * |
michael@0 | 136 | * @param aTestCaseMain |
michael@0 | 137 | * A function that takes no parameter. |
michael@0 | 138 | */ |
michael@0 | 139 | function startTestBase(aTestCaseMain) { |
michael@0 | 140 | Promise.resolve() |
michael@0 | 141 | .then(aTestCaseMain) |
michael@0 | 142 | .then(cleanUp, function() { |
michael@0 | 143 | ok(false, 'promise rejects during test.'); |
michael@0 | 144 | cleanUp(); |
michael@0 | 145 | }); |
michael@0 | 146 | } |