Skip to content

Commit

Permalink
Added OperationPhyReadTest and OperationPhyUpdateTest. Minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dariuszseweryn committed Dec 14, 2023
1 parent df8eebc commit e302fda
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.bluetooth.BluetoothGatt;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.RequiresPermission;

Expand Down Expand Up @@ -36,6 +37,7 @@ protected boolean startOperation(BluetoothGatt bluetoothGatt) {
return true;
}

@NonNull
@Override
public String toString() {
return "PhyReadOperation{" + super.toString() + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected Single<PhyPair> getCallback(RxBleGattCallback rxBleGattCallback) {
protected boolean startOperation(BluetoothGatt bluetoothGatt) {
bluetoothGatt.setPreferredPhy(
RxBlePhy.enumSetToValuesMask(txPhy),
RxBlePhy.enumSetToValuesMask(txPhy),
RxBlePhy.enumSetToValuesMask(rxPhy),
phyOptions.getValue()
);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.polidea.rxandroidble2.internal.operations

import android.bluetooth.BluetoothGatt
import com.polidea.rxandroidble2.PhyPair
import com.polidea.rxandroidble2.RxBlePhy
import com.polidea.rxandroidble2.RxBlePhyOption
import com.polidea.rxandroidble2.exceptions.BleGattCallbackTimeoutException
import com.polidea.rxandroidble2.exceptions.BleGattOperationType
import com.polidea.rxandroidble2.internal.connection.RxBleGattCallback
import com.polidea.rxandroidble2.internal.serialization.QueueReleaseInterface
import com.polidea.rxandroidble2.internal.util.MockOperationTimeoutConfiguration
import io.reactivex.schedulers.TestScheduler
import io.reactivex.subjects.PublishSubject
import spock.lang.Specification

import java.util.concurrent.TimeUnit

public class OperationPhyReadTest extends Specification {

static long timeout = 10
static TimeUnit timeoutTimeUnit = TimeUnit.SECONDS
QueueReleaseInterface mockQueueReleaseInterface = Mock QueueReleaseInterface
BluetoothGatt mockBluetoothGatt = Mock BluetoothGatt
RxBleGattCallback mockGattCallback = Mock RxBleGattCallback
TestScheduler testScheduler = new TestScheduler()
PublishSubject<PhyPair> readPhyPublishSubject = PublishSubject.create()
PhyReadOperation objectUnderTest

def setup() {
mockGattCallback.getOnPhyRead() >> readPhyPublishSubject
prepareObjectUnderTest()
}

def "should call BluetoothGatt.readPhy() exactly once when run()"() {

when:
objectUnderTest.run(mockQueueReleaseInterface).test()

then:
1 * mockBluetoothGatt.readPhy()
}

def "should emit an error if RxBleGattCallback will emit error on RxBleGattCallback.getOnPhyRead() and release queue"() {

given:
def testSubscriber = objectUnderTest.run(mockQueueReleaseInterface).test()
def testException = new Exception("test")

when:
readPhyPublishSubject.onError(testException)

then:
testSubscriber.assertError(testException)

and:
(1.._) * mockQueueReleaseInterface.release() // technically it's not an error to call it more than once
}

def "should timeout if will not response after 10 seconds "() {

given:
def testSubscriber = objectUnderTest.run(mockQueueReleaseInterface).test()

when:
testScheduler.advanceTimeTo(timeout + 5, timeoutTimeUnit)

then:
testSubscriber.assertError(BleGattCallbackTimeoutException)

and:
testSubscriber.assertError {
((BleGattCallbackTimeoutException)it).getBleGattOperationType() == BleGattOperationType.PHY_READ
}
}

private prepareObjectUnderTest() {
objectUnderTest = new PhyReadOperation(mockGattCallback, mockBluetoothGatt,
new MockOperationTimeoutConfiguration(timeout.intValue(), testScheduler))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.polidea.rxandroidble2.internal.operations

import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import com.polidea.rxandroidble2.PhyPair
import com.polidea.rxandroidble2.RxBlePhy
import com.polidea.rxandroidble2.RxBlePhyOption
import com.polidea.rxandroidble2.exceptions.BleGattCallbackTimeoutException
import com.polidea.rxandroidble2.exceptions.BleGattOperationType
import com.polidea.rxandroidble2.internal.connection.RxBleGattCallback
import com.polidea.rxandroidble2.internal.serialization.QueueReleaseInterface
import com.polidea.rxandroidble2.internal.util.MockOperationTimeoutConfiguration
import io.reactivex.schedulers.TestScheduler
import io.reactivex.subjects.PublishSubject
import spock.lang.Specification

import java.util.concurrent.TimeUnit

public class OperationPhyUpdateTest extends Specification {

static long timeout = 10
static TimeUnit timeoutTimeUnit = TimeUnit.SECONDS
QueueReleaseInterface mockQueueReleaseInterface = Mock QueueReleaseInterface
BluetoothGatt mockBluetoothGatt = Mock BluetoothGatt
RxBleGattCallback mockGattCallback = Mock RxBleGattCallback
TestScheduler testScheduler = new TestScheduler()
PublishSubject<PhyPair> updatedPhyPublishSubject = PublishSubject.create()
PhyUpdateOperation objectUnderTest
EnumSet<RxBlePhy> rxSet = EnumSet.of(RxBlePhy.PHY_1M)
EnumSet<RxBlePhy> txSet = EnumSet.of(RxBlePhy.PHY_1M, RxBlePhy.PHY_2M)
RxBlePhyOption phyOption = RxBlePhyOption.PHY_OPTION_S8

def setup() {
mockGattCallback.getOnPhyUpdate() >> updatedPhyPublishSubject
prepareObjectUnderTest()
}

def "should call BluetoothGatt.setPreferredPhy(int, int, int) exactly once when run()"() {

when:
objectUnderTest.run(mockQueueReleaseInterface).test()

then:
1 * mockBluetoothGatt.setPreferredPhy(
RxBlePhy.enumSetToValuesMask(txSet),
RxBlePhy.enumSetToValuesMask(rxSet),
phyOption.value
)
}

def "should emit an error if RxBleGattCallback will emit error on RxBleGattCallback.getOnPhyUpdate() and release queue"() {

given:
def testSubscriber = objectUnderTest.run(mockQueueReleaseInterface).test()
def testException = new Exception("test")

when:
updatedPhyPublishSubject.onError(testException)

then:
testSubscriber.assertError(testException)

and:
(1.._) * mockQueueReleaseInterface.release() // technically it's not an error to call it more than once
}

def "should timeout if will not response after 10 seconds "() {

given:
println(objectUnderTest.toString())
def testSubscriber = objectUnderTest.run(mockQueueReleaseInterface).test()

when:
testScheduler.advanceTimeTo(timeout + 5, timeoutTimeUnit)

then:
testSubscriber.assertError(BleGattCallbackTimeoutException)

and:
testSubscriber.assertError {
((BleGattCallbackTimeoutException)it).getBleGattOperationType() == BleGattOperationType.PHY_UPDATE
}
}

private prepareObjectUnderTest() {
objectUnderTest = new PhyUpdateOperation(mockGattCallback, mockBluetoothGatt,
new MockOperationTimeoutConfiguration(timeout.intValue(), testScheduler), txSet, rxSet, phyOption)
}
}

0 comments on commit e302fda

Please sign in to comment.