From 0348fa65c3d808f854f745fb9f8f185a5b74b922 Mon Sep 17 00:00:00 2001 From: tommyt Date: Mon, 14 Mar 2016 15:12:28 -0700 Subject: bluetooth: android: Confirm the notify session after the descriptor has been written. This change also implements WriteRemoteDescriptor and ReadRemoteDescriptor. Because of this, I've also added quite a few descriptor unit tests. These tests are pretty much the same as the read/write tests for characteristics. BUG=584369 Review URL: https://codereview.chromium.org/1712593002 Cr-Commit-Position: refs/heads/master@{#381088} --- .../src/org/chromium/device/bluetooth/Fakes.java | 72 +++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'device/bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java') diff --git a/device/bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java b/device/bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java index 46f9c66..f3d98a7 100644 --- a/device/bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java +++ b/device/bluetooth/test/android/java/src/org/chromium/device/bluetooth/Fakes.java @@ -368,6 +368,7 @@ class Fakes { boolean mReadCharacteristicWillFailSynchronouslyOnce = false; boolean mSetCharacteristicNotificationWillFailSynchronouslyOnce = false; boolean mWriteCharacteristicWillFailSynchronouslyOnce = false; + boolean mReadDescriptorWillFailSynchronouslyOnce = false; boolean mWriteDescriptorWillFailSynchronouslyOnce = false; public FakeBluetoothGatt(FakeBluetoothDevice device) { @@ -431,6 +432,16 @@ class Fakes { } @Override + boolean readDescriptor(Wrappers.BluetoothGattDescriptorWrapper descriptor) { + if (mReadDescriptorWillFailSynchronouslyOnce) { + mReadDescriptorWillFailSynchronouslyOnce = false; + return false; + } + nativeOnFakeBluetoothGattReadDescriptor(mDevice.mAdapter.mNativeBluetoothTestAndroid); + return true; + } + + @Override boolean writeDescriptor(Wrappers.BluetoothGattDescriptorWrapper descriptor) { if (mWriteDescriptorWillFailSynchronouslyOnce) { mWriteDescriptorWillFailSynchronouslyOnce = false; @@ -686,15 +697,65 @@ class Fakes { final FakeBluetoothGattCharacteristic mCharacteristic; final UUID mUuid; byte[] mValue; + static FakeBluetoothGattDescriptor sRememberedDescriptor; public FakeBluetoothGattDescriptor( FakeBluetoothGattCharacteristic characteristic, UUID uuid) { - super(null); + super(null, null); mCharacteristic = characteristic; mUuid = uuid; mValue = new byte[0]; } + // Implements BluetoothTestAndroid::RememberDescriptorForSubsequentAction. + @CalledByNative("FakeBluetoothGattDescriptor") + private static void rememberDescriptorForSubsequentAction( + ChromeBluetoothRemoteGattDescriptor chromeDescriptor) { + sRememberedDescriptor = (FakeBluetoothGattDescriptor) chromeDescriptor.mDescriptor; + } + + // Simulate a value being read from a descriptor. + @CalledByNative("FakeBluetoothGattDescriptor") + private static void valueRead( + ChromeBluetoothRemoteGattDescriptor chromeDescriptor, int status, byte[] value) { + if (chromeDescriptor == null && sRememberedDescriptor == null) + throw new IllegalArgumentException("rememberDescriptor wasn't called previously."); + + FakeBluetoothGattDescriptor fakeDescriptor = (chromeDescriptor == null) + ? sRememberedDescriptor + : (FakeBluetoothGattDescriptor) chromeDescriptor.mDescriptor; + + fakeDescriptor.mValue = value; + fakeDescriptor.mCharacteristic.mService.mDevice.mGattCallback.onDescriptorRead( + fakeDescriptor, status); + } + + // Simulate a value being written to a descriptor. + @CalledByNative("FakeBluetoothGattDescriptor") + private static void valueWrite( + ChromeBluetoothRemoteGattDescriptor chromeDescriptor, int status) { + if (chromeDescriptor == null && sRememberedDescriptor == null) + throw new IllegalArgumentException("rememberDescriptor wasn't called previously."); + + FakeBluetoothGattDescriptor fakeDescriptor = (chromeDescriptor == null) + ? sRememberedDescriptor + : (FakeBluetoothGattDescriptor) chromeDescriptor.mDescriptor; + + fakeDescriptor.mCharacteristic.mService.mDevice.mGattCallback.onDescriptorWrite( + fakeDescriptor, status); + } + + // Cause subsequent value read of a descriptor to fail synchronously. + @CalledByNative("FakeBluetoothGattDescriptor") + private static void setReadDescriptorWillFailSynchronouslyOnce( + ChromeBluetoothRemoteGattDescriptor chromeDescriptor) { + FakeBluetoothGattDescriptor fakeDescriptor = + (FakeBluetoothGattDescriptor) chromeDescriptor.mDescriptor; + + fakeDescriptor.mCharacteristic.mService.mDevice.mGatt + .mReadDescriptorWillFailSynchronouslyOnce = true; + } + // Cause subsequent value write of a descriptor to fail synchronously. @CalledByNative("FakeBluetoothGattDescriptor") private static void setWriteDescriptorWillFailSynchronouslyOnce( @@ -710,6 +771,11 @@ class Fakes { // Wrappers.BluetoothGattDescriptorWrapper overrides: @Override + public Wrappers.BluetoothGattCharacteristicWrapper getCharacteristic() { + return mCharacteristic; + } + + @Override public UUID getUuid() { return mUuid; } @@ -759,6 +825,10 @@ class Fakes { private static native void nativeOnFakeBluetoothGattWriteCharacteristic( long nativeBluetoothTestAndroid, byte[] value); + // Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadDescriptor. + private static native void nativeOnFakeBluetoothGattReadDescriptor( + long nativeBluetoothTestAndroid); + // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteDescriptor. private static native void nativeOnFakeBluetoothGattWriteDescriptor( long nativeBluetoothTestAndroid, byte[] value); -- cgit v1.1