public void test_disconnect_delegates_to_stub() throws RemoteException {
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    wrapper.disconnect();

    Mockito.verify(openVpnServiceStub).disconnect();
  }
  public void test_disconnect_RemoteException_disables_binding() throws RemoteException {
    Mockito.doThrow(new RemoteException()).when(openVpnServiceStub).disconnect();
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    wrapper.disconnect();

    assertFalse(wrapper.isBound());
  }
  public void test_getStatus_RemoteException_returns_stopped() throws RemoteException {
    Mockito.when(openVpnServiceStub.getStatus()).thenThrow(new RemoteException());
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    OpenVpnState status = wrapper.getStatus();

    assertFalse(status.isStarted());
  }
  public void test_supplyCredentials_delegates_to_stub() throws RemoteException {
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);
    OpenVpnCredentials param = new OpenVpnCredentials("u", "p");

    wrapper.supplyCredentials(param);

    Mockito.verify(openVpnServiceStub).supplyCredentials(param);
  }
  public void test_getStatusFor_delegates_to_stub() throws RemoteException {
    OpenVpnConfig config = new OpenVpnConfig(new File("/dev/null"));
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    wrapper.getStatusFor(config);

    Mockito.verify(openVpnServiceStub).getStatusFor(Mockito.same(config));
  }
  public void test_supplyPassphrase_delegates_to_stub() throws RemoteException {
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);
    OpenVpnPassphrase param = new OpenVpnPassphrase("p");

    wrapper.supplyPassphrase(param);

    Mockito.verify(openVpnServiceStub).supplyPassphrase(param);
  }
  public void test_connect_delegates_to_stub() throws RemoteException {
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);
    OpenVpnConfig param = new OpenVpnConfig(new File("test"));

    wrapper.connect(param);

    Mockito.verify(openVpnServiceStub).connect(param);
  }
  public void test_removeOpenVpnStateListener_delegates_to_stub() throws RemoteException {
    IOpenVpnStateListener param = mock(IOpenVpnStateListener.class);
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    wrapper.removeOpenVpnStateListener(param);

    Mockito.verify(openVpnServiceStub).removeOpenVpnStateListener(param);
  }
  public void test_onServiceConnected_registers_listeners() throws RemoteException {
    IOpenVpnStateListener param = mock(IOpenVpnStateListener.class);
    wrapper.addOpenVpnStateListener(param);

    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    Mockito.verify(openVpnServiceStub).addOpenVpnStateListener(param);
  }
  public void test_getStatusFor_RemoteException_returns_stopped() throws RemoteException {
    OpenVpnConfig config = new OpenVpnConfig(new File("/dev/null"));
    Mockito.when(openVpnServiceStub.getStatusFor(config)).thenThrow(new RemoteException());
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    OpenVpnState status = wrapper.getStatusFor(config);

    assertFalse(status.isStarted());
  }
  public void test_getStatusFor_RemoteException_disables_binding() throws RemoteException {
    OpenVpnConfig config = new OpenVpnConfig(new File("/dev/null"));
    Mockito.when(openVpnServiceStub.getStatusFor(config)).thenThrow(new RemoteException());
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    wrapper.getStatusFor(config);

    assertFalse(wrapper.isBound());
  }
  public void test_supplyPassphrase_RemoteException_disables_binding() throws RemoteException {
    Mockito.doThrow(new RemoteException())
        .when(openVpnServiceStub)
        .supplyPassphrase(Mockito.any(OpenVpnPassphrase.class));
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    wrapper.supplyPassphrase(new OpenVpnPassphrase("p"));

    assertFalse(wrapper.isBound());
  }
  public void test_addOpenVpnStateListener_RemoteException_disables_binding()
      throws RemoteException {
    IOpenVpnStateListener param = mock(IOpenVpnStateListener.class);
    Mockito.doThrow(new RemoteException()).when(openVpnServiceStub).addOpenVpnStateListener(param);
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    wrapper.addOpenVpnStateListener(param);

    assertFalse(wrapper.isBound());
  }
  public void test_connect_RemoteException_disables_binding() throws RemoteException {
    Mockito.doThrow(new RemoteException())
        .when(openVpnServiceStub)
        .connect(Mockito.any(OpenVpnConfig.class));
    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    wrapper.connect(new OpenVpnConfig(new File("test")));

    assertFalse(wrapper.isBound());
  }
  public void test_unbindService_removes_BroadcastReceiver() {
    wrapper.bindService();
    ArgumentCaptor<BroadcastReceiver> broadcastReceiverArgumentCaptor =
        ArgumentCaptor.forClass(BroadcastReceiver.class);
    verify(context)
        .registerReceiver(broadcastReceiverArgumentCaptor.capture(), any(IntentFilter.class));
    BroadcastReceiver broadcastReceiver = broadcastReceiverArgumentCaptor.getValue();

    wrapper.unbindService();

    verify(context).unregisterReceiver(broadcastReceiver);
  }
  public void test_resumeListeners_after_onServiceConnected() throws RemoteException {
    IOpenVpnStateListener param1 = mock(IOpenVpnStateListener.class);
    IOpenVpnStateListener param2 = mock(IOpenVpnStateListener.class);
    IOpenVpnStateListener param3 = mock(IOpenVpnStateListener.class);
    wrapper.addOpenVpnStateListener(param1);
    wrapper.addOpenVpnStateListener(param2);
    wrapper.addOpenVpnStateListener(param3);

    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);
    wrapper.resumeListeners();

    Mockito.verify(openVpnServiceStub).addOpenVpnStateListener(param1);
    Mockito.verify(openVpnServiceStub).addOpenVpnStateListener(param2);
    Mockito.verify(openVpnServiceStub).addOpenVpnStateListener(param3);
  }
  public void test_startService_succeeds() throws RemoteException {
    Mockito.when(context.startService(Mockito.any(Intent.class))).thenReturn(COMPONENT_NAME);

    boolean success = wrapper.startService();

    assertTrue(success);
  }
  public void test_startService_fails() throws RemoteException {
    Mockito.when(context.startService(Mockito.any(Intent.class))).thenReturn(null);

    boolean success = wrapper.startService();

    assertFalse(success);
  }
  public void test_onServiceConnected_registers_only_listeners_which_where_not_removed()
      throws RemoteException {
    IOpenVpnStateListener param1 = mock(IOpenVpnStateListener.class);
    IOpenVpnStateListener param2 = mock(IOpenVpnStateListener.class);
    IOpenVpnStateListener param3 = mock(IOpenVpnStateListener.class);
    wrapper.addOpenVpnStateListener(param1);
    wrapper.addOpenVpnStateListener(param2);
    wrapper.addOpenVpnStateListener(param3);
    wrapper.removeOpenVpnStateListener(param2);

    wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);

    Mockito.verify(openVpnServiceStub).addOpenVpnStateListener(param1);
    Mockito.verify(openVpnServiceStub, never()).addOpenVpnStateListener(param2);
    Mockito.verify(openVpnServiceStub).addOpenVpnStateListener(param3);
  }
  public void test_stopService() throws RemoteException {
    wrapper.stopService();

    ArgumentCaptor<Intent> intentCaptor = new ArgumentCaptor<Intent>();
    Mockito.verify(context).stopService(intentCaptor.capture());

    assert_intentAddressesOpenVpnService_with_no_action(intentCaptor.getValue());
  }
  public void test_bindService() throws RemoteException {
    wrapper.bindService();

    ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
    Mockito.verify(context)
        .bindService(intentCaptor.capture(), Mockito.same(wrapper), Mockito.eq(0));
    assert_intentAddressesOpenVpnService_with_no_action(intentCaptor.getValue());
  }
 public void test_removeOpenVpnStateListener_does_not_accept_null() throws RemoteException {
   try {
     wrapper.removeOpenVpnStateListener(null);
     fail("NullPointerException expected");
   } catch (NullPointerException e) {
     assertEquals("listener is null in removeOpenVpnStateListener", e.getMessage());
   }
 }
  public void test_bindService_succeeds() throws RemoteException {
    Mockito.when(
            context.bindService(Mockito.any(Intent.class), Mockito.same(wrapper), Mockito.eq(0)))
        .thenReturn(true);

    boolean success = wrapper.bindService();

    assertTrue(success);
  }
  public void test_binds_when_OPENVPN_STATE_CHANGED_broadcast_is_received() {
    wrapper.bindService();
    ArgumentCaptor<BroadcastReceiver> broadcastReceiverArgumentCaptor =
        ArgumentCaptor.forClass(BroadcastReceiver.class);
    verify(context)
        .registerReceiver(broadcastReceiverArgumentCaptor.capture(), any(IntentFilter.class));
    BroadcastReceiver broadcastReceiver = broadcastReceiverArgumentCaptor.getValue();
    reset(context);

    broadcastReceiver.onReceive(context, new Intent(Intents.OPENVPN_STATE_CHANGED.getAction()));

    ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
    Mockito.verify(context)
        .bindService(intentCaptor.capture(), Mockito.same(wrapper), Mockito.eq(0));
    assert_intentAddressesOpenVpnService_with_no_action(intentCaptor.getValue());
  }
  /** This tests also covers installment of the {@code BroadcastReceiver}. */
  public void test_bindService_installs_BroadcastReceiver() {
    wrapper.bindService();
    ArgumentCaptor<BroadcastReceiver> broadcastReceiverArgumentCaptor =
        ArgumentCaptor.forClass(BroadcastReceiver.class);
    ArgumentCaptor<IntentFilter> intentFilterArgumentCaptor =
        ArgumentCaptor.forClass(IntentFilter.class);

    verify(context)
        .registerReceiver(
            broadcastReceiverArgumentCaptor.capture(), intentFilterArgumentCaptor.capture());

    BroadcastReceiver broadcastReceiver = broadcastReceiverArgumentCaptor.getValue();
    assertNotNull(broadcastReceiver);

    IntentFilter intentFilter = intentFilterArgumentCaptor.getValue();
    assertTrue(intentFilter.hasAction(Intents.OPENVPN_STATE_CHANGED.getAction()));
  }
 public void test_createIntentAddressingOpenVpnService_verify_componentName() {
   assertEquals(
       COMPONENT_NAME,
       OpenVpnServiceWrapper.createDefaultIntentAddressingOpenVpnService().getComponent());
 }
 public void test_createIntentAddressingOpenVpnService_has_no_action() {
   assertEquals(
       null, OpenVpnServiceWrapper.createDefaultIntentAddressingOpenVpnService().getAction());
 }
 public void test_onServiceDisconnectedHook_is_not_called_when_listeners_are_paused() {
   assertFalse(onServiceDisconnectedCalled.get());
   wrapper.pauseListeners();
   wrapper.onServiceDisconnected(COMPONENT_NAME);
   assertFalse(onServiceDisconnectedCalled.get());
 }
 public void test_onServiceConnectedHook_is_called_when_listeners_are_not_paused() {
   assertFalse(onServiceConnectedCalled.get());
   wrapper.onServiceConnected(COMPONENT_NAME, openVpnServiceStub);
   assertTrue(onServiceConnectedCalled.get());
 }
  public void test_unbindService() throws RemoteException {
    wrapper.unbindService();

    Mockito.verify(context).unbindService(Mockito.same(wrapper));
  }