diff --git a/lib/tools/apk-utils.js b/lib/tools/apk-utils.js index 600e2a0e..1ae5f223 100644 --- a/lib/tools/apk-utils.js +++ b/lib/tools/apk-utils.js @@ -982,46 +982,31 @@ apkUtilsMethods.extractStringsFromApk = async function extractStringsFromApk ( * @return {Promise} 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} 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} 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(); }; /** @@ -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); })); };