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

Commit

Permalink
Bug 937554 -[Cost Control] Query for the data of the current *data SI…
Browse files Browse the repository at this point in the history
…M* accordingly to settings
  • Loading branch information
gitmai committed Dec 2, 2013
1 parent 4f36cf1 commit 6e2b106
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 15 deletions.
3 changes: 3 additions & 0 deletions apps/costcontrol/js/app.js
Expand Up @@ -374,6 +374,9 @@ var CostControlApp = (function() {

return {
init: function() {
SettingsListener.observe('ril.data.defaultServiceId', 0, function() {
Common.loadDataSIMIccId(updateUI);
});
Common.waitForDOMAndMessageHandler(window, onReady);
},
afterFTU: function(cb) {
Expand Down
68 changes: 65 additions & 3 deletions apps/costcontrol/js/common.js
Expand Up @@ -68,7 +68,7 @@ function updateNextReset(trackingPeriod, value, callback) {
function resetData(mode, onsuccess, onerror) {

// Get all availabe Interfaces
var currentSimcardInterface = Common.getCurrentSIMInterface();
var currentSimcardInterface = Common.getDataSIMInterface();
var wifiInterface = Common.getWifiInterface();

// Ask reset for all available Interfaces
Expand Down Expand Up @@ -194,8 +194,12 @@ var Common = {

allNetworkInterfaces: {},

dataSimIccId: null,

allNetworkInterfaceLoaded: false,

dataSimIccIdLoaded: false,

isValidICCID: function(iccid) {
return typeof iccid === 'string' && iccid.length;
},
Expand Down Expand Up @@ -319,9 +323,13 @@ var Common = {
}
},

getCurrentSIMInterface: function _getCurrentSIMInterface() {
var iccId = IccHelper.iccInfo ? IccHelper.iccInfo.iccid : null;
getDataSIMInterface: function _getDataSIMInterface() {
if (!this.dataSimIccIdLoaded) {
console.warn('Data simcard is not ready yet');
return;
}

var iccId = this.dataSimIccId;
if (iccId) {
var findCurrentInterface = function(networkInterface) {
if (networkInterface.id === iccId) {
Expand Down Expand Up @@ -359,5 +367,59 @@ var Common = {
onerror();
}
};
},

loadDataSIMIccId: function _loadDataSIMIccId(onsuccess, onerror) {
var settings = navigator.mozSettings,
mobileConnections = navigator.mozMobileConnections,
dataSlotId = 0;
var self = this;
var req = settings &&
settings.createLock().get('ril.data.defaultServiceId');

req.onsuccess = function _onsuccesSlotId() {
dataSlotId = req.result['ril.data.defaultServiceId'] || 0;
var mobileConnection = mobileConnections[dataSlotId];
var iccId = mobileConnection.iccId || null;
if (!iccId) {
console.error('The slot ' + dataSlotId +
', configured as the data slot, is empty');
if (onerror) {
onerror();
}
return;
}
self.dataSimIccId = iccId;
self.dataSimIccIdLoaded = true;
if (onsuccess) {
onsuccess();
}
};

req.onerror = function _onerrorSlotId() {
console.warn('ril.data.defaultServiceId does not exists');
var iccId = null;

// Load the fist slot with iccId
for (var i = 0; i < mobileConnections.length && !iccId; i++) {
if (mobileConnections[i]) {
iccId = mobileConnections[i].iccId;
}
}
if (!iccId) {
console.error('No SIM in the device');
if (onerror) {
onerror();
}
return;
}

self.dataSimIccId = iccId;
self.dataSimIccIdLoaded = true;

if (onsuccess) {
onsuccess();
}
};
}
};
10 changes: 8 additions & 2 deletions apps/costcontrol/js/costcontrol.js
Expand Up @@ -396,7 +396,7 @@ var CostControl = (function() {
}

var wifiInterface = Common.getWifiInterface();
var currentSimcardNetwork = Common.getCurrentSIMInterface();
var currentSimcardNetwork = Common.getDataSIMInterface();

var simRequest, wifiRequest;
var pendingRequests = 0;
Expand Down Expand Up @@ -492,6 +492,12 @@ var CostControl = (function() {
}
return [output, accum];
}
// XXX: See bug 944342 - [Cost control] move all the process related to the
// network and data interfaces loading to the start-up process of CostControl
function getInterfacesInformation() {
Common.loadNetworkInterfaces();
Common.loadDataSIMIccId();
}

var airplaneMode = false;
function init() {
Expand All @@ -500,7 +506,7 @@ var CostControl = (function() {
airplaneMode = value;
}
);
Common.loadNetworkInterfaces();
getInterfacesInformation();
}

return {
Expand Down
3 changes: 3 additions & 0 deletions apps/costcontrol/js/widget.js
Expand Up @@ -442,6 +442,9 @@ var Widget = (function() {

return {
init: function() {
SettingsListener.observe('ril.data.defaultServiceId', 0, function() {
Common.loadDataSIMIccId(updateUI.bind(null, true));
});
Common.waitForDOMAndMessageHandler(window, onReady);
document.getElementById('message-handler').src = 'message_handler.html';
}
Expand Down
122 changes: 119 additions & 3 deletions apps/costcontrol/test/unit/common_test.js
Expand Up @@ -8,12 +8,16 @@ requireApp('costcontrol/test/unit/mock_moz_network_stats.js');
requireApp('costcontrol/test/unit/mock_all_network_interfaces.js');
requireApp('costcontrol/test/unit/mock_config_manager.js');
requireApp('costcontrol/js/utils/toolkit.js');
requireApp('system/shared/test/unit/mocks/mock_navigator_moz_settings.js');
requireApp('system/shared/test/unit/mocks/mock_navigator_moz_mobile_connections.js');

var realIccHelper,
realMozL10n,
realMozNetworkStats,
realNetworkstatsProxy,
realConfigManager;
realConfigManager,
realMozSettings,
realMozMobileConnections;

if (!this.IccHelper) {
this.IccHelper = null;
Expand All @@ -31,6 +35,14 @@ if (!this.navigator.mozNetworkStats) {
this.navigator.mozNetworkStats = null;
}

if (!this.navigator.mozMobileConnections) {
this.navigator.mozMobileConnections = null;
}

if (!this.navigator.mozSettings) {
this.navigator.mozSettings = null;
}

if (!this.NetworkstatsProxy) {
this.NetworkstatsProxy = null;
}
Expand All @@ -48,9 +60,15 @@ suite('Cost Control Common >', function() {
realMozNetworkStats = window.navigator.mozNetworkStats;
navigator.mozNetworkStats = MockMozNetworkStats;

realMozMobileConnections = navigator.mozMobileConnections;
navigator.mozMobileConnections = MockNavigatorMozMobileConnections;

realNetworkstatsProxy = window.NetworkstatsProxy;
window.NetworkstatsProxy = MockMozNetworkStats;

realMozSettings = navigator.mozSettings;
navigator.mozSettings = MockNavigatorSettings;

realConfigManager = window.ConfigManager;

});
Expand All @@ -61,7 +79,8 @@ suite('Cost Control Common >', function() {
window.navigator.mozL10n = realMozL10n;
window.navigator.mozNetworkStats = realMozNetworkStats;
window.NetworkstatsProxy = realNetworkstatsProxy;

window.navigator.mozSettings = realMozSettings;
window.navigator.mozMobileConnections = realMozMobileConnections;
});

function getCustomClearStats(willFail) {
Expand All @@ -88,6 +107,27 @@ suite('Cost Control Common >', function() {
return getCustomClearStats(true);
}

function createLockRequestFails() {
return function() {
return {
set: null,
get: function() {
var request = {};
setTimeout(function() {
request.error = { name: 'error' };
request.onerror && request.onerror();
}, 0);
return request;
}
};
};
};

setup(function() {
Common.dataSimIccIdLoaded = false;
Common.dataSimIccId = null;
});

test('isValidICCID', function() {
assert.isTrue(!Common.isValidICCID());
assert.isTrue(!Common.isValidICCID(null));
Expand All @@ -97,7 +137,6 @@ suite('Cost Control Common >', function() {
});

test('loadNetworkInterfaces correctly', function(done) {

Common.loadNetworkInterfaces(
function() {
assert.isTrue(Common.allNetworkInterfaceLoaded);
Expand All @@ -112,7 +151,84 @@ suite('Cost Control Common >', function() {
);
});

test('loadIccDataSIM() works ok without settings', function(done) {
MockNavigatorMozMobileConnections[0] = {
iccId: MockAllNetworkInterfaces[1].id
};
Common.loadDataSIMIccId(
function() {
assert.isTrue(Common.dataSimIccIdLoaded);
assert.equal(Common.dataSimIccId,
MockAllNetworkInterfaces[1].id);
done();
}
);
});

test('loadIccDataSIM() fails noICC', function(done) {
MockNavigatorSettings.mSettings['ril.data.defaultServiceId'] = 0;
MockNavigatorMozMobileConnections[0] = {
iccId: null
};
Common.loadDataSIMIccId(function() { },
function _onError() {
assert.isFalse(Common.dataSimIccIdLoaded);
assert.isNull(Common.dataSimIccId);
done();
}
);
});

test('loadIccDataSIM() works correctly', function(done) {
MockNavigatorSettings.mSettings['ril.data.defaultServiceId'] = 0;
MockNavigatorMozMobileConnections[0] = {
iccId: Common.allNetworkInterfaces[1].id
};
Common.loadDataSIMIccId(
function() {
assert.isTrue(Common.dataSimIccIdLoaded);
assert.equal(Common.dataSimIccId,
Common.allNetworkInterfaces[1].id);
done();
}
);
});

test('loadIccDataSIM() works ok when settings request fails', function(done) {
sinon.stub(navigator.mozSettings, 'createLock', createLockRequestFails());
MockNavigatorMozMobileConnections[0] = {
iccId: MockAllNetworkInterfaces[1].id
};
Common.loadDataSIMIccId(function _onSuccess() {
assert.isTrue(Common.dataSimIccIdLoaded);
assert.equal(Common.dataSimIccId,
MockAllNetworkInterfaces[1].id);
navigator.mozSettings.createLock.restore();
done();
});
});

test('loadIccDataSIM() all fails', function(done) {
sinon.stub(navigator.mozSettings, 'createLock', createLockRequestFails());
MockNavigatorMozMobileConnections[0] = {
iccId: null
};

Common.loadDataSIMIccId(function() { },
function _onError() {
assert.isFalse(Common.dataSimIccIdLoaded);
navigator.mozSettings.createLock.restore();
done();
}
);
});

suite('Reset Data>', function() {
MockNavigatorSettings.mSettings['ril.data.defaultServiceId'] = 0;
MockNavigatorMozMobileConnections[0] = {
iccId: Common.allNetworkInterfaces[1].id
};

suiteSetup(function() {
sinon.stub(MockMozNetworkStats, 'clearStats', getSuccessfullClearStats());
});
Expand Down
8 changes: 4 additions & 4 deletions apps/costcontrol/test/unit/cost_control_test.js
Expand Up @@ -167,15 +167,15 @@ suite('Cost Control Service Hub Suite >', function() {
test(
'Get dataUsage without simcard interface',
function(done) {
sinon.stub(Common, 'getCurrentSIMInterface').returns(undefined);
sinon.stub(Common, 'getDataSIMInterface').returns(undefined);

CostControl.init();
CostControl.getInstance(function(service) {
service.request({type: 'datausage'}, function(result) {
assert.equal(result.status, 'success');
assert.equal(result.data.wifi.total, 112123944);
assert.equal(result.data.mobile.total, 0);
Common.getCurrentSIMInterface.restore();
Common.getDataSIMInterface.restore();
done();
});
});
Expand All @@ -185,7 +185,7 @@ suite('Cost Control Service Hub Suite >', function() {
test(
'Get dataUsage without network interfaces',
function(done) {
sinon.stub(Common, 'getCurrentSIMInterface').returns(undefined);
sinon.stub(Common, 'getDataSIMInterface').returns(undefined);
sinon.stub(Common, 'getWifiInterface').returns(undefined);

CostControl.init();
Expand All @@ -195,7 +195,7 @@ suite('Cost Control Service Hub Suite >', function() {
assert.equal(result.data.wifi.total, 0);
assert.equal(result.data.mobile.total, 0);

Common.getCurrentSIMInterface.restore();
Common.getDataSIMInterface.restore();
Common.getWifiInterface.restore();
done();
});
Expand Down
14 changes: 11 additions & 3 deletions apps/costcontrol/test/unit/mock_common.js
Expand Up @@ -19,6 +19,7 @@ var MockCommon = function(config) {
return {
COST_CONTROL_APP: 'app://costcontrol.gaiamobile.org',
allNetworkInterfaces: {},
dataSimIccId: null,
isValidICCID: function(iccid) {
assert.isDefined(
config.isValidICCID,
Expand Down Expand Up @@ -49,9 +50,9 @@ var MockCommon = function(config) {
window.dispatchEvent(event);
console.log('Alert: ' + msg);
},
getCurrentSIMInterface: function getCurrentSIMInterface() {
var currentSimCard = fakeAllInterfaces[1];
return currentSimCard;
getDataSIMInterface: function getDataSIMInterface() {
var dataSimCard = fakeAllInterfaces[1];
return dataSimCard;
},
getWifiInterface: function() {
var wifiInterface = fakeAllInterfaces[0];
Expand All @@ -63,6 +64,13 @@ var MockCommon = function(config) {
setTimeout(function() {
self.allNetworkInterfaces = fakeAllInterfaces;
}, 0);
},
loadDataSIMIccId: function() {
var self = this;

setTimeout(function() {
self.dataSimIccId = fakeAllInterfaces[1].id;
}, 0);
}
};
};

0 comments on commit 6e2b106

Please sign in to comment.