-
Notifications
You must be signed in to change notification settings - Fork 760
/
Copy pathtests.js
4287 lines (4028 loc) · 247 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/* eslint-env jasmine */
/* global WebKitBlobBuilder */
exports.defineAutoTests = function () {
/* eslint-disable no-undef */
const isBrowser = (cordova.platformId === 'browser');
// Use feature detection to determine current browser instead of checking user-agent
const isChrome = isBrowser && window.webkitRequestFileSystem && window.webkitResolveLocalFileSystemURL;
const isIE = isBrowser && (window.msIndexedDB);
const isIndexedDBShim = isBrowser && !isChrome; // Firefox and IE for example
const isWindows = cordova.platformId === 'windows';
/* eslint-enable no-undef */
const MEDIUM_TIMEOUT = 15000;
describe('File API', function () {
// Adding a Jasmine helper matcher, to report errors when comparing to FileError better.
const fileErrorMap = {
1: 'NOT_FOUND_ERR',
2: 'SECURITY_ERR',
3: 'ABORT_ERR',
4: 'NOT_READABLE_ERR',
5: 'ENCODING_ERR',
6: 'NO_MODIFICATION_ALLOWED_ERR',
7: 'INVALID_STATE_ERR',
8: 'SYNTAX_ERR',
9: 'INVALID_MODIFICATION_ERR',
10: 'QUOTA_EXCEEDED_ERR',
11: 'TYPE_MISMATCH_ERR',
12: 'PATH_EXISTS_ERR'
};
let root;
let temp_root;
let persistent_root;
beforeEach(function (done) {
// Custom Matchers
jasmine.Expectation.addMatchers({
toBeFileError: function () {
return {
compare: function (error, code) {
const pass = error.code === code;
return {
pass,
message: 'Expected FileError with code ' + fileErrorMap[error.code] + ' (' + error.code + ') to be ' + fileErrorMap[code] + '(' + code + ')'
};
}
};
},
toCanonicallyMatch: function () {
return {
compare: function (currentPath, path) {
const a = path.split('/').join('').split('\\').join('');
const b = currentPath.split('/').join('').split('\\').join('');
const pass = a === b;
return {
pass,
message: 'Expected paths to match : ' + path + ' should be ' + currentPath
};
}
};
},
toFailWithMessage: function () {
return {
compare: function (error, message) { // eslint-disable-line n/handle-callback-err
const pass = false;
return {
pass,
message
};
}
};
},
toBeDataUrl: function () {
return {
compare: function (url) {
let pass = false;
// "data:application/octet-stream;base64,"
const header = url.substr(0, url.indexOf(','));
const headerParts = header.split(/[:;]/);
if (headerParts.length === 3 &&
headerParts[0] === 'data' &&
headerParts[2] === 'base64') {
pass = true;
}
const message = 'Expected ' + url + ' to be a valid data url. ' + header + ' is not valid header for data uris';
return {
pass,
message
};
}
};
}
});
// Define global variables
const onError = function (e) {
console.log('[ERROR] Problem setting up root filesystem for test running! Error to follow.');
console.log(JSON.stringify(e));
};
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) { // eslint-disable-line no-undef
root = fileSystem.root;
// set in file.tests.js
persistent_root = root;
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem) { // eslint-disable-line no-undef
temp_root = fileSystem.root;
// set in file.tests.js
done();
}, onError);
}, onError);
});
// HELPER FUNCTIONS
// deletes specified file or directory
const deleteEntry = function (name, success, error) {
// deletes entry, if it exists
// entry.remove success callback is required: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#the-entry-interface
success = success || function () {};
error = error || failed.bind(null, success, 'deleteEntry failed.');
window.resolveLocalFileSystemURL(root.nativeURL + '/' + name, function (entry) {
if (entry.isDirectory === true) {
entry.removeRecursively(success, error);
} else {
entry.remove(success, error);
}
}, success);
};
// deletes file, if it exists, then invokes callback
const deleteFile = function (fileName, callback) {
// entry.remove success callback is required: http://www.w3.org/TR/2011/WD-file-system-api-20110419/#the-entry-interface
callback = callback || function () {};
root.getFile(fileName, null, // remove file system entry
function (entry) {
entry.remove(callback, function () {
console.log('[ERROR] deleteFile cleanup method invoked fail callback.');
});
}, // doesn't exist
callback);
};
// deletes and re-creates the specified file
const createFile = function (fileName, success, error) {
deleteEntry(fileName, function () {
root.getFile(fileName, {
create: true
}, success, error);
}, error);
};
// deletes and re-creates the specified directory
const createDirectory = function (dirName, success, error) {
deleteEntry(dirName, function () {
root.getDirectory(dirName, {
create: true
}, success, error);
}, error);
};
function failed (done, msg, error) {
const info = typeof msg === 'undefined' ? 'Unexpected error callback' : msg;
const codeMsg = (error && error.code) ? (': ' + fileErrorMap[error.code]) : '';
expect(true).toFailWithMessage(info + '\n' + JSON.stringify(error) + codeMsg);
done();
}
const succeed = function (done, msg) {
const info = typeof msg === 'undefined' ? 'Unexpected success callback' : msg;
expect(true).toFailWithMessage(info);
done();
};
const joinURL = function (base, extension) {
if (base.charAt(base.length - 1) !== '/' && extension.charAt(0) !== '/') {
return base + '/' + extension;
}
if (base.charAt(base.length - 1) === '/' && extension.charAt(0) === '/') {
return base + extension.substring(1);
}
return base + extension;
};
describe('FileError object', function () {
/* eslint-disable no-undef */
it('file.spec.1 should define FileError constants', function () {
expect(FileError.NOT_FOUND_ERR).toBe(1);
expect(FileError.SECURITY_ERR).toBe(2);
expect(FileError.ABORT_ERR).toBe(3);
expect(FileError.NOT_READABLE_ERR).toBe(4);
expect(FileError.ENCODING_ERR).toBe(5);
expect(FileError.NO_MODIFICATION_ALLOWED_ERR).toBe(6);
expect(FileError.INVALID_STATE_ERR).toBe(7);
expect(FileError.SYNTAX_ERR).toBe(8);
expect(FileError.INVALID_MODIFICATION_ERR).toBe(9);
expect(FileError.QUOTA_EXCEEDED_ERR).toBe(10);
expect(FileError.TYPE_MISMATCH_ERR).toBe(11);
expect(FileError.PATH_EXISTS_ERR).toBe(12);
});
});
describe('LocalFileSystem', function () {
it('file.spec.2 should define LocalFileSystem constants', function () {
expect(LocalFileSystem.TEMPORARY).toBe(0);
expect(LocalFileSystem.PERSISTENT).toBe(1);
/* eslint-enable no-undef */
});
describe('window.requestFileSystem', function () {
it('file.spec.3 should be defined', function () {
expect(window.requestFileSystem).toBeDefined();
});
it('file.spec.4 should be able to retrieve a PERSISTENT file system', function (done) {
const win = function (fileSystem) {
expect(fileSystem).toBeDefined();
expect(fileSystem.name).toBeDefined();
if (isChrome) {
expect(fileSystem.name).toContain('Persistent');
} else {
expect(fileSystem.name).toBe('persistent');
}
expect(fileSystem.root).toBeDefined();
expect(fileSystem.root.filesystem).toBeDefined();
// Shouldn't use cdvfile by default.
expect(fileSystem.root.toURL()).not.toMatch(/^cdvfile:/);
// All DirectoryEntry URLs should always have a trailing slash.
expect(fileSystem.root.toURL()).toMatch(/\/$/);
done();
};
// Request a little bit of space on the filesystem, unless we're running in a browser where that could cause a prompt.
const spaceRequired = isBrowser ? 0 : 1024;
// retrieve PERSISTENT file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, spaceRequired, win, failed.bind(null, done, 'window.requestFileSystem - Error retrieving PERSISTENT file system')); // eslint-disable-line no-undef
});
it('file.spec.5 should be able to retrieve a TEMPORARY file system', function (done) {
const win = function (fileSystem) {
expect(fileSystem).toBeDefined();
if (isChrome) {
expect(fileSystem.name).toContain('Temporary');
} else {
expect(fileSystem.name).toBe('temporary');
}
expect(fileSystem.root).toBeDefined();
expect(fileSystem.root.filesystem).toBeDefined();
expect(fileSystem.root.filesystem).toBe(fileSystem);
done();
};
// retrieve TEMPORARY file system
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, win, failed.bind(null, done, 'window.requestFileSystem - Error retrieving TEMPORARY file system')); // eslint-disable-line no-undef
});
it('file.spec.6 should error if you request a file system that is too large', function (done) {
if (isBrowser) {
/* window.requestFileSystem TEMPORARY and PERSISTENT filesystem quota is not limited in Chrome.
Firefox filesystem size is not limited but every 50MB request user permission.
IE10 allows up to 10mb of combined AppCache and IndexedDB used in implementation
of filesystem without prompting, once you hit that level you will be asked if you
want to allow it to be increased up to a max of 250mb per site.
So `size` parameter for `requestFileSystem` function does not affect on filesystem in Firefox and IE. */
pending();
}
const fail = function (error) {
expect(error).toBeDefined();
expect(error).toBeFileError(FileError.QUOTA_EXCEEDED_ERR); // eslint-disable-line no-undef
done();
};
// win = createWin('window.requestFileSystem');
// Request the file system
window.requestFileSystem(LocalFileSystem.TEMPORARY, 1000000000000000, failed.bind(null, done, 'window.requestFileSystem - Error retrieving TEMPORARY file system'), fail); // eslint-disable-line no-undef
});
it('file.spec.7 should error out if you request a file system that does not exist', function (done) {
const fail = function (error) {
expect(error).toBeDefined();
if (isChrome) {
/* INVALID_MODIFICATION_ERR (code: 9) or ??? (code: 13) is thrown instead of SYNTAX_ERR(code: 8)
on requesting of a non-existant filesystem. */
// expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
} else {
expect(error).toBeFileError(FileError.SYNTAX_ERR); // eslint-disable-line no-undef
}
done();
};
// Request the file system
window.requestFileSystem(-1, 0, succeed.bind(null, done, 'window.requestFileSystem'), fail);
});
});
describe('window.resolveLocalFileSystemURL', function () {
it('file.spec.8 should be defined', function () {
expect(window.resolveLocalFileSystemURL).toBeDefined();
});
it('file.spec.9 should resolve a valid file name', function (done) {
const fileName = 'file.spec.9';
const win = function (fileEntry) {
expect(fileEntry).toBeDefined();
expect(fileEntry.isFile).toBe(true);
expect(fileEntry.isDirectory).toBe(false);
expect(fileEntry.name).toCanonicallyMatch(fileName);
expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
// Clean-up
deleteEntry(fileName, done);
};
createFile(fileName, function (entry) {
window.resolveLocalFileSystemURL(entry.nativeURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entry.nativeURL));
}, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
});
it('file.spec.9.1 should resolve a file even with a terminating slash', function (done) {
const fileName = 'file.spec.9.1';
const win = function (fileEntry) {
expect(fileEntry).toBeDefined();
expect(fileEntry.isFile).toBe(true);
expect(fileEntry.isDirectory).toBe(false);
expect(fileEntry.name).toCanonicallyMatch(fileName);
expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
expect(fileEntry.toURL()).not.toMatch(/\/$/, 'URL should not end with a slash');
// Clean-up
deleteEntry(fileName, done);
};
createFile(fileName, function (entry) {
const entryURL = entry.nativeURL + '/';
window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URL: ' + entryURL));
}, failed.bind(null, done, 'createFile - Error creating file: ' + fileName), failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
});
it('file.spec.9.5 should resolve a directory', function (done) {
const fileName = 'file.spec.9.5';
const win = function (fileEntry) {
expect(fileEntry).toBeDefined();
expect(fileEntry.isFile).toBe(false);
expect(fileEntry.isDirectory).toBe(true);
expect(fileEntry.name).toCanonicallyMatch(fileName);
expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
// cleanup
deleteEntry(fileName, done);
};
function gotDirectory (entry) {
// lookup file system entry
window.resolveLocalFileSystemURL(entry.nativeURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entry.nativeURL));
}
createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
});
it('file.spec.9.6 should resolve a directory even without a terminating slash', function (done) {
const fileName = 'file.spec.9.6';
const win = function (fileEntry) {
expect(fileEntry).toBeDefined();
expect(fileEntry.isFile).toBe(false);
expect(fileEntry.isDirectory).toBe(true);
expect(fileEntry.name).toCanonicallyMatch(fileName);
expect(fileEntry.toURL()).not.toMatch(/^cdvfile:/, 'should not use cdvfile URL');
expect(fileEntry.toURL()).toMatch(/\/$/, 'URL end with a slash');
// cleanup
deleteEntry(fileName, done);
};
function gotDirectory (entry) {
// lookup file system entry
let entryURL = entry.nativeURL;
entryURL = entryURL.substring(0, entryURL.length - 1);
window.resolveLocalFileSystemURL(entryURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving directory URL: ' + entryURL));
}
createDirectory(fileName, gotDirectory, failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName), failed.bind(null, done, 'createDirectory - Error creating directory: ' + fileName));
});
it('file.spec.9.7 should resolve a file with valid nativeURL', function (done) {
if (isBrowser) {
pending('browsers doesn\'t return nativeURL');
}
const fileName = 'de.create.file';
const win = function (entry) {
const path = entry.nativeURL.split('///')[1];
expect(/\/{2,}/.test(path)).toBeFalsy();
// cleanup
deleteEntry(entry.name, done);
};
root.getFile(fileName, {
create: true
}, win, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName));
});
it('file.spec.10 resolve valid file name with parameters', function (done) {
const fileName = 'resolve.file.uri.params';
const win = function (fileEntry) {
expect(fileEntry).toBeDefined();
if (fileEntry.toURL().toLowerCase().substring(0, 10) === 'cdvfile://') {
expect(fileEntry.fullPath).toBe('/' + fileName + '?1234567890');
}
expect(fileEntry.name).toBe(fileName);
// cleanup
deleteEntry(fileName, done);
};
// create a new file entry
createFile(fileName, function (entry) {
window.resolveLocalFileSystemURL(entry.nativeURL + '?1234567890', win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URI: ' + entry.nativeURL));
}, failed.bind(null, done, 'createFile - Error creating file: ' + fileName));
});
it('file.spec.11 should error (NOT_FOUND_ERR) when resolving (non-existent) invalid file name', function (done) {
const fileName = joinURL(root.nativeURL, 'this.is.not.a.valid.file.txt');
const fail = function (error) {
expect(error).toBeDefined();
if (isChrome) {
expect(error).toBeFileError(FileError.SYNTAX_ERR); // eslint-disable-line no-undef
} else {
expect(error).toBeFileError(FileError.NOT_FOUND_ERR); // eslint-disable-line no-undef
}
done();
};
// lookup file system entry
window.resolveLocalFileSystemURL(fileName, succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Error unexpected callback resolving file URI: ' + fileName), fail);
});
it('file.spec.12 should error (ENCODING_ERR) when resolving invalid URI with leading /', function (done) {
const fileName = '/this.is.not.a.valid.url';
const fail = function (error) {
expect(error).toBeDefined();
if (isChrome) {
// O.o chrome returns error code 0
} else {
expect(error).toBeFileError(FileError.ENCODING_ERR); // eslint-disable-line no-undef
}
done();
};
// lookup file system entry
window.resolveLocalFileSystemURL(fileName, succeed.bind(null, done, 'window.resolveLocalFileSystemURL - Error unexpected callback resolving file URI: ' + fileName), fail);
});
});
});
// LocalFileSystem
describe('Metadata interface', function () {
it('file.spec.13 should exist and have the right properties', function () {
const metadata = new Metadata(); // eslint-disable-line no-undef
expect(metadata).toBeDefined();
expect(metadata.modificationTime).toBeDefined();
});
});
describe('Flags interface', function () {
it('file.spec.14 should exist and have the right properties', function () {
const flags = new Flags(false, true); // eslint-disable-line no-undef
expect(flags).toBeDefined();
expect(flags.create).toBeDefined();
expect(flags.create).toBe(false);
expect(flags.exclusive).toBeDefined();
expect(flags.exclusive).toBe(true);
});
});
describe('FileSystem interface', function () {
it('file.spec.15 should have a root that is a DirectoryEntry', function (done) {
const win = function (entry) {
expect(entry).toBeDefined();
expect(entry.isFile).toBe(false);
expect(entry.isDirectory).toBe(true);
expect(entry.name).toBeDefined();
expect(entry.fullPath).toBeDefined();
expect(entry.getMetadata).toBeDefined();
expect(entry.moveTo).toBeDefined();
expect(entry.copyTo).toBeDefined();
expect(entry.toURL).toBeDefined();
expect(entry.remove).toBeDefined();
expect(entry.getParent).toBeDefined();
expect(entry.createReader).toBeDefined();
expect(entry.getFile).toBeDefined();
expect(entry.getDirectory).toBeDefined();
expect(entry.removeRecursively).toBeDefined();
done();
};
window.resolveLocalFileSystemURL(root.nativeURL, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - Error resolving file URI: ' + root.nativeURL));
});
});
describe('DirectoryEntry', function () {
it('file.spec.16 getFile: get Entry for file that does not exist', function (done) {
const fileName = 'de.no.file';
const fail = function (error) {
expect(error).toBeDefined();
if (isChrome) {
expect(error).toBeFileError(FileError.SYNTAX_ERR); // eslint-disable-line no-undef
} else {
expect(error).toBeFileError(FileError.NOT_FOUND_ERR); // eslint-disable-line no-undef
}
done();
};
// create:false, exclusive:false, file does not exist
root.getFile(fileName, {
create: false
}, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName), fail);
});
it('file.spec.17 getFile: create new file', function (done) {
const fileName = 'de.create.file';
const filePath = joinURL(root.fullPath, fileName);
const win = function (entry) {
expect(entry).toBeDefined();
expect(entry.isFile).toBe(true);
expect(entry.isDirectory).toBe(false);
expect(entry.name).toCanonicallyMatch(fileName);
expect(entry.fullPath).toCanonicallyMatch(filePath);
// cleanup
deleteEntry(entry.name, done);
};
// create:true, exclusive:false, file does not exist
root.getFile(fileName, {
create: true
}, win, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName));
});
it('file.spec.18 getFile: create new file (exclusive)', function (done) {
const fileName = 'de.create.exclusive.file';
const filePath = joinURL(root.fullPath, fileName);
const win = function (entry) {
expect(entry).toBeDefined();
expect(entry.isFile).toBe(true);
expect(entry.isDirectory).toBe(false);
expect(entry.name).toBe(fileName);
expect(entry.fullPath).toCanonicallyMatch(filePath);
// cleanup
deleteEntry(entry.name, done);
};
// create:true, exclusive:true, file does not exist
root.getFile(fileName, {
create: true,
exclusive: true
}, win, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
});
it('file.spec.19 getFile: create file that already exists', function (done) {
const fileName = 'de.create.existing.file';
const filePath = joinURL(root.fullPath, fileName);
function win (entry) {
expect(entry).toBeDefined();
expect(entry.isFile).toBe(true);
expect(entry.isDirectory).toBe(false);
expect(entry.name).toCanonicallyMatch(fileName);
expect(entry.fullPath).toCanonicallyMatch(filePath);
// cleanup
deleteEntry(entry.name, done);
}
function getFile (file) {
// create:true, exclusive:false, file exists
root.getFile(fileName, {
create: true
}, win, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
}
// create file to kick off it
root.getFile(fileName, {
create: true
}, getFile, failed.bind(null, done, 'root.getFile - Error on initial creating file: ' + fileName));
});
it('file.spec.20 getFile: create file that already exists (exclusive)', function (done) {
const fileName = 'de.create.exclusive.existing.file';
let existingFile;
function fail (error) {
expect(error).toBeDefined();
if (isChrome) {
/* INVALID_MODIFICATION_ERR (code: 9) or ??? (code: 13) is thrown instead of PATH_EXISTS_ERR(code: 12)
on trying to exclusively create a file, which already exists in Chrome. */
// expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
} else {
expect(error).toBeFileError(FileError.PATH_EXISTS_ERR); // eslint-disable-line no-undef
}
// cleanup
deleteEntry(existingFile.name, done);
}
function getFile (file) {
existingFile = file;
// create:true, exclusive:true, file exists
root.getFile(fileName, {
create: true,
exclusive: true
}, succeed.bind(null, done, 'root.getFile - getFile function - Error unexpected callback, file should exists: ' + fileName), fail);
}
// create file to kick off it
root.getFile(fileName, {
create: true
}, getFile, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
});
it('file.spec.21 DirectoryEntry.getFile: get Entry for existing file', function (done) {
const fileName = 'de.get.file';
const filePath = joinURL(root.fullPath, fileName);
const win = function (entry) {
expect(entry).toBeDefined();
expect(entry.isFile).toBe(true);
expect(entry.isDirectory).toBe(false);
expect(entry.name).toCanonicallyMatch(fileName);
expect(entry.fullPath).toCanonicallyMatch(filePath);
expect(entry.filesystem).toBeDefined();
expect(entry.filesystem).toBe(root.filesystem);
// clean up
deleteEntry(entry.name, done);
};
const getFile = function (file) {
// create:false, exclusive:false, file exists
root.getFile(fileName, {
create: false
}, win, failed.bind(null, done, 'root.getFile - Error getting file entry: ' + fileName));
};
// create file to kick off it
root.getFile(fileName, {
create: true
}, getFile, failed.bind(null, done, 'root.getFile - Error creating file: ' + fileName));
});
it('file.spec.22 DirectoryEntry.getFile: get FileEntry for invalid path', function (done) {
if (isBrowser) {
/* The plugin does not follow to ["8.3 Naming restrictions"]
(http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions). */
pending();
}
const fileName = 'de:invalid:path';
const fail = function (error) {
expect(error).toBeDefined();
expect(error).toBeFileError(FileError.ENCODING_ERR); // eslint-disable-line no-undef
done();
};
// create:false, exclusive:false, invalid path
root.getFile(fileName, {
create: false
}, succeed.bind(null, done, 'root.getFile - Error unexpected callback, file should not exists: ' + fileName), fail);
});
it('file.spec.23 DirectoryEntry.getDirectory: get Entry for directory that does not exist', function (done) {
const dirName = 'de.no.dir';
const fail = function (error) {
expect(error).toBeDefined();
if (isChrome) {
expect(error).toBeFileError(FileError.SYNTAX_ERR); // eslint-disable-line no-undef
} else {
expect(error).toBeFileError(FileError.NOT_FOUND_ERR); // eslint-disable-line no-undef
}
done();
};
// create:false, exclusive:false, directory does not exist
root.getDirectory(dirName, {
create: false
}, succeed.bind(null, done, 'root.getDirectory - Error unexpected callback, directory should not exists: ' + dirName), fail);
});
it('file.spec.24 DirectoryEntry.getDirectory: create new dir with space then resolveLocalFileSystemURL', function (done) {
const dirName = 'de create dir';
function win (directory) {
expect(directory).toBeDefined();
expect(directory.isFile).toBe(false);
expect(directory.isDirectory).toBe(true);
expect(directory.name).toCanonicallyMatch(dirName);
expect(directory.fullPath).toCanonicallyMatch(joinURL(root.fullPath, dirName));
// cleanup
deleteEntry(directory.name, done);
}
function getDir (dirEntry) {
expect(dirEntry.filesystem).toBeDefined();
expect(dirEntry.filesystem).toBe(root.filesystem);
const dirURI = dirEntry.nativeURL;
// now encode URI and try to resolve
window.resolveLocalFileSystemURL(dirURI, win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - getDir function - Error resolving directory: ' + dirURI));
}
// create:true, exclusive:false, directory does not exist
root.getDirectory(dirName, {
create: true
}, getDir, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
});
// This test is excluded, and should probably be removed. Filesystem
// should always be properly encoded URLs, and *not* raw paths, and it
// doesn't make sense to double-encode the URLs and expect that to be
// handled by the implementation.
// If a particular platform uses paths internally rather than URLs, // then that platform should careful to pass them correctly to its
// backend.
xit('file.spec.25 DirectoryEntry.getDirectory: create new dir with space resolveLocalFileSystemURL with encoded URI', function (done) {
const dirName = 'de create dir2';
const dirPath = joinURL(root.fullPath, dirName);
function win (directory) {
expect(directory).toBeDefined();
expect(directory.isFile).toBe(false);
expect(directory.isDirectory).toBe(true);
expect(directory.name).toCanonicallyMatch(dirName);
expect(directory.fullPath).toCanonicallyMatch(dirPath);
// cleanup
deleteEntry(directory.name, done);
}
function getDir (dirEntry) {
const dirURI = dirEntry.toURL();
// now encode URI and try to resolve
window.resolveLocalFileSystemURL(encodeURI(dirURI), win, failed.bind(null, done, 'window.resolveLocalFileSystemURL - getDir function - Error resolving directory: ' + dirURI));
}
// create:true, exclusive:false, directory does not exist
root.getDirectory(dirName, {
create: true
}, getDir, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
});
it('file.spec.26 DirectoryEntry.getDirectory: create new directory', function (done) {
const dirName = 'de.create.dir';
const dirPath = joinURL(root.fullPath, dirName);
const win = function (directory) {
expect(directory).toBeDefined();
expect(directory.isFile).toBe(false);
expect(directory.isDirectory).toBe(true);
expect(directory.name).toCanonicallyMatch(dirName);
expect(directory.fullPath).toCanonicallyMatch(dirPath);
expect(directory.filesystem).toBeDefined();
expect(directory.filesystem).toBe(root.filesystem);
// cleanup
deleteEntry(directory.name, done);
};
// create:true, exclusive:false, directory does not exist
root.getDirectory(dirName, {
create: true
}, win, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
});
it('file.spec.27 DirectoryEntry.getDirectory: create new directory (exclusive)', function (done) {
const dirName = 'de.create.exclusive.dir';
const dirPath = joinURL(root.fullPath, dirName);
const win = function (directory) {
expect(directory).toBeDefined();
expect(directory.isFile).toBe(false);
expect(directory.isDirectory).toBe(true);
expect(directory.name).toCanonicallyMatch(dirName);
expect(directory.fullPath).toCanonicallyMatch(dirPath);
expect(directory.filesystem).toBeDefined();
expect(directory.filesystem).toBe(root.filesystem);
// cleanup
deleteEntry(directory.name, done);
};
// create:true, exclusive:true, directory does not exist
root.getDirectory(dirName, {
create: true,
exclusive: true
}, win, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
});
it('file.spec.28 DirectoryEntry.getDirectory: create directory that already exists', function (done) {
const dirName = 'de.create.existing.dir';
const dirPath = joinURL(root.fullPath, dirName);
const win = function (directory) {
expect(directory).toBeDefined();
expect(directory.isFile).toBe(false);
expect(directory.isDirectory).toBe(true);
expect(directory.name).toCanonicallyMatch(dirName);
expect(directory.fullPath).toCanonicallyMatch(dirPath);
// cleanup
deleteEntry(directory.name, done);
};
// create directory to kick off it
root.getDirectory(dirName, {
create: true
}, function () {
root.getDirectory(dirName, {
create: true
}, win, failed.bind(null, done, 'root.getDirectory - Error creating existent second directory : ' + dirName));
}, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
});
it('file.spec.29 DirectoryEntry.getDirectory: create directory that already exists (exclusive)', function (done) {
const dirName = 'de.create.exclusive.existing.dir';
let existingDir;
const fail = function (error) {
expect(error).toBeDefined();
if (isChrome) {
/* INVALID_MODIFICATION_ERR (code: 9) or ??? (code: 13) is thrown instead of PATH_EXISTS_ERR(code: 12)
on trying to exclusively create a file or directory, which already exists (Chrome). */
// expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
} else {
expect(error).toBeFileError(FileError.PATH_EXISTS_ERR); // eslint-disable-line no-undef
}
// cleanup
deleteEntry(existingDir.name, done);
};
// create directory to kick off it
root.getDirectory(dirName, {
create: true
}, function (directory) {
existingDir = directory;
// create:true, exclusive:true, directory exists
root.getDirectory(dirName, {
create: true,
exclusive: true
}, failed.bind(null, done, 'root.getDirectory - Unexpected success callback, second directory should not be created : ' + dirName), fail);
}, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
});
it('file.spec.30 DirectoryEntry.getDirectory: get Entry for existing directory', function (done) {
const dirName = 'de.get.dir';
const dirPath = joinURL(root.fullPath, dirName);
const win = function (directory) {
expect(directory).toBeDefined();
expect(directory.isFile).toBe(false);
expect(directory.isDirectory).toBe(true);
expect(directory.name).toCanonicallyMatch(dirName);
expect(directory.fullPath).toCanonicallyMatch(dirPath);
// cleanup
deleteEntry(directory.name, done);
};
// create directory to kick it off
root.getDirectory(dirName, {
create: true
}, function () {
root.getDirectory(dirName, {
create: false
}, win, failed.bind(null, done, 'root.getDirectory - Error getting directory entry : ' + dirName));
}, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
});
it('file.spec.31 DirectoryEntry.getDirectory: get DirectoryEntry for invalid path', function (done) {
if (isBrowser) {
/* The plugin does not follow to ["8.3 Naming restrictions"]
(http://www.w3.org/TR/2011/WD-file-system-api-20110419/#naming-restrictions). */
pending();
}
const dirName = 'de:invalid:path';
const fail = function (error) {
expect(error).toBeDefined();
expect(error).toBeFileError(FileError.ENCODING_ERR); // eslint-disable-line no-undef
done();
};
// create:false, exclusive:false, invalid path
root.getDirectory(dirName, {
create: false
}, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + dirName), fail);
});
it('file.spec.32 DirectoryEntry.getDirectory: get DirectoryEntry for existing file', function (done) {
const fileName = 'de.existing.file';
let existingFile;
const fail = function (error) {
expect(error).toBeDefined();
if (isChrome) {
// chrome returns an unknown error with code 17
} else {
expect(error).toBeFileError(FileError.TYPE_MISMATCH_ERR); // eslint-disable-line no-undef
}
// cleanup
deleteEntry(existingFile.name, done);
};
// create file to kick off it
root.getFile(fileName, {
create: true
}, function (file) {
existingFile = file;
root.getDirectory(fileName, {
create: false
}, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + fileName), fail);
}, failed.bind(null, done, 'root.getFile - Error creating file : ' + fileName));
});
it('file.spec.33 DirectoryEntry.getFile: get FileEntry for existing directory', function (done) {
const dirName = 'de.existing.dir';
let existingDir;
const fail = function (error) {
expect(error).toBeDefined();
if (isChrome) {
// chrome returns an unknown error with code 17
} else {
expect(error).toBeFileError(FileError.TYPE_MISMATCH_ERR); // eslint-disable-line no-undef
}
// cleanup
deleteEntry(existingDir.name, done);
};
// create directory to kick off it
root.getDirectory(dirName, {
create: true
}, function (directory) {
existingDir = directory;
root.getFile(dirName, {
create: false
}, succeed.bind(null, done, 'root.getFile - Unexpected success callback, file should not exists: ' + dirName), fail);
}, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
});
it('file.spec.34 DirectoryEntry.removeRecursively on directory', function (done) {
const dirName = 'de.removeRecursively';
const subDirName = 'dir';
const dirExists = function (error) {
expect(error).toBeDefined();
if (isChrome) {
expect(error).toBeFileError(FileError.SYNTAX_ERR); // eslint-disable-line no-undef
} else {
expect(error).toBeFileError(FileError.NOT_FOUND_ERR); // eslint-disable-line no-undef
}
done();
};
// create a new directory entry to kick off it
root.getDirectory(dirName, {
create: true
}, function (entry) {
entry.getDirectory(subDirName, {
create: true
}, function (dir) {
entry.removeRecursively(function () {
root.getDirectory(dirName, {
create: false
}, succeed.bind(null, done, 'root.getDirectory - Unexpected success callback, directory should not exists: ' + dirName), dirExists);
}, failed.bind(null, done, 'entry.removeRecursively - Error removing directory recursively : ' + dirName));
}, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + subDirName));
}, failed.bind(null, done, 'root.getDirectory - Error creating directory : ' + dirName));
});
it('file.spec.35 createReader: create reader on existing directory', function () {
// create reader for root directory
const reader = root.createReader();
expect(reader).toBeDefined();
expect(typeof reader.readEntries).toBe('function');
});
it('file.spec.36 removeRecursively on root file system', function (done) {
const remove = function (error) {
expect(error).toBeDefined();
if (isChrome) {
/* INVALID_MODIFICATION_ERR (code: 9) or ??? (code: 13) is thrown instead of
NO_MODIFICATION_ALLOWED_ERR(code: 6) on trying to call removeRecursively
on the root file system (Chrome). */
// expect(error).toBeFileError(FileError.INVALID_MODIFICATION_ERR);
} else {
expect(error).toBeFileError(FileError.NO_MODIFICATION_ALLOWED_ERR); // eslint-disable-line no-undef
}
done();
};
// remove root file system
root.removeRecursively(succeed.bind(null, done, 'root.removeRecursively - Unexpected success callback, root cannot be removed'), remove);
});
});
describe('DirectoryReader interface', function () {
describe('readEntries', function () {
it('file.spec.37 should read contents of existing directory', function (done) {
const win = function (entries) {
expect(entries).toBeDefined();
expect(entries instanceof Array).toBe(true);
done();
};
// create reader for root directory
const reader = root.createReader();
// read entries
reader.readEntries(win, failed.bind(null, done, 'reader.readEntries - Error reading entries'));
});
it('file.spec.37.1 should read contents of existing directory', function (done) {
const dirName = 'readEntries.dir';
const fileName = 'readeEntries.file';
root.getDirectory(dirName, {
create: true
}, function (directory) {
directory.getFile(fileName, {
create: true
}, function (fileEntry) {
const reader = directory.createReader();
reader.readEntries(function (entries) {
expect(entries).toBeDefined();
expect(entries instanceof Array).toBe(true);
expect(entries.length).toBe(1);
expect(entries[0].fullPath).toCanonicallyMatch(fileEntry.fullPath);
expect(entries[0].filesystem).not.toBe(null);
if (isChrome) {
// Slicing '[object {type}]' -> '{type}'
expect(entries[0].filesystem.toString().slice(8, -1)).toEqual('DOMFileSystem');
} else {
expect(entries[0].filesystem instanceof FileSystem).toBe(true); // eslint-disable-line no-undef
}
// cleanup