Skip to content

Commit

Permalink
TECH: pass SystemLanguage instance through Device class (#643)
Browse files Browse the repository at this point in the history
* TECH: pass SystemLanguage instance through Device class
  • Loading branch information
Nikitae57 committed May 17, 2024
1 parent 0aad56b commit 958d6cb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ interface Language {
*/
@MainThread
fun switchInApp(locale: Locale)

/**
* Changes locale for Android OS Settings.
* Under the hood grants CHANGE_CONFIGURATION permission
* (without this permission, it's impossible to change system language)
*
* @throws Throwable if something went wrong
*/
@MainThread
fun switchInSystem(locale: Locale)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import android.os.LocaleList
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.ConfigurationCompat
import androidx.core.os.LocaleListCompat
import com.kaspersky.kaspresso.docloc.SystemLanguage
import com.kaspersky.kaspresso.logger.UiTestLogger
import java.util.Locale

/**
* The implementation of [Language]
*/
class LanguageImpl(
internal class LanguageImpl(
private val logger: UiTestLogger,
private val instrumentation: Instrumentation,
private val systemLanguage: SystemLanguage,
) : Language {

override fun switchInApp(locale: Locale) {
Expand Down Expand Up @@ -45,6 +47,8 @@ class LanguageImpl(
}
}

override fun switchInSystem(locale: Locale) = systemLanguage.switch(locale)

private fun getCurrentLocale(): Locale? =
ConfigurationCompat.getLocales(instrumentation.targetContext.resources.configuration).get(0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.kaspersky.kaspresso.device.permissions.HackPermissions
import com.kaspersky.kaspresso.logger.UiTestLogger
import java.util.Locale

class SystemLanguage(
internal class SystemLanguage(
private val context: Context,
private val logger: UiTestLogger,
private val hackPermissions: HackPermissions
Expand Down Expand Up @@ -40,7 +40,7 @@ class SystemLanguage(
Failed to change system locale due to API restrictions. To fix this execute one of the following commands.
For Android 10 (API level 29) or higher:
"adb shell settings put global hidden_api_policy 1"
For Android 9 (API level 28):
For Android 9 (API level 28):
"adb shell settings put global hidden_api_policy_pre_p_apps 1"
"adb shell settings put global hidden_api_policy_p_apps 1"
To rollback these settings execute
Expand All @@ -62,6 +62,7 @@ class SystemLanguage(
* Try to grant permission CHANGE_CONFIGURATION if the permission was not granted
* @throws DocLocException In case of a failure to grant one
*/
@Suppress("DEPRECATION")
private fun grantPermissionsIfNeed() {
val permissionStateAtTheBeginning = context.checkPermission(Manifest.permission.CHANGE_CONFIGURATION, Process.myPid(), Process.myUid())
if (permissionStateAtTheBeginning == PackageManager.PERMISSION_GRANTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import com.kaspersky.kaspresso.device.video.VideosImpl
import com.kaspersky.kaspresso.device.video.recorder.VideoRecorderImpl
import com.kaspersky.kaspresso.device.viewhierarchy.ViewHierarchyDumper
import com.kaspersky.kaspresso.device.viewhierarchy.ViewHierarchyDumperImpl
import com.kaspersky.kaspresso.docloc.SystemLanguage
import com.kaspersky.kaspresso.failure.LoggingFailureHandler
import com.kaspersky.kaspresso.files.dirs.DefaultDirsProvider
import com.kaspersky.kaspresso.files.dirs.DirsProvider
Expand Down Expand Up @@ -742,7 +743,10 @@ data class Kaspresso(
instrumentalDependencyProviderFactory.getComponentProvider<ExploitImpl>(instrumentation),
adbServer
)
if (!::language.isInitialized) language = LanguageImpl(libLogger, instrumentation)
if (!::language.isInitialized) {
val systemLanguage = SystemLanguage(instrumentation.targetContext, testLogger, hackPermissions)
language = LanguageImpl(libLogger, instrumentation, systemLanguage)
}
if (!::logcat.isInitialized) logcat = LogcatImpl(libLogger, adbServer)

if (!::flakySafetyParams.isInitialized) flakySafetyParams = FlakySafetyParams.default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.test.rule.GrantPermissionRule
import com.kaspersky.kaspressample.MainActivity
import com.kaspersky.kaspressample.screen.ChangeLocaleScreen
import com.kaspersky.kaspressample.screen.MainScreen
import com.kaspersky.kaspresso.docloc.SystemLanguage
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
import org.junit.Rule
import org.junit.Test
Expand All @@ -24,20 +23,19 @@ class ChangeSystemLanguageMidTestCase : TestCase() {

@Test
fun checkLocaleChangeMidTest() = run {
val systemLanguage = SystemLanguage(device.targetContext, testLogger, device.hackPermissions)
step("Open change locale screen") {
MainScreen {
changeLocaleButton { click() }
}
}
step("Check EN locale text") {
systemLanguage.switch(Locale.ENGLISH)
device.language.switchInSystem(Locale.ENGLISH)
ChangeLocaleScreen {
text { containsText("123") }
}
}
step("Check RU locale text") {
systemLanguage.switch(Locale.forLanguageTag("RU"))
device.language.switchInSystem(Locale.forLanguageTag("RU"))
ChangeLocaleScreen {
text { containsText("321") }
}
Expand Down

0 comments on commit 958d6cb

Please sign in to comment.