Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6993d91e authored by William Escande's avatar William Escande
Browse files

GattService: Do not call native in test

Bug: 295237486
Test: atest GattServiceTest
Change-Id: I73049ec8714794e7657b77e0ce22799c2da09834
parent ea05bb33
Loading
Loading
Loading
Loading
+38 −6
Original line number Diff line number Diff line
@@ -18,13 +18,49 @@ package com.android.bluetooth.gatt;

import android.bluetooth.le.AdvertisingSetParameters;
import android.bluetooth.le.PeriodicAdvertisingParameters;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import com.android.bluetooth.Utils;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

/** Native interface for AdvertiseManager */
@VisibleForTesting
public class AdvertiseManagerNativeInterface {
    AdvertiseManager mManager;
    private static final String TAG = AdvertiseManagerNativeInterface.class.getSimpleName();

    @GuardedBy("INSTANCE_LOCK")
    private static AdvertiseManagerNativeInterface sInstance;

    private static final Object INSTANCE_LOCK = new Object();

    private AdvertiseManager mManager;

    static {
        if (Utils.isInstrumentationTestMode()) {
            Log.w(TAG, "App is instrumented. Skip loading the native");
        } else {
            classInitNative();
        }
    }

    /** Get singleton instance. */
    public static AdvertiseManagerNativeInterface getInstance() {
        synchronized (INSTANCE_LOCK) {
            if (sInstance == null) {
                sInstance = new AdvertiseManagerNativeInterface();
            }
            return sInstance;
        }
    }

    /** Set singleton instance. */
    @VisibleForTesting
    public static void setInstance(AdvertiseManagerNativeInterface instance) {
        synchronized (INSTANCE_LOCK) {
            sInstance = instance;
        }
    }

    void init(AdvertiseManager manager) {
        mManager = manager;
@@ -95,10 +131,6 @@ public class AdvertiseManagerNativeInterface {
        setPeriodicAdvertisingEnableNative(advertiserId, enable);
    }

    static {
        classInitNative();
    }

    void onAdvertisingSetStarted(int regId, int advertiserId, int txPower, int status)
            throws Exception {
        mManager.onAdvertisingSetStarted(regId, advertiserId, txPower, status);
+26 −9
Original line number Diff line number Diff line
@@ -16,8 +16,12 @@

package com.android.bluetooth.gatt;

import android.bluetooth.BluetoothDevice;
import android.os.RemoteException;
import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import java.util.ArrayList;
import java.util.List;
@@ -28,14 +32,20 @@ import java.util.List;
public class GattNativeInterface {
    private static final String TAG = GattNativeInterface.class.getSimpleName();

    static {
        classInitNative();
    }
    private GattService mGattService;

    @GuardedBy("INSTANCE_LOCK")
    private static GattNativeInterface sInstance;

    private static GattNativeInterface sInterface;
    private static final Object INSTANCE_LOCK = new Object();

    private GattService mGattService;
    static {
        if (Utils.isInstrumentationTestMode()) {
            Log.w(TAG, "App is instrumented. Skip loading the native");
        } else {
            classInitNative();
        }
    }

    private GattNativeInterface() {}

@@ -50,13 +60,20 @@ public class GattNativeInterface {
     */
    public static GattNativeInterface getInstance() {
        synchronized (INSTANCE_LOCK) {
            if (sInterface == null) {
                sInterface = new GattNativeInterface();
            if (sInstance == null) {
                sInstance = new GattNativeInterface();
            }
            return sInstance;
        }
        return sInterface;
    }

    /** Set singleton instance. */
    @VisibleForTesting
    public static void setInstance(GattNativeInterface instance) {
        synchronized (INSTANCE_LOCK) {
            sInstance = instance;
        }
    }

    /* Callbacks */

+1 −30
Original line number Diff line number Diff line
@@ -317,8 +317,6 @@ public class GattService extends ProfileService {
        return false;
    };

    private static GattService sGattService;

    /**
     * Reliable write queue
     */
@@ -350,7 +348,7 @@ public class GattService extends ProfileService {
        mAdvertiseManager =
                new AdvertiseManager(
                        this,
                        new AdvertiseManagerNativeInterface(),
                        AdvertiseManagerNativeInterface.getInstance(),
                        mAdapterService,
                        mAdvertiserMap);
        mAdvertiseManager.start();
@@ -367,7 +365,6 @@ public class GattService extends ProfileService {
                .createDistanceMeasurementManager(mAdapterService);
        mDistanceMeasurementManager.start();

        setGattService(this);
        return true;
    }

@@ -376,7 +373,6 @@ public class GattService extends ProfileService {
        if (DBG) {
            Log.d(TAG, "stop()");
        }
        setGattService(null);
        mScannerMap.clear();
        mAdvertiserMap.clear();
        mClientMap.clear();
@@ -445,24 +441,6 @@ public class GattService extends ProfileService {
        }
    }

    /**
     * Get the current instance of {@link GattService}
     *
     * @return current instance of {@link GattService}
     */
    @VisibleForTesting
    public static synchronized GattService getGattService() {
        if (sGattService == null) {
            Log.w(TAG, "getGattService(): service is null");
            return null;
        }
        if (!sGattService.isAvailable()) {
            Log.w(TAG, "getGattService(): service is not available");
            return null;
        }
        return sGattService;
    }

    @VisibleForTesting
    ScanManager getScanManager() {
        if (mScanManager == null) {
@@ -472,13 +450,6 @@ public class GattService extends ProfileService {
        return mScanManager;
    }

    private static synchronized void setGattService(GattService instance) {
        if (DBG) {
            Log.d(TAG, "setGattService(): set to: " + instance);
        }
        sGattService = instance;
    }

    // Suppressed because we are conditionally enforcing
    @SuppressLint("AndroidFrameworkRequiresPermission")
    private void permissionCheck(UUID characteristicUuid) {
+3 −0
Original line number Diff line number Diff line
@@ -95,6 +95,7 @@ public class GattServiceTest {
    @Mock private Set<String> mReliableQueue;
    @Mock private GattService.ServerMap mServerMap;
    @Mock private DistanceMeasurementManager mDistanceMeasurementManager;
    @Mock private AdvertiseManagerNativeInterface mAdvertiseManagerNativeInterface;

    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

@@ -139,6 +140,7 @@ public class GattServiceTest {
        mBtCompanionManager = new CompanionManager(mAdapterService, null);
        doReturn(mBtCompanionManager).when(mAdapterService).getCompanionManager();

        AdvertiseManagerNativeInterface.setInstance(mAdvertiseManagerNativeInterface);
        mService = new GattService(InstrumentationRegistry.getTargetContext());
        mService.start();

@@ -152,6 +154,7 @@ public class GattServiceTest {
    public void tearDown() throws Exception {
        mService.stop();
        mService = null;
        AdvertiseManagerNativeInterface.setInstance(null);

        TestUtils.clearAdapterService(mAdapterService);
        GattObjectsFactory.setInstanceForTesting(null);