Skip to content

Commit b7ff899

Browse files
committed
Add failure test case for RemoteSettingsFetcher
1 parent ab88144 commit b7ff899

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/settings/RemoteSettingsFetcher.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ internal interface CrashlyticsSettingsFetcher {
3333
)
3434
}
3535

36-
internal class RemoteSettingsFetcher(private val appInfo: ApplicationInfo) :
37-
CrashlyticsSettingsFetcher {
36+
internal class RemoteSettingsFetcher(
37+
private val appInfo: ApplicationInfo,
38+
private val baseUrl: String = FIREBASE_SESSIONS_BASE_URL_STRING
39+
) : CrashlyticsSettingsFetcher {
3840
override suspend fun doConfigFetch(
3941
headerOptions: Map<String, String>,
4042
onSuccess: suspend (JSONObject) -> Unit,
@@ -72,7 +74,7 @@ internal class RemoteSettingsFetcher(private val appInfo: ApplicationInfo) :
7274
val uri =
7375
Uri.Builder()
7476
.scheme("https")
75-
.authority(FIREBASE_SESSIONS_BASE_URL_STRING)
77+
.authority(baseUrl)
7678
.appendPath("spi")
7779
.appendPath("v2")
7880
.appendPath("platforms")

firebase-sessions/src/test/kotlin/com/google/firebase/sessions/RemoteSettingsTest.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@ import com.google.common.truth.Truth.assertThat
2121
import com.google.firebase.FirebaseApp
2222
import com.google.firebase.concurrent.TestOnlyExecutors
2323
import com.google.firebase.sessions.settings.RemoteSettings
24+
import com.google.firebase.sessions.settings.RemoteSettingsFetcher
2425
import com.google.firebase.sessions.testing.FakeFirebaseApp
2526
import com.google.firebase.sessions.testing.FakeFirebaseInstallations
2627
import com.google.firebase.sessions.testing.FakeRemoteConfigFetcher
28+
import com.google.firebase.sessions.testing.TestSessionEventData.TEST_APPLICATION_INFO
2729
import kotlin.time.Duration.Companion.minutes
30+
import kotlinx.coroutines.ExperimentalCoroutinesApi
2831
import kotlinx.coroutines.asCoroutineDispatcher
2932
import kotlinx.coroutines.test.runTest
3033
import org.json.JSONObject
3134
import org.junit.After
3235
import org.junit.Test
3336
import org.junit.runner.RunWith
3437

38+
@OptIn(ExperimentalCoroutinesApi::class)
3539
@RunWith(AndroidJUnit4::class)
3640
class RemoteSettingsTest {
3741

3842
@Test
39-
fun RemoteSettings_successfulFetchCachesValues() = runTest {
43+
fun remoteSettings_successfulFetchCachesValues() = runTest {
4044
val firebaseApp = FakeFirebaseApp().firebaseApp
4145
val context = firebaseApp.applicationContext
4246
val firebaseInstallations = FakeFirebaseInstallations("FaKeFiD")
@@ -67,7 +71,7 @@ class RemoteSettingsTest {
6771
}
6872

6973
@Test
70-
fun RemoteSettings_successfulFetchWithLessConfigsCachesOnlyReceivedValues() = runTest {
74+
fun remoteSettings_successfulFetchWithLessConfigsCachesOnlyReceivedValues() = runTest {
7175
val firebaseApp = FakeFirebaseApp().firebaseApp
7276
val context = firebaseApp.applicationContext
7377
val firebaseInstallations = FakeFirebaseInstallations("FaKeFiD")
@@ -100,7 +104,7 @@ class RemoteSettingsTest {
100104
}
101105

102106
@Test
103-
fun RemoteSettings_successfulRefetchUpdatesCache() = runTest {
107+
fun remoteSettings_successfulReFetchUpdatesCache() = runTest {
104108
val firebaseApp = FakeFirebaseApp().firebaseApp
105109
val context = firebaseApp.applicationContext
106110
val firebaseInstallations = FakeFirebaseInstallations("FaKeFiD")
@@ -146,7 +150,7 @@ class RemoteSettingsTest {
146150
}
147151

148152
@Test
149-
fun RemoteSettings_successfulFetchWithEmptyConfigRetainsOldConfigs() = runTest {
153+
fun remoteSettings_successfulFetchWithEmptyConfigRetainsOldConfigs() = runTest {
150154
val firebaseApp = FakeFirebaseApp().firebaseApp
151155
val context = firebaseApp.applicationContext
152156
val firebaseInstallations = FakeFirebaseInstallations("FaKeFiD")
@@ -186,6 +190,20 @@ class RemoteSettingsTest {
186190
remoteSettings.clearCachedSettings()
187191
}
188192

193+
@Test
194+
fun remoteSettingsFetcher_badFetch_callsOnFailure() = runTest {
195+
var failure: String? = null
196+
197+
RemoteSettingsFetcher(TEST_APPLICATION_INFO, baseUrl = "this.url.is.invalid")
198+
.doConfigFetch(
199+
headerOptions = emptyMap(),
200+
onSuccess = {},
201+
onFailure = { failure = it },
202+
)
203+
204+
assertThat(failure).isNotNull()
205+
}
206+
189207
@After
190208
fun cleanUp() {
191209
FirebaseApp.clearInstancesForTest()

firebase-sessions/src/test/kotlin/com/google/firebase/sessions/testing/TestSessionEventData.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,25 @@ internal object TestSessionEventData {
5858
firebaseInstallationId = "",
5959
)
6060

61+
val TEST_APPLICATION_INFO =
62+
ApplicationInfo(
63+
appId = FakeFirebaseApp.MOCK_APP_ID,
64+
deviceModel = Build.MODEL,
65+
sessionSdkVersion = BuildConfig.VERSION_NAME,
66+
osVersion = Build.VERSION.RELEASE,
67+
logEnvironment = LogEnvironment.LOG_ENVIRONMENT_PROD,
68+
AndroidApplicationInfo(
69+
packageName = ApplicationProvider.getApplicationContext<Context>().packageName,
70+
versionName = FakeFirebaseApp.MOCK_APP_VERSION,
71+
appBuildVersion = FakeFirebaseApp.MOCK_APP_BUILD_VERSION,
72+
deviceManufacturer = Build.MANUFACTURER,
73+
),
74+
)
75+
6176
val TEST_SESSION_EVENT =
6277
SessionEvent(
6378
eventType = EventType.SESSION_START,
6479
sessionData = TEST_SESSION_DATA,
65-
applicationInfo =
66-
ApplicationInfo(
67-
appId = FakeFirebaseApp.MOCK_APP_ID,
68-
deviceModel = Build.MODEL,
69-
sessionSdkVersion = BuildConfig.VERSION_NAME,
70-
osVersion = Build.VERSION.RELEASE,
71-
logEnvironment = LogEnvironment.LOG_ENVIRONMENT_PROD,
72-
AndroidApplicationInfo(
73-
packageName = ApplicationProvider.getApplicationContext<Context>().packageName,
74-
versionName = FakeFirebaseApp.MOCK_APP_VERSION,
75-
appBuildVersion = FakeFirebaseApp.MOCK_APP_BUILD_VERSION,
76-
deviceManufacturer = Build.MANUFACTURER,
77-
),
78-
)
80+
applicationInfo = TEST_APPLICATION_INFO
7981
)
8082
}

0 commit comments

Comments
 (0)