Loading android/app/src/com/android/bluetooth/gatt/GattObjectsFactory.java +0 −34 Original line number Diff line number Diff line Loading @@ -16,17 +16,10 @@ package com.android.bluetooth.gatt; import android.content.Context; import android.os.Looper; import android.util.Log; import com.android.bluetooth.Utils; import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.BluetoothAdapterProxy; import com.android.bluetooth.le_scan.PeriodicScanManager; import com.android.bluetooth.le_scan.ScanManager; import com.android.bluetooth.le_scan.ScanNativeInterface; import com.android.bluetooth.le_scan.TransitionalScanHelper; /** Factory class for object initialization to help with unit testing */ public class GattObjectsFactory { Loading Loading @@ -67,33 +60,6 @@ public class GattObjectsFactory { return GattNativeInterface.getInstance(); } public ScanNativeInterface getScanNativeInterface() { return ScanNativeInterface.getInstance(); } /** * Create an instance of ScanManager * * @param context a Context instance * @param scanHelper a TransitionalScanHelper instance * @param adapterService an AdapterService instance * @param bluetoothAdapterProxy a bluetoothAdapterProxy instance * @param looper the looper to be used for processing messages * @return the created ScanManager instance */ public ScanManager createScanManager( Context context, TransitionalScanHelper scanHelper, AdapterService adapterService, BluetoothAdapterProxy bluetoothAdapterProxy, Looper looper) { return new ScanManager(context, scanHelper, adapterService, bluetoothAdapterProxy, looper); } public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) { return new PeriodicScanManager(adapterService); } public DistanceMeasurementManager createDistanceMeasurementManager( AdapterService adapterService) { return new DistanceMeasurementManager(adapterService); Loading android/app/src/com/android/bluetooth/le_scan/ScanManager.java +1 −2 Original line number Diff line number Diff line Loading @@ -49,7 +49,6 @@ import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.BluetoothAdapterProxy; import com.android.bluetooth.flags.Flags; import com.android.bluetooth.gatt.FilterParams; import com.android.bluetooth.gatt.GattObjectsFactory; import com.android.bluetooth.gatt.GattServiceConfig; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; Loading Loading @@ -982,7 +981,7 @@ public class ScanManager { private ScanNativeInterface mNativeInterface; ScanNative(TransitionalScanHelper scanHelper) { mNativeInterface = GattObjectsFactory.getInstance().getScanNativeInterface(); mNativeInterface = ScanObjectsFactory.getInstance().getScanNativeInterface(); mNativeInterface.init(scanHelper); mFilterIndexStack = new ArrayDeque<Integer>(); mClientFilterIndexMap = new HashMap<Integer, Deque<Integer>>(); Loading android/app/src/com/android/bluetooth/le_scan/ScanObjectsFactory.java 0 → 100644 +88 −0 Original line number Diff line number Diff line /* * Copyright 2024 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.bluetooth.le_scan; import android.content.Context; import android.os.Looper; import android.util.Log; import com.android.bluetooth.Utils; import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.BluetoothAdapterProxy; /** Factory class for object initialization to help with unit testing */ public class ScanObjectsFactory { private static final String TAG = ScanObjectsFactory.class.getSimpleName(); private static ScanObjectsFactory sInstance; private static final Object INSTANCE_LOCK = new Object(); private ScanObjectsFactory() {} /** * Get the singleton instance of object factory * * @return the singleton instance, guaranteed not null */ public static ScanObjectsFactory getInstance() { synchronized (INSTANCE_LOCK) { if (sInstance == null) { sInstance = new ScanObjectsFactory(); } } return sInstance; } /** * Allow unit tests to substitute ScanObjectsFactory with a test instance * * @param objectsFactory a test instance of the ScanObjectsFactory */ public static void setInstanceForTesting(ScanObjectsFactory objectsFactory) { Utils.enforceInstrumentationTestMode(); synchronized (INSTANCE_LOCK) { Log.d(TAG, "setInstanceForTesting(), set to " + objectsFactory); sInstance = objectsFactory; } } public ScanNativeInterface getScanNativeInterface() { return ScanNativeInterface.getInstance(); } /** * Create an instance of ScanManager * * @param context a Context instance * @param scanHelper a TransitionalScanHelper instance * @param adapterService an AdapterService instance * @param bluetoothAdapterProxy a bluetoothAdapterProxy instance * @param looper the looper to be used for processing messages * @return the created ScanManager instance */ public ScanManager createScanManager( Context context, TransitionalScanHelper scanHelper, AdapterService adapterService, BluetoothAdapterProxy bluetoothAdapterProxy, Looper looper) { return new ScanManager(context, scanHelper, adapterService, bluetoothAdapterProxy, looper); } public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) { return new PeriodicScanManager(adapterService); } } android/app/src/com/android/bluetooth/le_scan/TransitionalScanHelper.java +2 −3 Original line number Diff line number Diff line Loading @@ -57,7 +57,6 @@ import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.BluetoothAdapterProxy; import com.android.bluetooth.flags.Flags; import com.android.bluetooth.gatt.ContextMap; import com.android.bluetooth.gatt.GattObjectsFactory; import com.android.bluetooth.gatt.GattServiceConfig; import com.android.bluetooth.util.NumberUtils; import com.android.internal.annotations.VisibleForTesting; Loading Loading @@ -197,7 +196,7 @@ public class TransitionalScanHelper { mCompanionManager = mContext.getSystemService(CompanionDeviceManager.class); mAdapterService = AdapterService.getAdapterService(); mScanManager = GattObjectsFactory.getInstance() ScanObjectsFactory.getInstance() .createScanManager( mContext, this, Loading @@ -206,7 +205,7 @@ public class TransitionalScanHelper { looper); mPeriodicScanManager = GattObjectsFactory.getInstance().createPeriodicScanManager(mAdapterService); ScanObjectsFactory.getInstance().createPeriodicScanManager(mAdapterService); } /** Stops the scanning component. */ Loading android/app/tests/unit/src/com/android/bluetooth/gatt/GattServiceTest.java +11 −5 Original line number Diff line number Diff line Loading @@ -50,6 +50,7 @@ import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.CompanionManager; import com.android.bluetooth.flags.Flags; import com.android.bluetooth.le_scan.ScanManager; import com.android.bluetooth.le_scan.ScanObjectsFactory; import com.android.bluetooth.le_scan.TransitionalScanHelper; import org.junit.After; Loading Loading @@ -104,7 +105,8 @@ public class GattServiceTest { @Mock private Resources mResources; @Mock private AdapterService mAdapterService; @Mock private GattObjectsFactory mFactory; @Mock private GattObjectsFactory mGattObjectsFactory; @Mock private ScanObjectsFactory mScanObjectsFactory; @Mock private GattNativeInterface mNativeInterface; private BluetoothDevice mCurrentDevice; private CompanionManager mBtCompanionManager; Loading @@ -115,12 +117,15 @@ public class GattServiceTest { TestUtils.setAdapterService(mAdapterService); GattObjectsFactory.setInstanceForTesting(mFactory); doReturn(mNativeInterface).when(mFactory).getNativeInterface(); doReturn(mScanManager).when(mFactory).createScanManager(any(), any(), any(), any(), any()); GattObjectsFactory.setInstanceForTesting(mGattObjectsFactory); ScanObjectsFactory.setInstanceForTesting(mScanObjectsFactory); doReturn(mNativeInterface).when(mGattObjectsFactory).getNativeInterface(); doReturn(mDistanceMeasurementManager) .when(mFactory) .when(mGattObjectsFactory) .createDistanceMeasurementManager(any()); doReturn(mScanManager) .when(mScanObjectsFactory) .createScanManager(any(), any(), any(), any(), any()); mAdapter = BluetoothAdapter.getDefaultAdapter(); mAttributionSource = mAdapter.getAttributionSource(); Loading Loading @@ -158,6 +163,7 @@ public class GattServiceTest { TestUtils.clearAdapterService(mAdapterService); GattObjectsFactory.setInstanceForTesting(null); ScanObjectsFactory.setInstanceForTesting(null); } @Test Loading Loading
android/app/src/com/android/bluetooth/gatt/GattObjectsFactory.java +0 −34 Original line number Diff line number Diff line Loading @@ -16,17 +16,10 @@ package com.android.bluetooth.gatt; import android.content.Context; import android.os.Looper; import android.util.Log; import com.android.bluetooth.Utils; import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.BluetoothAdapterProxy; import com.android.bluetooth.le_scan.PeriodicScanManager; import com.android.bluetooth.le_scan.ScanManager; import com.android.bluetooth.le_scan.ScanNativeInterface; import com.android.bluetooth.le_scan.TransitionalScanHelper; /** Factory class for object initialization to help with unit testing */ public class GattObjectsFactory { Loading Loading @@ -67,33 +60,6 @@ public class GattObjectsFactory { return GattNativeInterface.getInstance(); } public ScanNativeInterface getScanNativeInterface() { return ScanNativeInterface.getInstance(); } /** * Create an instance of ScanManager * * @param context a Context instance * @param scanHelper a TransitionalScanHelper instance * @param adapterService an AdapterService instance * @param bluetoothAdapterProxy a bluetoothAdapterProxy instance * @param looper the looper to be used for processing messages * @return the created ScanManager instance */ public ScanManager createScanManager( Context context, TransitionalScanHelper scanHelper, AdapterService adapterService, BluetoothAdapterProxy bluetoothAdapterProxy, Looper looper) { return new ScanManager(context, scanHelper, adapterService, bluetoothAdapterProxy, looper); } public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) { return new PeriodicScanManager(adapterService); } public DistanceMeasurementManager createDistanceMeasurementManager( AdapterService adapterService) { return new DistanceMeasurementManager(adapterService); Loading
android/app/src/com/android/bluetooth/le_scan/ScanManager.java +1 −2 Original line number Diff line number Diff line Loading @@ -49,7 +49,6 @@ import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.BluetoothAdapterProxy; import com.android.bluetooth.flags.Flags; import com.android.bluetooth.gatt.FilterParams; import com.android.bluetooth.gatt.GattObjectsFactory; import com.android.bluetooth.gatt.GattServiceConfig; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; Loading Loading @@ -982,7 +981,7 @@ public class ScanManager { private ScanNativeInterface mNativeInterface; ScanNative(TransitionalScanHelper scanHelper) { mNativeInterface = GattObjectsFactory.getInstance().getScanNativeInterface(); mNativeInterface = ScanObjectsFactory.getInstance().getScanNativeInterface(); mNativeInterface.init(scanHelper); mFilterIndexStack = new ArrayDeque<Integer>(); mClientFilterIndexMap = new HashMap<Integer, Deque<Integer>>(); Loading
android/app/src/com/android/bluetooth/le_scan/ScanObjectsFactory.java 0 → 100644 +88 −0 Original line number Diff line number Diff line /* * Copyright 2024 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.bluetooth.le_scan; import android.content.Context; import android.os.Looper; import android.util.Log; import com.android.bluetooth.Utils; import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.BluetoothAdapterProxy; /** Factory class for object initialization to help with unit testing */ public class ScanObjectsFactory { private static final String TAG = ScanObjectsFactory.class.getSimpleName(); private static ScanObjectsFactory sInstance; private static final Object INSTANCE_LOCK = new Object(); private ScanObjectsFactory() {} /** * Get the singleton instance of object factory * * @return the singleton instance, guaranteed not null */ public static ScanObjectsFactory getInstance() { synchronized (INSTANCE_LOCK) { if (sInstance == null) { sInstance = new ScanObjectsFactory(); } } return sInstance; } /** * Allow unit tests to substitute ScanObjectsFactory with a test instance * * @param objectsFactory a test instance of the ScanObjectsFactory */ public static void setInstanceForTesting(ScanObjectsFactory objectsFactory) { Utils.enforceInstrumentationTestMode(); synchronized (INSTANCE_LOCK) { Log.d(TAG, "setInstanceForTesting(), set to " + objectsFactory); sInstance = objectsFactory; } } public ScanNativeInterface getScanNativeInterface() { return ScanNativeInterface.getInstance(); } /** * Create an instance of ScanManager * * @param context a Context instance * @param scanHelper a TransitionalScanHelper instance * @param adapterService an AdapterService instance * @param bluetoothAdapterProxy a bluetoothAdapterProxy instance * @param looper the looper to be used for processing messages * @return the created ScanManager instance */ public ScanManager createScanManager( Context context, TransitionalScanHelper scanHelper, AdapterService adapterService, BluetoothAdapterProxy bluetoothAdapterProxy, Looper looper) { return new ScanManager(context, scanHelper, adapterService, bluetoothAdapterProxy, looper); } public PeriodicScanManager createPeriodicScanManager(AdapterService adapterService) { return new PeriodicScanManager(adapterService); } }
android/app/src/com/android/bluetooth/le_scan/TransitionalScanHelper.java +2 −3 Original line number Diff line number Diff line Loading @@ -57,7 +57,6 @@ import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.BluetoothAdapterProxy; import com.android.bluetooth.flags.Flags; import com.android.bluetooth.gatt.ContextMap; import com.android.bluetooth.gatt.GattObjectsFactory; import com.android.bluetooth.gatt.GattServiceConfig; import com.android.bluetooth.util.NumberUtils; import com.android.internal.annotations.VisibleForTesting; Loading Loading @@ -197,7 +196,7 @@ public class TransitionalScanHelper { mCompanionManager = mContext.getSystemService(CompanionDeviceManager.class); mAdapterService = AdapterService.getAdapterService(); mScanManager = GattObjectsFactory.getInstance() ScanObjectsFactory.getInstance() .createScanManager( mContext, this, Loading @@ -206,7 +205,7 @@ public class TransitionalScanHelper { looper); mPeriodicScanManager = GattObjectsFactory.getInstance().createPeriodicScanManager(mAdapterService); ScanObjectsFactory.getInstance().createPeriodicScanManager(mAdapterService); } /** Stops the scanning component. */ Loading
android/app/tests/unit/src/com/android/bluetooth/gatt/GattServiceTest.java +11 −5 Original line number Diff line number Diff line Loading @@ -50,6 +50,7 @@ import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.btservice.CompanionManager; import com.android.bluetooth.flags.Flags; import com.android.bluetooth.le_scan.ScanManager; import com.android.bluetooth.le_scan.ScanObjectsFactory; import com.android.bluetooth.le_scan.TransitionalScanHelper; import org.junit.After; Loading Loading @@ -104,7 +105,8 @@ public class GattServiceTest { @Mock private Resources mResources; @Mock private AdapterService mAdapterService; @Mock private GattObjectsFactory mFactory; @Mock private GattObjectsFactory mGattObjectsFactory; @Mock private ScanObjectsFactory mScanObjectsFactory; @Mock private GattNativeInterface mNativeInterface; private BluetoothDevice mCurrentDevice; private CompanionManager mBtCompanionManager; Loading @@ -115,12 +117,15 @@ public class GattServiceTest { TestUtils.setAdapterService(mAdapterService); GattObjectsFactory.setInstanceForTesting(mFactory); doReturn(mNativeInterface).when(mFactory).getNativeInterface(); doReturn(mScanManager).when(mFactory).createScanManager(any(), any(), any(), any(), any()); GattObjectsFactory.setInstanceForTesting(mGattObjectsFactory); ScanObjectsFactory.setInstanceForTesting(mScanObjectsFactory); doReturn(mNativeInterface).when(mGattObjectsFactory).getNativeInterface(); doReturn(mDistanceMeasurementManager) .when(mFactory) .when(mGattObjectsFactory) .createDistanceMeasurementManager(any()); doReturn(mScanManager) .when(mScanObjectsFactory) .createScanManager(any(), any(), any(), any(), any()); mAdapter = BluetoothAdapter.getDefaultAdapter(); mAttributionSource = mAdapter.getAttributionSource(); Loading Loading @@ -158,6 +163,7 @@ public class GattServiceTest { TestUtils.clearAdapterService(mAdapterService); GattObjectsFactory.setInstanceForTesting(null); ScanObjectsFactory.setInstanceForTesting(null); } @Test Loading