Skip to content

Instantly share code, notes, and snippets.

@AniketSK
Created August 4, 2019 13:39
Show Gist options
  • Save AniketSK/5f26b2cb07840759d737ea97e48aa83a to your computer and use it in GitHub Desktop.
Save AniketSK/5f26b2cb07840759d737ea97e48aa83a to your computer and use it in GitHub Desktop.
A rule to disable animations and all that other stuff you're supposed to do before running integration tests on a device. You will need this import among others. androidTestImplementation "androidx.test.uiautomator:uiautomator:2.2.0" the idea of it was taken from the book Android Espresso Revealed: Writing Automated Unit Tests by Denys Zelenchuk…
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="${applicationId}">
<uses-sdk tools:overrideLibrary="android_libs.ub_uiautomator"/>
</manifest>
package com.aniketkadam.tryoutstuff
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
class AnimationDisablerRule : TestRule {
private val commandsToSetUpTestEnvironment = listOf(
"settings put global transition_animation_scale 0.0",
"settings put global animator_duration_scale 0.0",
"settings put global window_animation_scale 0.0",
"settings put secure long_press_timeout 1500",
"settings put secure show_ime_with_hard_keyboard 0"
)
private fun setDevicePreferences(commands : List<String>) {
with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())){
commands.forEach {
executeShellCommand(it)
}
}
}
override fun apply(base: Statement?, description: Description?): Statement {
return object : Statement() {
override fun evaluate() {
setDevicePreferences(commandsToSetUpTestEnvironment)
base?.evaluate()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment