Skip to content

Commit 52bee14

Browse files
committed
add kotlinx-coroutines-play-services as a transitive dep to firebase-common-ktx
1 parent 42c8d72 commit 52bee14

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import com.google.firebase.gradle.MultiProjectReleasePlugin
1717

1818
buildscript {
1919
ext.kotlinVersion = '1.4.32'
20+
// Please update $coroutinesVersion alongside $kotlinVersion
21+
// A map of versions can be found here: https://kotlinlang.org/docs/releases.html
22+
ext.coroutinesVersion = '1.4.3'
23+
2024
repositories {
2125
google()
2226
mavenCentral()

firebase-common/ktx/ktx.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ dependencies {
4242
implementation project(':firebase-components')
4343
implementation 'androidx.annotation:annotation:1.1.0'
4444

45+
// We're exposing this library as a transitive dependency so developers can
46+
// get Kotlin Coroutines support out-of-the-box for methods that return a Task
47+
api "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutinesVersion"
48+
4549
testImplementation "org.robolectric:robolectric:$robolectricVersion"
4650
testImplementation 'junit:junit:4.12'
4751
testImplementation "com.google.truth:truth:$googleTruthVersion"
4852
testImplementation 'androidx.test:core:1.2.0'
53+
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
4954
}

firebase-common/ktx/src/test/kotlin/com/google/firebase/ktx/Tests.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
package com.google.firebase.ktx
1616

1717
import androidx.test.core.app.ApplicationProvider
18+
import com.google.android.gms.tasks.Tasks
1819
import com.google.common.truth.Truth.assertThat
1920
import com.google.firebase.FirebaseApp
2021
import com.google.firebase.FirebaseOptions
2122
import com.google.firebase.platforminfo.UserAgentPublisher
23+
import kotlinx.coroutines.runBlocking
24+
import kotlinx.coroutines.tasks.await
25+
import org.junit.Assert.fail
2226
import org.junit.Test
2327
import org.junit.runner.RunWith
2428
import org.robolectric.RobolectricTestRunner
@@ -36,6 +40,8 @@ fun withApp(name: String, block: FirebaseApp.() -> Unit) {
3640
}
3741
}
3842

43+
class TestException(message: String) : Exception(message)
44+
3945
@RunWith(RobolectricTestRunner::class)
4046
class VersionTests {
4147
@Test
@@ -101,3 +107,36 @@ class KtxTests {
101107
}
102108
}
103109
}
110+
111+
// TODO(thatfiredev): replace runBlocking() with runTest() once we update kotlin to version >= 1.6
112+
class CoroutinesPlayServicesTests {
113+
// We are only interested in the await() function offered by kotlinx-coroutines-play-services
114+
// So we're not testing the other functions provided by that library.
115+
116+
@Test
117+
fun `Task#await() resolves to the same result as Task#getResult()`() = runBlocking {
118+
val task = Tasks.forResult(21)
119+
120+
val expected = task.result
121+
val actual = task.await()
122+
123+
assertThat(actual).isEqualTo(expected)
124+
assertThat(task.isSuccessful).isTrue()
125+
assertThat(task.exception).isNull()
126+
}
127+
128+
@Test
129+
fun `Task#await() throws an Exception for failing Tasks`() = runBlocking {
130+
val task = Tasks.forException<TestException>(TestException("some error happened"))
131+
132+
try {
133+
task.await()
134+
fail("Task#await should throw an Exception")
135+
} catch (e: Exception) {
136+
assertThat(e).isInstanceOf(TestException::class.java)
137+
assertThat(task.isSuccessful).isFalse()
138+
}
139+
}
140+
141+
// TODO(thatfiredev): add a test for CancellationToken once we support Coroutines >= 1.6
142+
}

0 commit comments

Comments
 (0)