tests.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. /* eslint-env jasmine */
  22. /* global WinJS, device */
  23. exports.defineAutoTests = function () {
  24. var fail = function (done, context, message) {
  25. // prevents done() to be called several times
  26. if (context) {
  27. if (context.done) return;
  28. context.done = true;
  29. }
  30. if (message) {
  31. expect(false).toBe(true, message);
  32. } else {
  33. expect(false).toBe(true);
  34. }
  35. // watchPosition could call its callback sync (before returning the value)
  36. // so we invoke done async to make sure we know watcher id to .clear in afterEach
  37. setTimeout(function () {
  38. done();
  39. });
  40. };
  41. var succeed = function (done, context) {
  42. // prevents done() to be called several times
  43. if (context) {
  44. if (context.done) return;
  45. context.done = true;
  46. }
  47. expect(true).toBe(true);
  48. // watchPosition could call its callback sync (before returning the value)
  49. // so we invoke done async to make sure we know watcher id to .clear in afterEach
  50. setTimeout(function () {
  51. done();
  52. });
  53. };
  54. // On Windows, some tests prompt user for permission to use geolocation and interrupt autotests run
  55. var isWindowsStore = (cordova.platformId === 'windows8') || (cordova.platformId === 'windows' && !WinJS.Utilities.isPhone); // eslint-disable-line no-undef
  56. var majorDeviceVersion = null;
  57. var versionRegex = /(\d)\..+/.exec(device.version);
  58. if (versionRegex !== null) {
  59. majorDeviceVersion = Number(versionRegex[1]);
  60. }
  61. // Starting from Android 6.0 there are confirmation dialog which prevents us from running auto tests in silent mode (user interaction needed)
  62. // Also, Android emulator doesn't provide geo fix without manual interactions or mocks
  63. var skipAndroid = cordova.platformId === 'android' && (device.isVirtual || majorDeviceVersion >= 6); // eslint-disable-line no-undef
  64. var isIOSSim = false; // if iOS simulator does not have a location set, it will fail.
  65. describe('Geolocation (navigator.geolocation)', function () {
  66. it('geolocation.spec.1 should exist', function () {
  67. expect(navigator.geolocation).toBeDefined();
  68. });
  69. it('geolocation.spec.2 should contain a getCurrentPosition function', function () {
  70. expect(typeof navigator.geolocation.getCurrentPosition).toBeDefined();
  71. expect(typeof navigator.geolocation.getCurrentPosition === 'function').toBe(true);
  72. });
  73. it('geolocation.spec.3 should contain a watchPosition function', function () {
  74. expect(typeof navigator.geolocation.watchPosition).toBeDefined();
  75. expect(typeof navigator.geolocation.watchPosition === 'function').toBe(true);
  76. });
  77. it('geolocation.spec.4 should contain a clearWatch function', function () {
  78. expect(typeof navigator.geolocation.clearWatch).toBeDefined();
  79. expect(typeof navigator.geolocation.clearWatch === 'function').toBe(true);
  80. });
  81. });
  82. describe('getCurrentPosition method', function () {
  83. describe('error callback', function () {
  84. it('geolocation.spec.5 should be called if we set timeout to 0 and maximumAge to a very small number', function (done) {
  85. if (isWindowsStore || skipAndroid) {
  86. pending();
  87. }
  88. navigator.geolocation.getCurrentPosition(
  89. fail.bind(null, done),
  90. succeed.bind(null, done),
  91. {
  92. maximumAge: 0,
  93. timeout: 0
  94. });
  95. });
  96. it('geolocation.spec.9 on failure should return PositionError object with error code constants', function (done) {
  97. if (isWindowsStore || skipAndroid) {
  98. pending();
  99. }
  100. navigator.geolocation.getCurrentPosition(
  101. fail.bind(this, done),
  102. function (gpsError) {
  103. // W3C specs: http://dev.w3.org/geo/api/spec-source.html#position_error_interface
  104. expect(gpsError.PERMISSION_DENIED).toBe(1);
  105. expect(gpsError.POSITION_UNAVAILABLE).toBe(2);
  106. expect(gpsError.TIMEOUT).toBe(3);
  107. done();
  108. },
  109. {
  110. maximumAge: 0,
  111. timeout: 0
  112. });
  113. });
  114. });
  115. describe('success callback', function () {
  116. it('geolocation.spec.6 should be called with a Position object', function (done) {
  117. if (isWindowsStore || skipAndroid) {
  118. pending();
  119. }
  120. navigator.geolocation.getCurrentPosition(function (p) {
  121. expect(p.coords).toBeDefined();
  122. expect(p.timestamp).toBeDefined();
  123. done();
  124. }, function (err) {
  125. if (err.message && err.message.indexOf('kCLErrorDomain') > -1) {
  126. console.log('Error: Location not set in simulator, tests will fail.');
  127. expect(true).toBe(true);
  128. isIOSSim = true;
  129. done();
  130. } else {
  131. fail(done);
  132. }
  133. },
  134. {
  135. maximumAge: (5 * 60 * 1000) // 5 minutes maximum age of cached position
  136. });
  137. }, 25000); // first geolocation call can take several seconds on some devices
  138. });
  139. });
  140. describe('watchPosition method', function () {
  141. beforeEach(function (done) {
  142. // This timeout is set to lessen the load on platform's geolocation services
  143. // which were causing occasional test failures
  144. setTimeout(function () {
  145. done();
  146. }, 100);
  147. });
  148. describe('error callback', function () {
  149. var errorWatch = null;
  150. afterEach(function () {
  151. navigator.geolocation.clearWatch(errorWatch);
  152. });
  153. it('geolocation.spec.7 should be called if we set timeout to 0 and maximumAge to a very small number', function (done) {
  154. if (isWindowsStore || skipAndroid) {
  155. pending();
  156. }
  157. var context = this;
  158. errorWatch = navigator.geolocation.watchPosition(
  159. fail.bind(null, done, context, 'Unexpected win'),
  160. succeed.bind(null, done, context),
  161. {
  162. maximumAge: 0,
  163. timeout: 0
  164. });
  165. });
  166. it('geolocation.spec.10 on failure should return PositionError object with error code constants', function (done) {
  167. if (isWindowsStore || skipAndroid) {
  168. pending();
  169. }
  170. var context = this;
  171. errorWatch = navigator.geolocation.watchPosition(
  172. fail.bind(this, done, context, 'Unexpected win'),
  173. function (gpsError) {
  174. if (context.done) return;
  175. context.done = true;
  176. // W3C specs: http://dev.w3.org/geo/api/spec-source.html#position_error_interface
  177. expect(gpsError.PERMISSION_DENIED).toBe(1);
  178. expect(gpsError.POSITION_UNAVAILABLE).toBe(2);
  179. expect(gpsError.TIMEOUT).toBe(3);
  180. done();
  181. },
  182. {
  183. maximumAge: 0,
  184. timeout: 0
  185. });
  186. });
  187. });
  188. describe('success callback', function () {
  189. var successWatch = null;
  190. afterEach(function () {
  191. navigator.geolocation.clearWatch(successWatch);
  192. });
  193. it('geolocation.spec.8 should be called with a Position object', function (done) {
  194. if (isWindowsStore || skipAndroid || isIOSSim) {
  195. pending();
  196. }
  197. var context = this;
  198. successWatch = navigator.geolocation.watchPosition(
  199. function (p) {
  200. // prevents done() to be called several times
  201. if (context.done) return;
  202. context.done = true;
  203. expect(p.coords).toBeDefined();
  204. expect(p.timestamp).toBeDefined();
  205. // callback could be called sync so we invoke done async to make sure we know watcher id to .clear in afterEach
  206. setTimeout(function () {
  207. done();
  208. });
  209. },
  210. fail.bind(null, done, context, 'Unexpected fail callback'),
  211. {
  212. maximumAge: (5 * 60 * 1000) // 5 minutes maximum age of cached position
  213. });
  214. expect(successWatch).toBeDefined();
  215. });
  216. });
  217. });
  218. };
  219. /******************************************************************************/
  220. /******************************************************************************/
  221. /******************************************************************************/
  222. exports.defineManualTests = function (contentEl, createActionButton) {
  223. var watchLocationId = null;
  224. /**
  225. * Set location status
  226. */
  227. function setLocationStatus (status) {
  228. document.getElementById('location_status').innerHTML = status;
  229. }
  230. function setLocationDetails (p) {
  231. var date = (new Date(p.timestamp));
  232. document.getElementById('latitude').innerHTML = p.coords.latitude;
  233. document.getElementById('longitude').innerHTML = p.coords.longitude;
  234. document.getElementById('altitude').innerHTML = p.coords.altitude;
  235. document.getElementById('accuracy').innerHTML = p.coords.accuracy;
  236. document.getElementById('heading').innerHTML = p.coords.heading;
  237. document.getElementById('speed').innerHTML = p.coords.speed;
  238. document.getElementById('altitude_accuracy').innerHTML = p.coords.altitudeAccuracy;
  239. document.getElementById('timestamp').innerHTML = date.toDateString() + ' ' + date.toTimeString();
  240. }
  241. /**
  242. * Stop watching the location
  243. */
  244. function stopLocation () {
  245. var geo = navigator.geolocation;
  246. if (!geo) {
  247. alert('navigator.geolocation object is missing.'); // eslint-disable-line no-undef
  248. return;
  249. }
  250. setLocationStatus('Stopped');
  251. if (watchLocationId) {
  252. geo.clearWatch(watchLocationId);
  253. watchLocationId = null;
  254. }
  255. }
  256. /**
  257. * Start watching location
  258. */
  259. var watchLocation = function () {
  260. var geo = navigator.geolocation;
  261. if (!geo) {
  262. alert('navigator.geolocation object is missing.'); // eslint-disable-line no-undef
  263. return;
  264. }
  265. // Success callback
  266. var success = function (p) {
  267. setLocationDetails(p);
  268. };
  269. // Fail callback
  270. var fail = function (e) {
  271. console.log('watchLocation fail callback with error code ' + e);
  272. stopLocation(geo);
  273. };
  274. // Get location
  275. watchLocationId = geo.watchPosition(success, fail, { enableHighAccuracy: true });
  276. setLocationStatus('Running');
  277. };
  278. /**
  279. * Get current location
  280. */
  281. var getLocation = function (opts) {
  282. var geo = navigator.geolocation;
  283. if (!geo) {
  284. alert('navigator.geolocation object is missing.'); // eslint-disable-line no-undef
  285. return;
  286. }
  287. // Stop location if running
  288. stopLocation(geo);
  289. // Success callback
  290. var success = function (p) {
  291. setLocationDetails(p);
  292. setLocationStatus('Done');
  293. };
  294. // Fail callback
  295. var fail = function (e) {
  296. console.log('getLocation fail callback with error code ' + e.code);
  297. setLocationStatus('Error: ' + e.code);
  298. };
  299. setLocationStatus('Retrieving location...');
  300. // Get location
  301. geo.getCurrentPosition(success, fail, opts || { enableHighAccuracy: true }); //, {timeout: 10000});
  302. };
  303. /******************************************************************************/
  304. var location_div = '<div id="info">' +
  305. '<b>Status:</b> <span id="location_status">Stopped</span>' +
  306. '<table width="100%">';
  307. var latitude = '<tr>' +
  308. '<td><b>Latitude:</b></td>' +
  309. '<td id="latitude">&nbsp;</td>' +
  310. '<td>(decimal degrees) geographic coordinate [<a href="http://dev.w3.org/geo/api/spec-source.html#lat">#ref]</a></td>' +
  311. '</tr>';
  312. var longitude = '<tr>' +
  313. '<td><b>Longitude:</b></td>' +
  314. '<td id="longitude">&nbsp;</td>' +
  315. '<td>(decimal degrees) geographic coordinate [<a href="http://dev.w3.org/geo/api/spec-source.html#lat">#ref]</a></td>' +
  316. '</tr>';
  317. var altitude = '<tr>' +
  318. '<td><b>Altitude:</b></td>' +
  319. '<td id="altitude">&nbsp;</td>' +
  320. '<td>null if not supported;<br>' +
  321. '(meters) height above the [<a href="http://dev.w3.org/geo/api/spec-source.html#ref-wgs">WGS84</a>] ellipsoid. [<a href="http://dev.w3.org/geo/api/spec-source.html#altitude">#ref]</a></td>' +
  322. '</tr>';
  323. var accuracy = '<tr>' +
  324. '<td><b>Accuracy:</b></td>' +
  325. '<td id="accuracy">&nbsp;</td>' +
  326. '<td>(meters; non-negative; 95% confidence level) the accuracy level of the latitude and longitude coordinates. [<a href="http://dev.w3.org/geo/api/spec-source.html#accuracy">#ref]</a></td>' +
  327. '</tr>';
  328. var heading = '<tr>' +
  329. '<td><b>Heading:</b></td>' +
  330. '<td id="heading">&nbsp;</td>' +
  331. '<td>null if not supported;<br>' +
  332. 'NaN if speed == 0;<br>' +
  333. '(degrees; 0° ≤ heading < 360°) direction of travel of the hosting device- counting clockwise relative to the true north. [<a href="http://dev.w3.org/geo/api/spec-source.html#heading">#ref]</a></td>' +
  334. '</tr>';
  335. var speed = '<tr>' +
  336. '<td><b>Speed:</b></td>' +
  337. '<td id="speed">&nbsp;</td>' +
  338. '<td>null if not supported;<br>' +
  339. '(meters per second; non-negative) magnitude of the horizontal component of the hosting device current velocity. [<a href="http://dev.w3.org/geo/api/spec-source.html#speed">#ref]</a></td>' +
  340. '</tr>';
  341. var altitude_accuracy = '<tr>' +
  342. '<td><b>Altitude Accuracy:</b></td>' +
  343. '<td id="altitude_accuracy">&nbsp;</td>' +
  344. '<td>null if not supported;<br>(meters; non-negative; 95% confidence level) the accuracy level of the altitude. [<a href="http://dev.w3.org/geo/api/spec-source.html#altitude-accuracy">#ref]</a></td>' +
  345. '</tr>';
  346. var time = '<tr>' +
  347. '<td><b>Time:</b></td>' +
  348. '<td id="timestamp">&nbsp;</td>' +
  349. '<td>(DOMTimeStamp) when the position was acquired [<a href="http://dev.w3.org/geo/api/spec-source.html#timestamp">#ref]</a></td>' +
  350. '</tr>' +
  351. '</table>' +
  352. '</div>';
  353. var actions =
  354. '<div id="cordova-getLocation"></div>' +
  355. 'Expected result: Will update all applicable values in status box for current location. Status will read Retrieving Location (may not see this if location is retrieved immediately) then Done.' +
  356. '<p/> <div id="cordova-watchLocation"></div>' +
  357. 'Expected result: Will update all applicable values in status box for current location and update as location changes. Status will read Running.' +
  358. '<p/> <div id="cordova-stopLocation"></div>' +
  359. 'Expected result: Will stop watching the location so values will not be updated. Status will read Stopped.' +
  360. '<p/> <div id="cordova-getOld"></div>' +
  361. 'Expected result: Will update location values with a cached position that is up to 30 seconds old. Verify with time value. Status will read Done.';
  362. var values_info =
  363. '<h3>Details about each value are listed below in the status box</h3>';
  364. var note =
  365. '<h3>Allow use of current location, if prompted</h3>';
  366. contentEl.innerHTML = values_info + location_div + latitude + longitude + altitude + accuracy + heading + speed +
  367. altitude_accuracy + time + note + actions;
  368. createActionButton('Get Location', function () {
  369. getLocation();
  370. }, 'cordova-getLocation');
  371. createActionButton('Start Watching Location', function () {
  372. watchLocation();
  373. }, 'cordova-watchLocation');
  374. createActionButton('Stop Watching Location', function () {
  375. stopLocation();
  376. }, 'cordova-stopLocation');
  377. createActionButton('Get Location Up to 30 Sec Old', function () {
  378. getLocation({ maximumAge: 30000 });
  379. }, 'cordova-getOld');
  380. };