@Test
public void testConfigurePackages() throws Exception {

    final List<String> disallowed = new ArrayList<>();
    final List<String> allowed = new ArrayList<>();
    when(builder.addDisallowedApplication(anyString())).thenAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            disallowed.add(invocation.getArgumentAt(0, String.class));
            return null;
        }
    });
    when(builder.addAllowedApplication(anyString())).thenAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            allowed.add(invocation.getArgumentAt(0, String.class));
            return null;
        }
    });
    // We are defaulting to disallow: allow all allowed packages.
    allowed.clear();
    disallowed.clear();
    config.whitelist.defaultMode = Configuration.Whitelist.DEFAULT_MODE_NOT_ON_VPN;
    thread.configurePackages(builder, config);
    assertTrue(allowed.contains("onVpn"));
    assertEquals(new ArrayList<String>(), disallowed);
    // We are defaulting to allow: deny all non-allowed packages.
    allowed.clear();
    disallowed.clear();
    config.whitelist.defaultMode = Configuration.Whitelist.DEFAULT_MODE_ON_VPN;
    thread.configurePackages(builder, config);
    assertTrue(disallowed.contains("notOnVpn"));
    assertEquals(new ArrayList<String>(), allowed);
    // Intelligent is like allow, it only disallows system apps
    allowed.clear();
    disallowed.clear();
    config.whitelist.defaultMode = Configuration.Whitelist.DEFAULT_MODE_INTELLIGENT;
    thread.configurePackages(builder, config);
    assertTrue(disallowed.contains("notOnVpn"));
    assertEquals(new ArrayList<String>(), allowed);
}