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

Commit 4c8cb573 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "cherrypicker-L25000000957940065:N64700001324129790" into tm-qpr-dev

* changes:
  Add additional ContextMapTest
  Add additional ScanFilterQueueTest
  Add GattDebugUtilsTest
  Add AdvertiseManagerTest
parents 9e07b801 c15a4cbf
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -36,6 +36,9 @@ import android.os.ParcelFileDescriptor;
import android.provider.Telephony;
import android.util.Log;

import com.android.bluetooth.gatt.AppAdvertiseStats;
import com.android.bluetooth.gatt.ContextMap;
import com.android.bluetooth.gatt.GattService;
import com.android.internal.annotations.VisibleForTesting;
import com.android.obex.HeaderSet;

@@ -212,4 +215,12 @@ public class BluetoothMethodProxy {
            int advHandle, PeriodicAdvertisingCallback callback) {
        manager.transferSetInfo(bda, serviceData, advHandle, callback);
    }

    /**
     * Proxies {@link AppAdvertiseStats}.
     */
    public AppAdvertiseStats createAppAdvertiseStats(int appUid, int id, String name,
            ContextMap map, GattService service) {
        return new AppAdvertiseStats(appUid, id, name, map, service);
    }
}
+6 −2
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import android.bluetooth.le.PeriodicAdvertisingParameters;
import android.os.ParcelUuid;
import android.util.SparseArray;

import androidx.annotation.VisibleForTesting;

import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
@@ -35,7 +37,8 @@ import java.util.Map;
 * on a per application basis.
 * @hide
 */
