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 #12656 from arcturus/bug-914191
Browse files Browse the repository at this point in the history
Bug 914191 - [Contacts] Import contacts from SD card make phone not enter sleep.
  • Loading branch information
arcturus committed Oct 3, 2013
2 parents 79cc970 + c675fdb commit 12d41a0
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 14 deletions.
25 changes: 16 additions & 9 deletions apps/communications/contacts/js/views/settings.js
Expand Up @@ -641,13 +641,13 @@ contacts.Settings = (function() {
}
};
Contacts.confirmDialog(null, _('simContacts-error'), cancel, retry);
Contacts.hideOverlay();
resetWait(wakeLock);
};

importer.start();
};

var onSdImport = function onSdImport() {
var onSdImport = function onSdImport(cb) {
var cancelled = false;
var importer = null;
var progress = Contacts.showOverlay(
Expand All @@ -670,27 +670,27 @@ contacts.Settings = (function() {
'text/directory'
], ['vcf', 'vcard'], function(err, fileArray) {
if (err)
return import_error(err);
return import_error(err, cb);

if (cancelled)
return;

if (fileArray.length)
utils.sdcard.getTextFromFiles(fileArray, '', onFiles);
else
import_error('No contacts were found.');
import_error('No contacts were found.', cb);
});

function onFiles(err, text) {
if (err)
return import_error(err);
return import_error(err, cb);

if (cancelled)
return;

importer = new VCFReader(text);
if (!text || !importer)
return import_error('No contacts were found.');
return import_error('No contacts were found.', cb);

importer.onread = import_read;
importer.onimported = imported_contact;
Expand All @@ -707,6 +707,9 @@ contacts.Settings = (function() {
Contacts.showStatus(_('memoryCardContacts-imported3', {
n: importedContacts
}));
if (typeof cb === 'function') {
cb();
}
}
});
}, DELAY_FEEDBACK);
Expand All @@ -724,7 +727,7 @@ contacts.Settings = (function() {
progress.update();
}

function import_error(e) {
function import_error(e, cb) {
var cancel = {
title: _('cancel'),
callback: function() {
Expand All @@ -743,7 +746,10 @@ contacts.Settings = (function() {
};
Contacts.confirmDialog(null, _('memoryCardContacts-error'), cancel,
retry);
Contacts.hideOverlay();
resetWait(wakeLock);
if (typeof cb === 'function') {
cb();
}
}
};

Expand Down Expand Up @@ -906,6 +912,7 @@ contacts.Settings = (function() {
'onLineChanged': checkOnline,
'cardStateChanged': checkSIMCard,
'updateTimestamps': updateTimestamps,
'navigation': navigationHandler
'navigation': navigationHandler,
'importFromSDCard': onSdImport
};
})();
5 changes: 4 additions & 1 deletion apps/communications/contacts/test/unit/mock_asyncstorage.js
Expand Up @@ -11,8 +11,11 @@ var MockasyncStorage = {
cb(this.keys[key]);
},

setItem: function(key, value) {
setItem: function(key, value, cb) {
this.keys[key] = value;
if (typeof cb === 'function') {
cb();
}
},

removeItem: function(key) {
Expand Down
11 changes: 10 additions & 1 deletion apps/communications/contacts/test/unit/mock_contacts.js
Expand Up @@ -59,5 +59,14 @@ var MockContacts = {
},
view: function(view, callback) {
callback();
}
},
showOverlay: function(title, id) {
return {
'setClass': function(clazz) {},
'setHeaderMsg': function(msg) {},
'setTotal': function(total) {},
'update': function() {}
};
},
showStatus: function(status) {}
};
5 changes: 3 additions & 2 deletions apps/communications/contacts/test/unit/mock_sdcard.js
Expand Up @@ -22,7 +22,8 @@ var mock_sdcard_vcf = 'BEGIN:VCARD\n' +
var MockSdCard = {
NOT_INITIALIZED: 0,
NOT_AVAILABLE: 1,
AVAILABLE: 2
AVAILABLE: 2,
failOnRetrieveFiles: false
};

MockSdCard.status = MockSdCard.AVAILABLE;
Expand Down Expand Up @@ -61,7 +62,7 @@ MockSdCard.checkStorageCard = function sd_checkStorageCard() {
};

MockSdCard.retrieveFiles = function retrieveFilesContent(mimes, exts, cb) {
cb(null, [
cb(MockSdCard.failOnRetrieveFiles, [
{
name: 'vcf1.vcf',
type: 'text/vcard'
Expand Down
19 changes: 19 additions & 0 deletions apps/communications/contacts/test/unit/mock_wakelock.js
@@ -0,0 +1,19 @@
'use strict';

var MyLocks = {};

var MockWakeLock = function(type) {
if (MyLocks[type] == true) {
throw Exception('Already locked');
}

MyLocks[type] = true;

var lock = {
'unlock': function() {
MyLocks[type] = false;
}
};

return lock;
};
40 changes: 39 additions & 1 deletion apps/communications/contacts/test/unit/views/settings_test.js
Expand Up @@ -9,6 +9,7 @@ requireApp('communications/contacts/test/unit/mock_icc_helper.js');
requireApp('communications/dialer/test/unit/mock_confirm_dialog.js');
requireApp('communications/contacts/test/unit/mock_vcard_parser.js');
requireApp('communications/contacts/test/unit/mock_mozContacts.js');
requireApp('communications/contacts/test/unit/mock_wakelock.js');
requireApp('communications/contacts/js/import_utils.js');
requireApp('communications/contacts/js/navigation.js');
requireApp('communications/contacts/js/views/settings.js');
Expand All @@ -32,7 +33,8 @@ if (!this.realMozContacts) {
}

var mocksHelperForContactSettings = new MocksHelper([
'Contacts', 'asyncStorage', 'fb', 'ConfirmDialog', 'VCFReader', 'IccHelper'
'Contacts', 'asyncStorage', 'fb', 'ConfirmDialog', 'VCFReader', 'IccHelper',
'WakeLock'
]);
mocksHelperForContactSettings.init();

Expand Down Expand Up @@ -77,6 +79,7 @@ suite('Contacts settings', function() {
showMenu: function() {}
};
window._ = stub('blah');

});

suiteTeardown(function() {
Expand Down Expand Up @@ -116,10 +119,19 @@ suite('Contacts settings', function() {
});

suite('SD Card import', function() {
var showMenuSpy;
var showStatusSpy;
var realWakeLock;

setup(function() {
contacts.Settings.init();
checkForCard = utils.sdcard.checkStorageCard;
mocksHelper.setup();

showMenuSpy = sinon.spy(window.utils.overlay, 'showMenu');
showStatusSpy = sinon.spy(Contacts, 'showStatus');
realWakeLock = navigator.requestWakeLock;
navigator.requestWakeLock = MockWakeLock;
});

test('show SD Card import if SD card is present', function() {
Expand Down Expand Up @@ -147,10 +159,35 @@ suite('Contacts settings', function() {

});

test('SD Import went well', function(done) {
contacts.Settings.importFromSDCard(function onImported() {
sinon.assert.called(showMenuSpy);
sinon.assert.called(showStatusSpy);
assert.equal(false, MyLocks['cpu']);
done();
});
});

test('SD Import with error cause no files to import', function(done) {
// Simulate not finding any files
MockSdCard.failOnRetrieveFiles = true;
contacts.Settings.importFromSDCard(function onImported() {
sinon.assert.called(showMenuSpy);
sinon.assert.notCalled(showStatusSpy);
assert.equal(false, MyLocks['cpu']);
// Restore the mock
MockSdCard.failOnRetrieveFiles = false;
done();
});
});

teardown(function() {
utils.sdcard.checkStorageCard = checkForCard;
mocksHelper.teardown();
MockasyncStorage.clear();
showMenuSpy.restore();
showStatusSpy.restore();
navigator.requestWakeLock = realWakeLock;
});
});

Expand Down Expand Up @@ -210,6 +247,7 @@ suite('Contacts settings', function() {
checkForCard = utils.sdcard.checkStorageCard;
utils.sdcard.checkStorageCard = function() { return true; };
navigator.mozSettings = MockNavigatorSettings;

});

suiteTeardown(function() {
Expand Down

0 comments on commit 12d41a0

Please sign in to comment.