/
BaseMemoryLeaksTest.java
170 lines (156 loc) · 6.19 KB
/
BaseMemoryLeaksTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.robolectric.integrationtests.memoryleaks;
import static android.os.Build.VERSION_CODES.N;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Shadows.shadowOf;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.os.Looper;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentContainerView;
import com.google.common.testing.GcFinalization;
import java.lang.ref.WeakReference;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;
import org.robolectric.res.android.Registries;
import org.robolectric.util.ReflectionHelpers;
/**
* A test that verifies that activities and fragments become GC candidates after being destroyed, or
* after a test terminates.
*
* <p>For internal reasons, this class is subclassed rather than inlining {@link #assertNotLeaking}
* in this file.
*/
@RunWith(RobolectricTestRunner.class)
@Config(sdk = Config.ALL_SDKS)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public abstract class BaseMemoryLeaksTest {
private static WeakReference<Activity> awr = null;
@Test
// Do not shard these two tests, they must run on the same machine sequentially.
// These tests are prefixed with aaa_ to ensure they run first. Some leaks are caused by
// static setup that occurs once at the start of a test class, so these must be the first tests
// that run.
public void aaa_activityCanBeGcdBetweenTest_1() {
assertThat(awr).isNull();
ActivityController<Activity> ac = Robolectric.buildActivity(Activity.class).setup();
awr = new WeakReference<>(ac.get());
}
@Test
// Do not shard these two tests, they must run on the same machine sequentially.
// These tests are prefixed with aaa_ to ensure they run first. Some leaks are caused by
// static setup that occurs once at the start of a test class, so these must be the first tests
// that run.
public void aaa_activityCanBeGcdBetweenTest_2() {
assertThat(awr).isNotNull();
assertNotLeaking(awr::get);
}
@Test
public void activityCanBeGcdAfterDestroyed() {
assertNotLeaking(
() -> {
ActivityController<Activity> ac = Robolectric.buildActivity(Activity.class).setup();
Activity activity = ac.get();
ac.pause().stop().destroy();
return activity;
});
}
@Test
public void activityCanBeGcdAfterConfigChange() {
assertNotLeaking(
() -> {
ActivityController<Activity> ac = Robolectric.buildActivity(Activity.class).setup();
Activity activity = ac.get();
Configuration currentConfiguration = activity.getResources().getConfiguration();
Configuration newConfiguration = new Configuration(currentConfiguration);
newConfiguration.orientation =
currentConfiguration.orientation == Configuration.ORIENTATION_LANDSCAPE
? Configuration.ORIENTATION_PORTRAIT
: Configuration.ORIENTATION_LANDSCAPE;
ac.configurationChange(newConfiguration);
return activity;
});
}
@Test
public void fragmentCanBeGcdAfterActivityDestroyed() {
assertNotLeaking(
() -> {
ActivityController<FragmentActivity> ac =
Robolectric.buildActivity(FragmentActivity.class).setup();
FragmentContainerView contentView = new FragmentContainerView(ac.get());
contentView.setId(android.R.id.list_container);
ac.get().setContentView(contentView);
Fragment f = new Fragment();
ac.get()
.getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.list_container, f)
.commitNow();
ac.pause().stop().destroy();
// Idle any potential Fragment animations.
shadowOf(Looper.getMainLooper()).idle();
return f;
});
}
@Test
public void fragmentCanBeGcdAfterRemoved() {
assertNotLeaking(
() -> {
ActivityController<FragmentActivity> ac =
Robolectric.buildActivity(FragmentActivity.class).setup();
FragmentContainerView contentView = new FragmentContainerView(ac.get());
contentView.setId(android.R.id.list_container);
ac.get().setContentView(contentView);
Fragment f = new Fragment();
ac.get()
.getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.list_container, f)
.commitNow();
ac.get()
.getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.list_container, new Fragment())
.commitNow();
return f;
});
}
@Test
public void typedArrayData() {
assertNotLeaking(
() -> {
Context context = RuntimeEnvironment.getApplication();
TypedArray typedArray = context.obtainStyledAttributes(new int[] {});
return ReflectionHelpers.getField(typedArray, "mData");
});
}
@Test
@Config(minSdk = N)
public void themeObjectInNativeObjectRegistry() {
final AtomicLong themeId = new AtomicLong(0);
assertNotLeaking(
() -> {
Theme theme = RuntimeEnvironment.getApplication().getResources().newTheme();
long nativeId =
ReflectionHelpers.getField(ReflectionHelpers.getField(theme, "mThemeImpl"), "mTheme");
themeId.set(nativeId);
return theme;
});
// Also wait for the theme to be cleared from the registry.
GcFinalization.awaitDone(
() -> Registries.NATIVE_THEME9_REGISTRY.peekNativeObject(themeId.get()) == null);
}
public abstract <T> void assertNotLeaking(Callable<T> potentiallyLeakingCallable);
}