Skip to content

Commit d77b846

Browse files
authored
Merge 095f048 into ed10eb5
2 parents ed10eb5 + 095f048 commit d77b846

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

build.gradle

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

1818
buildscript {
1919
ext.kotlinVersion = '1.7.10'
20+
ext.coroutinesVersion = '1.6.4'
21+
2022
repositories {
2123
google()
2224
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: 36 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.tasks.await
24+
import kotlinx.coroutines.test.runTest
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,33 @@ class KtxTests {
101107
}
102108
}
103109
}
110+
111+
class CoroutinesPlayServicesTests {
112+
// We are only interested in the await() function offered by kotlinx-coroutines-play-services
113+
// So we're not testing the other functions provided by that library.
114+
115+
@Test
116+
fun `Task#await() resolves to the same result as Task#getResult()`() = runTest {
117+
val task = Tasks.forResult(21)
118+
119+
val expected = task.result
120+
val actual = task.await()
121+
122+
assertThat(actual).isEqualTo(expected)
123+
assertThat(task.isSuccessful).isTrue()
124+
assertThat(task.exception).isNull()
125+
}
126+
127+
@Test
128+
fun `Task#await() throws an Exception for failing Tasks`() = runTest {
129+
val task = Tasks.forException<TestException>(TestException("some error happened"))
130+
131+
try {
132+
task.await()
133+
fail("Task#await should throw an Exception")
134+
} catch (e: Exception) {
135+
assertThat(e).isInstanceOf(TestException::class.java)
136+
assertThat(task.isSuccessful).isFalse()
137+
}
138+
}
139+
}

firebase-firestore/ktx/ktx.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ dependencies {
5757
implementation project(':firebase-common:ktx')
5858
implementation project(':firebase-firestore')
5959
implementation 'androidx.annotation:annotation:1.1.0'
60-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
60+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
6161
implementation 'com.google.android.gms:play-services-basement:18.1.0'
6262
testImplementation project(':firebase-database-collection')
6363
testImplementation 'org.mockito:mockito-core:2.25.0'

0 commit comments

Comments
 (0)