
org.robolectric.shadows.ShadowJobScheduler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of framework Show documentation
Show all versions of framework Show documentation
An alternative Android testing framework.
The newest version!
package org.robolectric.shadows;
import android.app.JobSchedulerImpl;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.N;
@Implements(value = JobScheduler.class, minSdk = LOLLIPOP)
public abstract class ShadowJobScheduler {
@Implementation
public abstract int schedule(JobInfo job);
@Implementation
public abstract void cancel(int jobId);
@Implementation
public abstract void cancelAll();
@Implementation
public abstract List getAllPendingJobs();
@Implementation(minSdk = N)
public abstract JobInfo getPendingJob(int jobId);
public abstract void failOnJob(int jobId);
@Implements(value = JobSchedulerImpl.class, isInAndroidSdk = false)
public static class ShadowJobSchedulerImpl extends ShadowJobScheduler {
private Map scheduledJobs = new HashMap<>();
private Set jobsToFail = new HashSet<>();
@Override @Implementation
public int schedule(JobInfo job) {
if (jobsToFail.contains(job.getId())) {
return JobScheduler.RESULT_FAILURE;
}
scheduledJobs.put(job.getId(), job);
return JobScheduler.RESULT_SUCCESS;
}
@Override @Implementation
public void cancel(int jobId) {
scheduledJobs.remove(jobId);
}
@Override @Implementation
public void cancelAll() {
scheduledJobs.clear();
}
@Override @Implementation
public List getAllPendingJobs() {
return new ArrayList<>(scheduledJobs.values());
}
@Override @Implementation(minSdk = N)
public JobInfo getPendingJob(int jobId) {
return scheduledJobs.get(jobId);
}
@Override
public void failOnJob(int jobId) {
jobsToFail.add(jobId);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy