Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #14307 from crdlc/bug-945668
Browse files Browse the repository at this point in the history
Bug 945668 - [Download Manager] Adapt shared components to Download API
  • Loading branch information
Cristian Rodriguez committed Dec 3, 2013
2 parents a45dd9f + 3d9a38a commit 7fa083c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 58 deletions.
44 changes: 10 additions & 34 deletions apps/settings/test/unit/download_formatter_test.js
Expand Up @@ -38,15 +38,19 @@ suite('DownloadFormatter', function() {
});

test(' getPercentage', function() {
var percentage = DownloadFormatter.getFormattedPercentage(51.1, 100);
assert.equal(percentage, 51.10);
var mockDownload = new MockDownload({
totalBytes: 1024 * 100,
currentBytes: 1024 * 50
});
var percentage = DownloadFormatter.getPercentage(mockDownload);
assert.equal(percentage, 50);
});

test(' getFileName', function() {
var url = 'http://firefoxos.com/file/nameFile.mp3';
var path = '/mnt/sdcard/nameFile.mp3';
var mockDownload = new MockDownload(
{
url: url
path: path
}
);
assert.equal(DownloadFormatter.getFileName(mockDownload), 'nameFile.mp3');
Expand Down Expand Up @@ -172,42 +176,14 @@ suite('DownloadFormatter', function() {
assert.equal(params.unit, 'byteUnit-TB');
});


test(' getDownloadedPercentage without decimals', function() {
var total = 1024 * 1024 * 1024; // 1 GB
var currently = total * 0.5; // 0.5 GB or 50 %
var mockDownload = new MockDownload(
{
totalBytes: total,
currentBytes: currently
}
);
var percentage = DownloadFormatter.getDownloadedPercentage(mockDownload);
assert.equal(percentage, '50');
});

test(' getDownloadedPercentage with decimals', function() {
var total = 1024 * 1024 * 1024; // 1 GB
var currently = total * 0.611; // 0.611 GB or 61.10 %
var mockDownload = new MockDownload(
{
totalBytes: total,
currentBytes: currently
}
);
var percentage = DownloadFormatter.getDownloadedPercentage(mockDownload);
assert.equal(percentage, '61.10');
});

test(' getUUID', function() {
var now = new Date();
var expectedUUID = 'download-69';
var mockDownload = new MockDownload(
{
url: 'http://firefoxos.com/fichero.mp4',
startTime: now
id: expectedUUID
}
);
var expectedUUID = 'fichero.mp4' + now.getTime();
var retrievedUUID = DownloadFormatter.getUUID(mockDownload);
assert.equal(retrievedUUID, expectedUUID);
});
Expand Down
39 changes: 23 additions & 16 deletions shared/js/download/download_formatter.js
Expand Up @@ -27,6 +27,11 @@
var _ = navigator.mozL10n.get;
var index = parseInt(Math.round(_log1024(bytes)));
var value = (bytes / Math.pow(1024, index)).toFixed(NUMBER_OF_DECIMALS);
// Special case, otherwise will be infinite
if (bytes == 0) {
index = 0;
value = 0;
}

return _('fileSize', {
size: value,
Expand All @@ -35,24 +40,23 @@
}

function _calcPercentage(currently, total) {
var percentage = (100 * currently) / total;
var noPrecisionNeeded = !((100 * currently) % total);
return !noPrecisionNeeded ?
percentage.toFixed(NUMBER_OF_DECIMALS) : percentage;
if (total == 0) {
return 0;
}

return parseInt((100 * currently) / total);
}


var DownloadFormatter = {
getFormattedSize: function(bytes) {
return _getFormattedSize(bytes);
},
getFormattedPercentage: function(partial, total) {
return _calcPercentage(partial, total);
getPercentage: function(download) {
return _calcPercentage(download.currentBytes, download.totalBytes);
},
getFileName: function(download) {
var tmpAnchorElement = document.createElement('a');
tmpAnchorElement.href = download.url;
return tmpAnchorElement.pathname.split('/').pop(); // filename.php
return download.path.split('/').pop(); // filename.ext
},
getTotalSize: function(download) {
var bytes = download.totalBytes;
Expand All @@ -62,20 +66,23 @@
var bytes = download.currentBytes;
return _getFormattedSize(bytes);
},
getDownloadedPercentage: function(download) {
var totalBytes = download.totalBytes;
var downloadedBytes = download.currentBytes;
return _calcPercentage(downloadedBytes, totalBytes);
},
getDate: function(download, callback) {
var date = download.startTime;
var date;

try {
date = download.startTime;
} catch (ex) {
date = new Date();
console.error(ex);
}

LazyLoader.load(['shared/js/l10n_date.js'], function onload() {
var prettyDate = navigator.mozL10n.DateTimeFormat().fromNow(date, true);
callback && callback(prettyDate);
});
},
getUUID: function(download) {
return this.getFileName(download) + download.startTime.getTime();
return download.id || this.getFileName(download);
}
};

Expand Down
4 changes: 1 addition & 3 deletions shared/js/download/download_launcher.js
Expand Up @@ -169,10 +169,8 @@ var DownloadLauncher = (function() {
var req = new Request();

window.setTimeout(function launching() {
// TODO: Maybe the API should express that the download finished
// correctly, like 'finished' instead of 'stopped'
var state = download.state;
if (state === 'done') {
if (state === 'succeeded') {
LazyLoader.load(['shared/js/mime_mapper.js',
'shared/js/download/download_formatter.js'],
function loaded() {
Expand Down
10 changes: 6 additions & 4 deletions shared/js/download/download_store.js
Expand Up @@ -127,7 +127,9 @@ var DownloadStore = (function() {
navigator.getDataStores(DATASTORE_NAME).then(function(ds) {
if (ds.length < 1) {
console.error('Download Store: Cannot get access to the DataStore');
fail(e);
fail({
message: 'Download Store: Cannot get access to the DataStore'
});
return;
}

Expand Down Expand Up @@ -214,7 +216,7 @@ var DownloadStore = (function() {
var req = new Request();

window.setTimeout(function() {
init(doAdd.bind(null, download, req), req.failed);
init(doAdd.bind(null, download, req), req.failed.bind(req));
});

return req;
Expand All @@ -234,7 +236,7 @@ var DownloadStore = (function() {
var req = new Request();

window.setTimeout(function() {
init(doGetAll.bind(null, req), req.failed);
init(doGetAll.bind(null, req), req.failed.bind(req));
});

return req;
Expand Down Expand Up @@ -262,7 +264,7 @@ var DownloadStore = (function() {
var req = new Request();

window.setTimeout(function() {
init(doRemove.bind(null, download.id, req), req.failed);
init(doRemove.bind(null, download.id, req), req.failed.bind(req));
});

return req;
Expand Down
10 changes: 10 additions & 0 deletions shared/js/download/download_ui.js
Expand Up @@ -179,6 +179,16 @@ var DownloadUI = (function() {
if (!ignoreStyles) {
libs.push.apply(libs, styleSheets);
}

// We have to discover the type of UI depending on state when type is null
if (type === null) {
type = TYPES.STOPPED;

if (download.state === 'finalized') {
type = TYPES.FAILED;
}
}

LazyLoader.load(libs, createConfirm.call(this, type, req, download));
}, 0);

Expand Down
5 changes: 4 additions & 1 deletion shared/test/unit/mocks/mock_download.js
Expand Up @@ -8,17 +8,20 @@ var DEFAULT_PARAMS = {
path: '//SDCARD/Downloads/archivo.mp3',
state: 'downloading',
contentType: 'audio/mpeg',
startTime: new Date()
startTime: new Date(),
id: 1
};

function MockDownload(params) {
params = params || {};
this.totalBytes = params.totalBytes || DEFAULT_PARAMS.totalBytes;
this.currentBytes = params.currentBytes || DEFAULT_PARAMS.currentBytes;
this.url = params.url || DEFAULT_PARAMS.url;
this.path = params.path || DEFAULT_PARAMS.path;
this.state = params.state || DEFAULT_PARAMS.state;
this.contentType = params.contentType || DEFAULT_PARAMS.contentType;
this.startTime = params.startTime || DEFAULT_PARAMS.startTime;
this.id = params.id || DEFAULT_PARAMS.id;
}

MockAttachment.prototype = {
Expand Down

0 comments on commit 7fa083c

Please sign in to comment.