BluetoothManager { public synchronized void release(BluetoothManagerListener listener) { if (!mListeners.remove(listener) && mListeners.size() > 0) { Log.w(TAG, "release: The given listener does not exist in the list - probably already removed"); } if (mListeners.size() == 0) { if (mInitialized) { Log.i(TAG, "release: No more listeners, de-initializing..."); } deinitialize(); } else { Log.d(TAG, "release: " + mListeners.size() + " listener(s) left"); } } private  BluetoothManager(Context context); private  BluetoothManager(Context context, BluetoothAdapter bluetoothAdapter,
                             SharedPreferences sharedPreferences); static BluetoothManager getInstance(Context context); static BluetoothManager getInstance(Context context, BluetoothAdapter bluetoothAdapter,
                                               SharedPreferences sharedPreferences); synchronized boolean bind(BluetoothManagerListener listener); synchronized void release(BluetoothManagerListener listener); boolean isBluetoothSupported(); boolean isBleSupported(); @TargetApi(21) FeatureSupportedStatus isBleMultipleAdvertisementSupported(); @TargetApi(21) FeatureSupportedStatus isBleOffloadedScanBatchingSupported(); @TargetApi(21) FeatureSupportedStatus isBleOffloadedFilteringSupported(); boolean isBluetoothEnabled(); boolean setBluetoothEnabled(boolean enable); BluetoothAdapter getBluetoothAdapter(); String getBluetoothMacAddress(); String getBluetoothName(); BluetoothDevice getRemoteDevice(String address);  }
@Test public void testRelease() throws Exception { Field mInitializedField = mBluetoothManager.getClass().getDeclaredField("mInitialized"); mInitializedField.setAccessible(true); Field mListenersField = mBluetoothManager.getClass().getDeclaredField("mListeners"); mListenersField.setAccessible(true); CopyOnWriteArrayList<BluetoothManager.BluetoothManagerListener> myListeners = new CopyOnWriteArrayList<>(); mListenersField.set(mBluetoothManager, myListeners); mBluetoothManager.bind(mMockBluetoothManagerListener); mBluetoothManager.release(mMockBluetoothManagerListener); verify(mMockContext, times(1)).unregisterReceiver( any(BroadcastReceiver.class)); assertThat("should be empty as the given listener is removed from the list of listeners", myListeners.isEmpty(), is(true)); mBluetoothManager.release(mMockBluetoothManagerListener); reset(mMockContext); verify(mMockContext, never()).unregisterReceiver( any(BroadcastReceiver.class)); assertThat("should be false", mInitializedField.getBoolean(mBluetoothManager), is(false)); }