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