-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathMParticleOptionsTest.kt
More file actions
623 lines (587 loc) · 22 KB
/
Copy pathMParticleOptionsTest.kt
File metadata and controls
623 lines (587 loc) · 22 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package com.mparticle
import android.Manifest
import android.content.Context
import android.content.SharedPreferences
import android.os.Looper
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import com.mparticle.MParticleOptions.BatchCreationListener
import com.mparticle.internal.AccessUtils
import com.mparticle.internal.ConfigManager
import com.mparticle.internal.Constants
import com.mparticle.internal.Logger
import com.mparticle.internal.Logger.DefaultLogHandler
import com.mparticle.internal.MPUtility
import com.mparticle.networking.Matcher
import com.mparticle.testutils.AndroidUtils
import com.mparticle.testutils.BaseAbstractTest
import com.mparticle.testutils.MPLatch
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.UUID
import java.util.concurrent.CountDownLatch
class MParticleOptionsTest : BaseAbstractTest() {
lateinit var mContext: Context
private lateinit var mProductionContext: Context
@Before
fun before() {
if (Looper.myLooper() == null) {
Looper.prepare()
}
mContext = InstrumentationRegistry.getInstrumentation().context
mProductionContext = AndroidUtils.getProductionContext(mContext)
MParticle.setInstance(null)
Assert.assertNull(MParticle.getInstance())
}
@Test
@Throws(Exception::class)
fun testCrashOnNoCredentials() {
var thrown = false
clearStoredPreferences()
try {
MParticleOptions.builder(mContext).build()
} catch (ex: IllegalArgumentException) {
thrown = true
}
Assert.assertTrue(thrown)
clearStoredPreferences()
thrown = false
try {
MParticleOptions.builder(mContext)
.credentials("", "")
.build()
} catch (ex: IllegalArgumentException) {
thrown = true
}
Assert.assertTrue(thrown)
clearStoredPreferences()
thrown = false
try {
MParticleOptions.builder(mContext)
.credentials("key", "")
.build()
} catch (ex: IllegalArgumentException) {
thrown = true
}
Assert.assertTrue(thrown)
clearStoredPreferences()
thrown = false
try {
MParticleOptions.builder(mContext)
.credentials("", "key")
.build()
} catch (ex: IllegalArgumentException) {
thrown = true
}
Assert.assertTrue(thrown)
setStoredPreference("key", "secret")
try {
MParticleOptions.builder(mContext).buildForInternalRestart()
} catch (ex: IllegalArgumentException) {
Assert.fail("MParticleOptions should build without credentials if the internal build function is used")
}
try {
MParticleOptions.builder(mProductionContext).build()
} catch (ex: IllegalArgumentException) {
Assert.fail("MParticleOptions should build without credentials in a Production environment")
}
try {
MParticleOptions.builder(mProductionContext)
.credentials("", "")
.build()
} catch (ex: IllegalArgumentException) {
Assert.fail("MParticleOptions should build without credentials in a Production environment")
}
}
private fun clearStoredPreferences() {
credentialsPreferences
.edit()
.remove(Constants.PrefKeys.API_KEY)
.remove(Constants.PrefKeys.API_SECRET)
.commit()
}
private fun setStoredPreference(apiKey: String, apiSecret: String) {
credentialsPreferences
.edit()
.putString(Constants.PrefKeys.API_KEY, apiKey)
.putString(Constants.PrefKeys.API_SECRET, apiSecret)
.commit()
}
private val credentialsPreferences: SharedPreferences
get() = mContext.getSharedPreferences("mp_preferences", Context.MODE_PRIVATE)
@Test
@Throws(Exception::class)
fun testSetCredentials() {
val key = UUID.randomUUID().toString()
val secret = UUID.randomUUID().toString()
startMParticle(
MParticleOptions.builder(mProductionContext)
.credentials(key, secret)
)
Assert.assertEquals(MParticle.getInstance()?.mInternal?.configManager?.apiKey, key)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.apiSecret,
secret
)
}
@Test
@Throws(Exception::class)
fun testAndroidIdDisabled() {
// test defaults
Assert.assertFalse(MParticle.isAndroidIdEnabled())
Assert.assertTrue(MParticle.isAndroidIdDisabled())
MParticle.setInstance(null)
startMParticle(MParticleOptions.builder(mContext))
Assert.assertFalse(MParticle.isAndroidIdEnabled())
Assert.assertTrue(MParticle.isAndroidIdDisabled())
// test androidIdDisabled == true
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mContext)
.androidIdDisabled(true)
)
Assert.assertFalse(MParticle.isAndroidIdEnabled())
Assert.assertTrue(MParticle.isAndroidIdDisabled())
MParticle.setInstance(null)
// test androidIdEnabled == false
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mContext)
.androidIdEnabled(false)
)
Assert.assertFalse(MParticle.isAndroidIdEnabled())
Assert.assertTrue(MParticle.isAndroidIdDisabled())
MParticle.setInstance(null)
// test androidIdDisabled == false
startMParticle(
MParticleOptions.builder(mContext)
.androidIdDisabled(false)
)
Assert.assertTrue(MParticle.isAndroidIdEnabled())
Assert.assertFalse(MParticle.isAndroidIdDisabled())
// test androidIdEnabled == true
startMParticle(
MParticleOptions.builder(mContext)
.androidIdEnabled(true)
)
Assert.assertTrue(MParticle.isAndroidIdEnabled())
Assert.assertFalse(MParticle.isAndroidIdDisabled())
}
@Test
@Throws(Exception::class)
fun testDevicePerformanceMetricsDisabled() {
startMParticle()
MParticle.getInstance()?.let { Assert.assertFalse(it.isDevicePerformanceMetricsDisabled) }
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mContext)
.devicePerformanceMetricsDisabled(false)
)
MParticle.getInstance()?.let { Assert.assertFalse(it.isDevicePerformanceMetricsDisabled) }
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mContext)
.devicePerformanceMetricsDisabled(true)
)
MParticle.getInstance()?.let { Assert.assertTrue(it.isDevicePerformanceMetricsDisabled) }
MParticle.setInstance(null)
}
@Test
@Throws(Exception::class)
fun testLogLevel() {
startMParticle()
Assert.assertEquals(Logger.getMinLogLevel(), Logger.DEFAULT_MIN_LOG_LEVEL)
startMParticle(
MParticleOptions.builder(mProductionContext)
.logLevel(MParticle.LogLevel.VERBOSE)
)
Assert.assertEquals(Logger.getMinLogLevel(), MParticle.LogLevel.VERBOSE)
startMParticle(
MParticleOptions.builder(mProductionContext).logLevel(MParticle.LogLevel.ERROR)
)
Assert.assertEquals(Logger.getMinLogLevel(), MParticle.LogLevel.ERROR)
}
@Test
@Throws(Exception::class)
fun testEnvironment() {
startMParticle()
Assert.assertEquals(
MParticle.getInstance()?.environment,
MParticle.Environment.Development
)
startMParticle(
MParticleOptions.builder(mProductionContext)
.environment(MParticle.Environment.Production)
)
Assert.assertEquals(MParticle.getInstance()?.environment, MParticle.Environment.Production)
MParticle.setInstance(null)
val productionContext = mProductionContext
val debuggable = MPUtility.isAppDebuggable(productionContext)
Assert.assertFalse(debuggable)
startMParticle(
MParticleOptions.builder(productionContext)
.environment(MParticle.Environment.AutoDetect)
)
Assert.assertEquals(MParticle.getInstance()?.environment, MParticle.Environment.Production)
MParticle.setInstance(null)
}
@Test
@Throws(Exception::class)
fun testEnableUncaughtExceptionLogging() {
val options = MParticleOptions.builder(mProductionContext)
.credentials("key", "secret")
.build()
MParticle.start(options)
MParticle.getInstance()?.mInternal?.configManager?.let {
Assert.assertFalse(
it.logUnhandledExceptions
)
}
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mProductionContext)
.enableUncaughtExceptionLogging(true)
)
MParticle.getInstance()?.mInternal?.configManager?.logUnhandledExceptions?.let {
Assert.assertTrue(
it
)
}
startMParticle(
MParticleOptions.builder(mProductionContext)
.enableUncaughtExceptionLogging(false)
)
MParticle.getInstance()?.mInternal?.configManager?.let {
Assert.assertFalse(
it.logUnhandledExceptions
)
}
MParticle.setInstance(null)
}
@Test
@Throws(Exception::class)
fun testSessionTimeout() {
startMParticle()
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.sessionTimeout,
60000
)
startMParticle(
MParticleOptions.builder(mProductionContext)
.sessionTimeout(-123)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.sessionTimeout,
60000
)
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mProductionContext)
.sessionTimeout(123)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.sessionTimeout,
123000
)
// make sure it resets if the session timeout is not specified
startMParticle()
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.sessionTimeout,
60000
)
MParticle.setInstance(null)
}
@Test
@Throws(Exception::class)
fun testInstallType() {
startMParticle()
Assert.assertEquals(
AccessUtils.getInstallType(MParticle.getInstance()?.mMessageManager),
MParticle.InstallType.AutoDetect
)
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mProductionContext)
.installType(MParticle.InstallType.KnownInstall)
)
Assert.assertEquals(
AccessUtils.getInstallType(MParticle.getInstance()?.mMessageManager),
MParticle.InstallType.KnownInstall
)
startMParticle(
MParticleOptions.builder(mProductionContext)
.installType(MParticle.InstallType.KnownUpgrade)
)
Assert.assertEquals(
AccessUtils.getInstallType(MParticle.getInstance()?.mMessageManager),
MParticle.InstallType.KnownUpgrade
)
startMParticle(
MParticleOptions.builder(mProductionContext)
.installType(MParticle.InstallType.AutoDetect)
)
Assert.assertEquals(
AccessUtils.getInstallType(MParticle.getInstance()?.mMessageManager),
MParticle.InstallType.AutoDetect
)
MParticle.setInstance(null)
}
@Test
@Throws(Exception::class)
fun testUploadInterval() {
// default upload interval for production
startMParticle()
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.uploadInterval,
10000L
)
MParticle.setInstance(null)
// default upload interval for production
startMParticle(MParticleOptions.builder(mProductionContext))
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.uploadInterval,
600000L
)
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mProductionContext)
.uploadInterval(123)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.uploadInterval,
123000L
)
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mProductionContext)
.uploadInterval(-123)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.uploadInterval,
600000L
)
MParticle.setInstance(null)
}
@Test
@Throws(Exception::class)
fun testAttributionListener() {
startMParticle()
Assert.assertNull(MParticle.getInstance()?.attributionListener)
startMParticle(
MParticleOptions.builder(mContext)
.attributionListener(object : AttributionListener {
override fun onResult(result: AttributionResult) {}
override fun onError(error: AttributionError) {}
})
)
Assert.assertNotNull(MParticle.getInstance()?.attributionListener)
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mContext)
.attributionListener(null)
)
Assert.assertNull(MParticle.getInstance()?.attributionListener)
}
@Test
@Throws(InterruptedException::class)
fun setOperatingSystemTest() {
val called = AndroidUtils.Mutable(false)
val latch: CountDownLatch = MPLatch(1)
startMParticle(
MParticleOptions.builder(mContext)
.operatingSystem(MParticle.OperatingSystem.FIRE_OS)
)
mServer.waitForVerify(Matcher(mServer.Endpoints().eventsUrl)) { request ->
Assert.assertEquals("FireTV", request.bodyJson.optJSONObject("di")?.optString("dp"))
called.value = true
latch.countDown()
}
MParticle.getInstance()
?.logEvent(MPEvent.Builder("event name", MParticle.EventType.Location).build())
MParticle.getInstance()?.upload()
latch.await()
Assert.assertTrue(called.value)
}
@Test
@Throws(InterruptedException::class)
fun setOperatingSystemDefault() {
val called = AndroidUtils.Mutable(false)
val latch1: CountDownLatch = MPLatch(1)
startMParticle(MParticleOptions.builder(mContext))
mServer.waitForVerify(Matcher(mServer.Endpoints().eventsUrl)) { request ->
Assert.assertEquals("Android", request.bodyJson.optJSONObject("di")?.optString("dp"))
called.value = true
latch1.countDown()
}
MParticle.getInstance()
?.logEvent(MPEvent.Builder("event name", MParticle.EventType.Location).build())
MParticle.getInstance()?.upload()
latch1.await()
Assert.assertTrue(called.value)
}
@get:Rule
var mRuntimePermissionRule = GrantPermissionRule.grant(Manifest.permission.ACCESS_FINE_LOCATION)
@Test
@Throws(InterruptedException::class)
fun testLocationTracking() {
startMParticle(
MParticleOptions.builder(mContext)
.locationTrackingDisabled()
)
MParticle.getInstance()?.let { Assert.assertFalse(it.isLocationTrackingEnabled) }
MParticle.setInstance(null)
Assert.assertNull(MParticle.getInstance())
startMParticle(
MParticleOptions.builder(mContext)
.locationTrackingEnabled("passive", 100, 20)
)
MParticle.getInstance()?.let { Assert.assertTrue(it.isLocationTrackingEnabled) }
MParticle.setInstance(null)
Assert.assertNull(MParticle.getInstance())
startMParticle()
MParticle.getInstance()?.let { Assert.assertFalse(it.isLocationTrackingEnabled) }
}
@Test
@Throws(InterruptedException::class)
fun testTimeout() {
startMParticle(
MParticleOptions.builder(mProductionContext)
.identityConnectionTimeout(-123)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.identityConnectionTimeout,
(ConfigManager.DEFAULT_CONNECTION_TIMEOUT_SECONDS * 1000)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.connectionTimeout,
(ConfigManager.DEFAULT_CONNECTION_TIMEOUT_SECONDS * 1000)
)
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mProductionContext)
.identityConnectionTimeout(0)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.identityConnectionTimeout,
(ConfigManager.DEFAULT_CONNECTION_TIMEOUT_SECONDS * 1000)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.connectionTimeout,
(ConfigManager.DEFAULT_CONNECTION_TIMEOUT_SECONDS * 1000)
)
MParticle.setInstance(null)
startMParticle(
MParticleOptions.builder(mProductionContext)
.identityConnectionTimeout(123)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.identityConnectionTimeout,
123000
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.connectionTimeout,
(ConfigManager.DEFAULT_CONNECTION_TIMEOUT_SECONDS * 1000)
)
MParticle.setInstance(null)
startMParticle(MParticleOptions.builder(mProductionContext))
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.identityConnectionTimeout,
(ConfigManager.DEFAULT_CONNECTION_TIMEOUT_SECONDS * 1000)
)
Assert.assertEquals(
MParticle.getInstance()?.mInternal?.configManager?.connectionTimeout,
(ConfigManager.DEFAULT_CONNECTION_TIMEOUT_SECONDS * 1000)
)
}
@Test
fun testNetworkOptions() {
val options = MParticleOptions.builder(mProductionContext)
.credentials("key", "secret")
.build()
Assert.assertTrue(
com.mparticle.networking.AccessUtils.equals(
options.networkOptions,
com.mparticle.networking.AccessUtils.defaultNetworkOptions
)
)
}
@Test
fun testConfigStaleness() {
// nothing set, should return null
var options = MParticleOptions.builder(mContext)
.credentials("key", "secret")
.build()
Assert.assertNull(options.configMaxAge)
// 0 should return 0
options = MParticleOptions.builder(mContext)
.credentials("key", "secret")
.configMaxAgeSeconds(0)
.build()
Assert.assertEquals(0, options.configMaxAge)
// positive number should return positive number
val testValue = Math.abs(ran.nextInt())
options = MParticleOptions.builder(mContext)
.credentials("key", "secret")
.configMaxAgeSeconds(testValue)
.build()
Assert.assertEquals(testValue, options.configMaxAge)
// negative number should get thrown out and return null
options = MParticleOptions.builder(mContext)
.credentials("key", "secret")
.configMaxAgeSeconds(-5)
.build()
Assert.assertNull(options.configMaxAge)
}
@Test
fun testAndroidIdLogMessage() {
val infoLogs = ArrayList<String?>()
Logger.logHandler = object : DefaultLogHandler() {
override fun log(priority: MParticle.LogLevel, error: Throwable?, messages: String) {
super.log(priority, error, messages)
if (priority == MParticle.LogLevel.INFO) {
infoLogs.add(messages)
}
}
}
MParticleOptions.builder(mContext)
.credentials("this", "that")
.androidIdDisabled(true)
.build()
Assert.assertTrue(infoLogs.contains("ANDROID_ID will not be collected based on MParticleOptions settings"))
infoLogs.clear()
MParticleOptions.builder(mContext)
.credentials("this", "that")
.androidIdDisabled(false)
.build()
Assert.assertTrue(infoLogs.contains("ANDROID_ID will be collected based on MParticleOptions settings"))
infoLogs.clear()
// test default
MParticleOptions.builder(mContext)
.credentials("this", "that")
.build()
Assert.assertTrue(infoLogs.contains("ANDROID_ID will not be collected based on default settings"))
infoLogs.clear()
}
@Test
@Throws(InterruptedException::class)
fun testBatchCreationCallback() {
val listener = BatchCreationListener { batch -> batch }
var options = MParticleOptions.builder(mProductionContext)
.batchCreationListener(listener)
.credentials("this", "that")
.build()
Assert.assertEquals(listener, options.batchCreationListener)
options = MParticleOptions.builder(mProductionContext)
.credentials("this", "that")
.batchCreationListener(listener)
.batchCreationListener(null)
.build()
Assert.assertNull(options.batchCreationListener)
options = MParticleOptions.builder(mProductionContext)
.credentials("this", "that")
.build()
Assert.assertNull(options.batchCreationListener)
}
}