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 #21891 from mcjimenez/bug1026381
Browse files Browse the repository at this point in the history
Bug 1026381 - [SV] Do not modify the NFC setting if a valid SIM wasn't present at first run (r = jmcanterafonseca)
  • Loading branch information
Carmen Jiménez committed Jul 18, 2014
2 parents 92dd8cc + 7e953f1 commit 300c4b7
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 33 deletions.
3 changes: 2 additions & 1 deletion apps/operatorvariant/js/customizers/customizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var Customizer = (function(setting, resourceType, onerror) {
if (resourceType === 'data') {
self.set(value);
} else {
Resources.load(value, resourceType, self.set,
self.simPresentOnFirstBoot = event.detail.simPresentOnFirstBoot;
Resources.load(value, resourceType, self.set.bind(self),
function onerrorRetrieving(status) {
console.error('Customizer.js: Error retrieving the resource.');
onerror && onerror(status);
Expand Down
5 changes: 5 additions & 0 deletions apps/operatorvariant/js/customizers/nfc_customizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ var NfcCustomizer = (function() {
Customizer.call(this, 'nfc', 'json');

this.set = function(nfcParams) {
if (!this.simPresentOnFirstBoot) {
console.log('NfcCustomizer. No first RUN with configured SIM.');
return;
}

const NFC_SETTING = 'nfc.enabled';

var settings = navigator.mozSettings;
Expand Down
44 changes: 35 additions & 9 deletions apps/operatorvariant/js/operator_variant.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
'use strict';

var OperatorVariantManager = {
SETTING_FTU_SIM_PRESENT: 'ftu.simPresentOnFirstBoot',

// Settings 'ftu.simPresentOnFirstBoot' only had false value if the user
// powered the phone on without SIM
// In other case we would have true or undefined depending on what process was
// executed first, so our variable is true by default.
simPresentOnFirstBoot: true,

// This file is created during the BUILD process
CUSTOMIZATION_FILE: '/resources/customization.json',

Expand All @@ -29,14 +37,31 @@ var OperatorVariantManager = {
],

init: function ovm_init() {
window.navigator.mozSetMessageHandler('first-run-with-sim', msg => {
this.mcc_mnc = this.getMccMnc(msg.mcc, msg.mnc);
if (this.mcc_mnc) {
// Load the variant customizers and the variant JSON file.
LazyLoader.load(
this.CUSTOMIZERS,
this.loadVariantAndCustomize.bind(this)
);
navigator.mozSetMessageHandler('first-run-with-sim', (msg) => {
var self = this;
self.mcc_mnc = self.getMccMnc(msg.mcc, msg.mnc);
if (self.mcc_mnc) {
var settings = navigator.mozSettings;
if (!settings) {
console.log(
'Settings is not available. Cannot make the configuration changes');
return;
}
var req = settings.createLock().get(self.SETTING_FTU_SIM_PRESENT);

req.onsuccess = function osv_success(e) {
var simOnFirstBoot = req.result[self.SETTING_FTU_SIM_PRESENT];
self.simPresentOnFirstBoot = !simOnFirstBoot ||
req.result[self.SETTING_FTU_SIM_PRESENT] === self.mcc_mnc;
LazyLoader.load(
self.CUSTOMIZERS,
self.loadVariantAndCustomize.bind(self)
);
};

req.onerror = function osv_error(e) {
console.error('Error retrieving ftu.simPresentOnFirstBoot. ', e);
};
}
});
},
Expand Down Expand Up @@ -96,7 +121,8 @@ var OperatorVariantManager = {
var customizationEvent = new CustomEvent('customization', {
detail: {
setting: setting,
value: customizationSettings[setting]
value: customizationSettings[setting],
simPresentOnFirstBoot: this.simPresentOnFirstBoot
}
});
scheduleEvent(customizationEvent);
Expand Down
45 changes: 30 additions & 15 deletions apps/operatorvariant/test/unit/customizer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ suite(' Customizer > ', function() {

setup(function() {
customizer = new Customizer(eventName, resourceType);
customizer.set = function () {};
resourceLoaderSpy.reset();
});

Expand All @@ -47,16 +48,20 @@ suite(' Customizer > ', function() {
// Init for adding the listener
customizer.init();
// Check that listener is working as expected
var customizationEvent = new CustomEvent('customization', {
var eventContent = {
detail: {
setting: eventName,
value: resourcePath
value: resourcePath,
simPresentOnFirstBoot: 'A Random Value'
}
});
};
var customizationEvent = new CustomEvent('customization', eventContent);
// Once we dispatch the event, handler should
// manage this properly
window.dispatchEvent(customizationEvent);
assert.isTrue(resourceLoaderSpy.calledOnce);
assert.equal(customizer.simPresentOnFirstBoot,
eventContent.detail.simPresentOnFirstBoot);
// A new event should not be handled, because
// we are removing the listener when the event
// is handled.
Expand All @@ -67,31 +72,46 @@ suite(' Customizer > ', function() {

suite(' set > ', function() {
test(' resource available ', function(done) {
var eventContent = {
detail: {
setting: eventName,
value: resourcePath,
simPresentOnFirstBoot: 'a value'
}
};

var onerror = function() {
assert.ok(false, 'Resource not loaded properly');
done();
};
var customizerSuccessful =
new Customizer(eventName, resourceType, onerror);
customizerSuccessful.set = function() {
assert.equal(customizerSuccessful.simPresentOnFirstBoot,
eventContent.detail.simPresentOnFirstBoot);
done();
};
// Init for adding the listener
customizerSuccessful.init();
// Check that listener is working as expected
var customizationEvent = new CustomEvent('customization', {
detail: {
setting: eventName,
value: resourcePath
}
});
var customizationEvent = new CustomEvent('customization', eventContent);
// Once we dispatch the event, handler should
// manage this properly
window.dispatchEvent(customizationEvent);
});

test(' resource unavailable ', function(done) {
var eventContent = {
detail: {
setting: eventName,
value: 'wrong/path/file.jpg',
simPresentOnFirstBoot: 'Another value'
}
};

var customizerError = new Customizer(eventName, resourceType, function() {
assert.equal(customizerError.simPresentOnFirstBoot,
eventContent.detail.simPresentOnFirstBoot);
done();
});
customizerError.set = function() {
Expand All @@ -101,12 +121,7 @@ suite(' Customizer > ', function() {
// Init for adding the listener
customizerError.init();
// Check that listener is working as expected
var customizationEvent = new CustomEvent('customization', {
detail: {
setting: eventName,
value: 'wrong/path/file.jpg'
}
});
var customizationEvent = new CustomEvent('customization', eventContent);
// Once we dispatch the event, handler should
// manage this properly
window.dispatchEvent(customizationEvent);
Expand Down
37 changes: 31 additions & 6 deletions apps/operatorvariant/test/unit/nfc_customizer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ suite('NFC customizer >', function() {
'default': false
},
currentValue: false,
expectedValue: true
expectedValue: true,
simPresentOnFirstBoot: true
},
{
title: 'set false value. It has not changed previously > ',
Expand All @@ -30,7 +31,8 @@ suite('NFC customizer >', function() {
'default': false
},
currentValue: false,
expectedValue: false
expectedValue: false,
simPresentOnFirstBoot: true
},
{
title: 'set true value. It has changed previously > ',
Expand All @@ -39,7 +41,8 @@ suite('NFC customizer >', function() {
'default': false
},
currentValue: true,
expectedValue: true
expectedValue: true,
simPresentOnFirstBoot: true
},
{
title: 'set false value. It has changed previously > ',
Expand All @@ -48,7 +51,8 @@ suite('NFC customizer >', function() {
'default': false
},
currentValue: true,
expectedValue: true
expectedValue: true,
simPresentOnFirstBoot: true
},
{
title: 'set true value. Previously value undefined > ',
Expand All @@ -57,7 +61,8 @@ suite('NFC customizer >', function() {
'default': false
},
currentValue: undefined,
expectedValue: true
expectedValue: true,
simPresentOnFirstBoot: true
},
{
title: 'set false value. Previous value undefined > ',
Expand All @@ -66,7 +71,26 @@ suite('NFC customizer >', function() {
'default': false
},
currentValue: undefined,
expectedValue: false
expectedValue: false,
simPresentOnFirstBoot: true
},
{
title: 'set true value. Previous RUN with no SIM (or unconfigured one)> ',
inputValue: {
'isEnabled': true,
'default': false
},
expectedValue: undefined,
simPresentOnFirstBoot: false
},
{
title: 'set false value. Previous RUN with no SIM (or unconfigured one)>',
inputValue: {
'isEnabled': false,
'default': false
},
expectedValue: undefined,
simPresentOnFirstBoot: false
}
];

Expand Down Expand Up @@ -94,6 +118,7 @@ suite('NFC customizer >', function() {
window.MockNavigatorSettings.mSettings[NFC_SETTING] =
testCase.currentValue;
}
nfcCustomizer.simPresentOnFirstBoot = testCase.simPresentOnFirstBoot;
nfcCustomizer.set(testCase.inputValue);
this.sinon.clock.tick(TINY_TIMEOUT);
var mSettings = window.MockNavigatorSettings.mSettings;
Expand Down
16 changes: 14 additions & 2 deletions apps/operatorvariant/test/unit/operator_variant_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ suite(' OperatorVariantManager > ', function() {
}
};
const MSG_NAME = 'first-run-with-sim';
const FIRST_RUN_SETTING = 'ftu.simPresentOnFirstBoot';
var message = {
mcc: '214',
mnc: '007'
Expand Down Expand Up @@ -85,16 +86,27 @@ suite(' OperatorVariantManager > ', function() {
var eventsDispatched = {};
var eventsToDispatch = customizationList[TEST_MCC_MNC];

// Run the initialization with the setting set to true, the expected value
// is true
window.MockNavigatorSettings.mSettings[FIRST_RUN_SETTING] = TEST_MCC_MNC;
navigator.mozSetMessageHandler.mTrigger(MSG_NAME, message);
assert.isTrue(OperatorVariantManager.simPresentOnFirstBoot,
'The simPresentOnFirstBoot value should be true');

window.addEventListener('customization', function customizer(event) {
var setting = event.detail.setting;
var value = event.detail.value;
// Check that the value is the expected
assert.equal(value, eventsToDispatch[setting]);

// Check that we got true as ftu.simPresentOnFirstBoot on the event.
assert.isTrue(event.detail.simPresentOnFirstBoot,
'event simPresentOnFirstBoot should be true');

// Check that we have only one event dispatched per setting
if (eventsDispatched[setting]) {
assert.ok(false, 'The settting ' + setting +
' was dispatched several times.');
assert.ok(false, 'The setting ' + setting +
' was dispatched several times.');
done();
return;
}
Expand Down

0 comments on commit 300c4b7

Please sign in to comment.