-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMediaSessionTest.kt
More file actions
604 lines (516 loc) · 22.1 KB
/
Copy pathMediaSessionTest.kt
File metadata and controls
604 lines (516 loc) · 22.1 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
package com.mparticle
import com.mparticle.media.MediaSession
import com.mparticle.media.events.*
import com.mparticle.testutils.RandomUtils
import junit.framework.Assert.*
import org.junit.Assert
import org.junit.Assert.assertNotEquals
import org.junit.Test
import java.lang.reflect.Method
import java.util.*
import kotlin.math.absoluteValue
class MediaSessionTest {
private val random = Random()
private val randomUtils = RandomUtils()
@Test
fun testCallbackInvoke() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
streamType = StreamType.LIVE_STREAM
contentType = ContentType.VIDEO
logMediaEvents = random.nextBoolean()
logMPEvents = random.nextBoolean()
}
var lastMediaEvent: MediaEvent? = null
mediaSession.mediaEventListener = {
assertNull(lastMediaEvent)
lastMediaEvent = it
}
afterLogApiInvoked(mediaSession) { method ->
assertNotNull(lastMediaEvent)
lastMediaEvent = null
}
}
@Test
fun testLogOnlyMPEvents() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
streamType = StreamType.LIVE_STREAM
contentType = ContentType.VIDEO
logMediaEvents = false
logMPEvents = true
}
afterLogApiInvoked(mediaSession) { method ->
if (method.name != "logPlayheadPosition") {
assertTrue(method.toString(), mparticle.loggedEvents.size >= 1)
assertTrue(mparticle.loggedEvents.any { it is MPEvent && it.eventType == MParticle.EventType.Media })
mparticle.loggedEvents.clear()
}
}
}
@Test
fun testLogOnlyMedia() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
streamType = StreamType.LIVE_STREAM
contentType = ContentType.VIDEO
logMediaEvents = true
logMPEvents = false
}
afterLogApiInvoked(mediaSession) { method ->
assertTrue(method.toString(), mparticle.loggedEvents.size >= 1)
assertTrue(mparticle.loggedEvents.any { it is MediaEvent })
mparticle.loggedEvents.clear()
}
}
@Test
fun testAllMediaEventsHaveMediaContent() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId = "123"
duration = 1000
streamType = StreamType.LIVE_STREAM
contentType = ContentType.VIDEO
}
afterLogApiInvoked (mediaSession) { method ->
assertTrue(method.toString(), mparticle.loggedEvents.size >= 1)
val event = mparticle.loggedEvents[0] as MediaEvent?
assertNotNull(event!!.mediaContent)
assertEquals(mediaSession.title, event.mediaContent.name)
assertEquals(mediaSession.duration, event.mediaContent.duration)
assertEquals(mediaSession.mediaContentId, event.mediaContent.contentId)
assertEquals(mediaSession.contentType, event.mediaContent.contentType)
assertEquals(mediaSession.streamType, event.mediaContent.streamType)
mparticle.loggedEvents.clear()
}
}
@Test
fun testAllMediaEventsHavePlayheadPosition() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId = "123"
duration = 1000
streamType = StreamType.LIVE_STREAM
contentType = ContentType.VIDEO
}
var currentPlayhead = 0L
fun updateRandomPlayhead() {
currentPlayhead = random.nextLong().absoluteValue
mediaSession.logPlayheadPosition(currentPlayhead)
}
updateRandomPlayhead()
afterLogApiInvoked (mediaSession) { method ->
assertTrue(method.toString(), mparticle.loggedEvents.size >= 2)
assertTrue(mparticle.loggedEvents.any { it.isPlayheadEvent() })
if (method.name != "logPlayheadPosition") {
//if the method is NOT logPlayhead, both logged events should have the proper playhead position
assertTrue(mparticle.loggedEvents.any { !it.isPlayheadEvent() })
assertTrue(method.toString(), mparticle.loggedEvents.all { (it as? MediaEvent)?.playheadPosition?.equals(currentPlayhead) ?: true })
} else {
//if the method is logPlayhead, just make sure the first event (the one called from this method) has the proper playhead
// ..the one called from afterLogApiInvoked will have a random value
assertTrue(method.toString(), (mparticle.loggedEvents[0] as MediaEvent).playheadPosition == currentPlayhead)
}
mparticle.loggedEvents.clear()
updateRandomPlayhead()
}
}
@Test
fun reallyExplicitMakeSureLogPlayheadEventsAreNeverLoggedAsMPEventTest() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
streamType = StreamType.LIVE_STREAM
contentType = ContentType.VIDEO
logMediaEvents = false
logMPEvents = true
}
mediaSession.logPlayheadPosition(1L)
assertEquals(0, mparticle.loggedEvents.size)
mediaSession.logPlayheadPosition(random.nextLong())
assertEquals(0, mparticle.loggedEvents.size)
mediaSession.logPlayheadPosition(random.nextLong())
assertEquals(0, mparticle.loggedEvents.size)
mediaSession.logPlayheadPosition(random.nextLong())
assertEquals(0, mparticle.loggedEvents.size)
mediaSession.logPlayheadPosition(random.nextLong())
assertEquals(0, mparticle.loggedEvents.size)
}
@Test
fun attributesTest() {
val mediaSession = MediaSession.builder {
contentType = randomUtils.getAlphaNumericString(50)
streamType = randomUtils.getAlphaNumericString(50)
duration = random.nextLong().absoluteValue
mediaContentId = randomUtils.getAlphaNumericString(50)
title = randomUtils.getAlphaNumericString(50)
}
var attributes = mediaSession.attributes
assertEquals(5, attributes.size)
fun testSessionMediaContentAttributes() {
assertEquals(mediaSession.mediaContentId, attributes[MediaAttributeKeys.CONTENT_ID])
assertEquals(mediaSession.title, attributes[MediaAttributeKeys.TITLE])
assertEquals(mediaSession.duration, attributes[MediaAttributeKeys.DURATION])
assertEquals(mediaSession.contentType, attributes[MediaAttributeKeys.CONTENT_TYPE])
assertEquals(mediaSession.streamType, attributes[MediaAttributeKeys.STREAM_TYPE])
}
testSessionMediaContentAttributes()
val testAttributes = HashMap<String, Any?>()
testAttributes["CustomAttributeKey"] = "CustomAttributeValue"
mediaSession.mediaSessionAttributes = testAttributes
mediaSession.logMediaSessionStart()
attributes = mediaSession.attributes
assertEquals(7, attributes.size)
assertNotNull(attributes[MediaAttributeKeys.MEDIA_SESSION_ID])
assertEquals(testAttributes["CustomAttributeKey"], attributes["CustomAttributeKey"])
mediaSession.logPlayheadPosition(10L)
attributes = mediaSession.attributes
assertEquals(8, attributes.size)
assertEquals(10L, attributes[MediaAttributeKeys.PLAYHEAD_POSITION])
//make sure no other Media Sessions are started
afterLogApiInvoked(mediaSession) {}
attributes = mediaSession.attributes
assertEquals(8, attributes.size)
assertNotNull(attributes[MediaAttributeKeys.PLAYHEAD_POSITION])
assertNotNull(attributes[MediaAttributeKeys.MEDIA_SESSION_ID])
}
/**
* test to make sure that Option's customAttributes and currentPlayPosition are accepted for each "log" method,
* and applied uniformly
*/
@Test
fun optionsMPEventTest() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
contentType = randomUtils.getAlphaNumericString(50)
streamType = randomUtils.getAlphaNumericString(50)
duration = random.nextLong().absoluteValue
mediaContentId = randomUtils.getAlphaNumericString(50)
title = randomUtils.getAlphaNumericString(50)
}
val options = Options().apply {
currentPlayheadPosition = random.nextLong()
customAttributes = mapOf(
"testKey1" to "testValue1",
"testKey2" to "testValue2")
}
afterLogApiInvoked(mediaSession, options) {
if (it.name != "logPlayheadPosition") {
assertTrue(it.toString(), mparticle.loggedEvents.size >= 1)
val event = mparticle.loggedEvents[0] as MediaEvent
if (event.customAttributes?.containsKey("testKey1") == false) {
println("hi")
}
assertTrue(it.toString(), event.customAttributes?.containsKey("testKey1") ?: false)
assertTrue(it.toString(), event.customAttributes?.get("testKey1") == "testValue1")
assertTrue(it.toString(), event.customAttributes?.containsKey("testKey2") ?: false)
assertTrue(it.toString(), event.customAttributes?.get("testKey2") == "testValue2")
assertEquals(options.currentPlayheadPosition, mediaSession.currentPlayheadPosition)
}
mparticle.loggedEvents.clear()
}
}
@Test
fun qosTest() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
streamType = StreamType.LIVE_STREAM
contentType = ContentType.VIDEO
logMediaEvents = random.nextBoolean()
logMPEvents = random.nextBoolean()
}
var qos: MediaQoS? = null
mediaSession.mediaEventListener = { qos = it.qos }
//test empty QoS, should remain all null
mediaSession.logQos { }
assertNotNull(qos)
assertNull(qos?.startupTime)
assertNull(qos?.bitRate)
assertNull(qos?.fps)
assertNull(qos?.droppedFrames)
qos = null
//test single QoS value, all others should remain null since they've never been set
val singleTestQos = MediaQoS(fps = 5)
mediaSession.logQos(singleTestQos)
assertNotNull(qos)
assertNull(qos?.startupTime)
assertNull(qos?.bitRate)
assertEquals(qos?.fps, singleTestQos.fps)
assertNull(qos?.droppedFrames)
//test update whole object, overwrite everything
val fullTestQos = MediaQoS(startupTime = 1, bitRate = 2, fps = 3, droppedFrames = 4)
mediaSession.logQos(fullTestQos)
assertNotNull(qos)
assertEquals(qos?.startupTime, fullTestQos.startupTime)
assertEquals(qos?.bitRate, fullTestQos.bitRate)
assertEquals(qos?.fps, fullTestQos.fps)
assertEquals(qos?.droppedFrames, fullTestQos.droppedFrames)
qos = null
//test empty object when all fields are already set, should send all existing values
mediaSession.logQos { }
assertNotNull(qos)
assertEquals(qos?.startupTime, fullTestQos.startupTime)
assertEquals(qos?.bitRate, fullTestQos.bitRate)
assertEquals(qos?.fps, fullTestQos.fps)
assertEquals(qos?.droppedFrames, fullTestQos.droppedFrames)
qos = null
//test partial object when all fields are already set, should update fields included and send existing values for the rest
val partialTestQos = MediaQoS(startupTime = 5, fps = 6)
mediaSession.logQos(partialTestQos)
assertNotNull(qos)
assertEquals(qos?.startupTime, partialTestQos.startupTime)
assertEquals(qos?.bitRate, fullTestQos.bitRate)
assertEquals(qos?.fps, partialTestQos.fps)
assertEquals(qos?.droppedFrames, fullTestQos.droppedFrames)
}
@Test
fun `test MPEvents built from MediaSession have proper EventType`() {
val mparticle = MockMParticle()
val mediaSessionMPEvent = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
streamType = StreamType.LIVE_STREAM
contentType = ContentType.VIDEO
logMediaEvents = random.nextBoolean()
logMPEvents = random.nextBoolean()
}.buildMPEvent("some name", null)
assertEquals(MParticle.EventType.Media, mediaSessionMPEvent.eventType)
}
private fun afterLogApiInvoked(mediaSession: MediaSession, options: Options? = null, onEvent: (Method) -> Unit) {
val methods = ArrayList<Method>()
for (method in MediaSession::class.java.methods) {
if (method.name != "logCustomEvent" && method.name.startsWith("log") && !method.name.endsWith("\$default")) {
methods.add(method)
}
}
for (method in methods) {
if (Arrays.asList(*method.parameterTypes).contains(Function1::class.java)) {
continue
}
val arguments = arrayOfNulls<Any>(method.parameterTypes.size)
var i = 0
for (type in method.parameterTypes) {
if (type == Long::class.java || type == Long::class.javaPrimitiveType) {
arguments[i] = random.nextLong()
} else if (type == String::class.java) {
arguments[i] = UUID.randomUUID().toString()
} else if (type == Int::class.java || type == Int::class.javaPrimitiveType) {
arguments[i] = random.nextInt()
} else if (type == Double::class.java || type == Double::class.javaPrimitiveType) {
arguments[i] = random.nextDouble()
} else if (type == MediaAd::class.java) {
arguments[i] = MediaAd()
} else if (type == MediaAdBreak::class.java) {
arguments[i] = MediaAdBreak()
} else if (type == MediaSegment::class.java) {
arguments[i] = MediaSegment()
} else if (type == MediaQoS::class.java) {
arguments[i] = MediaQoS()
} else if (type == Options::class.java) {
arguments[i] = options
} else if (type == Map::class.java) {
arguments[i] = RandomUtils.getInstance().getRandomAttributes(6, false)
} else {
throw RuntimeException("unknown type: " + type.name + "\nmethod: " + method.toString())
}
i++
}
try {
if (arguments.size == 0) {
method.invoke(mediaSession)
} else {
method.invoke(mediaSession, *arguments)
}
} catch (e: Exception) {
throw e;
}
onEvent(method)
}
}
@Test
fun testLogAd() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
}
val events = mutableListOf<MediaEvent>()
mediaSession.mediaEventListener = { event ->
events.add(event)
}
//test with java interface methods
mediaSession.logAdBreakStart(MediaAdBreak().apply { id = "adbreak 1" })
mediaSession.logAdStart(MediaAd().apply { id = "ad 1" })
mediaSession.logAdClick()
mediaSession.logAdEnd()
mediaSession.logAdBreakEnd()
//test with kotlin interface methods (overloads)
mediaSession.logAdBreakStart {
id = "adbreak 2"
}
mediaSession.logAdStart {
id = "ad 2"
}
mediaSession.logAdClick()
mediaSession.logAdEnd()
mediaSession.logAdBreakEnd()
assertEquals(10, events.size)
events.forEach {
it.mediaContent.apply {
assertEquals("hello", name)
assertEquals("123", contentId)
assertEquals(1000L, duration)
}
}
events[0].assertTrue { it.eventName == MediaEventName.AD_BREAK_START && it.adBreak!!.id == "adbreak 1" }
events[1].assertTrue { it.eventName == MediaEventName.AD_START && it.mediaAd!!.id == "ad 1" }
events[2].assertTrue { it.eventName == MediaEventName.AD_CLICK && it.mediaAd!!.id == "ad 1" }
events[3].assertTrue { it.eventName == MediaEventName.AD_END }
events[4].assertTrue { it.eventName == MediaEventName.AD_BREAK_END }
events[5].assertTrue { it.eventName == MediaEventName.AD_BREAK_START && it.adBreak!!.id == "adbreak 2" }
events[6].assertTrue { it.eventName == MediaEventName.AD_START && it.mediaAd!!.id == "ad 2" }
events[7].assertTrue { it.eventName == MediaEventName.AD_CLICK && it.mediaAd!!.id == "ad 2" }
events[8].assertTrue { it.eventName == MediaEventName.AD_END }
events[9].assertTrue { it.eventName == MediaEventName.AD_BREAK_END}
}
fun BaseEvent.isPlayheadEvent(): Boolean {
return if (this is MediaEvent) {
eventName == MediaEventName.UPDATE_PLAYHEAD_POSITION
} else {
false
}
}
inner class MockMParticle : MParticle() {
var loggedEvents: MutableList<BaseEvent> = ArrayList()
override fun logEvent(baseEvent: BaseEvent) {
loggedEvents.add(baseEvent)
}
}
@Test
fun testLogMediaContentEnd() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
}
// logPlay is triggered to start media content time tracking.
mediaSession.logPlay()
// 1s delay added to account for the time spent on media content.
Thread.sleep(1000)
mediaSession.logMediaContentEnd()
// Another 1s delay added after logMediaContentEnd is triggered to
// account for time spent on media session (total = 2s).
Thread.sleep(1000)
mediaSession.logMediaSessionEnd()
val testContentTimeSpent = mediaSession.mediaContentTimeSpent
val testTimeSpent = mediaSession.mediaTimeSpent
assertNotEquals(testContentTimeSpent, testTimeSpent)
assertEquals(testContentTimeSpent, 1.0)
assertEquals(testTimeSpent, 2.0)
}
@Test
fun testLogPause() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
}
// logPlay is triggered to start media content time tracking.
mediaSession.logPlay()
// 1s delay added to account for the time spent on media content.
Thread.sleep(1000)
mediaSession.logPause()
// Another 1s delay added after logPause is triggered to
// account for time spent on media session (total = 2s).
Thread.sleep(1000)
mediaSession.logMediaSessionEnd()
val testContentTimeSpent = mediaSession.mediaContentTimeSpent
val testTimeSpent = mediaSession.mediaTimeSpent
assertNotEquals(testContentTimeSpent, testTimeSpent)
assertEquals(testContentTimeSpent, 1.0)
assertEquals(testTimeSpent, 2.0)
}
@Test
fun testLogAdSummary() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
}
val events = mutableListOf<MediaEvent>()
mediaSession.mediaEventListener = { event ->
events.add(event)
}
mediaSession.logAdStart { }
mediaSession.logAdBreakStart {
id = "123456"
title = "TestADBREAk"
}
mediaSession.logAdEnd()
mediaSession.logAdBreakEnd()
assertEquals(4, events.size)
events.forEach {
it.mediaContent.apply {
assertEquals("hello", name)
assertEquals("123", contentId)
assertEquals(1000L, duration)
}
}
assertNotNull(mparticle.loggedEvents[3].customAttributes)
assertNotNull(mparticle.loggedEvents[3].customAttributes?.get("ad_break_id"))
assertEquals("123456",mparticle.loggedEvents[3].customAttributes?.get("ad_break_id"))
}
@Test
fun testLogAdSummary_When_Break_ID_IS_NULL() {
val mparticle = MockMParticle()
val mediaSession = MediaSession.builder(mparticle) {
title = "hello"
mediaContentId ="123"
duration =1000
}
val events = mutableListOf<MediaEvent>()
mediaSession.mediaEventListener = { event ->
events.add(event)
}
mediaSession.logAdStart { }
mediaSession.logAdBreakStart {
title = "TestADBREAk"
}
mediaSession.logAdEnd()
mediaSession.logAdBreakEnd()
assertEquals(4, events.size)
events.forEach {
it.mediaContent.apply {
assertEquals("hello", name)
assertEquals("123", contentId)
assertEquals(1000L, duration)
}
}
assertNotNull(mparticle.loggedEvents[3].customAttributes)
assertNull(mparticle.loggedEvents[3].customAttributes?.get("ad_break_id"))
}
}
fun <T: Any> T.assertTrue(assertion: (T) -> Boolean) {
assertion(this)
}