Enable strict mode to detect leaks of SQLite objects
In particular this will detect leaked Cursors.
@@ -21,6 +21,7 @@ import static junit.framework.Assert.fail;
import android.content.Context;
import android.net.SSLCertificateSocketFactory;
+import android.os.StrictMode;
import android.support.test.InstrumentationRegistry;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
@@ -84,6 +85,7 @@ public class IntegrationTestUtil {
private static final FirestoreProvider provider = new FirestoreProvider();
+ private static boolean strictModeEnabled = false;
private static boolean backendPrimed = false;
public static FirestoreProvider provider() {
@@ -202,6 +204,27 @@ public class IntegrationTestUtil {
return testFirestore(BAD_PROJECT_ID, Level.DEBUG, newTestSettings());
}
+ /**
+ * Enable strict mode for integration tests. Currently checks for leaked SQLite or other Closeable
+ * objects.
+ *
+ * <p>If a leak is found, Android will log the leak and kill the test.
+ */
+ private static void ensureStrictMode() {
+ if (strictModeEnabled) {
+ return;
+ }
+
+ strictModeEnabled = true;
+ StrictMode.setVmPolicy(
+ new StrictMode.VmPolicy.Builder()
+ .detectLeakedSqlLiteObjects()
+ .detectLeakedClosableObjects()
+ .penaltyLog()
+ .penaltyDeath()
+ .build());
+ }
+
private static void clearPersistence(
Context context, DatabaseId databaseId, String persistenceKey) {
@SuppressWarnings("VisibleForTests")
@@ -228,6 +251,7 @@ public class IntegrationTestUtil {
DatabaseId databaseId = DatabaseId.forDatabase(projectId, DatabaseId.DEFAULT_DATABASE_ID);
String persistenceKey = "db" + firestoreStatus.size();
+ ensureStrictMode();
clearPersistence(context, databaseId, persistenceKey);
AsyncQueue asyncQueue = null;
