-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added OperationPhyReadTest and OperationPhyUpdateTest. Minor fixes.
- Loading branch information
1 parent
df8eebc
commit e302fda
Showing
4 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...src/test/groovy/com/polidea/rxandroidble2/internal/operations/OperationPhyReadTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...c/test/groovy/com/polidea/rxandroidble2/internal/operations/OperationPhyUpdateTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |