Skip to content

Commit 2b08850

Browse files
committed
Fix unit tests
1 parent 6ebd8a8 commit 2b08850

File tree

3 files changed

+86
-110
lines changed

3 files changed

+86
-110
lines changed

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

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
2222
import com.google.common.truth.Truth.assertThat
2323
import com.google.firebase.FirebaseApp
2424
import com.google.firebase.concurrent.TestOnlyExecutors
25+
import com.google.firebase.installations.FirebaseInstallationsApi
26+
import com.google.firebase.sessions.ApplicationInfo
2527
import com.google.firebase.sessions.SessionEvents
2628
import com.google.firebase.sessions.testing.FakeFirebaseApp
2729
import com.google.firebase.sessions.testing.FakeFirebaseInstallations
28-
import com.google.firebase.sessions.testing.FakeLazy
2930
import com.google.firebase.sessions.testing.FakeRemoteConfigFetcher
31+
import kotlin.coroutines.CoroutineContext
3032
import kotlin.time.Duration.Companion.minutes
3133
import kotlin.time.Duration.Companion.seconds
3234
import kotlinx.coroutines.Dispatchers
@@ -56,20 +58,17 @@ class RemoteSettingsTest {
5658
val fakeFetcher = FakeRemoteConfigFetcher()
5759

5860
val remoteSettings =
59-
RemoteSettings(
61+
buildRemoteSettings(
6062
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext,
6163
firebaseInstallations,
6264
SessionEvents.getApplicationInfo(firebaseApp),
6365
fakeFetcher,
64-
lazySettingsCache =
65-
FakeLazy {
66-
SettingsCache(
67-
PreferenceDataStoreFactory.create(
68-
scope = this,
69-
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
70-
)
71-
)
72-
},
66+
SettingsCache(
67+
PreferenceDataStoreFactory.create(
68+
scope = this,
69+
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
70+
)
71+
),
7372
)
7473

7574
runCurrent()
@@ -99,20 +98,17 @@ class RemoteSettingsTest {
9998
val fakeFetcher = FakeRemoteConfigFetcher()
10099

101100
val remoteSettings =
102-
RemoteSettings(
101+
buildRemoteSettings(
103102
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext,
104103
firebaseInstallations,
105104
SessionEvents.getApplicationInfo(firebaseApp),
106105
fakeFetcher,
107-
lazySettingsCache =
108-
FakeLazy {
109-
SettingsCache(
110-
PreferenceDataStoreFactory.create(
111-
scope = this,
112-
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
113-
)
114-
)
115-
},
106+
SettingsCache(
107+
PreferenceDataStoreFactory.create(
108+
scope = this,
109+
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
110+
)
111+
),
116112
)
117113

118114
runCurrent()
@@ -144,20 +140,17 @@ class RemoteSettingsTest {
144140
val fakeFetcher = FakeRemoteConfigFetcher()
145141

146142
val remoteSettings =
147-
RemoteSettings(
143+
buildRemoteSettings(
148144
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext,
149145
firebaseInstallations,
150146
SessionEvents.getApplicationInfo(firebaseApp),
151147
fakeFetcher,
152-
lazySettingsCache =
153-
FakeLazy {
154-
SettingsCache(
155-
PreferenceDataStoreFactory.create(
156-
scope = this,
157-
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
158-
)
159-
)
160-
},
148+
SettingsCache(
149+
PreferenceDataStoreFactory.create(
150+
scope = this,
151+
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
152+
)
153+
),
161154
)
162155

163156
val fetchedResponse = JSONObject(VALID_RESPONSE)
@@ -200,20 +193,17 @@ class RemoteSettingsTest {
200193
val fakeFetcher = FakeRemoteConfigFetcher()
201194

202195
val remoteSettings =
203-
RemoteSettings(
196+
buildRemoteSettings(
204197
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext,
205198
firebaseInstallations,
206199
SessionEvents.getApplicationInfo(firebaseApp),
207200
fakeFetcher,
208-
lazySettingsCache =
209-
FakeLazy {
210-
SettingsCache(
211-
PreferenceDataStoreFactory.create(
212-
scope = this,
213-
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
214-
)
215-
)
216-
},
201+
SettingsCache(
202+
PreferenceDataStoreFactory.create(
203+
scope = this,
204+
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
205+
)
206+
),
217207
)
218208

219209
val fetchedResponse = JSONObject(VALID_RESPONSE)
@@ -269,20 +259,17 @@ class RemoteSettingsTest {
269259
.put("sampling_rate", 0.125)
270260

271261
val remoteSettingsWithDelay =
272-
RemoteSettings(
262+
buildRemoteSettings(
273263
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext,
274264
firebaseInstallations,
275265
SessionEvents.getApplicationInfo(firebaseApp),
276-
configsFetcher = fakeFetcherWithDelay,
277-
lazySettingsCache =
278-
FakeLazy {
279-
SettingsCache(
280-
PreferenceDataStoreFactory.create(
281-
scope = this,
282-
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
283-
)
284-
)
285-
},
266+
fakeFetcherWithDelay,
267+
SettingsCache(
268+
PreferenceDataStoreFactory.create(
269+
scope = this,
270+
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
271+
)
272+
),
286273
)
287274

288275
// Do the first fetch. This one should fetched the configsFetcher.
@@ -310,7 +297,7 @@ class RemoteSettingsTest {
310297
FirebaseApp.clearInstancesForTest()
311298
}
312299

313-
private companion object {
300+
internal companion object {
314301
const val SESSION_TEST_CONFIGS_NAME = "firebase_session_settings_test"
315302

316303
const val VALID_RESPONSE =
@@ -331,5 +318,30 @@ class RemoteSettingsTest {
331318
}
332319
}
333320
"""
321+
322+
/**
323+
* Build an instance of [RemoteSettings] using the Dagger factory.
324+
*
325+
* This is needed because the SDK vendors Dagger to a difference namespace, but it does not for
326+
* these unit tests. The [RemoteSettings.lazySettingsCache] has type [dagger.Lazy] in these
327+
* tests, but type `com.google.firebase.sessions.dagger.Lazy` in the SDK. This method to build
328+
* the instance is the easiest I could find that does not need any reference to [dagger.Lazy] in
329+
* the test code.
330+
*/
331+
fun buildRemoteSettings(
332+
backgroundDispatcher: CoroutineContext,
333+
firebaseInstallationsApi: FirebaseInstallationsApi,
334+
appInfo: ApplicationInfo,
335+
configsFetcher: CrashlyticsSettingsFetcher,
336+
settingsCache: SettingsCache,
337+
): RemoteSettings =
338+
RemoteSettings_Factory.create(
339+
{ backgroundDispatcher },
340+
{ firebaseInstallationsApi },
341+
{ appInfo },
342+
{ configsFetcher },
343+
{ settingsCache },
344+
)
345+
.get()
334346
}
335347
}

firebase-sessions/src/test/kotlin/com/google/firebase/sessions/settings/SessionsSettingsTest.kt

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import com.google.firebase.sessions.SessionDataStoreConfigs
2626
import com.google.firebase.sessions.SessionEvents
2727
import com.google.firebase.sessions.testing.FakeFirebaseApp
2828
import com.google.firebase.sessions.testing.FakeFirebaseInstallations
29-
import com.google.firebase.sessions.testing.FakeLazy
3029
import com.google.firebase.sessions.testing.FakeRemoteConfigFetcher
3130
import com.google.firebase.sessions.testing.FakeSettingsProvider
3231
import kotlin.time.Duration.Companion.minutes
@@ -108,20 +107,17 @@ class SessionsSettingsTest {
108107
val fakeFetcher = FakeRemoteConfigFetcher(JSONObject(VALID_RESPONSE))
109108

110109
val remoteSettings =
111-
RemoteSettings(
110+
RemoteSettingsTest.buildRemoteSettings(
112111
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext,
113112
firebaseInstallations,
114113
SessionEvents.getApplicationInfo(firebaseApp),
115114
fakeFetcher,
116-
lazySettingsCache =
117-
FakeLazy {
118-
SettingsCache(
119-
PreferenceDataStoreFactory.create(
120-
scope = this,
121-
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
122-
)
123-
)
124-
},
115+
SettingsCache(
116+
PreferenceDataStoreFactory.create(
117+
scope = this,
118+
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
119+
)
120+
),
125121
)
126122

127123
val sessionsSettings =
@@ -154,20 +150,17 @@ class SessionsSettingsTest {
154150
val fakeFetcher = FakeRemoteConfigFetcher(JSONObject(VALID_RESPONSE))
155151

156152
val remoteSettings =
157-
RemoteSettings(
153+
RemoteSettingsTest.buildRemoteSettings(
158154
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext,
159155
firebaseInstallations,
160156
SessionEvents.getApplicationInfo(firebaseApp),
161157
fakeFetcher,
162-
lazySettingsCache =
163-
FakeLazy {
164-
SettingsCache(
165-
PreferenceDataStoreFactory.create(
166-
scope = this,
167-
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
168-
)
169-
)
170-
},
158+
SettingsCache(
159+
PreferenceDataStoreFactory.create(
160+
scope = this,
161+
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
162+
)
163+
),
171164
)
172165

173166
val sessionsSettings =
@@ -206,20 +199,17 @@ class SessionsSettingsTest {
206199
fakeFetcher.responseJSONObject = JSONObject(invalidResponse)
207200

208201
val remoteSettings =
209-
RemoteSettings(
202+
RemoteSettingsTest.buildRemoteSettings(
210203
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext,
211204
firebaseInstallations,
212205
SessionEvents.getApplicationInfo(firebaseApp),
213206
fakeFetcher,
214-
lazySettingsCache =
215-
FakeLazy {
216-
SettingsCache(
217-
PreferenceDataStoreFactory.create(
218-
scope = this,
219-
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
220-
)
221-
)
222-
},
207+
SettingsCache(
208+
PreferenceDataStoreFactory.create(
209+
scope = this,
210+
produceFile = { context.preferencesDataStoreFile(SESSION_TEST_CONFIGS_NAME) },
211+
)
212+
),
223213
)
224214

225215
val sessionsSettings =

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

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)