1 | |
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | exports.defineAutoTests = function () {
|
26 | var isWindows = (cordova.platformId === "windows") || (cordova.platformId === "windows8"),
|
27 |
|
28 |
|
29 |
|
30 | isAccelExist = isWindows ? Windows.Devices.Sensors.Accelerometer.getDefault() !== null : true;
|
31 |
|
32 | describe('Accelerometer (navigator.accelerometer)', function () {
|
33 | var fail = function(done) {
|
34 | expect(true).toBe(false);
|
35 | done();
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 | afterEach(function(done) {
|
41 | setTimeout(function() {
|
42 | done();
|
43 | }, 100);
|
44 | });
|
45 |
|
46 | it("accelerometer.spec.1 should exist", function () {
|
47 | expect(navigator.accelerometer).toBeDefined();
|
48 | });
|
49 |
|
50 | describe("getCurrentAcceleration", function() {
|
51 | it("accelerometer.spec.2 should exist", function() {
|
52 | expect(typeof navigator.accelerometer.getCurrentAcceleration).toBeDefined();
|
53 | expect(typeof navigator.accelerometer.getCurrentAcceleration == 'function').toBe(true);
|
54 | });
|
55 |
|
56 | it("accelerometer.spec.3 success callback should be called with an Acceleration object", function(done) {
|
57 |
|
58 | if (!isAccelExist) {
|
59 | pending();
|
60 | }
|
61 | var win = function(a) {
|
62 | expect(a).toBeDefined();
|
63 | expect(a.x).toBeDefined();
|
64 | expect(typeof a.x == 'number').toBe(true);
|
65 | expect(a.y).toBeDefined();
|
66 | expect(typeof a.y == 'number').toBe(true);
|
67 | expect(a.z).toBeDefined();
|
68 | expect(typeof a.z == 'number').toBe(true);
|
69 | expect(a.timestamp).toBeDefined();
|
70 | expect(typeof a.timestamp).toBe('number');
|
71 | done();
|
72 | };
|
73 |
|
74 | var onError = function(err){
|
75 | console.log(err);
|
76 | console.log("Skipping gyroscope tests, marking all as pending.");
|
77 | isAccelExist = false;
|
78 | expect(true).toBe(true);
|
79 | done();
|
80 | };
|
81 |
|
82 | navigator.accelerometer.getCurrentAcceleration(win, onError);
|
83 | });
|
84 |
|
85 | it("accelerometer.spec.4 success callback Acceleration object should have (reasonable) values for x, y and z expressed in m/s^2", function(done) {
|
86 |
|
87 | if (!isAccelExist) {
|
88 | pending();
|
89 | }
|
90 | var reasonableThreshold = 15;
|
91 | var win = function(a) {
|
92 | expect(a.x).toBeLessThan(reasonableThreshold);
|
93 | expect(a.x).toBeGreaterThan(reasonableThreshold * -1);
|
94 | expect(a.y).toBeLessThan(reasonableThreshold);
|
95 | expect(a.y).toBeGreaterThan(reasonableThreshold * -1);
|
96 | expect(a.z).toBeLessThan(reasonableThreshold);
|
97 | expect(a.z).toBeGreaterThan(reasonableThreshold * -1);
|
98 | done();
|
99 | };
|
100 |
|
101 | navigator.accelerometer.getCurrentAcceleration(win, fail.bind(null,done));
|
102 | });
|
103 |
|
104 | it("accelerometer.spec.5 success callback Acceleration object should return a recent timestamp", function(done) {
|
105 |
|
106 | if (!isAccelExist) {
|
107 | pending();
|
108 | }
|
109 | var veryRecently = (new Date()).getTime();
|
110 |
|
111 |
|
112 | var reasonableTimeLimit = veryRecently + 5000;
|
113 | var win = function(a) {
|
114 | expect(a.timestamp).toBeGreaterThan(veryRecently);
|
115 | expect(a.timestamp).toBeLessThan(reasonableTimeLimit);
|
116 | done();
|
117 | };
|
118 |
|
119 | navigator.accelerometer.getCurrentAcceleration(win, fail.bind(null,done));
|
120 | });
|
121 | });
|
122 |
|
123 | describe("watchAcceleration", function() {
|
124 | var id;
|
125 |
|
126 | afterEach(function(done) {
|
127 | if (id) {
|
128 | navigator.accelerometer.clearWatch(id);
|
129 | }
|
130 |
|
131 |
|
132 | setTimeout(done, 100);
|
133 | });
|
134 |
|
135 | it("accelerometer.spec.6 should exist", function() {
|
136 | expect(navigator.accelerometer.watchAcceleration).toBeDefined();
|
137 | expect(typeof navigator.accelerometer.watchAcceleration == 'function').toBe(true);
|
138 | });
|
139 |
|
140 | it("accelerometer.spec.7 success callback should be called with an Acceleration object", function(done) {
|
141 |
|
142 | if (!isAccelExist) {
|
143 | pending();
|
144 | }
|
145 | var win = function(a) {
|
146 | expect(a).toBeDefined();
|
147 | expect(a.x).toBeDefined();
|
148 | expect(typeof a.x == 'number').toBe(true);
|
149 | expect(a.y).toBeDefined();
|
150 | expect(typeof a.y == 'number').toBe(true);
|
151 | expect(a.z).toBeDefined();
|
152 | expect(typeof a.z == 'number').toBe(true);
|
153 | expect(a.timestamp).toBeDefined();
|
154 | expect(typeof a.timestamp).toBe('number');
|
155 | done();
|
156 | };
|
157 |
|
158 | id = navigator.accelerometer.watchAcceleration(win, fail.bind(null,done), {frequency:100});
|
159 | });
|
160 |
|
161 | it("accelerometer.spec.8 success callback Acceleration object should have (reasonable) values for x, y and z expressed in m/s^2", function(done) {
|
162 |
|
163 | if (!isAccelExist) {
|
164 | pending();
|
165 | }
|
166 | var reasonableThreshold = 15;
|
167 | var win = function(a) {
|
168 | expect(a.x).toBeLessThan(reasonableThreshold);
|
169 | expect(a.x).toBeGreaterThan(reasonableThreshold * -1);
|
170 | expect(a.y).toBeLessThan(reasonableThreshold);
|
171 | expect(a.y).toBeGreaterThan(reasonableThreshold * -1);
|
172 | expect(a.z).toBeLessThan(reasonableThreshold);
|
173 | expect(a.z).toBeGreaterThan(reasonableThreshold * -1);
|
174 | done();
|
175 | };
|
176 |
|
177 | id = navigator.accelerometer.watchAcceleration(win, fail.bind(null,done), {frequency:100});
|
178 | });
|
179 |
|
180 | it("accelerometer.spec.9 success callback Acceleration object should return a recent timestamp", function(done) {
|
181 |
|
182 | if (!isAccelExist) {
|
183 | pending();
|
184 | }
|
185 | var veryRecently = (new Date()).getTime();
|
186 |
|
187 |
|
188 | var reasonableTimeLimit = veryRecently + 5000;
|
189 | var win = function(a) {
|
190 | expect(a.timestamp).toBeGreaterThan(veryRecently);
|
191 | expect(a.timestamp).toBeLessThan(reasonableTimeLimit);
|
192 | done();
|
193 | };
|
194 |
|
195 | id = navigator.accelerometer.watchAcceleration(win, fail.bind(null,done), {frequency:100});
|
196 | });
|
197 |
|
198 | it("accelerometer.spec.12 success callback should be preserved and called several times", function (done) {
|
199 |
|
200 | if (!isAccelExist) {
|
201 | pending();
|
202 | }
|
203 | var callbacksCallCount = 0,
|
204 | callbacksCallTestCount = 3;
|
205 |
|
206 | var win = function (a) {
|
207 | if (callbacksCallCount++ < callbacksCallTestCount) return;
|
208 | expect(typeof a).toBe('object');
|
209 | done();
|
210 | };
|
211 |
|
212 | id = navigator.accelerometer.watchAcceleration(win, fail.bind(null, done), { frequency: 100 });
|
213 | });
|
214 | });
|
215 |
|
216 | describe("clearWatch", function() {
|
217 | it("accelerometer.spec.10 should exist", function() {
|
218 | expect(navigator.accelerometer.clearWatch).toBeDefined();
|
219 | expect(typeof navigator.accelerometer.clearWatch == 'function').toBe(true);
|
220 | });
|
221 |
|
222 | it("accelerometer.spec.11 should clear an existing watch", function(done) {
|
223 | // skip the test if Accelerometer doesn't exist on this device
|
224 | if (!isAccelExist) {
|
225 | pending();
|
226 | }
|
227 | var id;
|
228 |
|
229 | // expect win to get called exactly once
|
230 | var win = function(a) {
|
231 | // clear watch on first call
|
232 | navigator.accelerometer.clearWatch(id);
|
233 | // if win isn't called again in 201 ms we assume success
|
234 | var tid = setTimeout(function() {
|
235 | expect(true).toBe(true);
|
236 | done();
|
237 | }, 101);
|
238 | // if win is called again, clear the timeout and fail the test
|
239 | win = function() {
|
240 | clearTimeout(tid);
|
241 | fail(done);
|
242 | };
|
243 | };
|
244 |
|
245 | // wrap the success call in a closure since the value of win changes between calls
|
246 | id = navigator.accelerometer.watchAcceleration(function() { win(); }, fail.bind(null, done), {frequency:100});
|
247 | });
|
248 | });
|
249 | });
|
250 | };
|
251 |
|
252 | /******************************************************************************/
|
253 | /******************************************************************************/
|
254 | /******************************************************************************/
|
255 |
|
256 | exports.defineManualTests = function (contentEl, createActionButton) {
|
257 | function roundNumber(num) {
|
258 | var dec = 3;
|
259 | var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
|
260 | return result;
|
261 | }
|
262 |
|
263 | var watchAccelId = null;
|
264 |
|
265 | /**
|
266 | * Set accelerometer status
|
267 | */
|
268 | function setAccelStatus(status) {
|
269 | document.getElementById('accel_status').innerHTML = status;
|
270 | }
|
271 |
|
272 | /**
|
273 | * Stop watching the acceleration
|
274 | */
|
275 | function stopAccel() {
|
276 | console.log("stopAccel()");
|
277 | setAccelStatus("Stopped");
|
278 | if (watchAccelId) {
|
279 | navigator.accelerometer.clearWatch(watchAccelId);
|
280 | watchAccelId = null;
|
281 | }
|
282 | }
|
283 |
|
284 | /**
|
285 | * Start watching acceleration
|
286 | */
|
287 | var watchAccel = function () {
|
288 | console.log("watchAccel()");
|
289 |
|
290 | // Success callback
|
291 | var success = function (a) {
|
292 | document.getElementById('x').innerHTML = roundNumber(a.x);
|
293 | document.getElementById('y').innerHTML = roundNumber(a.y);
|
294 | document.getElementById('z').innerHTML = roundNumber(a.z);
|
295 | document.getElementById('t').innerHTML = a.timestamp;
|
296 | };
|
297 |
|
298 | // Fail callback
|
299 | var fail = function (e) {
|
300 | console.log("watchAccel fail callback with error code " + e);
|
301 | stopAccel();
|
302 | setAccelStatus(e);
|
303 | };
|
304 |
|
305 | // Update acceleration every 1 sec
|
306 | var opt = {};
|
307 | opt.frequency = 1000;
|
308 | watchAccelId = navigator.accelerometer.watchAcceleration(success, fail, opt);
|
309 |
|
310 | setAccelStatus("Running");
|
311 | };
|
312 |
|
313 | /**
|
314 | * Get current acceleration
|
315 | */
|
316 | var getAccel = function () {
|
317 | console.log("getAccel()");
|
318 |
|
319 | // Stop accel if running
|
320 | stopAccel();
|
321 |
|
322 | // Success callback
|
323 | var success = function (a) {
|
324 | document.getElementById('x').innerHTML = roundNumber(a.x);
|
325 | document.getElementById('y').innerHTML = roundNumber(a.y);
|
326 | document.getElementById('z').innerHTML = roundNumber(a.z);
|
327 | document.getElementById('t').innerHTML = a.timestamp;
|
328 | console.log("getAccel success callback");
|
329 | };
|
330 |
|
331 | // Fail callback
|
332 | var fail = function (e) {
|
333 | console.log("getAccel fail callback with error code " + e);
|
334 | setAccelStatus(e);
|
335 | };
|
336 |
|
337 | // Make call
|
338 | var opt = {};
|
339 | navigator.accelerometer.getCurrentAcceleration(success, fail, opt);
|
340 | };
|
341 |
|
342 | /******************************************************************************/
|
343 |
|
344 | var accelerometer_tests = '<div id="getAcceleration"></div>' +
|
345 | 'Expected result: Will update the status box with X, Y, and Z values when pressed. Status will read "Stopped"' +
|
346 | '<p/> <div id="watchAcceleration"></div>' +
|
347 | 'Expected result: When pressed, will start a watch on the accelerometer and update X,Y,Z values when movement is sensed. Status will read "Running"' +
|
348 | '<p/> <div id="clearAcceleration"></div>' +
|
349 | 'Expected result: Will clear the accelerometer watch, so X,Y,Z values will no longer be updated. Status will read "Stopped"';
|
350 |
|
351 | contentEl.innerHTML = '<div id="info">' +
|
352 | 'Status: <span id="accel_status">Stopped</span>' +
|
353 | '<table width="100%">' +
|
354 | '<tr><td width="30%">X:</td><td id="x"> </td></tr>' +
|
355 | '<tr><td width="30%">Y:</td><td id="y"> </td></tr>' +
|
356 | '<tr><td width="30%">Z:</td><td id="z"> </td></tr>' +
|
357 | '<tr><td width="30%">Timestamp:</td><td id="t"> </td></tr>' +
|
358 | '</table></div>' +
|
359 | accelerometer_tests;
|
360 |
|
361 | createActionButton('Get Acceleration', function () {
|
362 | getAccel();
|
363 | }, 'getAcceleration');
|
364 |
|
365 | createActionButton('Start Watch', function () {
|
366 | watchAccel();
|
367 | }, 'watchAcceleration');
|
368 |
|
369 | createActionButton('Clear Watch', function () {
|
370 | stopAccel();
|
371 | }, 'clearAcceleration');
|
372 | };
|
373 |
|
\ | No newline at end of file |