@@ -22,11 +22,13 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
2222import com.google.common.truth.Truth.assertThat
2323import com.google.firebase.FirebaseApp
2424import com.google.firebase.concurrent.TestOnlyExecutors
25+ import com.google.firebase.installations.FirebaseInstallationsApi
26+ import com.google.firebase.sessions.ApplicationInfo
2527import com.google.firebase.sessions.SessionEvents
2628import com.google.firebase.sessions.testing.FakeFirebaseApp
2729import com.google.firebase.sessions.testing.FakeFirebaseInstallations
28- import com.google.firebase.sessions.testing.FakeLazy
2930import com.google.firebase.sessions.testing.FakeRemoteConfigFetcher
31+ import kotlin.coroutines.CoroutineContext
3032import kotlin.time.Duration.Companion.minutes
3133import kotlin.time.Duration.Companion.seconds
3234import 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}
0 commit comments