Skip to content

Commit

Permalink
chore: Streamline locale validation (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Oct 31, 2024
1 parent 79b3bec commit 1b7e859
Showing 1 changed file with 39 additions and 61 deletions.
100 changes: 39 additions & 61 deletions lib/tools/apk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,46 +982,31 @@ apkUtilsMethods.extractStringsFromApk = async function extractStringsFromApk (
* @return {Promise<string>} The name of device language.
*/
apkUtilsMethods.getDeviceLanguage = async function getDeviceLanguage () {
let language;
if (await this.getApiLevel() < 23) {
language = await this.getDeviceSysLanguage();
if (!language) {
language = await this.getDeviceProductLanguage();
}
} else {
language = (await this.getDeviceLocale()).split('-')[0];
}
return language;
return await this.getApiLevel() < 23
? (await this.getDeviceSysLanguage() || await this.getDeviceProductLanguage())
: (await this.getDeviceLocale()).split('-')[0];
};

/**
* Get the country name of the device under test.
*
* @summary Could only be used for Android API < 23
* @this {import('../adb.js').ADB}
* @return {Promise<string>} The name of device country.
*/
apkUtilsMethods.getDeviceCountry = async function getDeviceCountry () {
// this method is only used in API < 23
let country = await this.getDeviceSysCountry();
if (!country) {
country = await this.getDeviceProductCountry();
}
return country;
return await this.getDeviceSysCountry() || await this.getDeviceProductCountry();
};

/**
* Get the locale name of the device under test.
*
* @summary Could only be used for Android API >= 23
* @this {import('../adb.js').ADB}
* @return {Promise<string>} The name of device locale.
*/
apkUtilsMethods.getDeviceLocale = async function getDeviceLocale () {
// this method is only used in API >= 23
let locale = await this.getDeviceSysLocale();
if (!locale) {
locale = await this.getDeviceProductLocale();
}
return locale;
return await this.getDeviceSysLocale() || await this.getDeviceProductLocale();
};

/**
Expand All @@ -1038,58 +1023,51 @@ apkUtilsMethods.getDeviceLocale = async function getDeviceLocale () {
apkUtilsMethods.ensureCurrentLocale = async function ensureCurrentLocale (language, country, script) {
const hasLanguage = _.isString(language);
const hasCountry = _.isString(country);

if (!hasLanguage && !hasCountry) {
log.warn('ensureCurrentLocale requires language or country');
return false;
}

// get lower case versions of the strings
language = (language || '').toLowerCase();
country = (country || '').toLowerCase();

const lcLanguage = (language || '').toLowerCase();
const lcCountry = (country || '').toLowerCase();
const apiLevel = await this.getApiLevel();

return /** @type {boolean} */ (await retryInterval(5, 1000, async () => {
try {
if (apiLevel < 23) {
let curLanguage, curCountry;
if (hasLanguage) {
curLanguage = (await this.getDeviceLanguage()).toLowerCase();
if (!hasCountry && language === curLanguage) {
return true;
}
}
if (hasCountry) {
curCountry = (await this.getDeviceCountry()).toLowerCase();
if (!hasLanguage && country === curCountry) {
return true;
}
}
if (language === curLanguage && country === curCountry) {
if (apiLevel < 23) {
log.debug(`Requested locale: ${lcLanguage}-${lcCountry}`);
let actualLanguage;
if (hasLanguage) {
actualLanguage = (await this.getDeviceLanguage()).toLowerCase();
log.debug(`Actual language: ${actualLanguage}`);
if (!hasCountry && lcLanguage === actualLanguage) {
return true;
}
} else {
const curLocale = (await this.getDeviceLocale()).toLowerCase();
// zh-hans-cn : zh-cn
const localeCode = script ? `${language}-${script.toLowerCase()}-${country}` : `${language}-${country}`;

if (localeCode === curLocale) {
log.debug(`Requested locale is equal to current locale: '${curLocale}'`);
}
let actualCountry;
if (hasCountry) {
actualCountry = (await this.getDeviceCountry()).toLowerCase();
log.debug(`Actual country: ${actualCountry}`);
if (!hasLanguage && lcCountry === actualCountry) {
return true;
}
}
return false;
} catch (err) {
// if there has been an error, restart adb and retry
log.error(`Unable to check device localization: ${err.message}`);
try {
await this.reconnect();
} catch (ign) {
await this.restartAdb();
}
throw err;
return lcLanguage === actualLanguage && lcCountry === actualCountry;
}
const actualLocale = (await this.getDeviceLocale()).toLowerCase();
// zh-hans-cn : zh-cn
const expectedLocale = script
? `${lcLanguage}-${script.toLowerCase()}-${lcCountry}`
: `${lcLanguage}-${lcCountry}`;
log.debug(`Requested locale: ${expectedLocale}. Actual locale: '${actualLocale}'`);
const languagePattern = `^${_.escapeRegExp(lcLanguage)}-${script ? (_.escapeRegExp(script) + '-') : ''}`;
const checkLocalePattern = (/** @type {string} */ p) => new RegExp(p, 'i').test(actualLocale);
if (hasLanguage && !hasCountry) {
return checkLocalePattern(languagePattern);
}
const countryPattern = `${script ? ('-' + _.escapeRegExp(script)) : ''}-${_.escapeRegExp(lcCountry)}$`;
if (!hasLanguage && hasCountry) {
return checkLocalePattern(countryPattern);
}
return [languagePattern, countryPattern].every(checkLocalePattern);
}));
};

Expand Down

0 comments on commit 1b7e859

Please sign in to comment.