You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4012 lines
234 KiB

  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, * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. */
  20. /* jshint jasmine: true */
  21. /* global WebKitBlobBuilder */
  22. exports.defineAutoTests = function () {
  23. var isBrowser = (cordova.platformId === "browser");
  24. // Use feature detection to determine current browser instead of checking user-agent
  25. var isChrome = isBrowser && window.webkitRequestFileSystem && window.webkitResolveLocalFileSystemURL;
  26. var isIE = isBrowser && (window.msIndexedDB);
  27. var isIndexedDBShim = isBrowser && !isChrome; // Firefox and IE for example
  28. var isWindows = (cordova.platformId === "windows" || cordova.platformId === "windows8");
  29. var MEDIUM_TIMEOUT = 15000;
  30. describe('File API', function () {
  31. // Adding a Jasmine helper matcher, to report errors when comparing to FileError better.
  32. var fileErrorMap = {
  33. 1 : 'NOT_FOUND_ERR',
  34. 2 : 'SECURITY_ERR',
  35. 3 : 'ABORT_ERR',
  36. 4 : 'NOT_READABLE_ERR',
  37. 5 : 'ENCODING_ERR',
  38. 6 : 'NO_MODIFICATION_ALLOWED_ERR',
  39. 7 : 'INVALID_STATE_ERR',
  40. 8 : 'SYNTAX_ERR',
  41. 9 : 'INVALID_MODIFICATION_ERR',
  42. 10 : 'QUOTA_EXCEEDED_ERR',
  43. 11 : 'TYPE_MISMATCH_ERR',
  44. 12 : 'PATH_EXISTS_ERR'
  45. },
  46. root,
  47. temp_root,
  48. persistent_root;
  49. beforeEach(function (done) {
  50. // Custom Matchers
  51. jasmine.Expectation.addMatchers({
  52. toBeFileError : function () {
  53. return {
  54. compare : function (error, code) {
  55. var pass = error.code === code;
  56. return {
  57. pass : pass,
  58. message : 'Expected FileError with code ' + fileErrorMap[error.code] + ' (' + error.code + ') to be ' + fileErrorMap[code] + '(' + code + ')'
  59. };
  60. }
  61. };
  62. },
  63. toCanonicallyMatch : function () {
  64. return {
  65. compare : function (currentPath, path) {
  66. var a = path.split("/").join("").split("\\").join(""),
  67. b = currentPath.split("/").join("").split("\\").join(""),
  68. pass = a === b;
  69. return {
  70. pass : pass,
  71. message : 'Expected paths to match : ' + path + ' should be ' + currentPath
  72. };
  73. }
  74. };
  75. },
  76. toFailWithMessage : function () {
  77. return {
  78. compare : function (error, message) {
  79. var pass = false;
  80. return {
  81. pass : pass,
  82. message : message
  83. };
  84. }
  85. };
  86. },
  87. toBeDataUrl: function () {
  88. return {
  89. compare : function (url) {
  90. var pass = false;
  91. // "data:application/octet-stream;base64,"
  92. var header = url.substr(0, url.indexOf(','));
  93. var headerParts = header.split(/[:;]/);
  94. if (headerParts.length === 3 &&
  95. headerParts[0] === 'data' &&
  96. headerParts[2] === 'base64') {
  97. pass = true;
  98. }
  99. var message = 'Expected ' + url + ' to be a valid data url. ' + header + ' is not valid header for data uris';
  100. return {
  101. pass : pass,
  102. message : message
  103. };
  104. }
  105. };
  106. }
  107. });
  108. //Define global variables
  109. var onError = function (e) {
  110. console.log('[ERROR] Problem setting up root filesystem for test running! Error to follow.');
  111. console.log(JSON.stringify(e));
  112. };
  113. window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
  114. root = fileSystem.root;
  115. // set in file.tests.js
  116. persistent_root = root;
  117. window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem) {
  118. temp_root = fileSystem.root;
  119. // set in file.tests.js
  120. done();
  121. }, onError);
  122. }, onError);
  123. });
  124. // HELPER FUNCTIONS
  125. // deletes specified file or directory
  126. var deleteEntry = function (name, success, error) {
  127. // deletes entry, if it exists
  128. // entry.remove success callback is required: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#the-entry-interface
  129. success = success || function() {};
  130. error = error || failed.bind(null, success, 'deleteEntry failed.');
  131. window.resolveLocalFileSystemURL(root.toURL() + '/' + name, function (entry) {
  132. if (entry.isDirectory === true) {
  133. entry.removeRecursively(success, error);
  134. } else {
  135. entry.remove(success, error);
  136. }
  137. }, success);
  138. };
  139. // deletes file, if it exists, then invokes callback
  140. var deleteFile = function (fileName, callback) {
  141. // entry.remove success callback is required: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#the-entry-interface
  142. callback = callback || function() {};
  143. root.getFile(fileName, null, // remove file system entry
  144. function (entry) {
  145. entry.remove(callback, function () {
  146. console.log('[ERROR] deleteFile cleanup method invoked fail callback.');
  147. });
  148. }, // doesn't exist
  149. callback);
  150. };
  151. // deletes and re-creates the specified file
  152. var createFile = function (fileName, success, error) {
  153. deleteEntry(fileName, function () {
  154. root.getFile(fileName, {
  155. create : true
  156. }, success, error);
  157. }, error);
  158. };
  159. // deletes and re-creates the specified directory
  160. var createDirectory = function (dirName, success, error) {
  161. deleteEntry(dirName, function () {
  162. root.getDirectory(dirName, {
  163. create : true
  164. }, success, error);
  165. }, error);
  166. };
  167. function failed(done, msg, error) {
  168. var info = typeof msg == 'undefined' ? 'Unexpected error callback' : msg;
  169. var codeMsg = (error && error.code) ? (': ' + fileErrorMap[error.code]) : '';
  170. expect(true).toFailWithMessage(info + '\n' + JSON.stringify(error) + codeMsg);
  171. done();
  172. }
  173. var succeed = function (done, msg) {
  174. var info = typeof msg == 'undefined' ? 'Unexpected success callback' : msg;
  175. expect(true).toFailWithMessage(info);
  176. done();
  177. };
  178. var joinURL = function (base, extension) {
  179. if (base.charAt(base.length - 1) !== '/' && extension.charAt(0) !== '/') {
  180. return base + '/' + extension;
  181. }
  182. if (base.charAt(base.length - 1) === '/' && extension.charAt(0) === '/') {
  183. return base + extension.substring(1);
  184. }
  185. return base + extension;
  186. };
  187. describe('FileError object', function () {
  188. it("file.spec.1 should define FileError constants", function () {
  189. expect(FileError.NOT_FOUND_ERR).toBe(1);
  190. expect(FileError.SECURITY_ERR).toBe(2);
  191. expect(FileError.ABORT_ERR).toBe(3);
  192. expect(FileError.NOT_READABLE_ERR).toBe(4);
  193. expect(FileError.ENCODING_ERR).toBe(5);
  194. expect(FileError.NO_MODIFICATION_ALLOWED_ERR).toBe(6);
  195. expect(FileError.INVALID_STATE_ERR).toBe(7);
  196. expect(FileError.SYNTAX_ERR).toBe(8);
  197. expect(FileError.INVALID_MODIFICATION_ERR).toBe(9);
  198. expect(FileError.QUOTA_EXCEEDED_ERR).toBe(10);
  199. expect(FileError.TYPE_MISMATCH_ERR).toBe(11);
  200. expect(FileError.PATH_EXISTS_ERR).toBe(12);
  201. });
  202. });
  203. describe('LocalFileSystem', function () {
  204. it("file.spec.2 should define LocalFileSystem constants", function () {
  205. expect(LocalFileSystem.TEMPORARY).toBe(0);
  206. expect(LocalFileSystem.PERSISTENT).toBe(1);
  207. });
  208. describe('window.requestFileSystem', function () {
  209. it("file.spec.3 should be defined", function () {
  210. expect(window.requestFileSystem).toBeDefined();
  211. });
  212. it("file.spec.4 should be able to retrieve a PERSISTENT file system", function (done) {
  213. var win = function (fileSystem) {
  214. expect(fileSystem).toBeDefined();
  215. expect(fileSystem.name).toBeDefined();
  216. if (isChrome) {
  217. expect(fileSystem.name).toContain("Persistent");
  218. } else {
  219. expect(fileSystem.name).toBe("persistent");
  220. }
  221. expect(fileSystem.root).toBeDefined();
  222. expect(fileSystem.root.filesystem).toBeDefined();
  223. // Shouldn't use cdvfile by default.
  224. expect(fileSystem.root.toURL()).not.toMatch(/^cdvfile:/);
  225. // All DirectoryEntry URLs should always have a trailing slash.
  226. expect(fileSystem.root.toURL()).toMatch(/\/$/);
  227. done();
  228. };
  229. // Request a little bit of space on the filesystem, unless we're running in a browser where that could cause a prompt.
  230. var spaceRequired = isBrowser ? 0 : 1024;
  231. // retrieve PERSISTENT file system
  232. window.requestFileSystem(LocalFileSystem.PERSISTENT, spaceRequired, win, failed.bind(null, done, 'window.requestFileSystem - Error retrieving PERSISTENT file system'));
  233. });
  234. it("file.spec.5 should be able to retrieve a TEMPORARY file system", function (done) {
  235. var win = function (fileSystem) {
  236. expect(fileSystem).toBeDefined();
  237. if (isChrome) {
  238. expect(fileSystem.name).toContain("Temporary");
  239. } else {
  240. expect(fileSystem.name).toBe("temporary");
  241. }
  242. expect(fileSystem.root).toBeDefined();
  243. expect(fileSystem.root.filesystem).toBeDefined();
  244. expect(fileSystem.root.filesystem).toBe(fileSystem);
  245. done();
  246. };
  247. //retrieve TEMPORARY file system
  248. window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, win, failed.bind(null, done, 'window.requestFileSystem - Error retrieving TEMPORARY file system'));
  249. });
  250. it("file.spec.6 should error if you request a file system that is too large", function (done) {
  251. if (isBrowser) {
  252. /*window.requestFileSystem TEMPORARY and PERSISTENT filesystem quota is not limited in Chrome.
  253. Firefox filesystem size is not limited but every 50MB request user permission.
  254. IE10 allows up to 10mb of combined AppCache and IndexedDB used in implementation
  255. of filesystem without prompting, once you hit that level you will be asked if you
  256. want to allow it to be increased up to a max of 250mb per site.
  257. So `size` parameter for `requestFileSystem` function does not affect on filesystem in Firefox and IE.*/
  258. pending();
  259. }
  260. var fail = function (error) {
  261. expect(error).toBeDefined();
  262. expect(error).toBeFileError(FileError.QUOTA_EXCEEDED_ERR);
  263. done();
  264. };
  265. //win = createWin('window.requestFileSystem');
  266. // Request the file system
  267. window.requestFileSystem(LocalFileSystem.TEMPORARY, 1000000000000000, failed.bind(null, done, 'window.requestFileSystem - Error retrieving TEMPORARY file system'), fail);
  268. });
  269. it("file.spec.7 should error out if you request a file system that does not exist", function (done) {
  270. var fail = function (error) {
  271. expect(error).toBeDefined();
  272. if (isChrome) {
  273. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of SYNTAX_ERR(code: 8)
  274. on requesting of a non-existant filesystem.*/
  275. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  276. } else {
  277. expect(error).toBeFileError(FileError.SYNTAX_ERR);
  278. }
  279. done();
  280. };
  281. // Request the file system
  282. window.requestFileSystem(-1, 0, succeed.bind(null, done, 'window.requestFileSystem'), fail);
  283. });
  284. });
  285. describe('window.resolveLocalFileSystemURL', function () {
  286. it("file.spec.8 should be defined", function () {
  287. expect(window.resolveLocalFileSystemURL).toBeDefined();
  288. });
  289. it("file.spec.9 should resolve a valid file name", function (done) {
  290. var fileName = 'file.spec.9';
  291. var win = function (fileEntry) {
  292. expect(fileEntry).toBeDefined();
  293. expect(fileEntry.isFile).toBe(true);
  294. expect(fileEntry.isDirectory).toBe(false);
  295. expect(fileEntry.name).toCanonicallyMatch(fileName);
  296. expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
  297. expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
  298. // Clean-up
  299. deleteEntry(fileName, done);
  300. };
  301. createFile(fileName, function (entry) {
  302. window.resolveLocalFileSystemURL(entry.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toURL()));
  303. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  304. });
  305. it("file.spec.9.1 should resolve a file even with a terminating slash", function (done) {
  306. var fileName = 'file.spec.9.1';
  307. var win = function (fileEntry) {
  308. expect(fileEntry).toBeDefined();
  309. expect(fileEntry.isFile).toBe(true);
  310. expect(fileEntry.isDirectory).toBe(false);
  311. expect(fileEntry.name).toCanonicallyMatch(fileName);
  312. expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
  313. expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
  314. // Clean-up
  315. deleteEntry(fileName, done);
  316. };
  317. createFile(fileName, function (entry) {
  318. var entryURL = entry.toURL() + '/';
  319. window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entryURL));
  320. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  321. });
  322. it("file.spec.9.5 should resolve a directory", function (done) {
  323. var fileName = 'file.spec.9.5';
  324. var win = function (fileEntry) {
  325. expect(fileEntry).toBeDefined();
  326. expect(fileEntry.isFile).toBe(false);
  327. expect(fileEntry.isDirectory).toBe(true);
  328. expect(fileEntry.name).toCanonicallyMatch(fileName);
  329. expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
  330. expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
  331. // cleanup
  332. deleteEntry(fileName, done);
  333. };
  334. function gotDirectory(entry) {
  335. // lookup file system entry
  336. window.resolveLocalFileSystemURL(entry.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entry.toURL()));
  337. }
  338. createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
  339. });
  340. it("file.spec.9.6 should resolve a directory even without a terminating slash", function (done) {
  341. var fileName = 'file.spec.9.6';
  342. var win = function (fileEntry) {
  343. expect(fileEntry).toBeDefined();
  344. expect(fileEntry.isFile).toBe(false);
  345. expect(fileEntry.isDirectory).toBe(true);
  346. expect(fileEntry.name).toCanonicallyMatch(fileName);
  347. expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
  348. expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
  349. // cleanup
  350. deleteEntry(fileName, done);
  351. };
  352. function gotDirectory(entry) {
  353. // lookup file system entry
  354. var entryURL = entry.toURL();
  355. entryURL = entryURL.substring(0, entryURL.length - 1);
  356. window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entryURL));
  357. }
  358. createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
  359. });
  360. it("file.spec.9.7 should resolve a file with valid nativeURL", function (done) {
  361. var fileName = "de.create.file",
  362. win = function (entry) {
  363. var path = entry.nativeURL.split('///')[1];
  364. expect(/\/{2,}/.test(path)).toBeFalsy();
  365. // cleanup
  366. deleteEntry(entry.name, done);
  367. };
  368. root.getFile(fileName, {
  369. create: true
  370. }, win, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName));
  371. });
  372. it("file.spec.10 resolve valid file name with parameters", function (done) {
  373. var fileName = "resolve.file.uri.params",
  374. win = function (fileEntry) {
  375. expect(fileEntry).toBeDefined();
  376. if (fileEntry.toURL().toLowerCase().substring(0, 10) === "cdvfile://") {
  377. expect(fileEntry.fullPath).toBe("/" + fileName + "?1234567890");
  378. }
  379. expect(fileEntry.name).toBe(fileName);
  380. // cleanup
  381. deleteEntry(fileName, done);
  382. };
  383. // create a new file entry
  384. createFile(fileName, function (entry) {
  385. window.resolveLocalFileSystemURL(entry.toURL() + "?1234567890", win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URI: ' + entry.toURL()));
  386. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  387. });
  388. it("file.spec.11 should error (NOT_FOUND_ERR) when resolving (non-existent) invalid file name", function (done) {
  389. var fileName = cordova.platformId === 'windowsphone' ? root.toURL() + "/" + "this.is.not.a.valid.file.txt" : joinURL(root.toURL(), "this.is.not.a.valid.file.txt"),
  390. fail = function (error) {
  391. expect(error).toBeDefined();
  392. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  393. done();
  394. };
  395. // lookup file system entry
  396. window.resolveLocalFileSystemURL(fileName, succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Error unexpected callback resolving file URI: ' + fileName), fail);
  397. });
  398. it("file.spec.12 should error (ENCODING_ERR) when resolving invalid URI with leading /", function (done) {
  399. var fileName = "/this.is.not.a.valid.url",
  400. fail = function (error) {
  401. expect(error).toBeDefined();
  402. expect(error).toBeFileError(FileError.ENCODING_ERR);
  403. done();
  404. };
  405. // lookup file system entry
  406. window.resolveLocalFileSystemURL(fileName, succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Error unexpected callback resolving file URI: ' + fileName), fail);
  407. });
  408. });
  409. });
  410. //LocalFileSystem
  411. describe('Metadata interface', function () {
  412. it("file.spec.13 should exist and have the right properties", function () {
  413. var metadata = new Metadata();
  414. expect(metadata).toBeDefined();
  415. expect(metadata.modificationTime).toBeDefined();
  416. });
  417. });
  418. describe('Flags interface', function () {
  419. it("file.spec.14 should exist and have the right properties", function () {
  420. var flags = new Flags(false, true);
  421. expect(flags).toBeDefined();
  422. expect(flags.create).toBeDefined();
  423. expect(flags.create).toBe(false);
  424. expect(flags.exclusive).toBeDefined();
  425. expect(flags.exclusive).toBe(true);
  426. });
  427. });
  428. describe('FileSystem interface', function () {
  429. it("file.spec.15 should have a root that is a DirectoryEntry", function (done) {
  430. var win = function (entry) {
  431. expect(entry).toBeDefined();
  432. expect(entry.isFile).toBe(false);
  433. expect(entry.isDirectory).toBe(true);
  434. expect(entry.name).toBeDefined();
  435. expect(entry.fullPath).toBeDefined();
  436. expect(entry.getMetadata).toBeDefined();
  437. expect(entry.moveTo).toBeDefined();
  438. expect(entry.copyTo).toBeDefined();
  439. expect(entry.toURL).toBeDefined();
  440. expect(entry.remove).toBeDefined();
  441. expect(entry.getParent).toBeDefined();
  442. expect(entry.createReader).toBeDefined();
  443. expect(entry.getFile).toBeDefined();
  444. expect(entry.getDirectory).toBeDefined();
  445. expect(entry.removeRecursively).toBeDefined();
  446. done();
  447. };
  448. window.resolveLocalFileSystemURL(root.toURL(), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URI: ' + root.toURL()));
  449. });
  450. });
  451. describe('DirectoryEntry', function () {
  452. it("file.spec.16 getFile: get Entry for file that does not exist", function (done) {
  453. var fileName = "de.no.file",
  454. fail = function (error) {
  455. expect(error).toBeDefined();
  456. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  457. done();
  458. };
  459. // create:false, exclusive:false, file does not exist
  460. root.getFile(fileName, {
  461. create : false
  462. }, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName), fail);
  463. });
  464. it("file.spec.17 getFile: create new file", function (done) {
  465. var fileName = "de.create.file",
  466. filePath = joinURL(root.fullPath, fileName),
  467. win = function (entry) {
  468. expect(entry).toBeDefined();
  469. expect(entry.isFile).toBe(true);
  470. expect(entry.isDirectory).toBe(false);
  471. expect(entry.name).toCanonicallyMatch(fileName);
  472. expect(entry.fullPath).toCanonicallyMatch(filePath);
  473. // cleanup
  474. deleteEntry(entry.name, done);
  475. };
  476. // create:true, exclusive:false, file does not exist
  477. root.getFile(fileName, {
  478. create : true
  479. }, win, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName));
  480. });
  481. it("file.spec.18 getFile: create new file (exclusive)", function (done) {
  482. var fileName = "de.create.exclusive.file",
  483. filePath = joinURL(root.fullPath, fileName),
  484. win = function (entry) {
  485. expect(entry).toBeDefined();
  486. expect(entry.isFile).toBe(true);
  487. expect(entry.isDirectory).toBe(false);
  488. expect(entry.name).toBe(fileName);
  489. expect(entry.fullPath).toCanonicallyMatch(filePath);
  490. // cleanup
  491. deleteEntry(entry.name, done);
  492. };
  493. // create:true, exclusive:true, file does not exist
  494. root.getFile(fileName, {
  495. create : true,
  496. exclusive : true
  497. }, win, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  498. });
  499. it("file.spec.19 getFile: create file that already exists", function (done) {
  500. var fileName = "de.create.existing.file",
  501. filePath = joinURL(root.fullPath, fileName);
  502. function win(entry) {
  503. expect(entry).toBeDefined();
  504. expect(entry.isFile).toBe(true);
  505. expect(entry.isDirectory).toBe(false);
  506. expect(entry.name).toCanonicallyMatch(fileName);
  507. expect(entry.fullPath).toCanonicallyMatch(filePath);
  508. // cleanup
  509. deleteEntry(entry.name, done);
  510. }
  511. function getFile(file) {
  512. // create:true, exclusive:false, file exists
  513. root.getFile(fileName, {
  514. create : true
  515. }, win, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  516. }
  517. // create file to kick off it
  518. root.getFile(fileName, {
  519. create : true
  520. }, getFile, failed.bind(null, done, 'root.getFile - Error on initial creating file: ' + fileName));
  521. });
  522. it("file.spec.20 getFile: create file that already exists (exclusive)", function (done) {
  523. var fileName = "de.create.exclusive.existing.file",
  524. existingFile;
  525. function fail(error) {
  526. expect(error).toBeDefined();
  527. if (isChrome) {
  528. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of PATH_EXISTS_ERR(code: 12)
  529. on trying to exclusively create a file, which already exists in Chrome.*/
  530. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  531. } else {
  532. expect(error).toBeFileError(FileError.PATH_EXISTS_ERR);
  533. }
  534. // cleanup
  535. deleteEntry(existingFile.name, done);
  536. }
  537. function getFile(file) {
  538. existingFile = file;
  539. // create:true, exclusive:true, file exists
  540. root.getFile(fileName, {
  541. create : true,
  542. exclusive : true
  543. }, succeed.bind(null, done, 'root.getFile - getFile function - Error unexpected callback, file should exists: ' + fileName), fail);
  544. }
  545. // create file to kick off it
  546. root.getFile(fileName, {
  547. create : true
  548. }, getFile, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  549. });
  550. it("file.spec.21 DirectoryEntry.getFile: get Entry for existing file", function (done) {
  551. var fileName = "de.get.file",
  552. filePath = joinURL(root.fullPath, fileName),
  553. win = function (entry) {
  554. expect(entry).toBeDefined();
  555. expect(entry.isFile).toBe(true);
  556. expect(entry.isDirectory).toBe(false);
  557. expect(entry.name).toCanonicallyMatch(fileName);
  558. expect(entry.fullPath).toCanonicallyMatch(filePath);
  559. expect(entry.filesystem).toBeDefined();
  560. expect(entry.filesystem).toBe(root.filesystem);
  561. //clean up
  562. deleteEntry(entry.name, done);
  563. },
  564. getFile = function (file) {
  565. // create:false, exclusive:false, file exists
  566. root.getFile(fileName, {
  567. create : false
  568. }, win, failed.bind(null, done, 'root.getFile - Error getting file entry: ' + fileName));
  569. };
  570. // create file to kick off it
  571. root.getFile(fileName, {
  572. create : true
  573. }, getFile, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  574. });
  575. it("file.spec.22 DirectoryEntry.getFile: get FileEntry for invalid path", function (done) {
  576. if (isBrowser) {
  577. /*The plugin does not follow to ["8.3 Naming restrictions"]
  578. (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions).*/
  579. pending();
  580. }
  581. var fileName = "de:invalid:path",
  582. fail = function (error) {
  583. expect(error).toBeDefined();
  584. expect(error).toBeFileError(FileError.ENCODING_ERR);
  585. done();
  586. };
  587. // create:false, exclusive:false, invalid path
  588. root.getFile(fileName, {
  589. create : false
  590. }, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName), fail);
  591. });
  592. it("file.spec.23 DirectoryEntry.getDirectory: get Entry for directory that does not exist", function (done) {
  593. var dirName = "de.no.dir",
  594. fail = function (error) {
  595. expect(error).toBeDefined();
  596. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  597. done();
  598. };
  599. // create:false, exclusive:false, directory does not exist
  600. root.getDirectory(dirName, {
  601. create : false
  602. }, succeed.bind(null, done, 'root.getDirectory - Error unexpected callback, directory should not exists: ' + dirName), fail);
  603. });
  604. it("file.spec.24 DirectoryEntry.getDirectory: create new dir with space then resolveLocalFileSystemURL", function (done) {
  605. var dirName = "de create dir";
  606. function win(directory) {
  607. expect(directory).toBeDefined();
  608. expect(directory.isFile).toBe(false);
  609. expect(directory.isDirectory).toBe(true);
  610. expect(directory.name).toCanonicallyMatch(dirName);
  611. expect(directory.fullPath).toCanonicallyMatch(joinURL(root.fullPath, dirName));
  612. // cleanup
  613. deleteEntry(directory.name, done);
  614. }
  615. function getDir(dirEntry) {
  616. expect(dirEntry.filesystem).toBeDefined();
  617. expect(dirEntry.filesystem).toBe(root.filesystem);
  618. var dirURI = dirEntry.toURL();
  619. // now encode URI and try to resolve
  620. window.resolveLocalFileSystemURL(dirURI, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - getDir function - Error resolving directory: ' + dirURI));
  621. }
  622. // create:true, exclusive:false, directory does not exist
  623. root.getDirectory(dirName, {
  624. create : true
  625. }, getDir, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  626. });
  627. // This test is excluded, and should probably be removed. Filesystem
  628. // should always be properly encoded URLs, and *not* raw paths, and it
  629. // doesn't make sense to double-encode the URLs and expect that to be
  630. // handled by the implementation.
  631. // If a particular platform uses paths internally rather than URLs, // then that platform should careful to pass them correctly to its
  632. // backend.
  633. xit("file.spec.25 DirectoryEntry.getDirectory: create new dir with space resolveLocalFileSystemURL with encoded URI", function (done) {
  634. var dirName = "de create dir2",
  635. dirPath = joinURL(root.fullPath, dirName);
  636. function win(directory) {
  637. expect(directory).toBeDefined();
  638. expect(directory.isFile).toBe(false);
  639. expect(directory.isDirectory).toBe(true);
  640. expect(directory.name).toCanonicallyMatch(dirName);
  641. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  642. // cleanup
  643. deleteEntry(directory.name, done);
  644. }
  645. function getDir(dirEntry) {
  646. var dirURI = dirEntry.toURL();
  647. // now encode URI and try to resolve
  648. window.resolveLocalFileSystemURL(encodeURI(dirURI), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - getDir function - Error resolving directory: ' + dirURI));
  649. }
  650. // create:true, exclusive:false, directory does not exist
  651. root.getDirectory(dirName, {
  652. create : true
  653. }, getDir, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  654. });
  655. it("file.spec.26 DirectoryEntry.getDirectory: create new directory", function (done) {
  656. var dirName = "de.create.dir",
  657. dirPath = joinURL(root.fullPath, dirName),
  658. win = function (directory) {
  659. expect(directory).toBeDefined();
  660. expect(directory.isFile).toBe(false);
  661. expect(directory.isDirectory).toBe(true);
  662. expect(directory.name).toCanonicallyMatch(dirName);
  663. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  664. expect(directory.filesystem).toBeDefined();
  665. expect(directory.filesystem).toBe(root.filesystem);
  666. // cleanup
  667. deleteEntry(directory.name, done);
  668. };
  669. // create:true, exclusive:false, directory does not exist
  670. root.getDirectory(dirName, {
  671. create : true
  672. }, win, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  673. });
  674. it("file.spec.27 DirectoryEntry.getDirectory: create new directory (exclusive)", function (done) {
  675. var dirName = "de.create.exclusive.dir",
  676. dirPath = joinURL(root.fullPath, dirName),
  677. win = function (directory) {
  678. expect(directory).toBeDefined();
  679. expect(directory.isFile).toBe(false);
  680. expect(directory.isDirectory).toBe(true);
  681. expect(directory.name).toCanonicallyMatch(dirName);
  682. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  683. expect(directory.filesystem).toBeDefined();
  684. expect(directory.filesystem).toBe(root.filesystem);
  685. // cleanup
  686. deleteEntry(directory.name, done);
  687. };
  688. // create:true, exclusive:true, directory does not exist
  689. root.getDirectory(dirName, {
  690. create : true,
  691. exclusive : true
  692. }, win, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  693. });
  694. it("file.spec.28 DirectoryEntry.getDirectory: create directory that already exists", function (done) {
  695. var dirName = "de.create.existing.dir",
  696. dirPath = joinURL(root.fullPath, dirName),
  697. win = function (directory) {
  698. expect(directory).toBeDefined();
  699. expect(directory.isFile).toBe(false);
  700. expect(directory.isDirectory).toBe(true);
  701. expect(directory.name).toCanonicallyMatch(dirName);
  702. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  703. // cleanup
  704. deleteEntry(directory.name, done);
  705. };
  706. // create directory to kick off it
  707. root.getDirectory(dirName, {
  708. create : true
  709. }, function () {
  710. root.getDirectory(dirName, {
  711. create : true
  712. }, win, failed.bind(null, done, 'root.getDirectory - Error creating existent second directory : ' + dirName));
  713. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  714. });
  715. it("file.spec.29 DirectoryEntry.getDirectory: create directory that already exists (exclusive)", function (done) {
  716. var dirName = "de.create.exclusive.existing.dir",
  717. existingDir,
  718. fail = function (error) {
  719. expect(error).toBeDefined();
  720. if (isChrome) {
  721. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of PATH_EXISTS_ERR(code: 12)
  722. on trying to exclusively create a file or directory, which already exists (Chrome).*/
  723. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  724. } else {
  725. expect(error).toBeFileError(FileError.PATH_EXISTS_ERR);
  726. }
  727. // cleanup
  728. deleteEntry(existingDir.name, done);
  729. };
  730. // create directory to kick off it
  731. root.getDirectory(dirName, {
  732. create : true
  733. }, function (directory) {
  734. existingDir = directory;
  735. // create:true, exclusive:true, directory exists
  736. root.getDirectory(dirName, {
  737. create : true,
  738. exclusive : true
  739. }, failed.bind(null, done, 'root.getDirectory - Unexpected success callback, second directory should not be created : ' + dirName), fail);
  740. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  741. });
  742. it("file.spec.30 DirectoryEntry.getDirectory: get Entry for existing directory", function (done) {
  743. var dirName = "de.get.dir",
  744. dirPath = joinURL(root.fullPath, dirName),
  745. win = function (directory) {
  746. expect(directory).toBeDefined();
  747. expect(directory.isFile).toBe(false);
  748. expect(directory.isDirectory).toBe(true);
  749. expect(directory.name).toCanonicallyMatch(dirName);
  750. expect(directory.fullPath).toCanonicallyMatch(dirPath);
  751. // cleanup
  752. deleteEntry(directory.name, done);
  753. };
  754. // create directory to kick it off
  755. root.getDirectory(dirName, {
  756. create : true
  757. }, function () {
  758. root.getDirectory(dirName, {
  759. create : false
  760. }, win, failed.bind(null, done, 'root.getDirectory - Error getting directory entry : ' + dirName));
  761. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  762. });
  763. it("file.spec.31 DirectoryEntry.getDirectory: get DirectoryEntry for invalid path", function (done) {
  764. if (isBrowser) {
  765. /*The plugin does not follow to ["8.3 Naming restrictions"]
  766. (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions).*/
  767. pending();
  768. }
  769. var dirName = "de:invalid:path",
  770. fail = function (error) {
  771. expect(error).toBeDefined();
  772. expect(error).toBeFileError(FileError.ENCODING_ERR);
  773. done();
  774. };
  775. // create:false, exclusive:false, invalid path
  776. root.getDirectory(dirName, {
  777. create : false
  778. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + dirName), fail);
  779. });
  780. it("file.spec.32 DirectoryEntry.getDirectory: get DirectoryEntry for existing file", function (done) {
  781. var fileName = "de.existing.file",
  782. existingFile,
  783. fail = function (error) {
  784. expect(error).toBeDefined();
  785. expect(error).toBeFileError(FileError.TYPE_MISMATCH_ERR);
  786. // cleanup
  787. deleteEntry(existingFile.name, done);
  788. };
  789. // create file to kick off it
  790. root.getFile(fileName, {
  791. create : true
  792. }, function (file) {
  793. existingFile = file;
  794. root.getDirectory(fileName, {
  795. create : false
  796. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + fileName), fail);
  797. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  798. });
  799. it("file.spec.33 DirectoryEntry.getFile: get FileEntry for existing directory", function (done) {
  800. var dirName = "de.existing.dir",
  801. existingDir,
  802. fail = function (error) {
  803. expect(error).toBeDefined();
  804. expect(error).toBeFileError(FileError.TYPE_MISMATCH_ERR);
  805. // cleanup
  806. deleteEntry(existingDir.name, done);
  807. };
  808. // create directory to kick off it
  809. root.getDirectory(dirName, {
  810. create : true
  811. }, function (directory) {
  812. existingDir = directory;
  813. root.getFile(dirName, {
  814. create : false
  815. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, file should not exists: ' + dirName), fail);
  816. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  817. });
  818. it("file.spec.34 DirectoryEntry.removeRecursively on directory", function (done) {
  819. var dirName = "de.removeRecursively",
  820. subDirName = "dir",
  821. dirExists = function (error) {
  822. expect(error).toBeDefined();
  823. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  824. done();
  825. };
  826. // create a new directory entry to kick off it
  827. root.getDirectory(dirName, {
  828. create : true
  829. }, function (entry) {
  830. entry.getDirectory(subDirName, {
  831. create : true
  832. }, function (dir) {
  833. entry.removeRecursively(function () {
  834. root.getDirectory(dirName, {
  835. create : false
  836. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + dirName), dirExists);
  837. }, failed.bind(null, done, 'entry.removeRecursively - Error removing directory recursively : ' + dirName));
  838. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + subDirName));
  839. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  840. });
  841. it("file.spec.35 createReader: create reader on existing directory", function () {
  842. // create reader for root directory
  843. var reader = root.createReader();
  844. expect(reader).toBeDefined();
  845. expect(typeof reader.readEntries).toBe('function');
  846. });
  847. it("file.spec.36 removeRecursively on root file system", function (done) {
  848. var remove = function (error) {
  849. expect(error).toBeDefined();
  850. if (isChrome) {
  851. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of
  852. NO_MODIFICATION_ALLOWED_ERR(code: 6) on trying to call removeRecursively
  853. on the root file system (Chrome).*/
  854. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  855. } else {
  856. expect(error).toBeFileError(FileError.NO_MODIFICATION_ALLOWED_ERR);
  857. }
  858. done();
  859. };
  860. // remove root file system
  861. root.removeRecursively(succeed.bind(null, done, 'root.removeRecursively - Unexpected success callback, root cannot be removed'), remove);
  862. });
  863. });
  864. describe('DirectoryReader interface', function () {
  865. describe("readEntries", function () {
  866. it("file.spec.37 should read contents of existing directory", function (done) {
  867. var reader,
  868. win = function (entries) {
  869. expect(entries).toBeDefined();
  870. expect(entries instanceof Array).toBe(true);
  871. done();
  872. };
  873. // create reader for root directory
  874. reader = root.createReader();
  875. // read entries
  876. reader.readEntries(win, failed.bind(null, done, 'reader.readEntries - Error reading entries'));
  877. });
  878. it("file.spec.37.1 should read contents of existing directory", function (done) {
  879. var dirName = 'readEntries.dir',
  880. fileName = 'readeEntries.file';
  881. root.getDirectory(dirName, {
  882. create : true
  883. }, function (directory) {
  884. directory.getFile(fileName, {
  885. create : true
  886. }, function (fileEntry) {
  887. var reader = directory.createReader();
  888. reader.readEntries(function (entries) {
  889. expect(entries).toBeDefined();
  890. expect(entries instanceof Array).toBe(true);
  891. expect(entries.length).toBe(1);
  892. expect(entries[0].fullPath).toCanonicallyMatch(fileEntry.fullPath);
  893. expect(entries[0].filesystem).not.toBe(null);
  894. if (isChrome) {
  895. // Slicing '[object {type}]' -> '{type}'
  896. expect(entries[0].filesystem.toString().slice(8, -1)).toEqual("DOMFileSystem");
  897. }
  898. else {
  899. expect(entries[0].filesystem instanceof FileSystem).toBe(true);
  900. }
  901. // cleanup
  902. deleteEntry(directory.name, done);
  903. }, failed.bind(null, done, 'reader.readEntries - Error reading entries from directory: ' + dirName));
  904. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + fileName));
  905. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  906. });
  907. it("file.spec.109 should return an empty entry list on the second call", function (done) {
  908. var reader,
  909. fileName = 'test109.txt';
  910. // Add a file to ensure the root directory is non-empty and then read the contents of the directory.
  911. root.getFile(fileName, {
  912. create : true
  913. }, function (entry) {
  914. reader = root.createReader();
  915. //First read
  916. reader.readEntries(function (entries) {
  917. expect(entries).toBeDefined();
  918. expect(entries instanceof Array).toBe(true);
  919. expect(entries.length).not.toBe(0);
  920. //Second read
  921. reader.readEntries(function (entries_) {
  922. expect(entries_).toBeDefined();
  923. expect(entries_ instanceof Array).toBe(true);
  924. expect(entries_.length).toBe(0);
  925. //Clean up
  926. deleteEntry(entry.name, done);
  927. }, failed.bind(null, done, 'reader.readEntries - Error during SECOND reading of entries from [root] directory'));
  928. }, failed.bind(null, done, 'reader.readEntries - Error during FIRST reading of entries from [root] directory'));
  929. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  930. });
  931. });
  932. it("file.spec.38 should read contents of directory that has been removed", function (done) {
  933. var dirName = "de.createReader.notfound";
  934. // create a new directory entry to kick off it
  935. root.getDirectory(dirName, {
  936. create : true
  937. }, function (directory) {
  938. directory.removeRecursively(function () {
  939. var reader = directory.createReader();
  940. reader.readEntries(succeed.bind(null, done, 'reader.readEntries - Unexpected success callback, it should not read entries from deleted dir: ' + dirName), function (error) {
  941. expect(error).toBeDefined();
  942. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  943. root.getDirectory(dirName, {
  944. create : false
  945. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, it should not get deleted directory: ' + dirName), function (err) {
  946. expect(err).toBeDefined();
  947. expect(err).toBeFileError(FileError.NOT_FOUND_ERR);
  948. done();
  949. });
  950. });
  951. }, failed.bind(null, done, 'directory.removeRecursively - Error removing directory recursively : ' + dirName));
  952. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
  953. });
  954. });
  955. //DirectoryReader interface
  956. describe('File', function () {
  957. it("file.spec.39 constructor should be defined", function () {
  958. expect(File).toBeDefined();
  959. expect(typeof File).toBe('function');
  960. });
  961. it("file.spec.40 should be define File attributes", function () {
  962. var file = new File();
  963. expect(file.name).toBeDefined();
  964. expect(file.type).toBeDefined();
  965. expect(file.lastModifiedDate).toBeDefined();
  966. expect(file.size).toBeDefined();
  967. });
  968. });
  969. //File
  970. describe('FileEntry', function () {
  971. it("file.spec.41 should be define FileEntry methods", function (done) {
  972. var fileName = "fe.methods",
  973. testFileEntry = function (fileEntry) {
  974. expect(fileEntry).toBeDefined();
  975. expect(typeof fileEntry.createWriter).toBe('function');
  976. expect(typeof fileEntry.file).toBe('function');
  977. // cleanup
  978. deleteEntry(fileEntry.name, done);
  979. };
  980. // create a new file entry to kick off it
  981. root.getFile(fileName, {
  982. create : true
  983. }, testFileEntry, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  984. });
  985. it("file.spec.42 createWriter should return a FileWriter object", function (done) {
  986. var fileName = "fe.createWriter",
  987. testFile,
  988. testWriter = function (writer) {
  989. expect(writer).toBeDefined();
  990. if (isChrome) {
  991. // Slicing '[object {type}]' -> '{type}'
  992. expect(writer.toString().slice(8, -1)).toEqual("FileWriter");
  993. }
  994. else {
  995. expect(writer instanceof FileWriter).toBe(true);
  996. }
  997. // cleanup
  998. deleteEntry(testFile.name, done);
  999. };
  1000. // create a new file entry to kick off it
  1001. root.getFile(fileName, {
  1002. create : true
  1003. }, function (fileEntry) {
  1004. testFile = fileEntry;
  1005. fileEntry.createWriter(testWriter, failed.bind(null, done, 'fileEntry.createWriter - Error creating Writer from entry'));
  1006. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  1007. });
  1008. it("file.spec.43 file should return a File object", function (done) {
  1009. var fileName = "fe.file",
  1010. newFile,
  1011. testFile = function (file) {
  1012. expect(file).toBeDefined();
  1013. if (isChrome) {
  1014. // Slicing '[object {type}]' -> '{type}'
  1015. expect(file.toString().slice(8, -1)).toEqual("File");
  1016. }
  1017. else {
  1018. expect(file instanceof File).toBe(true);
  1019. }
  1020. // cleanup
  1021. deleteEntry(newFile.name, done);
  1022. };
  1023. // create a new file entry to kick off it
  1024. root.getFile(fileName, {
  1025. create : true
  1026. }, function (fileEntry) {
  1027. newFile = fileEntry;
  1028. fileEntry.file(testFile, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
  1029. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  1030. });
  1031. it("file.spec.44 file: on File that has been removed", function (done) {
  1032. var fileName = "fe.no.file";
  1033. // create a new file entry to kick off it
  1034. root.getFile(fileName, {
  1035. create : true
  1036. }, function (fileEntry) {
  1037. fileEntry.remove(function () {
  1038. fileEntry.file(succeed.bind(null, done, 'fileEntry.file - Unexpected success callback, file it should not be created from removed entry'), function (error) {
  1039. expect(error).toBeDefined();
  1040. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1041. done();
  1042. });
  1043. }, failed.bind(null, done, 'fileEntry.remove - Error removing entry : ' + fileName));
  1044. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
  1045. });
  1046. });
  1047. //FileEntry
  1048. describe('Entry', function () {
  1049. it("file.spec.45 Entry object", function (done) {
  1050. var fileName = "entry",
  1051. fullPath = joinURL(root.fullPath, fileName),
  1052. winEntry = function (entry) {
  1053. expect(entry).toBeDefined();
  1054. expect(entry.isFile).toBe(true);
  1055. expect(entry.isDirectory).toBe(false);
  1056. expect(entry.name).toCanonicallyMatch(fileName);
  1057. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  1058. expect(typeof entry.getMetadata).toBe('function');
  1059. expect(typeof entry.setMetadata).toBe('function');
  1060. expect(typeof entry.moveTo).toBe('function');
  1061. expect(typeof entry.copyTo).toBe('function');
  1062. expect(typeof entry.toURL).toBe('function');
  1063. expect(typeof entry.remove).toBe('function');
  1064. expect(typeof entry.getParent).toBe('function');
  1065. expect(typeof entry.createWriter).toBe('function');
  1066. expect(typeof entry.file).toBe('function');
  1067. // Clean up
  1068. deleteEntry(fileName, done);
  1069. };
  1070. // create a new file entry
  1071. createFile(fileName, winEntry, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1072. });
  1073. it("file.spec.46 Entry.getMetadata on file", function (done) {
  1074. var fileName = "entry.metadata.file";
  1075. // create a new file entry
  1076. createFile(fileName, function (entry) {
  1077. entry.getMetadata(function (metadata) {
  1078. expect(metadata).toBeDefined();
  1079. expect(metadata.modificationTime instanceof Date).toBe(true);
  1080. expect(typeof metadata.size).toBe("number");
  1081. // cleanup
  1082. deleteEntry(fileName, done);
  1083. }, failed.bind(null, done, 'entry.getMetadata - Error getting metadata from entry : ' + fileName));
  1084. }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1085. });
  1086. it("file.spec.47 Entry.getMetadata on directory", function (done) {
  1087. if (isIndexedDBShim) {
  1088. /* Does not support metadata for directories (Firefox, IE) */
  1089. pending();
  1090. }
  1091. var dirName = "entry.metadata.dir";
  1092. // create a new directory entry
  1093. createDirectory(dirName, function (entry) {
  1094. entry.getMetadata(function (metadata) {
  1095. expect(metadata).toBeDefined();
  1096. expect(metadata.modificationTime instanceof Date).toBe(true);
  1097. expect(typeof metadata.size).toBe("number");
  1098. expect(metadata.size).toBe(0);
  1099. // cleanup
  1100. deleteEntry(dirName, done);
  1101. }, failed.bind(null, done, 'entry.getMetadata - Error getting metadata from entry : ' + dirName));
  1102. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1103. });
  1104. it("file.spec.48 Entry.getParent on file in root file system", function (done) {
  1105. var fileName = "entry.parent.file",
  1106. rootPath = root.fullPath;
  1107. // create a new file entry
  1108. createFile(fileName, function (entry) {
  1109. entry.getParent(function (parent) {
  1110. expect(parent).toBeDefined();
  1111. expect(parent.fullPath).toCanonicallyMatch(rootPath);
  1112. // cleanup
  1113. deleteEntry(fileName, done);
  1114. }, failed.bind(null, done, 'entry.getParent - Error getting parent directory of file : ' + fileName));
  1115. }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1116. });
  1117. it("file.spec.49 Entry.getParent on directory in root file system", function (done) {
  1118. var dirName = "entry.parent.dir",
  1119. rootPath = root.fullPath;
  1120. // create a new directory entry
  1121. createDirectory(dirName, function (entry) {
  1122. entry.getParent(function (parent) {
  1123. expect(parent).toBeDefined();
  1124. expect(parent.fullPath).toCanonicallyMatch(rootPath);
  1125. // cleanup
  1126. deleteEntry(dirName, done);
  1127. }, failed.bind(null, done, 'entry.getParent - Error getting parent directory of directory : ' + dirName));
  1128. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1129. });
  1130. it("file.spec.50 Entry.getParent on root file system", function (done) {
  1131. var rootPath = root.fullPath,
  1132. winParent = function (parent) {
  1133. expect(parent).toBeDefined();
  1134. expect(parent.fullPath).toCanonicallyMatch(rootPath);
  1135. done();
  1136. };
  1137. // create a new directory entry
  1138. root.getParent(winParent, failed.bind(null, done, 'root.getParent - Error getting parent directory of root'));
  1139. });
  1140. it("file.spec.51 Entry.toURL on file", function (done) {
  1141. var fileName = "entry.uri.file",
  1142. rootPath = root.fullPath,
  1143. winURI = function (entry) {
  1144. var uri = entry.toURL();
  1145. expect(uri).toBeDefined();
  1146. expect(uri.indexOf(rootPath)).not.toBe(-1);
  1147. // cleanup
  1148. deleteEntry(fileName, done);
  1149. };
  1150. // create a new file entry
  1151. createFile(fileName, winURI, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1152. });
  1153. it("file.spec.52 Entry.toURL on directory", function (done) {
  1154. var dirName_1 = "num 1",
  1155. dirName_2 = "num 2",
  1156. rootPath = root.fullPath;
  1157. createDirectory(dirName_1, function (entry) {
  1158. entry.getDirectory(dirName_2, {
  1159. create : true
  1160. }, function (entryFile) {
  1161. var uri = entryFile.toURL();
  1162. expect(uri).toBeDefined();
  1163. expect(uri).toContain('/num%201/num%202/');
  1164. expect(uri.indexOf(rootPath)).not.toBe(-1);
  1165. // cleanup
  1166. deleteEntry(dirName_1, done);
  1167. }, failed.bind(null, done, 'entry.getDirectory - Error creating directory : ' + dirName_2));
  1168. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName_1));
  1169. });
  1170. it("file.spec.53 Entry.remove on file", function (done) {
  1171. var fileName = "entr .rm.file";
  1172. // create a new file entry
  1173. createFile(fileName, function (entry) {
  1174. expect(entry).toBeDefined();
  1175. entry.remove(function () {
  1176. root.getFile(fileName, null, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get deleted file : ' + fileName), function (error) {
  1177. expect(error).toBeDefined();
  1178. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1179. // cleanup
  1180. deleteEntry(fileName, done);
  1181. });
  1182. }, failed.bind(null, done, 'entry.remove - Error removing entry : ' + fileName));
  1183. }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1184. });
  1185. it("file.spec.53.1 Entry.remove on filename with #s", function (done) {
  1186. var fileName = "entry.#rm#.file";
  1187. // create a new file entry
  1188. createFile(fileName, function (entry) {
  1189. expect(entry).toBeDefined();
  1190. entry.remove(function () {
  1191. root.getFile(fileName, null, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get deleted file : ' + fileName), function (error) {
  1192. expect(error).toBeDefined();
  1193. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1194. // cleanup
  1195. deleteEntry(fileName, done);
  1196. });
  1197. }, failed.bind(null, done, 'entry.remove - Error removing entry : ' + fileName));
  1198. }, failed.bind(null, done, 'createFile - Error creating file : ' + fileName));
  1199. });
  1200. it("file.spec.54 remove on empty directory", function (done) {
  1201. var dirName = "entry.rm.dir";
  1202. // create a new directory entry
  1203. createDirectory(dirName, function (entry) {
  1204. expect(entry).toBeDefined();
  1205. entry.remove(function () {
  1206. root.getDirectory(dirName, null, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, it should not get deleted directory : ' + dirName), function (error) {
  1207. expect(error).toBeDefined();
  1208. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1209. // cleanup
  1210. deleteEntry(dirName, done);
  1211. });
  1212. }, failed.bind(null, done, 'entry.remove - Error removing entry : ' + dirName));
  1213. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1214. });
  1215. it("file.spec.55 remove on non-empty directory", function (done) {
  1216. if (isIndexedDBShim) {
  1217. /* Both Entry.remove and directoryEntry.removeRecursively don't fail when removing
  1218. non-empty directories - directories being removed are cleaned
  1219. along with contents instead (Firefox, IE)*/
  1220. pending();
  1221. }
  1222. var dirName = "ent y.rm.dir.not.empty",
  1223. fileName = "re ove.txt",
  1224. fullPath = joinURL(root.fullPath, dirName);
  1225. // create a new directory entry
  1226. createDirectory(dirName, function (entry) {
  1227. entry.getFile(fileName, {
  1228. create : true
  1229. }, function (fileEntry) {
  1230. entry.remove(succeed.bind(null, done, 'entry.remove - Unexpected success callback, it should not remove a directory that contains files : ' + dirName), function (error) {
  1231. expect(error).toBeDefined();
  1232. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1233. root.getDirectory(dirName, null, function (entry) {
  1234. expect(entry).toBeDefined();
  1235. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  1236. // cleanup
  1237. deleteEntry(dirName, done);
  1238. }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + dirName));
  1239. });
  1240. }, failed.bind(null, done, 'entry.getFile - Error creating file : ' + fileName + ' inside of ' + dirName));
  1241. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1242. });
  1243. it("file.spec.56 remove on root file system", function (done) {
  1244. // remove entry that doesn't exist
  1245. root.remove(succeed.bind(null, done, 'entry.remove - Unexpected success callback, it should not remove entry that it does not exists'), function (error) {
  1246. expect(error).toBeDefined();
  1247. if (isChrome) {
  1248. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of
  1249. NO_MODIFICATION_ALLOWED_ERR(code: 6) on trying to call removeRecursively
  1250. on the root file system.*/
  1251. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1252. } else {
  1253. expect(error).toBeFileError(FileError.NO_MODIFICATION_ALLOWED_ERR);
  1254. }
  1255. done();
  1256. });
  1257. });
  1258. it("file.spec.57 copyTo: file", function (done) {
  1259. var file1 = "entry copy.file1",
  1260. file2 = "entry copy.file2",
  1261. fullPath = joinURL(root.fullPath, file2);
  1262. // create a new file entry to kick off it
  1263. deleteEntry(file2, function () {
  1264. createFile(file1, function (fileEntry) {
  1265. // copy file1 to file2
  1266. fileEntry.copyTo(root, file2, function (entry) {
  1267. expect(entry).toBeDefined();
  1268. expect(entry.isFile).toBe(true);
  1269. expect(entry.isDirectory).toBe(false);
  1270. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  1271. expect(entry.name).toCanonicallyMatch(file2);
  1272. root.getFile(file2, {
  1273. create : false
  1274. }, function (entry2) {
  1275. expect(entry2).toBeDefined();
  1276. expect(entry2.isFile).toBe(true);
  1277. expect(entry2.isDirectory).toBe(false);
  1278. expect(entry2.fullPath).toCanonicallyMatch(fullPath);
  1279. expect(entry2.name).toCanonicallyMatch(file2);
  1280. // cleanup
  1281. deleteEntry(file1, function () {
  1282. deleteEntry(file2, done);
  1283. });
  1284. }, failed.bind(null, done, 'root.getFile - Error getting copied file : ' + file2));
  1285. }, failed.bind(null, done, 'fileEntry.copyTo - Error copying file : ' + file2));
  1286. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1287. }, failed.bind(null, done, 'deleteEntry - Error removing file : ' + file2));
  1288. });
  1289. it("file.spec.58 copyTo: file onto itself", function (done) {
  1290. var file1 = "entry.copy.fos.file1";
  1291. // create a new file entry to kick off it
  1292. createFile(file1, function (entry) {
  1293. // copy file1 onto itself
  1294. entry.copyTo(root, null, succeed.bind(null, done, 'entry.copyTo - Unexpected success callback, it should not copy a null file'), function (error) {
  1295. expect(error).toBeDefined();
  1296. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1297. // cleanup
  1298. deleteEntry(file1, done);
  1299. });
  1300. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1301. });
  1302. it("file.spec.59 copyTo: directory", function (done) {
  1303. if (isIndexedDBShim) {
  1304. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1305. pending();
  1306. }
  1307. var file1 = "file1",
  1308. srcDir = "entry.copy.srcDir",
  1309. dstDir = "entry.copy.dstDir",
  1310. dstPath = joinURL(root.fullPath, dstDir),
  1311. filePath = joinURL(dstPath, file1);
  1312. // create a new directory entry to kick off it
  1313. deleteEntry(dstDir, function () {
  1314. createDirectory(srcDir, function (directory) {
  1315. // create a file within new directory
  1316. directory.getFile(file1, {
  1317. create : true
  1318. }, function () {
  1319. directory.copyTo(root, dstDir, function (directory) {
  1320. expect(directory).toBeDefined();
  1321. expect(directory.isFile).toBe(false);
  1322. expect(directory.isDirectory).toBe(true);
  1323. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  1324. expect(directory.name).toCanonicallyMatch(dstDir);
  1325. root.getDirectory(dstDir, {
  1326. create : false
  1327. }, function (dirEntry) {
  1328. expect(dirEntry).toBeDefined();
  1329. expect(dirEntry.isFile).toBe(false);
  1330. expect(dirEntry.isDirectory).toBe(true);
  1331. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  1332. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  1333. dirEntry.getFile(file1, {
  1334. create : false
  1335. }, function (fileEntry) {
  1336. expect(fileEntry).toBeDefined();
  1337. expect(fileEntry.isFile).toBe(true);
  1338. expect(fileEntry.isDirectory).toBe(false);
  1339. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1340. expect(fileEntry.name).toCanonicallyMatch(file1);
  1341. // cleanup
  1342. deleteEntry(srcDir, function () {
  1343. deleteEntry(dstDir, done);
  1344. });
  1345. }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1));
  1346. }, failed.bind(null, done, 'root.getDirectory - Error getting copied directory : ' + dstDir));
  1347. }, failed.bind(null, done, 'directory.copyTo - Error copying directory : ' + srcDir + ' to :' + dstDir));
  1348. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1349. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1350. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1351. });
  1352. it("file.spec.60 copyTo: directory to backup at same root directory", function (done) {
  1353. if (isIndexedDBShim) {
  1354. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1355. pending();
  1356. }
  1357. var file1 = "file1",
  1358. srcDir = "entry.copy srcDirSame",
  1359. dstDir = "entry.copy srcDirSame-backup",
  1360. dstPath = joinURL(root.fullPath, dstDir),
  1361. filePath = joinURL(dstPath, file1);
  1362. // create a new directory entry to kick off it
  1363. deleteEntry(dstDir, function () {
  1364. createDirectory(srcDir, function (directory) {
  1365. directory.getFile(file1, {
  1366. create : true
  1367. }, function () {
  1368. directory.copyTo(root, dstDir, function (directory) {
  1369. expect(directory).toBeDefined();
  1370. expect(directory.isFile).toBe(false);
  1371. expect(directory.isDirectory).toBe(true);
  1372. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  1373. expect(directory.name).toCanonicallyMatch(dstDir);
  1374. root.getDirectory(dstDir, {
  1375. create : false
  1376. }, function (dirEntry) {
  1377. expect(dirEntry).toBeDefined();
  1378. expect(dirEntry.isFile).toBe(false);
  1379. expect(dirEntry.isDirectory).toBe(true);
  1380. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  1381. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  1382. dirEntry.getFile(file1, {
  1383. create : false
  1384. }, function (fileEntry) {
  1385. expect(fileEntry).toBeDefined();
  1386. expect(fileEntry.isFile).toBe(true);
  1387. expect(fileEntry.isDirectory).toBe(false);
  1388. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1389. expect(fileEntry.name).toCanonicallyMatch(file1);
  1390. // cleanup
  1391. deleteEntry(srcDir, function () {
  1392. deleteEntry(dstDir, done);
  1393. });
  1394. }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1));
  1395. }, failed.bind(null, done, 'root.getDirectory - Error getting copied directory : ' + dstDir));
  1396. }, failed.bind(null, done, 'directory.copyTo - Error copying directory : ' + srcDir + ' to :' + dstDir));
  1397. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1398. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1399. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1400. });
  1401. it("file.spec.61 copyTo: directory onto itself", function (done) {
  1402. if (isIndexedDBShim) {
  1403. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1404. pending();
  1405. }
  1406. var file1 = "file1",
  1407. srcDir = "entry.copy.dos.srcDir",
  1408. srcPath = joinURL(root.fullPath, srcDir),
  1409. filePath = joinURL(srcPath, file1);
  1410. // create a new directory entry to kick off it
  1411. createDirectory(srcDir, function (directory) {
  1412. // create a file within new directory
  1413. directory.getFile(file1, {
  1414. create : true
  1415. }, function (fileEntry) {
  1416. // copy srcDir onto itself
  1417. directory.copyTo(root, null, succeed.bind(null, done, 'directory.copyTo - Unexpected success callback, it should not copy file: ' + srcDir + ' to a null destination'), function (error) {
  1418. expect(error).toBeDefined();
  1419. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1420. root.getDirectory(srcDir, {
  1421. create : false
  1422. }, function (dirEntry) {
  1423. expect(dirEntry).toBeDefined();
  1424. expect(dirEntry.fullPath).toCanonicallyMatch(srcPath);
  1425. dirEntry.getFile(file1, {
  1426. create : false
  1427. }, function (fileEntry) {
  1428. expect(fileEntry).toBeDefined();
  1429. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1430. // cleanup
  1431. deleteEntry(srcDir, done);
  1432. }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1));
  1433. }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + srcDir));
  1434. });
  1435. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1436. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1437. });
  1438. it("file.spec.62 copyTo: directory into itself", function (done) {
  1439. if (isIndexedDBShim) {
  1440. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1441. pending();
  1442. }
  1443. var srcDir = "entry.copy.dis.srcDir",
  1444. dstDir = "entry.copy.dis.dstDir",
  1445. srcPath = joinURL(root.fullPath, srcDir);
  1446. // create a new directory entry to kick off it
  1447. createDirectory(srcDir, function (directory) {
  1448. // copy source directory into itself
  1449. directory.copyTo(directory, dstDir, succeed.bind(null, done, 'directory.copyTo - Unexpected success callback, it should not copy a directory ' + srcDir + ' into itself'), function (error) {
  1450. expect(error).toBeDefined();
  1451. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1452. root.getDirectory(srcDir, {
  1453. create : false
  1454. }, function (dirEntry) {
  1455. // returning confirms existence so just check fullPath entry
  1456. expect(dirEntry).toBeDefined();
  1457. expect(dirEntry.fullPath).toCanonicallyMatch(srcPath);
  1458. // cleanup
  1459. deleteEntry(srcDir, done);
  1460. }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + srcDir));
  1461. });
  1462. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1463. });
  1464. it("file.spec.63 copyTo: directory that does not exist", function (done) {
  1465. var file1 = "entry.copy.dnf.file1",
  1466. dirName = 'dir-foo';
  1467. createFile(file1, function (fileEntry) {
  1468. createDirectory(dirName, function (dirEntry) {
  1469. dirEntry.remove(function () {
  1470. fileEntry.copyTo(dirEntry, null, succeed.bind(null, done, 'fileEntry.copyTo - Unexpected success callback, it should not copy a file ' + file1 + ' into a removed directory'), function (error) {
  1471. expect(error).toBeDefined();
  1472. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1473. done();
  1474. });
  1475. }, failed.bind(null, done, 'dirEntry.remove - Error removing directory : ' + dirName));
  1476. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dirName));
  1477. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1478. });
  1479. it("file.spec.64 copyTo: invalid target name", function (done) {
  1480. if (isBrowser) {
  1481. /*The plugin does not follow ["8.3 Naming restrictions"]
  1482. (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions*/
  1483. pending();
  1484. }
  1485. var file1 = "entry.copy.itn.file1",
  1486. file2 = "bad:file:name";
  1487. // create a new file entry
  1488. createFile(file1, function (entry) {
  1489. // copy file1 to file2
  1490. entry.copyTo(root, file2, succeed.bind(null, done, 'entry.copyTo - Unexpected success callback, it should not copy a file ' + file1 + ' to an invalid file name: ' + file2), function (error) {
  1491. expect(error).toBeDefined();
  1492. expect(error).toBeFileError(FileError.ENCODING_ERR);
  1493. // cleanup
  1494. deleteEntry(file1, done);
  1495. });
  1496. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1497. });
  1498. it("file.spec.65 moveTo: file to same parent", function (done) {
  1499. var file1 = "entry.move.fsp.file1",
  1500. file2 = "entry.move.fsp.file2",
  1501. dstPath = joinURL(root.fullPath, file2);
  1502. // create a new file entry to kick off it
  1503. createFile(file1, function (entry) {
  1504. // move file1 to file2
  1505. entry.moveTo(root, file2, function (entry) {
  1506. expect(entry).toBeDefined();
  1507. expect(entry.isFile).toBe(true);
  1508. expect(entry.isDirectory).toBe(false);
  1509. expect(entry.fullPath).toCanonicallyMatch(dstPath);
  1510. expect(entry.name).toCanonicallyMatch(file2);
  1511. root.getFile(file2, {
  1512. create : false
  1513. }, function (fileEntry) {
  1514. expect(fileEntry).toBeDefined();
  1515. expect(fileEntry.fullPath).toCanonicallyMatch(dstPath);
  1516. root.getFile(file1, {
  1517. create : false
  1518. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1519. //expect(navigator.fileMgr.testFileExists(srcPath) === false, "original file should not exist.");
  1520. expect(error).toBeDefined();
  1521. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1522. // cleanup
  1523. deleteEntry(file1, function () {
  1524. deleteEntry(file2, done);
  1525. });
  1526. });
  1527. }, failed.bind(null, done, 'root.getFile - Error getting file : ' + file2));
  1528. }, failed.bind(null, done, 'entry.moveTo - Error moving file : ' + file1 + ' to root as: ' + file2));
  1529. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1530. });
  1531. it("file.spec.66 moveTo: file to new parent", function (done) {
  1532. var file1 = "entry.move.fnp.file1",
  1533. dir = "entry.move.fnp.dir",
  1534. dstPath = joinURL(joinURL(root.fullPath, dir), file1);
  1535. // ensure destination directory is cleaned up first
  1536. deleteEntry(dir, function () {
  1537. // create a new file entry to kick off it
  1538. createFile(file1, function (entry) {
  1539. // create a parent directory to move file to
  1540. root.getDirectory(dir, {
  1541. create : true
  1542. }, function (directory) {
  1543. // move file1 to new directory
  1544. // move the file
  1545. entry.moveTo(directory, null, function (entry) {
  1546. expect(entry).toBeDefined();
  1547. expect(entry.isFile).toBe(true);
  1548. expect(entry.isDirectory).toBe(false);
  1549. expect(entry.fullPath).toCanonicallyMatch(dstPath);
  1550. expect(entry.name).toCanonicallyMatch(file1);
  1551. // test the moved file exists
  1552. directory.getFile(file1, {
  1553. create : false
  1554. }, function (fileEntry) {
  1555. expect(fileEntry).toBeDefined();
  1556. expect(fileEntry.fullPath).toCanonicallyMatch(dstPath);
  1557. root.getFile(file1, {
  1558. create : false
  1559. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1560. expect(error).toBeDefined();
  1561. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1562. // cleanup
  1563. deleteEntry(file1, function () {
  1564. deleteEntry(dir, done);
  1565. });
  1566. });
  1567. }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + dir));
  1568. }, failed.bind(null, done, 'entry.moveTo - Error moving file : ' + file1 + ' to: ' + dir + ' with the same name'));
  1569. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dir));
  1570. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1571. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dir));
  1572. });
  1573. it("file.spec.67 moveTo: directory to same parent", function (done) {
  1574. if (isIndexedDBShim) {
  1575. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1576. pending();
  1577. }
  1578. var file1 = "file1",
  1579. srcDir = "entry.move.dsp.srcDir",
  1580. dstDir = "entry.move.dsp.dstDir",
  1581. dstPath = joinURL(root.fullPath, dstDir),
  1582. filePath = joinURL(dstPath, file1);
  1583. // ensure destination directory is cleaned up before it
  1584. deleteEntry(dstDir, function () {
  1585. // create a new directory entry to kick off it
  1586. createDirectory(srcDir, function (directory) {
  1587. // create a file within directory
  1588. directory.getFile(file1, {
  1589. create : true
  1590. }, function () {
  1591. // move srcDir to dstDir
  1592. directory.moveTo(root, dstDir, function (directory) {
  1593. expect(directory).toBeDefined();
  1594. expect(directory.isFile).toBe(false);
  1595. expect(directory.isDirectory).toBe(true);
  1596. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  1597. expect(directory.name).toCanonicallyMatch(dstDir);
  1598. // test that moved file exists in destination dir
  1599. directory.getFile(file1, {
  1600. create : false
  1601. }, function (fileEntry) {
  1602. expect(fileEntry).toBeDefined();
  1603. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1604. // check that the moved file no longer exists in original dir
  1605. root.getFile(file1, {
  1606. create : false
  1607. }, succeed.bind(null, done, 'directory.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1608. expect(error).toBeDefined();
  1609. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1610. // cleanup
  1611. deleteEntry(srcDir, function() {
  1612. deleteEntry(dstDir, done);
  1613. });
  1614. });
  1615. }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + srcDir));
  1616. }, failed.bind(null, done, 'entry.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
  1617. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1618. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1619. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1620. });
  1621. it("file.spec.68 moveTo: directory to same parent with same name", function (done) {
  1622. if (isIndexedDBShim) {
  1623. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1624. pending();
  1625. }
  1626. var file1 = "file1",
  1627. srcDir = "entry.move.dsp.srcDir",
  1628. dstDir = "entry.move.dsp.srcDir-backup",
  1629. dstPath = joinURL(root.fullPath, dstDir),
  1630. filePath = joinURL(dstPath, file1);
  1631. // ensure destination directory is cleaned up before it
  1632. deleteEntry(dstDir, function () {
  1633. // create a new directory entry to kick off it
  1634. createDirectory(srcDir, function (directory) {
  1635. // create a file within directory
  1636. directory.getFile(file1, {
  1637. create : true
  1638. }, function () {
  1639. // move srcDir to dstDir
  1640. directory.moveTo(root, dstDir, function (directory) {
  1641. expect(directory).toBeDefined();
  1642. expect(directory.isFile).toBe(false);
  1643. expect(directory.isDirectory).toBe(true);
  1644. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  1645. expect(directory.name).toCanonicallyMatch(dstDir);
  1646. // check that moved file exists in destination dir
  1647. directory.getFile(file1, {
  1648. create : false
  1649. }, function (fileEntry) {
  1650. expect(fileEntry).toBeDefined();
  1651. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1652. // check that the moved file no longer exists in original dir
  1653. root.getFile(file1, {
  1654. create : false
  1655. }, succeed.bind(null, done, 'directory.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1656. expect(error).toBeDefined();
  1657. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1658. // cleanup
  1659. deleteEntry(srcDir, function() {
  1660. deleteEntry(dstDir, done);
  1661. });
  1662. });
  1663. }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + srcDir));
  1664. }, failed.bind(null, done, 'entry.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
  1665. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1666. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1667. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1668. });
  1669. it("file.spec.69 moveTo: directory to new parent", function (done) {
  1670. if (isIndexedDBShim) {
  1671. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1672. pending();
  1673. }
  1674. var file1 = "file1",
  1675. srcDir = "entry.move.dnp.srcDir",
  1676. dstDir = "entry.move.dnp.dstDir",
  1677. dstPath = joinURL(root.fullPath, dstDir),
  1678. filePath = joinURL(dstPath, file1);
  1679. // ensure destination directory is cleaned up before it
  1680. deleteEntry(dstDir, function () {
  1681. // create a new directory entry to kick off it
  1682. createDirectory(srcDir, function (directory) {
  1683. // create a file within directory
  1684. directory.getFile(file1, {
  1685. create : true
  1686. }, function () {
  1687. // move srcDir to dstDir
  1688. directory.moveTo(root, dstDir, function (dirEntry) {
  1689. expect(dirEntry).toBeDefined();
  1690. expect(dirEntry.isFile).toBe(false);
  1691. expect(dirEntry.isDirectory).toBe(true);
  1692. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  1693. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  1694. // test that moved file exists in destination dir
  1695. dirEntry.getFile(file1, {
  1696. create : false
  1697. }, function (fileEntry) {
  1698. expect(fileEntry).toBeDefined();
  1699. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1700. // test that the moved file no longer exists in original dir
  1701. root.getFile(file1, {
  1702. create : false
  1703. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not get invalid or moved file: ' + file1), function (error) {
  1704. expect(error).toBeDefined();
  1705. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  1706. // cleanup
  1707. deleteEntry(srcDir, function() {
  1708. deleteEntry(dstDir, done);
  1709. });
  1710. });
  1711. }, failed.bind(null, done, 'directory.getFile - Error getting file : ' + file1 + ' from: ' + dstDir));
  1712. }, failed.bind(null, done, 'directory.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
  1713. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1714. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1715. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1716. });
  1717. it("file.spec.131 moveTo: directories tree to new parent", function (done) {
  1718. if (isIndexedDBShim) {
  1719. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1720. pending();
  1721. }
  1722. var srcDir = "entry.move.dnp.srcDir";
  1723. var srcDirNestedFirst = "entry.move.dnp.srcDir.Nested1";
  1724. var srcDirNestedSecond = "entry.move.dnp.srcDir.Nested2";
  1725. var dstDir = "entry.move.dnp.dstDir";
  1726. createDirectory(dstDir, function (dstDirectory) {
  1727. createDirectory(srcDir, function (srcDirectory) {
  1728. srcDirectory.getDirectory(srcDirNestedFirst, { create: true }, function () {
  1729. srcDirectory.getDirectory(srcDirNestedSecond, { create: true }, function () {
  1730. srcDirectory.moveTo(dstDirectory, srcDir, function successMove(transferredDirectory) {
  1731. var directoryReader = transferredDirectory.createReader();
  1732. directoryReader.readEntries(function successRead(entries) {
  1733. expect(entries.length).toBe(2);
  1734. expect(entries[0].name).toBe(srcDirNestedFirst);
  1735. expect(entries[1].name).toBe(srcDirNestedSecond);
  1736. deleteEntry(dstDir, done);
  1737. }, failed.bind(null, done, 'Error getting entries from: ' + transferredDirectory));
  1738. }, failed.bind(null, done, 'directory.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
  1739. }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + srcDirNestedSecond));
  1740. }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + srcDirNestedFirst));
  1741. }, failed.bind(null, done, 'createDirectory - Error creating source directory : ' + srcDir));
  1742. }, failed.bind(null, done, 'createDirectory - Error creating dest directory : ' + dstDir));
  1743. });
  1744. it("file.spec.70 moveTo: directory onto itself", function (done) {
  1745. if (isIndexedDBShim) {
  1746. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1747. pending();
  1748. }
  1749. var file1 = "file1",
  1750. srcDir = "entry.move.dos.srcDir",
  1751. srcPath = joinURL(root.fullPath, srcDir),
  1752. filePath = joinURL(srcPath, file1);
  1753. // create a new directory entry to kick off it
  1754. createDirectory(srcDir, function (directory) {
  1755. // create a file within new directory
  1756. directory.getFile(file1, {
  1757. create : true
  1758. }, function () {
  1759. // move srcDir onto itself
  1760. directory.moveTo(root, null, succeed.bind(null, done, 'directory.moveTo - Unexpected success callback, it should not move directory to invalid path'), function (error) {
  1761. expect(error).toBeDefined();
  1762. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1763. // test that original dir still exists
  1764. root.getDirectory(srcDir, {
  1765. create : false
  1766. }, function (dirEntry) {
  1767. // returning confirms existence so just check fullPath entry
  1768. expect(dirEntry).toBeDefined();
  1769. expect(dirEntry.fullPath).toCanonicallyMatch(srcPath);
  1770. dirEntry.getFile(file1, {
  1771. create : false
  1772. }, function (fileEntry) {
  1773. expect(fileEntry).toBeDefined();
  1774. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1775. // cleanup
  1776. deleteEntry(srcDir, done);
  1777. }, failed.bind(null, done, 'dirEntry.getFile - Error getting file : ' + file1 + ' from: ' + srcDir));
  1778. }, failed.bind(null, done, 'root.getDirectory - Error getting directory : ' + srcDir));
  1779. });
  1780. }, failed.bind(null, done, 'directory.getFile - Error creating file : ' + file1));
  1781. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1782. });
  1783. it("file.spec.71 moveTo: directory into itself", function (done) {
  1784. if (isIndexedDBShim) {
  1785. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1786. pending();
  1787. }
  1788. var srcDir = "entry.move.dis.srcDir",
  1789. dstDir = "entry.move.dis.dstDir",
  1790. srcPath = joinURL(root.fullPath, srcDir);
  1791. // create a new directory entry to kick off it
  1792. createDirectory(srcDir, function (directory) {
  1793. // move source directory into itself
  1794. directory.moveTo(directory, dstDir, succeed.bind(null, done, 'directory.moveTo - Unexpected success callback, it should not move a directory into itself: ' + srcDir), function (error) {
  1795. expect(error).toBeDefined();
  1796. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1797. // make sure original directory still exists
  1798. root.getDirectory(srcDir, {
  1799. create : false
  1800. }, function (entry) {
  1801. expect(entry).toBeDefined();
  1802. expect(entry.fullPath).toCanonicallyMatch(srcPath);
  1803. // cleanup
  1804. deleteEntry(srcDir, done);
  1805. }, failed.bind(null, done, 'root.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
  1806. });
  1807. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1808. });
  1809. it("file.spec.130 moveTo: directory into similar directory", function (done) {
  1810. if (isIndexedDBShim) {
  1811. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1812. pending();
  1813. }
  1814. var srcDir = "entry.move.dis.srcDir",
  1815. dstDir = "entry.move.dis.srcDir-backup";
  1816. // create a new directory entry to kick off it
  1817. createDirectory(srcDir, function (srcDirEntry) {
  1818. deleteEntry(dstDir, function () {
  1819. createDirectory(dstDir, function (dstDirEntry) {
  1820. // move source directory into itself
  1821. srcDirEntry.moveTo(dstDirEntry, 'file', function (newDirEntry) {
  1822. expect(newDirEntry).toBeDefined();
  1823. deleteEntry(dstDir, done);
  1824. }, failed.bind(null, done, 'directory.moveTo - Error moving a directory into a similarly-named directory: ' + srcDir));
  1825. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + dstDir));
  1826. }, failed.bind(null, done, 'deleteEntry - Error deleting directory : ' + dstDir));
  1827. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1828. });
  1829. it("file.spec.72 moveTo: file onto itself", function (done) {
  1830. var file1 = "entry.move.fos.file1",
  1831. filePath = joinURL(root.fullPath, file1);
  1832. // create a new file entry to kick off it
  1833. createFile(file1, function (entry) {
  1834. // move file1 onto itself
  1835. entry.moveTo(root, null, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, it should not move a file: ' + file1 + ' into the same parent'), function (error) {
  1836. expect(error).toBeDefined();
  1837. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1838. //test that original file still exists
  1839. root.getFile(file1, {
  1840. create : false
  1841. }, function (fileEntry) {
  1842. expect(fileEntry).toBeDefined();
  1843. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1844. // cleanup
  1845. deleteEntry(file1, done);
  1846. }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
  1847. });
  1848. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1849. });
  1850. it("file.spec.73 moveTo: file onto existing directory", function (done) {
  1851. var file1 = "entry.move.fod.file1",
  1852. dstDir = "entry.move.fod.dstDir",
  1853. subDir = "subDir",
  1854. dirPath = joinURL(joinURL(root.fullPath, dstDir), subDir),
  1855. filePath = joinURL(root.fullPath, file1);
  1856. // ensure destination directory is cleaned up before it
  1857. deleteEntry(dstDir, function () {
  1858. // create a new file entry to kick off it
  1859. createFile(file1, function (entry) {
  1860. // create top level directory
  1861. root.getDirectory(dstDir, {
  1862. create : true
  1863. }, function (directory) {
  1864. // create sub-directory
  1865. directory.getDirectory(subDir, {
  1866. create : true
  1867. }, function (subDirectory) {
  1868. // move file1 onto sub-directory
  1869. entry.moveTo(directory, subDir, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, it should not move a file: ' + file1 + ' into directory: ' + dstDir + '\n' + subDir + ' directory already exists'), function (error) {
  1870. expect(error).toBeDefined();
  1871. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1872. // check that original dir still exists
  1873. directory.getDirectory(subDir, {
  1874. create : false
  1875. }, function (dirEntry) {
  1876. expect(dirEntry).toBeDefined();
  1877. expect(dirEntry.fullPath).toCanonicallyMatch(dirPath);
  1878. // check that original file still exists
  1879. root.getFile(file1, {
  1880. create : false
  1881. }, function (fileEntry) {
  1882. expect(fileEntry).toBeDefined();
  1883. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1884. // cleanup
  1885. deleteEntry(file1, function () {
  1886. deleteEntry(dstDir, done);
  1887. });
  1888. }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
  1889. }, failed.bind(null, done, 'directory.getDirectory - Error getting directory, making sure that original directory still exists: ' + subDir));
  1890. });
  1891. }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + subDir));
  1892. }, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dstDir));
  1893. }, failed.bind(null, done, 'createFile - Error creating file : ' + file1));
  1894. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  1895. });
  1896. it("file.spec.74 moveTo: directory onto existing file", function (done) {
  1897. if (isIndexedDBShim) {
  1898. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1899. pending();
  1900. }
  1901. var file1 = "entry.move.dof.file1",
  1902. srcDir = "entry.move.dof.srcDir",
  1903. dirPath = joinURL(root.fullPath, srcDir),
  1904. filePath = joinURL(root.fullPath, file1);
  1905. // create a new directory entry to kick off it
  1906. createDirectory(srcDir, function (entry) {
  1907. // create file
  1908. root.getFile(file1, {
  1909. create : true
  1910. }, function (fileEntry) {
  1911. // move directory onto file
  1912. entry.moveTo(root, file1, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, it should not move : \n' + srcDir + ' into root directory renamed as ' + file1 + '\n' + file1 + ' file already exists'), function (error) {
  1913. expect(error).toBeDefined();
  1914. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1915. // test that original directory exists
  1916. root.getDirectory(srcDir, {
  1917. create : false
  1918. }, function (dirEntry) {
  1919. // returning confirms existence so just check fullPath entry
  1920. expect(dirEntry).toBeDefined();
  1921. expect(dirEntry.fullPath).toCanonicallyMatch(dirPath);
  1922. // test that original file exists
  1923. root.getFile(file1, {
  1924. create : false
  1925. }, function (fileEntry) {
  1926. expect(fileEntry).toBeDefined();
  1927. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1928. // cleanup
  1929. deleteEntry(file1, function () {
  1930. deleteEntry(srcDir, done);
  1931. });
  1932. }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
  1933. }, failed.bind(null, done, 'directory.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
  1934. });
  1935. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + file1));
  1936. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1937. });
  1938. it("file.spec.75 copyTo: directory onto existing file", function (done) {
  1939. if (isIndexedDBShim) {
  1940. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1941. pending();
  1942. }
  1943. var file1 = "entry.copy.dof.file1",
  1944. srcDir = "entry.copy.dof.srcDir",
  1945. dirPath = joinURL(root.fullPath, srcDir),
  1946. filePath = joinURL(root.fullPath, file1);
  1947. // create a new directory entry to kick off it
  1948. createDirectory(srcDir, function (entry) {
  1949. // create file
  1950. root.getFile(file1, {
  1951. create : true
  1952. }, function () {
  1953. // copy directory onto file
  1954. entry.copyTo(root, file1, succeed.bind(null, done, 'entry.copyTo - Unexpected success callback, it should not copy : \n' + srcDir + ' into root directory renamed as ' + file1 + '\n' + file1 + ' file already exists'), function (error) {
  1955. expect(error).toBeDefined();
  1956. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  1957. //check that original dir still exists
  1958. root.getDirectory(srcDir, {
  1959. create : false
  1960. }, function (dirEntry) {
  1961. // returning confirms existence so just check fullPath entry
  1962. expect(dirEntry).toBeDefined();
  1963. expect(dirEntry.fullPath).toCanonicallyMatch(dirPath);
  1964. // test that original file still exists
  1965. root.getFile(file1, {
  1966. create : false
  1967. }, function (fileEntry) {
  1968. expect(fileEntry).toBeDefined();
  1969. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  1970. // cleanup
  1971. deleteEntry(file1, function () {
  1972. deleteEntry(srcDir, done);
  1973. });
  1974. }, failed.bind(null, done, 'root.getFile - Error getting file, making sure that original file still exists: ' + file1));
  1975. }, failed.bind(null, done, 'root.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
  1976. });
  1977. }, failed.bind(null, done, 'root.getFile - Error creating file : ' + file1));
  1978. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  1979. });
  1980. it("file.spec.76 moveTo: directory onto directory that is not empty", function (done) {
  1981. if (isIndexedDBShim) {
  1982. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  1983. pending();
  1984. }
  1985. var srcDir = "entry.move.dod.srcDir",
  1986. dstDir = "entry.move.dod.dstDir",
  1987. subDir = "subDir",
  1988. srcPath = joinURL(root.fullPath, srcDir),
  1989. dstPath = joinURL(joinURL(root.fullPath, dstDir), subDir);
  1990. // ensure destination directory is cleaned up before it
  1991. deleteEntry(dstDir, function () {
  1992. // create a new file entry to kick off it
  1993. createDirectory(srcDir, function (entry) {
  1994. // create top level directory
  1995. root.getDirectory(dstDir, {
  1996. create : true
  1997. }, function (directory) {
  1998. // create sub-directory
  1999. directory.getDirectory(subDir, {
  2000. create : true
  2001. }, function () {
  2002. // move srcDir onto dstDir (not empty)
  2003. entry.moveTo(root, dstDir, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, it should not copy : \n' + srcDir + ' into root directory renamed as ' + dstDir + '\n' + dstDir + ' directory already exists'), function (error) {
  2004. expect(error).toBeDefined();
  2005. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  2006. // making sure destination directory still exists
  2007. directory.getDirectory(subDir, {
  2008. create : false
  2009. }, function (dirEntry) {
  2010. // returning confirms existence so just check fullPath entry
  2011. expect(dirEntry).toBeDefined();
  2012. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  2013. // making sure source directory exists
  2014. root.getDirectory(srcDir, {
  2015. create : false
  2016. }, function (srcEntry) {
  2017. expect(srcEntry).toBeDefined();
  2018. expect(srcEntry.fullPath).toCanonicallyMatch(srcPath);
  2019. // cleanup
  2020. deleteEntry(srcDir, function () {
  2021. deleteEntry(dstDir, done);
  2022. });
  2023. }, failed.bind(null, done, 'root.getDirectory - Error getting directory, making sure that original directory still exists: ' + srcDir));
  2024. }, failed.bind(null, done, 'directory.getDirectory - Error getting directory, making sure that original directory still exists: ' + subDir));
  2025. });
  2026. }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + subDir));
  2027. }, failed.bind(null, done, 'directory.getDirectory - Error creating directory : ' + subDir));
  2028. }, failed.bind(null, done, 'createDirectory - Error creating directory : ' + srcDir));
  2029. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  2030. });
  2031. it("file.spec.77 moveTo: file replace existing file", function (done) {
  2032. var file1 = "entry.move.frf.file1",
  2033. file2 = "entry.move.frf.file2",
  2034. file2Path = joinURL(root.fullPath, file2);
  2035. // create a new directory entry to kick off it
  2036. createFile(file1, function (entry) {
  2037. // create file
  2038. root.getFile(file2, {
  2039. create : true
  2040. }, function () {
  2041. // replace file2 with file1
  2042. entry.moveTo(root, file2, function (entry2) {
  2043. expect(entry2).toBeDefined();
  2044. expect(entry2.isFile).toBe(true);
  2045. expect(entry2.isDirectory).toBe(false);
  2046. expect(entry2.fullPath).toCanonicallyMatch(file2Path);
  2047. expect(entry2.name).toCanonicallyMatch(file2);
  2048. // old file should not exists
  2049. root.getFile(file1, {
  2050. create : false
  2051. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, file: ' + file1 + ' should not exists'), function (error) {
  2052. expect(error).toBeDefined();
  2053. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  2054. // test that new file exists
  2055. root.getFile(file2, {
  2056. create : false
  2057. }, function (fileEntry) {
  2058. expect(fileEntry).toBeDefined();
  2059. expect(fileEntry.fullPath).toCanonicallyMatch(file2Path);
  2060. // cleanup
  2061. deleteEntry(file1, function () {
  2062. deleteEntry(file2, done);
  2063. });
  2064. }, failed.bind(null, done, 'root.getFile - Error getting moved file: ' + file2));
  2065. });
  2066. }, failed.bind(null, done, 'entry.moveTo - Error moving file : ' + file1 + ' to root as: ' + file2));
  2067. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + file2));
  2068. }, failed.bind(null, done, 'createFile - Error creating file: ' + file1));
  2069. });
  2070. it("file.spec.78 moveTo: directory replace empty directory", function (done) {
  2071. if (isIndexedDBShim) {
  2072. /* `copyTo` and `moveTo` functions do not support directories (Firefox, IE) */
  2073. pending();
  2074. }
  2075. var file1 = "file1",
  2076. srcDir = "entry.move.drd.srcDir",
  2077. dstDir = "entry.move.drd.dstDir",
  2078. dstPath = joinURL(root.fullPath, dstDir),
  2079. filePath = dstPath + '/' + file1;
  2080. // ensure destination directory is cleaned up before it
  2081. deleteEntry(dstDir, function () {
  2082. // create a new directory entry to kick off it
  2083. createDirectory(srcDir, function (directory) {
  2084. // create a file within source directory
  2085. directory.getFile(file1, {
  2086. create : true
  2087. }, function () {
  2088. // create destination directory
  2089. root.getDirectory(dstDir, {
  2090. create : true
  2091. }, function () {
  2092. // move srcDir to dstDir
  2093. directory.moveTo(root, dstDir, function (dirEntry) {
  2094. expect(dirEntry).toBeDefined();
  2095. expect(dirEntry.isFile).toBe(false);
  2096. expect(dirEntry.isDirectory).toBe(true);
  2097. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  2098. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  2099. // check that old directory contents have been moved
  2100. dirEntry.getFile(file1, {
  2101. create : false
  2102. }, function (fileEntry) {
  2103. expect(fileEntry).toBeDefined();
  2104. expect(fileEntry.fullPath).toCanonicallyMatch(filePath);
  2105. // check that old directory no longer exists
  2106. root.getDirectory(srcDir, {
  2107. create : false
  2108. }, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory: ' + srcDir + ' should not exists'), function (error) {
  2109. expect(error).toBeDefined();
  2110. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  2111. // cleanup
  2112. deleteEntry(srcDir, function () {
  2113. deleteEntry(dstDir, done);
  2114. });
  2115. });
  2116. }, failed.bind(null, done, 'dirEntry.getFile - Error getting moved file: ' + file1));
  2117. }, failed.bind(null, done, 'entry.moveTo - Error moving directory : ' + srcDir + ' to root as: ' + dstDir));
  2118. }, failed.bind(null, done, 'root.getDirectory - Error creating directory: ' + dstDir));
  2119. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + file1));
  2120. }, failed.bind(null, done, 'createDirectory - Error creating directory: ' + srcDir));
  2121. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  2122. });
  2123. it("file.spec.79 moveTo: directory that does not exist", function (done) {
  2124. var file1 = "entry.move.dnf.file1",
  2125. dstDir = "entry.move.dnf.dstDir",
  2126. dstPath = joinURL(root.fullPath, dstDir);
  2127. // create a new file entry to kick off it
  2128. createFile(file1, function (entry) {
  2129. // move file to directory that does not exist
  2130. var directory = new DirectoryEntry();
  2131. directory.filesystem = root.filesystem;
  2132. directory.fullPath = dstPath;
  2133. entry.moveTo(directory, null, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, parent directory: ' + dstPath + ' should not exists'), function (error) {
  2134. expect(error).toBeDefined();
  2135. if (isChrome) {
  2136. /*INVALID_MODIFICATION_ERR (code: 9) is thrown instead of NOT_FOUND_ERR(code: 1)
  2137. on trying to moveTo directory that does not exist.*/
  2138. expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
  2139. } else {
  2140. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  2141. }
  2142. // cleanup
  2143. deleteEntry(file1, done);
  2144. });
  2145. }, failed.bind(null, done, 'createFile - Error creating file: ' + file1));
  2146. });
  2147. it("file.spec.80 moveTo: invalid target name", function (done) {
  2148. if (isBrowser) {
  2149. /*The plugin does not follow ["8.3 Naming restrictions"]
  2150. (http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions*/
  2151. pending();
  2152. }
  2153. var file1 = "entry.move.itn.file1",
  2154. file2 = "bad:file:name";
  2155. // create a new file entry to kick off it
  2156. createFile(file1, function (entry) {
  2157. // move file1 to file2
  2158. entry.moveTo(root, file2, succeed.bind(null, done, 'entry.moveTo - Unexpected success callback, : ' + file1 + ' to root as: ' + file2), function (error) {
  2159. expect(error).toBeDefined();
  2160. expect(error).toBeFileError(FileError.ENCODING_ERR);
  2161. // cleanup
  2162. deleteEntry(file1, done);
  2163. });
  2164. }, failed.bind(null, done, 'createFile - Error creating file: ' + file1));
  2165. });
  2166. });
  2167. //Entry
  2168. describe('FileReader', function () {
  2169. it("file.spec.81 should have correct methods", function () {
  2170. var reader = new FileReader();
  2171. expect(reader).toBeDefined();
  2172. expect(typeof reader.readAsBinaryString).toBe('function');
  2173. expect(typeof reader.readAsDataURL).toBe('function');
  2174. expect(typeof reader.readAsText).toBe('function');
  2175. expect(typeof reader.readAsArrayBuffer).toBe('function');
  2176. expect(typeof reader.abort).toBe('function');
  2177. expect(reader.result).toBe(null);
  2178. });
  2179. });
  2180. //FileReader
  2181. describe('Read method', function () {
  2182. it("file.spec.82 should error out on non-existent file", function (done) {
  2183. var fileName = cordova.platformId === 'windowsphone' ? root.toURL() + "/" + "somefile.txt" : "somefile.txt",
  2184. verifier = function (evt) {
  2185. expect(evt).toBeDefined();
  2186. expect(evt.target.error).toBeFileError(FileError.NOT_FOUND_ERR);
  2187. done();
  2188. };
  2189. root.getFile(fileName, {
  2190. create : true
  2191. }, function (entry) {
  2192. entry.file(function (file) {
  2193. deleteEntry(fileName, function () {
  2194. //Create FileReader
  2195. var reader = new FileReader();
  2196. reader.onerror = verifier;
  2197. reader.onload = succeed.bind(null, done, 'reader.onload - Unexpected success callback, file: ' + fileName + ' it should not exists');
  2198. reader.readAsText(file);
  2199. }, failed.bind(null, done, 'deleteEntry - Error removing file: ' + fileName));
  2200. }, failed.bind(null, done, 'entry.file - Error reading file: ' + fileName));
  2201. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  2202. });
  2203. it("file.spec.83 should be able to read native blob objects", function (done) {
  2204. // Skip test if blobs are not supported (e.g.: Android 2.3).
  2205. if (typeof window.Blob == 'undefined' || typeof window.Uint8Array == 'undefined') {
  2206. expect(true).toFailWithMessage('Platform does not supported this feature');
  2207. done();
  2208. }
  2209. var contents = 'asdf';
  2210. var uint8Array = new Uint8Array(contents.length);
  2211. for (var i = 0; i < contents.length; ++i) {
  2212. uint8Array[i] = contents.charCodeAt(i);
  2213. }
  2214. var Builder = window.BlobBuilder || window.WebKitBlobBuilder;
  2215. var blob;
  2216. if (Builder) {
  2217. var builder = new Builder();
  2218. builder.append(uint8Array.buffer);
  2219. builder.append(contents);
  2220. blob = builder.getBlob("text/plain");
  2221. } else {
  2222. try {
  2223. // iOS 6 does not support Views, so pass in the buffer.
  2224. blob = new Blob([uint8Array.buffer, contents]);
  2225. } catch (e) {
  2226. // Skip the test if we can't create a blob (e.g.: iOS 5).
  2227. if (e instanceof TypeError) {
  2228. expect(true).toFailWithMessage('Platform does not supported this feature');
  2229. done();
  2230. }
  2231. throw e;
  2232. }
  2233. }
  2234. var verifier = function (evt) {
  2235. expect(evt).toBeDefined();
  2236. expect(evt.target.result).toBe('asdfasdf');
  2237. done();
  2238. };
  2239. var reader = new FileReader();
  2240. reader.onloadend = verifier;
  2241. reader.readAsText(blob);
  2242. });
  2243. function writeDummyFile(writeBinary, callback, done, fileContents) {
  2244. var fileName = "dummy.txt",
  2245. fileEntry = null,
  2246. // use default string if file data is not provided
  2247. fileData = fileContents !== undefined ? fileContents :
  2248. '\u20AC\xEB - There is an exception to every rule. Except this one.',
  2249. fileDataAsBinaryString = fileContents !== undefined ? fileContents :
  2250. '\xe2\x82\xac\xc3\xab - There is an exception to every rule. Except this one.';
  2251. function createWriter(fe) {
  2252. fileEntry = fe;
  2253. fileEntry.createWriter(writeFile, failed.bind(null, done, 'fileEntry.createWriter - Error reading file: ' + fileName));
  2254. }
  2255. // writes file and reads it back in
  2256. function writeFile(writer) {
  2257. writer.onwriteend = function () {
  2258. fileEntry.file(function (f) {
  2259. callback(fileEntry, f, fileData, fileDataAsBinaryString);
  2260. }, failed.bind(null, done, 'writer.onwriteend - Error writing data on file: ' + fileName));
  2261. };
  2262. writer.write(fileData);
  2263. }
  2264. fileData += writeBinary ? 'bin:\x01\x00' : '';
  2265. fileDataAsBinaryString += writeBinary ? 'bin:\x01\x00' : '';
  2266. // create a file, write to it, and read it in again
  2267. createFile(fileName, createWriter, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2268. }
  2269. function runReaderTest(funcName, writeBinary, done, progressFunc, verifierFunc, sliceStart, sliceEnd, fileContents) {
  2270. writeDummyFile(writeBinary, function (fileEntry, file, fileData, fileDataAsBinaryString) {
  2271. var verifier = function (evt) {
  2272. expect(evt).toBeDefined();
  2273. verifierFunc(evt, fileData, fileDataAsBinaryString);
  2274. };
  2275. var reader = new FileReader();
  2276. reader.onprogress = progressFunc;
  2277. reader.onload = verifier;
  2278. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + file + ' using function: ' + funcName);
  2279. if (sliceEnd !== undefined) {
  2280. // 'type' is specified so that is will be preserved in the resulting file:
  2281. // http://www.w3.org/TR/FileAPI/#slice-method-algo -> "6.4.1. The slice method" -> 4. A), 6. c)
  2282. file = file.slice(sliceStart, sliceEnd, file.type);
  2283. } else if (sliceStart !== undefined) {
  2284. file = file.slice(sliceStart, file.size, file.type);
  2285. }
  2286. reader[funcName](file);
  2287. }, done, fileContents);
  2288. }
  2289. function arrayBufferEqualsString(ab, str) {
  2290. var buf = new Uint8Array(ab);
  2291. var match = buf.length == str.length;
  2292. for (var i = 0; match && i < buf.length; i++) {
  2293. match = buf[i] == str.charCodeAt(i);
  2294. }
  2295. return match;
  2296. }
  2297. it("file.spec.84 should read file properly, readAsText", function (done) {
  2298. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2299. expect(evt.target.result).toBe(fileData);
  2300. done();
  2301. });
  2302. });
  2303. it("file.spec.84.1 should read JSON file properly, readAsText", function (done) {
  2304. var testObject = {key1: "value1", key2: 2};
  2305. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2306. expect(evt.target.result).toEqual(JSON.stringify(testObject));
  2307. done();
  2308. }, undefined, undefined, JSON.stringify(testObject));
  2309. });
  2310. it("file.spec.85 should read file properly, Data URI", function (done) {
  2311. runReaderTest('readAsDataURL', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2312. /* `readAsDataURL` function is supported, but the mediatype in Chrome depends on entry name extension,
  2313. mediatype in IE is always empty (which is the same as `text-plain` according the specification),
  2314. the mediatype in Firefox is always `application/octet-stream`.
  2315. For example, if the content is `abcdefg` then Firefox returns `data:application/octet-stream;base64,YWJjZGVmZw==`,
  2316. IE returns `data:;base64,YWJjZGVmZw==`, Chrome returns `data:<mediatype depending on extension of entry name>;base64,YWJjZGVmZw==`. */
  2317. expect(evt.target.result).toBeDataUrl();
  2318. //The atob function it is completely ignored during mobilespec execution, besides the returned object: evt
  2319. //it is encoded and the atob function is aimed to decode a string. Even with btoa (encode) the function it gets stucked
  2320. //because of the Unicode characters that contains the fileData object.
  2321. //Issue reported at JIRA with all the details: CB-7095
  2322. //expect(evt.target.result.slice(23)).toBe(atob(fileData));
  2323. done();
  2324. });
  2325. });
  2326. it("file.spec.86 should read file properly, readAsBinaryString", function (done) {
  2327. if (isIE) {
  2328. /*`readAsBinaryString` function is not supported by IE and has not the stub.*/
  2329. pending();
  2330. }
  2331. runReaderTest('readAsBinaryString', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2332. expect(evt.target.result).toBe(fileDataAsBinaryString);
  2333. done();
  2334. });
  2335. });
  2336. it("file.spec.87 should read file properly, readAsArrayBuffer", function (done) {
  2337. // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
  2338. if (typeof window.ArrayBuffer == 'undefined') {
  2339. expect(true).toFailWithMessage('Platform does not supported this feature');
  2340. done();
  2341. }
  2342. runReaderTest('readAsArrayBuffer', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2343. expect(arrayBufferEqualsString(evt.target.result, fileDataAsBinaryString)).toBe(true);
  2344. done();
  2345. });
  2346. });
  2347. it("file.spec.88 should read sliced file: readAsText", function (done) {
  2348. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2349. expect(evt.target.result).toBe(fileDataAsBinaryString.slice(10, 40));
  2350. done();
  2351. }, 10, 40);
  2352. });
  2353. it("file.spec.89 should read sliced file: slice past eof", function (done) {
  2354. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2355. expect(evt.target.result).toBe(fileData.slice(-5, 9999));
  2356. done();
  2357. }, -5, 9999);
  2358. });
  2359. it("file.spec.90 should read sliced file: slice to eof", function (done) {
  2360. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2361. expect(evt.target.result).toBe(fileData.slice(-5));
  2362. done();
  2363. }, -5);
  2364. });
  2365. it("file.spec.91 should read empty slice", function (done) {
  2366. runReaderTest('readAsText', false, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2367. expect(evt.target.result).toBe('');
  2368. done();
  2369. }, 0, 0);
  2370. });
  2371. it("file.spec.92 should read sliced file properly, readAsDataURL", function (done) {
  2372. runReaderTest('readAsDataURL', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2373. /* `readAsDataURL` function is supported, but the mediatype in Chrome depends on entry name extension,
  2374. mediatype in IE is always empty (which is the same as `text-plain` according the specification),
  2375. the mediatype in Firefox is always `application/octet-stream`.
  2376. For example, if the content is `abcdefg` then Firefox returns `data:application/octet-stream;base64,YWJjZGVmZw==`,
  2377. IE returns `data:;base64,YWJjZGVmZw==`, Chrome returns `data:<mediatype depending on extension of entry name>;base64,YWJjZGVmZw==`. */
  2378. expect(evt.target.result).toBeDataUrl();
  2379. //The atob function it is completely ignored during mobilespec execution, besides the returned object: evt
  2380. //it is encoded and the atob function is aimed to decode a string. Even with btoa (encode) the function it gets stucked
  2381. //because of the Unicode characters that contains the fileData object.
  2382. //Issue reported at JIRA with all the details: CB-7095
  2383. //expect(evt.target.result.slice(23)).toBe(atob(fileDataAsBinaryString.slice(10, -3)));
  2384. done();
  2385. }, 10, -3);
  2386. });
  2387. it("file.spec.93 should read sliced file properly, readAsBinaryString", function (done) {
  2388. if (isIE) {
  2389. /*`readAsBinaryString` function is not supported by IE and has not the stub.*/
  2390. pending();
  2391. }
  2392. runReaderTest('readAsBinaryString', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2393. expect(evt.target.result).toBe(fileDataAsBinaryString.slice(-10, -5));
  2394. done();
  2395. }, -10, -5);
  2396. });
  2397. it("file.spec.94 should read sliced file properly, readAsArrayBuffer", function (done) {
  2398. // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
  2399. if (typeof window.ArrayBuffer == 'undefined') {
  2400. expect(true).toFailWithMessage('Platform does not supported this feature');
  2401. done();
  2402. }
  2403. runReaderTest('readAsArrayBuffer', true, done, null, function (evt, fileData, fileDataAsBinaryString) {
  2404. expect(arrayBufferEqualsString(evt.target.result, fileDataAsBinaryString.slice(0, -1))).toBe(true);
  2405. done();
  2406. }, 0, -1);
  2407. });
  2408. it("file.spec.94.5 should read large file in multiple chunks, readAsArrayBuffer", function (done) {
  2409. // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
  2410. if (typeof window.ArrayBuffer == 'undefined') {
  2411. expect(true).toFailWithMessage('Platform does not supported this feature');
  2412. done();
  2413. }
  2414. var largeText = "";
  2415. for (var i = 0; i < 1000; i++) {
  2416. largeText += "Test " + i + "\n";
  2417. }
  2418. // Set the chunk size so that the read will take 5 chunks
  2419. FileReader.READ_CHUNK_SIZE = Math.floor(largeText.length / 4) + 1;
  2420. var chunkCount = 0;
  2421. var lastProgressValue = -1;
  2422. var progressFunc = function (evt) {
  2423. expect(evt.loaded).toBeDefined();
  2424. expect(evt.total).toBeDefined();
  2425. expect(evt.total >= largeText.length).toBe(true);
  2426. expect(evt.total <= largeText.length + 5).toBe(true);
  2427. expect(evt.loaded > lastProgressValue).toBe(true);
  2428. expect(evt.loaded <= evt.total).toBe(true);
  2429. lastProgressValue = evt.loaded;
  2430. chunkCount++;
  2431. };
  2432. runReaderTest(
  2433. 'readAsArrayBuffer', true, done, progressFunc,
  2434. function (evt, fileData, fileDataAsBinaryString) {
  2435. expect(arrayBufferEqualsString(evt.target.result, fileDataAsBinaryString.slice(0, -1))).toBe(true);
  2436. expect(lastProgressValue >= largeText.length).toBe(true);
  2437. expect(lastProgressValue <= largeText.length + 5).toBe(true);
  2438. expect(chunkCount).toBe(5);
  2439. done();
  2440. },
  2441. 0, -1, largeText);
  2442. });
  2443. it("file.spec.94.6 should read large file in multiple chunks, readAsDataURL", function (done) {
  2444. var largeText = "";
  2445. for (var i = 0; i < 10; i++) {
  2446. largeText += "Test " + i + "\n";
  2447. }
  2448. // Set the chunk size so that the read will take 5 chunks
  2449. FileReader.READ_CHUNK_SIZE = Math.floor(largeText.length / 4) + 1;
  2450. var lastProgressValue = 0;
  2451. var progressFunc = function (evt) {
  2452. expect(evt.total).toBeDefined();
  2453. expect(evt.total).toEqual(largeText.length);
  2454. expect(evt.loaded).toBeDefined();
  2455. expect(evt.loaded).toBeGreaterThan(lastProgressValue);
  2456. expect(evt.loaded).toBeLessThan(evt.total + 1);
  2457. lastProgressValue = evt.loaded;
  2458. };
  2459. runReaderTest('readAsDataURL', false, done, progressFunc,
  2460. function (evt, fileData, fileDataAsBinaryString) {
  2461. expect(function () {
  2462. // Cut off data uri prefix
  2463. var base64Data = evt.target.result.substring(evt.target.result.indexOf(',') + 1);
  2464. expect(window.atob(base64Data)).toEqual(fileData);
  2465. }).not.toThrow();
  2466. expect(lastProgressValue).toEqual(largeText.length);
  2467. done();
  2468. },
  2469. undefined, undefined, largeText);
  2470. });
  2471. });
  2472. //Read method
  2473. describe('FileWriter', function () {
  2474. it("file.spec.95 should have correct methods", function (done) {
  2475. // retrieve a FileWriter object
  2476. var fileName = "writer.methods";
  2477. // FileWriter
  2478. root.getFile(fileName, {
  2479. create : true
  2480. }, function (fileEntry) {
  2481. fileEntry.createWriter(function (writer) {
  2482. expect(writer).toBeDefined();
  2483. expect(typeof writer.write).toBe('function');
  2484. expect(typeof writer.seek).toBe('function');
  2485. expect(typeof writer.truncate).toBe('function');
  2486. expect(typeof writer.abort).toBe('function');
  2487. // cleanup
  2488. deleteFile(fileName, done);
  2489. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2490. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  2491. });
  2492. it("file.spec.96 should be able to write and append to file, createWriter", function (done) {
  2493. var fileName = "writer.append.createWriter", // file content
  2494. content = "There is an exception to every rule.", // for checkin file length
  2495. exception = " Except this one.",
  2496. length = content.length;
  2497. // create file, then write and append to it
  2498. createFile(fileName, function (fileEntry) {
  2499. // writes initial file content
  2500. fileEntry.createWriter(function (writer) {
  2501. //Verifiers declaration
  2502. function verifier(evt) {
  2503. expect(writer.length).toBe(length);
  2504. expect(writer.position).toBe(length);
  2505. // Append some more data
  2506. writer.onwriteend = secondVerifier;
  2507. length += exception.length;
  2508. writer.seek(writer.length);
  2509. writer.write(exception);
  2510. }
  2511. function secondVerifier(evt) {
  2512. expect(writer.length).toBe(length);
  2513. expect(writer.position).toBe(length);
  2514. var reader = new FileReader();
  2515. reader.onloadend = thirdVerifier;
  2516. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
  2517. fileEntry.file(function(f){reader.readAsText(f);});
  2518. }
  2519. function thirdVerifier(evt) {
  2520. expect(evt.target.result).toBe(content+exception);
  2521. // cleanup
  2522. deleteFile(fileName, done);
  2523. }
  2524. //Write process
  2525. writer.onwriteend = verifier;
  2526. writer.write(content);
  2527. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2528. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2529. });
  2530. it("file.spec.97 should be able to write and append to file, File object", function (done) {
  2531. var fileName = "writer.append.File", // file content
  2532. content = "There is an exception to every rule.", // for checkin file length
  2533. exception = " Except this one.",
  2534. length = content.length;
  2535. root.getFile(fileName, {
  2536. create : true
  2537. }, function (fileEntry) {
  2538. fileEntry.createWriter(function (writer) {
  2539. //Verifiers declaration
  2540. function verifier() {
  2541. expect(writer.length).toBe(length);
  2542. expect(writer.position).toBe(length);
  2543. // Append some more data
  2544. writer.onwriteend = secondVerifier;
  2545. length += exception.length;
  2546. writer.seek(writer.length);
  2547. writer.write(exception);
  2548. }
  2549. function secondVerifier() {
  2550. expect(writer.length).toBe(length);
  2551. expect(writer.position).toBe(length);
  2552. var reader = new FileReader();
  2553. reader.onloadend = thirdVerifier;
  2554. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
  2555. fileEntry.file(function(f){reader.readAsText(f);});
  2556. }
  2557. function thirdVerifier(evt) {
  2558. expect(evt.target.result).toBe(content+exception);
  2559. // cleanup
  2560. deleteFile(fileName, done);
  2561. }
  2562. //Write process
  2563. writer.onwriteend = verifier;
  2564. writer.write(content);
  2565. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2566. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
  2567. });
  2568. it("file.spec.98 should be able to seek to the middle of the file and write more data than file.length", function (done) {
  2569. var fileName = "writer.seek.write", // file content
  2570. content = "This is our sentence.", // for checking file length
  2571. exception = "newer sentence.",
  2572. length = content.length;
  2573. // create file, then write and append to it
  2574. createFile(fileName, function (fileEntry) {
  2575. fileEntry.createWriter(function (writer) {
  2576. //Verifiers declaration
  2577. function verifier(evt) {
  2578. expect(writer.length).toBe(length);
  2579. expect(writer.position).toBe(length);
  2580. // Append some more data
  2581. writer.onwriteend = secondVerifier;
  2582. length = 12 + exception.length;
  2583. writer.seek(12);
  2584. writer.write(exception);
  2585. }
  2586. function secondVerifier(evt) {
  2587. expect(writer.length).toBe(length);
  2588. expect(writer.position).toBe(length);
  2589. var reader = new FileReader();
  2590. reader.onloadend = thirdVerifier;
  2591. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
  2592. fileEntry.file(function(f){reader.readAsText(f);});
  2593. }
  2594. function thirdVerifier(evt) {
  2595. expect(evt.target.result).toBe(content.substr(0,12)+exception);
  2596. // cleanup
  2597. deleteFile(fileName, done);
  2598. }
  2599. //Write process
  2600. writer.onwriteend = verifier;
  2601. writer.write(content);
  2602. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2603. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2604. });
  2605. it("file.spec.99 should be able to seek to the middle of the file and write less data than file.length", function (done) {
  2606. if (isChrome) {
  2607. /* Chrome (re)writes as follows: "This is our sentence." -> "This is new.sentence.",
  2608. i.e. the length is not being changed from content.length and writer length will be equal 21 */
  2609. pending();
  2610. }
  2611. var fileName = "writer.seek.write2", // file content
  2612. content = "This is our sentence.", // for checking file length
  2613. exception = "new.",
  2614. length = content.length;
  2615. // create file, then write and append to it
  2616. createFile(fileName, function (fileEntry) {
  2617. fileEntry.createWriter(function (writer) {
  2618. // Verifiers declaration
  2619. function verifier(evt) {
  2620. expect(writer.length).toBe(length);
  2621. expect(writer.position).toBe(length);
  2622. // Append some more data
  2623. writer.onwriteend = secondVerifier;
  2624. length = 8 + exception.length;
  2625. writer.seek(8);
  2626. writer.write(exception);
  2627. }
  2628. function secondVerifier(evt) {
  2629. expect(writer.length).toBe(length);
  2630. expect(writer.position).toBe(length);
  2631. var reader = new FileReader();
  2632. reader.onloadend = thirdVerifier;
  2633. reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
  2634. fileEntry.file(function(f){reader.readAsText(f);});
  2635. }
  2636. function thirdVerifier(evt) {
  2637. expect(evt.target.result).toBe(content.substr(0,8)+exception);
  2638. // cleanup
  2639. deleteFile(fileName, done);
  2640. }
  2641. //Write process
  2642. writer.onwriteend = verifier;
  2643. writer.write(content);
  2644. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2645. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2646. });
  2647. it("file.spec.100 should be able to write XML data", function (done) {
  2648. var fileName = "writer.xml", // file content
  2649. content = '<?xml version="1.0" encoding="UTF-8"?>\n<test prop="ack">\nData\n</test>\n', // for testing file length
  2650. length = content.length;
  2651. // creates file, then write XML data
  2652. createFile(fileName, function (fileEntry) {
  2653. fileEntry.createWriter(function (writer) {
  2654. //Verifier content
  2655. var verifier = function (evt) {
  2656. expect(writer.length).toBe(length);
  2657. expect(writer.position).toBe(length);
  2658. // cleanup
  2659. deleteFile(fileName, done);
  2660. };
  2661. //Write process
  2662. writer.onwriteend = verifier;
  2663. writer.write(content);
  2664. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2665. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2666. });
  2667. it("file.spec.101 should be able to write JSON data", function (done) {
  2668. var fileName = "writer.json", // file content
  2669. content = '{ "name": "Guy Incognito", "email": "here@there.com" }', // for testing file length
  2670. length = content.length;
  2671. // creates file, then write JSON content
  2672. createFile(fileName, function (fileEntry) {
  2673. fileEntry.createWriter(function (writer) {
  2674. //Verifier declaration
  2675. var verifier = function (evt) {
  2676. expect(writer.length).toBe(length);
  2677. expect(writer.position).toBe(length);
  2678. // cleanup
  2679. deleteFile(fileName, done);
  2680. };
  2681. //Write process
  2682. writer.onwriteend = verifier;
  2683. writer.write(content);
  2684. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2685. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2686. });
  2687. it("file.spec.102 should be able to seek", function (done) {
  2688. var fileName = "writer.seek", // file content
  2689. content = "There is an exception to every rule. Except this one.", // for testing file length
  2690. length = content.length;
  2691. // creates file, then write JSON content
  2692. createFile(fileName, function (fileEntry) {
  2693. // writes file content and tests writer.seek
  2694. fileEntry.createWriter(function (writer) {
  2695. //Verifier declaration
  2696. var verifier = function () {
  2697. expect(writer.position).toBe(length);
  2698. writer.seek(-5);
  2699. expect(writer.position).toBe(length - 5);
  2700. writer.seek(length + 100);
  2701. expect(writer.position).toBe(length);
  2702. writer.seek(10);
  2703. expect(writer.position).toBe(10);
  2704. // cleanup
  2705. deleteFile(fileName, done);
  2706. };
  2707. //Write process
  2708. writer.onwriteend = verifier;
  2709. writer.seek(-100);
  2710. expect(writer.position).toBe(0);
  2711. writer.write(content);
  2712. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2713. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2714. });
  2715. it("file.spec.103 should be able to truncate", function (done) {
  2716. if (isIndexedDBShim) {
  2717. /* `abort` and `truncate` functions are not supported (Firefox, IE) */
  2718. pending();
  2719. }
  2720. var fileName = "writer.truncate",
  2721. content = "There is an exception to every rule. Except this one.";
  2722. // creates file, writes to it, then truncates it
  2723. createFile(fileName, function (fileEntry) {
  2724. fileEntry.createWriter(function (writer) {
  2725. // Verifier declaration
  2726. var verifier = function () {
  2727. expect(writer.length).toBe(36);
  2728. expect(writer.position).toBe(36);
  2729. // cleanup
  2730. deleteFile(fileName, done);
  2731. };
  2732. //Write process
  2733. writer.onwriteend = function () {
  2734. //Truncate process after write
  2735. writer.onwriteend = verifier;
  2736. writer.truncate(36);
  2737. };
  2738. writer.write(content);
  2739. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2740. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2741. });
  2742. it("file.spec.104 should be able to write binary data from an ArrayBuffer", function (done) {
  2743. // Skip test if ArrayBuffers are not supported (e.g.: Android 2.3).
  2744. if (typeof window.ArrayBuffer == 'undefined') {
  2745. expect(true).toFailWithMessage('Platform does not supported this feature');
  2746. done();
  2747. return;
  2748. }
  2749. var fileName = "bufferwriter.bin", // file content
  2750. data = new ArrayBuffer(32),
  2751. dataView = new Int8Array(data), // for verifying file length
  2752. length = 32;
  2753. for (var i = 0; i < dataView.length; i++) {
  2754. dataView[i] = i;
  2755. }
  2756. // creates file, then write content
  2757. createFile(fileName, function (fileEntry) {
  2758. // writes file content
  2759. fileEntry.createWriter(function (writer) {
  2760. //Verifier declaration
  2761. var verifier = function () {
  2762. expect(writer.length).toBe(length);
  2763. expect(writer.position).toBe(length);
  2764. // cleanup
  2765. deleteFile(fileName, done);
  2766. };
  2767. //Write process
  2768. writer.onwriteend = verifier;
  2769. writer.write(data);
  2770. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2771. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2772. });
  2773. it("file.spec.105 should be able to write binary data from a Blob", function (done) {
  2774. // Skip test if Blobs are not supported (e.g.: Android 2.3).
  2775. if ((typeof window.Blob == 'undefined' && typeof window.WebKitBlobBuilder == 'undefined') || typeof window.ArrayBuffer == 'undefined') {
  2776. expect(true).toFailWithMessage('Platform does not supported this feature');
  2777. done();
  2778. return;
  2779. }
  2780. var fileName = "blobwriter.bin", // file content
  2781. data = new ArrayBuffer(32),
  2782. dataView = new Int8Array(data),
  2783. blob, // for verifying file length
  2784. length = 32;
  2785. for (var i = 0; i < dataView.length; i++) {
  2786. dataView[i] = i;
  2787. }
  2788. try {
  2789. // Mobile Safari: Use Blob constructor
  2790. blob = new Blob([data], {
  2791. "type" : "application/octet-stream"
  2792. });
  2793. } catch (e) {
  2794. if (window.WebKitBlobBuilder) {
  2795. // Android Browser: Use deprecated BlobBuilder
  2796. var builder = new WebKitBlobBuilder();
  2797. builder.append(data);
  2798. blob = builder.getBlob('application/octet-stream');
  2799. } else {
  2800. // We have no way defined to create a Blob, so fail
  2801. fail();
  2802. }
  2803. }
  2804. if (typeof blob !== 'undefined') {
  2805. // creates file, then write content
  2806. createFile(fileName, function (fileEntry) {
  2807. fileEntry.createWriter(function (writer) {
  2808. //Verifier declaration
  2809. var verifier = function () {
  2810. expect(writer.length).toBe(length);
  2811. expect(writer.position).toBe(length);
  2812. // cleanup
  2813. deleteFile(fileName, done);
  2814. };
  2815. //Write process
  2816. writer.onwriteend = verifier;
  2817. writer.write(blob);
  2818. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2819. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2820. }
  2821. });
  2822. it("file.spec.106 should be able to write a File to a FileWriter", function (done) {
  2823. var dummyFileName = 'dummy.txt',
  2824. outputFileName = 'verify.txt',
  2825. dummyFileText = 'This text should be written to two files',
  2826. verifier = function (outputFileWriter) {
  2827. expect(outputFileWriter.length).toBe(dummyFileText.length);
  2828. expect(outputFileWriter.position).toBe(dummyFileText.length);
  2829. deleteFile(outputFileName, done);
  2830. },
  2831. writeFile = function (fileName, fileData, win) {
  2832. var theWriter,
  2833. write_file = function (fileEntry) {
  2834. // writes file content to new file
  2835. fileEntry.createWriter(function (writer) {
  2836. theWriter = writer;
  2837. writer.onwriteend = function (ev) {
  2838. if (typeof fileData.length !== "undefined") {
  2839. expect(theWriter.length).toBe(fileData.length);
  2840. expect(theWriter.position).toBe(fileData.length);
  2841. }
  2842. win(theWriter);
  2843. };
  2844. writer.onerror = failed.bind(null, done, 'writer.onerror - Error writing content on file: ' + fileName);
  2845. writer.write(fileData);
  2846. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2847. };
  2848. createFile(fileName, write_file, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2849. },
  2850. openFile = function (fileName, callback) {
  2851. root.getFile(fileName, {
  2852. create : false
  2853. }, function (fileEntry) {
  2854. fileEntry.file(callback, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
  2855. }, failed.bind(null, done, 'root.getFile - Error getting file: ' + fileName));
  2856. };
  2857. writeFile(dummyFileName, dummyFileText, function (dummyFileWriter) {
  2858. openFile(dummyFileName, function (file) {
  2859. writeFile(outputFileName, file, verifier);
  2860. });
  2861. });
  2862. });
  2863. it("file.spec.107 should be able to write a sliced File to a FileWriter", function (done) {
  2864. var dummyFileName = 'dummy2.txt',
  2865. outputFileName = 'verify2.txt',
  2866. dummyFileText = 'This text should be written to two files',
  2867. verifier = function (outputFileWriter) {
  2868. expect(outputFileWriter.length).toBe(10);
  2869. expect(outputFileWriter.position).toBe(10);
  2870. deleteFile(outputFileName, done);
  2871. },
  2872. writeFile = function (fileName, fileData, win) {
  2873. var theWriter,
  2874. write_file = function (fileEntry) {
  2875. // writes file content to new file
  2876. fileEntry.createWriter(function (writer) {
  2877. theWriter = writer;
  2878. writer.onwriteend = function (ev) {
  2879. if (typeof fileData.length !== "undefined") {
  2880. expect(theWriter.length).toBe(fileData.length);
  2881. expect(theWriter.position).toBe(fileData.length);
  2882. }
  2883. win(theWriter);
  2884. };
  2885. writer.onerror = failed.bind(null, done, 'writer.onerror - Error writing content on file: ' + fileName);
  2886. writer.write(fileData);
  2887. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2888. };
  2889. createFile(fileName, write_file, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2890. },
  2891. openFile = function (fileName, callback) {
  2892. root.getFile(fileName, {
  2893. create : false
  2894. }, function (fileEntry) {
  2895. fileEntry.file(callback, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
  2896. }, failed.bind(null, done, 'root.getFile - Error getting file: ' + fileName));
  2897. };
  2898. writeFile(dummyFileName, dummyFileText, function (dummyFileWriter) {
  2899. openFile(dummyFileName, function (file) {
  2900. writeFile(outputFileName, file.slice(10, 20), verifier);
  2901. });
  2902. });
  2903. });
  2904. it("file.spec.108 should be able to write binary data from a File", function (done) {
  2905. // Skip test if Blobs are not supported (e.g.: Android 2.3).
  2906. if (typeof window.Blob == 'undefined' && typeof window.WebKitBlobBuilder == 'undefined') {
  2907. expect(true).toFailWithMessage('Platform does not supported this feature');
  2908. done();
  2909. }
  2910. var dummyFileName = "blobwriter.bin",
  2911. outputFileName = 'verify.bin', // file content
  2912. data = new ArrayBuffer(32),
  2913. dataView = new Int8Array(data),
  2914. blob, // for verifying file length
  2915. length = 32,
  2916. verifier = function (outputFileWriter) {
  2917. expect(outputFileWriter.length).toBe(length);
  2918. expect(outputFileWriter.position).toBe(length);
  2919. // cleanup
  2920. deleteFile(outputFileName);
  2921. done();
  2922. },
  2923. writeFile = function (fileName, fileData, win) {
  2924. var theWriter,
  2925. write_file = function (fileEntry) {
  2926. // writes file content to new file
  2927. fileEntry.createWriter(function (writer) {
  2928. theWriter = writer;
  2929. writer.onwriteend = function (ev) {
  2930. if (typeof fileData.length !== "undefined") {
  2931. expect(theWriter.length).toBe(fileData.length);
  2932. expect(theWriter.position).toBe(fileData.length);
  2933. }
  2934. win(theWriter);
  2935. };
  2936. writer.onerror = failed.bind(null, done, 'writer.onerror - Error writing content on file: ' + fileName);
  2937. writer.write(fileData);
  2938. }, failed.bind(null, done, 'fileEntry.createWriter - Error creating writer using fileEntry: ' + fileEntry.name));
  2939. };
  2940. createFile(fileName, write_file, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  2941. },
  2942. openFile = function (fileName, callback) {
  2943. root.getFile(fileName, {
  2944. create : false
  2945. }, function (fileEntry) {
  2946. fileEntry.file(callback, failed.bind(null, done, 'fileEntry.file - Error reading file using fileEntry: ' + fileEntry.name));
  2947. }, failed.bind(null, done, 'root.getFile - Error getting file: ' + fileName));
  2948. };
  2949. for (var i = 0; i < dataView.length; i++) {
  2950. dataView[i] = i;
  2951. }
  2952. try {
  2953. // Mobile Safari: Use Blob constructor
  2954. blob = new Blob([data], {
  2955. "type" : "application/octet-stream"
  2956. });
  2957. } catch (e) {
  2958. if (window.WebKitBlobBuilder) {
  2959. // Android Browser: Use deprecated BlobBuilder
  2960. var builder = new WebKitBlobBuilder();
  2961. builder.append(data);
  2962. blob = builder.getBlob('application/octet-stream');
  2963. } else {
  2964. // We have no way defined to create a Blob, so fail
  2965. fail();
  2966. }
  2967. }
  2968. if (typeof blob !== 'undefined') {
  2969. // creates file, then write content
  2970. writeFile(dummyFileName, blob, function (dummyFileWriter) {
  2971. openFile(dummyFileName, function (file) {
  2972. writeFile(outputFileName, file, verifier);
  2973. });
  2974. });
  2975. }
  2976. });
  2977. });
  2978. //FileWritter
  2979. describe('Backwards compatibility', function () {
  2980. /* These specs exist to test that the File plugin can still recognize file:///
  2981. * URLs, and can resolve them to FileEntry and DirectoryEntry objects.
  2982. * They rely on an undocumented interface to File which provides absolute file
  2983. * paths, which are not used internally anymore.
  2984. * If that interface is not present, then these tests will silently succeed.
  2985. */
  2986. it("file.spec.109 should be able to resolve a file:/// URL", function (done) {
  2987. var localFilename = 'file.txt';
  2988. var originalEntry;
  2989. root.getFile(localFilename, {
  2990. create : true
  2991. }, function (entry) {
  2992. originalEntry = entry;
  2993. /* This is an undocumented interface to File which exists only for testing
  2994. * backwards compatibilty. By obtaining the raw filesystem path of the download
  2995. * location, we can pass that to ft.download() to make sure that previously-stored
  2996. * paths are still valid.
  2997. */
  2998. cordova.exec(function (localPath) {
  2999. window.resolveLocalFileSystemURL("file://" + encodeURI(localPath), function (fileEntry) {
  3000. expect(fileEntry.toURL()).toEqual(originalEntry.toURL());
  3001. // cleanup
  3002. deleteFile(localFilename);
  3003. done();
  3004. }, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving URI: file://' + encodeURI(localPath)));
  3005. }, done, 'File', '_getLocalFilesystemPath', [entry.toURL()]);
  3006. }, failed.bind(null, done, 'root.getFile - Error creating file: ' + localFilename));
  3007. });
  3008. });
  3009. //Backwards Compatibility
  3010. describe('Parent References', function () {
  3011. /* These specs verify that paths with parent references i("..") in them
  3012. * work correctly, and do not cause the application to crash.
  3013. */
  3014. it("file.spec.110 should not throw exception resolving parent refefences", function (done) {
  3015. /* This is a direct copy of file.spec.9, with the filename changed, * as reported in CB-5721.
  3016. */
  3017. var fileName = "resolve.file.uri";
  3018. var dirName = "resolve.dir.uri";
  3019. // create a new file entry
  3020. createDirectory(dirName, function () {
  3021. createFile(dirName+"/../" + fileName, function (entry) {
  3022. // lookup file system entry
  3023. window.resolveLocalFileSystemURL(entry.toURL(), function (fileEntry) {
  3024. expect(fileEntry).toBeDefined();
  3025. expect(fileEntry.name).toCanonicallyMatch(fileName);
  3026. // cleanup
  3027. deleteEntry(fileName, done);
  3028. }, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving URI: ' + entry.toURL()));
  3029. }, failed.bind(null, done, 'createFile - Error creating file: ../' + fileName));
  3030. }, failed.bind(null, done, 'createDirectory - Error creating directory: ' + dirName));
  3031. });
  3032. it("file.spec.111 should not traverse above above the root directory", function (done) {
  3033. var fileName = "traverse.file.uri";
  3034. // create a new file entry
  3035. createFile(fileName, function (entry) {
  3036. // lookup file system entry
  3037. root.getFile('../' + fileName, {
  3038. create : false
  3039. }, function (fileEntry) {
  3040. // Note: we expect this to still resolve, as the correct behaviour is to ignore the ../, not to fail out.
  3041. expect(fileEntry).toBeDefined();
  3042. expect(fileEntry.name).toBe(fileName);
  3043. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath +'/' + fileName);
  3044. // cleanup
  3045. deleteEntry(fileName, done);
  3046. }, failed.bind(null, done, 'root.getFile - Error getting file: ../' + fileName));
  3047. }, failed.bind(null, done, 'createFile - Error creating file: ../' + fileName));
  3048. });
  3049. it("file.spec.112 should traverse above above the current directory", function (done) {
  3050. var fileName = "traverse2.file.uri",
  3051. dirName = "traverse2.subdir";
  3052. // create a new directory and a file entry
  3053. createFile(fileName, function () {
  3054. createDirectory(dirName, function (entry) {
  3055. // lookup file system entry
  3056. entry.getFile('../' + fileName, {
  3057. create : false
  3058. }, function (fileEntry) {
  3059. expect(fileEntry).toBeDefined();
  3060. expect(fileEntry.name).toBe(fileName);
  3061. expect(fileEntry.fullPath).toCanonicallyMatch('/' + fileName);
  3062. // cleanup
  3063. deleteEntry(fileName, function () {
  3064. deleteEntry(dirName, done);
  3065. });
  3066. }, failed.bind(null, done, 'entry.getFile - Error getting file: ' + fileName + ' recently created above: ' + dirName));
  3067. }, failed.bind(null, done, 'createDirectory - Error creating directory: ' + dirName));
  3068. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3069. });
  3070. it("file.spec.113 getFile: get Entry should error for missing file above root directory", function (done) {
  3071. var fileName = "../missing.file";
  3072. // create:false, exclusive:false, file does not exist
  3073. root.getFile(fileName, {
  3074. create : false
  3075. }, succeed.bind(null, done, 'root.getFile - Unexpected success callback, it should not locate nonexistent file: ' + fileName), function (error) {
  3076. expect(error).toBeDefined();
  3077. expect(error).toBeFileError(FileError.NOT_FOUND_ERR);
  3078. done();
  3079. });
  3080. });
  3081. });
  3082. //Parent References
  3083. describe('toNativeURL interface', function () {
  3084. /* These specs verify that FileEntries have a toNativeURL method
  3085. * which appears to be sane.
  3086. */
  3087. var pathExpect = cordova.platformId === 'windowsphone' ? "//nativ" : "file://";
  3088. if (isChrome) {
  3089. pathExpect = 'filesystem:file://';
  3090. }
  3091. it("file.spec.114 fileEntry should have a toNativeURL method", function (done) {
  3092. var fileName = "native.file.uri";
  3093. if (isWindows) {
  3094. var rootPath = root.fullPath;
  3095. pathExpect = rootPath.substr(0, rootPath.indexOf(":"));
  3096. }
  3097. // create a new file entry
  3098. createFile(fileName, function (entry) {
  3099. expect(entry.toNativeURL).toBeDefined();
  3100. expect(entry.name).toCanonicallyMatch(fileName);
  3101. expect(typeof entry.toNativeURL).toBe('function');
  3102. var nativeURL = entry.toNativeURL();
  3103. expect(typeof nativeURL).toBe("string");
  3104. expect(nativeURL.substring(0, pathExpect.length)).toEqual(pathExpect);
  3105. expect(nativeURL.substring(nativeURL.length - fileName.length)).toEqual(fileName);
  3106. // cleanup
  3107. deleteEntry(fileName, done);
  3108. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3109. });
  3110. it("file.spec.115 DirectoryReader should return entries with toNativeURL method", function (done) {
  3111. var dirName = 'nativeEntries.dir',
  3112. fileName = 'nativeEntries.file',
  3113. directory,
  3114. checkEntries = function (entries) {
  3115. expect(entries).toBeDefined();
  3116. expect(entries instanceof Array).toBe(true);
  3117. expect(entries.length).toBe(1);
  3118. expect(entries[0].toNativeURL).toBeDefined();
  3119. expect(typeof entries[0].toNativeURL).toBe('function');
  3120. var nativeURL = entries[0].toNativeURL();
  3121. expect(typeof nativeURL).toBe("string");
  3122. expect(nativeURL.substring(0, pathExpect.length)).toEqual(pathExpect);
  3123. expect(nativeURL.substring(nativeURL.length - fileName.length)).toEqual(fileName);
  3124. // cleanup
  3125. directory.removeRecursively(null, null);
  3126. done();
  3127. };
  3128. // create a new file entry
  3129. root.getDirectory(dirName, {
  3130. create : true
  3131. }, function (dir) {
  3132. directory = dir;
  3133. directory.getFile(fileName, {
  3134. create : true
  3135. }, function (fileEntry) {
  3136. var reader = directory.createReader();
  3137. reader.readEntries(checkEntries, failed.bind(null, done, 'reader.readEntries - Error reading entries from directory: ' + dirName));
  3138. }, failed.bind(null, done, 'directory.getFile - Error creating file: ' + fileName));
  3139. }, failed.bind(null, done, 'root.getDirectory - Error creating directory: ' + dirName));
  3140. });
  3141. it("file.spec.116 resolveLocalFileSystemURL should return entries with toNativeURL method", function (done) {
  3142. var fileName = "native.resolve.uri";
  3143. // create a new file entry
  3144. createFile(fileName, function (entry) {
  3145. resolveLocalFileSystemURL(entry.toURL(), function (entry) {
  3146. expect(entry.toNativeURL).toBeDefined();
  3147. expect(entry.name).toCanonicallyMatch(fileName);
  3148. expect(typeof entry.toNativeURL).toBe('function');
  3149. var nativeURL = entry.toNativeURL();
  3150. expect(typeof nativeURL).toBe("string");
  3151. expect(nativeURL.substring(0, pathExpect.length)).toEqual(pathExpect);
  3152. expect(nativeURL.substring(nativeURL.length - fileName.length)).toEqual(fileName);
  3153. // cleanup
  3154. deleteEntry(fileName, done);
  3155. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toURL()));
  3156. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3157. });
  3158. });
  3159. //toNativeURL interface
  3160. describe('resolveLocalFileSystemURL on file://', function () {
  3161. /* These specs verify that window.resolveLocalFileSystemURL works correctly on file:// URLs
  3162. */
  3163. it("file.spec.117 should not resolve native URLs outside of FS roots", function (done) {
  3164. // lookup file system entry
  3165. window.resolveLocalFileSystemURL("file:///this.is.an.invalid.url", succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Unexpected success callback, it should not resolve invalid URL: file:///this.is.an.invalid.url'), function (error) {
  3166. expect(error).toBeDefined();
  3167. done();
  3168. });
  3169. });
  3170. it("file.spec.118 should not resolve native URLs outside of FS roots", function (done) {
  3171. // lookup file system entry
  3172. window.resolveLocalFileSystemURL("file://localhost/this.is.an.invalid.url", succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Unexpected success callback, it should not resolve invalid URL: file://localhost/this.is.an.invalid.url'), function (error) {
  3173. expect(error).toBeDefined();
  3174. done();
  3175. });
  3176. });
  3177. it("file.spec.119 should not resolve invalid native URLs", function (done) {
  3178. // lookup file system entry
  3179. window.resolveLocalFileSystemURL("file://localhost", succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Unexpected success callback, it should not resolve invalid URL: file://localhost'), function (error) {
  3180. expect(error).toBeDefined();
  3181. done();
  3182. });
  3183. });
  3184. it("file.spec.120 should not resolve invalid native URLs with query strings", function (done) {
  3185. // lookup file system entry
  3186. window.resolveLocalFileSystemURL("file://localhost?test/test", succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Unexpected success callback, it should not resolve invalid URL: file://localhost?test/test'), function (error) {
  3187. expect(error).toBeDefined();
  3188. done();
  3189. });
  3190. });
  3191. it("file.spec.121 should resolve native URLs returned by API", function (done) {
  3192. var fileName = "native.resolve.uri1";
  3193. // create a new file entry
  3194. createFile(fileName, function (entry) {
  3195. resolveLocalFileSystemURL(entry.toNativeURL(), function (fileEntry) {
  3196. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
  3197. // cleanup
  3198. deleteEntry(fileName, done);
  3199. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + entry.toNativeURL()));
  3200. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3201. });
  3202. it("file.spec.122 should resolve native URLs returned by API with localhost", function (done) {
  3203. var fileName = "native.resolve.uri2";
  3204. // create a new file entry
  3205. createFile(fileName, function (entry) {
  3206. var url = entry.toNativeURL();
  3207. url = url.replace("///", "//localhost/");
  3208. resolveLocalFileSystemURL(url, function (fileEntry) {
  3209. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
  3210. // cleanup
  3211. deleteEntry(fileName, done);
  3212. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + url));
  3213. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3214. });
  3215. it("file.spec.123 should resolve native URLs returned by API with query string", function (done) {
  3216. var fileName = "native.resolve.uri3";
  3217. // create a new file entry
  3218. createFile(fileName, function (entry) {
  3219. var url = entry.toNativeURL();
  3220. url = url + "?test/test";
  3221. resolveLocalFileSystemURL(url, function (fileEntry) {
  3222. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
  3223. // cleanup
  3224. deleteEntry(fileName, done);
  3225. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + url));
  3226. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3227. });
  3228. it("file.spec.124 should resolve native URLs returned by API with localhost and query string", function (done) {
  3229. var fileName = "native.resolve.uri4";
  3230. // create a new file entry
  3231. createFile(fileName, function (entry) {
  3232. var url = entry.toNativeURL();
  3233. url = url.replace("///", "//localhost/") + "?test/test";
  3234. resolveLocalFileSystemURL(url, function (fileEntry) {
  3235. expect(fileEntry.fullPath).toCanonicallyMatch(root.fullPath + "/" + fileName);
  3236. // cleanup
  3237. deleteEntry(fileName, done);
  3238. }, failed.bind(null, done, 'resolveLocalFileSystemURL - Error resolving file URL: ' + url));
  3239. }, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
  3240. });
  3241. });
  3242. //resolveLocalFileSystemURL on file://
  3243. describe('cross-file-system copy and move', function () {
  3244. /* These specs verify that Entry.copyTo and Entry.moveTo work correctly
  3245. * when crossing filesystem boundaries.
  3246. */
  3247. it("file.spec.125 copyTo: temporary -> persistent", function (done) {
  3248. var file1 = "entry.copy.file1a",
  3249. file2 = "entry.copy.file2a",
  3250. sourceEntry,
  3251. fullPath = joinURL(root.fullPath, file2),
  3252. validateFile = function (entry) {
  3253. // a bit redundant since copy returned this entry already
  3254. expect(entry).toBeDefined();
  3255. expect(entry.isFile).toBe(true);
  3256. expect(entry.isDirectory).toBe(false);
  3257. expect(entry.name).toCanonicallyMatch(file2);
  3258. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3259. expect(entry.filesystem).toBeDefined();
  3260. if (isChrome) {
  3261. expect(entry.filesystem.name).toContain("Persistent");
  3262. } else {
  3263. expect(entry.filesystem.name).toEqual("persistent");
  3264. }
  3265. // cleanup
  3266. deleteEntry(entry.name);
  3267. deleteEntry(sourceEntry.name, done);
  3268. },
  3269. createSourceAndTransfer = function () {
  3270. temp_root.getFile(file1, {
  3271. create : true
  3272. }, function (entry) {
  3273. expect(entry.filesystem).toBeDefined();
  3274. if (isChrome) {
  3275. expect(entry.filesystem.name).toContain("Temporary");
  3276. } else {
  3277. expect(entry.filesystem.name).toEqual("temporary");
  3278. }
  3279. sourceEntry = entry;
  3280. // Save for later cleanup
  3281. entry.copyTo(persistent_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + file1 + ' to PERSISTENT root as: ' + file2));
  3282. }, failed.bind(null, done, 'temp_root.getFile - Error creating file: ' + file1 + 'at TEMPORAL root'));
  3283. };
  3284. // Delete any existing file to start things off
  3285. persistent_root.getFile(file2, {}, function (entry) {
  3286. entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3287. }, createSourceAndTransfer);
  3288. });
  3289. it("file.spec.126 copyTo: persistent -> temporary", function (done) {
  3290. var file1 = "entry.copy.file1b",
  3291. file2 = "entry.copy.file2b",
  3292. sourceEntry,
  3293. fullPath = joinURL(temp_root.fullPath, file2),
  3294. validateFile = function (entry) {
  3295. expect(entry).toBeDefined();
  3296. expect(entry.isFile).toBe(true);
  3297. expect(entry.isDirectory).toBe(false);
  3298. expect(entry.name).toCanonicallyMatch(file2);
  3299. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3300. if (isChrome) {
  3301. expect(entry.filesystem.name).toContain("Temporary");
  3302. } else {
  3303. expect(entry.filesystem.name).toEqual("temporary");
  3304. }
  3305. // cleanup
  3306. deleteEntry(entry.name);
  3307. deleteEntry(sourceEntry.name, done);
  3308. },
  3309. createSourceAndTransfer = function () {
  3310. persistent_root.getFile(file1, {
  3311. create : true
  3312. }, function (entry) {
  3313. expect(entry).toBeDefined();
  3314. expect(entry.filesystem).toBeDefined();
  3315. if (isChrome) {
  3316. expect(entry.filesystem.name).toContain("Persistent");
  3317. } else {
  3318. expect(entry.filesystem.name).toEqual("persistent");
  3319. }
  3320. sourceEntry = entry;
  3321. // Save for later cleanup
  3322. entry.copyTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + file1 + ' to TEMPORAL root as: ' + file2));
  3323. }, failed.bind(null, done, 'persistent_root.getFile - Error creating file: ' + file1 + 'at PERSISTENT root'));
  3324. };
  3325. // Delete any existing file to start things off
  3326. temp_root.getFile(file2, {}, function (entry) {
  3327. entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3328. }, createSourceAndTransfer);
  3329. });
  3330. it("file.spec.127 moveTo: temporary -> persistent", function (done) {
  3331. var file1 = "entry.copy.file1a",
  3332. file2 = "entry.copy.file2a",
  3333. sourceEntry,
  3334. fullPath = joinURL(root.fullPath, file2),
  3335. validateFile = function (entry) {
  3336. // a bit redundant since copy returned this entry already
  3337. expect(entry).toBeDefined();
  3338. expect(entry.isFile).toBe(true);
  3339. expect(entry.isDirectory).toBe(false);
  3340. expect(entry.name).toCanonicallyMatch(file2);
  3341. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3342. expect(entry.filesystem).toBeDefined();
  3343. if (isChrome) {
  3344. expect(entry.filesystem.name).toContain("Persistent");
  3345. } else {
  3346. expect(entry.filesystem.name).toEqual("persistent");
  3347. }
  3348. // cleanup
  3349. deleteEntry(entry.name);
  3350. deleteEntry(sourceEntry.name, done);
  3351. },
  3352. createSourceAndTransfer = function () {
  3353. temp_root.getFile(file1, {
  3354. create : true
  3355. }, function (entry) {
  3356. expect(entry.filesystem).toBeDefined();
  3357. if (isChrome) {
  3358. expect(entry.filesystem.name).toContain("Temporary");
  3359. } else {
  3360. expect(entry.filesystem.name).toEqual("temporary");
  3361. }
  3362. sourceEntry = entry;
  3363. // Save for later cleanup
  3364. entry.moveTo(persistent_root, file2, validateFile, failed.bind(null, done, 'entry.moveTo - Error moving file: ' + file1 + ' to PERSISTENT root as: ' + file2));
  3365. }, failed.bind(null, done, 'temp_root.getFile - Error creating file: ' + file1 + 'at TEMPORAL root'));
  3366. };
  3367. // Delete any existing file to start things off
  3368. persistent_root.getFile(file2, {}, function (entry) {
  3369. entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3370. }, createSourceAndTransfer);
  3371. });
  3372. it("file.spec.128 moveTo: persistent -> temporary", function (done) {
  3373. var file1 = "entry.copy.file1b",
  3374. file2 = "entry.copy.file2b",
  3375. sourceEntry,
  3376. fullPath = joinURL(temp_root.fullPath, file2),
  3377. validateFile = function (entry) {
  3378. expect(entry).toBeDefined();
  3379. expect(entry.isFile).toBe(true);
  3380. expect(entry.isDirectory).toBe(false);
  3381. expect(entry.name).toCanonicallyMatch(file2);
  3382. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3383. if(isChrome) {
  3384. expect(entry.filesystem.name).toContain("Temporary");
  3385. } else {
  3386. expect(entry.filesystem.name).toEqual("temporary");
  3387. }
  3388. // cleanup
  3389. deleteEntry(entry.name);
  3390. deleteEntry(sourceEntry.name, done);
  3391. },
  3392. createSourceAndTransfer = function () {
  3393. persistent_root.getFile(file1, {
  3394. create : true
  3395. }, function (entry) {
  3396. expect(entry).toBeDefined();
  3397. expect(entry.filesystem).toBeDefined();
  3398. if (isChrome) {
  3399. expect(entry.filesystem.name).toContain("Persistent");
  3400. } else {
  3401. expect(entry.filesystem.name).toEqual("persistent");
  3402. }
  3403. sourceEntry = entry;
  3404. // Save for later cleanup
  3405. entry.moveTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.moveTo - Error moving file: ' + file1 + ' to TEMPORAL root as: ' + file2));
  3406. }, failed.bind(null, done, 'persistent_root.getFile - Error creating file: ' + file1 + 'at PERSISTENT root'));
  3407. };
  3408. // Delete any existing file to start things off
  3409. temp_root.getFile(file2, {}, function (entry) {
  3410. entry.remove(createSourceAndTransfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3411. }, createSourceAndTransfer);
  3412. });
  3413. it("file.spec.129 cordova.file.*Directory are set", function () {
  3414. var expectedPaths = ['applicationDirectory', 'applicationStorageDirectory', 'dataDirectory', 'cacheDirectory'];
  3415. if (cordova.platformId == 'android' || cordova.platformId == 'amazon-fireos') {
  3416. if (cordova.file.externalApplicationStorageDirectory !== null) {
  3417. // https://issues.apache.org/jira/browse/CB-10411
  3418. // If external storage can't be mounted, the cordova.file.external* properties are null.
  3419. expectedPaths.push('externalApplicationStorageDirectory', 'externalRootDirectory', 'externalCacheDirectory', 'externalDataDirectory');
  3420. }
  3421. } else if (cordova.platformId == 'blackberry10') {
  3422. expectedPaths.push('externalRootDirectory', 'sharedDirectory');
  3423. } else if (cordova.platformId == 'ios') {
  3424. expectedPaths.push('syncedDataDirectory', 'documentsDirectory', 'tempDirectory');
  3425. } else if (cordova.platformId == 'osx') {
  3426. expectedPaths.push('documentsDirectory', 'tempDirectory', 'rootDirectory');
  3427. } else {
  3428. console.log('Skipping test due on unsupported platform.');
  3429. return;
  3430. }
  3431. for (var i = 0; i < expectedPaths.length; ++i) {
  3432. expect(typeof cordova.file[expectedPaths[i]]).toBe('string');
  3433. expect(cordova.file[expectedPaths[i]]).toMatch(/\/$/, 'Path should end with a slash');
  3434. }
  3435. });
  3436. });
  3437. describe('resolveLocalFileSystemURL on cdvfile://', function () {
  3438. it("file.spec.147 should be able to resolve cdvfile applicationDirectory fs root", function(done) {
  3439. var cdvfileApplicationDirectoryFsRootName;
  3440. if (cordova.platformId === 'android') {
  3441. cdvfileApplicationDirectoryFsRootName = 'assets';
  3442. } else if (cordova.platformId === 'ios') {
  3443. cdvfileApplicationDirectoryFsRootName = 'bundle';
  3444. } else {
  3445. pending();
  3446. }
  3447. resolveLocalFileSystemURL('cdvfile://localhost/' + cdvfileApplicationDirectoryFsRootName + '/', function(applicationDirectoryRoot) {
  3448. expect(applicationDirectoryRoot.isFile).toBe(false);
  3449. expect(applicationDirectoryRoot.isDirectory).toBe(true);
  3450. expect(applicationDirectoryRoot.name).toCanonicallyMatch('');
  3451. expect(applicationDirectoryRoot.fullPath).toCanonicallyMatch('/');
  3452. expect(applicationDirectoryRoot.filesystem.name).toEqual(cdvfileApplicationDirectoryFsRootName);
  3453. // Requires HelloCordova www assets, <allow-navigation href="cdvfile:*" /> in config.xml or
  3454. // cdvfile: in CSP and <access origin="cdvfile://*" /> in config.xml
  3455. resolveLocalFileSystemURL('cdvfile://localhost/' + cdvfileApplicationDirectoryFsRootName + '/www/img/logo.png', function(entry) {
  3456. expect(entry.isFile).toBe(true);
  3457. expect(entry.isDirectory).toBe(false);
  3458. expect(entry.name).toCanonicallyMatch('logo.png');
  3459. expect(entry.fullPath).toCanonicallyMatch('/www/img/logo.png');
  3460. expect(entry.filesystem.name).toEqual(cdvfileApplicationDirectoryFsRootName);
  3461. var img = new Image();
  3462. img.onerror = function(err) {
  3463. expect(err).not.toBeDefined();
  3464. done();
  3465. };
  3466. img.onload = function() {
  3467. done();
  3468. };
  3469. img.src = entry.toInternalURL();
  3470. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for cdvfile applicationDirectory'));
  3471. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for cdvfile applicationDirectory'));
  3472. });
  3473. });
  3474. //cross-file-system copy and move
  3475. describe('IndexedDB-based impl', function () {
  3476. it("file.spec.131 Nested file or nested directory should be removed when removing a parent directory", function (done) {
  3477. var parentDirName = 'deletedDir131',
  3478. nestedDirName = 'nestedDir131',
  3479. nestedFileName = 'nestedFile131.txt';
  3480. createDirectory(parentDirName, function (parent) {
  3481. parent.getDirectory(nestedDirName, { create: true}, function () {
  3482. parent.getFile(nestedFileName, { create: true}, function () {
  3483. parent.removeRecursively(function() {
  3484. root.getDirectory(parentDirName,{ create: false}, failed.bind(this, done, 'root.getDirectory - unexpected success callback : ' + parentDirName), function(){
  3485. parent.getFile(nestedFileName,{ create: false}, failed.bind(this, done, 'getFile - unexpected success callback : ' + nestedFileName), function(){
  3486. parent.getDirectory(nestedDirName, { create: false}, failed.bind(this, done, 'getDirectory - unexpected success callback : ' + nestedDirName), done);
  3487. });
  3488. });
  3489. }, failed.bind(this, done, 'removeRecursively - Error removing directory : ' + parentDirName));
  3490. }, failed.bind(this, done, 'getFile - Error creating file : ' + nestedFileName));
  3491. },failed.bind(this, done, 'getDirectory - Error creating directory : ' + nestedDirName));
  3492. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + parentDirName));
  3493. });
  3494. it("file.spec.132 Entry should be created succesfully when using relative paths if its parent directory exists", function (done) {
  3495. /* Directory entries have to be created successively.
  3496. For example, the call `fs.root.getDirectory('dir1/dir2', {create:true}, successCallback, errorCallback)`
  3497. will fail if dir1 did not exist. */
  3498. var parentName = 'parentName132';
  3499. var nestedName = 'nestedName132';
  3500. var path = parentName + '/' + nestedName;
  3501. var win = function(directory){
  3502. expect(directory).toBeDefined();
  3503. expect(directory.isFile).toBe(false);
  3504. expect(directory.isDirectory).toBe(true);
  3505. expect(directory.name).toCanonicallyMatch(nestedName);
  3506. expect(directory.fullPath).toCanonicallyMatch('/' + path + '/');
  3507. deleteEntry(directory.name);
  3508. deleteEntry(parentName, done);
  3509. };
  3510. createDirectory(parentName, function() {
  3511. root.getDirectory(parentName + '/' + nestedName, {create:true}, win,
  3512. failed.bind(this, done, 'root.getDirectory - Error getting directory : ' + path));
  3513. }, failed.bind(this, done, 'root.getDirectory - Error getting directory : ' + parentName));
  3514. });
  3515. it("file.spec.133 A file being removed should not affect another file with name being a prefix of the removed file name.", function (done) {
  3516. // Names include special symbols so that we check the IndexedDB range used
  3517. var deletedFileName = 'deletedFile.0',
  3518. secondFileName = 'deletedFile.0.1';
  3519. var win = function(fileEntry){
  3520. expect(fileEntry).toBeDefined();
  3521. expect(fileEntry.isFile).toBe(true);
  3522. expect(fileEntry.isDirectory).toBe(false);
  3523. expect(fileEntry.name).toCanonicallyMatch(secondFileName);
  3524. deleteEntry(fileEntry.name, done);
  3525. };
  3526. createFile(deletedFileName, function (deletedFile) {
  3527. createFile(secondFileName, function () {
  3528. deletedFile.remove(function() {
  3529. root.getFile(deletedFileName, {create: false}, failed.bind(this, done, 'getFile - unexpected success callback getting deleted file : ' + deletedFileName), function(){
  3530. root.getFile(secondFileName, {create: false}, win, failed.bind(this, done, 'getFile - Error getting file after deleting deletedFile : ' + secondFileName));
  3531. });
  3532. }, failed.bind(this, done, 'remove - Error removing file : ' + deletedFileName));
  3533. }, failed.bind(this, done, 'getFile - Error creating file : ' + secondFileName));
  3534. }, failed.bind(this, done, 'getFile - Error creating file : ' + deletedFileName));
  3535. });
  3536. it("file.spec.134 A directory being removed should not affect another directory with name being a prefix of the removed directory name.", function (done) {
  3537. // Names include special symbols so that we check the IndexedDB range used
  3538. var deletedDirName = 'deletedDir.0',
  3539. secondDirName = 'deletedDir.0.1';
  3540. var win = function(directory){
  3541. expect(directory).toBeDefined();
  3542. expect(directory.isFile).toBe(false);
  3543. expect(directory.isDirectory).toBe(true);
  3544. expect(directory.name).toCanonicallyMatch(secondDirName);
  3545. deleteEntry(directory.name, done);
  3546. };
  3547. createDirectory(deletedDirName, function (deletedDir) {
  3548. createDirectory(secondDirName, function () {
  3549. deletedDir.remove(function() {
  3550. root.getDirectory(deletedDirName, {create: false}, failed.bind(this, done, 'getDirectory - unexpected success callback getting deleted directory : ' + deletedDirName), function() {
  3551. root.getDirectory(secondDirName, {create: false}, win, failed.bind(this, done, 'getDirectory - Error getting directory after deleting deletedDirectory : ' + secondDirName));
  3552. });
  3553. }, failed.bind(this, done, 'remove - Error removing directory : ' + deletedDirName));
  3554. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + secondDirName));
  3555. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + deletedDirName));
  3556. });
  3557. it("file.spec.135 Deletion of a child directory should not affect the parent directory.", function (done) {
  3558. var parentName = 'parentName135';
  3559. var childName = 'childName135';
  3560. var win = function(directory){
  3561. expect(directory).toBeDefined();
  3562. expect(directory.isFile).toBe(false);
  3563. expect(directory.isDirectory).toBe(true);
  3564. expect(directory.name).toCanonicallyMatch(parentName);
  3565. deleteEntry(directory.name, done);
  3566. };
  3567. createDirectory(parentName, function(parent){
  3568. parent.getDirectory(childName, {create: true}, function(child){
  3569. child.removeRecursively(function(){
  3570. root.getDirectory(parentName, {create: false}, win, failed.bind(this, done, 'root.getDirectory - Error getting parent directory : ' + parentName));
  3571. },
  3572. failed.bind(this, done, 'getDirectory - Error removing directory : ' + childName));
  3573. }, failed.bind(this, done, 'getDirectory - Error creating directory : ' + childName));
  3574. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + parentName));
  3575. });
  3576. it("file.spec.136 Paths should support Unicode symbols.", function (done) {
  3577. var dirName = '文件插件';
  3578. var win = function(directory){
  3579. expect(directory).toBeDefined();
  3580. expect(directory.isFile).toBe(false);
  3581. expect(directory.isDirectory).toBe(true);
  3582. expect(directory.name).toCanonicallyMatch(dirName);
  3583. deleteEntry(directory.name, done);
  3584. };
  3585. createDirectory(dirName, function(){
  3586. root.getDirectory(dirName, {create: false}, win,
  3587. failed.bind(this, done, 'root.getDirectory - Error getting directory : ' + dirName));
  3588. }, failed.bind(this, done, 'root.getDirectory - Error creating directory : ' + dirName));
  3589. });
  3590. });
  3591. // Content and Asset URLs
  3592. if (cordova.platformId == 'android') {
  3593. describe('content: URLs', function() {
  3594. // Warning: Default HelloWorld www directory structure is required for these tests (www/index.html at least)
  3595. function testContentCopy(src, done) {
  3596. var file2 = "entry.copy.file2b",
  3597. fullPath = joinURL(temp_root.fullPath, file2),
  3598. validateFile = function (entry) {
  3599. expect(entry.isFile).toBe(true);
  3600. expect(entry.isDirectory).toBe(false);
  3601. expect(entry.name).toCanonicallyMatch(file2);
  3602. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3603. expect(entry.filesystem.name).toEqual("temporary");
  3604. // cleanup
  3605. deleteEntry(entry.name, done);
  3606. },
  3607. transfer = function () {
  3608. resolveLocalFileSystemURL(src, function(entry) {
  3609. expect(entry).toBeDefined();
  3610. expect(entry.filesystem.name).toEqual("content");
  3611. entry.copyTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + entry.toURL() + ' to TEMPORAL root as: ' + file2));
  3612. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for content provider'));
  3613. };
  3614. // Delete any existing file to start things off
  3615. temp_root.getFile(file2, {}, function (entry) {
  3616. entry.remove(transfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3617. }, transfer);
  3618. }
  3619. it("file.spec.138 copyTo: content", function(done) {
  3620. testContentCopy('content://org.apache.cordova.file.testprovider/www/index.html', done);
  3621. });
  3622. it("file.spec.139 copyTo: content /w space and query", function(done) {
  3623. testContentCopy('content://org.apache.cordova.file.testprovider/?name=foo%20bar&realPath=%2Fwww%2Findex.html', done);
  3624. });
  3625. it("file.spec.140 delete: content should fail", function(done) {
  3626. resolveLocalFileSystemURL('content://org.apache.cordova.file.testprovider/www/index.html', function(entry) {
  3627. entry.remove(failed.bind(null, done, 'expected delete to fail'), done);
  3628. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for content provider'));
  3629. });
  3630. });
  3631. // these tests ensure that you can read and copy from android_asset folder
  3632. // for details see https://issues.apache.org/jira/browse/CB-6428
  3633. // and https://mail-archives.apache.org/mod_mbox/cordova-dev/201508.mbox/%3C782154441.8406572.1440182722528.JavaMail.yahoo%40mail.yahoo.com%3E
  3634. describe('asset: URLs', function() {
  3635. it("file.spec.141 filePaths.applicationStorage", function() {
  3636. expect(cordova.file.applicationDirectory).toEqual('file:///android_asset/');
  3637. }, MEDIUM_TIMEOUT);
  3638. it("file.spec.142 assets should be enumerable", function(done) {
  3639. resolveLocalFileSystemURL('file:///android_asset/www/fixtures/asset-test', function(entry) {
  3640. var reader = entry.createReader();
  3641. reader.readEntries(function (entries) {
  3642. expect(entries.length).not.toBe(0);
  3643. done();
  3644. }, failed.bind(null, done, 'reader.readEntries - Error during reading of entries from assets directory'));
  3645. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
  3646. }, MEDIUM_TIMEOUT);
  3647. it("file.spec.145 asset subdirectories should be obtainable", function(done) {
  3648. resolveLocalFileSystemURL('file:///android_asset/www/fixtures', function(entry) {
  3649. entry.getDirectory('asset-test', { create: false }, function (subDir) {
  3650. expect(subDir).toBeDefined();
  3651. expect(subDir.isFile).toBe(false);
  3652. expect(subDir.isDirectory).toBe(true);
  3653. expect(subDir.name).toCanonicallyMatch('asset-test');
  3654. done();
  3655. }, failed.bind(null, done, 'entry.getDirectory - Error getting asset subdirectory'));
  3656. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
  3657. }, MEDIUM_TIMEOUT);
  3658. it("file.spec.146 asset files should be readable", function(done) {
  3659. resolveLocalFileSystemURL('file:///android_asset/www/fixtures/asset-test/asset-test.txt', function(entry) {
  3660. expect(entry.isFile).toBe(true);
  3661. entry.file(function (file) {
  3662. expect(file).toBeDefined();
  3663. var reader = new FileReader();
  3664. reader.onerror = failed.bind(null, done, 'reader.readAsText - Error reading asset text file');
  3665. reader.onloadend = function () {
  3666. expect(this.result).toBeDefined();
  3667. expect(this.result.length).not.toBe(0);
  3668. done();
  3669. };
  3670. reader.readAsText(file);
  3671. }, failed.bind(null, done, 'entry.file - Error reading asset file'));
  3672. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
  3673. }, MEDIUM_TIMEOUT);
  3674. it("file.spec.143 copyTo: asset -> temporary", function(done) {
  3675. var file2 = "entry.copy.file2b",
  3676. fullPath = joinURL(temp_root.fullPath, file2),
  3677. validateFile = function (entry) {
  3678. expect(entry.isFile).toBe(true);
  3679. expect(entry.isDirectory).toBe(false);
  3680. expect(entry.name).toCanonicallyMatch(file2);
  3681. expect(entry.fullPath).toCanonicallyMatch(fullPath);
  3682. expect(entry.filesystem.name).toEqual("temporary");
  3683. // cleanup
  3684. deleteEntry(entry.name, done);
  3685. },
  3686. transfer = function () {
  3687. resolveLocalFileSystemURL('file:///android_asset/www/index.html', function(entry) {
  3688. expect(entry.filesystem.name).toEqual('assets');
  3689. entry.copyTo(temp_root, file2, validateFile, failed.bind(null, done, 'entry.copyTo - Error copying file: ' + entry.toURL() + ' to TEMPORAL root as: ' + file2));
  3690. }, failed.bind(null, done, 'resolveLocalFileSystemURL failed for assets'));
  3691. };
  3692. // Delete any existing file to start things off
  3693. temp_root.getFile(file2, {}, function (entry) {
  3694. entry.remove(transfer, failed.bind(null, done, 'entry.remove - Error removing file: ' + file2));
  3695. }, transfer);
  3696. }, MEDIUM_TIMEOUT);
  3697. });
  3698. it("file.spec.144 copyTo: asset directory", function (done) {
  3699. var srcUrl = 'file:///android_asset/www/fixtures/asset-test';
  3700. var dstDir = "entry.copy.dstDir";
  3701. var dstPath = joinURL(root.fullPath, dstDir);
  3702. // create a new directory entry to kick off it
  3703. deleteEntry(dstDir, function () {
  3704. resolveLocalFileSystemURL(srcUrl, function(directory) {
  3705. directory.copyTo(root, dstDir, function (directory) {
  3706. expect(directory).toBeDefined();
  3707. expect(directory.isFile).toBe(false);
  3708. expect(directory.isDirectory).toBe(true);
  3709. expect(directory.fullPath).toCanonicallyMatch(dstPath);
  3710. expect(directory.name).toCanonicallyMatch(dstDir);
  3711. root.getDirectory(dstDir, {
  3712. create : false
  3713. }, function (dirEntry) {
  3714. expect(dirEntry).toBeDefined();
  3715. expect(dirEntry.isFile).toBe(false);
  3716. expect(dirEntry.isDirectory).toBe(true);
  3717. expect(dirEntry.fullPath).toCanonicallyMatch(dstPath);
  3718. expect(dirEntry.name).toCanonicallyMatch(dstDir);
  3719. dirEntry.getFile('asset-test.txt', {
  3720. create : false
  3721. }, function (fileEntry) {
  3722. expect(fileEntry).toBeDefined();
  3723. expect(fileEntry.isFile).toBe(true);
  3724. // cleanup
  3725. deleteEntry(dstDir, done);
  3726. }, failed.bind(null, done, 'dirEntry.getFile - Error getting subfile'));
  3727. }, failed.bind(null, done, 'root.getDirectory - Error getting copied directory'));
  3728. }, failed.bind(null, done, 'directory.copyTo - Error copying directory'));
  3729. }, failed.bind(null, done, 'resolving src dir'));
  3730. }, failed.bind(null, done, 'deleteEntry - Error removing directory : ' + dstDir));
  3731. }, MEDIUM_TIMEOUT);
  3732. }
  3733. });
  3734. };
  3735. //******************************************************************************************
  3736. //***************************************Manual Tests***************************************
  3737. //******************************************************************************************
  3738. exports.defineManualTests = function (contentEl, createActionButton) {
  3739. function resolveFs(fsname) {
  3740. var fsURL = "cdvfile://localhost/" + fsname + "/";
  3741. logMessage("Resolving URL: " + fsURL);
  3742. resolveLocalFileSystemURL(fsURL, function (entry) {
  3743. logMessage("Success", 'green');
  3744. logMessage(entry.toURL(), 'blue');
  3745. logMessage(entry.toInternalURL(), 'blue');
  3746. logMessage("Resolving URL: " + entry.toURL());
  3747. resolveLocalFileSystemURL(entry.toURL(), function (entry2) {
  3748. logMessage("Success", 'green');
  3749. logMessage(entry2.toURL(), 'blue');
  3750. logMessage(entry2.toInternalURL(), 'blue');
  3751. }, logError("resolveLocalFileSystemURL"));
  3752. }, logError("resolveLocalFileSystemURL"));
  3753. }
  3754. function testPrivateURL() {
  3755. requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem) {
  3756. logMessage("Temporary root is at " + fileSystem.root.toNativeURL());
  3757. fileSystem.root.getFile("testfile", {
  3758. create : true
  3759. }, function (entry) {
  3760. logMessage("Temporary file is at " + entry.toNativeURL());
  3761. if (entry.toNativeURL().substring(0, 12) == "file:///var/") {
  3762. logMessage("File starts with /var/, trying /private/var");
  3763. var newURL = "file://localhost/private/var/" + entry.toNativeURL().substring(12) + "?and=another_thing";
  3764. //var newURL = entry.toNativeURL();
  3765. logMessage(newURL, 'blue');
  3766. resolveLocalFileSystemURL(newURL, function (newEntry) {
  3767. logMessage("Successfully resolved.", 'green');
  3768. logMessage(newEntry.toURL(), 'blue');
  3769. logMessage(newEntry.toNativeURL(), 'blue');
  3770. }, logError("resolveLocalFileSystemURL"));
  3771. }
  3772. }, logError("getFile"));
  3773. }, logError("requestFileSystem"));
  3774. }
  3775. function resolveFsContactImage() {
  3776. navigator.contacts.pickContact(function(contact) {
  3777. var logBox = document.getElementById("logContactBox");
  3778. logBox.innerHTML = "";
  3779. var resolveResult = document.createElement("p");
  3780. if (contact.photos) {
  3781. var photoURL = contact.photos[0].value;
  3782. resolveLocalFileSystemURL(photoURL, function(entry) {
  3783. var contactImage = document.createElement("img");
  3784. var contactLabelImage = document.createElement("p");
  3785. contactLabelImage.innerHTML = "Result contact image";
  3786. contactImage.setAttribute("src", entry.toURL());
  3787. resolveResult.innerHTML = "Success resolve\n" + entry.toURL();
  3788. logBox.appendChild(contactLabelImage);
  3789. logBox.appendChild(contactImage);
  3790. logBox.appendChild(resolveResult);
  3791. },
  3792. function(err) {
  3793. console.log("resolve error" + err);
  3794. });
  3795. }
  3796. else {
  3797. resolveResult.innerHTML = "Contact has no photos";
  3798. logBox.appendChild(resolveResult);
  3799. }
  3800. },
  3801. function(err) {
  3802. console.log("contact pick error" + err);
  3803. });
  3804. }
  3805. function clearLog() {
  3806. var log = document.getElementById("info");
  3807. log.innerHTML = "";
  3808. }
  3809. function logMessage(message, color) {
  3810. var log = document.getElementById("info");
  3811. var logLine = document.createElement('div');
  3812. if (color) {
  3813. logLine.style.color = color;
  3814. }
  3815. logLine.innerHTML = message;
  3816. log.appendChild(logLine);
  3817. }
  3818. function logError(serviceName) {
  3819. return function (err) {
  3820. logMessage("ERROR: " + serviceName + " " + JSON.stringify(err), "red");
  3821. };
  3822. }
  3823. var fsRoots = {
  3824. "ios" : "library,library-nosync,documents,documents-nosync,cache,bundle,root,private",
  3825. "osx" : "library,library-nosync,documents,documents-nosync,cache,bundle,root,private",
  3826. "android" : "files,files-external,documents,sdcard,cache,cache-external,assets,root",
  3827. "amazon-fireos" : "files,files-external,documents,sdcard,cache,cache-external,root",
  3828. "windows": "temporary,persistent"
  3829. };
  3830. //Add title and align to content
  3831. var div = document.createElement('h2');
  3832. div.appendChild(document.createTextNode('File Systems'));
  3833. div.setAttribute("align", "center");
  3834. contentEl.appendChild(div);
  3835. div = document.createElement('h3');
  3836. div.appendChild(document.createTextNode('Results are displayed in yellow status box below with expected results noted under that'));
  3837. div.setAttribute("align", "center");
  3838. contentEl.appendChild(div);
  3839. div = document.createElement('div');
  3840. div.setAttribute("id", "button");
  3841. div.setAttribute("align", "center");
  3842. contentEl.appendChild(div);
  3843. if (fsRoots.hasOwnProperty(cordova.platformId)) {
  3844. (fsRoots[cordova.platformId].split(',')).forEach(function (fs) {
  3845. if (cordova.platformId === 'ios' && fs === 'private') {
  3846. createActionButton("Test private URL (iOS)", function () {
  3847. clearLog();
  3848. testPrivateURL();
  3849. }, 'button');
  3850. } else {
  3851. createActionButton(fs, function () {
  3852. clearLog();
  3853. resolveFs(fs);
  3854. }, 'button');
  3855. }
  3856. });
  3857. }
  3858. div = document.createElement('div');
  3859. div.setAttribute("id", "info");
  3860. div.setAttribute("align", "center");
  3861. contentEl.appendChild(div);
  3862. div = document.createElement('h3');
  3863. div.appendChild(document.createTextNode('For each test above, file or directory should be successfully found. ' +
  3864. 'Status box should say Resolving URL was Success. The first URL resolved is the internal URL. ' +
  3865. 'The second URL resolved is the absolute URL. Blue URLs must match.'));
  3866. contentEl.appendChild(div);
  3867. div = document.createElement('h3');
  3868. div.appendChild(document.createTextNode('For Test private URL (iOS), the private URL (first blue URL in status box) ' +
  3869. 'should be successfully resolved. Status box should say Successfully resolved. Both blue URLs below ' +
  3870. 'that should match.'));
  3871. contentEl.appendChild(div);
  3872. div = document.createElement('h2');
  3873. div.appendChild(document.createTextNode('Resolving content urls'));
  3874. div.setAttribute("align", "center");
  3875. contentEl.appendChild(div);
  3876. div = document.createElement('div');
  3877. div.setAttribute("id", "contactButton");
  3878. div.setAttribute("align", "center");
  3879. contentEl.appendChild(div);
  3880. div = document.createElement('div');
  3881. div.setAttribute("id", "logContactBox");
  3882. div.setAttribute("align", "center");
  3883. contentEl.appendChild(div);
  3884. createActionButton('show-contact-image', function () {
  3885. resolveFsContactImage();
  3886. }, 'contactButton');
  3887. };