UNPKG

13.8 kBJavaScriptView Raw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22/* jshint jasmine: true */
23/* global Windows */
24
25exports.defineAutoTests = function () {
26 var isWindows = (cordova.platformId === "windows") || (cordova.platformId === "windows8"),
27 // Checking existence of accelerometer for windows platform
28 // Assumed that accelerometer always exists on other platforms. Extend
29 // condition to support accelerometer check on other platforms
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 // This timeout is here to lessen the load on native accelerometer
39 // intensive use of which can lead to occasional test failures
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 // skip the test if Accelerometer doesn't exist on this device
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 // skip the test if Accelerometer doesn't exist on this device
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 // skip the test if Accelerometer doesn't exist on this device
106 if (!isAccelExist) {
107 pending();
108 }
109 var veryRecently = (new Date()).getTime();
110 // Need to check that dates returned are not vastly greater than a recent time stamp.
111 // In case the timestamps returned are ridiculously high
112 var reasonableTimeLimit = veryRecently + 5000; // 5 seconds from now
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() {
127 navigator.accelerometer.clearWatch(id);
128 });
129
130 it("accelerometer.spec.6 should exist", function() {
131 expect(navigator.accelerometer.watchAcceleration).toBeDefined();
132 expect(typeof navigator.accelerometer.watchAcceleration == 'function').toBe(true);
133 });
134
135 it("accelerometer.spec.7 success callback should be called with an Acceleration object", function(done) {
136 // skip the test if Accelerometer doesn't exist on this device
137 if (!isAccelExist) {
138 pending();
139 }
140 var win = function(a) {
141 expect(a).toBeDefined();
142 expect(a.x).toBeDefined();
143 expect(typeof a.x == 'number').toBe(true);
144 expect(a.y).toBeDefined();
145 expect(typeof a.y == 'number').toBe(true);
146 expect(a.z).toBeDefined();
147 expect(typeof a.z == 'number').toBe(true);
148 expect(a.timestamp).toBeDefined();
149 expect(typeof a.timestamp).toBe('number');
150 done();
151 };
152
153 id = navigator.accelerometer.watchAcceleration(win, fail.bind(null,done), {frequency:100});
154 });
155
156 it("accelerometer.spec.8 success callback Acceleration object should have (reasonable) values for x, y and z expressed in m/s^2", function(done) {
157 // skip the test if Accelerometer doesn't exist on this device
158 if (!isAccelExist) {
159 pending();
160 }
161 var reasonableThreshold = 15;
162 var win = function(a) {
163 expect(a.x).toBeLessThan(reasonableThreshold);
164 expect(a.x).toBeGreaterThan(reasonableThreshold * -1);
165 expect(a.y).toBeLessThan(reasonableThreshold);
166 expect(a.y).toBeGreaterThan(reasonableThreshold * -1);
167 expect(a.z).toBeLessThan(reasonableThreshold);
168 expect(a.z).toBeGreaterThan(reasonableThreshold * -1);
169 done();
170 };
171
172 id = navigator.accelerometer.watchAcceleration(win, fail.bind(null,done), {frequency:100});
173 });
174
175 it("accelerometer.spec.9 success callback Acceleration object should return a recent timestamp", function(done) {
176 // skip the test if Accelerometer doesn't exist on this device
177 if (!isAccelExist) {
178 pending();
179 }
180 var veryRecently = (new Date()).getTime();
181 // Need to check that dates returned are not vastly greater than a recent time stamp.
182 // In case the timestamps returned are ridiculously high
183 var reasonableTimeLimit = veryRecently + 5000; // 5 seconds from now
184 var win = function(a) {
185 expect(a.timestamp).toBeGreaterThan(veryRecently);
186 expect(a.timestamp).toBeLessThan(reasonableTimeLimit);
187 done();
188 };
189
190 id = navigator.accelerometer.watchAcceleration(win, fail.bind(null,done), {frequency:100});
191 });
192
193 it("accelerometer.spec.12 success callback should be preserved and called several times", function (done) {
194 // skip the test if Accelerometer doesn't exist on this device
195 if (!isAccelExist) {
196 pending();
197 }
198 var callbacksCallCount = 0,
199 callbacksCallTestCount = 3;
200
201 var win = function (a) {
202 if (callbacksCallCount++ < callbacksCallTestCount) return;
203 expect(typeof a).toBe('object');
204 done();
205 };
206
207 id = navigator.accelerometer.watchAcceleration(win, fail.bind(null, done), { frequency: 100 });
208 });
209 });
210
211 describe("clearWatch", function() {
212 it("accelerometer.spec.10 should exist", function() {
213 expect(navigator.accelerometer.clearWatch).toBeDefined();
214 expect(typeof navigator.accelerometer.clearWatch == 'function').toBe(true);
215 });
216
217 it("accelerometer.spec.11 should clear an existing watch", function(done) {
218 // skip the test if Accelerometer doesn't exist on this device
219 if (!isAccelExist) {
220 pending();
221 }
222 var id;
223
224 // expect win to get called exactly once
225 var win = function(a) {
226 // clear watch on first call
227 navigator.accelerometer.clearWatch(id);
228 // if win isn't called again in 201 ms we assume success
229 var tid = setTimeout(function() {
230 expect(true).toBe(true);
231 done();
232 }, 101);
233 // if win is called again, clear the timeout and fail the test
234 win = function() {
235 clearTimeout(tid);
236 fail(done);
237 };
238 };
239
240 // wrap the success call in a closure since the value of win changes between calls
241 id = navigator.accelerometer.watchAcceleration(function() { win(); }, fail.bind(null, done), {frequency:100});
242 });
243 });
244 });
245};
246
247/******************************************************************************/
248/******************************************************************************/
249/******************************************************************************/
250
251exports.defineManualTests = function (contentEl, createActionButton) {
252 function roundNumber(num) {
253 var dec = 3;
254 var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
255 return result;
256 }
257
258 var watchAccelId = null;
259
260 /**
261 * Set accelerometer status
262 */
263 function setAccelStatus(status) {
264 document.getElementById('accel_status').innerHTML = status;
265 }
266
267 /**
268 * Stop watching the acceleration
269 */
270 function stopAccel() {
271 console.log("stopAccel()");
272 setAccelStatus("Stopped");
273 if (watchAccelId) {
274 navigator.accelerometer.clearWatch(watchAccelId);
275 watchAccelId = null;
276 }
277 }
278
279 /**
280 * Start watching acceleration
281 */
282 var watchAccel = function () {
283 console.log("watchAccel()");
284
285 // Success callback
286 var success = function (a) {
287 document.getElementById('x').innerHTML = roundNumber(a.x);
288 document.getElementById('y').innerHTML = roundNumber(a.y);
289 document.getElementById('z').innerHTML = roundNumber(a.z);
290 };
291
292 // Fail callback
293 var fail = function (e) {
294 console.log("watchAccel fail callback with error code " + e);
295 stopAccel();
296 setAccelStatus(e);
297 };
298
299 // Update acceleration every 1 sec
300 var opt = {};
301 opt.frequency = 1000;
302 watchAccelId = navigator.accelerometer.watchAcceleration(success, fail, opt);
303
304 setAccelStatus("Running");
305 };
306
307 /**
308 * Get current acceleration
309 */
310 var getAccel = function () {
311 console.log("getAccel()");
312
313 // Stop accel if running
314 stopAccel();
315
316 // Success callback
317 var success = function (a) {
318 document.getElementById('x').innerHTML = roundNumber(a.x);
319 document.getElementById('y').innerHTML = roundNumber(a.y);
320 document.getElementById('z').innerHTML = roundNumber(a.z);
321 console.log("getAccel success callback");
322 };
323
324 // Fail callback
325 var fail = function (e) {
326 console.log("getAccel fail callback with error code " + e);
327 setAccelStatus(e);
328 };
329
330 // Make call
331 var opt = {};
332 navigator.accelerometer.getCurrentAcceleration(success, fail, opt);
333 };
334
335 /******************************************************************************/
336
337 var accelerometer_tests = '<div id="getAcceleration"></div>' +
338 'Expected result: Will update the status box with X, Y, and Z values when pressed. Status will read "Stopped"' +
339 '<p/> <div id="watchAcceleration"></div>' +
340 '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"' +
341 '<p/> <div id="clearAcceleration"></div>' +
342 'Expected result: Will clear the accelerometer watch, so X,Y,Z values will no longer be updated. Status will read "Stopped"';
343
344 contentEl.innerHTML = '<div id="info">' +
345 'Status: <span id="accel_status">Stopped</span>' +
346 '<table width="100%">' +
347 '<tr><td width="20%">X:</td><td id="x"> </td></tr>' +
348 '<tr><td width="20%">Y:</td><td id="y"> </td></tr>' +
349 '<tr><td width="20%">Z:</td><td id="z"> </td></tr>' +
350 '</table></div>' +
351 accelerometer_tests;
352
353 createActionButton('Get Acceleration', function () {
354 getAccel();
355 }, 'getAcceleration');
356
357 createActionButton('Start Watch', function () {
358 watchAccel();
359 }, 'watchAcceleration');
360
361 createActionButton('Clear Watch', function () {
362 stopAccel();
363 }, 'clearAcceleration');
364};
365
\No newline at end of file