Loading android/app/src/com/android/bluetooth/gatt/GattService.java +36 −64 Original line number Diff line number Diff line Loading @@ -24,6 +24,8 @@ import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREG import static com.android.bluetooth.Utils.callerIsSystemOrActiveOrManagedUser; import static com.android.bluetooth.Utils.checkCallerTargetSdk; import static java.util.Objects.requireNonNull; import android.annotation.RequiresPermission; import android.annotation.SuppressLint; import android.app.ActivityManager; Loading Loading @@ -55,7 +57,6 @@ import android.bluetooth.le.ScanFilter; import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanSettings; import android.content.AttributionSource; import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.PackageInfoFlags; Loading Loading @@ -149,22 +150,19 @@ public class GattService extends ProfileService { @VisibleForTesting static final int GATT_CLIENT_LIMIT_PER_APP = 32; public final TransitionalScanHelper mTransitionalScanHelper = new TransitionalScanHelper(this, this::isTestModeEnabled); public final TransitionalScanHelper mTransitionalScanHelper; /** This is only used when Flags.scanManagerRefactor() is true. */ private static GattService sGattService; /** List of our registered clients. */ ContextMap<IBluetoothGattCallback> mClientMap = new ContextMap<>(); @VisibleForTesting ContextMap<IBluetoothGattCallback> mClientMap = new ContextMap<>(); /** List of our registered server apps. */ ContextMap<IBluetoothGattServerCallback> mServerMap = new ContextMap<>(); @VisibleForTesting ContextMap<IBluetoothGattServerCallback> mServerMap = new ContextMap<>(); /** Server handle map. */ HandleMap mHandleMap = new HandleMap(); private List<UUID> mAdvertisingServiceUuids = new ArrayList<>(); private final HandleMap mHandleMap = new HandleMap(); /** * Set of restricted (which require a BLUETOOTH_PRIVILEGED permission) handles per connectionId. Loading @@ -177,46 +175,31 @@ public class GattService extends ProfileService { */ private final HashMap<String, Integer> mPermits = new HashMap<>(); private AdapterService mAdapterService; AdvertiseManager mAdvertiseManager; DistanceMeasurementManager mDistanceMeasurementManager; private Handler mTestModeHandler; private ActivityManager mActivityManager; private PackageManager mPackageManager; private final Object mTestModeLock = new Object(); public GattService(Context ctx) { super(ctx); } public static boolean isEnabled() { return BluetoothProperties.isProfileGattEnabled().orElse(true); } /** Reliable write queue */ @VisibleForTesting Set<String> mReliableQueue = new HashSet<>(); private GattNativeInterface mNativeInterface; private final AdapterService mAdapterService; private final AdvertiseManager mAdvertiseManager; private final GattNativeInterface mNativeInterface; private final DistanceMeasurementManager mDistanceMeasurementManager; private final ActivityManager mActivityManager; private final PackageManager mPackageManager; @Override protected IProfileServiceBinder initBinder() { return new BluetoothGattBinder(this); } private Handler mTestModeHandler; @Override public void start() { Log.d(TAG, "start()"); public GattService(AdapterService adapterService) { super(requireNonNull(adapterService)); mAdapterService = adapterService; mActivityManager = requireNonNull(getSystemService(ActivityManager.class)); mPackageManager = requireNonNull(mAdapterService.getPackageManager()); if (Flags.scanManagerRefactor() && sGattService != null) { throw new IllegalStateException("start() called twice"); } mTransitionalScanHelper = new TransitionalScanHelper(adapterService, this::isTestModeEnabled); Settings.Global.putInt( getContentResolver(), "bluetooth_sanitized_exposure_notification_supported", 1); mNativeInterface = GattObjectsFactory.getInstance().getNativeInterface(); mNativeInterface.init(this); mAdapterService = AdapterService.getAdapterService(); mAdvertiseManager = new AdvertiseManager(this); if (!Flags.scanManagerRefactor()) { Loading @@ -227,14 +210,23 @@ public class GattService extends ProfileService { mDistanceMeasurementManager = GattObjectsFactory.getInstance().createDistanceMeasurementManager(mAdapterService); mActivityManager = getSystemService(ActivityManager.class); mPackageManager = mAdapterService.getPackageManager(); if (Flags.scanManagerRefactor()) { setGattService(this); } } public static boolean isEnabled() { return BluetoothProperties.isProfileGattEnabled().orElse(true); } /** Reliable write queue */ @VisibleForTesting Set<String> mReliableQueue = new HashSet<>(); @Override protected IProfileServiceBinder initBinder() { return new BluetoothGattBinder(this); } @Override public void stop() { Log.d(TAG, "stop()"); Loading @@ -261,16 +253,9 @@ public class GattService extends ProfileService { @Override public void cleanup() { Log.d(TAG, "cleanup()"); if (mNativeInterface != null) { mNativeInterface.cleanup(); mNativeInterface = null; } if (mAdvertiseManager != null) { mAdvertiseManager.cleanup(); } if (mDistanceMeasurementManager != null) { mDistanceMeasurementManager.cleanup(); } if (!Flags.scanManagerRefactor()) { mTransitionalScanHelper.cleanup(); } Loading Loading @@ -2729,7 +2714,7 @@ public class GattService extends ProfileService { // Link supervision timeout is measured in N * 10ms int timeout = 500; // 5s CompanionManager manager = AdapterService.getAdapterService().getCompanionManager(); CompanionManager manager = mAdapterService.getCompanionManager(); minInterval = manager.getGattConnParameters( Loading Loading @@ -3607,10 +3592,6 @@ public class GattService extends ProfileService { } private void logClientForegroundInfo(int uid, boolean isDirect) { if (mPackageManager == null) { return; } String packageName = mPackageManager.getPackagesForUid(uid)[0]; int importance = mActivityManager.getPackageImportance(packageName); if (importance == IMPORTANCE_FOREGROUND_SERVICE) { Loading @@ -3635,10 +3616,6 @@ public class GattService extends ProfileService { } private void logServerForegroundInfo(int uid, boolean isDirect) { if (mPackageManager == null) { return; } String packageName = mPackageManager.getPackagesForUid(uid)[0]; int importance = mActivityManager.getPackageImportance(packageName); if (importance == IMPORTANCE_FOREGROUND_SERVICE) { Loading Loading @@ -3733,11 +3710,6 @@ public class GattService extends ProfileService { @Override public void dump(StringBuilder sb) { super.dump(sb); println(sb, "mAdvertisingServiceUuids:"); for (UUID uuid : mAdvertisingServiceUuids) { println(sb, " " + uuid); } sb.append("\nRegistered App\n"); dumpRegisterId(sb); Loading android/app/src/com/android/bluetooth/le_scan/ScanController.java +8 −4 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package com.android.bluetooth.le_scan; import static java.util.Objects.requireNonNull; import android.app.PendingIntent; import android.bluetooth.BluetoothDevice; import android.bluetooth.IBluetoothScan; Loading @@ -25,7 +27,6 @@ import android.bluetooth.le.ScanFilter; import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanSettings; import android.content.AttributionSource; import android.content.Context; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; Loading @@ -35,6 +36,8 @@ import android.os.WorkSource; import android.text.format.DateUtils; import android.util.Log; import com.android.bluetooth.btservice.AdapterService; import libcore.util.HexEncoding; import java.util.List; Loading Loading @@ -63,9 +66,10 @@ public class ScanController { "0201061AFF4C000215426C7565436861726D426561636F6E730EFE1355C509168020691E0EFE13551109426C7565436861726D5F31363936383500000000", }; public ScanController(Context ctx) { mTransitionalScanHelper = new TransitionalScanHelper(ctx, () -> mTestModeEnabled); mMainLooper = ctx.getMainLooper(); public ScanController(AdapterService adapterService) { mTransitionalScanHelper = new TransitionalScanHelper(requireNonNull(adapterService), () -> mTestModeEnabled); mMainLooper = adapterService.getMainLooper(); mBinder = new BluetoothScanBinder(this); mIsAvailable = true; HandlerThread thread = new HandlerThread("BluetoothScanManager"); Loading android/app/src/com/android/bluetooth/le_scan/TransitionalScanHelper.java +75 −70 File changed.Preview size limit exceeded, changes collapsed. Show changes android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java +3 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,7 @@ import static com.google.common.truth.Truth.assertWithMessage; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import android.app.ActivityManager; import android.app.AlarmManager; import android.app.AppOpsManager; import android.app.admin.DevicePolicyManager; Loading Loading @@ -275,6 +276,8 @@ public class AdapterServiceTest { mockGetSystemService(Context.ALARM_SERVICE, AlarmManager.class); mockGetSystemService(Context.APP_OPS_SERVICE, AppOpsManager.class); mockGetSystemService(Context.AUDIO_SERVICE, AudioManager.class); mockGetSystemService(Context.ACTIVITY_SERVICE, ActivityManager.class); DevicePolicyManager dpm = mockGetSystemService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class); doReturn(false).when(dpm).isCommonCriteriaModeEnabled(any()); Loading android/app/tests/unit/src/com/android/bluetooth/gatt/GattServiceTest.java +37 −38 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.*; import static org.mockito.Mockito.verify; import android.app.ActivityManager; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; Loading @@ -39,12 +40,15 @@ import android.content.AttributionSource; import android.content.Context; import android.content.res.Resources; import android.location.LocationManager; import android.os.Bundle; import android.platform.test.annotations.DisableFlags; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; import android.test.mock.MockContentProvider; import android.test.mock.MockContentResolver; import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; import androidx.test.rule.ServiceTestRule; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.TestUtils; Loading Loading @@ -76,41 +80,49 @@ import java.util.UUID; @SmallTest @RunWith(AndroidJUnit4.class) public class GattServiceTest { private static final String REMOTE_DEVICE_ADDRESS = "00:00:00:00:00:00"; private static final int TIMES_UP_AND_DOWN = 3; private GattService mService; @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); @Mock private ContextMap<IBluetoothGattCallback> mClientMap; @Mock private ScannerMap mScannerMap; @Mock private ScanManager mScanManager; @Mock private Set<String> mReliableQueue; @Mock private ContextMap<IBluetoothGattServerCallback> mServerMap; @Mock private DistanceMeasurementManager mDistanceMeasurementManager; @Mock private AdvertiseManagerNativeInterface mAdvertiseManagerNativeInterface; @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); private BluetoothAdapter mAdapter; private AttributionSource mAttributionSource; @Mock private Resources mResources; @Mock private AdapterService mAdapterService; @Mock private GattObjectsFactory mGattObjectsFactory; @Mock private ScanObjectsFactory mScanObjectsFactory; @Mock private GattNativeInterface mNativeInterface; private static final String REMOTE_DEVICE_ADDRESS = "00:00:00:00:00:00"; private static final int TIMES_UP_AND_DOWN = 3; private final BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter(); private final AttributionSource mAttributionSource = mAdapter.getAttributionSource(); private final Context mContext = InstrumentationRegistry.getTargetContext(); private MockContentResolver mMockContentResolver; private GattService mService; private CompanionManager mBtCompanionManager; @Before public void setUp() throws Exception { TestUtils.setAdapterService(mAdapterService); mMockContentResolver = new MockContentResolver(mContext); mMockContentResolver.addProvider( Settings.AUTHORITY, new MockContentProvider() { @Override public Bundle call(String method, String request, Bundle args) { return Bundle.EMPTY; } }); GattObjectsFactory.setInstanceForTesting(mGattObjectsFactory); ScanObjectsFactory.setInstanceForTesting(mScanObjectsFactory); doReturn(mNativeInterface).when(mGattObjectsFactory).getNativeInterface(); doReturn(mDistanceMeasurementManager) .when(mGattObjectsFactory) Loading @@ -118,27 +130,23 @@ public class GattServiceTest { doReturn(mScanManager) .when(mScanObjectsFactory) .createScanManager(any(), any(), any(), any(), any()); mAdapter = BluetoothAdapter.getDefaultAdapter(); mAttributionSource = mAdapter.getAttributionSource(); when(mAdapterService.getResources()).thenReturn(mResources); when(mResources.getInteger(anyInt())).thenReturn(0); when(mAdapterService.getSharedPreferences(anyString(), anyInt())) .thenReturn( InstrumentationRegistry.getTargetContext() .getSharedPreferences( "GattServiceTestPrefs", Context.MODE_PRIVATE)); doReturn(mContext.getPackageManager()).when(mAdapterService).getPackageManager(); doReturn(mContext.getSharedPreferences("GattServiceTestPrefs", Context.MODE_PRIVATE)) .when(mAdapterService) .getSharedPreferences(anyString(), anyInt()); doReturn(mResources).when(mAdapterService).getResources(); doReturn(mMockContentResolver).when(mAdapterService).getContentResolver(); TestUtils.mockGetSystemService( mAdapterService, Context.LOCATION_SERVICE, LocationManager.class); TestUtils.mockGetSystemService( mAdapterService, Context.ACTIVITY_SERVICE, ActivityManager.class); mBtCompanionManager = new CompanionManager(mAdapterService, null); doReturn(mBtCompanionManager).when(mAdapterService).getCompanionManager(); AdvertiseManagerNativeInterface.setInstance(mAdvertiseManagerNativeInterface); mService = new GattService(InstrumentationRegistry.getTargetContext()); mService.start(); mService = new GattService(mAdapterService); mService.mClientMap = mClientMap; mService.mTransitionalScanHelper.setScannerMap(mScannerMap); Loading @@ -149,10 +157,8 @@ public class GattServiceTest { @After public void tearDown() throws Exception { mService.stop(); mService = null; AdvertiseManagerNativeInterface.setInstance(null); TestUtils.clearAdapterService(mAdapterService); GattObjectsFactory.setInstanceForTesting(null); ScanObjectsFactory.setInstanceForTesting(null); } Loading @@ -161,14 +167,7 @@ public class GattServiceTest { public void testServiceUpAndDown() throws Exception { for (int i = 0; i < TIMES_UP_AND_DOWN; i++) { mService.stop(); mService = null; TestUtils.clearAdapterService(mAdapterService); reset(mAdapterService); TestUtils.setAdapterService(mAdapterService); mService = new GattService(InstrumentationRegistry.getTargetContext()); mService.start(); mService = new GattService(mAdapterService); } } Loading Loading
android/app/src/com/android/bluetooth/gatt/GattService.java +36 −64 Original line number Diff line number Diff line Loading @@ -24,6 +24,8 @@ import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREG import static com.android.bluetooth.Utils.callerIsSystemOrActiveOrManagedUser; import static com.android.bluetooth.Utils.checkCallerTargetSdk; import static java.util.Objects.requireNonNull; import android.annotation.RequiresPermission; import android.annotation.SuppressLint; import android.app.ActivityManager; Loading Loading @@ -55,7 +57,6 @@ import android.bluetooth.le.ScanFilter; import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanSettings; import android.content.AttributionSource; import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.PackageInfoFlags; Loading Loading @@ -149,22 +150,19 @@ public class GattService extends ProfileService { @VisibleForTesting static final int GATT_CLIENT_LIMIT_PER_APP = 32; public final TransitionalScanHelper mTransitionalScanHelper = new TransitionalScanHelper(this, this::isTestModeEnabled); public final TransitionalScanHelper mTransitionalScanHelper; /** This is only used when Flags.scanManagerRefactor() is true. */ private static GattService sGattService; /** List of our registered clients. */ ContextMap<IBluetoothGattCallback> mClientMap = new ContextMap<>(); @VisibleForTesting ContextMap<IBluetoothGattCallback> mClientMap = new ContextMap<>(); /** List of our registered server apps. */ ContextMap<IBluetoothGattServerCallback> mServerMap = new ContextMap<>(); @VisibleForTesting ContextMap<IBluetoothGattServerCallback> mServerMap = new ContextMap<>(); /** Server handle map. */ HandleMap mHandleMap = new HandleMap(); private List<UUID> mAdvertisingServiceUuids = new ArrayList<>(); private final HandleMap mHandleMap = new HandleMap(); /** * Set of restricted (which require a BLUETOOTH_PRIVILEGED permission) handles per connectionId. Loading @@ -177,46 +175,31 @@ public class GattService extends ProfileService { */ private final HashMap<String, Integer> mPermits = new HashMap<>(); private AdapterService mAdapterService; AdvertiseManager mAdvertiseManager; DistanceMeasurementManager mDistanceMeasurementManager; private Handler mTestModeHandler; private ActivityManager mActivityManager; private PackageManager mPackageManager; private final Object mTestModeLock = new Object(); public GattService(Context ctx) { super(ctx); } public static boolean isEnabled() { return BluetoothProperties.isProfileGattEnabled().orElse(true); } /** Reliable write queue */ @VisibleForTesting Set<String> mReliableQueue = new HashSet<>(); private GattNativeInterface mNativeInterface; private final AdapterService mAdapterService; private final AdvertiseManager mAdvertiseManager; private final GattNativeInterface mNativeInterface; private final DistanceMeasurementManager mDistanceMeasurementManager; private final ActivityManager mActivityManager; private final PackageManager mPackageManager; @Override protected IProfileServiceBinder initBinder() { return new BluetoothGattBinder(this); } private Handler mTestModeHandler; @Override public void start() { Log.d(TAG, "start()"); public GattService(AdapterService adapterService) { super(requireNonNull(adapterService)); mAdapterService = adapterService; mActivityManager = requireNonNull(getSystemService(ActivityManager.class)); mPackageManager = requireNonNull(mAdapterService.getPackageManager()); if (Flags.scanManagerRefactor() && sGattService != null) { throw new IllegalStateException("start() called twice"); } mTransitionalScanHelper = new TransitionalScanHelper(adapterService, this::isTestModeEnabled); Settings.Global.putInt( getContentResolver(), "bluetooth_sanitized_exposure_notification_supported", 1); mNativeInterface = GattObjectsFactory.getInstance().getNativeInterface(); mNativeInterface.init(this); mAdapterService = AdapterService.getAdapterService(); mAdvertiseManager = new AdvertiseManager(this); if (!Flags.scanManagerRefactor()) { Loading @@ -227,14 +210,23 @@ public class GattService extends ProfileService { mDistanceMeasurementManager = GattObjectsFactory.getInstance().createDistanceMeasurementManager(mAdapterService); mActivityManager = getSystemService(ActivityManager.class); mPackageManager = mAdapterService.getPackageManager(); if (Flags.scanManagerRefactor()) { setGattService(this); } } public static boolean isEnabled() { return BluetoothProperties.isProfileGattEnabled().orElse(true); } /** Reliable write queue */ @VisibleForTesting Set<String> mReliableQueue = new HashSet<>(); @Override protected IProfileServiceBinder initBinder() { return new BluetoothGattBinder(this); } @Override public void stop() { Log.d(TAG, "stop()"); Loading @@ -261,16 +253,9 @@ public class GattService extends ProfileService { @Override public void cleanup() { Log.d(TAG, "cleanup()"); if (mNativeInterface != null) { mNativeInterface.cleanup(); mNativeInterface = null; } if (mAdvertiseManager != null) { mAdvertiseManager.cleanup(); } if (mDistanceMeasurementManager != null) { mDistanceMeasurementManager.cleanup(); } if (!Flags.scanManagerRefactor()) { mTransitionalScanHelper.cleanup(); } Loading Loading @@ -2729,7 +2714,7 @@ public class GattService extends ProfileService { // Link supervision timeout is measured in N * 10ms int timeout = 500; // 5s CompanionManager manager = AdapterService.getAdapterService().getCompanionManager(); CompanionManager manager = mAdapterService.getCompanionManager(); minInterval = manager.getGattConnParameters( Loading Loading @@ -3607,10 +3592,6 @@ public class GattService extends ProfileService { } private void logClientForegroundInfo(int uid, boolean isDirect) { if (mPackageManager == null) { return; } String packageName = mPackageManager.getPackagesForUid(uid)[0]; int importance = mActivityManager.getPackageImportance(packageName); if (importance == IMPORTANCE_FOREGROUND_SERVICE) { Loading @@ -3635,10 +3616,6 @@ public class GattService extends ProfileService { } private void logServerForegroundInfo(int uid, boolean isDirect) { if (mPackageManager == null) { return; } String packageName = mPackageManager.getPackagesForUid(uid)[0]; int importance = mActivityManager.getPackageImportance(packageName); if (importance == IMPORTANCE_FOREGROUND_SERVICE) { Loading Loading @@ -3733,11 +3710,6 @@ public class GattService extends ProfileService { @Override public void dump(StringBuilder sb) { super.dump(sb); println(sb, "mAdvertisingServiceUuids:"); for (UUID uuid : mAdvertisingServiceUuids) { println(sb, " " + uuid); } sb.append("\nRegistered App\n"); dumpRegisterId(sb); Loading
android/app/src/com/android/bluetooth/le_scan/ScanController.java +8 −4 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package com.android.bluetooth.le_scan; import static java.util.Objects.requireNonNull; import android.app.PendingIntent; import android.bluetooth.BluetoothDevice; import android.bluetooth.IBluetoothScan; Loading @@ -25,7 +27,6 @@ import android.bluetooth.le.ScanFilter; import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanSettings; import android.content.AttributionSource; import android.content.Context; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; Loading @@ -35,6 +36,8 @@ import android.os.WorkSource; import android.text.format.DateUtils; import android.util.Log; import com.android.bluetooth.btservice.AdapterService; import libcore.util.HexEncoding; import java.util.List; Loading Loading @@ -63,9 +66,10 @@ public class ScanController { "0201061AFF4C000215426C7565436861726D426561636F6E730EFE1355C509168020691E0EFE13551109426C7565436861726D5F31363936383500000000", }; public ScanController(Context ctx) { mTransitionalScanHelper = new TransitionalScanHelper(ctx, () -> mTestModeEnabled); mMainLooper = ctx.getMainLooper(); public ScanController(AdapterService adapterService) { mTransitionalScanHelper = new TransitionalScanHelper(requireNonNull(adapterService), () -> mTestModeEnabled); mMainLooper = adapterService.getMainLooper(); mBinder = new BluetoothScanBinder(this); mIsAvailable = true; HandlerThread thread = new HandlerThread("BluetoothScanManager"); Loading
android/app/src/com/android/bluetooth/le_scan/TransitionalScanHelper.java +75 −70 File changed.Preview size limit exceeded, changes collapsed. Show changes
android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java +3 −0 Original line number Diff line number Diff line Loading @@ -30,6 +30,7 @@ import static com.google.common.truth.Truth.assertWithMessage; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import android.app.ActivityManager; import android.app.AlarmManager; import android.app.AppOpsManager; import android.app.admin.DevicePolicyManager; Loading Loading @@ -275,6 +276,8 @@ public class AdapterServiceTest { mockGetSystemService(Context.ALARM_SERVICE, AlarmManager.class); mockGetSystemService(Context.APP_OPS_SERVICE, AppOpsManager.class); mockGetSystemService(Context.AUDIO_SERVICE, AudioManager.class); mockGetSystemService(Context.ACTIVITY_SERVICE, ActivityManager.class); DevicePolicyManager dpm = mockGetSystemService(Context.DEVICE_POLICY_SERVICE, DevicePolicyManager.class); doReturn(false).when(dpm).isCommonCriteriaModeEnabled(any()); Loading
android/app/tests/unit/src/com/android/bluetooth/gatt/GattServiceTest.java +37 −38 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.*; import static org.mockito.Mockito.verify; import android.app.ActivityManager; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; Loading @@ -39,12 +40,15 @@ import android.content.AttributionSource; import android.content.Context; import android.content.res.Resources; import android.location.LocationManager; import android.os.Bundle; import android.platform.test.annotations.DisableFlags; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; import android.test.mock.MockContentProvider; import android.test.mock.MockContentResolver; import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; import androidx.test.rule.ServiceTestRule; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.TestUtils; Loading Loading @@ -76,41 +80,49 @@ import java.util.UUID; @SmallTest @RunWith(AndroidJUnit4.class) public class GattServiceTest { private static final String REMOTE_DEVICE_ADDRESS = "00:00:00:00:00:00"; private static final int TIMES_UP_AND_DOWN = 3; private GattService mService; @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); @Mock private ContextMap<IBluetoothGattCallback> mClientMap; @Mock private ScannerMap mScannerMap; @Mock private ScanManager mScanManager; @Mock private Set<String> mReliableQueue; @Mock private ContextMap<IBluetoothGattServerCallback> mServerMap; @Mock private DistanceMeasurementManager mDistanceMeasurementManager; @Mock private AdvertiseManagerNativeInterface mAdvertiseManagerNativeInterface; @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); private BluetoothAdapter mAdapter; private AttributionSource mAttributionSource; @Mock private Resources mResources; @Mock private AdapterService mAdapterService; @Mock private GattObjectsFactory mGattObjectsFactory; @Mock private ScanObjectsFactory mScanObjectsFactory; @Mock private GattNativeInterface mNativeInterface; private static final String REMOTE_DEVICE_ADDRESS = "00:00:00:00:00:00"; private static final int TIMES_UP_AND_DOWN = 3; private final BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter(); private final AttributionSource mAttributionSource = mAdapter.getAttributionSource(); private final Context mContext = InstrumentationRegistry.getTargetContext(); private MockContentResolver mMockContentResolver; private GattService mService; private CompanionManager mBtCompanionManager; @Before public void setUp() throws Exception { TestUtils.setAdapterService(mAdapterService); mMockContentResolver = new MockContentResolver(mContext); mMockContentResolver.addProvider( Settings.AUTHORITY, new MockContentProvider() { @Override public Bundle call(String method, String request, Bundle args) { return Bundle.EMPTY; } }); GattObjectsFactory.setInstanceForTesting(mGattObjectsFactory); ScanObjectsFactory.setInstanceForTesting(mScanObjectsFactory); doReturn(mNativeInterface).when(mGattObjectsFactory).getNativeInterface(); doReturn(mDistanceMeasurementManager) .when(mGattObjectsFactory) Loading @@ -118,27 +130,23 @@ public class GattServiceTest { doReturn(mScanManager) .when(mScanObjectsFactory) .createScanManager(any(), any(), any(), any(), any()); mAdapter = BluetoothAdapter.getDefaultAdapter(); mAttributionSource = mAdapter.getAttributionSource(); when(mAdapterService.getResources()).thenReturn(mResources); when(mResources.getInteger(anyInt())).thenReturn(0); when(mAdapterService.getSharedPreferences(anyString(), anyInt())) .thenReturn( InstrumentationRegistry.getTargetContext() .getSharedPreferences( "GattServiceTestPrefs", Context.MODE_PRIVATE)); doReturn(mContext.getPackageManager()).when(mAdapterService).getPackageManager(); doReturn(mContext.getSharedPreferences("GattServiceTestPrefs", Context.MODE_PRIVATE)) .when(mAdapterService) .getSharedPreferences(anyString(), anyInt()); doReturn(mResources).when(mAdapterService).getResources(); doReturn(mMockContentResolver).when(mAdapterService).getContentResolver(); TestUtils.mockGetSystemService( mAdapterService, Context.LOCATION_SERVICE, LocationManager.class); TestUtils.mockGetSystemService( mAdapterService, Context.ACTIVITY_SERVICE, ActivityManager.class); mBtCompanionManager = new CompanionManager(mAdapterService, null); doReturn(mBtCompanionManager).when(mAdapterService).getCompanionManager(); AdvertiseManagerNativeInterface.setInstance(mAdvertiseManagerNativeInterface); mService = new GattService(InstrumentationRegistry.getTargetContext()); mService.start(); mService = new GattService(mAdapterService); mService.mClientMap = mClientMap; mService.mTransitionalScanHelper.setScannerMap(mScannerMap); Loading @@ -149,10 +157,8 @@ public class GattServiceTest { @After public void tearDown() throws Exception { mService.stop(); mService = null; AdvertiseManagerNativeInterface.setInstance(null); TestUtils.clearAdapterService(mAdapterService); GattObjectsFactory.setInstanceForTesting(null); ScanObjectsFactory.setInstanceForTesting(null); } Loading @@ -161,14 +167,7 @@ public class GattServiceTest { public void testServiceUpAndDown() throws Exception { for (int i = 0; i < TIMES_UP_AND_DOWN; i++) { mService.stop(); mService = null; TestUtils.clearAdapterService(mAdapterService); reset(mAdapterService); TestUtils.setAdapterService(mAdapterService); mService = new GattService(InstrumentationRegistry.getTargetContext()); mService.start(); mService = new GattService(mAdapterService); } } Loading