Add FileTransfer tests for progress events and abort()
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/commit/6ac22a5a Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/tree/6ac22a5a Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/diff/6ac22a5a Branch: refs/heads/master Commit: 6ac22a5a6f6df37f870f775de95ff2883bcefab0 Parents: 80e69c2 Author: Andrew Grieve <agri...@chromium.org> Authored: Thu Sep 20 23:21:44 2012 -0400 Committer: Andrew Grieve <agri...@chromium.org> Committed: Thu Sep 20 23:21:44 2012 -0400 ---------------------------------------------------------------------- autotest/tests/filetransfer.tests.js | 69 ++++++++++++++++++++++++++++- 1 files changed, 68 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-mobile-spec/blob/6ac22a5a/autotest/tests/filetransfer.tests.js ---------------------------------------------------------------------- diff --git a/autotest/tests/filetransfer.tests.js b/autotest/tests/filetransfer.tests.js index 6b83621..22c4f40 100644 --- a/autotest/tests/filetransfer.tests.js +++ b/autotest/tests/filetransfer.tests.js @@ -100,7 +100,7 @@ describe('FileTransfer', function() { root.getFile(fileName, null, // remove file system entry function(entry) { - entry.remove(callback, function() { console.log('[ERROR] deleteFile cleanup method invoked fail callback.'); }); + entry.remove(callback, function() { expect(true).toBe(false, 'deleteFile cleanup method invoked fail callback. File: ' + fileName); }); }, // doesn't exist callback); @@ -141,8 +141,11 @@ describe('FileTransfer', function() { var fail = createDoNotCallSpy('downloadFail'); var remoteFile = server + "/robots.txt" var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1); + var lastProgressEvent = null; + var downloadWin = jasmine.createSpy().andCallFake(function(entry) { expect(entry.name).toBe(localFileName); + expect(lastProgressEvent.loaded).toBeGreaterThan(1); }); this.after(function() { @@ -150,6 +153,9 @@ describe('FileTransfer', function() { }); runs(function() { var ft = new FileTransfer(); + ft.onprogress = function(e) { + lastProgressEvent = e; + }; ft.download(remoteFile, root.fullPath + "/" + localFileName, downloadWin, fail); }); @@ -177,6 +183,30 @@ describe('FileTransfer', function() { waitsForAny(fileWin, downloadFail, fileFail); }); + it("should be stopped by abort() right away", function() { + var downloadWin = createDoNotCallSpy('downloadWin'); + var remoteFile = 'http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3'; + var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1); + var startTime = +new Date(); + + var downloadFail = jasmine.createSpy().andCallFake(function(e) { + expect(e.code).toBe(FileTransferError.ABORT_ERR); + expect(new Date() - startTime).toBeLessThan(150); + }); + + this.after(function() { + deleteFile(localFileName); + }); + runs(function() { + var ft = new FileTransfer(); + ft.abort(); // should be a no-op. + ft.download(remoteFile, root.fullPath + "/" + localFileName, downloadWin, downloadFail); + ft.abort(); + ft.abort(); // should be a no-op. + }); + + waitsForAny(downloadWin, downloadFail); + }); it("should get http status on failure", function() { var downloadWin = createDoNotCallSpy('downloadWin'); @@ -294,6 +324,43 @@ describe('FileTransfer', function() { waitsForAny(uploadWin, uploadFail, fileFail); }); + it("should be stopped by abort() right away.", function() { + var remoteFile = server + "/upload"; + var localFileName = "upload2.txt"; + + var fileFail = createDoNotCallSpy('fileFail'); + var uploadWin = createDoNotCallSpy('uploadWin', 'Should have been aborted'); + var startTime = +new Date(); + + var uploadFail = jasmine.createSpy().andCallFake(function(e) { + expect(e.code).toBe(FileTransferError.ABORT_ERR); + expect(new Date() - startTime).toBeLessThan(150); + }); + + var fileWin = function(fileEntry) { + ft = new FileTransfer(); + + var options = new FileUploadOptions(); + options.fileKey = "file"; + options.fileName = localFileName; + options.mimeType = "text/plain"; + + // removing options cause Android to timeout + ft.abort(); // should be a no-op. + ft.upload(fileEntry.fullPath, remoteFile, uploadWin, uploadFail, options); + ft.abort(); + ft.abort(); // should be a no-op. + }; + + this.after(function() { + deleteFile(localFileName); + }); + runs(function() { + writeFile(localFileName, new Array(10000).join('aborttest!'), fileWin, fileFail); + }); + + waitsForAny(uploadWin, uploadFail, fileFail); + }); it("should get http status on failure", function() { var fileFail = createDoNotCallSpy('fileFail'); var uploadWin = createDoNotCallSpy('uploadWin');