UNPKG

9.68 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
22exports.defineAutoTests = function () {
23 var fail = function (done, message) {
24 message = (typeof message !== 'string') ? "Forced failure: wrong callback called" : message;
25 expect(true).toFailWithMessage(message);
26 done();
27 },
28 unexpectedSuccess = "Forced failure: success callback should not have been called",
29 unexpectedFailure = "Forced failure: error callback should not have been called";
30
31 describe('Compass (navigator.compass)', function () {
32 var hardwarefailure = false;
33 beforeEach(function () {
34 jasmine.Expectation.addMatchers({
35 toFailWithMessage: function () {
36 return {
37 compare: function (actual, customMessage) {
38 var pass = false;
39 if (customMessage === undefined) {
40 customMessage = "Forced failure: wrong callback called";
41 }
42 return {
43 pass: pass,
44 message: customMessage
45 };
46 }
47 };
48 }
49 });
50 });
51
52 it("compass.hardwarecheck is compass supported", function (done) {
53 navigator.compass.getCurrentHeading(function onSuccess() { done(); },
54 function onError(error) {
55 if (error.code == CompassError.COMPASS_NOT_SUPPORTED) {
56 hardwarefailure = true;
57 expect(this).toFailWithMessage("The device does not have compass support. Any tests relying on support will be marked pending.");
58 }
59 done();
60 });
61 });
62
63 it("compass.spec.1 should exist", function () {
64 console.log("In spec 1");
65 expect(navigator.compass).toBeDefined();
66 });
67
68 it("compass.spec.2 should contain a getCurrentHeading function", function () {
69 expect(navigator.compass.getCurrentHeading).toBeDefined();
70 expect(typeof navigator.compass.getCurrentHeading == 'function').toBe(true);
71 });
72
73 it("compass.spec.3 getCurrentHeading success callback should be called with a Heading object", function (done) {
74 if (hardwarefailure) {
75 pending();
76 }
77 navigator.compass.getCurrentHeading(function (a) {
78 expect(a instanceof CompassHeading).toBe(true);
79 expect(a.magneticHeading).toBeDefined();
80 expect(typeof a.magneticHeading == 'number').toBe(true);
81 expect(a.trueHeading).not.toBe(undefined);
82 expect(typeof a.trueHeading == 'number' || a.trueHeading === null).toBe(true);
83 expect(a.headingAccuracy).not.toBe(undefined);
84 expect(typeof a.headingAccuracy == 'number' || a.headingAccuracy === null).toBe(true);
85 expect(typeof a.timestamp == 'number').toBe(true);
86 done();
87 }, fail.bind(null, done, unexpectedFailure));
88 });
89
90 it("compass.spec.4 should contain a watchHeading function", function () {
91 expect(navigator.compass.watchHeading).toBeDefined();
92 expect(typeof navigator.compass.watchHeading == 'function').toBe(true);
93 });
94
95 it("compass.spec.5 should contain a clearWatch function", function () {
96 expect(navigator.compass.clearWatch).toBeDefined();
97 expect(typeof navigator.compass.clearWatch == 'function').toBe(true);
98 });
99
100 describe('Compass Constants (window.CompassError)', function () {
101 it("compass.spec.1 should exist", function () {
102 expect(window.CompassError).toBeDefined();
103 expect(window.CompassError.COMPASS_INTERNAL_ERR).toBe(0);
104 expect(window.CompassError.COMPASS_NOT_SUPPORTED).toBe(20);
105 });
106 });
107
108 describe('Compass Heading model (CompassHeading)', function () {
109 it("compass.spec.1 should exist", function () {
110 expect(CompassHeading).toBeDefined();
111 });
112
113 it("compass.spec.8 should be able to create a new CompassHeading instance with no parameters", function () {
114 var h = new CompassHeading();
115 expect(h).toBeDefined();
116 expect(h.magneticHeading).toBeUndefined();
117 expect(h.trueHeading).toBeUndefined();
118 expect(h.headingAccuracy).toBeUndefined();
119 expect(typeof h.timestamp == 'number').toBe(true);
120 });
121
122 it("compass.spec.9 should be able to create a new CompassHeading instance with parameters", function () {
123 var h = new CompassHeading(1, 2, 3, 4);
124 expect(h.magneticHeading).toBe(1);
125 expect(h.trueHeading).toBe(2);
126 expect(h.headingAccuracy).toBe(3);
127 expect(h.timestamp.valueOf()).toBe(4);
128 expect(typeof h.timestamp == 'number').toBe(true);
129 });
130 });
131 });
132};
133
134/******************************************************************************/
135/******************************************************************************/
136/******************************************************************************/
137
138exports.defineManualTests = function (contentEl, createActionButton) {
139 function roundNumber(num) {
140 var dec = 3;
141 var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
142 return result;
143 }
144
145 var watchCompassId = null;
146
147 /**
148 * Start watching compass
149 */
150 var watchCompass = function () {
151 console.log("watchCompass()");
152
153 // Success callback
154 var success = function (a) {
155 document.getElementById('compassHeading').innerHTML = roundNumber(a.magneticHeading);
156 };
157
158 // Fail callback
159 var fail = function (e) {
160 console.log("watchCompass fail callback with error code " + e);
161 stopCompass();
162 setCompassStatus(e);
163 };
164
165 // Stop compass if running
166 stopCompass();
167
168 // Update heading every 1 sec
169 var opt = {};
170 opt.frequency = 1000;
171 watchCompassId = navigator.compass.watchHeading(success, fail, opt);
172
173 setCompassStatus("Running");
174 };
175
176 /**
177 * Stop watching the acceleration
178 */
179 var stopCompass = function () {
180 setCompassStatus("Stopped");
181 if (watchCompassId) {
182 navigator.compass.clearWatch(watchCompassId);
183 watchCompassId = null;
184 }
185 };
186
187 /**
188 * Get current compass
189 */
190 var getCompass = function () {
191 console.log("getCompass()");
192
193 // Stop compass if running
194 stopCompass();
195
196 // Success callback
197 var success = function (a) {
198 document.getElementById('compassHeading').innerHTML = roundNumber(a.magneticHeading);
199 };
200
201 // Fail callback
202 var fail = function (e) {
203 console.log("getCompass fail callback with error code " + e.toString);
204 setCompassStatus(e);
205 };
206
207 // Make call
208 var opt = {};
209 navigator.compass.getCurrentHeading(success, fail, opt);
210 };
211
212 /**
213 * Set compass status
214 */
215 var setCompassStatus = function (status) {
216 document.getElementById('compass_status').innerHTML = status;
217 };
218
219 /******************************************************************************/
220
221 var orientation_tests = '<h3>iOS devices may bring up a calibration screen when initiating these tests</h3>' +
222 '<div id="getCompass"></div>' +
223 'Expected result: Will update the status box with current heading. Status will read "Stopped"' +
224 '<p/> <div id="watchCompass"></div>' +
225 'Expected result: When pressed, will start a watch on the compass and update the heading value when heading changes. Status will read "Running"' +
226 '<p/> <div id="stopCompass"></div>' +
227 'Expected result: Will clear the compass watch, so heading value will no longer be updated. Status will read "Stopped"';
228
229 contentEl.innerHTML = '<div id="info"><b>Status: </b>' +
230 '<span id="compass_status">Stopped</span>' +
231 '<table width="100%"><tr>' +
232 '<td width="33%">Heading: <span id="compassHeading"></span>' +
233 '</td></tr></table></div>' +
234 orientation_tests;
235
236 createActionButton('Get Compass', function () {
237 getCompass();
238 }, 'getCompass');
239
240 createActionButton('Start Watching Compass', function () {
241 watchCompass();
242 }, 'watchCompass');
243
244 createActionButton('Stop Watching Compass', function () {
245 stopCompass();
246 }, 'stopCompass');
247};