2

With robolectric 3.3, I am trying to get the package manager to return correct values for queryIntentServices so that the Firebase Job Dispatcher works.

In my AndroidManifest.xml I have:

<service
    android:name="com.jongla.soundmash.service.SoundMashService"
    android:exported="false">
    <intent-filter>
      <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
    </intent-filter>
</service>

And in my test application class that Robolectric invokes I have put:

val executeIntent = Intent("com.firebase.jobdispatcher.ACTION_EXECUTE")
executeIntent.setClassName(RuntimeEnvironment.application, "com.jongla.soundmash.service.SoundMashService")
Shadows.shadowOf(packageManager)
  .addResolveInfoForIntent(executeIntent, ResolveInfo().apply { serviceInfo = ServiceInfo().apply { enabled = true } })

However, Firebase is still throwing this error:

com.firebase.jobdispatcher.ValidationEnforcer$ValidationException: JobParameters is invalid: Couldn't find a registered service with the name
com.jongla.soundmash.service.SoundMashService. Is it declared in the manifest with the right intent-filter?

The relevant code in the Firebase project is below. It looks really straightforward, so I don't think it's a bug on their side.

PackageManager pm = context.getPackageManager();
if (pm == null) {
    return getMutableSingletonList("PackageManager is null, can't validate service");
}

final String msg = "Couldn't find a registered service with the name " + service
    + ". Is it declared in the manifest with the right intent-filter?";

Intent executeIntent = new Intent(JobService.ACTION_EXECUTE);
executeIntent.setClassName(context, service);
List<ResolveInfo> intentServices = pm.queryIntentServices(executeIntent, 0);
if (intentServices == null || intentServices.isEmpty()) {
    return getMutableSingletonList(msg);
}

for (ResolveInfo info : intentServices) {
    if (info.serviceInfo != null && info.serviceInfo.enabled) {
// found a match!
return null;
    }
}

Am I doing something wrong w/ my usage of Robolectric?

mikesol
  • 1,177
  • 1
  • 11
  • 20
  • You can improve your question to be useful to other readers if you edit your code to show the enclosing class and method for the Java code. – Code-Apprentice Jun 19 '17 at 19:22

1 Answers1

2

Answering my own question...the calling code needs to be in onCreate and not beforeTest - then it works just fine!

mikesol
  • 1,177
  • 1
  • 11
  • 20