OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.device.bluetooth; | 5 package org.chromium.device.bluetooth; |
6 | 6 |
7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
8 import android.bluetooth.BluetoothDevice; | 8 import android.bluetooth.BluetoothDevice; |
9 import android.bluetooth.le.ScanFilter; | 9 import android.bluetooth.le.ScanFilter; |
10 import android.bluetooth.le.ScanSettings; | 10 import android.bluetooth.le.ScanSettings; |
11 import android.content.BroadcastReceiver; | 11 import android.content.BroadcastReceiver; |
12 import android.content.Context; | 12 import android.content.Context; |
13 import android.content.Intent; | 13 import android.content.Intent; |
14 import android.content.IntentFilter; | 14 import android.content.IntentFilter; |
15 | 15 |
16 import android.os.Build; | 16 import android.os.Build; |
17 import android.os.ParcelUuid; | 17 import android.os.ParcelUuid; |
18 | 18 |
19 import android.test.mock.MockContext; | 19 import android.test.mock.MockContext; |
20 | 20 |
21 import org.chromium.base.Log; | 21 import org.chromium.base.Log; |
22 import org.chromium.base.annotations.CalledByNative; | 22 import org.chromium.base.annotations.CalledByNative; |
23 import org.chromium.base.annotations.JNINamespace; | 23 import org.chromium.base.annotations.JNINamespace; |
24 import org.chromium.components.location.LocationUtils; | 24 import org.chromium.components.location.LocationUtils; |
| 25 import org.chromium.device.bluetooth.test.TestRSSI; |
| 26 import org.chromium.device.bluetooth.test.TestTxPower; |
25 | 27 |
26 import java.util.ArrayList; | 28 import java.util.ArrayList; |
27 import java.util.HashMap; | 29 import java.util.HashMap; |
28 import java.util.List; | 30 import java.util.List; |
29 import java.util.UUID; | 31 import java.util.UUID; |
30 | 32 |
31 /** | 33 /** |
32 * Fake implementations of android.bluetooth.* classes for testing. | 34 * Fake implementations of android.bluetooth.* classes for testing. |
33 * | 35 * |
34 * Fakes are contained in a single file to simplify code. Only one C++ file may | 36 * Fakes are contained in a single file to simplify code. Only one C++ file may |
35 * access a Java file via JNI, and all of these classes are accessed by | 37 * access a Java file via JNI, and all of these classes are accessed by |
36 * bluetooth_test_android.cc. The alternative would be a C++ .h, .cc file for | 38 * bluetooth_test_android.cc. The alternative would be a C++ .h, .cc file for |
37 * each of these classes. | 39 * each of these classes. |
38 */ | 40 */ |
39 @JNINamespace("device") | 41 @JNINamespace("device") |
40 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 42 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
41 class Fakes { | 43 class Fakes { |
42 private static final String TAG = "cr.Bluetooth"; | 44 private static final String TAG = "cr.Bluetooth"; |
43 | 45 |
| 46 // Android uses Integer.MIN_VALUE to signal no Tx Power in advertisement |
| 47 // packet. |
| 48 // https://developer.android.com/reference/android/bluetooth/le/ScanRecord.h
tml#getTxPowerLevel() |
| 49 private static final int NO_TX_POWER = Integer.MIN_VALUE; |
| 50 |
44 /** | 51 /** |
45 * Sets the factory for LocationUtils to return an instance whose | 52 * Sets the factory for LocationUtils to return an instance whose |
46 * hasAndroidLocationPermission and isSystemLocationSettingEnabled return | 53 * hasAndroidLocationPermission and isSystemLocationSettingEnabled return |
47 * values depend on |hasPermission| and |isEnabled| respectively. | 54 * values depend on |hasPermission| and |isEnabled| respectively. |
48 */ | 55 */ |
49 @CalledByNative | 56 @CalledByNative |
50 public static void setLocationServicesState( | 57 public static void setLocationServicesState( |
51 final boolean hasPermission, final boolean isEnabled) { | 58 final boolean hasPermission, final boolean isEnabled) { |
52 LocationUtils.setFactory(new LocationUtils.Factory() { | 59 LocationUtils.setFactory(new LocationUtils.Factory() { |
53 @Override | 60 @Override |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 if (mFakeScanner == null) { | 107 if (mFakeScanner == null) { |
101 return; | 108 return; |
102 } | 109 } |
103 | 110 |
104 switch (deviceOrdinal) { | 111 switch (deviceOrdinal) { |
105 case 1: { | 112 case 1: { |
106 ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(2); | 113 ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(2); |
107 uuids.add(ParcelUuid.fromString("00001800-0000-1000-8000-008
05f9b34fb")); | 114 uuids.add(ParcelUuid.fromString("00001800-0000-1000-8000-008
05f9b34fb")); |
108 uuids.add(ParcelUuid.fromString("00001801-0000-1000-8000-008
05f9b34fb")); | 115 uuids.add(ParcelUuid.fromString("00001801-0000-1000-8000-008
05f9b34fb")); |
109 | 116 |
110 mFakeScanner.mScanCallback.onScanResult(ScanSettings.CALLBAC
K_TYPE_ALL_MATCHES, | 117 mFakeScanner.mScanCallback.onScanResult( |
| 118 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, |
111 new FakeScanResult(new FakeBluetoothDevice(this, "01
:00:00:90:1E:BE", | 119 new FakeScanResult(new FakeBluetoothDevice(this, "01
:00:00:90:1E:BE", |
112 "FakeBluetoothDevice"), | 120 "FakeBluetoothDevice"), |
113 uuids)); | 121 TestRSSI.LOWEST, uuids, TestTxPower.LOWEST))
; |
114 break; | 122 break; |
115 } | 123 } |
116 case 2: { | 124 case 2: { |
117 ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(2); | 125 ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(2); |
118 uuids.add(ParcelUuid.fromString("00001802-0000-1000-8000-008
05f9b34fb")); | 126 uuids.add(ParcelUuid.fromString("00001802-0000-1000-8000-008
05f9b34fb")); |
119 uuids.add(ParcelUuid.fromString("00001803-0000-1000-8000-008
05f9b34fb")); | 127 uuids.add(ParcelUuid.fromString("00001803-0000-1000-8000-008
05f9b34fb")); |
120 | 128 |
121 mFakeScanner.mScanCallback.onScanResult(ScanSettings.CALLBAC
K_TYPE_ALL_MATCHES, | 129 mFakeScanner.mScanCallback.onScanResult( |
| 130 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, |
122 new FakeScanResult(new FakeBluetoothDevice(this, "01
:00:00:90:1E:BE", | 131 new FakeScanResult(new FakeBluetoothDevice(this, "01
:00:00:90:1E:BE", |
123 "FakeBluetoothDevice"), | 132 "FakeBluetoothDevice"), |
124 uuids)); | 133 TestRSSI.LOWER, uuids, TestTxPower.LOWER)); |
125 break; | 134 break; |
126 } | 135 } |
127 case 3: { | 136 case 3: { |
128 ArrayList<ParcelUuid> uuids = null; | 137 ArrayList<ParcelUuid> uuids = null; |
129 mFakeScanner.mScanCallback.onScanResult( | 138 mFakeScanner.mScanCallback.onScanResult( |
130 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, | 139 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, |
131 new FakeScanResult( | 140 new FakeScanResult( |
132 new FakeBluetoothDevice(this, "01:00:00:90:1
E:BE", ""), uuids)); | 141 new FakeBluetoothDevice(this, "01:00:00:90:1
E:BE", ""), |
| 142 TestRSSI.LOW, uuids, NO_TX_POWER)); |
133 | 143 |
134 break; | 144 break; |
135 } | 145 } |
136 case 4: { | 146 case 4: { |
137 ArrayList<ParcelUuid> uuids = null; | 147 ArrayList<ParcelUuid> uuids = null; |
138 mFakeScanner.mScanCallback.onScanResult( | 148 mFakeScanner.mScanCallback.onScanResult( |
139 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, | 149 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, |
140 new FakeScanResult( | 150 new FakeScanResult( |
141 new FakeBluetoothDevice(this, "02:00:00:8B:7
4:63", ""), uuids)); | 151 new FakeBluetoothDevice(this, "02:00:00:8B:7
4:63", ""), |
| 152 TestRSSI.MEDIUM, uuids, NO_TX_POWER)); |
142 | 153 |
143 break; | 154 break; |
144 } | 155 } |
145 case 5: { | 156 case 5: { |
146 ArrayList<ParcelUuid> uuids = null; | 157 ArrayList<ParcelUuid> uuids = null; |
147 mFakeScanner.mScanCallback.onScanResult(ScanSettings.CALLBAC
K_TYPE_ALL_MATCHES, | 158 mFakeScanner.mScanCallback.onScanResult( |
148 new FakeScanResult(new FakeBluetoothDevice( | 159 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, |
149 this, "01:00:00:90:1E:BE"
, null), | 160 new FakeScanResult( |
150 uuids)); | 161 new FakeBluetoothDevice(this, "01:00:00:90:1
E:BE", null), |
| 162 TestRSSI.HIGH, uuids, NO_TX_POWER)); |
151 break; | 163 break; |
152 } | 164 } |
153 } | 165 } |
154 } | 166 } |
155 | 167 |
156 @CalledByNative("FakeBluetoothAdapter") | 168 @CalledByNative("FakeBluetoothAdapter") |
157 public void forceIllegalStateException() { | 169 public void forceIllegalStateException() { |
158 if (mFakeScanner != null) { | 170 if (mFakeScanner != null) { |
159 mFakeScanner.forceIllegalStateException(); | 171 mFakeScanner.forceIllegalStateException(); |
160 } | 172 } |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 void forceIllegalStateException() { | 278 void forceIllegalStateException() { |
267 mThrowException = true; | 279 mThrowException = true; |
268 } | 280 } |
269 } | 281 } |
270 | 282 |
271 /** | 283 /** |
272 * Fakes android.bluetooth.le.ScanResult | 284 * Fakes android.bluetooth.le.ScanResult |
273 */ | 285 */ |
274 static class FakeScanResult extends Wrappers.ScanResultWrapper { | 286 static class FakeScanResult extends Wrappers.ScanResultWrapper { |
275 private final FakeBluetoothDevice mDevice; | 287 private final FakeBluetoothDevice mDevice; |
| 288 private final int mRssi; |
| 289 private final int mTxPower; |
276 private final ArrayList<ParcelUuid> mUuids; | 290 private final ArrayList<ParcelUuid> mUuids; |
277 | 291 |
278 FakeScanResult(FakeBluetoothDevice device, ArrayList<ParcelUuid> uuids)
{ | 292 FakeScanResult( |
| 293 FakeBluetoothDevice device, int rssi, ArrayList<ParcelUuid> uuid
s, int txPower) { |
279 super(null); | 294 super(null); |
280 mDevice = device; | 295 mDevice = device; |
| 296 mRssi = rssi; |
281 mUuids = uuids; | 297 mUuids = uuids; |
| 298 mTxPower = txPower; |
282 } | 299 } |
283 | 300 |
284 @Override | 301 @Override |
285 public Wrappers.BluetoothDeviceWrapper getDevice() { | 302 public Wrappers.BluetoothDeviceWrapper getDevice() { |
286 return mDevice; | 303 return mDevice; |
287 } | 304 } |
288 | 305 |
289 @Override | 306 @Override |
| 307 public int getRssi() { |
| 308 return mRssi; |
| 309 } |
| 310 |
| 311 @Override |
290 public List<ParcelUuid> getScanRecord_getServiceUuids() { | 312 public List<ParcelUuid> getScanRecord_getServiceUuids() { |
291 return mUuids; | 313 return mUuids; |
292 } | 314 } |
| 315 |
| 316 @Override |
| 317 public int getScanRecord_getTxPowerLevel() { |
| 318 return mTxPower; |
| 319 } |
293 } | 320 } |
294 | 321 |
295 /** | 322 /** |
296 * Fakes android.bluetooth.BluetoothDevice. | 323 * Fakes android.bluetooth.BluetoothDevice. |
297 */ | 324 */ |
298 static class FakeBluetoothDevice extends Wrappers.BluetoothDeviceWrapper { | 325 static class FakeBluetoothDevice extends Wrappers.BluetoothDeviceWrapper { |
299 final FakeBluetoothAdapter mAdapter; | 326 final FakeBluetoothAdapter mAdapter; |
300 private String mAddress; | 327 private String mAddress; |
301 private String mName; | 328 private String mName; |
302 final FakeBluetoothGatt mGatt; | 329 final FakeBluetoothGatt mGatt; |
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 long nativeBluetoothTestAndroid, byte[] value); | 869 long nativeBluetoothTestAndroid, byte[] value); |
843 | 870 |
844 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadDescriptor. | 871 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadDescriptor. |
845 private static native void nativeOnFakeBluetoothGattReadDescriptor( | 872 private static native void nativeOnFakeBluetoothGattReadDescriptor( |
846 long nativeBluetoothTestAndroid); | 873 long nativeBluetoothTestAndroid); |
847 | 874 |
848 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteDescriptor. | 875 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteDescriptor. |
849 private static native void nativeOnFakeBluetoothGattWriteDescriptor( | 876 private static native void nativeOnFakeBluetoothGattWriteDescriptor( |
850 long nativeBluetoothTestAndroid, byte[] value); | 877 long nativeBluetoothTestAndroid, byte[] value); |
851 } | 878 } |
OLD | NEW |