From 57a2d3d649f083e0bed992e6d8766031ae5f4bbb Mon Sep 17 00:00:00 2001 From: Fernando Rodriguez Sela Date: Thu, 3 Apr 2014 12:45:20 +0200 Subject: [PATCH] Bug 989852 - [Sora][STK]'OK ' item is grayed when execute 'Get input' with a default text and user input command., r=vingtetun --- apps/system/js/icc.js | 3 + apps/system/js/icc_worker.js | 5 +- apps/system/test/unit/icc_test.js | 228 ++++++++++++++++++ apps/system/test/unit/icc_worker_test.js | 101 ++++++++ apps/system/test/unit/mock_system_icc.js | 36 +++ .../test/unit/mock_system_icc_worker.js | 116 +++++++++ shared/test/unit/mocks/mock_dump.js | 14 ++ 7 files changed, 500 insertions(+), 3 deletions(-) create mode 100644 apps/system/test/unit/icc_test.js create mode 100644 apps/system/test/unit/icc_worker_test.js create mode 100644 apps/system/test/unit/mock_system_icc.js create mode 100644 apps/system/test/unit/mock_system_icc_worker.js diff --git a/apps/system/js/icc.js b/apps/system/js/icc.js index 7fbafea49fb2..21059aeb24c7 100644 --- a/apps/system/js/icc.js +++ b/apps/system/js/icc.js @@ -445,6 +445,9 @@ var icc = { setTimeout(function workaround_bug818270() { self.icc_input_box.maxLength = options.maxLength; self.icc_input_box.value = options.defaultText || ''; + self.icc_input_btn.disabled = !checkInputLengthValid( + self.icc_input_box.value.length, options.minLength, + options.maxLength); }, 500); this.icc_input_box.placeholder = message; this.icc_input_box.type = options.isAlphabet ? 'text' : 'tel'; diff --git a/apps/system/js/icc_worker.js b/apps/system/js/icc_worker.js index f878237a8c35..e8207e1381a7 100644 --- a/apps/system/js/icc_worker.js +++ b/apps/system/js/icc_worker.js @@ -275,9 +275,8 @@ var icc_worker = { icc.hideViews(); }, true); - var timeout = options.duration || - icc.calculateDurationInMS(options.duration) || - icc._inputTimeout; + var timeout = (options.duration && + icc.calculateDurationInMS(options.duration)) || icc._inputTimeout; icc.input(message, options.text, timeout, options, function(response, value) { if (response == null) { diff --git a/apps/system/test/unit/icc_test.js b/apps/system/test/unit/icc_test.js new file mode 100644 index 000000000000..38e22dc04100 --- /dev/null +++ b/apps/system/test/unit/icc_test.js @@ -0,0 +1,228 @@ +/* global MocksHelper, MockNavigatorMozIccManager, icc, + MockNavigatorMozMobileConnections, MockNavigatormozSetMessageHandler, + MockL10n, MockFtuLauncher, MockNavigatorSettings */ +'use strict'; + +requireApp('system/test/unit/mock_l10n.js'); +requireApp('system/test/unit/mock_system_icc_worker.js'); +requireApp('system/test/unit/mock_ftu_launcher.js'); +require('/shared/test/unit/mocks/mock_navigator_moz_icc_manager.js'); +require('/shared/test/unit/mocks/mock_navigator_moz_settings.js'); +require('/shared/test/unit/mocks/mock_navigator_moz_set_message_handler.js'); +require('/shared/test/unit/mocks/mock_navigator_moz_mobile_connections.js'); +require('/shared/test/unit/mocks/mock_dump.js'); +require('/shared/test/unit/load_body_html_helper.js'); + + +mocha.globals(['FtuLaucher', 'icc_worker', 'icc']); + +var mocksForIcc = new MocksHelper([ + 'Dump', + 'FtuLauncher', + 'SystemICCWorker' +]).init(); + +suite('STK (icc) >', function() { + mocksForIcc.attachTestHelpers(); + var realMozIccManager, realSettings, realL10n, + realNavigatormozSetMessageHandler, realNavigatormozMobileConnections; + var stkTestCommands = {}; + + suiteSetup(function() { + loadBodyHTML('/index.html'); + + realSettings = window.navigator.mozSettings; + window.navigator.mozSettings = MockNavigatorSettings; + + realMozIccManager = navigator.mozIccManager; + navigator.mozIccManager = MockNavigatorMozIccManager; + + realL10n = navigator.mozL10n; + navigator.mozL10n = MockL10n; + + MockNavigatormozSetMessageHandler.mSetup(); + realNavigatormozSetMessageHandler = navigator.mozSetMessageHandler; + navigator.mozSetMessageHandler = MockNavigatormozSetMessageHandler; + + realNavigatormozMobileConnections = navigator.mozMobileConnections; + navigator.mozMobileConnections = MockNavigatorMozMobileConnections; + }); + + suiteTeardown(function() { + window.navigator.mozSettings = realSettings; + + MockNavigatorMozIccManager.mTeardown(); + navigator.mozIccManager = realMozIccManager; + + navigator.mozL10n = realL10n; + + MockNavigatormozSetMessageHandler.mTeardown(); + navigator.mozSetMessageHandler = realNavigatormozSetMessageHandler; + + MockNavigatorMozMobileConnections.mTeardown(); + navigator.mozMobileConnections = realNavigatormozMobileConnections; + }); + + setup(function(done) { + MockFtuLauncher.mIsRunning = false; + + requireApp('system/js/icc.js', done); + }); + + function launchStkCommand(cmd) { + icc.handleSTKCommand(cmd); + } + + setup(function() { + window.navigator.mozIccManager.addIcc('1010011010'); + + stkTestCommands.STK_CMD_GET_INPUT = { + iccId: '1010011010', + command: { + commandNumber: 1, + typeOfCommand: navigator.mozIccManager.STK_CMD_GET_INPUT, + commandQualifier: 0, + options: { + text: 'stk Input test text', + duration: { + timeUnit: navigator.mozIccManager.STK_TIME_UNIT_TENTH_SECOND, + timeInterval: 5 + }, + minLength: 2, + maxLength: 10, + defaultText: 'default' + } + } + }; + }); + + test('getIcc', function() { + assert.isObject(window.icc.getIcc('1010011010')); + }); + + test('getIccInfo', function(done) { + window.icc.getIccInfo(); + setTimeout(function() { + assert.equal(window.icc._defaultURL, + 'http://www.mozilla.org/en-US/firefoxos/'); + done(); + }, 100); + }); + + test('responseSTKCommand', function(done) { + window.icc.getIcc('1010011010').sendStkResponse = function(msg, res) { + assert.equal(res, 'dummy'); + done(); + }; + icc.responseSTKCommand(stkTestCommands.STK_CMD_GET_INPUT, 'dummy'); + }); + + test('terminateResponse', function(done) { + window.icc.getIcc('1010011010').sendStkResponse = function(msg, res) { + assert.equal(res.resultCode, + navigator.mozIccManager.STK_RESULT_UICC_SESSION_TERM_BY_USER); + done(); + }; + icc.terminateResponse(stkTestCommands.STK_CMD_GET_INPUT); + }); + + test('backResponse', function(done) { + window.icc.getIcc('1010011010').sendStkResponse = function(msg, res) { + assert.equal(res.resultCode, + navigator.mozIccManager.STK_RESULT_BACKWARD_MOVE_BY_USER); + done(); + }; + icc.backResponse(stkTestCommands.STK_CMD_GET_INPUT); + }); + + test('calculateDurationInMS', function() { + assert.equal(window.icc.calculateDurationInMS( + navigator.mozIccManager.STK_TIME_UNIT_MINUTE, 1), 3600000); + assert.equal(window.icc.calculateDurationInMS( + navigator.mozIccManager.STK_TIME_UNIT_MINUTE, 2), 7200000); + assert.equal(window.icc.calculateDurationInMS( + navigator.mozIccManager.STK_TIME_UNIT_SECOND, 1), 1000); + assert.equal(window.icc.calculateDurationInMS( + navigator.mozIccManager.STK_TIME_UNIT_SECOND, 2), 2000); + assert.equal(window.icc.calculateDurationInMS( + navigator.mozIccManager.STK_TIME_UNIT_TENTH_SECOND, 1), 100); + assert.equal(window.icc.calculateDurationInMS( + navigator.mozIccManager.STK_TIME_UNIT_TENTH_SECOND, 2), 200); + }); + + test('hideViews', function() { + window.icc.hideViews(); + assert.isFalse(document.getElementById('icc-view').classList.contains( + 'visible')); + var icc_view_boxes = document.getElementById('icc-view').children; + for (var i = 0; i < icc_view_boxes.length; i++) { + assert.isFalse(icc_view_boxes[i].classList.contains('visible')); + } + }); + + test('UI: Input (timeout 1sec)', function(done) { + var testCmd = stkTestCommands.STK_CMD_GET_INPUT; + window.icc.input(testCmd, testCmd.command.options.text, 1000, + stkTestCommands.STK_CMD_GET_INPUT.command.options, function(res, value) { + done(); + }); + }); + + test('UI: Input (contents)', function(done) { + var testCmd = stkTestCommands.STK_CMD_GET_INPUT; + window.icc.input(testCmd, testCmd.command.options.text, 0, + stkTestCommands.STK_CMD_GET_INPUT.command.options, function() {}); + + // We need to wait because workaround. See bug #818270. Followup: #895314 + // See function workaround_bug818270 in icc.js + setTimeout(function() { + assert.equal(document.getElementById('icc-input-msg').textContent, + testCmd.command.options.text); + assert.equal(document.getElementById('icc-input-btn').textContent, 'OK'); + assert.equal(document.getElementById('icc-input-btn').disabled, false); + assert.equal(document.getElementById('icc-input-btn_back').textContent, + 'Back'); + assert.equal(document.getElementById('icc-input-btn_help').textContent, + 'Help'); + done(); + }, 600); + }); + + test('UI: Input (checkInputLengthValid)', function() { + var testCmd = stkTestCommands.STK_CMD_GET_INPUT; + window.icc.input(testCmd, testCmd.command.options.text, 0, + stkTestCommands.STK_CMD_GET_INPUT.command.options, function() {}); + + var button = document.getElementById('icc-input-btn'); + var inputbox = document.getElementById('icc-input-box'); + + // We need to wait because workaround. See bug #818270. Followup: #895314 + // See function workaround_bug818270 in icc.js + setTimeout(function() { + // Using default string shall be enabled + assert.equal(button.disabled, false); + // Length between 3 and 10, empty is disabled + inputbox.value = ''; + assert.equal(button.disabled, true); + inputbox.value = '1'; + assert.equal(button.disabled, true); + inputbox.value = '12'; + assert.equal(button.disabled, true); + inputbox.value = '123'; + assert.equal(button.disabled, false); + inputbox.value = '1234567890'; + assert.equal(button.disabled, false); + inputbox.value = '12345678901'; + assert.equal(button.disabled, true); + }, 600); + }); + + test('launchStkCommand: STK_CMD_GET_INPUT', function(done) { + window.icc_worker.onmessagereceived = function(message) { + assert.equal(message, stkTestCommands.STK_CMD_GET_INPUT); + done(); + }; + launchStkCommand(stkTestCommands.STK_CMD_GET_INPUT); + }); + +}); diff --git a/apps/system/test/unit/icc_worker_test.js b/apps/system/test/unit/icc_worker_test.js new file mode 100644 index 000000000000..8c4e438b19ca --- /dev/null +++ b/apps/system/test/unit/icc_worker_test.js @@ -0,0 +1,101 @@ +'use strict'; + +/* global MocksHelper, MockNavigatorMozIccManager, MockSystemICC, icc_worker */ + +requireApp('system/test/unit/mock_l10n.js'); +requireApp('system/test/unit/mock_system_icc.js'); +require('/shared/test/unit/mocks/mock_navigator_moz_icc_manager.js'); +require('/shared/test/unit/mocks/mock_dump.js'); +requireApp('system/js/icc_worker.js'); + +mocha.globals(['icc']); + +var mocksForIcc = new MocksHelper([ + 'L10n', + 'Dump' +]).init(); + +suite('STK (icc_worker) >', function() { + mocksForIcc.attachTestHelpers(); + var realMozIccManager, realSystemICC; + var stkTestCommands = {}; + + suiteSetup(function() { + realMozIccManager = navigator.mozIccManager; + navigator.mozIccManager = MockNavigatorMozIccManager; + realSystemICC = window.icc; + window.icc = MockSystemICC(MockNavigatorMozIccManager); + }); + + suiteTeardown(function() { + MockNavigatorMozIccManager.mTeardown(); + navigator.mozIccManager = realMozIccManager; + MockSystemICC.mTeardown(); + window.icc = realSystemICC; + }); + + setup(function() { + stkTestCommands.STK_CMD_GET_INPUT = { + iccId: '1010011010', + command: { + commandNumber: 1, + typeOfCommand: navigator.mozIccManager.STK_CMD_GET_INPUT, + commandQualifier: 0, + options: { + text: 'stk Input test text', + duration: { + timeUnit: navigator.mozIccManager.STK_TIME_UNIT_TENTH_SECOND, + timeInterval: 5 + }, + minLength: 2, + maxLength: 10, + defaultText: 'default' + } + } + }; + }); + + function launchStkCommand(cmd) { + function stkCmd(CMD) { + return '0x' + CMD.toString(16); + } + icc_worker[stkCmd(cmd.command.typeOfCommand)](cmd); + } + + test('Check Dummy response', function(done) { + window.icc.onresponse = function(message, response) { + assert.equal(response.resultCode, navigator.mozIccManager.STK_RESULT_OK); + done(); + }; + icc_worker.dummy(); + }); + + test('STK_CMD_GET_INPUT (User response)', function(done) { + var stkResponse = 'stk introduced text'; + window.icc.input = function(stkMsg, message, timeout, options, callback) { + setTimeout(function() { + callback(true, stkResponse); + }); + }; + window.icc.onresponse = function(message, response) { + assert.equal(response.resultCode, navigator.mozIccManager.STK_RESULT_OK); + assert.equal(response.input, stkResponse); + done(); + }; + launchStkCommand(stkTestCommands.STK_CMD_GET_INPUT); + }); + + test('STK_CMD_GET_INPUT (Timeout)', function(done) { + window.icc.input = function(stkMsg, message, timeout, options, callback) { + setTimeout(function() { + callback(false); + }); + }; + window.icc.onresponse = function(message, response) { + assert.equal(response.resultCode, + navigator.mozIccManager.STK_RESULT_NO_RESPONSE_FROM_USER); + done(); + }; + launchStkCommand(stkTestCommands.STK_CMD_GET_INPUT); + }); +}); diff --git a/apps/system/test/unit/mock_system_icc.js b/apps/system/test/unit/mock_system_icc.js new file mode 100644 index 000000000000..a6399e4c6d03 --- /dev/null +++ b/apps/system/test/unit/mock_system_icc.js @@ -0,0 +1,36 @@ +'use strict'; + +/* exported MockSystemICC */ + +function MockSystemICC(iccManager) { + return { + onresponse: function() {}, + _iccManager: iccManager, + getSIMNumber: function() { + return 1; + }, + responseSTKCommand: function(message, response) { + this.onresponse(message, response); + }, + + calculateDurationInMS: function icc_calculateDurationInMS(timeUnit, + timeInterval) { + var timeout = timeInterval; + switch (timeUnit) { + case this._iccManager.STK_TIME_UNIT_MINUTE: + timeout *= 3600000; + break; + case this._iccManager.STK_TIME_UNIT_SECOND: + timeout *= 1000; + break; + case this._iccManager.STK_TIME_UNIT_TENTH_SECOND: + timeout *= 100; + break; + } + return timeout; + } + }; +} + +MockSystemICC.mTeardown = function() { +}; diff --git a/apps/system/test/unit/mock_system_icc_worker.js b/apps/system/test/unit/mock_system_icc_worker.js new file mode 100644 index 000000000000..67e468a932fc --- /dev/null +++ b/apps/system/test/unit/mock_system_icc_worker.js @@ -0,0 +1,116 @@ +'use strict'; + +/* exported MockSystemICCWorker */ + +function MockSystemICCWorker() { + return { + onmessagereceived: function() {}, + + // STK_CMD_REFRESH + '0x1': function STK_CMD_REFRESH(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_POLL_INTERVAL + '0x3': function STK_CMD_POLL_INTERVAL(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_POLL_OFF + '0x4': function STK_CMD_POLL_OFF(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_SET_UP_EVENT_LIST + '0x5': function STK_CMD_SET_UP_EVENT_LIST(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_SET_UP_CALL + '0x10': function STK_CMD_SET_UP_CALL(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_SEND_SS + '0x11': function STK_CMD_SEND_SS(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_SEND_USSD + '0x12': function STK_CMD_SEND_USSD(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_SEND_SMS + '0x13': function STK_CMD_SEND_SMS(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_SEND_DTMF + '0x14': function STK_CMD_SEND_DTMF(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_LAUNCH_BROWSER + '0x15': function STK_CMD_LAUNCH_BROWSER(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_PLAY_TONE + '0x20': function STK_CMD_PLAY_TONE(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_DISPLAY_TEXT + '0x21': function STK_CMD_DISPLAY_TEXT(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_GET_INKEY + '0x22': function STK_CMD_GET_INKEY(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_GET_INPUT + '0x23': function STK_CMD_GET_INPUT(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_SELECT_ITEM + '0x24': function STK_CMD_SELECT_ITEM(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_SET_UP_MENU + '0x25': function STK_CMD_SET_UP_MENU(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_PROVIDE_LOCAL_INFO + '0x26': function STK_CMD_PROVIDE_LOCAL_INFO(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_TIMER_MANAGEMENT + '0x27': function STK_CMD_TIMER_MANAGEMENT(message) { + this.onmessagereceived(message); + }, + + // STK_CMD_SET_UP_IDLE_MODE_TEXT + '0x28': function STK_CMD_SET_UP_IDLE_MODE_TEXT(message) { + this.onmessagereceived(message); + } + }; +} + +var realIccWorker = null; + +MockSystemICCWorker.mSetup = function() { + realIccWorker = window.icc_worker; + window.icc_worker = MockSystemICCWorker(); +}; + +MockSystemICCWorker.mTeardown = function() { + window.icc_worker = realIccWorker; + realIccWorker = null; +}; diff --git a/shared/test/unit/mocks/mock_dump.js b/shared/test/unit/mocks/mock_dump.js index 6ae3c07d4377..0fb4458283de 100644 --- a/shared/test/unit/mocks/mock_dump.js +++ b/shared/test/unit/mocks/mock_dump.js @@ -1,5 +1,8 @@ +/* global mocha */ 'use strict'; +mocha.globals(['DUMP']); + var MockDump = (function() { var dump_off = function() {}; var dump_on = function(msg, optionalObject) { @@ -21,3 +24,14 @@ var MockDump = (function() { }()); MockDump.disable(); + +var realDUMP = null; + +MockDump.mSetup = function() { + realDUMP = window.DUMP; + window.DUMP = MockDump.disable(); +}; + +MockDump.mTeardown = function() { + window.DUMP = realDUMP; +};