Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ dependencies {
mavenPublishing {
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)

signAllPublications()
signAllPublications() // disabled for local publishing

coordinates(groupId, artifactId, version.toString())

Expand Down Expand Up @@ -127,7 +127,7 @@ mavenPublishing {
// Add JaCoCo tasks
tasks.register<JacocoReport>("jacocoAndroidTestReport") {
dependsOn("connectedDebugAndroidTest")

reports {
xml.required.set(true)
html.required.set(true)
Expand Down Expand Up @@ -174,11 +174,11 @@ tasks.register<JacocoReport>("jacocoAndroidTestReport") {
// Configure Sonar
sonar {
properties {
property("sonar.coverage.jacoco.xmlReportPaths",
property("sonar.coverage.jacoco.xmlReportPaths",
layout.buildDirectory.file("reports/jacoco/jacocoAndroidTestReport/jacocoAndroidTestReport.xml").get().asFile.path)
}
}

tasks.sonar {
dependsOn("jacocoAndroidTestReport")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import java.util.concurrent.TimeUnit
@RunWith(AndroidJUnit4::class)
class FormbricksInstrumentedTest {

private val environmentId = "environmentId"
private val workspaceId = "workspaceId"
private val appUrl = "https://example.com"
private val userId = "6CCCE716-6783-4D0F-8344-9C7DFA43D8F7"
private val surveyID = "cm6ovw6j7000gsf0kduf4oo4i"
Expand All @@ -43,7 +43,7 @@ class FormbricksInstrumentedTest {
Formbricks.language = "default"
UserManager.logout()
UpdateQueue.reset()
SurveyManager.environmentDataHolder = null
SurveyManager.workspaceDataHolder = null
SurveyManager.filteredSurveys.clear()
FormbricksApi.service = MockFormbricksApiService()
}
Expand All @@ -56,7 +56,7 @@ class FormbricksInstrumentedTest {
// Everything should be in the default state
assertFalse(Formbricks.isInitialized)
assertEquals(0, SurveyManager.filteredSurveys.size)
assertNull(SurveyManager.environmentDataHolder)
assertNull(SurveyManager.workspaceDataHolder)
assertNull(UserManager.userId)
assertEquals("default", Formbricks.language)

Expand All @@ -75,7 +75,7 @@ class FormbricksInstrumentedTest {
Formbricks.setLanguage("")

// Call the setup and initialize the SDK
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, environmentId).setLoggingEnabled(true).build())
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

// Should be ignored, becuase we don't have user ID yet
Expand All @@ -86,7 +86,7 @@ class FormbricksInstrumentedTest {
// Verify the base variables are set properly
assertTrue(Formbricks.isInitialized)
assertEquals(appUrl, Formbricks.appUrl)
assertEquals(environmentId, Formbricks.environmentId)
assertEquals(workspaceId, Formbricks.workspaceId)

// User manager default state. There is no user yet.
assertEquals(UserManager.displays?.count(), 0)
Expand All @@ -96,7 +96,7 @@ class FormbricksInstrumentedTest {
// Check error state handling
(FormbricksApi.service as MockFormbricksApiService).isErrorResponseNeeded = true
assertFalse(SurveyManager.hasApiError)
SurveyManager.refreshEnvironmentIfNeeded(true)
SurveyManager.refreshWorkspaceIfNeeded(true)
waitForSeconds(3) // Increased wait time to 3 seconds
assertTrue(SurveyManager.hasApiError)
(FormbricksApi.service as MockFormbricksApiService).isErrorResponseNeeded = false
Expand All @@ -107,8 +107,8 @@ class FormbricksInstrumentedTest {
assertEquals(userId, UserManager.userId)
assertNotNull(UserManager.syncTimer)

// The environment should be fetched already
assertNotNull(SurveyManager.environmentDataHolder)
// The workspace should be fetched already
assertNotNull(SurveyManager.workspaceDataHolder)

// Check if the filter method works properly
assertEquals(1, SurveyManager.filteredSurveys.size)
Expand All @@ -126,7 +126,7 @@ class FormbricksInstrumentedTest {
assertNotNull("Should have a survey before tracking", firstSurveyBeforeTrack)
assertEquals("Should have the correct survey ID", surveyID, firstSurveyBeforeTrack?.id)

val actionClasses = SurveyManager.environmentDataHolder?.data?.data?.actionClasses ?: listOf()
val actionClasses = SurveyManager.workspaceDataHolder?.data?.data?.actionClasses ?: listOf()
val clickDemoButtonAction = actionClasses.firstOrNull { it.key == "click_demo_button" }
assertNotNull("Should have click_demo_button action class", clickDemoButtonAction)

Expand Down Expand Up @@ -183,7 +183,7 @@ class FormbricksInstrumentedTest {
@Test
fun testSetAttributesWithUserId() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, environmentId).setLoggingEnabled(true).build())
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

// Set userId first, then set attributes - exercises UpdateQueue.setAttributes with a valid userId
Expand All @@ -204,7 +204,7 @@ class FormbricksInstrumentedTest {
@Test
fun testAddAttributeWithUserId() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, environmentId).setLoggingEnabled(true).build())
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

// Set userId first, then add attributes - exercises UpdateQueue.addAttribute with a valid userId
Expand All @@ -223,7 +223,7 @@ class FormbricksInstrumentedTest {
@Test
fun testSetLanguageWithUserId() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, environmentId).setLoggingEnabled(true).build())
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

// Set userId first, then set language - exercises the if-branch in UpdateQueue.setLanguage
Expand All @@ -241,7 +241,7 @@ class FormbricksInstrumentedTest {
@Test
fun testSetUserIdSameValueIsNoOp() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, environmentId).setLoggingEnabled(true).build())
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

Formbricks.setUserId(userId)
Expand All @@ -256,7 +256,7 @@ class FormbricksInstrumentedTest {
@Test
fun testSetUserIdDifferentValueOverridesPrevious() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, environmentId).setLoggingEnabled(true).build())
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

Formbricks.setUserId(userId)
Expand Down Expand Up @@ -292,7 +292,7 @@ class FormbricksInstrumentedTest {
@Test
fun testSyncUserSetsLanguageFromResponse() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, environmentId).setLoggingEnabled(true).build())
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

assertEquals("default", Formbricks.language)
Expand Down Expand Up @@ -321,7 +321,7 @@ class FormbricksInstrumentedTest {
@Test
fun testSyncUserCatchBlockOnApiError() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, environmentId).setLoggingEnabled(true).build())
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

// Enable error mode so postUser returns a failure, exercising the catch block
Expand All @@ -337,7 +337,7 @@ class FormbricksInstrumentedTest {
@Test
fun testLogoutClearsAllUserState() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, environmentId).setLoggingEnabled(true).build())
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

// Set up a user so we have state to clear
Expand All @@ -361,6 +361,52 @@ class FormbricksInstrumentedTest {
assertEquals("default", Formbricks.language)
}

// MARK: - workspaceId / environmentId parameter tests

/** The deprecated `withEnvironmentId` factory is still supported for backward compatibility. */
@Suppress("DEPRECATION")
@Test
fun testSetupWithDeprecatedEnvironmentIdFactory() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val legacyId = "legacy-env-id"
val config = FormbricksConfig.Builder.withEnvironmentId(appUrl, legacyId)
.setLoggingEnabled(true)
.build()
Formbricks.setup(appContext, config)
waitForSeconds(1)

assertTrue(Formbricks.isInitialized)
assertEquals("environmentId should be stored as workspaceId", legacyId, Formbricks.workspaceId)
assertTrue(config.usedDeprecatedEnvironmentId)
}

/** New `Builder(workspaceId)` path does not mark the config as using a deprecated parameter. */
@Test
fun testSetupWithWorkspaceIdDoesNotFlagDeprecation() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val config = FormbricksConfig.Builder(appUrl, workspaceId)
.setLoggingEnabled(true)
.build()
Formbricks.setup(appContext, config)
waitForSeconds(1)

assertTrue(Formbricks.isInitialized)
assertEquals(workspaceId, Formbricks.workspaceId)
assertFalse(config.usedDeprecatedEnvironmentId)
}

/** The legacy `Formbricks.environmentId` accessor still returns the canonical id. */
@Suppress("DEPRECATION")
@Test
fun testLegacyEnvironmentIdAccessorMirrorsWorkspaceId() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Formbricks.setup(appContext, FormbricksConfig.Builder(appUrl, workspaceId).setLoggingEnabled(true).build())
waitForSeconds(1)

assertEquals(workspaceId, Formbricks.environmentId)
assertEquals(Formbricks.workspaceId, Formbricks.environmentId)
}

private fun waitForSeconds(seconds: Long) {
val latch = CountDownLatch(1)
latch.await(seconds, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.formbricks.android

import androidx.test.platform.app.InstrumentationRegistry
import com.formbricks.android.model.environment.EnvironmentDataHolder
import com.formbricks.android.model.environment.EnvironmentResponse
import com.formbricks.android.model.workspace.WorkspaceDataHolder
import com.formbricks.android.model.workspace.WorkspaceResponse
import com.formbricks.android.model.user.PostUserBody
import com.formbricks.android.model.user.UserResponse
import com.formbricks.android.network.FormbricksApiService
Expand All @@ -11,32 +11,32 @@ import com.formbricks.android.model.error.SDKError

class MockFormbricksApiService: FormbricksApiService() {
private val gson = Gson()
private val environment: EnvironmentResponse
private val workspace: WorkspaceResponse
internal var user: UserResponse
var isErrorResponseNeeded = false

init {
val context = InstrumentationRegistry.getInstrumentation().context
val environmentJson = context.assets.open("Environment.json").bufferedReader().readText()
val workspaceJson = context.assets.open("Workspace.json").bufferedReader().readText()
val userJson = context.assets.open("User.json").bufferedReader().readText()
environment = gson.fromJson(environmentJson, EnvironmentResponse::class.java)

workspace = gson.fromJson(workspaceJson, WorkspaceResponse::class.java)
user = gson.fromJson(userJson, UserResponse::class.java)
}

override fun getEnvironmentStateObject(environmentId: String): Result<EnvironmentDataHolder> {
override fun getWorkspaceStateObject(workspaceId: String): Result<WorkspaceDataHolder> {
return if (isErrorResponseNeeded) {
Result.failure(SDKError.unableToRefreshEnvironment)
} else {
Result.success(EnvironmentDataHolder(environment.data, mapOf()))
Result.success(WorkspaceDataHolder(workspace.data, mapOf()))
}
}

override fun postUser(environmentId: String, body: PostUserBody): Result<UserResponse> {
override fun postUser(workspaceId: String, body: PostUserBody): Result<UserResponse> {
return if (isErrorResponseNeeded) {
Result.failure(SDKError.unableToPostResponse)
} else {
Result.success(user)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.formbricks.android.manager

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.formbricks.android.model.environment.*
import com.formbricks.android.model.workspace.*
import com.formbricks.android.model.user.Display
import org.junit.Assert.*
import org.junit.Before
Expand All @@ -15,8 +15,8 @@ class SurveyManagerInstrumentedTest {
fun setup() {
// Reset UserManager state before each test
UserManager.lastDisplayedAt = null
// Reset SurveyManager environment state
setBackingEnvironmentDataHolder(null)
// Reset SurveyManager workspace state
setBackingWorkspaceDataHolder(null)
}

@Test
Expand Down Expand Up @@ -434,8 +434,8 @@ class SurveyManagerInstrumentedTest {

// region helper methods

private fun setBackingEnvironmentDataHolder(value: EnvironmentDataHolder?) {
val field = SurveyManager::class.java.getDeclaredField("backingEnvironmentDataHolder")
private fun setBackingWorkspaceDataHolder(value: WorkspaceDataHolder?) {
val field = SurveyManager::class.java.getDeclaredField("backingWorkspaceDataHolder")
field.isAccessible = true
field.set(SurveyManager, value)
}
Expand Down
Loading
Loading