-
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 request to change connection priority
Summary: #111 Reviewers: pawel.urban, dariusz.seweryn Reviewed By: dariusz.seweryn Differential Revision: https://phabricator.polidea.com/D2363
- Loading branch information
Showing
9 changed files
with
233 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
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
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
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
52 changes: 52 additions & 0 deletions
52
...olidea/rxandroidble/internal/operations/RxBleRadioOperationConnectionPriorityRequest.java
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,52 @@ | ||
package com.polidea.rxandroidble.internal.operations; | ||
|
||
import android.bluetooth.BluetoothGatt; | ||
import android.os.Build; | ||
import android.support.annotation.RequiresApi; | ||
|
||
import com.polidea.rxandroidble.exceptions.BleGattCannotStartException; | ||
import com.polidea.rxandroidble.exceptions.BleGattOperationType; | ||
import com.polidea.rxandroidble.internal.RxBleSingleGattRadioOperation; | ||
import com.polidea.rxandroidble.internal.connection.RxBleGattCallback; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.inject.Inject; | ||
|
||
import rx.Observable; | ||
import rx.Scheduler; | ||
|
||
public class RxBleRadioOperationConnectionPriorityRequest extends RxBleSingleGattRadioOperation<Long> { | ||
|
||
private final int connectionPriority; | ||
private final long operationTimeout; | ||
private final TimeUnit timeUnit; | ||
private final Scheduler delayScheduler; | ||
|
||
@Inject | ||
RxBleRadioOperationConnectionPriorityRequest( | ||
RxBleGattCallback rxBleGattCallback, | ||
BluetoothGatt bluetoothGatt, | ||
TimeoutConfiguration timeoutConfiguration, | ||
int connectionPriority, | ||
long operationTimeout, | ||
TimeUnit timeUnit, | ||
Scheduler delayScheduler) { | ||
super(bluetoothGatt, rxBleGattCallback, BleGattOperationType.CONNECTION_PRIORITY_CHANGE, timeoutConfiguration); | ||
this.connectionPriority = connectionPriority; | ||
this.operationTimeout = operationTimeout; | ||
this.timeUnit = timeUnit; | ||
this.delayScheduler = delayScheduler; | ||
} | ||
|
||
@Override | ||
protected Observable<Long> getCallback(RxBleGattCallback rxBleGattCallback) { | ||
return Observable.timer(operationTimeout, timeUnit, delayScheduler); | ||
} | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
@Override | ||
protected boolean startOperation(BluetoothGatt bluetoothGatt) throws IllegalArgumentException, BleGattCannotStartException { | ||
return bluetoothGatt.requestConnectionPriority(connectionPriority); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
.../rxandroidble/internal/operations/RxBleRadioOperationConnectionPriorityRequestTest.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,78 @@ | ||
package com.polidea.rxandroidble.internal.operations | ||
|
||
import android.bluetooth.BluetoothGatt | ||
import com.polidea.rxandroidble.exceptions.BleGattCannotStartException | ||
import com.polidea.rxandroidble.internal.connection.RxBleGattCallback | ||
import com.polidea.rxandroidble.internal.util.MockOperationTimeoutConfiguration | ||
import rx.observers.TestSubscriber | ||
import rx.schedulers.TestScheduler | ||
import spock.lang.Specification | ||
|
||
import java.util.concurrent.Semaphore | ||
import java.util.concurrent.TimeUnit | ||
|
||
class RxBleRadioOperationConnectionPriorityRequestTest extends Specification { | ||
|
||
static long timeout = 10 | ||
int completedDelay = 500L | ||
TimeUnit delayUnit = TimeUnit.MILLISECONDS | ||
BluetoothGatt mockBluetoothGatt = Mock BluetoothGatt | ||
RxBleGattCallback mockGattCallback = Mock RxBleGattCallback | ||
Semaphore mockSemaphore = Mock Semaphore | ||
TestSubscriber<Integer> testSubscriber = new TestSubscriber() | ||
TestScheduler testScheduler = new TestScheduler() | ||
TimeoutConfiguration mockTimeoutConfiguration = new MockOperationTimeoutConfiguration( | ||
timeout.intValue(), | ||
testScheduler | ||
) | ||
RxBleRadioOperationConnectionPriorityRequest objectUnderTest | ||
|
||
def setup() { | ||
prepareObjectUnderTest(BluetoothGatt.CONNECTION_PRIORITY_LOW_POWER) | ||
} | ||
|
||
def "should call BluetoothGatt.requestConnectionPriority(int) exactly once when run()"() { | ||
when: | ||
objectUnderTest.run() | ||
|
||
then: | ||
1 * mockBluetoothGatt.requestConnectionPriority(_) >> true | ||
} | ||
|
||
def "should complete after specified time if BluetoothGatt.requestConnectionPriority() will return true"() { | ||
given: | ||
mockBluetoothGatt.requestConnectionPriority(_) >> true | ||
objectUnderTest.run() | ||
|
||
when: | ||
testScheduler.advanceTimeBy(completedDelay + 500, delayUnit) | ||
|
||
then: | ||
testSubscriber.assertCompleted() | ||
} | ||
|
||
def "should throw exception if operation failed"() { | ||
given: | ||
mockBluetoothGatt.requestConnectionPriority(_) >> false | ||
|
||
when: | ||
objectUnderTest.run() | ||
|
||
then: | ||
testSubscriber.assertError BleGattCannotStartException | ||
} | ||
|
||
def prepareObjectUnderTest(int connectionPriority) { | ||
objectUnderTest = new RxBleRadioOperationConnectionPriorityRequest( | ||
mockGattCallback, | ||
mockBluetoothGatt, | ||
mockTimeoutConfiguration, | ||
connectionPriority, | ||
completedDelay, | ||
delayUnit, | ||
testScheduler | ||
) | ||
objectUnderTest.setRadioBlockingSemaphore(mockSemaphore) | ||
objectUnderTest.asObservable().subscribe(testSubscriber) | ||
} | ||
} |