1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/events/test/marionette/head.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,146 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +const {Cc: Cc, Ci: Ci, Cr: Cr, Cu: Cu} = SpecialPowers; 1.8 + 1.9 +let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise; 1.10 + 1.11 +let _pendingEmulatorCmdCount = 0; 1.12 + 1.13 +/** 1.14 + * Send emulator command with safe guard. 1.15 + * 1.16 + * We should only call |finish()| after all emulator command transactions 1.17 + * end, so here comes with the pending counter. Resolve when the emulator 1.18 + * gives positive response, and reject otherwise. 1.19 + * 1.20 + * Fulfill params: 1.21 + * result -- an array of emulator response lines. 1.22 + * Reject params: 1.23 + * result -- an array of emulator response lines. 1.24 + * 1.25 + * @return A deferred promise. 1.26 + */ 1.27 +function runEmulatorCmdSafe(aCommand) { 1.28 + let deferred = Promise.defer(); 1.29 + 1.30 + ++_pendingEmulatorCmdCount; 1.31 + runEmulatorCmd(aCommand, function(aResult) { 1.32 + --_pendingEmulatorCmdCount; 1.33 + 1.34 + ok(true, "Emulator response: " + JSON.stringify(aResult)); 1.35 + if (Array.isArray(aResult) && 1.36 + aResult[aResult.length - 1] === "OK") { 1.37 + deferred.resolve(aResult); 1.38 + } else { 1.39 + deferred.reject(aResult); 1.40 + } 1.41 + }); 1.42 + 1.43 + return deferred.promise; 1.44 +} 1.45 + 1.46 +/** 1.47 + * Get emulator sensor values of a named sensor. 1.48 + * 1.49 + * Fulfill params: 1.50 + * result -- an array of emulator sensor values. 1.51 + * Reject params: (none) 1.52 + * 1.53 + * @param aSensorName 1.54 + * A string name of the sensor. Availables are: "acceleration" 1.55 + * "magnetic-field", "orientation", "temperature", "proximity". 1.56 + * 1.57 + * @return A deferred promise. 1.58 + */ 1.59 +function getEmulatorSensorValues(aSensorName) { 1.60 + return runEmulatorCmdSafe("sensor get " + aSensorName) 1.61 + .then(function(aResult) { 1.62 + // aResult = ["orientation = 0:0:0", "OK"] 1.63 + return aResult[0].split(" ")[2].split(":").map(function(aElement) { 1.64 + return parseInt(aElement, 10); 1.65 + }); 1.66 + }); 1.67 +} 1.68 + 1.69 +/** 1.70 + * Convenient alias function for getting orientation sensor values. 1.71 + */ 1.72 +function getEmulatorOrientationValues() { 1.73 + return getEmulatorSensorValues("orientation"); 1.74 +} 1.75 + 1.76 +/** 1.77 + * Set emulator orientation sensor values. 1.78 + * 1.79 + * Fulfill params: (none) 1.80 + * Reject params: (none) 1.81 + * 1.82 + * @param aAzimuth 1.83 + * @param aPitch 1.84 + * @param aRoll 1.85 + * 1.86 + * @return A deferred promise. 1.87 + */ 1.88 +function setEmulatorOrientationValues(aAzimuth, aPitch, aRoll) { 1.89 + let cmd = "sensor set orientation " + aAzimuth + ":" + aPitch + ":" + aRoll; 1.90 + return runEmulatorCmdSafe(cmd); 1.91 +} 1.92 + 1.93 +/** 1.94 + * Wait for a named window event. 1.95 + * 1.96 + * Resolve if that named event occurs. Never reject. 1.97 + * 1.98 + * Forfill params: the DOMEvent passed. 1.99 + * 1.100 + * @param aEventName 1.101 + * A string event name. 1.102 + * 1.103 + * @return A deferred promise. 1.104 + */ 1.105 +function waitForWindowEvent(aEventName) { 1.106 + let deferred = Promise.defer(); 1.107 + 1.108 + window.addEventListener(aEventName, function onevent(aEvent) { 1.109 + window.removeEventListener(aEventName, onevent); 1.110 + 1.111 + ok(true, "Window event '" + aEventName + "' got."); 1.112 + deferred.resolve(aEvent); 1.113 + }); 1.114 + 1.115 + return deferred.promise; 1.116 +} 1.117 + 1.118 +/** 1.119 + * Flush permission settings and call |finish()|. 1.120 + */ 1.121 +function cleanUp() { 1.122 + waitFor(function() { 1.123 + SpecialPowers.flushPermissions(function() { 1.124 + // Use ok here so that we have at least one test run. 1.125 + ok(true, "permissions flushed"); 1.126 + 1.127 + finish(); 1.128 + }); 1.129 + }, function() { 1.130 + return _pendingEmulatorCmdCount === 0; 1.131 + }); 1.132 +} 1.133 + 1.134 +/** 1.135 + * Basic test routine helper. 1.136 + * 1.137 + * This helper does nothing but clean-ups. 1.138 + * 1.139 + * @param aTestCaseMain 1.140 + * A function that takes no parameter. 1.141 + */ 1.142 +function startTestBase(aTestCaseMain) { 1.143 + Promise.resolve() 1.144 + .then(aTestCaseMain) 1.145 + .then(cleanUp, function() { 1.146 + ok(false, 'promise rejects during test.'); 1.147 + cleanUp(); 1.148 + }); 1.149 +}