cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject [1/2] spec commit: [CB-3992] Test that FileWriter.write(file) supports binary I/O
Date Mon, 24 Jun 2013 18:43:57 GMT
Updated Branches:
  refs/heads/master bb0090287 -> 146e09380


[CB-3992] Test that FileWriter.write(file) supports binary I/O


Project: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/commit/146e0938
Tree: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/tree/146e0938
Diff: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/diff/146e0938

Branch: refs/heads/master
Commit: 146e09380e6f3f425f1e80b3d70558a8acca1662
Parents: 4e9b35e
Author: Ian Clelland <iclelland@chromium.org>
Authored: Mon Jun 24 14:40:25 2013 -0400
Committer: Ian Clelland <iclelland@chromium.org>
Committed: Mon Jun 24 14:43:07 2013 -0400

----------------------------------------------------------------------
 autotest/tests/file.tests.js | 81 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/146e0938/autotest/tests/file.tests.js
----------------------------------------------------------------------
diff --git a/autotest/tests/file.tests.js b/autotest/tests/file.tests.js
index 3176472..464cef2 100644
--- a/autotest/tests/file.tests.js
+++ b/autotest/tests/file.tests.js
@@ -3693,5 +3693,86 @@ describe('File API', function() {
             });
 
         });
+        it("file.spec.108 should be able to write binary data from a File", function() {
+            // Skip test if Blobs are not supported (e.g.: Android 2.3).
+            if (typeof window.Blob == 'undefined' || typeof window.ArrayBuffer == 'undefined')
{
+                return;
+            }
+            var dummyFileName = "blobwriter.bin",
+                outputFileName = 'verify.bin',
+                fail = createFail('FileWriter'),
+                // file content
+                data = new ArrayBuffer(32),
+                dataView = new Int8Array(data),
+                blob,
+                // for verifying file length
+                length = 32,
+                verifier = jasmine.createSpy("verifier").andCallFake(function(outputFileWriter)
{
+                    expect(outputFileWriter.length).toBe(length);
+                    expect(outputFileWriter.position).toBe(length);
+
+                    // cleanup
+                    deleteFile(fileName);
+                }),
+                writeFile = function(fileName, fileData, win) {
+                    var theWriter,
+                        filePath = root.fullPath + '/' + fileName,
+                        // writes file content to new file
+                        write_file = function(fileEntry) {
+                            writerEntry = fileEntry;
+                            fileEntry.createWriter(function(writer) {
+                                theWriter = writer;
+                                writer.onwriteend = function(ev) {
+                                    if (typeof fileData.length !== "undefined") {
+                                        expect(theWriter.length).toBe(fileData.length);
+                                        expect(theWriter.position).toBe(fileData.length);
+                                    }
+                                    win(theWriter);
+                                }
+                                writer.onerror = fail;
+                                writer.write(fileData);
+                            }, fail);
+                        };
+                    createFile(fileName, write_file, fail);
+                },
+
+                openFile = function(fileName, callback) {
+                    root.getFile(fileName, {create: false}, function(fileEntry) {
+                        fileEntry.file(callback, fail);
+                    }, fail);
+                };
+
+            for (i=0; i < dataView.length; i++) {
+                dataView[i] = i;
+            }
+            try {
+                // Mobile Safari: Use Blob constructor
+                blob = new Blob([data], {"type": "application/octet-stream"})
+            } catch(e) {
+                if (window.WebKitBlobBuilder) {
+                    // Android Browser: Use deprecated BlobBuilder
+                    var builder = new WebKitBlobBuilder()
+                    builder.append(data)
+                    blob = builder.getBlob('application/octet-stream');
+                } else {
+                    // We have no way defined to create a Blob, so fail
+                    fail();
+                }
+            }
+
+            runs(function() {
+                writeFile(dummyFileName, blob, function(dummyFileWriter) {
+                    openFile(dummyFileName, function(file) {
+                        writeFile(outputFileName, file, verifier);
+                    });
+                });
+            });
+            waitsFor(function() { return (verifier.wasCalled || fail.wasCalled); }, "callbacks
never called", Tests.TEST_TIMEOUT);
+
+            runs(function() {
+                expect(verifier).toHaveBeenCalled();
+                expect(fail).not.toHaveBeenCalled();
+            });
+        });
     });
 });


Mime
View raw message