@Test
public void cancelConnectionTest() throws Exception {

    peripheral.connect();
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
    ArgumentCaptor<BluetoothGattCallback> captor = ArgumentCaptor.forClass(BluetoothGattCallback.class);
    verify(device).connectGatt(any(Context.class), anyBoolean(), captor.capture(), anyInt());
    BluetoothGattCallback callback = captor.getValue();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);
    peripheral.cancelConnection();
    verify(gatt).disconnect();
    assertEquals(STATE_DISCONNECTING,  peripheral.getState());
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_DISCONNECTED);
    verify(gatt).close();
    assertEquals(STATE_DISCONNECTED,  peripheral.getState());
}