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

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

Merge changes from topic "cherrypicker-L78100000957439934:N08000001313698819" into tm-qpr-dev

* changes:
  Add additional SdpMasRecordTest
  Add SdpMasRecordTest
  Add BluetoothTetheringNetworkFactoryTest
  Add additional BatteryStateMachineTest
  Add AdvtFilterOnFoundOnLostInfoTest
parents 7cbf0c42 f381c369
Loading
Loading
Loading
Loading
+81 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.bluetooth.bas;

import static android.bluetooth.BluetoothGatt.GATT_SUCCESS;

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

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
@@ -32,6 +34,7 @@ import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.HandlerThread;
@@ -189,6 +192,72 @@ public class BatteryStateMachineTest {
                IsInstanceOf.instanceOf(BatteryStateMachine.Connected.class));
    }

    @Test
    public void testConnectedStateChanges() {
        allowConnection(true);
        allowConnectGatt(true);

        // Connected -> CONNECT
        reconnect();

        mBatteryStateMachine.sendMessage(BatteryStateMachine.CONNECT);

        assertThat(mBatteryStateMachine.getCurrentState())
                .isInstanceOf(BatteryStateMachine.Connected.class);

        // Connected -> DISCONNECT
        reconnect();

        mBatteryStateMachine.sendMessage(BatteryStateMachine.DISCONNECT);

        TestUtils.waitForLooperToFinishScheduledTask(mBatteryStateMachine.getHandler().getLooper());

        mBatteryStateMachine.notifyConnectionStateChanged(
                GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED);

        assertThat(mBatteryStateMachine.getCurrentState())
                .isInstanceOf(BatteryStateMachine.Disconnected.class);

        // Connected -> STATE_DISCONNECTED
        reconnect();

        mBatteryStateMachine.sendMessage(
                BatteryStateMachine.CONNECTION_STATE_CHANGED, BluetoothGatt.STATE_DISCONNECTED);

        TestUtils.waitForLooperToFinishScheduledTask(mBatteryStateMachine.getHandler().getLooper());

        mBatteryStateMachine.notifyConnectionStateChanged(
                GATT_SUCCESS, BluetoothProfile.STATE_DISCONNECTED);

        // Connected -> STATE_CONNECTED
        reconnect();

        mBatteryStateMachine.sendMessage(
                BatteryStateMachine.CONNECTION_STATE_CHANGED, BluetoothGatt.STATE_CONNECTED);

        assertThat(mBatteryStateMachine.getCurrentState())
                .isInstanceOf(BatteryStateMachine.Connected.class);

        // Connected -> ILLEGAL_STATE
        reconnect();

        int badState = -1;
        mBatteryStateMachine.sendMessage(
                BatteryStateMachine.CONNECTION_STATE_CHANGED, badState);

        assertThat(mBatteryStateMachine.getCurrentState())
                .isInstanceOf(BatteryStateMachine.Connected.class);

        // Connected -> NOT_HANDLED
        reconnect();

        int notHandled = -1;
        mBatteryStateMachine.sendMessage(notHandled);

        assertThat(mBatteryStateMachine.getCurrentState())
                .isInstanceOf(BatteryStateMachine.Connected.class);
    }

    @Test
    public void testConnectGattTimeout() {
        allowConnection(true);
@@ -244,6 +313,18 @@ public class BatteryStateMachineTest {
                .handleBatteryChanged(any(BluetoothDevice.class), anyInt());
    }

    private void reconnect() {
        // Inject an event for when incoming connection is requested
        mBatteryStateMachine.sendMessage(BatteryStateMachine.CONNECT);

        TestUtils.waitForLooperToFinishScheduledTask(mBatteryStateMachine.getHandler().getLooper());

        mBatteryStateMachine.notifyConnectionStateChanged(
                GATT_SUCCESS, BluetoothProfile.STATE_CONNECTED);

        TestUtils.waitForLooperToFinishScheduledTask(mBatteryStateMachine.getHandler().getLooper());
    }

    // It simulates GATT connection for testing.
    public class StubBatteryStateMachine extends BatteryStateMachine {
        boolean mShouldAllowGatt = true;
+83 −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 androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

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

    @Test
    public void advtFilterOnFoundOnLostInfoParams() {
        int clientIf = 0;
        int advPktLen = 1;
        byte[] advPkt = new byte[]{0x02};
        int scanRspLen = 3;
        byte[] scanRsp = new byte[]{0x04};
        int filtIndex = 5;
        int advState = 6;
        int advInfoPresent = 7;
        String address = "00:11:22:33:FF:EE";
        int addrType = 8;
        int txPower = 9;
        int rssiValue = 10;
        int timeStamp = 11;

        AdvtFilterOnFoundOnLostInfo advtFilterOnFoundOnLostInfo = new AdvtFilterOnFoundOnLostInfo(
                clientIf,
                advPktLen,
                advPkt,
                scanRspLen,
                scanRsp,
                filtIndex,
                advState,
                advInfoPresent,
                address,
                addrType,
                txPower,
                rssiValue,
                timeStamp
        );

        assertThat(advtFilterOnFoundOnLostInfo.getClientIf()).isEqualTo(clientIf);
        assertThat(advtFilterOnFoundOnLostInfo.getFiltIndex()).isEqualTo(filtIndex);
        assertThat(advtFilterOnFoundOnLostInfo.getAdvState()).isEqualTo(advState);
        assertThat(advtFilterOnFoundOnLostInfo.getTxPower()).isEqualTo(txPower);
        assertThat(advtFilterOnFoundOnLostInfo.getTimeStamp()).isEqualTo(timeStamp);
        assertThat(advtFilterOnFoundOnLostInfo.getRSSIValue()).isEqualTo(rssiValue);
        assertThat(advtFilterOnFoundOnLostInfo.getAdvInfoPresent()).isEqualTo(advInfoPresent);
        assertThat(advtFilterOnFoundOnLostInfo.getAddress()).isEqualTo(address);
        assertThat(advtFilterOnFoundOnLostInfo.getAddressType()).isEqualTo(addrType);
        assertThat(advtFilterOnFoundOnLostInfo.getAdvPacketData()).isEqualTo(advPkt);
        assertThat(advtFilterOnFoundOnLostInfo.getAdvPacketLen()).isEqualTo(advPktLen);
        assertThat(advtFilterOnFoundOnLostInfo.getScanRspData()).isEqualTo(scanRsp);
        assertThat(advtFilterOnFoundOnLostInfo.getScanRspLen()).isEqualTo(scanRspLen);

        byte[] resultByteArray = new byte[]{2, 4};
        assertThat(advtFilterOnFoundOnLostInfo.getResult()).isEqualTo(resultByteArray);
    }
}
+118 −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.pan;

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

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Looper;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

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

import java.util.ArrayList;
import java.util.List;

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

    @Mock
    private PanService mPanService;

    private Context mContext = ApplicationProvider.getApplicationContext();

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

    @Test
    public void networkStartReverseTetherEmptyIface() {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }

        BluetoothTetheringNetworkFactory bluetoothTetheringNetworkFactory =
                new BluetoothTetheringNetworkFactory(mContext, Looper.myLooper(), mPanService);

        String iface = "";
        bluetoothTetheringNetworkFactory.startReverseTether(iface);

        assertThat(bluetoothTetheringNetworkFactory.getProvider()).isNull();
    }

    @Test
    public void networkStartReverseTether() {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }

        BluetoothTetheringNetworkFactory bluetoothTetheringNetworkFactory =
                new BluetoothTetheringNetworkFactory(mContext, Looper.myLooper(), mPanService);

        String iface = "iface";
        bluetoothTetheringNetworkFactory.startReverseTether(iface);

        assertThat(bluetoothTetheringNetworkFactory.getProvider()).isNotNull();
    }

    @Test
    public void networkStartReverseTetherStop() {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }

        BluetoothTetheringNetworkFactory bluetoothTetheringNetworkFactory =
                new BluetoothTetheringNetworkFactory(mContext, Looper.myLooper(), mPanService);

        String iface = "iface";
        bluetoothTetheringNetworkFactory.startReverseTether(iface);

        assertThat(bluetoothTetheringNetworkFactory.getProvider()).isNotNull();

        BluetoothAdapter adapter =
                mContext.getSystemService(BluetoothManager.class).getAdapter();
        List<BluetoothDevice> bluetoothDevices = new ArrayList<>();
        BluetoothDevice bluetoothDevice = adapter.getRemoteDevice("11:11:11:11:11:11");
        bluetoothDevices.add(bluetoothDevice);

        when(mPanService.getConnectedDevices()).thenReturn(bluetoothDevices);

        bluetoothTetheringNetworkFactory.stopReverseTether();

        verify(mPanService, times(1)).getConnectedDevices();
        verify(mPanService, times(1)).disconnect(bluetoothDevice);
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ android_test {
        "android.test.base",
    ],
    static_libs: [
        "androidx.test.ext.truth",
        "androidx.test.rules",
        "junit",
        "modules-utils-bytesmatcher",
+140 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 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 android.bluetooth;

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

import android.os.Parcel;

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

import org.junit.Test;
import org.junit.runner.RunWith;

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

    @Test
    public void createSdpMasRecord() {
        int masInstanceId = 1;
        int l2capPsm = 1;
        int rfcommChannelNumber = 1;
        int profileVersion = 1;
        int supportedFeatures = 1;
        int supportedMessageTypes = 1;
        String serviceName = "MasRecord";

        SdpMasRecord record = new SdpMasRecord(
                masInstanceId,
                l2capPsm,
                rfcommChannelNumber,
                profileVersion,
                supportedFeatures,
                supportedMessageTypes,
                serviceName
        );

        assertThat(record.getMasInstanceId()).isEqualTo(masInstanceId);
        assertThat(record.getL2capPsm()).isEqualTo(l2capPsm);
        assertThat(record.getRfcommCannelNumber()).isEqualTo(rfcommChannelNumber);
        assertThat(record.getProfileVersion()).isEqualTo(profileVersion);
        assertThat(record.getSupportedFeatures()).isEqualTo(supportedFeatures);
        assertThat(record.getSupportedMessageTypes()).isEqualTo(supportedMessageTypes);
        assertThat(record.getServiceName()).isEqualTo(serviceName);
    }

    @Test
    public void writeToParcel() {
        int masInstanceId = 1;
        int l2capPsm = 1;
        int rfcommChannelNumber = 1;
        int profileVersion = 1;
        int supportedFeatures = 1;
        int supportedMessageTypes = 1;
        String serviceName = "MasRecord";

        SdpMasRecord originalRecord = new SdpMasRecord(
                masInstanceId,
                l2capPsm,
                rfcommChannelNumber,
                profileVersion,
                supportedFeatures,
                supportedMessageTypes,
                serviceName
        );

        Parcel parcel = Parcel.obtain();
        originalRecord.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);

        SdpMasRecord recordOut = (SdpMasRecord) SdpMasRecord.CREATOR.createFromParcel(parcel);
        parcel.recycle();

        assertThat(recordOut.getMasInstanceId())
                .isEqualTo(originalRecord.getMasInstanceId());
        assertThat(recordOut.getL2capPsm())
                .isEqualTo(originalRecord.getL2capPsm());
        assertThat(recordOut.getRfcommCannelNumber())
                .isEqualTo(originalRecord.getRfcommCannelNumber());
        assertThat(recordOut.getProfileVersion())
                .isEqualTo(originalRecord.getProfileVersion());
        assertThat(recordOut.getSupportedFeatures())
                .isEqualTo(originalRecord.getSupportedFeatures());
        assertThat(recordOut.getSupportedMessageTypes())
                .isEqualTo(originalRecord.getSupportedMessageTypes());
        assertThat(recordOut.getServiceName())
                .isEqualTo(originalRecord.getServiceName());
    }

    @Test
    public void sdpMasRecordToString() {
        int masInstanceId = 1;
        int l2capPsm = 1;
        int rfcommChannelNumber = 1;
        int profileVersion = 1;
        int supportedFeatures = 1;
        int supportedMessageTypes = 1;
        String serviceName = "MasRecord";

        SdpMasRecord record = new SdpMasRecord(
                masInstanceId,
                l2capPsm,
                rfcommChannelNumber,
                profileVersion,
                supportedFeatures,
                supportedMessageTypes,
                serviceName
        );

        String sdpMasRecordString = record.toString();
        String expectedToString = "Bluetooth MAS SDP Record:\n"
                + "Mas Instance Id: " + masInstanceId + "\n"
                + "RFCOMM Chan Number: " + l2capPsm + "\n"
                + "L2CAP PSM: " + rfcommChannelNumber + "\n"
                + "Service Name: " + serviceName + "\n"
                + "Profile version: " + profileVersion + "\n"
                + "Supported msg types: " + supportedMessageTypes + "\n"
                + "Supported features: " + supportedFeatures + "\n";

        assertThat(sdpMasRecordString).isEqualTo(expectedToString);
    }
}