Skip to content

Commit 3408d56

Browse files
authored
chore: add test for json helpers (#13)
* chore: add test for json helpers * add more test for FormbricksViewModel * add WebInterfaceInstrument test
1 parent 0e7c172 commit 3408d56

File tree

3 files changed

+280
-0
lines changed

3 files changed

+280
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.formbricks.android.helper
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import kotlinx.serialization.json.*
5+
import org.junit.Assert.*
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
@RunWith(AndroidJUnit4::class)
10+
class JsonHelperInstrumentedTest {
11+
@Test
12+
fun testMapToJsonElement_withPrimitives() {
13+
val map = mapOf(
14+
"string" to "value",
15+
"int" to 1,
16+
"double" to 2.5,
17+
"bool" to true
18+
)
19+
val json = mapToJsonElement(map)
20+
assertEquals("value", json.jsonObject["string"]?.jsonPrimitive?.content)
21+
assertEquals(1, json.jsonObject["int"]?.jsonPrimitive?.int)
22+
assertEquals(2.5, json.jsonObject["double"]?.jsonPrimitive?.double ?: 0.0, 0.0)
23+
assertEquals(true, json.jsonObject["bool"]?.jsonPrimitive?.boolean)
24+
}
25+
26+
@Test
27+
fun testMapToJsonElement_withNestedMap() {
28+
val map = mapOf(
29+
"outer" to mapOf(
30+
"inner" to "inside"
31+
)
32+
)
33+
val json = mapToJsonElement(map)
34+
assertEquals("inside", json.jsonObject["outer"]?.jsonObject?.get("inner")?.jsonPrimitive?.content)
35+
}
36+
37+
@Test
38+
fun testMapToJsonElement_withList() {
39+
val map = mapOf(
40+
"list" to listOf(1, 2, 3)
41+
)
42+
val json = mapToJsonElement(map)
43+
val arr = json.jsonObject["list"] as JsonArray
44+
assertEquals(3, arr.size)
45+
assertEquals(1, arr[0].jsonPrimitive.int)
46+
assertEquals(2, arr[1].jsonPrimitive.int)
47+
assertEquals(3, arr[2].jsonPrimitive.int)
48+
}
49+
50+
@Test
51+
fun testMapToJsonElement_withNull() {
52+
val map = mapOf(
53+
"something" to null
54+
)
55+
val json = mapToJsonElement(map)
56+
assertTrue(json.jsonObject["something"] is JsonNull)
57+
}
58+
59+
@Test
60+
fun testMapToJsonElement_withListOfMaps() {
61+
val map = mapOf(
62+
"list" to listOf(
63+
mapOf("a" to 1),
64+
mapOf("b" to 2)
65+
)
66+
)
67+
val json = mapToJsonElement(map)
68+
val arr = json.jsonObject["list"] as JsonArray
69+
assertEquals(2, arr.size)
70+
assertEquals(1, arr[0].jsonObject["a"]?.jsonPrimitive?.int)
71+
assertEquals(2, arr[1].jsonObject["b"]?.jsonPrimitive?.int)
72+
}
73+
74+
@Test(expected = IllegalArgumentException::class)
75+
fun testMapToJsonElement_withUnsupportedType() {
76+
val map = mapOf("bad" to Any())
77+
mapToJsonElement(map)
78+
}
79+
80+
@Test
81+
fun testMapToJsonElementItem_withPrimitives() {
82+
assertEquals(JsonPrimitive("hello"), mapToJsonElementItem("hello"))
83+
assertEquals(JsonPrimitive(42), mapToJsonElementItem(42))
84+
assertEquals(JsonPrimitive(true), mapToJsonElementItem(true))
85+
}
86+
87+
@Test
88+
fun testMapToJsonElementItem_withNull() {
89+
assertTrue(mapToJsonElementItem(null) is JsonNull)
90+
}
91+
92+
@Test
93+
fun testMapToJsonElementItem_withList() {
94+
val list = listOf("a", "b")
95+
val json = mapToJsonElementItem(list)
96+
assertTrue(json is JsonArray)
97+
assertEquals("a", (json as JsonArray)[0].jsonPrimitive.content)
98+
assertEquals("b", json[1].jsonPrimitive.content)
99+
}
100+
101+
@Test(expected = IllegalArgumentException::class)
102+
fun testMapToJsonElementItem_withUnsupportedType() {
103+
mapToJsonElementItem(Any())
104+
}
105+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.formbricks.android.webview
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import com.formbricks.android.Formbricks
5+
import com.formbricks.android.manager.UserManager
6+
import com.formbricks.android.model.environment.EnvironmentDataHolder
7+
import com.formbricks.android.model.environment.EnvironmentResponseData
8+
import com.formbricks.android.model.environment.EnvironmentData
9+
import com.formbricks.android.model.environment.Project
10+
import com.formbricks.android.model.environment.Survey
11+
import org.junit.Assert.*
12+
import org.junit.Before
13+
import org.junit.Test
14+
import org.junit.runner.RunWith
15+
16+
@RunWith(AndroidJUnit4::class)
17+
class FormbricksViewModelInstrumentedTest {
18+
@Before
19+
fun setup() {
20+
// Set up static singletons with minimal values
21+
Formbricks.appUrl = "https://test.formbricks.com"
22+
Formbricks.environmentId = "env123"
23+
Formbricks.language = "en"
24+
// Use reflection to set private contactId
25+
val contactIdField = UserManager::class.java.getDeclaredField("backingContactId")
26+
contactIdField.isAccessible = true
27+
contactIdField.set(UserManager, "contact123")
28+
}
29+
30+
@Test
31+
fun testGetJson_minimalEnvironment() {
32+
// Minimal survey and environment
33+
val surveyId = "survey1"
34+
val survey = Survey(
35+
id = surveyId,
36+
name = "Test Survey",
37+
triggers = null,
38+
recontactDays = null,
39+
displayLimit = null,
40+
delay = null,
41+
displayPercentage = null,
42+
displayOption = null,
43+
segment = null,
44+
styling = null,
45+
languages = null
46+
)
47+
val project = Project(
48+
id = "proj1",
49+
recontactDays = null,
50+
clickOutsideClose = null,
51+
darkOverlay = null,
52+
placement = null,
53+
inAppSurveyBranding = null,
54+
styling = null
55+
)
56+
val envData = EnvironmentData(
57+
surveys = listOf(survey),
58+
actionClasses = null,
59+
project = project
60+
)
61+
val envResponseData = EnvironmentResponseData(
62+
data = envData,
63+
expiresAt = null
64+
)
65+
val envHolder = EnvironmentDataHolder(
66+
data = envResponseData,
67+
originalResponseMap = mapOf()
68+
)
69+
val viewModel = FormbricksViewModel()
70+
val json = viewModel.javaClass.getDeclaredMethod("getJson", EnvironmentDataHolder::class.java, String::class.java)
71+
.apply { isAccessible = true }
72+
.invoke(viewModel, envHolder, surveyId) as String
73+
// Check that the output JSON string contains expected keys/values
74+
assertTrue(json.contains("\"survey\""))
75+
assertTrue(json.contains("\"isBrandingEnabled\":true"))
76+
assertTrue(json.contains("https://test.formbricks.com"))
77+
assertTrue(json.contains("env123"))
78+
assertTrue(json.contains("contact123"))
79+
assertTrue(json.contains("\"languageCode\":\"default\""))
80+
}
81+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.formbricks.android.webview
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import com.formbricks.android.model.javascript.EventType
5+
import com.formbricks.android.model.javascript.FileUploadData
6+
import org.junit.Assert.*
7+
import org.junit.Before
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
11+
@RunWith(AndroidJUnit4::class)
12+
class WebAppInterfaceInstrumentedTest {
13+
private lateinit var callback: FakeCallback
14+
private lateinit var webAppInterface: WebAppInterface
15+
16+
class FakeCallback : WebAppInterface.WebAppCallback {
17+
var closed = false
18+
var displayCreated = false
19+
var responseCreated = false
20+
var filePick: FileUploadData? = null
21+
var surveyLibraryLoadError = false
22+
override fun onClose() { closed = true }
23+
override fun onDisplayCreated() { displayCreated = true }
24+
override fun onResponseCreated() { responseCreated = true }
25+
override fun onFilePick(data: FileUploadData) { filePick = data }
26+
override fun onSurveyLibraryLoadError() { surveyLibraryLoadError = true }
27+
}
28+
29+
@Before
30+
fun setup() {
31+
callback = FakeCallback()
32+
webAppInterface = WebAppInterface(callback)
33+
}
34+
35+
@Test
36+
fun testMessage_onClose() {
37+
val json = "{\"event\":\"onClose\"}"
38+
webAppInterface.message(json)
39+
assertTrue(callback.closed)
40+
}
41+
42+
@Test
43+
fun testMessage_onDisplayCreated() {
44+
val json = "{\"event\":\"onDisplayCreated\"}"
45+
webAppInterface.message(json)
46+
assertTrue(callback.displayCreated)
47+
}
48+
49+
@Test
50+
fun testMessage_onResponseCreated() {
51+
val json = "{\"event\":\"onResponseCreated\"}"
52+
webAppInterface.message(json)
53+
assertTrue(callback.responseCreated)
54+
}
55+
56+
@Test
57+
fun testMessage_onFilePick() {
58+
val json = "{\"event\":\"onFilePick\",\"fileUploadParams\":{\"allowedFileExtensions\":\"jpg\",\"allowMultipleFiles\":true}}"
59+
webAppInterface.message(json)
60+
assertNotNull(callback.filePick)
61+
assertEquals("jpg", callback.filePick?.fileUploadParams?.allowedFileExtensions)
62+
assertEquals(true, callback.filePick?.fileUploadParams?.allowMultipleFiles)
63+
}
64+
65+
@Test
66+
fun testMessage_onSurveyLibraryLoadError() {
67+
val json = "{\"event\":\"onSurveyLibraryLoadError\"}"
68+
webAppInterface.message(json)
69+
assertTrue(callback.surveyLibraryLoadError)
70+
}
71+
72+
@Test
73+
fun testMessage_invalidJson() {
74+
// Should not throw, should not call any callback
75+
webAppInterface.message("not a json")
76+
assertFalse(callback.closed)
77+
assertFalse(callback.displayCreated)
78+
assertFalse(callback.responseCreated)
79+
assertNull(callback.filePick)
80+
assertFalse(callback.surveyLibraryLoadError)
81+
}
82+
83+
@Test
84+
fun testMessage_unknownEvent() {
85+
val json = "{\"event\":\"unknown_event\"}"
86+
webAppInterface.message(json)
87+
// Should not call any callback
88+
assertFalse(callback.closed)
89+
assertFalse(callback.displayCreated)
90+
assertFalse(callback.responseCreated)
91+
assertNull(callback.filePick)
92+
assertFalse(callback.surveyLibraryLoadError)
93+
}
94+
}

0 commit comments

Comments
 (0)