mercredi 22 juin 2016

Test cannot perform click and not recording intents

I have two problems while creating my tests:

1 - When trying to test that triggering the onClick event of my activity button opens a new activity, I see that the activity is opened in my emulator/device. However, the test fails giving the trace that cannot perform the onclick.

My code:

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

Test:

@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()));

    }
} 

I also tried to use InstrumentationTestCase but with same results:

   @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);

    }

The traces I get:

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)

2 - Problem with testing intents I send:

When trying to test that I'm sending an intent (no matter if it is for starting an activity or sending one via LocalBroadcast), I cannot find the way that my test records it:

Activity:

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

Test:

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

And trace:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: has component: has component with: class name: is "com.testproject.disframus:INTENT_DEFAULT" package name: an instance of java.lang.String short class name: an instance of java.lang.String

Matched intents:[]

Recorded intents:[]

Can you guide me on what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire