@@ -63,10 +63,9 @@ protected override void Start() {
63
63
#else // FIREBASE_RUNNING_FROM_CI && (UNITY_IOS || UNITY_TVOS)
64
64
65
65
Func < Task > [ ] tests = {
66
- // Disable these tests on desktop, as desktop never receives a token, and so WaitForToken
67
- // (called by all of these tests) stalls forever.
66
+ // Disable these tests on desktop, as desktop uses a stub implementation.
68
67
#if ( UNITY_IOS || UNITY_TVOS || UNITY_ANDROID )
69
- MakeTest ( TestWaitForToken ) ,
68
+ TestGetRegistrationToken ,
70
69
#if ! ( UNITY_IOS || UNITY_TVOS )
71
70
// TODO(b/130674454) This test times out on iOS, disabling until fixed.
72
71
MakeTest ( TestSendPlaintextMessageToDevice ) ,
@@ -83,10 +82,9 @@ protected override void Start() {
83
82
} ;
84
83
85
84
string [ ] customTests = {
86
- // Disable these tests on desktop, as desktop never receives a token, and so WaitForToken
87
- // (called by all of these tests) stalls forever.
85
+ // Disable these tests on desktop, as desktop uses a stub implementation.
88
86
#if ( UNITY_IOS || UNITY_TVOS || UNITY_ANDROID )
89
- "TestWaitForToken " ,
87
+ "TestGetRegistrationToken " ,
90
88
#if ! ( UNITY_IOS || UNITY_TVOS )
91
89
// TODO(b/130674454) This test times out on iOS, disabling until fixed.
92
90
"TestSendPlaintextMessageToDevice" ,
@@ -140,24 +138,37 @@ Func<Task> MakeTest(Func<TaskCompletionSource<string>, IEnumerator> coroutine) {
140
138
} ;
141
139
}
142
140
143
- // Waits until the app is given a registration token, expected shortly after startup.
144
- IEnumerator TestWaitForToken ( TaskCompletionSource < string > tcs ) {
145
- yield return StartCoroutine ( WaitForToken ( ) ) ;
146
- tcs . SetResult ( registrationToken ) ;
141
+ // Guarantee that the registration token is set, before running other tests.
142
+ Task TestGetRegistrationToken ( ) {
143
+ // The registration token might already be set, if gotten via OnTokenReceived
144
+ if ( ! string . IsNullOrEmpty ( registrationToken ) ) {
145
+ DebugLog ( "Already have a registration token, skipping GetTokenAsync call" ) ;
146
+ return Task . CompletedTask ;
147
+ }
148
+
149
+ // Otherwise, call GetTokenAsync, to fetch one. This can happen if the app
150
+ // already had a token from a previous run, and thus didn't need a new token.
151
+ return Firebase . Messaging . FirebaseMessaging . GetTokenAsync ( ) . ContinueWithOnMainThread ( t => {
152
+ if ( t . IsFaulted ) {
153
+ throw t . Exception ;
154
+ }
155
+
156
+ registrationToken = t . Result ;
157
+ } ) ;
147
158
}
148
159
149
- // Blocks until registrationToken is non-empty. Trying to send a message without a registration
150
- // token will fail .
151
- IEnumerator WaitForToken ( ) {
152
- while ( String . IsNullOrEmpty ( registrationToken ) ) {
153
- yield return new WaitForSeconds ( 0.5f ) ;
160
+ // If the registration token is missing, throw an exception.
161
+ // Use for tests that require a registration token to function properly .
162
+ void ThrowIfMissingRegistrationToken ( ) {
163
+ if ( string . IsNullOrEmpty ( registrationToken ) ) {
164
+ throw new InvalidOperationException ( "Registration Token is missing." ) ;
154
165
}
155
166
}
156
167
157
168
// Sends a plaintext message to the server, setting this device as the addressee, waits until the
158
169
// app receives the message and verifies the contents are the same as were sent.
159
170
IEnumerator TestSendPlaintextMessageToDevice ( TaskCompletionSource < string > tcs ) {
160
- yield return StartCoroutine ( WaitForToken ( ) ) ;
171
+ ThrowIfMissingRegistrationToken ( ) ;
161
172
SendPlaintextMessageToDeviceAsync ( PlaintextMessage , registrationToken ) ;
162
173
// TODO(b/65218400): check message id.
163
174
while ( lastReceivedMessage == null ) {
@@ -170,7 +181,7 @@ IEnumerator TestSendPlaintextMessageToDevice(TaskCompletionSource<string> tcs) {
170
181
// Sends a JSON message to the server, setting this device as the addressee, waits until the app
171
182
// receives the message and verifies the contents are the same as were sent.
172
183
IEnumerator TestSendJsonMessageToDevice ( TaskCompletionSource < string > tcs ) {
173
- yield return StartCoroutine ( WaitForToken ( ) ) ;
184
+ ThrowIfMissingRegistrationToken ( ) ;
174
185
SendJsonMessageToDeviceAsync ( JsonMessageA , registrationToken ) ;
175
186
// TODO(b/65218400): check message id.
176
187
while ( lastReceivedMessage == null ) {
@@ -183,7 +194,7 @@ IEnumerator TestSendJsonMessageToDevice(TaskCompletionSource<string> tcs) {
183
194
// Sends a JSON message to the server, specifying a topic to which this device is subscribed,
184
195
// waits until the app receives the message and verifies the contents are the same as were sent.
185
196
IEnumerator TestSendJsonMessageToSubscribedTopic ( TaskCompletionSource < string > tcs ) {
186
- yield return StartCoroutine ( WaitForToken ( ) ) ;
197
+ ThrowIfMissingRegistrationToken ( ) ;
187
198
// Note: Ideally this would use a more unique topic, but topic creation and subscription
188
199
// takes additional time, so instead this only subscribes during this one test, and doesn't
189
200
// fully test unsubscribing.
0 commit comments