[Filetransfer] Fixed performance of strict Blob object passing 78/247778/3
authorPiotr Kosko <p.kosko@samsung.com>
Mon, 16 Nov 2020 08:33:10 +0000 (09:33 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Mon, 16 Nov 2020 11:14:23 +0000 (12:14 +0100)
commite6250f7b56a12370ca9b93576d14979fc16c118e
treee0719bbbaf3cfb8a8efdd49fe7e2ce8027112293
parent82bb6f3f6c4bd016ea4e28e9cd6f8855cdb46ee3
[Filetransfer] Fixed performance of strict Blob object passing

[Bug] Because of need to pass strict Blob object to FormData since
Chromium M76, there is a need to increase a performance to pass
cordova tests - finish test in timeout.

Because of that, xml request creation was moved before reading a file
to make possible to trigger abort callback, even when the file is not
fully loaded to memory yet.

[Verification] Below code (based on failing test) produces times below 600ms.
E.g. TEST FINISHED: Time taken: 555

/// <testing precondition>, you need to have running http server accepting POST
/// requests running on SERVER ip address

var SERVER = "http://192.168.0.220:5000";

var persistentRoot;
var GRACE_TIME_DELTA = 600; // in milliseconds
var DEFAULT_FILESYSTEM_SIZE = 1024 * 50; //filesystem size in bytes
var ABORT_DELAY = 100; // for abort() tests

window.requestFileSystem(LocalFileSystem.PERSISTENT, DEFAULT_FILESYSTEM_SIZE,
    function (fileSystem) {
        persistentRoot = fileSystem.root;
        runTest();
    },
    function () {
        throw new Error('Failed to initialize persistent file system.');
    }
);

var unexpectedCallbacks = {
    httpFail: function () { },
    httpWin: function () { },
    fileSystemFail: function () { },
    fileSystemWin: function () { },
    fileOperationFail: function () { },
    fileOperationWin: function () { },
};

var writeFile = function (fileSystem, name, content, success) {
    fileSystem.getFile(name, { create: true },
        function (fileEntry) {
            fileEntry.createWriter(function (writer) {

                writer.onwrite = function () {
                    success(fileEntry);
                };

                writer.onabort = function (evt) {
                    throw new Error('aborted creating test file \'' + name + '\': ' + evt);
                };

                writer.error = function (evt) {
                    throw new Error('aborted creating test file \'' + name + '\': ' + evt);
                };

                if (cordova.platformId === 'browser') {
                    // var builder = new BlobBuilder();
                    // builder.append(content + '\n');
                    var blob = new Blob([content + '\n'], { type: 'text/plain' });
                    writer.write(blob);
                } else {
                    writer.write(content + "\n");
                }

            }, unexpectedCallbacks.fileOperationFail);
        },
        function () {
            throw new Error('could not create test file \'' + name + '\'');
        }
    );
};

var runTest = function () {
    transfer = new FileTransfer();

    // assign onprogress handler
    var defaultOnProgressHandler = function (event) {
        if (event.lengthComputable) {
            expect(event.loaded).toBeGreaterThan(1);
            expect(event.total).toBeGreaterThan(0);
            expect(event.total).not.toBeLessThan(event.loaded);
            expect(event.lengthComputable).toBe(true, 'lengthComputable');
        } else {
            // In IE, when lengthComputable === false, event.total somehow is equal to 2^64
            if (isIE) {
                expect(event.total).toBe(Math.pow(2, 64));
            } else {
                expect(event.total).toBe(0);
            }
        }
    };

    transfer.onprogress = defaultOnProgressHandler;

    root = persistentRoot;
    fileName = 'testFile.txt';
    localFilePath = root.toURL() + fileName;

    uploadParams = {};
    uploadParams.value1 = "test";
    uploadParams.value2 = "param";

    uploadOptions = new FileUploadOptions();
    uploadOptions.fileKey = "file";
    uploadOptions.fileName = fileName;
    uploadOptions.mimeType = "text/plain";
    uploadOptions.params = uploadParams;

    var fileURL = SERVER + '';
    var startTime;

    var uploadFail = function (e) {
        console.log('uploadFail - ' + JSON.stringify(e));
        console.log('TEST FINISHED: Time taken: ' + (new Date() - startTime));
    };

    var fileWin = function () {
        console.log('fileWin');
        startTime = +new Date();

        transfer.onprogress = (s) => {
            console.log('upload on progress: ' + JSON.stringify(s) +
            'time taken: ' + (new Date() - startTime)) };
        console.log('Call upload, time taken: ' + (new Date() - startTime))
        transfer.upload(localFilePath, fileURL, (s) => {
            console.log('uploadWin!!! ' + JSON.stringify(s)); }, uploadFail, uploadOptions, true);
        console.log('After upload call, time taken: ' + (new Date() - startTime))
        setTimeout(function () {
            console.log('Call abort, time taken: ' + (new Date() - startTime))
            transfer.abort();
        }, ABORT_DELAY);
    };

    writeFile(root, fileName, new Array(100000).join('aborttest!'), fileWin);
}

Change-Id: I29717417009dc309456158529cebc85b904e31d3
src/lib/plugins/cordova-plugin-file-transfer/tizen/FileTransfer.js