"Animators may only be run on Looper threads" 在设备上的仪器测试期间
"Animators may only be run on Looper threads" during on-device intrumentation testing
这是我的测试代码:
@RunWith(AndroidJUnit4.class)
@SmallTest
public class WelcomeActivityTests extends BaseTest {
ApplicationController applicationController;
@Rule
public ActivityTestRule<WelcomeActivity> activityTestRule = new ActivityTestRule<>(WelcomeActivity.class);
ArgumentCaptor<Callback> argumentCaptor;
@Before
@Override public void setUp() {
applicationController = (ApplicationController) InstrumentationRegistry.getTargetContext().getApplicationContext();
applicationController.setMockMode(true);
argumentCaptor = ArgumentCaptor.forClass(Callback.class);
super.setUp();
}
@Test
public void testLogin() throws InterruptedException {
onView(withId(R.id.btnLogInW)).perform(click());
onView(withId(R.id.email)).perform(typeText("good.email@example.com"));
onView(withId(R.id.passL)).perform(typeText("strong.password"));
onView(withId(R.id.btnLogInL)).perform(click());
User user = new User();
user.first_name = "Fake name";
user.last_name = "Fake name";
user.id = 1;
user.email = "fake.email@gmail.com";
AuthResponse authResponse = new AuthResponse();
authResponse.api_key = "fake_api_key";
authResponse.status = "ok";
authResponse.user = user;
Mockito.verify(api).login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), argumentCaptor.capture());
argumentCaptor.getValue().success(authResponse, null);
assert true;
}
@After
public void release() {
applicationController.setMockMode(false);
}
}
我使用 espresso 单击的按钮基于 material 主题,它们下面可能有一些动画。结果是,当我尝试 运行 时,每次我执行点击时,一些仪器测试应用程序都会崩溃。我尝试在系统开发人员选项中关闭动画,但没有用。
android.util.AndroidRuntimeException: Animators may only be run on
Looper threads at
android.animation.ValueAnimator.start(ValueAnimator.java:1002) at
android.animation.ValueAnimator.start(ValueAnimator.java:1050) at
android.animation.ObjectAnimator.start(ObjectAnimator.java:829) at
android.animation.AnimatorSet.start(AnimatorSet.java:585) at
android.animation.StateListAnimator.start(StateListAnimator.java:187)
at
android.animation.StateListAnimator.setState(StateListAnimator.java:180)
at android.view.View.drawableStateChanged(View.java:15988) at
android.widget.TextView.drawableStateChanged(TextView.java:3659) at
android.view.View.refreshDrawableState(View.java:16032) at
android.view.View.setEnabled(View.java:6724) at
android.widget.TextView.setEnabled(TextView.java:1446) at
my.app.ui.fragments.welcome.LoginFragment.unlock(LoginFragment.java:263)
at
my.app.ui.fragments.welcome.LoginFragment.success(LoginFragment.java:224)
at
my.app.ui.fragments.welcome.LoginFragment.success(LoginFragment.java:222)
at
my.app.WelcomeActivityTests.testRate(WelcomeActivityTests.java:84)
at java.lang.reflect.Method.invoke(Native Method) at
java.lang.reflect.Method.invoke(Method.java:372) at
org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at
android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at
android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
at org.junit.rules.RunRules.evaluate(RunRules.java:18) at
org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner.run(ParentRunner.java:231) at
org.junit.runners.ParentRunner.schedule(ParentRunner.java:60) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at
org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:50) at
org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at
org.junit.runners.ParentRunner.run(ParentRunner.java:300) at
org.junit.runners.Suite.runChild(Suite.java:128) at
org.junit.runners.Suite.runChild(Suite.java:24) at
org.junit.runners.ParentRunner.run(ParentRunner.java:231) at
org.junit.runners.ParentRunner.schedule(ParentRunner.java:60) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at
org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:50) at
org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at
org.junit.runners.ParentRunner.run(ParentRunner.java:300) at
org.junit.runner.JUnitCore.run(JUnitCore.java:157) at
org.junit.runner.JUnitCore.run(JUnitCore.java:136) at
android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at
android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228)
at
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
这是使我的应用程序崩溃的行:
btnFacebook.setEnabled(false);
编辑:我找到了合适的解决方案,寻找可接受的答案。
尝试在模拟线程中调用该行:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
btnFacebook.setEnabled(false);
}
}, 100);
好的,我找到了合适的解决方案!在处理使用 Handlers 的库和 API 时,您需要使用 @UiThreadTest 注释您的测试用例。此外,您正在存根的每个异步回调都应该使用 Instrumentation 的 runOnMainSync(Runnable r) 方法调用。示例:
@Test
@UiThreadTest
public void testLoginSuccess() {
Instrumentation.ActivityMonitor monitor = InstrumentationRegistry.getInstrumentation().addMonitor(EventsListActivity.class.getName(), null, true);
onView(withId(R.id.btnLogInW)).perform(click());
onView(withId(R.id.email)).perform(typeText("good.email@example.com"));
onView(withId(R.id.passL)).perform(typeText("strong.password"));
onView(withId(R.id.btnLogInL)).perform(click());
User user = new User();
user.first_name = "Fake name";
user.last_name = "Fake name";
user.id = 1;
user.email = "fake.email@gmail.com";
final AuthResponse authResponse = new AuthResponse();
authResponse.api_key = "fake_api_key";
authResponse.status = "ok";
authResponse.user = user;
Mockito.verify(api).login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), argumentCaptor.capture());
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override public void run() {
argumentCaptor.getValue().success(authResponse, null);
}
});
assertThat(1, equalTo(monitor.getHits()));
InstrumentationRegistry.getInstrumentation().removeMonitor(monitor);
}
这是我的测试代码:
@RunWith(AndroidJUnit4.class)
@SmallTest
public class WelcomeActivityTests extends BaseTest {
ApplicationController applicationController;
@Rule
public ActivityTestRule<WelcomeActivity> activityTestRule = new ActivityTestRule<>(WelcomeActivity.class);
ArgumentCaptor<Callback> argumentCaptor;
@Before
@Override public void setUp() {
applicationController = (ApplicationController) InstrumentationRegistry.getTargetContext().getApplicationContext();
applicationController.setMockMode(true);
argumentCaptor = ArgumentCaptor.forClass(Callback.class);
super.setUp();
}
@Test
public void testLogin() throws InterruptedException {
onView(withId(R.id.btnLogInW)).perform(click());
onView(withId(R.id.email)).perform(typeText("good.email@example.com"));
onView(withId(R.id.passL)).perform(typeText("strong.password"));
onView(withId(R.id.btnLogInL)).perform(click());
User user = new User();
user.first_name = "Fake name";
user.last_name = "Fake name";
user.id = 1;
user.email = "fake.email@gmail.com";
AuthResponse authResponse = new AuthResponse();
authResponse.api_key = "fake_api_key";
authResponse.status = "ok";
authResponse.user = user;
Mockito.verify(api).login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), argumentCaptor.capture());
argumentCaptor.getValue().success(authResponse, null);
assert true;
}
@After
public void release() {
applicationController.setMockMode(false);
}
}
我使用 espresso 单击的按钮基于 material 主题,它们下面可能有一些动画。结果是,当我尝试 运行 时,每次我执行点击时,一些仪器测试应用程序都会崩溃。我尝试在系统开发人员选项中关闭动画,但没有用。
android.util.AndroidRuntimeException: Animators may only be run on Looper threads at android.animation.ValueAnimator.start(ValueAnimator.java:1002) at android.animation.ValueAnimator.start(ValueAnimator.java:1050) at android.animation.ObjectAnimator.start(ObjectAnimator.java:829) at android.animation.AnimatorSet.start(AnimatorSet.java:585) at android.animation.StateListAnimator.start(StateListAnimator.java:187) at android.animation.StateListAnimator.setState(StateListAnimator.java:180) at android.view.View.drawableStateChanged(View.java:15988) at android.widget.TextView.drawableStateChanged(TextView.java:3659) at android.view.View.refreshDrawableState(View.java:16032) at android.view.View.setEnabled(View.java:6724) at android.widget.TextView.setEnabled(TextView.java:1446) at my.app.ui.fragments.welcome.LoginFragment.unlock(LoginFragment.java:263) at my.app.ui.fragments.welcome.LoginFragment.success(LoginFragment.java:224) at my.app.ui.fragments.welcome.LoginFragment.success(LoginFragment.java:222) at my.app.WelcomeActivityTests.testRate(WelcomeActivityTests.java:84) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257) at org.junit.rules.RunRules.evaluate(RunRules.java:18) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner.run(ParentRunner.java:231) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:231) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at org.junit.runner.JUnitCore.run(JUnitCore.java:136) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
这是使我的应用程序崩溃的行:
btnFacebook.setEnabled(false);
编辑:我找到了合适的解决方案,寻找可接受的答案。
尝试在模拟线程中调用该行:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
btnFacebook.setEnabled(false);
}
}, 100);
好的,我找到了合适的解决方案!在处理使用 Handlers 的库和 API 时,您需要使用 @UiThreadTest 注释您的测试用例。此外,您正在存根的每个异步回调都应该使用 Instrumentation 的 runOnMainSync(Runnable r) 方法调用。示例:
@Test
@UiThreadTest
public void testLoginSuccess() {
Instrumentation.ActivityMonitor monitor = InstrumentationRegistry.getInstrumentation().addMonitor(EventsListActivity.class.getName(), null, true);
onView(withId(R.id.btnLogInW)).perform(click());
onView(withId(R.id.email)).perform(typeText("good.email@example.com"));
onView(withId(R.id.passL)).perform(typeText("strong.password"));
onView(withId(R.id.btnLogInL)).perform(click());
User user = new User();
user.first_name = "Fake name";
user.last_name = "Fake name";
user.id = 1;
user.email = "fake.email@gmail.com";
final AuthResponse authResponse = new AuthResponse();
authResponse.api_key = "fake_api_key";
authResponse.status = "ok";
authResponse.user = user;
Mockito.verify(api).login(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), argumentCaptor.capture());
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override public void run() {
argumentCaptor.getValue().success(authResponse, null);
}
});
assertThat(1, equalTo(monitor.getHits()));
InstrumentationRegistry.getInstrumentation().removeMonitor(monitor);
}