Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify IntentService start

I'm trying to test my app behavior with Espresso-2.2

On main activity, when button pressed both service and another activity is being started:

public class MainActivity extends Activity {

    public void onButtonClicked() {
        startActivity(SecondActivity.getStartIntent());
        startService(MyIntentService.getStartIntent());
    }
}

I'm testing if intended components are being started:

public class MainActivityTest {
    @Rule
    public final IntentsTestRule<MainActivity> intentsRule = new IntentsTestRule<>(MainActivity.class, true);

    @Test
    public void shouldStartServiceOnButtonClicked() {
        onView(withId(R.id.button)).perform(click());
        intended(hasComponent(hasClassName(SecondActivity.class.getName())));
        intended(hasComponent(hasClassName(MyIntentService.class.getName())));
    }
}

but I'm getting error:

Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: has component: has component with: class name: is "com.example.MyIntentService" package name: an instance of java.lang.String short class name: an instance of java.lang.String

Matched intents:[]

Recorded intents:
-Intent { cmp=com.example/.SecondActivity (has extras) } handling packages:[[com.example]], extras:[Bundle[...]])
at junit.framework.Assert.fail(Assert.java:50)

Start of SecondActivity is registered. Why start of my IntentService is not registered (I checked it is started)?

like image 855
Kamil Seweryn Avatar asked Jul 20 '15 15:07

Kamil Seweryn


People also ask

How can I tell if IntentService is running?

Show activity on this post. In that case you can use a status field in broadcast receiver and change it's value according to service status. Whenever your activity gets focus you can check for this value.

How do I start IntentService?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, when user gets the data from intent service it will update.

How do I use IntentService?

To use it, extend IntentService and implement onHandleIntent(android. content. Intent) . IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

Does IntentService run on background thread?

If the Background task is to be performed for a long time we can use the intent service. Service will always run on the main thread. intent service always runs on the worker thread triggered from the main thread.


1 Answers

tl;dr it doesn't appear as if this is possible given the current espresso-intents provided functionality

I dug around in the source code since I'm trying to do something similar and this is what I found:

the 'intended' matcher works by looking at a list of recorded Intents. These are tracked by a callback that gets registered with the IntentMonitor instance when Intents.init() is called. The IntentMonitor instance is defined by the current Instrumentation. Any time the IntentMonitor's signalIntent() is called, the callback in Intents will be fired.

The problem lies in the fact that signalIntent() never actually gets called in the case where you call activity.startService() with an intent. This is because signalIntent() in the AndroidJunitRunner case only gets called by methods that are exposed on ExposedInstrumentationApi (which happen to correlate only to the various startActivity() methods). In order to use espresso-intents with startService() it would appear that an instrumentation hook for startService() would need to be available that would allow for calling signalIntent() on the IntentMonitor.

I don't have experience with adding custom Instrumentation to my test apks, but this is going to be my next avenue of investigation. I'll update my answer if I discover anything that works.

like image 58
jdonmoyer Avatar answered Oct 04 '22 05:10

jdonmoyer