RTCConfigurationTest.kt
4.2 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
/*
* Copyright 2023 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.livekit.android.webrtc
import io.livekit.android.BaseTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.mockito.Mockito
import org.webrtc.PeerConnection.RTCConfiguration
class RTCConfigurationTest : BaseTest() {
@Test
fun copyTest() {
val originalConfig = RTCConfiguration(mutableListOf())
fillWithMockData(originalConfig)
val newConfig = originalConfig.copy()
newConfig::class.java
.declaredFields
.forEach { field ->
if (field.isSynthetic) {
return@forEach
}
assertEquals("Failed on ${field.name}", field.get(originalConfig), field.get(newConfig))
}
}
// Test to make sure the copy test is actually checking properly
@Test
fun copyFailureCheckTest() {
val originalConfig = RTCConfiguration(mutableListOf())
fillWithMockData(originalConfig)
val newConfig = originalConfig.copy()
newConfig.activeResetSrtpParams = false
var caughtError = false
try {
newConfig::class.java
.declaredFields
.forEach { field ->
assertEquals("Failed on ${field.name}", field.get(originalConfig), field.get(newConfig))
}
} catch (e: java.lang.AssertionError) {
// Error expected
caughtError = true
}
assertTrue(caughtError)
}
private fun fillWithMockData(config: RTCConfiguration) {
config::class.java
.declaredFields
.forEach { field ->
// Ignore iceServers.
if (field.name == "iceServers") {
return@forEach
}
// Ignore synthetic fields. Jacoco will create some that aren't relevant to this test.
if (field.isSynthetic) {
return@forEach
}
val value = field.get(config)
val newValue = if (value == null) {
when (field.type) {
Byte::class.javaObjectType -> 1.toByte()
Short::class.javaObjectType -> 1.toShort()
Int::class.javaObjectType -> 1
Long::class.javaObjectType -> 1.toLong()
Float::class.javaObjectType -> 1.toFloat()
Double::class.javaObjectType -> 1.toDouble()
Boolean::class.javaObjectType -> true
Char::class.javaObjectType -> 1.toChar()
String::class.javaObjectType -> "mock string"
else -> Mockito.mock(field.type)
}
} else {
when (value::class.javaObjectType) {
Byte::class.javaObjectType -> ((value as Byte) + 1).toByte()
Short::class.javaObjectType -> ((value as Short) + 1).toShort()
Int::class.javaObjectType -> (value as Int) + 1
Long::class.javaObjectType -> (value as Long) + 1
Float::class.javaObjectType -> (value as Float) + 1
Double::class.javaObjectType -> (value as Double) + 1
Boolean::class.javaObjectType -> !(value as Boolean)
Char::class.javaObjectType -> (value as Char) + 1
String::class.javaObjectType -> "mock string"
else -> Mockito.mock(field.type)
}
}
field.set(config, newValue)
}
}
}