AudienceChecks { @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) static boolean checkAudienceForScheduling(Context context, @Nullable Audience audience, boolean isNewUser) { if (audience == null) { return true; } if (audience.getNewUser() != null && audience.getNewUser() != isNewUser) { return false; } if (!audience.getTestDevices().isEmpty()) { byte[] digest = UAStringUtil.sha256Digest(UAirship.shared().getChannel().getId()); if (digest == null || digest.length < 16) { return false; } digest = Arrays.copyOf(digest, 16); for (String testDevice : audience.getTestDevices()) { byte[] decoded = UAStringUtil.base64Decode(testDevice); if (Arrays.equals(digest, decoded)) { return true; } } return false; } return true; }  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 testNewUser() { Audience requiresNewUser = Audience.newBuilder() .setNewUser(true) .build(); Audience requiresExistingUser = Audience.newBuilder() .setNewUser(false) .build(); assertFalse(AudienceChecks.checkAudienceForScheduling(context, requiresNewUser, false)); assertTrue(AudienceChecks.checkAudienceForScheduling(context, requiresExistingUser, false)); assertTrue(AudienceChecks.checkAudienceForScheduling(context, requiresNewUser, true)); assertFalse(AudienceChecks.checkAudienceForScheduling(context, requiresExistingUser, true)); }
@Test public void testTestDevices() { byte[] bytes = Arrays.copyOf(UAStringUtil.sha256Digest("test channel"), 16); String testDevice = Base64.encodeToString(bytes, Base64.DEFAULT); Audience testDeviceAudience = Audience.newBuilder() .addTestDevice(testDevice) .build(); Audience someOtherTestDeviceAudience = Audience.newBuilder() .addTestDevice(UAStringUtil.sha256("some other channel")) .build(); when(airshipChannel.getId()).thenReturn("test channel"); assertTrue(AudienceChecks.checkAudienceForScheduling(context, testDeviceAudience, false)); assertFalse(AudienceChecks.checkAudienceForScheduling(context, someOtherTestDeviceAudience, false)); }