Bug 1476237 - Fix TestFileCleanupController after the Oreo migration; r?sdaswani
This tests verified if a Service was started or not. After the IntentServices
migration to JobIntentService this checks would not work anymore.
To keep the same test logic they will now check if JobScheduler has any
scheduled jobs.
Removed testStartIfReadyDoesNotRunTwiceInSuccession() because JobScheduler
already guarantees no two identical jobs can be scheduled in parallel.
MozReview-Commit-ID: JZ2XMecjsXq
--- a/mobile/android/app/src/test/java/org/mozilla/gecko/cleanup/TestFileCleanupController.java
+++ b/mobile/android/app/src/test/java/org/mozilla/gecko/cleanup/TestFileCleanupController.java
@@ -1,76 +1,60 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.mozilla.gecko.cleanup;
+import android.app.job.JobScheduler;
import android.content.Context;
-import android.content.Intent;
import android.content.SharedPreferences;
+
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
import java.util.ArrayList;
+import java.util.Objects;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.any;
-import static org.mockito.Mockito.atMost;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
/**
* Tests functionality of the {@link FileCleanupController}.
*/
@RunWith(RobolectricTestRunner.class)
+@Config(manifest = Config.NONE, sdk = 26)
public class TestFileCleanupController {
@Test
public void testStartIfReadyEmptySharedPrefsRunsCleanup() {
- final Context context = mock(Context.class);
- FileCleanupController.startIfReady(context, getSharedPreferences(), "");
- verify(context).startService(any(Intent.class));
+ final Context appContext = getAppContext();
+ final JobScheduler scheduler = getJobScheduler();
+
+ FileCleanupController.startIfReady(appContext, getSharedPreferences(), "");
+ assertTrue("File cleanup is not started", scheduler.getAllPendingJobs().size() == 1);
}
@Test
public void testStartIfReadyLastRunNowDoesNotRun() {
+ final Context appContext = getAppContext();
+ final JobScheduler scheduler = getJobScheduler();
final SharedPreferences sharedPrefs = getSharedPreferences();
sharedPrefs.edit()
.putLong(FileCleanupController.PREF_LAST_CLEANUP_MILLIS, System.currentTimeMillis())
.commit(); // synchronous to finish before test runs.
- final Context context = mock(Context.class);
- FileCleanupController.startIfReady(context, sharedPrefs, "");
-
- verify(context, never()).startService((any(Intent.class)));
- }
+ FileCleanupController.startIfReady(appContext, sharedPrefs, "");
- /**
- * Depends on {@link #testStartIfReadyEmptySharedPrefsRunsCleanup()} success –
- * i.e. we expect the cleanup to run with empty prefs.
- */
- @Test
- public void testStartIfReadyDoesNotRunTwiceInSuccession() {
- final Context context = mock(Context.class);
- final SharedPreferences sharedPrefs = getSharedPreferences();
-
- FileCleanupController.startIfReady(context, sharedPrefs, "");
- verify(context).startService(any(Intent.class));
-
- // Note: the Controller relies on SharedPrefs.apply, but
- // robolectric made this a synchronous call. Yay!
- FileCleanupController.startIfReady(context, sharedPrefs, "");
- verify(context, atMost(1)).startService(any(Intent.class));
+ assertTrue("There is a job scheduled", scheduler.getAllPendingJobs().size() == 0);
}
@Test
public void testGetFilesToCleanupContainsProfilePath() {
final String profilePath = "/a/profile/path";
final ArrayList<String> fileList = FileCleanupController.getFilesToCleanup(profilePath);
assertNotNull("Returned file list is non-null", fileList);
@@ -82,11 +66,20 @@ public class TestFileCleanupController {
// we could display the Strings in console output.
atLeastOneStartsWithProfilePath = true;
}
}
assertTrue("At least one returned String starts with a profile path", atLeastOneStartsWithProfilePath);
}
private SharedPreferences getSharedPreferences() {
- return RuntimeEnvironment.application.getSharedPreferences("TestFileCleanupController", 0);
+ return getAppContext().getSharedPreferences("TestFileCleanupController", 0);
+ }
+
+ private Context getAppContext() {
+ return RuntimeEnvironment.application;
+ }
+
+ private JobScheduler getJobScheduler() {
+ return Objects.requireNonNull((JobScheduler)
+ getAppContext().getSystemService(Context.JOB_SCHEDULER_SERVICE));
}
}