/*package*/ class AppAdvertiseStats {
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
public class AppAdvertiseStats {
    private static final String TAG = AppAdvertiseStats.class.getSimpleName();

    private static DateTimeFormatter sDateFormat = DateTimeFormatter.ofPattern("MM-dd HH:mm:ss")
@@ -98,7 +101,8 @@ import java.util.Map;
    public ArrayList<AppAdvertiserRecord> mAdvertiserRecords =
            new ArrayList<AppAdvertiserRecord>();

    AppAdvertiseStats(int appUid, int id, String name, ContextMap map, GattService service) {
    @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
    public AppAdvertiseStats(int appUid, int id, String name, ContextMap map, GattService service) {
        this.mAppUid = appUid;
        this.mId = id;
        this.mAppName = name;
+7 −3
Original line number Diff line number Diff line
@@ -27,6 +27,9 @@ import android.os.UserHandle;
import android.os.WorkSource;
import android.util.Log;

import androidx.annotation.VisibleForTesting;

import com.android.bluetooth.BluetoothMethodProxy;
import com.android.internal.annotations.GuardedBy;

import com.google.common.collect.EvictingQueue;
@@ -46,7 +49,8 @@ import java.util.UUID;
 * This class manages application callbacks and keeps track of GATT connections.
 * @hide
 */
/*package*/ class ContextMap<C, T> {
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
public class ContextMap<C, T> {
    private static final String TAG = GattServiceConfig.TAG_PREFIX + "ContextMap";

    /**
@@ -241,8 +245,8 @@ import java.util.UUID;
        synchronized (mAppsLock) {
            synchronized (this) {
                if (!mAppAdvertiseStats.containsKey(id)) {
                    AppAdvertiseStats appAdvertiseStats =
                            new AppAdvertiseStats(appUid, id, appName, this, service);
                    AppAdvertiseStats appAdvertiseStats = BluetoothMethodProxy.getInstance()
                            .createAppAdvertiseStats(appUid, id, appName, this, service);
                    mAppAdvertiseStats.put(id, appAdvertiseStats);
                }
            }
+16 −7
Original line number Diff line number Diff line
@@ -20,6 +20,9 @@ import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

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

import java.util.UUID;

/**
@@ -29,17 +32,23 @@ import java.util.UUID;
    private static final String TAG = GattServiceConfig.TAG_PREFIX + "DebugUtils";
    private static final boolean DEBUG_ADMIN = GattServiceConfig.DEBUG_ADMIN;

    private static final String ACTION_GATT_PAIRING_CONFIG =
    @VisibleForTesting
    static final String ACTION_GATT_PAIRING_CONFIG =
            "android.bluetooth.action.GATT_PAIRING_CONFIG";

    private static final String ACTION_GATT_TEST_USAGE = "android.bluetooth.action.GATT_TEST_USAGE";
    private static final String ACTION_GATT_TEST_ENABLE =
    @VisibleForTesting
    static final String ACTION_GATT_TEST_USAGE = "android.bluetooth.action.GATT_TEST_USAGE";
    @VisibleForTesting
    static final String ACTION_GATT_TEST_ENABLE =
            "android.bluetooth.action.GATT_TEST_ENABLE";
    private static final String ACTION_GATT_TEST_CONNECT =
    @VisibleForTesting
    static final String ACTION_GATT_TEST_CONNECT =
            "android.bluetooth.action.GATT_TEST_CONNECT";
    private static final String ACTION_GATT_TEST_DISCONNECT =
    @VisibleForTesting
    static final String ACTION_GATT_TEST_DISCONNECT =
            "android.bluetooth.action.GATT_TEST_DISCONNECT";
    private static final String ACTION_GATT_TEST_DISCOVER =
    @VisibleForTesting
    static final String ACTION_GATT_TEST_DISCOVER =
            "android.bluetooth.action.GATT_TEST_DISCOVER";

    private static final String EXTRA_ENABLE = "enable";
@@ -69,7 +78,7 @@ import java.util.UUID;
     *   import com.android.bluetooth.gatt.GattService;
     */
    static boolean handleDebugAction(GattService svc, Intent intent) {
        if (!DEBUG_ADMIN) {
        if (!DEBUG_ADMIN && !Utils.isInstrumentationTestMode()) {
            return false;
        }

+158 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 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.gatt;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertisingSetParameters;
import android.bluetooth.le.IAdvertisingSetCallback;
import android.bluetooth.le.PeriodicAdvertisingParameters;
import android.os.IBinder;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/**
 * Test cases for {@link AdvertiseManager}.
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
public class AdvertiseManagerTest {

    @Mock
    private AdapterService mAdapterService;

    @Mock
    private GattService mService;

    @Mock
    private GattService.AdvertiserMap mAdvertiserMap;

    @Mock
    private IAdvertisingSetCallback mCallback;

    @Mock
    private IBinder mBinder;

    private AdvertiseManager mAdvertiseManager;
    private int mAdvertiserId;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        TestUtils.setAdapterService(mAdapterService);

        mAdvertiseManager = new AdvertiseManager(mService, mAdapterService, mAdvertiserMap);
        AdvertisingSetParameters parameters = new AdvertisingSetParameters.Builder().build();
        AdvertiseData advertiseData = new AdvertiseData.Builder().build();
        AdvertiseData scanResponse = new AdvertiseData.Builder().build();
        PeriodicAdvertisingParameters periodicParameters =
                new PeriodicAdvertisingParameters.Builder().build();
        AdvertiseData periodicData = new AdvertiseData.Builder().build();
        int duration = 10;
        int maxExtAdvEvents = 15;

        doReturn(mBinder).when(mCallback).asBinder();
        doNothing().when(mBinder).linkToDeath(any(), eq(0));

        mAdvertiseManager.startAdvertisingSet(parameters, advertiseData, scanResponse,
                periodicParameters, periodicData, duration, maxExtAdvEvents, mCallback);

        mAdvertiserId = AdvertiseManager.sTempRegistrationId;
    }

    @After
    public void tearDown() throws Exception {
        TestUtils.clearAdapterService(mAdapterService);
    }

    @Test
    public void advertisingSet() {
        boolean enable = true;
        int duration = 60;
        int maxExtAdvEvents = 100;

        mAdvertiseManager.enableAdvertisingSet(mAdvertiserId, enable, duration, maxExtAdvEvents);

        verify(mAdvertiserMap).enableAdvertisingSet(mAdvertiserId, enable, duration,
                maxExtAdvEvents);
    }

    @Test
    public void advertisingData() {
        AdvertiseData advertiseData = new AdvertiseData.Builder().build();

        mAdvertiseManager.setAdvertisingData(mAdvertiserId, advertiseData);

        verify(mAdvertiserMap).setAdvertisingData(mAdvertiserId, advertiseData);
    }

    @Test
    public void scanResponseData() {
        AdvertiseData scanResponse = new AdvertiseData.Builder().build();

        mAdvertiseManager.setScanResponseData(mAdvertiserId, scanResponse);

        verify(mAdvertiserMap).setScanResponseData(mAdvertiserId, scanResponse);
    }

    @Test
    public void advertisingParameters() {
        AdvertisingSetParameters parameters = new AdvertisingSetParameters.Builder().build();

        mAdvertiseManager.setAdvertisingParameters(mAdvertiserId, parameters);

        verify(mAdvertiserMap).setAdvertisingParameters(mAdvertiserId, parameters);
    }

    @Test
    public void periodicAdvertisingParameters() {
        PeriodicAdvertisingParameters periodicParameters =
                new PeriodicAdvertisingParameters.Builder().build();

        mAdvertiseManager.setPeriodicAdvertisingParameters(mAdvertiserId, periodicParameters);

        verify(mAdvertiserMap).setPeriodicAdvertisingParameters(mAdvertiserId, periodicParameters);
    }

    @Test
    public void periodicAdvertisingData() {
        AdvertiseData periodicData = new AdvertiseData.Builder().build();

        mAdvertiseManager.setPeriodicAdvertisingData(mAdvertiserId, periodicData);

        verify(mAdvertiserMap).setPeriodicAdvertisingData(mAdvertiserId, periodicData);
    }
}
Loading