Android 浓缩咖啡测试禁用动画

Android 浓缩咖啡测试禁用动画,android,animation,android-testing,android-espresso,Android,Animation,Android Testing,Android Espresso,我在用涉及动画的视图进行浓缩咖啡测试时遇到了一个问题,我知道浓缩咖啡无法处理动画,所以我在下面做了。 -禁用我的测试设备窗口动画、过渡动画和animator持续时间比例全部设置为关闭(此选项不起作用) -然后我尝试在代码中添加一个标志,例如espresso_testing=true。如果为true,我的代码将跳过调用所有startAnimation()函数调用。-->这是有效的。然而,有一项要求是,在编写浓缩咖啡测试用例时,我不能更改应用程序上的代码。包括上面的一个测试用例 还有别的办法吗?提前

我在用涉及动画的视图进行浓缩咖啡测试时遇到了一个问题,我知道浓缩咖啡无法处理动画,所以我在下面做了。 -禁用我的测试设备窗口动画、过渡动画和animator持续时间比例全部设置为关闭(此选项不起作用) -然后我尝试在代码中添加一个标志,例如espresso_testing=true。如果为true,我的代码将跳过调用所有startAnimation()函数调用。-->这是有效的。然而,有一项要求是,在编写浓缩咖啡测试用例时,我不能更改应用程序上的代码。包括上面的一个测试用例


还有别的办法吗?提前感谢。

没错,您不应该在生产代码中添加测试代码。这里的问题在于动画。如果使用
处理程序
可运行程序
执行动画,则不能使用开发人员选项将其关闭。我们使用此设置动画的常见位置是在自定义视图中

但即使在自定义视图中,也要确保使用
ValueAnimator
ObjectAnimator
AnimatorSet
执行动画。只有这样,才能通过在“开发人员选项”中关闭“动画持续时间比例”(Animator duration scale))来关闭动画


一个很好的参考是
ProgressBar

确保更新您的插件:

buildscript {
  repositories {
    google()
    gradlePluginPortal()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
  }
}
android {

  ...

  testOptions {
    animationsDisabled = true
  }
}
# Turn off animations
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext());

使用
testOptions
中名为
animationsDisabled
的新标志:

buildscript {
  repositories {
    google()
    gradlePluginPortal()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
  }
}
android {

  ...

  testOptions {
    animationsDisabled = true
  }
}
# Turn off animations
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext());
来源:

您可以尝试手动关闭设备/模拟器上的动画:

buildscript {
  repositories {
    google()
    gradlePluginPortal()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
  }
}
android {

  ...

  testOptions {
    animationsDisabled = true
  }
}
# Turn off animations
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext());
为了避免片状,我们强烈建议您关闭系统 用于测试的虚拟或物理设备上的动画。在…上 在设备的“设置”>“开发人员选项”下,禁用以下选项 3种设置:

窗口动画缩放过渡动画缩放动画生成器持续时间 鳞片

来源:

您可以通过命令行尝试使用
adb

buildscript {
  repositories {
    google()
    gradlePluginPortal()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
  }
}
android {

  ...

  testOptions {
    animationsDisabled = true
  }
}
# Turn off animations
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext());
来源:

你可以试试LinkedIn的
TestButler

buildscript {
  repositories {
    google()
    gradlePluginPortal()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
  }
}
android {

  ...

  testOptions {
    animationsDisabled = true
  }
}
# Turn off animations
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext());
来源:

您可以尝试为您的浓缩咖啡测试创建
TestRule
Gradle
任务:

buildscript {
  repositories {
    google()
    gradlePluginPortal()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
  }
}
android {

  ...

  testOptions {
    animationsDisabled = true
  }
}
# Turn off animations
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext());

来源:

你可以看看这个

构建项目并下载生成的.apk文件,然后按照项目中提到的说明禁用动画,之后您应该会一帆风顺。您还可以从许多其他来源下载相同的.apk文件。 获得.apk文件后,发出以下命令:

adb install -r android_emulator_hacks.apk
adb shell pm grant no.finn.android_emulator_hacks android.permission.SET_ANIMATION_SCALE
adb shell am start -n no.finn.android_emulator_hacks/no.finn.android_emulator_hacks.HackActivity

这将为您禁用系统动画。

1。您在Gradle中使用此选项

android {

  //...

  testOptions {
    animationsDisabled = true
  }

  // ...
}
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
class DisableAnimationsRule : TestRule {
    override fun apply(base: Statement, description: Description): Statement {
        return object : Statement() {
            @Throws(Throwable::class)
           override fun evaluate() {
                // disable animations for test run
                changeAnimationStatus(enable = false)
                try {
                    base.evaluate()
                } finally {
                    // enable after test run
                    changeAnimationStatus(enable = true)
                }
            }
        }
    }

    @Throws(IOException::class)
    private fun changeAnimationStatus(enable:Boolean = true) {
        with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())){
            executeShellCommand("settings put global transition_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global window_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global animator_duration_scale ${if(enable) 1 else 0}")
        }
    }
}
2。在ADB中用于设备

android {

  //...

  testOptions {
    animationsDisabled = true
  }

  // ...
}
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
class DisableAnimationsRule : TestRule {
    override fun apply(base: Statement, description: Description): Statement {
        return object : Statement() {
            @Throws(Throwable::class)
           override fun evaluate() {
                // disable animations for test run
                changeAnimationStatus(enable = false)
                try {
                    base.evaluate()
                } finally {
                    // enable after test run
                    changeAnimationStatus(enable = true)
                }
            }
        }
    }

    @Throws(IOException::class)
    private fun changeAnimationStatus(enable:Boolean = true) {
        with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())){
            executeShellCommand("settings put global transition_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global window_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global animator_duration_scale ${if(enable) 1 else 0}")
        }
    }
}
3。使用规则

android {

  //...

  testOptions {
    animationsDisabled = true
  }

  // ...
}
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
class DisableAnimationsRule : TestRule {
    override fun apply(base: Statement, description: Description): Statement {
        return object : Statement() {
            @Throws(Throwable::class)
           override fun evaluate() {
                // disable animations for test run
                changeAnimationStatus(enable = false)
                try {
                    base.evaluate()
                } finally {
                    // enable after test run
                    changeAnimationStatus(enable = true)
                }
            }
        }
    }

    @Throws(IOException::class)
    private fun changeAnimationStatus(enable:Boolean = true) {
        with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())){
            executeShellCommand("settings put global transition_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global window_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global animator_duration_scale ${if(enable) 1 else 0}")
        }
    }
}

我在屏幕截图中添加了我的设备动画。即使有这些设置,动画测试仍然失败。非常重要的注意。感谢我正在使用真实设备进行测试,上述内容是否仍然适用?
animationsDisabled
不是未知属性。我刚才给你指了指官方文件。你必须拥有最新的android插件
2.3.1
@JaredBurrows,你是否自己测试了
animationsDisabled
标志,还是仅仅依赖文档?就我个人而言,我无法使用该标志,对于其他标志:
animationsDisabled
相当于
am instrument——运行测试时没有窗口动画
,并且
adb shell设置在运行测试前将全局窗口动画缩放为0
。要禁用所有动画,请在运行测试之前运行上面的所有三个
adb shell…
命令。来源:
animationsDisabled
根据我刚才使用gradle-5.1.1-all.zip和com.android.tools的经验,不起作用。编译:gradle:3.4.0。检查此项。它跑得很快