@@ -63,10 +63,9 @@ protected override void Start() {
6363#else // FIREBASE_RUNNING_FROM_CI && (UNITY_IOS || UNITY_TVOS)
6464
6565 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.
6867#if ( UNITY_IOS || UNITY_TVOS || UNITY_ANDROID )
69- MakeTest ( TestWaitForToken ) ,
68+ TestGetRegistrationToken ,
7069#if ! ( UNITY_IOS || UNITY_TVOS )
7170 // TODO(b/130674454) This test times out on iOS, disabling until fixed.
7271 MakeTest ( TestSendPlaintextMessageToDevice ) ,
@@ -83,10 +82,9 @@ protected override void Start() {
8382 } ;
8483
8584 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.
8886#if ( UNITY_IOS || UNITY_TVOS || UNITY_ANDROID )
89- "TestWaitForToken " ,
87+ "TestGetRegistrationToken " ,
9088#if ! ( UNITY_IOS || UNITY_TVOS )
9189 // TODO(b/130674454) This test times out on iOS, disabling until fixed.
9290 "TestSendPlaintextMessageToDevice" ,
@@ -140,24 +138,37 @@ Func<Task> MakeTest(Func<TaskCompletionSource<string>, IEnumerator> coroutine) {
140138 } ;
141139 }
142140
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+ } ) ;
147158 }
148159
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." ) ;
154165 }
155166 }
156167
157168 // Sends a plaintext message to the server, setting this device as the addressee, waits until the
158169 // app receives the message and verifies the contents are the same as were sent.
159170 IEnumerator TestSendPlaintextMessageToDevice ( TaskCompletionSource < string > tcs ) {
160- yield return StartCoroutine ( WaitForToken ( ) ) ;
171+ ThrowIfMissingRegistrationToken ( ) ;
161172 SendPlaintextMessageToDeviceAsync ( PlaintextMessage , registrationToken ) ;
162173 // TODO(b/65218400): check message id.
163174 while ( lastReceivedMessage == null ) {
@@ -170,7 +181,7 @@ IEnumerator TestSendPlaintextMessageToDevice(TaskCompletionSource<string> tcs) {
170181 // Sends a JSON message to the server, setting this device as the addressee, waits until the app
171182 // receives the message and verifies the contents are the same as were sent.
172183 IEnumerator TestSendJsonMessageToDevice ( TaskCompletionSource < string > tcs ) {
173- yield return StartCoroutine ( WaitForToken ( ) ) ;
184+ ThrowIfMissingRegistrationToken ( ) ;
174185 SendJsonMessageToDeviceAsync ( JsonMessageA , registrationToken ) ;
175186 // TODO(b/65218400): check message id.
176187 while ( lastReceivedMessage == null ) {
@@ -183,7 +194,7 @@ IEnumerator TestSendJsonMessageToDevice(TaskCompletionSource<string> tcs) {
183194 // Sends a JSON message to the server, specifying a topic to which this device is subscribed,
184195 // waits until the app receives the message and verifies the contents are the same as were sent.
185196 IEnumerator TestSendJsonMessageToSubscribedTopic ( TaskCompletionSource < string > tcs ) {
186- yield return StartCoroutine ( WaitForToken ( ) ) ;
197+ ThrowIfMissingRegistrationToken ( ) ;
187198 // Note: Ideally this would use a more unique topic, but topic creation and subscription
188199 // takes additional time, so instead this only subscribes during this one test, and doesn't
189200 // fully test unsubscribing.
0 commit comments