Skip to content

Commit

Permalink
add localeScript capability to set script in locale (#460)
Browse files Browse the repository at this point in the history
* add localeScript capability

* use a ternary operator

* use setting 2.10.0

* bump appium-adb
  • Loading branch information
KazuCocoa committed Nov 5, 2018
1 parent d7c5291 commit ea55aec
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
23 changes: 18 additions & 5 deletions lib/android-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,30 @@ helpers.ensureNetworkSpeed = function (adb, networkSpeed) {
return adb.NETWORK_SPEED.FULL;
};

helpers.ensureDeviceLocale = async function (adb, language, country) {
/**
* Set and ensure the locale name of the device under test.
*
* @param {Object} adb - The adb module instance.
* @param {string} language - Language. The language field is case insensitive, but Locale always canonicalizes to lower case.
* format: [a-zA-Z]{2,8}. e.g. en, ja : https://developer.android.com/reference/java/util/Locale.html
* @param {string} country - Country. The country (region) field is case insensitive, but Locale always canonicalizes to upper case.
* format: [a-zA-Z]{2} | [0-9]{3}. e.g. US, JP : https://developer.android.com/reference/java/util/Locale.html
* @param {?string} script - Script. The script field is case insensitive but Locale always canonicalizes to title case.
* format: [a-zA-Z]{4}. e.g. Hans in zh-Hans-CN : https://developer.android.com/reference/java/util/Locale.html
* @throws {Error} If it failed to set locale properly
*/
helpers.ensureDeviceLocale = async function (adb, language, country, script = null) {
if (!_.isString(language) && !_.isString(country)) {
logger.warn(`setDeviceLanguageCountry requires language or country.`);
logger.warn(`Got language: '${language}' and country: '${country}'`);
return;
}

await adb.setDeviceLanguageCountry(language, country);
await adb.setDeviceLanguageCountry(language, country, script);

if (!await adb.ensureCurrentLocale(language, country)) {
throw new Error(`Failed to set language: ${language} and country: ${country}`);
if (!await adb.ensureCurrentLocale(language, country, script)) {
const message = script ? `language: ${language}, country: ${country} and script: ${script}` : `language: ${language} and country: ${country}`;
throw new Error(`Failed to set ${message}`);
}
};

Expand Down Expand Up @@ -612,7 +625,7 @@ helpers.initDevice = async function (adb, opts) {
await helpers.setMockLocationApp(adb, SETTINGS_HELPER_PKG_ID);
}

await helpers.ensureDeviceLocale(adb, opts.language, opts.locale);
await helpers.ensureDeviceLocale(adb, opts.language, opts.locale, opts.localeScript);
await adb.startLogcat();
if (opts.unicodeKeyboard) {
return await helpers.initUnicodeKeyboard(adb);
Expand Down
5 changes: 4 additions & 1 deletion lib/desired-caps.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ let commonCapConstraints = {
},
pageLoadStrategy: {
isString: true
}
},
localeScript: {
isString: true
},
};

let uiautomatorCapConstraints = {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
],
"dependencies": {
"@babel/runtime": "^7.0.0",
"appium-adb": "^6.19.0",
"appium-adb": "^6.25.0",
"appium-base-driver": "^3.0.0",
"appium-chromedriver": "^4.0.0",
"appium-support": "^2.24.1",
"asyncbox": "^2.0.4",
"bluebird": "^3.4.7",
"io.appium.settings": "^2.8.0",
"io.appium.settings": "^2.10.0",
"jimp": "^0.5.3",
"lodash": "^4.17.4",
"moment": "^2.22.2",
Expand Down
10 changes: 10 additions & 0 deletions test/functional/android-helper-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ describe('android-helpers e2e', function () {
await adb.getDeviceLocale().should.eventually.equal('fr-FR');
}
});
it('should set device language and country with script', async function () {
await helpers.ensureDeviceLocale(adb, 'zh', 'CN', 'Hans');

if (await adb.getApiLevel() < 23) {
await adb.getDeviceLanguage().should.eventually.equal('fr');
await adb.getDeviceCountry().should.eventually.equal('FR');
} else {
await adb.getDeviceLocale().should.eventually.equal('fr-Hans-CN');
}
});
});
describe('pushSettingsApp', function () {
const settingsPkg = 'io.appium.settings';
Expand Down
24 changes: 18 additions & 6 deletions test/unit/android-helper-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,32 @@ describe('Android Helpers', function () {
});
describe('ensureDeviceLocale', withMocks({adb}, (mocks) => {
it('should call setDeviceLanguageCountry', async function () {
mocks.adb.expects('setDeviceLanguageCountry').withExactArgs('en', 'US').once();
mocks.adb.expects('ensureCurrentLocale').withExactArgs('en', 'US').once().returns(true);
mocks.adb.expects('setDeviceLanguageCountry').withExactArgs('en', 'US', null).once();
mocks.adb.expects('ensureCurrentLocale').withExactArgs('en', 'US', null).once().returns(true);
await helpers.ensureDeviceLocale(adb, 'en', 'US');
mocks.adb.verify();
});
it('should call setDeviceLanguageCountry without script', async function () {
mocks.adb.expects('setDeviceLanguageCountry').withExactArgs('en', 'US', null).once();
mocks.adb.expects('ensureCurrentLocale').withExactArgs('en', 'US', null).once().returns(true);
await helpers.ensureDeviceLocale(adb, 'en', 'US', undefined);
mocks.adb.verify();
});
it('should call setDeviceLanguageCountry with script', async function () {
mocks.adb.expects('setDeviceLanguageCountry').withExactArgs('zh', 'CN', 'Hans').once();
mocks.adb.expects('ensureCurrentLocale').withExactArgs('zh', 'CN', 'Hans').once().returns(true);
await helpers.ensureDeviceLocale(adb, 'zh', 'CN', 'Hans');
mocks.adb.verify();
});
it('should never call setDeviceLanguageCountry', async function () {
mocks.adb.expects('setDeviceLanguageCountry').never();
mocks.adb.expects('getApiLevel').never();
await helpers.ensureDeviceLocale(adb);
mocks.adb.verify();
});
it('should call setDeviceLanguageCountry with throw', async function () {
mocks.adb.expects('setDeviceLanguageCountry').withExactArgs('fr', 'FR').once();
mocks.adb.expects('ensureCurrentLocale').withExactArgs('fr', 'FR').once().returns(false);
mocks.adb.expects('setDeviceLanguageCountry').withExactArgs('fr', 'FR', null).once();
mocks.adb.expects('ensureCurrentLocale').withExactArgs('fr', 'FR', null).once().returns(false);
await helpers.ensureDeviceLocale(adb, 'fr', 'FR').should.eventually.be.rejectedWith(Error, `Failed to set language: fr and country: FR`);
mocks.adb.verify();
});
Expand Down Expand Up @@ -620,11 +632,11 @@ describe('Android Helpers', function () {
}));
describe('initDevice', withMocks({helpers, adb}, (mocks) => {
it('should init device', async function () {
const opts = {language: "en", locale: "us"};
const opts = {language: "en", locale: "us", localeScript: 'Script'};
mocks.adb.expects('waitForDevice').once();
mocks.adb.expects('startLogcat').once();
mocks.helpers.expects('pushSettingsApp').once();
mocks.helpers.expects('ensureDeviceLocale').withExactArgs(adb, opts.language, opts.locale).once();
mocks.helpers.expects('ensureDeviceLocale').withExactArgs(adb, opts.language, opts.locale, opts.localeScript).once();
mocks.helpers.expects('setMockLocationApp').withExactArgs(adb, 'io.appium.settings').once();
await helpers.initDevice(adb, opts);
mocks.helpers.verify();
Expand Down

0 comments on commit ea55aec

Please sign in to comment.