tests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /* jshint jasmine: true */
  22. /* global CaptureAudioOptions, CaptureImageOptions, CaptureVideoOptions, CaptureError */
  23. /* global Media, MediaFile, MediaFileData, resolveLocalFileSystemURL */
  24. exports.defineAutoTests = function () {
  25. describe('Capture (navigator.device.capture)', function () {
  26. it("capture.spec.1 should exist", function () {
  27. expect(navigator.device).toBeDefined();
  28. expect(navigator.device.capture).toBeDefined();
  29. });
  30. it("capture.spec.2 should have the correct properties ", function () {
  31. expect(navigator.device.capture.supportedAudioModes).toBeDefined();
  32. expect(navigator.device.capture.supportedImageModes).toBeDefined();
  33. expect(navigator.device.capture.supportedVideoModes).toBeDefined();
  34. });
  35. it("capture.spec.3 should contain a captureAudio function", function () {
  36. expect(navigator.device.capture.captureAudio).toBeDefined();
  37. expect(typeof navigator.device.capture.captureAudio == 'function').toBe(true);
  38. });
  39. it("capture.spec.4 should contain a captureImage function", function () {
  40. expect(navigator.device.capture.captureImage).toBeDefined();
  41. expect(typeof navigator.device.capture.captureImage == 'function').toBe(true);
  42. });
  43. it("capture.spec.5 should contain a captureVideo function", function () {
  44. expect(navigator.device.capture.captureVideo).toBeDefined();
  45. expect(typeof navigator.device.capture.captureVideo == 'function').toBe(true);
  46. });
  47. describe('CaptureAudioOptions', function () {
  48. it("capture.spec.6 CaptureAudioOptions constructor should exist", function () {
  49. var options = new CaptureAudioOptions();
  50. expect(options).toBeDefined();
  51. expect(options.limit).toBeDefined();
  52. expect(options.duration).toBeDefined();
  53. });
  54. });
  55. describe('CaptureImageOptions', function () {
  56. it("capture.spec.7 CaptureImageOptions constructor should exist", function () {
  57. var options = new CaptureImageOptions();
  58. expect(options).toBeDefined();
  59. expect(options.limit).toBeDefined();
  60. });
  61. });
  62. describe('CaptureVideoOptions', function () {
  63. it("capture.spec.8 CaptureVideoOptions constructor should exist", function () {
  64. var options = new CaptureVideoOptions();
  65. expect(options).toBeDefined();
  66. expect(options.limit).toBeDefined();
  67. expect(options.duration).toBeDefined();
  68. });
  69. });
  70. describe('CaptureError interface', function () {
  71. it("capture.spec.9 CaptureError constants should be defined", function () {
  72. expect(CaptureError.CAPTURE_INTERNAL_ERR).toBe(0);
  73. expect(CaptureError.CAPTURE_APPLICATION_BUSY).toBe(1);
  74. expect(CaptureError.CAPTURE_INVALID_ARGUMENT).toBe(2);
  75. expect(CaptureError.CAPTURE_NO_MEDIA_FILES).toBe(3);
  76. });
  77. it("capture.spec.10 CaptureError properties should exist", function () {
  78. var error = new CaptureError();
  79. expect(error).toBeDefined();
  80. expect(error.code).toBeDefined();
  81. });
  82. });
  83. describe('MediaFileData', function () {
  84. it("capture.spec.11 MediaFileData constructor should exist", function () {
  85. var fileData = new MediaFileData();
  86. expect(fileData).toBeDefined();
  87. expect(fileData.bitrate).toBeDefined();
  88. expect(fileData.codecs).toBeDefined();
  89. expect(fileData.duration).toBeDefined();
  90. expect(fileData.height).toBeDefined();
  91. expect(fileData.width).toBeDefined();
  92. });
  93. });
  94. describe('MediaFile', function () {
  95. it("capture.spec.12 MediaFile constructor should exist", function () {
  96. var fileData = new MediaFile();
  97. expect(fileData).toBeDefined();
  98. expect(fileData.name).toBeDefined();
  99. expect(fileData.type).toBeDefined();
  100. expect(fileData.lastModifiedDate).toBeDefined();
  101. expect(fileData.size).toBeDefined();
  102. });
  103. });
  104. });
  105. };
  106. /******************************************************************************/
  107. /******************************************************************************/
  108. /******************************************************************************/
  109. exports.defineManualTests = function (contentEl, createActionButton) {
  110. var pageStartTime = +new Date();
  111. function log(value) {
  112. document.getElementById('camera_status').textContent += (new Date() - pageStartTime) / 1000 + ': ' + value + '\n';
  113. console.log(value);
  114. }
  115. function captureAudioWin(mediaFiles) {
  116. var path = mediaFiles[0].fullPath;
  117. log('Audio captured: ' + path);
  118. var m = new Media(path);
  119. m.play();
  120. }
  121. function captureAudioFail(e) {
  122. log('Error getting audio: ' + e.code);
  123. }
  124. function getAudio() {
  125. clearStatus();
  126. var options = { limit: 1, duration: 10 };
  127. navigator.device.capture.captureAudio(captureAudioWin, captureAudioFail, options);
  128. }
  129. function captureImageWin(mediaFiles) {
  130. var path = mediaFiles[0].fullPath;
  131. // Necessary since windows doesn't allow file URLs for <img> elements
  132. if (cordova.platformId == 'windows' || cordova.platformId == 'windows8' || cordova.platformId === 'browser') {
  133. path = mediaFiles[0].localURL;
  134. }
  135. log('Image captured: ' + path);
  136. document.getElementById('camera_image').src = path;
  137. }
  138. function captureImageFail(e) {
  139. log('Error getting image: ' + e.code);
  140. }
  141. function getImage() {
  142. clearStatus();
  143. var options = { limit: 1 };
  144. navigator.device.capture.captureImage(captureImageWin, captureImageFail, options);
  145. }
  146. function captureVideoWin(mediaFiles) {
  147. var path = mediaFiles[0].fullPath;
  148. log('Video captured: ' + path);
  149. // need to inject the video element into the html
  150. // doesn't seem to work if you have a pre-existing video element and
  151. // add in a source tag
  152. var vid = document.createElement('video');
  153. vid.id = "theVideo";
  154. vid.width = "320";
  155. vid.height = "240";
  156. vid.controls = "true";
  157. var source_vid = document.createElement('source');
  158. source_vid.id = "theSource";
  159. source_vid.src = path;
  160. vid.appendChild(source_vid);
  161. document.getElementById('video_container').appendChild(vid);
  162. }
  163. function captureVideoFail(e) {
  164. log('Error getting video: ' + e.code);
  165. }
  166. function getVideo() {
  167. clearStatus();
  168. var options = { limit: 1, duration: 10 };
  169. navigator.device.capture.captureVideo(captureVideoWin, captureVideoFail, options);
  170. }
  171. function resolveMediaFileURL(mediaFile, callback) {
  172. resolveLocalFileSystemURL(mediaFile.localURL, function (entry) {
  173. log("Resolved by URL: " + mediaFile.localURL);
  174. if (callback) callback();
  175. }, function (err) {
  176. log("Failed to resolve by URL: " + mediaFile.localURL);
  177. log("Error: " + JSON.stringify(err));
  178. if (callback) callback();
  179. });
  180. }
  181. function resolveMediaFile(mediaFile, callback) {
  182. resolveLocalFileSystemURL(mediaFile.fullPath, function (entry) {
  183. log("Resolved by path: " + mediaFile.fullPath);
  184. if (callback) callback();
  185. }, function (err) {
  186. log("Failed to resolve by path: " + mediaFile.fullPath);
  187. log("Error: " + JSON.stringify(err));
  188. if (callback) callback();
  189. });
  190. }
  191. function resolveVideo() {
  192. clearStatus();
  193. var options = { limit: 1, duration: 5 };
  194. navigator.device.capture.captureVideo(function (mediaFiles) {
  195. captureVideoWin(mediaFiles);
  196. resolveMediaFile(mediaFiles[0], function () {
  197. resolveMediaFileURL(mediaFiles[0]);
  198. });
  199. }, captureVideoFail, options);
  200. }
  201. function clearStatus() {
  202. document.getElementById('camera_status').innerHTML = '';
  203. document.getElementById('camera_image').src = 'about:blank';
  204. }
  205. /******************************************************************************/
  206. contentEl.innerHTML = '<div id="info" style="white-space: pre-wrap">' +
  207. '<b>Status:</b> <div id="camera_status"></div>' +
  208. 'img: <img width="100" id="camera_image">' +
  209. 'video: <div id="video_container"></div>' +
  210. '</div><div id="audio"></div>' +
  211. 'Expected result: Audio recorder will come up. Press record button to record for 10 seconds. Press Done. Status box will update with audio file and automatically play recording.' +
  212. '<p/> <div id="image"></div>' +
  213. 'Expected result: Status box will update with image just taken.' +
  214. '<p/> <div id="video"></div>' +
  215. 'Expected result: Record 10 second video. Status box will update with video file that you can play.' +
  216. '<p/> <div id="video_and_resolve"></div>' +
  217. 'Expected result: Record 5 second video. Status box will show that URL was resolved and video will get added at the bottom of the status box for playback.';
  218. createActionButton('Capture 10 sec of audio and play', function () {
  219. getAudio();
  220. }, 'audio');
  221. createActionButton('Capture 1 image', function () {
  222. getImage();
  223. }, 'image');
  224. createActionButton('Capture 10 sec of video', function () {
  225. getVideo();
  226. }, 'video');
  227. createActionButton('Capture 5 sec of video and resolve', function () {
  228. resolveVideo();
  229. }, 'video_and_resolve');
  230. };