Bug 898445 - Part 6-2: Convert test_mobile_preferred_network_type to Promise. r=hsinyi

This commit is contained in:
Edgar Chen 2014-04-17 19:26:57 +08:00
parent a7c82b687b
commit e302b4bbb3
4 changed files with 184 additions and 135 deletions

View File

@ -10,6 +10,7 @@ const SETTINGS_KEY_DATA_APN_SETTINGS = "ril.data.apnSettings";
let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise;
let _pendingEmulatorCmdCount = 0;
let _pendingEmulatorShellCmdCount = 0;
/**
* Send emulator command with safe guard.
@ -47,6 +48,35 @@ function runEmulatorCmdSafe(aCommand) {
return deferred.promise;
}
/**
* Send emulator shell command with safe guard.
*
* We should only call |finish()| after all emulator shell command transactions
* end, so here comes with the pending counter. Resolve when the emulator
* shell gives response. Never reject.
*
* Fulfill params:
* result -- an array of emulator shell response lines.
*
* @param aCommands
* A string array commands to be passed to emulator through adb shell.
*
* @return A deferred promise.
*/
function runEmulatorShellCmdSafe(aCommands) {
let deferred = Promise.defer();
++_pendingEmulatorShellCmdCount;
runEmulatorShell(aCommands, function(aResult) {
--_pendingEmulatorShellCmdCount;
log("Emulator shell response: " + JSON.stringify(aResult));
deferred.resolve(aResult);
});
return deferred.promise;
}
/**
* Wrap DOMRequest onsuccess/onerror events to Promise resolve/reject.
*
@ -436,6 +466,45 @@ function sendMMI(aMmi) {
.then(null, () => { throw request.error });
}
/**
* Set preferred network type.
*
* Fulfill params: (none)
* Reject params:
* 'RadioNotAvailable', 'RequestNotSupported', 'ModeNotSupported' or
* 'GenericFailure'.
*
* @param aType
* 'wcdma/gsm', 'gsm', 'wcdma', 'wcdma/gsm-auto', 'cdma/evdo', 'cdma',
* 'evdo', 'wcdma/gsm/cdma/evdo', 'lte/cdma/evdo', 'lte/wcdma/gsm',
* 'lte/wcdma/gsm/cdma/evdo' or 'lte'.
*
* @return A deferred promise.
*/
function setPreferredNetworkType(aType) {
let request = mobileConnection.setPreferredNetworkType(aType);
return wrapDomRequestAsPromise(request)
.then(null, () => { throw request.error });
}
/**
* Query current preferred network type.
*
* Fulfill params:
* 'wcdma/gsm', 'gsm', 'wcdma', 'wcdma/gsm-auto', 'cdma/evdo', 'cdma',
* 'evdo', 'wcdma/gsm/cdma/evdo', 'lte/cdma/evdo', 'lte/wcdma/gsm',
* 'lte/wcdma/gsm/cdma/evdo' or 'lte'.
* Reject params:
* 'RadioNotAvailable', 'RequestNotSupported', or 'GenericFailure'.
*
* @return A deferred promise.
*/
function getPreferredNetworkType() {
let request = mobileConnection.getPreferredNetworkType();
return wrapDomRequestAsPromise(request)
.then(() => request.result, () => { throw request.error });
}
/**
* Set data connection enabling state and wait for "datachange" event.
*
@ -859,7 +928,8 @@ function cleanUp() {
finish();
});
}, function() {
return _pendingEmulatorCmdCount === 0;
return _pendingEmulatorCmdCount === 0 &&
_pendingEmulatorShellCmdCount === 0;
});
}

View File

@ -26,5 +26,6 @@ qemu = true
[test_mobile_signal_strength.js]
[test_mobile_data_ipv6.js]
disabled = Bug 979137
[test_mobile_supported_network_types.js]
[test_dsds_mobile_data_connection.js]
[test_mobile_clir_radio_off.js]

View File

@ -2,145 +2,85 @@
http://creativecommons.org/publicdomain/zero/1.0/ */
MARIONETTE_TIMEOUT = 60000;
MARIONETTE_HEAD_JS = "mobile_header.js";
MARIONETTE_HEAD_JS = "head.js";
function doSetAndVerifyPreferredNetworkType(preferredNetworkType, callback) {
log("setPreferredNetworkType to '" + preferredNetworkType + "'.");
let setRequest = mobileConnection.setPreferredNetworkType(preferredNetworkType);
ok(setRequest instanceof DOMRequest,
"setRequest instanceof " + setRequest.constructor);
const TEST_DATA = [
{
preferredNetworkType: "gsm"
}, {
preferredNetworkType: "wcdma"
}, {
preferredNetworkType: "wcdma/gsm-auto"
}, {
preferredNetworkType: "cdma/evdo"
}, {
preferredNetworkType: "evdo"
}, {
preferredNetworkType: "cdma"
}, {
preferredNetworkType: "wcdma/gsm/cdma/evdo"
},
{
preferredNetworkType: "wcdma/gsm" // Restore to default
},
// Currently emulator doesn't support lte network. So we expect to get a
// 'ModeNotSupported' error here.
{
preferredNetworkType: "lte/cdma/evdo",
expectedErrorMessage: "ModeNotSupported"
}, {
preferredNetworkType: "lte/wcdma/gsm",
expectedErrorMessage: "ModeNotSupported"
}, {
preferredNetworkType: "lte/wcdma/gsm/cdma/evdo",
expectedErrorMessage: "ModeNotSupported"
}, {
preferredNetworkType: "lte",
expectedErrorMessage: "ModeNotSupported"
},
// Test passing an invalid mode. We expect to get an exception here.
{
preferredNetworkType: "InvalidTypes",
expectGotException: true
}, {
preferredNetworkType: null,
expectGotException: true
}
];
setRequest.onsuccess = function() {
log("Verify preferred network.");
let getRequest = mobileConnection.getPreferredNetworkType();
ok(getRequest instanceof DOMRequest,
"getRequest instanceof " + getRequest.constructor);
getRequest.onsuccess = function() {
is(getRequest.result, preferredNetworkType, "Check preferred network type.");
callback();
};
getRequest.onerror = function() {
ok(false, "getPreferredNetworkType got error: " + getRequest.error.name);
callback();
};
};
setRequest.onerror = function() {
ok(false, "setPreferredNetwork got error: " + setRequest.error.name);
callback();
};
function verifyPreferredNetworkType(aExpectedType) {
return getPreferredNetworkType()
.then(function resolve(aType) {
is(aType, aExpectedType, "getPreferredNetworkType success");
}, function reject() {
ok(false, "failed to getPreferredNetworkType");
});
}
function doFailToSetPreferredNetworkType(preferredNetworkType, expectedError, callback) {
log("setPreferredNetworkType to '" + preferredNetworkType + "'.");
let request = mobileConnection.setPreferredNetworkType(preferredNetworkType);
ok(request instanceof DOMRequest,
"request instanceof " + request.constructor);
function setAndVerifyPreferredNetworkType(aType, aExpectedErrorMsg, aExpectGotException) {
log("Test setting preferred network types to " + aType);
request.onsuccess = function() {
ok(false, "Should not success");
callback();
};
request.onerror = function() {
is(request.error.name, expectedError, "Check error message.");
callback();
};
try {
return setPreferredNetworkType(aType)
.then(function resolve() {
ok(!aExpectedErrorMsg, "setPreferredNetworkType success");
return verifyPreferredNetworkType(aType);
}, function reject(aError) {
is(aError.name, aExpectedErrorMsg, "failed to setPreferredNetworkType");
});
} catch (e) {
ok(aExpectGotException, "caught an exception: " + e);
}
}
function getSupportedNetworkTypesFromSystemProperties(clientId, callback) {
let key = "ro.moz.ril." + clientId + ".network_types";
runEmulatorShell(["getprop", key], function(results) {
let result = results[0];
if (!result || result === "") {
// Copied from GECKO_SUPPORTED_NETWORK_TYPES_DEFAULT in dom/system/gonk/ril_consts.js.
result = "wcdma,gsm";
}
callback(result.split(","));
});
}
/* Test supportedNetworkTypes */
taskHelper.push(function testSupportedNetworkTypes() {
let supportedNetworkTypes = mobileConnection.supportedNetworkTypes;
ok(Array.isArray(supportedNetworkTypes), "supportedNetworkTypes should be an array");
getSupportedNetworkTypesFromSystemProperties(0, function(testData) {
is(testData.length, supportedNetworkTypes.length);
for (let i = 0; i < testData.length; i++) {
ok(supportedNetworkTypes.indexOf(testData[i]) >= 0, "Should support '" + testData[i] + "'");
}
taskHelper.runNext();
});
// Start tests
startTestCommon(function() {
let promise = Promise.resolve();
for (let i = 0; i < TEST_DATA.length; i++) {
let data = TEST_DATA[i];
promise = promise.then(() => setAndVerifyPreferredNetworkType(data.preferredNetworkType,
data.expectedErrorMessage,
data.expectGotException));
}
return promise;
});
/* Test switching to supported preferred types */
taskHelper.push(function testPreferredNetworkTypes() {
let supportedTypes = [
'gsm',
'wcdma',
'wcdma/gsm-auto',
'cdma/evdo',
'evdo',
'cdma',
'wcdma/gsm/cdma/evdo',
// Restore to default
'wcdma/gsm'
];
// Run all test data.
(function do_call() {
let type = supportedTypes.shift();
if (!type) {
taskHelper.runNext();
return;
}
doSetAndVerifyPreferredNetworkType(type, do_call);
})();
});
/* Test switching to unsupported preferred types */
taskHelper.push(function testUnsupportedPreferredNetworkTypes() {
// Currently emulator doesn't support lte network
let unsupportedTypes = [
'lte/cdma/evdo',
'lte/wcdma/gsm',
'lte/wcdma/gsm/cdma/evdo',
'lte'
];
// Run all test data.
(function do_call() {
let type = unsupportedTypes.shift();
if (!type) {
taskHelper.runNext();
return;
}
doFailToSetPreferredNetworkType(type, "ModeNotSupported", do_call);
})();
});
/* Test switching to invalid preferred types */
taskHelper.push(function testInvalidPreferredNetworkTypes() {
let invalidTypes = [
' ',
'AnInvalidType'
];
// Run all test data.
(function do_call() {
let type = invalidTypes.shift();
if (!type) {
taskHelper.runNext();
return;
}
doFailToSetPreferredNetworkType(type, "InvalidParameter", do_call);
})();
});
// Start test
taskHelper.runNext();

View File

@ -0,0 +1,38 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
MARIONETTE_TIMEOUT = 60000;
MARIONETTE_HEAD_JS = "head.js";
function getSupportedNetworkTypesFromSystemProperties(aClientId) {
let key = "ro.moz.ril." + aClientId + ".network_types";
return runEmulatorShellCmdSafe(["getprop", key])
.then(function resolve(aResults) {
let result = aResults[0];
if (!result || result === "") {
// Copied from GECKO_SUPPORTED_NETWORK_TYPES_DEFAULT in
// dom/system/gonk/ril_consts.js.
result = "wcdma,gsm";
}
return result.split(",");
});
}
// Start test
startTestCommon(function() {
return Promise.resolve()
// Get SupportedNetworkTypes from system properties.
.then(() => getSupportedNetworkTypesFromSystemProperties(0))
.then((testData) => {
let supportedNetworkTypes = mobileConnection.supportedNetworkTypes;
ok(Array.isArray(supportedNetworkTypes),
"supportedNetworkTypes should be an array");
is(testData.length, supportedNetworkTypes.length);
for (let i = 0; i < testData.length; i++) {
ok(supportedNetworkTypes.indexOf(testData[i]) >= 0,
"Should support '" + testData[i] + "'");
}
});
});