| 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; |
| 20 |
| 19 import org.chromium.base.Log; | 21 import org.chromium.base.Log; |
| 20 import org.chromium.base.annotations.CalledByNative; | 22 import org.chromium.base.annotations.CalledByNative; |
| 21 import org.chromium.base.annotations.JNINamespace; | 23 import org.chromium.base.annotations.JNINamespace; |
| 24 import org.chromium.components.location.LocationUtils; |
| 22 | 25 |
| 23 import java.util.ArrayList; | 26 import java.util.ArrayList; |
| 24 import java.util.HashMap; | 27 import java.util.HashMap; |
| 25 import java.util.List; | 28 import java.util.List; |
| 26 import java.util.UUID; | 29 import java.util.UUID; |
| 27 | 30 |
| 28 /** | 31 /** |
| 29 * Fake implementations of android.bluetooth.* classes for testing. | 32 * Fake implementations of android.bluetooth.* classes for testing. |
| 30 * | 33 * |
| 31 * Fakes are contained in a single file to simplify code. Only one C++ file may | 34 * Fakes are contained in a single file to simplify code. Only one C++ file may |
| 32 * access a Java file via JNI, and all of these classes are accessed by | 35 * access a Java file via JNI, and all of these classes are accessed by |
| 33 * bluetooth_test_android.cc. The alternative would be a C++ .h, .cc file for | 36 * bluetooth_test_android.cc. The alternative would be a C++ .h, .cc file for |
| 34 * each of these classes. | 37 * each of these classes. |
| 35 */ | 38 */ |
| 36 @JNINamespace("device") | 39 @JNINamespace("device") |
| 37 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 40 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 38 class Fakes { | 41 class Fakes { |
| 39 private static final String TAG = "cr.Bluetooth"; | 42 private static final String TAG = "cr.Bluetooth"; |
| 40 | 43 |
| 41 /** | 44 /** |
| 45 * Sets the factory for LocationUtils to return an instance whose |
| 46 * hasAndroidLocationPermission and isSystemLocationSettingEnabled return |
| 47 * values depend on |hasPermission| and |isEnabled| respectively. |
| 48 */ |
| 49 @CalledByNative |
| 50 public static void setLocationServicesState( |
| 51 final boolean hasPermission, final boolean isEnabled) { |
| 52 LocationUtils.setFactory(new LocationUtils.Factory() { |
| 53 @Override |
| 54 public LocationUtils create() { |
| 55 return new LocationUtils() { |
| 56 @Override |
| 57 public boolean hasAndroidLocationPermission(Context context)
{ |
| 58 return hasPermission; |
| 59 } |
| 60 |
| 61 @Override |
| 62 public boolean isSystemLocationSettingEnabled(Context contex
t) { |
| 63 return isEnabled; |
| 64 } |
| 65 }; |
| 66 } |
| 67 }); |
| 68 } |
| 69 |
| 70 /** |
| 42 * Fakes android.bluetooth.BluetoothAdapter. | 71 * Fakes android.bluetooth.BluetoothAdapter. |
| 43 */ | 72 */ |
| 44 static class FakeBluetoothAdapter extends Wrappers.BluetoothAdapterWrapper { | 73 static class FakeBluetoothAdapter extends Wrappers.BluetoothAdapterWrapper { |
| 45 private final FakeContext mFakeContext; | 74 private final FakeContext mFakeContext; |
| 46 private final FakeBluetoothLeScanner mFakeScanner; | 75 private final FakeBluetoothLeScanner mFakeScanner; |
| 47 private boolean mPowered = true; | 76 private boolean mPowered = true; |
| 48 final long mNativeBluetoothTestAndroid; | 77 final long mNativeBluetoothTestAndroid; |
| 49 | 78 |
| 50 /** | 79 /** |
| 51 * Creates a FakeBluetoothAdapter. | 80 * Creates a FakeBluetoothAdapter. |
| 52 */ | 81 */ |
| 53 @CalledByNative("FakeBluetoothAdapter") | 82 @CalledByNative("FakeBluetoothAdapter") |
| 54 public static FakeBluetoothAdapter create(long nativeBluetoothTestAndroi
d) { | 83 public static FakeBluetoothAdapter create(long nativeBluetoothTestAndroi
d) { |
| 55 Log.v(TAG, "FakeBluetoothAdapter created."); | 84 Log.v(TAG, "FakeBluetoothAdapter created."); |
| 56 return new FakeBluetoothAdapter(nativeBluetoothTestAndroid); | 85 return new FakeBluetoothAdapter(nativeBluetoothTestAndroid); |
| 57 } | 86 } |
| 58 | 87 |
| 59 private FakeBluetoothAdapter(long nativeBluetoothTestAndroid) { | 88 private FakeBluetoothAdapter(long nativeBluetoothTestAndroid) { |
| 60 super(null, new FakeContext()); | 89 super(null, new FakeContext()); |
| 61 mNativeBluetoothTestAndroid = nativeBluetoothTestAndroid; | 90 mNativeBluetoothTestAndroid = nativeBluetoothTestAndroid; |
| 62 mFakeContext = (FakeContext) mContext; | 91 mFakeContext = (FakeContext) mContext; |
| 63 mFakeScanner = new FakeBluetoothLeScanner(); | 92 mFakeScanner = new FakeBluetoothLeScanner(); |
| 64 } | 93 } |
| 65 | 94 |
| 66 @CalledByNative("FakeBluetoothAdapter") | |
| 67 public void denyPermission() { | |
| 68 mFakeContext.mHasLocation = false; | |
| 69 } | |
| 70 | |
| 71 /** | 95 /** |
| 72 * Creates and discovers a new device. | 96 * Creates and discovers a new device. |
| 73 */ | 97 */ |
| 74 @CalledByNative("FakeBluetoothAdapter") | 98 @CalledByNative("FakeBluetoothAdapter") |
| 75 public void simulateLowEnergyDevice(int deviceOrdinal) { | 99 public void simulateLowEnergyDevice(int deviceOrdinal) { |
| 76 if (mFakeScanner == null) { | 100 if (mFakeScanner == null) { |
| 77 return; | 101 return; |
| 78 } | 102 } |
| 79 | 103 |
| 80 switch (deviceOrdinal) { | 104 switch (deviceOrdinal) { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 return mPowered; | 205 return mPowered; |
| 182 } | 206 } |
| 183 | 207 |
| 184 @Override | 208 @Override |
| 185 public boolean isDiscovering() { | 209 public boolean isDiscovering() { |
| 186 return false; | 210 return false; |
| 187 } | 211 } |
| 188 } | 212 } |
| 189 | 213 |
| 190 /** | 214 /** |
| 191 * Fakes android.content.Context. | 215 * Fakes android.content.Context by extending MockContext. |
| 192 */ | 216 */ |
| 193 static class FakeContext extends Wrappers.ContextWrapper { | 217 static class FakeContext extends MockContext { |
| 194 public boolean mHasLocation = true; | |
| 195 | |
| 196 public FakeContext() { | 218 public FakeContext() { |
| 197 super(null); | 219 super(); |
| 198 } | 220 } |
| 199 | 221 |
| 200 @Override | 222 @Override |
| 201 public boolean hasAndroidLocationPermission() { | |
| 202 return mHasLocation; | |
| 203 } | |
| 204 | |
| 205 @Override | |
| 206 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter
filter) { | 223 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter
filter) { |
| 207 return null; | 224 return null; |
| 208 } | 225 } |
| 209 | 226 |
| 210 @Override | 227 @Override |
| 211 public void unregisterReceiver(BroadcastReceiver receiver) {} | 228 public void unregisterReceiver(BroadcastReceiver receiver) {} |
| 212 } | 229 } |
| 213 | 230 |
| 214 /** | 231 /** |
| 215 * Fakes android.bluetooth.le.BluetoothLeScanner. | 232 * Fakes android.bluetooth.le.BluetoothLeScanner. |
| (...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 long nativeBluetoothTestAndroid, byte[] value); | 828 long nativeBluetoothTestAndroid, byte[] value); |
| 812 | 829 |
| 813 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadDescriptor. | 830 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadDescriptor. |
| 814 private static native void nativeOnFakeBluetoothGattReadDescriptor( | 831 private static native void nativeOnFakeBluetoothGattReadDescriptor( |
| 815 long nativeBluetoothTestAndroid); | 832 long nativeBluetoothTestAndroid); |
| 816 | 833 |
| 817 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteDescriptor. | 834 // Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteDescriptor. |
| 818 private static native void nativeOnFakeBluetoothGattWriteDescriptor( | 835 private static native void nativeOnFakeBluetoothGattWriteDescriptor( |
| 819 long nativeBluetoothTestAndroid, byte[] value); | 836 long nativeBluetoothTestAndroid, byte[] value); |
| 820 } | 837 } |
| OLD | NEW |