ConnectionManager extends AbstractBluetoothConnectivityAgent implements
        BluetoothConnector.BluetoothConnectorListener,
        ConnectionManagerSettings.Listener,
        DiscoveryManagerSettings.Listener { public ConnectionManagerState getState() { return mState; } ConnectionManager(
            Context context, ConnectionManagerListener listener,
            UUID myUuid, String myName);  ConnectionManager(
            Context context, ConnectionManagerListener listener,
            UUID myUuid, String myName, BluetoothManager bluetoothManager,
            SharedPreferences preferences); ConnectionManagerState getState(); synchronized boolean startListeningForIncomingConnections(); synchronized void stopListeningForIncomingConnections(); synchronized boolean connect(PeerProperties peerToConnectTo); synchronized boolean cancelConnectionAttempt(PeerProperties peerConnectingTo); synchronized void cancelAllConnectionAttempts(); @Override void dispose(); @Override void onConnectionManagerSettingsChanged(); @Override void onHandshakeRequiredSettingChanged(boolean handshakeRequired); @Override void onBluetoothAdapterScanModeChanged(int mode); @Override void onBluetoothAdapterStateChanged(int state); @Override void onIsServerStartedChanged(boolean isStarted); @Override void onConnecting(String bluetoothDeviceName, String bluetoothDeviceAddress); @Override void onConnected(
            final BluetoothSocket bluetoothSocket, final boolean isIncoming,
            final PeerProperties peerProperties); @Override void onConnectionTimeout(final PeerProperties peerProperties); @Override void onConnectionFailed(final PeerProperties peerProperties, final String errorMessage); @Override void onDiscoveryModeChanged(DiscoveryManager.DiscoveryMode discoveryMode, boolean startIfNotRunning); @Override void onPeerExpirationSettingChanged(long peerExpirationInMilliseconds); @Override void onAdvertiseScanSettingsChanged(); @Override void onPeerExtraInfoChanged(int newExtraInfo); @Override boolean setExtraInfo(int extraInfo);  }
@Test public void testConstructorThatTakesContextAndPrefs() throws Exception { ConnectionManager cm = new ConnectionManager( mMockContext, mMockManagerListener, new UUID(1, 1), "testConnection", mMockBluetoothManager, mMockSharedPreferences); assertThat(cm, is(notNullValue())); assertThat(cm.getState(), is(equalTo(ConnectionManager.ConnectionManagerState.NOT_STARTED))); }
@Test public void testUpdateState() throws Exception { class UpdateStateChecker { Field mShouldBeStartedField; Field mIsServerStartedField; Method updateStateMethod; UpdateStateChecker() throws Exception { mShouldBeStartedField = connectionManager.getClass().getDeclaredField("mShouldBeStarted"); mShouldBeStartedField.setAccessible(true); mIsServerStartedField = connectionManager.getClass().getDeclaredField("mIsServerStarted"); mIsServerStartedField.setAccessible(true); updateStateMethod = connectionManager.getClass().getDeclaredMethod("updateState"); updateStateMethod.setAccessible(true); } void prepareCheck(boolean bluetoothEnabled, boolean shouldBeStarted, boolean isStarted) throws Exception { when(mMockBluetoothManager.isBluetoothEnabled()).thenReturn(bluetoothEnabled); mShouldBeStartedField.setBoolean(connectionManager, shouldBeStarted); mIsServerStartedField.setBoolean(connectionManager, isStarted); } void check(ConnectionManager.ConnectionManagerState expectedState) throws Exception { updateStateMethod.invoke(connectionManager); assertThat(connectionManager.getState(), is(expectedState)); } } UpdateStateChecker checker = new UpdateStateChecker(); checker.prepareCheck(false, false, false); checker.check(ConnectionManager.ConnectionManagerState.NOT_STARTED); checker.prepareCheck(false, true, false); checker.check(ConnectionManager.ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED); checker.prepareCheck(true, false, false); checker.check(ConnectionManager.ConnectionManagerState.NOT_STARTED); checker.prepareCheck(true, true, false); checker.check(ConnectionManager.ConnectionManagerState.NOT_STARTED); checker.prepareCheck(true, true, true); checker.check(ConnectionManager.ConnectionManagerState.RUNNING); }
@Test public void testGetState() throws Exception { assertThat("Has a proper default state", connectionManager.getState(), is(equalTo(ConnectionManager.ConnectionManagerState.NOT_STARTED))); Field stateField = connectionManager.getClass().getDeclaredField("mState"); stateField.setAccessible(true); stateField.set(connectionManager, ConnectionManager.ConnectionManagerState.WAITING_FOR_SERVICES_TO_BE_ENABLED); assertThat("Has a proper state when waiting for bluetooth", connectionManager.getState(), is(equalTo(ConnectionManager.ConnectionManagerState .WAITING_FOR_SERVICES_TO_BE_ENABLED))); stateField.set(connectionManager, ConnectionManager.ConnectionManagerState.RUNNING); assertThat("Has a proper state when running", connectionManager.getState(), is(equalTo(ConnectionManager.ConnectionManagerState.RUNNING))); }