DefaultJobValidator implements JobValidator { @VisibleForTesting List<String> validateService(String service) { if (service == null || service.isEmpty()) { return getMutableSingletonList("Service can't be empty"); } if (context == null) { return getMutableSingletonList("Context is null, can't query PackageManager"); } PackageManager pm = context.getPackageManager(); if (pm == null) { return getMutableSingletonList("PackageManager is null, can't validate service"); } Intent executeIntent = new Intent(JobService.ACTION_EXECUTE); executeIntent.setClassName(context, service); List<ResolveInfo> intentServices = pm.queryIntentServices(executeIntent, 0); if (intentServices == null || intentServices.isEmpty()) { String msg = "Couldn't find a registered service with the name " + service + ". Is it declared in the manifest with the right intent-filter?" + " If not, the job won't be started."; Log.e(TAG, msg); return null; } for (ResolveInfo info : intentServices) { if (info.serviceInfo != null && info.serviceInfo.enabled) { return null; } } return getMutableSingletonList(service + " is disabled."); } DefaultJobValidator(@NonNull Context context); @Nullable @Override @CallSuper List<String> validate(@NonNull JobParameters job); @Nullable @Override @CallSuper List<String> validate(@NonNull JobTrigger trigger); @Nullable @Override @CallSuper List<String> validate(@NonNull RetryStrategy retryStrategy); static final int MAX_TAG_LENGTH; static final int MAX_EXTRAS_SIZE_BYTES; }
@Test public void validateService_packageManagerIsNotAvailable() { when(mockContext.getPackageManager()).thenReturn(null); List<String> errors = validator.validateService("service"); assertTrue(errors.contains("PackageManager is null, can't validate service")); }
@Test public void validateService_noSuchService() { when(mockContext.getPackageManager()).thenReturn(packageManagerMock); when(packageManagerMock.queryIntentServices(any(Intent.class), eq(0))) .thenReturn(Collections.<ResolveInfo>emptyList()); when(mockContext.getPackageName()).thenReturn("package"); List<String> errors = validator.validateService("service"); assertNull(errors); verify(packageManagerMock).queryIntentServices(intentCaptor.capture(), eq(0)); Intent intent = intentCaptor.getValue(); assertEquals(JobService.ACTION_EXECUTE, intent.getAction()); assertEquals(new ComponentName("package", "service"), intent.getComponent()); }
@Test public void validateService_noActiveService() { when(mockContext.getPackageManager()).thenReturn(packageManagerMock); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.serviceInfo = new ServiceInfo(); resolveInfo.serviceInfo.enabled = false; when(packageManagerMock.queryIntentServices(any(Intent.class), eq(0))) .thenReturn(Arrays.asList(resolveInfo)); when(mockContext.getPackageName()).thenReturn("package"); List<String> errors = validator.validateService("service"); assertTrue(errors.contains("service is disabled.")); }
@Test public void validateService() { when(mockContext.getPackageManager()).thenReturn(packageManagerMock); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.serviceInfo = new ServiceInfo(); resolveInfo.serviceInfo.enabled = true; when(packageManagerMock.queryIntentServices(any(Intent.class), eq(0))) .thenReturn(Arrays.asList(resolveInfo)); when(mockContext.getPackageName()).thenReturn("package"); List<String> errors = validator.validateService("service"); assertNull(errors); }
@Test public void validateService_null() { List<String> errors = validator.validateService(null); assertTrue(errors.contains("Service can't be empty")); }
@Test public void validateService_empty() { List<String> errors = validator.validateService(""); assertTrue(errors.contains("Service can't be empty")); }