How to check if IntentService is running

I am trying to test the behavior of my application using Espresso-2.2

In the main mode, when the button is pressed, both service and other actions are simultaneously launched:

public class MainActivity extends Activity { public void onButtonClicked() { startActivity(SecondActivity.getStartIntent()); startService(MyIntentService.getStartIntent()); } } 

I am testing if the target components are running:

 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 get an 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) 

Registered start of SecondActivity. Why is the beginning of my IntentService not registered (I checked that it is running)?

+6
source share
2 answers

tl; dr it doesn’t look as if it were possible, given the current espresso intentions provided

I dug in the source code since I am trying to do something like this, and here is what I found:

The β€œintended” assistant works by looking at the list of recorded intentions. They are tracked by a callback that registers with the IntentMonitor instance when Intents.init () is called. The IntentMonitor instance is determined by the current Instrumentation. Each time IntentMonitor signalIntent () is called, the callback in Intents will be fired.

The problem is that signalIntent () never calls when you call activity.startService () with the intent. This is due to the fact that signalIntent () in the case of AndroidJunitRunner is called only by methods that are mapped to ExposedInstrumentationApi (which, as a rule, only correlate with various startActivity () methods). To use espresso-intents with startService (), it would seem that the tool hook for startService () would have to be available, which would allow signalIntent () to be called on IntentMonitor.

I have no experience adding custom Instrumentation to my test apk, but this will be my next research path. I will update my answer if I find anything that works.

+10
source

I could suggest that the problem is that you are binding the statement that an IntentService is created and started right after the startService() call, while startService() not a synchronization call. this happens in the user interface thread, but not immediately. You can defer checking by several rooms and check if the service has started.

0
source

All Articles