5
5
import android .os .Handler ;
6
6
import android .os .HandlerThread ;
7
7
import android .os .Process ;
8
- import android .test .AndroidTestCase ;
8
+
9
+ import androidx .test .ext .junit .runners .AndroidJUnit4 ;
10
+ import androidx .test .filters .SmallTest ;
11
+ import androidx .test .platform .app .InstrumentationRegistry ;
9
12
10
13
import com .mixpanel .android .util .Base64Coder ;
11
14
import com .mixpanel .android .util .HttpService ;
14
17
import org .json .JSONArray ;
15
18
import org .json .JSONException ;
16
19
import org .json .JSONObject ;
20
+ import org .junit .After ;
21
+ import org .junit .Before ;
22
+ import org .junit .Test ;
23
+ import org .junit .runner .RunWith ;
24
+ import static org .junit .Assert .assertEquals ;
25
+ import static org .junit .Assert .assertNull ;
26
+ import static org .junit .Assert .assertTrue ;
17
27
18
28
import java .io .IOException ;
19
29
import java .util .Map ;
29
39
* Created by sergioalonso on 5/16/17.
30
40
*/
31
41
32
- public class AutomaticEventsTest extends AndroidTestCase {
42
+ @ RunWith (AndroidJUnit4 .class )
43
+ @ SmallTest
44
+ public class AutomaticEventsTest {
33
45
34
46
private MixpanelAPI mCleanMixpanelAPI ;
35
47
private static final String TOKEN = "Automatic Events Token" ;
@@ -44,10 +56,9 @@ public class AutomaticEventsTest extends AndroidTestCase {
44
56
private MPDbAdapter mockAdapter ;
45
57
private CountDownLatch mMinRequestsLatch ;
46
58
47
- @ Override
48
- protected void setUp () throws Exception {
49
- super .setUp ();
50
- mMockReferrerPreferences = new TestUtils .EmptyPreferences (getContext ());
59
+ @ Before
60
+ public void setUp () {
61
+ mMockReferrerPreferences = new TestUtils .EmptyPreferences (InstrumentationRegistry .getInstrumentation ().getContext ());
51
62
mTrackedEvents = 0 ;
52
63
mCanRunDecide = true ;
53
64
mMinRequestsLatch = new CountDownLatch (2 ); // First Time Open and Update
@@ -80,9 +91,9 @@ public byte[] performRequest(String endpointUrl, Map<String, Object> params, SSL
80
91
}
81
92
};
82
93
83
- getContext ().deleteDatabase ("mixpanel" );
94
+ InstrumentationRegistry . getInstrumentation (). getContext ().deleteDatabase ("mixpanel" );
84
95
85
- mockAdapter = new MPDbAdapter (getContext ()) {
96
+ mockAdapter = new MPDbAdapter (InstrumentationRegistry . getInstrumentation (). getContext ()) {
86
97
@ Override
87
98
public void cleanupEvents (String last_id , Table table , String token , boolean includeAutomaticEvents ) {
88
99
if (token .equalsIgnoreCase (TOKEN )) {
@@ -102,7 +113,7 @@ public int addJSON(JSONObject j, String token, Table table, boolean isAutomaticR
102
113
}
103
114
};
104
115
105
- final AnalyticsMessages automaticAnalyticsMessages = new AnalyticsMessages (getContext ()) {
116
+ final AnalyticsMessages automaticAnalyticsMessages = new AnalyticsMessages (InstrumentationRegistry . getInstrumentation (). getContext ()) {
106
117
107
118
@ Override
108
119
protected RemoteService getPoster () {
@@ -140,7 +151,7 @@ public void runDecideCheck(String token, RemoteService poster) throws RemoteServ
140
151
}
141
152
};
142
153
143
- mCleanMixpanelAPI = new MixpanelAPI (getContext (), mMockReferrerPreferences , TOKEN , false , null ) {
154
+ mCleanMixpanelAPI = new MixpanelAPI (InstrumentationRegistry . getInstrumentation (). getContext (), mMockReferrerPreferences , TOKEN , false , null ) {
144
155
145
156
@ Override
146
157
/* package */ PersistentIdentity getPersistentIdentity (final Context context , final Future <SharedPreferences > referrerPreferences , final String token ) {
@@ -166,12 +177,12 @@ AnalyticsMessages getAnalyticsMessages() {
166
177
};
167
178
}
168
179
169
- @ Override
170
- protected void tearDown () throws Exception {
180
+ @ After
181
+ public void tearDown () throws Exception {
171
182
mMinRequestsLatch .await (MAX_TIMEOUT_POLL , TimeUnit .MILLISECONDS );
172
- super .tearDown ();
173
183
}
174
184
185
+ @ Test
175
186
public void testAutomaticOneInstance () throws InterruptedException {
176
187
int calls = 3 ; // First Time Open, App Update, An Event One
177
188
mLatch = new CountDownLatch (calls );
@@ -185,6 +196,7 @@ public void testAutomaticOneInstance() throws InterruptedException {
185
196
assertEquals (null , mPerformRequestEvents .poll (MAX_TIMEOUT_POLL , TimeUnit .MILLISECONDS ));
186
197
}
187
198
199
+ @ Test
188
200
public void testDisableAutomaticEvents () throws InterruptedException {
189
201
mCanRunDecide = false ;
190
202
@@ -211,6 +223,7 @@ public void testDisableAutomaticEvents() throws InterruptedException {
211
223
assertEquals (null , mPerformRequestEvents .poll (MAX_TIMEOUT_POLL , TimeUnit .MILLISECONDS ));
212
224
}
213
225
226
+ @ Test
214
227
public void testAutomaticMultipleInstances () throws InterruptedException {
215
228
final String SECOND_TOKEN = "Automatic Events Token Two" ;
216
229
mCanRunDecide = true ;
@@ -243,7 +256,7 @@ public byte[] performRequest(String endpointUrl, Map<String, Object> params, SSL
243
256
}
244
257
};
245
258
246
- final MPDbAdapter mpSecondDbAdapter = new MPDbAdapter (getContext ()) {
259
+ final MPDbAdapter mpSecondDbAdapter = new MPDbAdapter (InstrumentationRegistry . getInstrumentation (). getContext ()) {
247
260
@ Override
248
261
public void cleanupEvents (String last_id , Table table , String token , boolean includeAutomaticEvents ) {
249
262
if (token .equalsIgnoreCase (SECOND_TOKEN )) {
@@ -262,7 +275,7 @@ public int addJSON(JSONObject j, String token, Table table, boolean isAutomaticR
262
275
}
263
276
};
264
277
265
- final AnalyticsMessages mpSecondAnalyticsMessages = new AnalyticsMessages (getContext ()) {
278
+ final AnalyticsMessages mpSecondAnalyticsMessages = new AnalyticsMessages (InstrumentationRegistry . getInstrumentation (). getContext ()) {
266
279
@ Override
267
280
protected RemoteService getPoster () {
268
281
return mpSecondPoster ;
@@ -299,7 +312,7 @@ public void runDecideCheck(String token, RemoteService poster) throws RemoteServ
299
312
}
300
313
};
301
314
302
- MixpanelAPI mpSecondInstance = new TestUtils .CleanMixpanelAPI (getContext (), new TestUtils .EmptyPreferences (getContext ()), SECOND_TOKEN ) {
315
+ MixpanelAPI mpSecondInstance = new TestUtils .CleanMixpanelAPI (InstrumentationRegistry . getInstrumentation (). getContext (), new TestUtils .EmptyPreferences (InstrumentationRegistry . getInstrumentation (). getContext ()), SECOND_TOKEN ) {
303
316
@ Override
304
317
AnalyticsMessages getAnalyticsMessages () {
305
318
return mpSecondAnalyticsMessages ;
@@ -310,15 +323,15 @@ AnalyticsMessages getAnalyticsMessages() {
310
323
assertEquals (initialCalls , mTrackedEvents );
311
324
312
325
assertTrue (secondLatch .await (MAX_TIMEOUT_POLL , TimeUnit .MILLISECONDS ));
313
- mLatch = new CountDownLatch (MPConfig .getInstance (getContext ()).getBulkUploadLimit () - initialCalls );
314
- for (int i = 0 ; i < MPConfig .getInstance (getContext ()).getBulkUploadLimit () - initialCalls ; i ++) {
326
+ mLatch = new CountDownLatch (MPConfig .getInstance (InstrumentationRegistry . getInstrumentation (). getContext ()).getBulkUploadLimit () - initialCalls );
327
+ for (int i = 0 ; i < MPConfig .getInstance (InstrumentationRegistry . getInstrumentation (). getContext ()).getBulkUploadLimit () - initialCalls ; i ++) {
315
328
mCleanMixpanelAPI .track ("Track event " + i );
316
329
}
317
330
assertTrue (mLatch .await (MAX_TIMEOUT_POLL , TimeUnit .MILLISECONDS ));
318
331
319
332
assertEquals (AutomaticEvents .FIRST_OPEN , mPerformRequestEvents .poll (MAX_TIMEOUT_POLL , TimeUnit .MILLISECONDS ));
320
333
assertEquals (AutomaticEvents .APP_UPDATED , mPerformRequestEvents .poll (MAX_TIMEOUT_POLL , TimeUnit .MILLISECONDS ));
321
- for (int i = 0 ; i < MPConfig .getInstance (getContext ()).getBulkUploadLimit () - initialCalls ; i ++) {
334
+ for (int i = 0 ; i < MPConfig .getInstance (InstrumentationRegistry . getInstrumentation (). getContext ()).getBulkUploadLimit () - initialCalls ; i ++) {
322
335
assertEquals ("Track event " + i , mPerformRequestEvents .poll (MAX_TIMEOUT_POLL , TimeUnit .MILLISECONDS ));
323
336
}
324
337
0 commit comments