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