Skip to content

Commit 0964b05

Browse files
whymmaskara
authored andcommitted
Disable LeakCanary for Robolectric tests
See 'For Robolectric users:' https://github.com/square/leakcanary
1 parent 3adb5d8 commit 0964b05

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

app/src/main/java/fr/free/nrw/commons/CommonsApplication.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.facebook.drawee.backends.pipeline.Fresco;
1818
import com.facebook.stetho.Stetho;
1919
import com.squareup.leakcanary.LeakCanary;
20+
import com.squareup.leakcanary.RefWatcher;
2021

2122
import org.acra.ACRA;
2223
import org.acra.ReportingInteractionMode;
@@ -117,12 +118,8 @@ public synchronized NearbyPlaces getNearbyPlaces() {
117118
@Override
118119
public void onCreate() {
119120
super.onCreate();
120-
if (LeakCanary.isInAnalyzerProcess(this)) {
121-
// This process is dedicated to LeakCanary for heap analysis.
122-
// You should not init your app in this process.
123-
return;
124-
}
125-
LeakCanary.install(this);
121+
122+
setupLeakCanary();
126123

127124
Timber.plant(new Timber.DebugTree());
128125

@@ -143,6 +140,13 @@ public void onCreate() {
143140
cacheData = new CacheController();
144141
}
145142

143+
protected RefWatcher setupLeakCanary() {
144+
if (LeakCanary.isInAnalyzerProcess(this)) {
145+
return RefWatcher.DISABLED;
146+
}
147+
return LeakCanary.install(this);
148+
}
149+
146150
/**
147151
* @return Account|null
148152
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package fr.free.nrw.commons;
2+
3+
import com.squareup.leakcanary.RefWatcher;
4+
5+
// This class is automatically discovered by Robolectric
6+
public class TestCommonsApplication extends CommonsApplication {
7+
@Override protected RefWatcher setupLeakCanary() {
8+
// No leakcanary in unit tests.
9+
return RefWatcher.DISABLED;
10+
}
11+
}

0 commit comments

Comments
 (0)