Skip to content

Commit 11c0fdd

Browse files
authored
Change the Messaging topic test to avoid accidental messages (#805)
* Use a unique topic in Messaging tests * Change Messaging Topic tests * Update UIHandlerAutomated.cs * Typo in comment
1 parent 0ba37b5 commit 11c0fdd

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

messaging/testapp/Assets/Firebase/Sample/Messaging/UIHandler.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ class UIHandler : MonoBehaviour {
3333
private string topic = "TestTopic";
3434
private bool UIEnabled = true;
3535

36+
// Should this Subscribe to the topic on startup.
37+
protected virtual bool SubscribeToTopicOnStart {
38+
get {
39+
return true;
40+
}
41+
}
42+
3643
// Log the result of the specified task, returning true if the task
3744
// completed successfully, false otherwise.
3845
protected bool LogTaskCompletion(Task task, string operation) {
@@ -87,9 +94,11 @@ void InitializeFirebase() {
8794
task => {
8895
LogTaskCompletion(task, "RequestPermissionAsync");
8996

90-
Firebase.Messaging.FirebaseMessaging.SubscribeAsync(topic).ContinueWithOnMainThread(task => {
91-
LogTaskCompletion(task, "SubscribeAsync");
92-
});
97+
if (SubscribeToTopicOnStart) {
98+
Firebase.Messaging.FirebaseMessaging.SubscribeAsync(topic).ContinueWithOnMainThread(task => {
99+
LogTaskCompletion(task, "SubscribeAsync");
100+
});
101+
}
93102
}
94103
);
95104
isFirebaseInitialized = true;

messaging/testapp/Assets/Firebase/Sample/Messaging/UIHandlerAutomated.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public class UIHandlerAutomated : UIHandler {
3434
private string registrationToken;
3535
private FirebaseMessage lastReceivedMessage;
3636

37+
// Don't subscribe to a topic, since it might confuse the tests.
38+
protected override bool SubscribeToTopicOnStart {
39+
get {
40+
return false;
41+
}
42+
}
43+
3744
protected override void Start() {
3845
#if FIREBASE_RUNNING_FROM_CI && (UNITY_IOS || UNITY_TVOS)
3946
// Messaging on iOS requires user interaction to give permissions
@@ -177,13 +184,21 @@ IEnumerator TestSendJsonMessageToDevice(TaskCompletionSource<string> tcs) {
177184
// waits until the app receives the message and verifies the contents are the same as were sent.
178185
IEnumerator TestSendJsonMessageToSubscribedTopic(TaskCompletionSource<string> tcs) {
179186
yield return StartCoroutine(WaitForToken());
180-
SendJsonMessageToTopicAsync(JsonMessageB, TestTopic);
187+
// Note: Ideally this would use a more unique topic, but topic creation and subscription
188+
// takes additional time, so instead this only subscribes during this one test, and doesn't
189+
// fully test unsubscribing.
190+
Firebase.Messaging.FirebaseMessaging.SubscribeAsync(TestTopic).ContinueWithOnMainThread(t => {
191+
SendJsonMessageToTopicAsync(JsonMessageB, TestTopic);
192+
});
181193
// TODO(b/65218400): check message id.
182194
while (lastReceivedMessage == null) {
183195
yield return new WaitForSeconds(0.5f);
184196
}
185-
ValidateJsonMessageB(tcs, lastReceivedMessage);
186-
lastReceivedMessage = null;
197+
// Unsubscribe from the test topic, to make sure that other messages aren't received.
198+
Firebase.Messaging.FirebaseMessaging.UnsubscribeAsync(TestTopic).ContinueWithOnMainThread(t => {
199+
ValidateJsonMessageB(tcs, lastReceivedMessage);
200+
lastReceivedMessage = null;
201+
});
187202
}
188203

189204
// Fake test (always passes immediately). Can be used on platforms with no other tests.

0 commit comments

Comments
 (0)