Skip to content

Commit

Permalink
Fixed test and behaviour of RxBleRadioOperationDisconnect when no `…
Browse files Browse the repository at this point in the history
…BluetoothGatt` is available. #178

Reviewers: pawel.urban

Reviewed By: pawel.urban

Differential Revision: https://phabricator.polidea.com/D2325
  • Loading branch information
dariuszseweryn committed Apr 26, 2017
1 parent a5bf7c4 commit ea243e6
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ protected final void onNext(T next) {

/**
* A convenience method for calling the Subscriber's onCompleted()
* Calling this method automatically releases the radio -> calls releaseRadio().
*/
protected final void onCompleted() {
releaseRadio();
replaySubject.onCompleted();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import rx.Observable;
import rx.Scheduler;
import rx.Subscription;
import rx.functions.Action0;

public abstract class RxBleSingleGattRadioOperation<T> extends RxBleRadioOperation<T> {

Expand Down Expand Up @@ -44,12 +43,6 @@ final protected void protectedRun() throws Throwable {
timeoutFallbackProcedure(bluetoothGatt, rxBleGattCallback, timeoutConfiguration.timeoutScheduler),
timeoutConfiguration.timeoutScheduler
)
.doOnCompleted(new Action0() {
@Override
public void call() {
RxBleSingleGattRadioOperation.this.releaseRadio();
}
})
.subscribe(getSubscriber());

boolean success = startOperation(bluetoothGatt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,7 @@ protected void protectedRun() throws Throwable {
throw new IllegalArgumentException("The custom operation asObservable method must return a non-null observable");
}

operationObservable.doOnCompleted(new Action0() {
@Override
public void call() {
releaseRadio();
}
}).subscribe(getSubscriber());
operationObservable.subscribe(getSubscriber());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ protected void protectedRun() throws Throwable {
public void call() {
onNext(bytesToWrite);
onCompleted();
releaseRadio();
}
},
new Action1<Throwable>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,6 @@ public RxBleRadioOperationConnect build() {
private final BluetoothGattProvider bluetoothGattProvider;
private final TimeoutConfiguration connectTimeout;
private final boolean autoConnect;
private final Runnable releaseRadioRunnable = new Runnable() {
@Override
public void run() {
releaseRadio();
}
};
private final Runnable emptyRunnable = new Runnable() {
@Override
public void run() {
}
};

private final BehaviorSubject<Boolean> isSubscribed = BehaviorSubject.create();

Expand Down Expand Up @@ -124,9 +113,6 @@ public Observable<BluetoothGatt> asObservable() {

@Override
protected void protectedRun() {
final Runnable onConnectionEstablishedRunnable = autoConnect ? emptyRunnable : releaseRadioRunnable;
final Runnable onConnectCalledRunnable = autoConnect ? releaseRadioRunnable : emptyRunnable;

getConnectedBluetoothGatt()
.compose(wrapWithTimeoutWhenNotAutoconnecting())
// when there are no subscribers there is no point of continuing work -> next will be disconnect operation
Expand All @@ -136,20 +122,18 @@ public void call(Boolean noSubscribers) {
RxBleLog.d("No subscribers, finishing operation");
}
}))
.doOnCompleted(new Action0() {
@Override
public void call() {
onConnectionEstablishedRunnable.run();
}
})
.doOnNext(new Action1<BluetoothGatt>() {
@Override
public void call(BluetoothGatt ignored) {
isSubscribed.onCompleted();
}
})
.subscribe(getSubscriber());
onConnectCalledRunnable.run();

if (autoConnect) {
// with autoConnect the connection may be established after a really long time
releaseRadio();
}
}

private Observable.Transformer<BluetoothGatt, BluetoothGatt> wrapWithTimeoutWhenNotAutoconnecting() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ protected void protectedRun() {
onCompleted();
} else {
(isDisconnected(bluetoothGatt) ? just(bluetoothGatt) : disconnect(bluetoothGatt))
.doOnTerminate(new Action0() {
@Override
public void call() {
releaseRadio();
}
})
.observeOn(mainThreadScheduler)
.subscribe(
new Action1<BluetoothGatt>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ public class RxBleRadioOperationDisconnectTest extends Specification {
RxAndroidPlugins.getInstance().reset()
}

def setup() {
private def testWithGattProviderReturning(BluetoothGatt providedBluetoothGatt) {
mockBluetoothGattProvider = Mock(BluetoothGattProvider)
mockBluetoothGattProvider.getBluetoothGatt() >>mockBluetoothGatt
mockBluetoothGattProvider.getBluetoothGatt() >> providedBluetoothGatt
mockGattCallback.getOnConnectionStateChange() >> connectionStatePublishSubject
mockBluetoothGatt.getDevice() >> mockDevice
prepareObjectUnderTest()
}

def "should complete if AtomicReference<BluetoothGatt> contains null and then release the radio"() {

given:
testWithGattProviderReturning(null)

when:
objectUnderTest.run()

Expand All @@ -78,6 +82,7 @@ public class RxBleRadioOperationDisconnectTest extends Specification {
def "should call BluetoothGatt.close() if BluetoothGatt is disconnected at the time of running and then release the radio"() {

given:
testWithGattProviderReturning(mockBluetoothGatt)
mockBluetoothManager.getConnectionState(mockDevice, GATT) >> STATE_DISCONNECTED

when:
Expand All @@ -94,6 +99,7 @@ public class RxBleRadioOperationDisconnectTest extends Specification {
def "should call BluetoothGatt.disconnect() if BluetoothGatt is not disconnected at the time of running and then BluetoothGatt.close() when RxBleGattCallback.getOnConnectionStateChange() will emit RxBleConnection.RxBleConnectionState.DISCONNECTED and then release the radio"() {

given:
testWithGattProviderReturning(mockBluetoothGatt)
mockBluetoothManager.getConnectionState(mockDevice, GATT) >> initialState

when:
Expand Down

0 comments on commit ea243e6

Please sign in to comment.