Skip to content

add coroutines-play-services as a transitive dep to firebase-common-ktx #4044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import com.google.firebase.gradle.MultiProjectReleasePlugin

buildscript {
ext.kotlinVersion = '1.7.10'
// Please update $coroutinesVersion alongside $kotlinVersion
// A map of versions can be found here: https://kotlinlang.org/docs/releases.html
ext.coroutinesVersion = '1.5.2'

repositories {
google()
mavenCentral()
Expand Down
5 changes: 5 additions & 0 deletions firebase-common/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ dependencies {
implementation project(':firebase-components')
implementation 'androidx.annotation:annotation:1.1.0'

// We're exposing this library as a transitive dependency so developers can
// get Kotlin Coroutines support out-of-the-box for methods that return a Task
api "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutinesVersion"

testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation 'junit:junit:4.12'
testImplementation "com.google.truth:truth:$googleTruthVersion"
testImplementation 'androidx.test:core:1.2.0'
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
package com.google.firebase.ktx

import androidx.test.core.app.ApplicationProvider
import com.google.android.gms.tasks.Tasks
import com.google.common.truth.Truth.assertThat
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.platforminfo.UserAgentPublisher
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.tasks.await
import org.junit.Assert.fail
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
Expand All @@ -36,6 +40,8 @@ fun withApp(name: String, block: FirebaseApp.() -> Unit) {
}
}

class TestException(message: String) : Exception(message)

@RunWith(RobolectricTestRunner::class)
class VersionTests {
@Test
Expand Down Expand Up @@ -101,3 +107,36 @@ class KtxTests {
}
}
}

// TODO(thatfiredev): replace runBlocking() with runTest() once we update kotlin to version >= 1.6
class CoroutinesPlayServicesTests {
// We are only interested in the await() function offered by kotlinx-coroutines-play-services
// So we're not testing the other functions provided by that library.

@Test
fun `Task#await() resolves to the same result as Task#getResult()`() = runBlocking {
val task = Tasks.forResult(21)

val expected = task.result
val actual = task.await()

assertThat(actual).isEqualTo(expected)
assertThat(task.isSuccessful).isTrue()
assertThat(task.exception).isNull()
}

@Test
fun `Task#await() throws an Exception for failing Tasks`() = runBlocking {
val task = Tasks.forException<TestException>(TestException("some error happened"))

try {
task.await()
fail("Task#await should throw an Exception")
} catch (e: Exception) {
assertThat(e).isInstanceOf(TestException::class.java)
assertThat(task.isSuccessful).isFalse()
}
}

// TODO(thatfiredev): add a test for CancellationToken once we support Coroutines >= 1.6
}