21
21
22
22
import android .content .Context ;
23
23
import android .net .SSLCertificateSocketFactory ;
24
+ import android .os .StrictMode ;
24
25
import android .support .test .InstrumentationRegistry ;
25
26
import com .google .android .gms .tasks .Task ;
26
27
import com .google .android .gms .tasks .TaskCompletionSource ;
@@ -84,6 +85,7 @@ public class IntegrationTestUtil {
84
85
85
86
private static final FirestoreProvider provider = new FirestoreProvider ();
86
87
88
+ private static boolean strictModeEnabled = false ;
87
89
private static boolean backendPrimed = false ;
88
90
89
91
public static FirestoreProvider provider () {
@@ -202,6 +204,27 @@ public static FirebaseFirestore testAlternateFirestore() {
202
204
return testFirestore (BAD_PROJECT_ID , Level .DEBUG , newTestSettings ());
203
205
}
204
206
207
+ /**
208
+ * Enable strict mode for integration tests. Currently checks for leaked SQLite or other Closeable
209
+ * objects.
210
+ *
211
+ * <p>If a leak is found, Android will log the leak and kill the test.
212
+ */
213
+ private static void ensureStrictMode () {
214
+ if (strictModeEnabled ) {
215
+ return ;
216
+ }
217
+
218
+ strictModeEnabled = true ;
219
+ StrictMode .setVmPolicy (
220
+ new StrictMode .VmPolicy .Builder ()
221
+ .detectLeakedSqlLiteObjects ()
222
+ .detectLeakedClosableObjects ()
223
+ .penaltyLog ()
224
+ .penaltyDeath ()
225
+ .build ());
226
+ }
227
+
205
228
private static void clearPersistence (
206
229
Context context , DatabaseId databaseId , String persistenceKey ) {
207
230
@ SuppressWarnings ("VisibleForTests" )
@@ -228,6 +251,7 @@ public static FirebaseFirestore testFirestore(
228
251
DatabaseId databaseId = DatabaseId .forDatabase (projectId , DatabaseId .DEFAULT_DATABASE_ID );
229
252
String persistenceKey = "db" + firestoreStatus .size ();
230
253
254
+ ensureStrictMode ();
231
255
clearPersistence (context , databaseId , persistenceKey );
232
256
233
257
AsyncQueue asyncQueue = null ;
0 commit comments