测试不能执行点击而不能记录意图

时间:2016-06-22 07:40:30

标签: android unit-testing android-espresso

创建测试时遇到两个问题:

1 - 当尝试测试触发我的活动的onClick事件按钮打开一个新活动时,我看到该活动在我的模拟器/设备中打开。但是,测试无法提供无法执行onclick的跟踪。

我的代码:

select t.*
from t
where t.changeno = (select max(t2.changeno)
                    from t t2
                    where t2.bom = t.bom and t2.altbom = t.altbom
                   );

测试:

@OnClick(R.id.my_activity_next_activity_button)
public void onNextActivityButtonClick(){
    Intent intent = new Intent(this, StreamingActivity.class);
    startActivity(intent);
}

我也试过使用InstrumentationTestCase但结果相同:

@RunWith(AndroidJUnit4.class)
public class MyActivityTest{

   @Rule
    public IntentsTestRule<MyActivity> rule = new IntentsTestRule<>(MyActivity.class);

    @Test
    public void testButtonOnClickShouldOpenNewActivity(){

        onView(withId(R.id.my_activity_next_activity_button)).perform(click());
        Intents.intended(IntentMatchers.hasComponent(NewActivity.class.getName()));

    }
} 

我得到的痕迹:

   @Test
    public void testPickUpButtonShouldEmitPickUpIntentAndChangeStateToInitial(){

        Instrumentation.ActivityMonitor activityMonitor = new Instrumentation.ActivityMonitor(StreamingActivity.class.getName(), null, false);
        getInstrumentation().addMonitor(activityMonitor);

        onView(withId(R.id.activity_incoming_call_button_pick_up)).perform(click());

        Activity activity = activityMonitor.waitForActivityWithTimeout(5000);

        assertNotNull(activity);

    }

2 - 我发送测试意图的问题:

当我试图测试我发送意图时(无论是用于启动活动还是通过LocalBroadcast发送活动),我找不到我的测试记录它的方式:

的活动:

android.support.test.espresso.PerformException: Error performing 'single click - At Coordinates: 788, 1688 and precision: 16, 16' on view 'with id: com.entelgy.telemadrid.haztutele:id/activity_incoming_call_button_pick_up'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:80)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.entelgy.telemadrid.haztutele.ActivitiesTests.IncomingCallActivityTest.testPickUpButtonShouldEmitPickUpIntentAndChangeStateToInitial(IncomingCallActivityTest.java:53)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 3624 iterations over 60 SECONDS. The following Idle Conditions failed .
at android.support.test.espresso.IdlingPolicy.handleTimeout(IdlingPolicy.java:61)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:480)
at android.support.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:411)
at android.support.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:229)
at android.support.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:138)
at android.support.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:118)
at android.support.test.espresso.action.Tap.sendSingleTap(Tap.java:135)
at android.support.test.espresso.action.Tap.access$100(Tap.java:35)
at android.support.test.espresso.action.Tap$1.sendTap(Tap.java:40)
at android.support.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:98)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

测试:

 @OnClick(R.id.my_activity_button_send_intent)
 public void onIntentButtonClick(){
    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Constants.INTENT_DEFAULT));
 }

跟踪:

    @Test
    public void testOnButtonClickShouldSendIntent(){
        onView(withId(R.id.my_activity_button_send_intent)).perform(click());
        Intents.intended(IntentMatchers.hasComponent(Constants.INTENT_DEFAULT));
    }

你能指导我做错的事吗?

1 个答案:

答案 0 :(得分:0)

出现第一个问题是因为某些东西正在UI线程上工作。例如。 first second second second second second ... 在ui线程启动时(StreamingActivity / onCreate / onStart)或其他onResume(启动Activity时工作阻止ui线程,例如在Intent / onPause

是否有任何活动

  1. onStop中的任何ProgressBar中显示不确定的Activities(例如onCreate() / onPause / onStop)?
  2. 显示任何类型的动画或DrawableAnimation
  3. 在ui线程上工作?
  4. 第二个问题。首先,意图匹配是错误的,hasComponent()用于检查要启动的正确组件Intent(例如Activity)。您可以使用完全限定名称:

    intended(hasComponent(StreamingActivity.class.getName());
    

    目前尚不清楚,因为您发布的并非所有代码,但如果testOnButtonClickShouldSendIntent()测试从问题1开始StreamingActivity,那么ui阻塞问题也会阻止记录意图。