[BlackBerry10] Implement File API as client side wrapper Reviewed by Jeffrey Heifetz <jheif...@blackberry.com> Tested by Tracy Li <t...@blackberry.com>
Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/f0fb6ec0 Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/f0fb6ec0 Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/f0fb6ec0 Branch: refs/heads/master Commit: f0fb6ec02c3c216c04591441b2359c4100cea66e Parents: f4f3f4b Author: Bryan Higgins <bhigg...@blackberry.com> Authored: Tue Apr 9 17:09:07 2013 -0400 Committer: Bryan Higgins <bhigg...@blackberry.com> Committed: Fri May 3 09:50:04 2013 -0400 ---------------------------------------------------------------------- lib/blackberry10/plugin/DirectoryEntry.js | 57 ++ lib/blackberry10/plugin/DirectoryReader.js | 47 ++ lib/blackberry10/plugin/Entry.js | 118 ++++ lib/blackberry10/plugin/FileEntry.js | 47 ++ lib/blackberry10/plugin/FileReader.js | 98 ++++ lib/blackberry10/plugin/FileSystem.js | 27 + lib/blackberry10/plugin/FileWriter.js | 120 ++++ lib/blackberry10/plugin/blackberry10/file.js | 424 --------------- .../plugin/blackberry10/fileTransfer.js | 33 +- lib/blackberry10/plugin/blackberry10/fileUtils.js | 47 ++ lib/blackberry10/plugin/blackberry10/platform.js | 3 + .../plugin/blackberry10/pluginUtils.js | 4 +- .../plugin/blackberry10/requestFileSystem.js | 39 -- lib/blackberry10/plugin/requestFileSystem.js | 38 ++ .../plugin/resolveLocalFileSystemURI.js | 55 ++ test/blackberry10/test.fileTransfer.js | 85 --- 16 files changed, 674 insertions(+), 568 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/DirectoryEntry.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/DirectoryEntry.js b/lib/blackberry10/plugin/DirectoryEntry.js new file mode 100644 index 0000000..259a79e --- /dev/null +++ b/lib/blackberry10/plugin/DirectoryEntry.js @@ -0,0 +1,57 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var argscheck = require('cordova/argscheck'), + utils = require('cordova/utils'), + Entry = require('cordova/plugin/Entry'), + FileError = require('cordova/plugin/FileError'), + DirectoryReader = require('cordova/plugin/DirectoryReader'), + fileUtils = require('cordova/plugin/blackberry10/fileUtils'), + DirectoryEntry = function (name, fullPath) { + DirectoryEntry.__super__.constructor.call(this, false, true, name, fullPath); + }; + +utils.extend(DirectoryEntry, Entry); + +DirectoryEntry.prototype.createReader = function () { + return new DirectoryReader(this.fullPath); +}; + +DirectoryEntry.prototype.getDirectory = function (path, options, successCallback, errorCallback) { + argscheck.checkArgs('sOFF', 'DirectoryEntry.getDirectory', arguments); + this.nativeEntry.getDirectory(path, options, function (entry) { + successCallback(fileUtils.createEntry(entry)); + }, errorCallback); +}; + +DirectoryEntry.prototype.removeRecursively = function (successCallback, errorCallback) { + argscheck.checkArgs('FF', 'DirectoryEntry.removeRecursively', arguments); + this.nativeEntry.removeRecursively(successCallback, errorCallback); +}; + +DirectoryEntry.prototype.getFile = function (path, options, successCallback, errorCallback) { + argscheck.checkArgs('sOFF', 'DirectoryEntry.getFile', arguments); + this.nativeEntry.getFile(path, options, function (entry) { + successCallback(fileUtils.createEntry(entry)); + }, errorCallback); +}; + +module.exports = DirectoryEntry; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/DirectoryReader.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/DirectoryReader.js b/lib/blackberry10/plugin/DirectoryReader.js new file mode 100644 index 0000000..8bb9968 --- /dev/null +++ b/lib/blackberry10/plugin/DirectoryReader.js @@ -0,0 +1,47 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var FileError = require('cordova/plugin/FileError'), + fileUtils = require('cordova/plugin/blackberry10/fileUtils'); + +function DirectoryReader(path) { + this.path = path; +} + +DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) { + var win = typeof successCallback !== 'function' ? null : function(result) { + var retVal = []; + for (var i=0; i<result.length; i++) { + retVal.push(fileUtils.createEntry(result[i])); + } + successCallback(retVal); + }, + fail = typeof errorCallback !== 'function' ? null : function(code) { + errorCallback(new FileError(code)); + }; + fileUtils.getEntryForURI(this.path, function (entry) { + entry.nativeEntry.createReader().readEntries(win, fail); + }, function () { + fail(FileError.NOT_FOUND_ERR); + }); +}; + +module.exports = DirectoryReader; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/Entry.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/Entry.js b/lib/blackberry10/plugin/Entry.js new file mode 100644 index 0000000..f80e879 --- /dev/null +++ b/lib/blackberry10/plugin/Entry.js @@ -0,0 +1,118 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var argscheck = require('cordova/argscheck'), + FileError = require('cordova/plugin/FileError'), + Metadata = require('cordova/plugin/Metadata'), + fileUtils = require('cordova/plugin/blackberry10/fileUtils'); + +function Entry(isFile, isDirectory, name, fullPath, fileSystem) { + this.isFile = !!isFile; + this.isDirectory = !!isDirectory; + this.name = name || ''; + this.fullPath = fullPath || ''; + this.filesystem = fileSystem || null; +} + +Entry.prototype.getMetadata = function(successCallback, errorCallback) { + argscheck.checkArgs('FF', 'Entry.getMetadata', arguments); + var success = function(lastModified) { + var metadata = new Metadata(lastModified); + if (typeof successCallback === 'function') { + successCallback(metadata); + } + }; + this.nativeEntry.getMetadata(success, errorCallback); +}; + +Entry.prototype.setMetadata = function(successCallback, errorCallback, metadataObject) { + argscheck.checkArgs('FFO', 'Entry.setMetadata', arguments); + errorCallback("Not supported by platform"); +}; + +Entry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) { + argscheck.checkArgs('oSFF', 'Entry.moveTo', arguments); + var fail = errorCallback && function(code) { + errorCallback(new FileError(code)); + }, + srcPath = this.fullPath, + name = newName || this.name, + success = function(entry) { + if (entry) { + if (successCallback === 'function') { + successCallback(fileUtils.createEntry(entry)); + } + } + else { + if (typeof fail === 'function') { + fail(FileError.NOT_FOUND_ERR); + } + } + }; + this.nativeEntry.moveTo(parent.nativeEntry, newName, success, errorCallback); +}; + + +Entry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) { + argscheck.checkArgs('oSFF', 'Entry.copyTo', arguments); + var fail = errorCallback && function(code) { + errorCallback(new FileError(code)); + }, + srcPath = this.fullPath, + name = newName || this.name, + success = function(entry) { + if (entry) { + if (typeof successCallback === 'function') { + successCallback(fileUtils.createEntry(entry)); + } + } + else { + if (typeof fail === 'function') { + fail(FileError.NOT_FOUND_ERR); + } + } + }; + this.nativeEntry.copyTo(parent.nativeEntry, newName, success, errorCallback); +}; + +Entry.prototype.toURL = function() { + return this.fullPath; +}; + +Entry.prototype.toURI = function(mimeType) { + console.log("DEPRECATED: Update your code to use 'toURL'"); + return this.toURL(); +}; + +Entry.prototype.remove = function(successCallback, errorCallback) { + argscheck.checkArgs('FF', 'Entry.remove', arguments); + this.nativeEntry.remove(successCallback, errorCallback); +}; + +Entry.prototype.getParent = function(successCallback, errorCallback) { + argscheck.checkArgs('FF', 'Entry.getParent', arguments); + var win = successCallback && function(result) { + successCallback(fileUtils.createEntry(result)); + }; + this.nativeEntry.getParent(win, errorCallback); +}; + +module.exports = Entry; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/FileEntry.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/FileEntry.js b/lib/blackberry10/plugin/FileEntry.js new file mode 100644 index 0000000..17f9393 --- /dev/null +++ b/lib/blackberry10/plugin/FileEntry.js @@ -0,0 +1,47 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var utils = require('cordova/utils'), + Entry = require('cordova/plugin/Entry'), + FileWriter = require('cordova/plugin/FileWriter'), + File = require('cordova/plugin/File'), + FileError = require('cordova/plugin/FileError'), + FileEntry = function (name, fullPath) { + FileEntry.__super__.constructor.apply(this, [true, false, name, fullPath]); + }; + +utils.extend(FileEntry, Entry); + +FileEntry.prototype.createWriter = function(successCallback, errorCallback) { + this.file(function (file) { + successCallback(new FileWriter(file)); + }, errorCallback); +}; + +FileEntry.prototype.file = function(successCallback, errorCallback) { + var fullPath = this.fullPath, + success = function (file) { + successCallback(new File(file.name, fullPath, file.type, file.lastModifiedDate, file.size)); + }; + this.nativeEntry.file(success, errorCallback); +}; + +module.exports = FileEntry; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/FileReader.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/FileReader.js b/lib/blackberry10/plugin/FileReader.js new file mode 100644 index 0000000..c806b3f --- /dev/null +++ b/lib/blackberry10/plugin/FileReader.js @@ -0,0 +1,98 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var origFileReader = window.FileReader, + fileUtils = require('cordova/plugin/blackberry10/fileUtils'), + utils = require('cordova/utils'); + +var FileReader = function() { + this.nativeReader = new origFileReader(); +}; + +utils.defineGetter(FileReader.prototype, 'readyState', function() { + return this.nativeReader.readyState; +}); + +utils.defineGetter(FileReader.prototype, 'error', function() { + return this.nativeReader.error; +}); + +utils.defineGetter(FileReader.prototype, 'result', function() { + return this.nativeReader.result; +}); + +function defineEvent(eventName) { + utils.defineGetterSetter(FileReader.prototype, eventName, function() { + return this.nativeReader[eventName] || null; + }, function(value) { + this.nativeReader[eventName] = value; + }); +} + +defineEvent('onabort'); +defineEvent('onerror'); +defineEvent('onload'); +defineEvent('onloadend'); +defineEvent('onloadstart'); +defineEvent('onprogress'); + +FileReader.prototype.abort = function() { + return this.nativeReader.abort(); +}; + +FileReader.prototype.readAsText = function(file, encoding) { + var that = this; + fileUtils.getEntryForURI(file.fullPath, function (entry) { + entry.nativeEntry.file(function (nativeFile) { + that.nativeReader.readAsText(nativeFile, encoding); + }, that.onerror); + }, that.onerror); +}; + +FileReader.prototype.readAsDataURL = function(file) { + var that = this; + fileUtils.getEntryForURI(file.fullPath, function (entry) { + entry.nativeEntry.file(function (nativeFile) { + that.nativeReader.readAsDataURL(nativeFile); + }, that.onerror); + }, that.onerror); +}; + +FileReader.prototype.readAsBinaryString = function(file) { + var that = this; + fileUtils.getEntryForURI(file.fullPath, function (entry) { + entry.nativeEntry.file(function (nativeFile) { + that.nativeReader.readAsBinaryString(nativeFile); + }, that.onerror); + }, that.onerror); +}; + +FileReader.prototype.readAsArrayBuffer = function(file) { + var that = this; + fileUtils.getEntryForURI(file.fullPath, function (entry) { + entry.nativeEntry.file(function (nativeFile) { + that.nativeReader.readAsArrayBuffer(nativeFile); + }, that.onerror); + }, that.onerror); +}; + +window.FileReader = FileReader; +module.exports = FileReader; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/FileSystem.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/FileSystem.js b/lib/blackberry10/plugin/FileSystem.js new file mode 100644 index 0000000..d1432d6 --- /dev/null +++ b/lib/blackberry10/plugin/FileSystem.js @@ -0,0 +1,27 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +module.exports = function(name, root) { + this.name = name || null; + if (root) { + this.root = root; + } +}; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/FileWriter.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/FileWriter.js b/lib/blackberry10/plugin/FileWriter.js new file mode 100644 index 0000000..0f71d6a --- /dev/null +++ b/lib/blackberry10/plugin/FileWriter.js @@ -0,0 +1,120 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var FileError = require('cordova/plugin/FileError'), + ProgressEvent = require('cordova/plugin/ProgressEvent'), + fileUtils = require('cordova/plugin/blackberry10/fileUtils'), + utils = require('cordova/utils'); + +function FileWriter (file) { + var that = this; + this.file = file; + this.events = {}; + this.pending = []; + fileUtils.getEntryForURI(file.fullPath, function (entry) { + entry.nativeEntry.createWriter(function (writer) { + var i, + event; + that.nativeWriter = writer; + for (event in that.events) { + if (that.events.hasOwnProperty(event)) { + that.nativeWriter[event] = that.events[event]; + } + } + for (i = 0; i < that.pending.length; i++) { + that.pending[i](); + } + }); + }); + this.events = {}; + this.pending = []; +} + +utils.defineGetter(FileWriter.prototype, 'error', function() { + return this.nativeWriter ? this.nativeWriter.error : null; +}); + +utils.defineGetter(FileWriter.prototype, 'fileName', function() { + return this.nativeWriter ? this.nativeWriter.fileName : this.file.name; +}); + +utils.defineGetter(FileWriter.prototype, 'length', function() { + return this.nativeWriter ? this.nativeWriter.length : this.file.size; +}); + +utils.defineGetter(FileWriter.prototype, 'position', function() { + return this.nativeWriter ? this.nativeWriter.position : 0; +}); + +utils.defineGetter(FileWriter.prototype, 'readyState', function() { + return this.nativeWriter ? this.nativeWriter.readyState : 0; +}); + +function defineEvent(eventName) { + utils.defineGetterSetter(FileWriter.prototype, eventName, function() { + return this.nativeWriter ? this.nativeWriter[eventName] || null : this.events[eventName] || null; + }, function(value) { + if (this.nativeWriter) { + this.nativeWriter[eventName] = value; + } + else { + this.events[eventName] = value; + } + }); +} + +defineEvent('onabort'); +defineEvent('onerror'); +defineEvent('onprogress'); +defineEvent('onwrite'); +defineEvent('onwriteend'); +defineEvent('onwritestart'); + +FileWriter.prototype.abort = function() { + this.nativeWriter.abort(); +}; + +FileWriter.prototype.write = function(text) { + var that = this, + op = function () { + that.nativeWriter.write(new Blob([text])); + }; + this.nativeWriter ? op() : this.pending.push(op); + +}; + +FileWriter.prototype.seek = function(offset) { + var that = this, + op = function () { + that.nativeWriter.seek(offset); + }; + this.nativeWriter ? op() : this.pending.push(op); +}; + +FileWriter.prototype.truncate = function(size) { + var that = this, + op = function () { + that.nativeWriter.truncate(size); + }; + this.nativeWriter ? op() : this.pending.push(op); +}; + +module.exports = FileWriter; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/blackberry10/file.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/blackberry10/file.js b/lib/blackberry10/plugin/blackberry10/file.js deleted file mode 100644 index d611daf..0000000 --- a/lib/blackberry10/plugin/blackberry10/file.js +++ /dev/null @@ -1,424 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -/*global WebKitBlobBuilder:false */ -/*global Blob:false */ -var cordova = require('cordova'), - FileError = require('cordova/plugin/FileError'), - DirectoryEntry = require('cordova/plugin/DirectoryEntry'), - FileEntry = require('cordova/plugin/FileEntry'), - File = require('cordova/plugin/File'), - FileSystem = require('cordova/plugin/FileSystem'), - FileReader = require('cordova/plugin/FileReader'), - nativeRequestFileSystem = window.webkitRequestFileSystem, - nativeResolveLocalFileSystemURI = function(uri, success, fail) { - if (uri.substring(0,11) !== "filesystem:") { - uri = "filesystem:" + uri; - } - window.webkitResolveLocalFileSystemURL(uri, success, fail); - }, - NativeFileReader = window.FileReader; - -window.FileReader = FileReader; -window.File = File; - -function getFileSystemName(nativeFs) { - return (nativeFs.name.indexOf("Persistent") != -1) ? "persistent" : "temporary"; -} - -function makeEntry(entry) { - if (entry.isDirectory) { - return new DirectoryEntry(entry.name, decodeURI(entry.toURL()).substring(11)); - } - else { - return new FileEntry(entry.name, decodeURI(entry.toURL()).substring(11)); - } -} - -module.exports = { - /* requestFileSystem */ - requestFileSystem: function(args, successCallback, errorCallback) { - var type = args[0], - size = args[1]; - - nativeRequestFileSystem(type, size, function(nativeFs) { - successCallback(new FileSystem(getFileSystemName(nativeFs), makeEntry(nativeFs.root))); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - /* resolveLocalFileSystemURI */ - resolveLocalFileSystemURI: function(args, successCallback, errorCallback) { - var uri = args[0]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - successCallback(makeEntry(entry)); - }, function(error) { - var code = error.code; - switch (code) { - case 5: - code = FileError.NOT_FOUND_ERR; - break; - - case 2: - code = FileError.ENCODING_ERR; - break; - } - errorCallback(code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - /* DirectoryReader */ - readEntries: function(args, successCallback, errorCallback) { - var uri = args[0]; - - nativeResolveLocalFileSystemURI(uri, function(dirEntry) { - var reader = dirEntry.createReader(); - reader.readEntries(function(entries) { - var retVal = []; - for (var i = 0; i < entries.length; i++) { - retVal.push(makeEntry(entries[i])); - } - successCallback(retVal); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - /* Entry */ - getMetadata: function(args, successCallback, errorCallback) { - var uri = args[0]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - entry.getMetadata(function(metaData) { - successCallback(metaData.modificationTime); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - moveTo: function(args, successCallback, errorCallback) { - var srcUri = args[0], - parentUri = args[1], - name = args[2]; - - nativeResolveLocalFileSystemURI(srcUri, function(source) { - nativeResolveLocalFileSystemURI(parentUri, function(parent) { - source.moveTo(parent, name, function(entry) { - successCallback(makeEntry(entry)); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - copyTo: function(args, successCallback, errorCallback) { - var srcUri = args[0], - parentUri = args[1], - name = args[2]; - - nativeResolveLocalFileSystemURI(srcUri, function(source) { - nativeResolveLocalFileSystemURI(parentUri, function(parent) { - source.copyTo(parent, name, function(entry) { - successCallback(makeEntry(entry)); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - remove: function(args, successCallback, errorCallback) { - var uri = args[0]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - if (entry.fullPath === "/") { - errorCallback(FileError.NO_MODIFICATION_ALLOWED_ERR); - } else { - entry.remove( - function (success) { - if (successCallback) { - successCallback(success); - } - }, - function(error) { - if (errorCallback) { - errorCallback(error.code); - } - } - ); - } - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - getParent: function(args, successCallback, errorCallback) { - var uri = args[0]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - entry.getParent(function(entry) { - successCallback(makeEntry(entry)); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - /* FileEntry */ - getFileMetadata: function(args, successCallback, errorCallback) { - var uri = args[0]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - entry.file(function(file) { - var retVal = new File(file.name, decodeURI(entry.toURL()), file.type, file.lastModifiedDate, file.size); - successCallback(retVal); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - /* DirectoryEntry */ - getDirectory: function(args, successCallback, errorCallback) { - var uri = args[0], - path = args[1], - options = args[2]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - entry.getDirectory(path, options, function(entry) { - successCallback(makeEntry(entry)); - }, function(error) { - if (error.code === FileError.INVALID_MODIFICATION_ERR) { - if (options.create) { - errorCallback(FileError.PATH_EXISTS_ERR); - } else { - errorCallback(FileError.ENCODING_ERR); - } - } else { - errorCallback(error.code); - } - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - removeRecursively: function(args, successCallback, errorCallback) { - var uri = args[0]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - if (entry.fullPath === "/") { - errorCallback(FileError.NO_MODIFICATION_ALLOWED_ERR); - } else { - entry.removeRecursively( - function (success) { - if (successCallback) { - successCallback(success); - } - }, - function(error) { - errorCallback(error.code); - } - ); - } - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - getFile: function(args, successCallback, errorCallback) { - var uri = args[0], - path = args[1], - options = args[2]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - entry.getFile(path, options, function(entry) { - successCallback(makeEntry(entry)); - }, function(error) { - if (error.code === FileError.INVALID_MODIFICATION_ERR) { - if (options.create) { - errorCallback(FileError.PATH_EXISTS_ERR); - } else { - errorCallback(FileError.NOT_FOUND_ERR); - } - } else { - errorCallback(error.code); - } - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - /* FileReader */ - readAsText: function(args, successCallback, errorCallback) { - var uri = args[0], - encoding = args[1]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - var onLoadEnd = function(evt) { - if (!evt.target.error) { - successCallback(evt.target.result); - } - }, - onError = function(evt) { - errorCallback(evt.target.error.code); - }; - - var reader = new NativeFileReader(); - - reader.onloadend = onLoadEnd; - reader.onerror = onError; - entry.file(function(file) { - reader.readAsText(file, encoding); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - readAsDataURL: function(args, successCallback, errorCallback) { - var uri = args[0]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - var onLoadEnd = function(evt) { - if (!evt.target.error) { - successCallback(evt.target.result); - } - }, - onError = function(evt) { - errorCallback(evt.target.error.code); - }; - - var reader = new NativeFileReader(); - - reader.onloadend = onLoadEnd; - reader.onerror = onError; - entry.file(function(file) { - reader.readAsDataURL(file); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - /* FileWriter */ - write: function(args, successCallback, errorCallback) { - var uri = args[0], - text = args[1], - position = args[2]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - var onWriteEnd = function(evt) { - if(!evt.target.error) { - successCallback(evt.target.position - position); - } else { - errorCallback(evt.target.error.code); - } - }, - onError = function(evt) { - errorCallback(evt.target.error.code); - }; - - entry.createWriter(function(writer) { - writer.onwriteend = onWriteEnd; - writer.onerror = onError; - - writer.seek(position); - writer.write(new Blob([text], {type: "text/plain"})); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - }, - - truncate: function(args, successCallback, errorCallback) { - var uri = args[0], - size = args[1]; - - nativeResolveLocalFileSystemURI(uri, function(entry) { - var onWriteEnd = function(evt) { - if(!evt.target.error) { - successCallback(evt.target.length); - } else { - errorCallback(evt.target.error.code); - } - }, - onError = function(evt) { - errorCallback(evt.target.error.code); - }; - - entry.createWriter(function(writer) { - writer.onwriteend = onWriteEnd; - writer.onerror = onError; - - writer.truncate(size); - }, function(error) { - errorCallback(error.code); - }); - }, function(error) { - errorCallback(error.code); - }); - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; - } -}; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/blackberry10/fileTransfer.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/blackberry10/fileTransfer.js b/lib/blackberry10/plugin/blackberry10/fileTransfer.js index 6848516..c225433 100644 --- a/lib/blackberry10/plugin/blackberry10/fileTransfer.js +++ b/lib/blackberry10/plugin/blackberry10/fileTransfer.js @@ -21,9 +21,6 @@ /*global Blob:false */ var cordova = require('cordova'), - FileEntry = require('cordova/plugin/FileEntry'), - FileTransferError = require('cordova/plugin/FileTransferError'), - FileUploadResult = require('cordova/plugin/FileUploadResult'), ProgressEvent = require('cordova/plugin/ProgressEvent'), nativeResolveLocalFileSystemURI = function(uri, success, fail) { if (uri.substring(0,11) !== "filesystem:") { @@ -69,7 +66,7 @@ module.exports = { headers = args[8]; if (!checkURL(server)) { - fail(new FileTransferError(FileTransferError.INVALID_URL_ERR)); + fail(new window.FileTransferError(window.FileTransferError.INVALID_URL_ERR)); } nativeResolveLocalFileSystemURI(filePath, function(entry) { @@ -88,22 +85,22 @@ module.exports = { xhr.open("POST", server); xhr.onload = function(evt) { if (xhr.status == 200) { - var result = new FileUploadResult(); + var result = new window.FileUploadResult(); result.bytesSent = file.size; result.responseCode = xhr.status; result.response = xhr.response; win(result); } else if (xhr.status == 404) { - fail(new FileTransferError(FileTransferError.INVALID_URL_ERR, server, filePath, xhr.status)); + fail(new window.FileTransferError(window.FileTransferError.INVALID_URL_ERR, server, filePath, xhr.status)); } else { - fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr.status)); + fail(new window.FileTransferError(window.FileTransferError.CONNECTION_ERR, server, filePath, xhr.status)); } }; xhr.ontimeout = function(evt) { - fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, xhr.status)); + fail(new window.FileTransferError(window.FileTransferError.CONNECTION_ERR, server, filePath, xhr.status)); }; xhr.onerror = function () { - fail(new FileTransferError(FileTransferError.CONNECTION_ERR, server, filePath, this.status)); + fail(new window.FileTransferError(window.FileTransferError.CONNECTION_ERR, server, filePath, this.status)); }; xhr.onprogress = function (evt) { win(evt); @@ -133,10 +130,10 @@ module.exports = { end = start + bytesPerChunk; } }, function(error) { - fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR)); + fail(new window.FileTransferError(window.FileTransferError.FILE_NOT_FOUND_ERR)); }); }, function(error) { - fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR)); + fail(new window.FileTransferError(window.FileTransferError.FILE_NOT_FOUND_ERR)); }); return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "async"}; @@ -148,7 +145,7 @@ module.exports = { fileWriter; if (!checkURL(source)) { - fail(new FileTransferError(FileTransferError.INVALID_URL_ERR)); + fail(new window.FileTransferError(window.FileTransferError.INVALID_URL_ERR)); } xhr = new XMLHttpRequest(); @@ -158,7 +155,7 @@ module.exports = { fileWriter = writer; fileWriter.onwriteend = function (evt) { if (!evt.target.error) { - win(new FileEntry(entry.name, entry.toURL())); + win(new window.FileEntry(entry.name, entry.toURL())); } else { fail(evt.target.error); } @@ -173,7 +170,7 @@ module.exports = { } xhr.onerror = function (e) { - fail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr.status)); + fail(new window.FileTransferError(window.FileTransferError.CONNECTION_ERR, source, target, xhr.status)); }; xhr.onload = function () { @@ -181,15 +178,15 @@ module.exports = { if (xhr.status === 200 && xhr.response) { nativeResolveLocalFileSystemURI(getParentPath(target), function (dir) { dir.getFile(getFileName(target), {create: true}, writeFile, function (error) { - fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR)); + fail(new window.FileTransferError(window.FileTransferError.FILE_NOT_FOUND_ERR)); }); }, function (error) { - fail(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR)); + fail(new window.FileTransferError(window.FileTransferError.FILE_NOT_FOUND_ERR)); }); } else if (xhr.status === 404) { - fail(new FileTransferError(FileTransferError.INVALID_URL_ERR, source, target, xhr.status)); + fail(new window.FileTransferError(window.FileTransferError.INVALID_URL_ERR, source, target, xhr.status)); } else { - fail(new FileTransferError(FileTransferError.CONNECTION_ERR, source, target, xhr.status)); + fail(new window.FileTransferError(window.FileTransferError.CONNECTION_ERR, source, target, xhr.status)); } } }; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/blackberry10/fileUtils.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/blackberry10/fileUtils.js b/lib/blackberry10/plugin/blackberry10/fileUtils.js new file mode 100644 index 0000000..e26e4e9 --- /dev/null +++ b/lib/blackberry10/plugin/blackberry10/fileUtils.js @@ -0,0 +1,47 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +function convertPath(url) { + return decodeURI(url).substring(11).replace(/\/$/, ''); +} + +module.exports = { + + createEntry: function (entry) { + var cordovaEntry; + if (entry.isFile) { + cordovaEntry = new window.FileEntry(entry.name, convertPath(entry.toURL())); + } else { + cordovaEntry = new window.DirectoryEntry(entry.name, convertPath(entry.toURL())); + } + cordovaEntry.nativeEntry = entry; + return cordovaEntry; + }, + + getEntryForURI: function (uri, success, fail) { + //TODO: account for local vs file system + window.resolveLocalFileSystemURI(uri, success, fail); + }, + + getFileSystemName: function (fs) { + return (fs.name.indexOf('Persistent') != -1) ? 'persistent' : 'temporary'; + } +}; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/blackberry10/platform.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/blackberry10/platform.js b/lib/blackberry10/plugin/blackberry10/platform.js index b1ab163..231d890 100644 --- a/lib/blackberry10/plugin/blackberry10/platform.js +++ b/lib/blackberry10/plugin/blackberry10/platform.js @@ -54,6 +54,9 @@ module.exports = { } } } + }, + File: { + path: 'cordova/plugin/File' } }, merges: { http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/blackberry10/pluginUtils.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/blackberry10/pluginUtils.js b/lib/blackberry10/plugin/blackberry10/pluginUtils.js index aa49077..ed41518 100644 --- a/lib/blackberry10/plugin/blackberry10/pluginUtils.js +++ b/lib/blackberry10/plugin/blackberry10/pluginUtils.js @@ -58,7 +58,7 @@ module.exports = { } } if (count === 0) { - callback(); + callback(); } }, @@ -85,4 +85,4 @@ module.exports = { }; request.send(null); } -} +}; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/blackberry10/requestFileSystem.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/blackberry10/requestFileSystem.js b/lib/blackberry10/plugin/blackberry10/requestFileSystem.js deleted file mode 100644 index 18c64e5..0000000 --- a/lib/blackberry10/plugin/blackberry10/requestFileSystem.js +++ /dev/null @@ -1,39 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -function getFileSystemName(fs) { - return (fs.name.indexOf("Persistent") != -1) ? "persistent" : "temporary"; -} - -function makeEntry(entry) { - if (entry.isDirectory) { - return new DirectoryEntry(entry.name, decodeURI(entry.toURL()).substring(11)); - } - else { - return new FileEntry(entry.name, decodeURI(entry.toURL()).substring(11)); - } -} - -module.exports = function (type, size, success, fail) { - window.webkitRequestFileSystem(type, size, function (fs) { - success((new FileSystem(getFileSystemName(fs), makeEntry(fs.root)))); - }, fail); -}; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/requestFileSystem.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/requestFileSystem.js b/lib/blackberry10/plugin/requestFileSystem.js new file mode 100644 index 0000000..62e1753 --- /dev/null +++ b/lib/blackberry10/plugin/requestFileSystem.js @@ -0,0 +1,38 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var fileUtils = require('cordova/plugin/blackberry10/fileUtils'), + FileError = require('cordova/plugin/FileError'), + FileSystem = require('cordova/plugin/FileSystem'); + +module.exports = function (type, size, success, fail) { + if (size >= 1000000000000000) { + fail(new FileError(FileError.QUOTA_EXCEEDED_ERR)); + } else if (type !== 1 && type !== 0) { + fail(new FileError(FileError.SYNTAX_ERR)); + } else { + window.webkitRequestFileSystem(type, size, function (fs) { + success((new FileSystem(fileUtils.getFileSystemName(fs), fileUtils.createEntry(fs.root)))); + }, function (error) { + fail(new FileError(error)); + }); + } +}; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/lib/blackberry10/plugin/resolveLocalFileSystemURI.js ---------------------------------------------------------------------- diff --git a/lib/blackberry10/plugin/resolveLocalFileSystemURI.js b/lib/blackberry10/plugin/resolveLocalFileSystemURI.js new file mode 100644 index 0000000..d11b0ca --- /dev/null +++ b/lib/blackberry10/plugin/resolveLocalFileSystemURI.js @@ -0,0 +1,55 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var fileUtils = require('cordova/plugin/blackberry10/fileUtils'), + FileError = require('cordova/plugin/FileError'); + +module.exports = function (uri, success, fail) { + var type, + path, + paramPath; + if (!uri || uri.indexOf("/") === 0) { + fail(new FileError(FileError.ENCODING_ERR)); + } else { + type = uri.indexOf("persistent") === -1 ? 0 : 1; + path = uri.substring(type === 1 ? uri.indexOf("persistent") + 11 : uri.indexOf("temporary") + 10); + if (path.substring(0,1) == "/") { + path = path.substring(1); + } + paramPath = path.indexOf("?"); + if (paramPath > -1) { + path = path.substring(0, paramPath); + } + window.webkitRequestFileSystem(type, 25*1024*1024, function (fs) { + if (path === "") { + success(fileUtils.createEntry(fs.root)); + } else { + fs.root.getDirectory(path, {}, function (entry) { + success(fileUtils.createEntry(entry)); + }, function () { + fs.root.getFile(path, {}, function (entry) { + success(fileUtils.createEntry(entry)); + }, fail); + }); + } + }, fail); + } +}; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f0fb6ec0/test/blackberry10/test.fileTransfer.js ---------------------------------------------------------------------- diff --git a/test/blackberry10/test.fileTransfer.js b/test/blackberry10/test.fileTransfer.js deleted file mode 100644 index 1d7ed57..0000000 --- a/test/blackberry10/test.fileTransfer.js +++ /dev/null @@ -1,85 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -describe("blackberry10 fileTransfer", function () { - var fileTransfer = require('cordova/plugin/blackberry10/fileTransfer'), - cordova = require('cordova'), - win = jasmine.createSpy('win'), - fail = jasmine.createSpy('fail') - xhrSend = jasmine.createSpy('xhr send'); - xhrOpen = jasmine.createSpy('xhr open'); - - beforeEach(function () { - global.blackberry = { - io:{ - filetransfer: { - download: jasmine.createSpy('download'), - upload: jasmine.createSpy('upload') - } - } - }; - XMLHttpRequest = function () { - var xhr = { - send: xhrSend, - open: xhrOpen - }; - return xhr; - }; - window.webkitResolveLocalFileSystemURL = jasmine.createSpy("resolveFS") - }); - - afterEach(function () { - delete global.blackberry; - delete XMLHttpRequest; - delete webkitResolveLocalFileSystemURL; - delete window.webkitResolveLocalFileSystemURL; - }); - - describe("download", function(){ - it('should call the blackberry download', function () { - fileTransfer.download(["source/file", "target/file"], win, fail); - expect(xhrOpen).toHaveBeenCalled(); - expect(xhrSend).toHaveBeenCalled(); - }); - - it('should return No Result', function(){ - expect(fileTransfer.download(["location/source", "location/place/here"], win, fail)).toEqual({ - status: cordova.callbackStatus.NO_RESULT, - message: "async" - }); - }); - }); - - describe('upload', function(){ - it('should call the blackberry upload', function(){ - fileTransfer.upload(["source", "target", "fileKey", "fileName", "mimeType", "params", "chunkedMode"], win, fail); - expect(xhrOpen).toHaveBeenCalled(); - expect(xhrSend).toHaveBeenCalled(); - }); - - it('should return No Result', function(){ - expect(fileTransfer.upload(["location/source", "location/place/here"], win, fail)).toEqual({ - status: cordova.callbackStatus.NO_RESULT, - message: "async" - }); - }); - }); -});