public void testWritingTaskWithExtras() throws Exception {
    JobInfo.Builder b =
        new Builder(8, mComponent)
            .setRequiresDeviceIdle(true)
            .setPeriodic(10000L)
            .setRequiresCharging(true)
            .setPersisted(true);

    PersistableBundle extras = new PersistableBundle();
    extras.putDouble("hello", 3.2);
    extras.putString("hi", "there");
    extras.putInt("into", 3);
    b.setExtras(extras);
    final JobInfo task = b.build();
    JobStatus taskStatus = JobStatus.createFromJobInfo(task, SOME_UID, null, -1, null);

    mTaskStoreUnderTest.add(taskStatus);
    Thread.sleep(IO_WAIT);

    final JobSet jobStatusSet = new JobSet();
    mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet);
    assertEquals("Incorrect # of persisted tasks.", 1, jobStatusSet.size());
    JobStatus loaded = jobStatusSet.getAllJobs().iterator().next();
    assertTasksEqual(task, loaded.getJob());
  }
 public void testPriorityPersisted() throws Exception {
   JobInfo.Builder b =
       new Builder(92, mComponent).setOverrideDeadline(5000).setPriority(42).setPersisted(true);
   final JobStatus js = JobStatus.createFromJobInfo(b.build(), SOME_UID, null, -1, null);
   mTaskStoreUnderTest.add(js);
   Thread.sleep(IO_WAIT);
   final JobSet jobStatusSet = new JobSet();
   mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet);
   JobStatus loaded = jobStatusSet.getAllJobs().iterator().next();
   assertEquals("Priority not correctly persisted.", 42, loaded.getPriority());
 }
 /** Test that non persisted job is not written to disk. */
 public void testNonPersistedTaskIsNotPersisted() throws Exception {
   JobInfo.Builder b = new Builder(42, mComponent).setOverrideDeadline(10000).setPersisted(false);
   JobStatus jsNonPersisted = JobStatus.createFromJobInfo(b.build(), SOME_UID, null, -1, null);
   mTaskStoreUnderTest.add(jsNonPersisted);
   b = new Builder(43, mComponent).setOverrideDeadline(10000).setPersisted(true);
   JobStatus jsPersisted = JobStatus.createFromJobInfo(b.build(), SOME_UID, null, -1, null);
   mTaskStoreUnderTest.add(jsPersisted);
   Thread.sleep(IO_WAIT);
   final JobSet jobStatusSet = new JobSet();
   mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet);
   assertEquals("Job count is incorrect.", 1, jobStatusSet.size());
   JobStatus jobStatus = jobStatusSet.getAllJobs().iterator().next();
   assertEquals("Wrong job persisted.", 43, jobStatus.getJobId());
 }
  public void testMassivePeriodClampedOnRead() throws Exception {
    final long ONE_HOUR = 60 * 60 * 1000L; // flex
    final long TWO_HOURS = 2 * ONE_HOUR; // period
    JobInfo.Builder b =
        new Builder(8, mComponent).setPeriodic(TWO_HOURS, ONE_HOUR).setPersisted(true);
    final long invalidLateRuntimeElapsedMillis =
        SystemClock.elapsedRealtime() + (TWO_HOURS * ONE_HOUR) + TWO_HOURS; // > period+flex
    final long invalidEarlyRuntimeElapsedMillis =
        invalidLateRuntimeElapsedMillis - TWO_HOURS; // Early is (late - period).
    final JobStatus js =
        new JobStatus(
            b.build(),
            SOME_UID,
            "somePackage",
            0 /* sourceUserId */,
            "someTag",
            invalidEarlyRuntimeElapsedMillis,
            invalidLateRuntimeElapsedMillis);

    mTaskStoreUnderTest.add(js);
    Thread.sleep(IO_WAIT);

    final JobSet jobStatusSet = new JobSet();
    mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet);
    assertEquals("Incorrect # of persisted tasks.", 1, jobStatusSet.size());
    JobStatus loaded = jobStatusSet.getAllJobs().iterator().next();

    // Assert early runtime was clamped to be under now + period. We can do <= here b/c we'll
    // call SystemClock.elapsedRealtime after doing the disk i/o.
    final long newNowElapsed = SystemClock.elapsedRealtime();
    assertTrue(
        "Early runtime wasn't correctly clamped.",
        loaded.getEarliestRunTime() <= newNowElapsed + TWO_HOURS);
    // Assert late runtime was clamped to be now + period + flex.
    assertTrue(
        "Early runtime wasn't correctly clamped.",
        loaded.getEarliestRunTime() <= newNowElapsed + TWO_HOURS + ONE_HOUR);
  }
  public static void schedule(Context ctx, long delay) {
    synchronized (KeyValueBackupJob.class) {
      if (!sScheduled) {
        if (delay <= 0) {
          delay = BATCH_INTERVAL + new Random().nextInt(FUZZ_MILLIS);
        }
        if (BackupManagerService.DEBUG_SCHEDULING) {
          Slog.v(TAG, "Scheduling k/v pass in " + (delay / 1000 / 60) + " minutes");
        }
        JobScheduler js = (JobScheduler) ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE);
        JobInfo.Builder builder =
            new JobInfo.Builder(JOB_ID, sKeyValueJobService)
                .setMinimumLatency(delay)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setRequiresCharging(true)
                .setOverrideDeadline(MAX_DEFERRAL);
        js.schedule(builder.build());

        sNextScheduled = System.currentTimeMillis() + delay;
        sScheduled = true;
      }
    }
  }
  public void testWritingTaskWithFlex() throws Exception {
    JobInfo.Builder b =
        new Builder(8, mComponent)
            .setRequiresDeviceIdle(true)
            .setPeriodic(5 * 60 * 60 * 1000, 1 * 60 * 60 * 1000)
            .setRequiresCharging(true)
            .setPersisted(true);
    JobStatus taskStatus = JobStatus.createFromJobInfo(b.build(), SOME_UID, null, -1, null);

    mTaskStoreUnderTest.add(taskStatus);
    Thread.sleep(IO_WAIT);

    final JobSet jobStatusSet = new JobSet();
    mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet);
    assertEquals("Incorrect # of persisted tasks.", 1, jobStatusSet.size());
    JobStatus loaded = jobStatusSet.getAllJobs().iterator().next();
    assertEquals(
        "Period not equal.",
        loaded.getJob().getIntervalMillis(),
        taskStatus.getJob().getIntervalMillis());
    assertEquals(
        "Flex not equal.", loaded.getJob().getFlexMillis(), taskStatus.getJob().getFlexMillis());
  }
  public void testWritingTaskWithSourcePackage() throws Exception {
    JobInfo.Builder b =
        new Builder(8, mComponent)
            .setRequiresDeviceIdle(true)
            .setPeriodic(10000L)
            .setRequiresCharging(true)
            .setPersisted(true);
    JobStatus taskStatus =
        JobStatus.createFromJobInfo(b.build(), SOME_UID, "com.google.android.gms", 0, null);

    mTaskStoreUnderTest.add(taskStatus);
    Thread.sleep(IO_WAIT);

    final JobSet jobStatusSet = new JobSet();
    mTaskStoreUnderTest.readJobMapFromDisk(jobStatusSet);
    assertEquals("Incorrect # of persisted tasks.", 1, jobStatusSet.size());
    JobStatus loaded = jobStatusSet.getAllJobs().iterator().next();
    assertEquals(
        "Source package not equal.",
        loaded.getSourcePackageName(),
        taskStatus.getSourcePackageName());
    assertEquals("Source user not equal.", loaded.getSourceUserId(), taskStatus.getSourceUserId());
  }