Chromium Code Reviews| 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.le.ScanFilter; | 9 import android.bluetooth.le.ScanFilter; |
| 9 import android.bluetooth.le.ScanSettings; | 10 import android.bluetooth.le.ScanSettings; |
| 10 import android.os.Build; | 11 import android.os.Build; |
| 12 import android.os.ParcelUuid; | |
| 11 | 13 |
| 12 import org.chromium.base.CalledByNative; | 14 import org.chromium.base.CalledByNative; |
| 13 import org.chromium.base.Log; | 15 import org.chromium.base.Log; |
| 14 | 16 |
| 17 import java.util.ArrayList; | |
| 15 import java.util.List; | 18 import java.util.List; |
| 16 | 19 |
| 17 /** | 20 /** |
| 18 * Fake implementations of android.bluetooth.* classes for testing. | 21 * Fake implementations of android.bluetooth.* classes for testing. |
| 19 */ | 22 */ |
| 20 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 23 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 21 class Fakes { | 24 class Fakes { |
| 22 private static final String TAG = "cr.Bluetooth"; | 25 private static final String TAG = "cr.Bluetooth"; |
| 23 | 26 |
| 24 /** | 27 /** |
| 25 * Fakes android.bluetooth.BluetoothAdapter. | 28 * Fakes android.bluetooth.BluetoothAdapter. |
| 26 */ | 29 */ |
| 27 static class FakeBluetoothAdapter extends Wrappers.BluetoothAdapterWrapper { | 30 static class FakeBluetoothAdapter extends Wrappers.BluetoothAdapterWrapper { |
| 28 private final FakeBluetoothLeScanner mFakeScanner; | 31 private final FakeBluetoothLeScanner mFakeScanner; |
| 29 | 32 |
| 30 /** | 33 /** |
| 31 * Creates a FakeBluetoothAdapter. | 34 * Creates a FakeBluetoothAdapter. |
| 32 */ | 35 */ |
| 33 @CalledByNative("FakeBluetoothAdapter") | 36 @CalledByNative("FakeBluetoothAdapter") |
| 34 public static FakeBluetoothAdapter create() { | 37 public static FakeBluetoothAdapter create() { |
| 35 Log.v(TAG, "FakeBluetoothAdapter created."); | 38 Log.v(TAG, "FakeBluetoothAdapter created."); |
| 36 return new FakeBluetoothAdapter(); | 39 return new FakeBluetoothAdapter(); |
| 37 } | 40 } |
| 38 | 41 |
| 39 private FakeBluetoothAdapter() { | 42 private FakeBluetoothAdapter() { |
| 40 super(null, new FakeBluetoothLeScanner()); | 43 super(null, new FakeBluetoothLeScanner()); |
| 41 mFakeScanner = (FakeBluetoothLeScanner) mScanner; | 44 mFakeScanner = (FakeBluetoothLeScanner) mScanner; |
| 42 } | 45 } |
| 43 | 46 |
| 47 /** | |
| 48 * Creates and discovers a new device. | |
| 49 */ | |
| 50 @CalledByNative("FakeBluetoothAdapter") | |
| 51 public void discoverLowEnergyDevice(int deviceOrdinal) { | |
| 52 switch (deviceOrdinal) { | |
| 53 case 1: { | |
| 54 ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(2); | |
| 55 uuids.add(ParcelUuid.fromString("00001800-0000-1000-8000-008 05f9b34fb")); | |
| 56 uuids.add(ParcelUuid.fromString("00001801-0000-1000-8000-008 05f9b34fb")); | |
| 57 | |
| 58 mFakeScanner.mCallback.onScanResultWrapper( | |
| 59 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, | |
| 60 new FakeScanResult( | |
| 61 new FakeBluetoothDevice("FakeBluetoothDevice "), uuids)); | |
| 62 break; | |
| 63 } | |
| 64 case 2: { | |
| 65 ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(2); | |
| 66 uuids.add(ParcelUuid.fromString("00001802-0000-1000-8000-008 05f9b34fb")); | |
| 67 uuids.add(ParcelUuid.fromString("00001803-0000-1000-8000-008 05f9b34fb")); | |
| 68 | |
| 69 mFakeScanner.mCallback.onScanResultWrapper( | |
| 70 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, | |
| 71 new FakeScanResult( | |
| 72 new FakeBluetoothDevice("FakeBluetoothDevice "), uuids)); | |
| 73 break; | |
| 74 } | |
| 75 case 3: { | |
| 76 ArrayList<ParcelUuid> uuids = new ArrayList<ParcelUuid>(0); | |
| 77 mFakeScanner.mCallback.onScanResultWrapper( | |
| 78 ScanSettings.CALLBACK_TYPE_ALL_MATCHES, | |
| 79 new FakeScanResult(new FakeBluetoothDevice(""), uuid s)); | |
| 80 break; | |
| 81 } | |
| 82 } | |
| 83 } | |
| 84 | |
| 44 // --------------------------------------------------------------------- -------------------- | 85 // --------------------------------------------------------------------- -------------------- |
| 45 // BluetoothAdapterWrapper overrides: | 86 // BluetoothAdapterWrapper overrides: |
| 46 | 87 |
| 47 @Override | 88 @Override |
| 48 public boolean isEnabled() { | 89 public boolean isEnabled() { |
| 49 return true; | 90 return true; |
| 50 } | 91 } |
| 51 | 92 |
| 52 @Override | 93 @Override |
| 53 public String getAddress() { | 94 public String getAddress() { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 } | 138 } |
| 98 mCallback = null; | 139 mCallback = null; |
| 99 } | 140 } |
| 100 } | 141 } |
| 101 | 142 |
| 102 /** | 143 /** |
| 103 * Fakes android.bluetooth.le.ScanResult | 144 * Fakes android.bluetooth.le.ScanResult |
| 104 */ | 145 */ |
| 105 static class FakeScanResult extends Wrappers.ScanResultWrapper { | 146 static class FakeScanResult extends Wrappers.ScanResultWrapper { |
| 106 private final FakeBluetoothDevice mDevice; | 147 private final FakeBluetoothDevice mDevice; |
| 148 private final ArrayList<ParcelUuid> mUuids; | |
| 107 | 149 |
| 108 FakeScanResult(FakeBluetoothDevice device) { | 150 FakeScanResult(FakeBluetoothDevice device, ArrayList<ParcelUuid> uuids) { |
| 109 super(null); | 151 super(null); |
| 110 mDevice = device; | 152 mDevice = device; |
| 153 mUuids = uuids; | |
| 111 } | 154 } |
| 112 | 155 |
| 113 @Override | 156 @Override |
| 114 public Wrappers.BluetoothDeviceWrapper getDevice() { | 157 public Wrappers.BluetoothDeviceWrapper getDevice() { |
| 115 return mDevice; | 158 return mDevice; |
| 116 } | 159 } |
| 160 | |
| 161 @Override | |
| 162 public List<ParcelUuid> getScanRecord_getServiceUuids() { | |
| 163 return mUuids; | |
| 164 } | |
| 117 } | 165 } |
| 118 | 166 |
| 119 /** | 167 /** |
| 120 * Fakes android.bluetooth.BluetoothDevice. | 168 * Fakes android.bluetooth.BluetoothDevice. |
| 121 */ | 169 */ |
| 122 static class FakeBluetoothDevice extends Wrappers.BluetoothDeviceWrapper { | 170 static class FakeBluetoothDevice extends Wrappers.BluetoothDeviceWrapper { |
| 123 private static final String ADDRESS = "A1:B2:C3:DD:DD:DD"; | 171 private static final String ADDRESS = "A1:B2:C3:DD:DD:DD"; |
| 124 private static final String NAME = "FakeBluetoothDevice"; | 172 private String mName; |
| 125 | 173 |
| 126 public FakeBluetoothDevice() { | 174 public FakeBluetoothDevice(String name) { |
| 127 super(null); | 175 super(null); |
| 176 mName = name; | |
| 128 } | 177 } |
| 129 | 178 |
| 130 @Override | 179 @Override |
| 131 public String getAddress() { | 180 public String getAddress() { |
| 132 return ADDRESS; | 181 return ADDRESS; |
|
Jeffrey Yasskin
2015/07/07 00:11:14
We're going to need to be able to return multiple
scheib
2015/07/08 00:49:10
Added multiple addresses and tests that use them.
| |
| 133 } | 182 } |
| 134 | 183 |
| 135 @Override | 184 @Override |
| 185 public int getBluetoothClass_getDeviceClass() { | |
| 186 return 0x1F00; // Unspecified Device Class | |
| 187 } | |
| 188 | |
| 189 @Override | |
| 190 public int getBondState() { | |
| 191 return BluetoothDevice.BOND_BONDED; | |
| 192 } | |
| 193 | |
| 194 @Override | |
| 136 public String getName() { | 195 public String getName() { |
| 137 return NAME; | 196 return mName; |
| 138 } | 197 } |
| 139 } | 198 } |
| 140 } | 199 } |
| OLD | NEW |