~zaspire/cordova-ubuntu-tests/disable-more-tests

« back to all changes in this revision

Viewing changes to www/autotest/tests/file.tests.js

  • Committer: VĂ­ctor R. Ruiz
  • Date: 2013-07-25 13:09:34 UTC
  • Revision ID: victor.ruiz@canonical.com-20130725130934-d4q95mh8eehbv363
Initial import

Show diffs side-by-side

added added

removed removed

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