15
15
package com.google.firebase.ktx
16
16
17
17
import androidx.test.core.app.ApplicationProvider
18
+ import com.google.android.gms.tasks.Tasks
18
19
import com.google.common.truth.Truth.assertThat
19
20
import com.google.firebase.FirebaseApp
20
21
import com.google.firebase.FirebaseOptions
21
22
import com.google.firebase.platforminfo.UserAgentPublisher
23
+ import kotlinx.coroutines.runBlocking
24
+ import kotlinx.coroutines.tasks.await
25
+ import org.junit.Assert.fail
22
26
import org.junit.Test
23
27
import org.junit.runner.RunWith
24
28
import org.robolectric.RobolectricTestRunner
@@ -36,6 +40,8 @@ fun withApp(name: String, block: FirebaseApp.() -> Unit) {
36
40
}
37
41
}
38
42
43
+ class TestException (message : String ) : Exception(message)
44
+
39
45
@RunWith(RobolectricTestRunner ::class )
40
46
class VersionTests {
41
47
@Test
@@ -101,3 +107,36 @@ class KtxTests {
101
107
}
102
108
}
103
109
}
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