2121using OptimizelySDK . Event ;
2222using OptimizelySDK . Logger ;
2323using OptimizelySDK . Notifications ;
24+ using NotificationType = OptimizelySDK . Notifications . NotificationCenter . NotificationType ;
2425
2526namespace OptimizelySDK . Tests . NotificationTests
2627{
@@ -29,9 +30,11 @@ public class NotificationCenterTests
2930 private Mock < ILogger > LoggerMock ;
3031 private NotificationCenter NotificationCenter ;
3132 private TestNotificationCallbacks TestNotificationCallbacks ;
32- private NotificationCenter . NotificationType NotificationTypeActivate = NotificationCenter . NotificationType . Activate ;
33- private NotificationCenter . NotificationType NotificationTypeTrack = NotificationCenter . NotificationType . Track ;
34- private NotificationCenter . NotificationType NotificationTypeFeatureExperiment = NotificationCenter . NotificationType . FeatureExperiment ;
33+
34+ private NotificationType NotificationTypeActivate = NotificationType . Activate ;
35+ private NotificationType NotificationTypeTrack = NotificationType . Track ;
36+ private NotificationType NotificationTypeFeatureExperiment = NotificationType . FeatureExperiment ;
37+ private NotificationType NotificationTypeFeatureRollout = NotificationType . FeatureRollout ;
3538
3639 [ SetUp ]
3740 public void Setup ( )
@@ -53,6 +56,13 @@ public void TestAddAndRemoveNotificationListener()
5356 // Verify that callback removed successfully.
5457 Assert . AreEqual ( true , NotificationCenter . RemoveNotification ( 1 ) ) ;
5558 Assert . AreEqual ( 0 , NotificationCenter . NotificationsCount ) ;
59+
60+ //Verify return false with invalid ID.
61+ Assert . AreEqual ( false , NotificationCenter . RemoveNotification ( 1 ) ) ;
62+
63+ // Verify that callback added successfully and return right notification ID.
64+ Assert . AreEqual ( NotificationCenter . NotificationId , NotificationCenter . AddNotification ( NotificationTypeActivate , TestNotificationCallbacks . TestActivateCallback ) ) ;
65+ Assert . AreEqual ( 1 , NotificationCenter . NotificationsCount ) ;
5666 }
5767
5868 [ Test ]
@@ -158,34 +168,69 @@ public void TestSendNotifications()
158168
159169 // Mocking notification callbacks.
160170 var notificationCallbackMock = new Mock < TestNotificationCallbacks > ( ) ;
171+
161172 notificationCallbackMock . Setup ( nc => nc . TestActivateCallback ( It . IsAny < Experiment > ( ) , It . IsAny < string > ( ) ,
162173 It . IsAny < UserAttributes > ( ) , It . IsAny < Variation > ( ) , It . IsAny < LogEvent > ( ) ) ) ;
174+
163175 notificationCallbackMock . Setup ( nc => nc . TestAnotherActivateCallback ( It . IsAny < Experiment > ( ) ,
164176 It . IsAny < string > ( ) , It . IsAny < UserAttributes > ( ) , It . IsAny < Variation > ( ) , It . IsAny < LogEvent > ( ) ) ) ;
165177
178+ notificationCallbackMock . Setup ( nc => nc . TestFeatureRolloutCallback ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ,
179+ It . IsAny < UserAttributes > ( ) , It . IsAny < Audience [ ] > ( ) ) ) ;
180+
166181 // Adding decision notifications.
167182 NotificationCenter . AddNotification ( NotificationTypeActivate , notificationCallbackMock . Object . TestActivateCallback ) ;
168183 NotificationCenter . AddNotification ( NotificationTypeActivate , notificationCallbackMock . Object . TestAnotherActivateCallback ) ;
184+ NotificationCenter . AddNotification ( NotificationTypeFeatureRollout , notificationCallbackMock . Object . TestFeatureRolloutCallback ) ;
185+
169186
170187 // Adding track notifications.
171188 NotificationCenter . AddNotification ( NotificationTypeTrack , notificationCallbackMock . Object . TestTrackCallback ) ;
172189
173- // Firing decision type notifications.
190+ // Fire decision type notifications.
174191 NotificationCenter . SendNotifications ( NotificationTypeActivate , config . GetExperimentFromKey ( "test_experiment" ) ,
175192 "testUser" , new UserAttributes ( ) , config . GetVariationFromId ( "test_experiment" , "7722370027" ) , null ) ;
176193
177194 // Verify that only the registered notifications of decision type are called.
178195 notificationCallbackMock . Verify ( nc => nc . TestActivateCallback ( It . IsAny < Experiment > ( ) , It . IsAny < string > ( ) ,
179- It . IsAny < UserAttributes > ( ) , It . IsAny < Variation > ( ) , It . IsAny < LogEvent > ( ) ) , Times . Exactly ( 1 ) ) ;
196+ It . IsAny < UserAttributes > ( ) , It . IsAny < Variation > ( ) , It . IsAny < LogEvent > ( ) ) , Times . Once ) ;
197+
180198 notificationCallbackMock . Verify ( nc => nc . TestAnotherActivateCallback ( It . IsAny < Experiment > ( ) , It . IsAny < string > ( ) ,
181- It . IsAny < UserAttributes > ( ) , It . IsAny < Variation > ( ) , It . IsAny < LogEvent > ( ) ) , Times . Exactly ( 1 ) ) ;
199+ It . IsAny < UserAttributes > ( ) , It . IsAny < Variation > ( ) , It . IsAny < LogEvent > ( ) ) , Times . Once ) ;
200+
182201 notificationCallbackMock . Verify ( nc => nc . TestTrackCallback ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ,
183202 It . IsAny < UserAttributes > ( ) , It . IsAny < EventTags > ( ) , It . IsAny < LogEvent > ( ) ) , Times . Never ) ;
184203
185- // Verify that SendNotifications does not break when no notification exists.
204+
205+ // Fire feature rollout notification
206+ NotificationCenter . SendNotifications ( NotificationTypeFeatureRollout , "featureKey" , "testUser" , new UserAttributes ( ) , null ) ;
207+
208+ notificationCallbackMock . Verify ( nc => nc . TestFeatureRolloutCallback ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ,
209+ It . IsAny < UserAttributes > ( ) , It . IsAny < Audience [ ] > ( ) ) , Times . Once ) ;
210+
211+
212+ // Verify that after clearing notifications, SendNotification should not call any notification
213+ // which were previously registered.
186214 NotificationCenter . ClearAllNotifications ( ) ;
215+ notificationCallbackMock . ResetCalls ( ) ;
216+
187217 NotificationCenter . SendNotifications ( NotificationTypeActivate , config . GetExperimentFromKey ( "test_experiment" ) ,
188218 "testUser" , new UserAttributes ( ) , config . GetVariationFromId ( "test_experiment" , "7722370027" ) , null ) ;
219+
220+
221+ // Again verify notifications which were registered are not called.
222+ notificationCallbackMock . Verify ( nc => nc . TestActivateCallback ( It . IsAny < Experiment > ( ) , It . IsAny < string > ( ) ,
223+ It . IsAny < UserAttributes > ( ) , It . IsAny < Variation > ( ) , It . IsAny < LogEvent > ( ) ) , Times . Never ) ;
224+
225+ notificationCallbackMock . Verify ( nc => nc . TestAnotherActivateCallback ( It . IsAny < Experiment > ( ) , It . IsAny < string > ( ) ,
226+ It . IsAny < UserAttributes > ( ) , It . IsAny < Variation > ( ) , It . IsAny < LogEvent > ( ) ) , Times . Never ) ;
227+
228+ notificationCallbackMock . Verify ( nc => nc . TestTrackCallback ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ,
229+ It . IsAny < UserAttributes > ( ) , It . IsAny < EventTags > ( ) , It . IsAny < LogEvent > ( ) ) , Times . Never ) ;
230+
231+ notificationCallbackMock . Verify ( nc => nc . TestFeatureRolloutCallback ( It . IsAny < string > ( ) , It . IsAny < string > ( ) ,
232+ It . IsAny < UserAttributes > ( ) , It . IsAny < Audience [ ] > ( ) ) , Times . Never ) ;
233+
189234 }
190235 }
191236
0 commit comments