AudienceChecks { public static boolean checkAudience(@NonNull Context context, @Nullable Audience audience) { return checkAudience(context, audience, null); }  static boolean checkAudience(@NonNull Context context, @Nullable Audience audience); static boolean checkAudience(@NonNull Context context, @Nullable Audience audience, @Nullable Map<String, Set<String>> tagGroups);  }
@Test public void testEmptyAudience() { Audience audience = Audience.newBuilder().build(); assertTrue(AudienceChecks.checkAudience(context, audience)); }
@Test public void testNotificationOptIn() { Audience requiresOptedIn = Audience.newBuilder() .setNotificationsOptIn(true) .build(); Audience requiresOptedOut = Audience.newBuilder() .setNotificationsOptIn(false) .build(); assertFalse(AudienceChecks.checkAudience(context, requiresOptedIn)); assertTrue(AudienceChecks.checkAudience(context, requiresOptedOut)); when(pushManager.areNotificationsOptedIn()).thenReturn(true); assertTrue(AudienceChecks.checkAudience(context, requiresOptedIn)); assertFalse(AudienceChecks.checkAudience(context, requiresOptedOut)); }
@Test public void testLocationOptIn() { Audience requiresOptedIn = Audience.newBuilder() .setLocationOptIn(true) .build(); Audience requiresOptedOut = Audience.newBuilder() .setLocationOptIn(false) .build(); assertFalse(AudienceChecks.checkAudience(context, requiresOptedIn)); assertTrue(AudienceChecks.checkAudience(context, requiresOptedOut)); when(locationClient.isOptIn()).thenReturn(true); assertTrue(AudienceChecks.checkAudience(context, requiresOptedIn)); assertFalse(AudienceChecks.checkAudience(context, requiresOptedOut)); }
@Test public void testTagSelector() { final Set<String> tags = new HashSet<>(); when(airshipChannel.getTags()).then(new Answer<Set<String>>() { @Override public Set<String> answer(InvocationOnMock invocation) throws Throwable { return tags; } }); Audience audience = Audience.newBuilder() .setTagSelector(TagSelector.tag("expected tag")) .build(); assertFalse(AudienceChecks.checkAudience(context, audience)); tags.add("expected tag"); assertTrue(AudienceChecks.checkAudience(context, audience)); }
@Test public void testTagSelectorWithGroups() { final Set<String> tags = new HashSet<>(); when(airshipChannel.getTags()).then(new Answer<Set<String>>() { @Override public Set<String> answer(InvocationOnMock invocation) throws Throwable { return tags; } }); Audience audience = Audience.newBuilder() .setTagSelector(TagSelector.tag("expected tag", "expected group")) .build(); Map<String, Set<String>> tagGroups = new HashMap<>(); assertFalse(AudienceChecks.checkAudience(context, audience, tagGroups)); tagGroups.put("expected group", tagSet("expected tag")); assertTrue(AudienceChecks.checkAudience(context, audience, tagGroups)); }
@Test @Config public void testLocales() { Audience audience = Audience.newBuilder() .addLanguageTag("en-US") .build(); assertTrue(AudienceChecks.checkAudience(context, audience)); audience = Audience.newBuilder() .addLanguageTag("en") .build(); assertTrue(AudienceChecks.checkAudience(context, audience)); audience = Audience.newBuilder() .addLanguageTag("fr") .build(); assertFalse(AudienceChecks.checkAudience(context, audience)); }
@Test public void testAppVersion() { Audience audience = Audience.newBuilder() .setVersionMatcher(ValueMatcher.newNumberRangeMatcher(1.0, 2.0)) .build(); when(applicationMetrics.getCurrentAppVersion()).thenReturn(1l); assertTrue(AudienceChecks.checkAudience(context, audience)); when(applicationMetrics.getCurrentAppVersion()).thenReturn(2l); assertTrue(AudienceChecks.checkAudience(context, audience)); when(applicationMetrics.getCurrentAppVersion()).thenReturn(3l); assertFalse(AudienceChecks.checkAudience(context, audience)); }
@Test public void testSanitizeLocalesInChecks() { Audience audience = Audience.newBuilder() .addLanguageTag("en-") .addLanguageTag("en_") .addLanguageTag("en") .addLanguageTag("-") .addLanguageTag("_") .addLanguageTag("") .build(); assertTrue(AudienceChecks.checkAudience(context, audience)); }