Skip to content

Commit

Permalink
Adds fake implementations of new interfaces and creates a Fake Regist…
Browse files Browse the repository at this point in the history
…rar to be used in unit tests.
  • Loading branch information
bryanatkinson committed Oct 20, 2023
1 parent 90c7211 commit cac4d36
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
33 changes: 33 additions & 0 deletions firebase-sessions/src/test/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2022 Google LLC -->
<!-- -->
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
<!-- you may not use this file except in compliance with the License. -->
<!-- You may obtain a copy of the License at -->
<!-- -->
<!-- http://www.apache.org/licenses/LICENSE-2.0 -->
<!-- -->
<!-- Unless required by applicable law or agreed to in writing, software -->
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
<!-- See the License for the specific language governing permissions and -->
<!-- limitations under the License. -->

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>
<service
android:name="com.google.firebase.components.ComponentDiscoveryService"
android:exported="false">
<meta-data
android:name="com.google.firebase.components:com.google.firebase.sessions.FirebaseSessionsRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar"
tools:node="remove"/>
<meta-data
android:name="com.google.firebase.components:com.google.firebase.sessions.testing.FirebaseSessionsFakeRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
</application>
</manifest>

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.sessions.testing

import com.google.firebase.sessions.SessionDetails
import com.google.firebase.sessions.SessionFirelogPublisher

/**
* Fake implementation of [SessionFirelogPublisher] that allows for inspecting the session details
* that were sent to it.
*/
internal class FakeFirelogPublisher : SessionFirelogPublisher {

/** All the sessions that were uploaded via this fake [SessionFirelogPublisher] */
val loggedSessions = ArrayList<SessionDetails>()

override fun logSession(sessionDetails: SessionDetails) {
loggedSessions.add(sessionDetails)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.sessions.testing

import com.google.firebase.sessions.SessionDatastore

/**
* Fake implementaiton of the [SessionDatastore] that allows for inspecting and modifying the
* currently stored values in unit tests.
*/
internal class FakeSessionDatastore : SessionDatastore {

/** The currently stored value */
private var currentSessionId: String? = null

override fun updateSessionId(sessionId: String) {
currentSessionId = sessionId
}

override fun getCurrentSessionId() = currentSessionId
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.sessions.testing

import androidx.annotation.Keep
import com.google.android.datatransport.TransportFactory
import com.google.firebase.FirebaseApp
import com.google.firebase.annotations.concurrent.Background
import com.google.firebase.annotations.concurrent.Blocking
import com.google.firebase.components.Component
import com.google.firebase.components.ComponentRegistrar
import com.google.firebase.components.Dependency
import com.google.firebase.components.Qualified.qualified
import com.google.firebase.components.Qualified.unqualified
import com.google.firebase.installations.FirebaseInstallationsApi
import com.google.firebase.platforminfo.LibraryVersionComponent
import com.google.firebase.sessions.BuildConfig
import com.google.firebase.sessions.Dispatchers
import com.google.firebase.sessions.FirebaseSessions
import com.google.firebase.sessions.SessionDatastore
import com.google.firebase.sessions.SessionFirelogPublisher
import com.google.firebase.sessions.SessionGenerator
import com.google.firebase.sessions.WallClock
import com.google.firebase.sessions.settings.SessionsSettings
import kotlinx.coroutines.CoroutineDispatcher

/**
* [ComponentRegistrar] for setting up Fake components for [FirebaseSessions] and its internal
* dependencies for unit tests.
*
* @hide
*/
@Keep
internal class FirebaseSessionsFakeRegistrar : ComponentRegistrar {
override fun getComponents() =
listOf(
Component.builder(SessionGenerator::class.java)
.name("session-generator")
.factory { SessionGenerator(timeProvider = WallClock) }
.build(),
Component.builder(FakeFirelogPublisher::class.java)
.name("fake-session-publisher")
.factory { FakeFirelogPublisher() }
.build(),
Component.builder(SessionFirelogPublisher::class.java)
.name("session-publisher")
.add(Dependency.required(fakeFirelogPublisher))
.factory { container -> container.get(fakeFirelogPublisher) }
.build(),
Component.builder(SessionsSettings::class.java)
.name("sessions-settings")
.add(Dependency.required(firebaseApp))
.add(Dependency.required(blockingDispatcher))
.add(Dependency.required(backgroundDispatcher))
.add(Dependency.required(firebaseInstallationsApi))
.factory { container ->
SessionsSettings(
container.get(firebaseApp),
container.get(blockingDispatcher),
container.get(backgroundDispatcher),
fakeFirebaseInstallations,
)
}
.build(),
Component.builder(Dispatchers::class.java)
.name("sessions-dispatchers")
.add(Dependency.required(blockingDispatcher))
.add(Dependency.required(backgroundDispatcher))
.factory { container ->
Dispatchers(container.get(blockingDispatcher), container.get(backgroundDispatcher))
}
.build(),
Component.builder(FakeSessionDatastore::class.java)
.name("fake-sessions-datastore")
.factory { FakeSessionDatastore() }
.build(),
Component.builder(SessionDatastore::class.java)
.name("sessions-datastore")
.add(Dependency.required(fakeDatastore))
.factory { container -> container.get(fakeDatastore) }
.build(),
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME),
)

private companion object {
private const val LIBRARY_NAME = "fire-sessions"

private val firebaseApp = unqualified(FirebaseApp::class.java)
private val firebaseInstallationsApi = unqualified(FirebaseInstallationsApi::class.java)
private val backgroundDispatcher =
qualified(Background::class.java, CoroutineDispatcher::class.java)
private val blockingDispatcher =
qualified(Blocking::class.java, CoroutineDispatcher::class.java)
private val transportFactory = unqualified(TransportFactory::class.java)
private val fakeFirelogPublisher = unqualified(FakeFirelogPublisher::class.java)
private val fakeDatastore = unqualified(FakeSessionDatastore::class.java)
private val sessionGenerator = unqualified(SessionGenerator::class.java)
private val sessionsSettings = unqualified(SessionsSettings::class.java)

private val fakeFirebaseInstallations = FakeFirebaseInstallations("FaKeFiD")
}
}

0 comments on commit cac4d36

Please sign in to comment.