|
30 | 30 | import com.optimizely.ab.event.LogEvent;
|
31 | 31 | import com.optimizely.ab.internal.ReservedEventKey;
|
32 | 32 | import com.optimizely.ab.notification.ActivateNotificationListener;
|
| 33 | +import com.optimizely.ab.notification.DecisionNotification; |
33 | 34 | import com.optimizely.ab.notification.NotificationCenter;
|
| 35 | +import com.optimizely.ab.notification.NotificationHandler; |
| 36 | +import com.optimizely.ab.notification.NotificationManager; |
| 37 | +import com.optimizely.ab.notification.TrackNotification; |
34 | 38 | import com.optimizely.ab.notification.TrackNotificationListener;
|
35 | 39 |
|
36 | 40 | import org.junit.Assert;
|
|
58 | 62 | import static junit.framework.Assert.assertNull;
|
59 | 63 | import static junit.framework.Assert.assertTrue;
|
60 | 64 | import static org.hamcrest.Matchers.hasEntry;
|
| 65 | +import static org.junit.Assert.assertNotEquals; |
61 | 66 | import static org.junit.Assert.assertThat;
|
62 | 67 | import static org.junit.Assume.assumeTrue;
|
63 | 68 | import static org.mockito.Mockito.mock;
|
@@ -1757,4 +1762,80 @@ public void testBadGetFeatureVariableString() {
|
1757 | 1762 | GENERIC_USER_ID
|
1758 | 1763 | );
|
1759 | 1764 | }
|
| 1765 | + |
| 1766 | + @Test |
| 1767 | + public void testAddDecisionNotificationHandler() { |
| 1768 | + assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString())); |
| 1769 | + |
| 1770 | + OptimizelyClient optimizelyClient = new OptimizelyClient( |
| 1771 | + optimizely, |
| 1772 | + logger |
| 1773 | + ); |
| 1774 | + |
| 1775 | + int notificationId = optimizelyClient.addDecisionNotificationHandler(decisionNotification -> {}); |
| 1776 | + assertTrue(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId)); |
| 1777 | + } |
| 1778 | + |
| 1779 | + @Test |
| 1780 | + public void testAddTrackNotificationHandler() { |
| 1781 | + OptimizelyClient optimizelyClient = new OptimizelyClient( |
| 1782 | + optimizely, |
| 1783 | + logger |
| 1784 | + ); |
| 1785 | + NotificationManager<TrackNotification> manager = optimizely.getNotificationCenter() |
| 1786 | + .getNotificationManager(TrackNotification.class); |
| 1787 | + |
| 1788 | + int notificationId = optimizelyClient.addTrackNotificationHandler(trackNotification -> {}); |
| 1789 | + assertTrue(manager.remove(notificationId)); |
| 1790 | + } |
| 1791 | + |
| 1792 | + @Test |
| 1793 | + public void testAddingTrackNotificationHandlerWithInvalidOptimizely() { |
| 1794 | + OptimizelyClient optimizelyClient = new OptimizelyClient( |
| 1795 | + null, |
| 1796 | + logger |
| 1797 | + ); |
| 1798 | + NotificationManager<TrackNotification> manager = optimizely.getNotificationCenter() |
| 1799 | + .getNotificationManager(TrackNotification.class); |
| 1800 | + |
| 1801 | + int notificationId = optimizelyClient.addTrackNotificationHandler(trackNotification -> {}); |
| 1802 | + assertEquals(-1, notificationId); |
| 1803 | + assertFalse(manager.remove(notificationId)); |
| 1804 | + } |
| 1805 | + |
| 1806 | + @Test |
| 1807 | + public void testAddingDecisionNotificationHandlerWithInvalidOptimizely() { |
| 1808 | + assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString())); |
| 1809 | + |
| 1810 | + OptimizelyClient optimizelyClient = new OptimizelyClient( |
| 1811 | + null, |
| 1812 | + logger |
| 1813 | + ); |
| 1814 | + NotificationManager<DecisionNotification> manager = optimizely.getNotificationCenter() |
| 1815 | + .getNotificationManager(DecisionNotification.class); |
| 1816 | + int notificationId = optimizelyClient.addDecisionNotificationHandler(decisionNotification -> {}); |
| 1817 | + assertEquals(-1, notificationId); |
| 1818 | + assertFalse(manager.remove(notificationId)); |
| 1819 | + } |
| 1820 | + |
| 1821 | + @Test |
| 1822 | + public void testAddingDecisionNotificationHandlerTwice() { |
| 1823 | + assumeTrue(datafileVersion == Integer.parseInt(ProjectConfig.Version.V4.toString())); |
| 1824 | + |
| 1825 | + OptimizelyClient optimizelyClient = new OptimizelyClient( |
| 1826 | + optimizely, |
| 1827 | + logger |
| 1828 | + ); |
| 1829 | + NotificationHandler<DecisionNotification> decisionNotificationHandler = new NotificationHandler<DecisionNotification>() { |
| 1830 | + @Override |
| 1831 | + public void handle(DecisionNotification decisionNotification) { |
| 1832 | + } |
| 1833 | + }; |
| 1834 | + int notificationId = optimizelyClient.addDecisionNotificationHandler(decisionNotificationHandler); |
| 1835 | + int notificationId2 = optimizelyClient.addDecisionNotificationHandler(decisionNotificationHandler); |
| 1836 | + assertNotEquals(-1, notificationId); |
| 1837 | + assertEquals(-1, notificationId2); |
| 1838 | + assertTrue(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId)); |
| 1839 | + assertFalse(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId2)); |
| 1840 | + } |
1760 | 1841 | }
|
0 commit comments