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

Commit 97113ec9 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Add LeAudioNativeInterfaceTest

Bug: 237467631
Test: atest BluetoothInstrumentationTests:LeAudioNativeInterfaceTest
Change-Id: Ic496163d2904124f26d9fd4d49a78b18d9103cb0
(cherry picked from commit 228ebbaa)
Merged-In: Ic496163d2904124f26d9fd4d49a78b18d9103cb0
parent 3d5ad38b
Loading
Loading
Loading
Loading
+14 −7
Original line number Diff line number Diff line
@@ -111,7 +111,8 @@ public class LeAudioNativeInterface {
        sendMessageToService(event);
    }

    private void onConnectionStateChanged(int state, byte[] address) {
    @VisibleForTesting
    void onConnectionStateChanged(int state, byte[] address) {
        LeAudioStackEvent event =
                new LeAudioStackEvent(LeAudioStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
        event.device = getDevice(address);
@@ -123,7 +124,8 @@ public class LeAudioNativeInterface {
        sendMessageToService(event);
    }

    private void onGroupStatus(int groupId, int groupStatus) {
    @VisibleForTesting
    void onGroupStatus(int groupId, int groupStatus) {
        LeAudioStackEvent event =
                new LeAudioStackEvent(LeAudioStackEvent.EVENT_TYPE_GROUP_STATUS_CHANGED);
        event.valueInt1 = groupId;
@@ -135,7 +137,8 @@ public class LeAudioNativeInterface {
        sendMessageToService(event);
    }

    private void onGroupNodeStatus(byte[] address, int groupId, int nodeStatus) {
    @VisibleForTesting
    void onGroupNodeStatus(byte[] address, int groupId, int nodeStatus) {
        LeAudioStackEvent event =
                new LeAudioStackEvent(LeAudioStackEvent.EVENT_TYPE_GROUP_NODE_STATUS_CHANGED);
        event.valueInt1 = groupId;
@@ -148,7 +151,8 @@ public class LeAudioNativeInterface {
        sendMessageToService(event);
    }

    private void onAudioConf(int direction, int groupId, int sinkAudioLocation,
    @VisibleForTesting
    void onAudioConf(int direction, int groupId, int sinkAudioLocation,
                             int sourceAudioLocation, int availableContexts) {
        LeAudioStackEvent event =
                new LeAudioStackEvent(LeAudioStackEvent.EVENT_TYPE_AUDIO_CONF_CHANGED);
@@ -164,7 +168,8 @@ public class LeAudioNativeInterface {
        sendMessageToService(event);
    }

    private void onSinkAudioLocationAvailable(byte[] address, int sinkAudioLocation) {
    @VisibleForTesting
    void onSinkAudioLocationAvailable(byte[] address, int sinkAudioLocation) {
        LeAudioStackEvent event =
                new LeAudioStackEvent(LeAudioStackEvent.EVENT_TYPE_SINK_AUDIO_LOCATION_AVAILABLE);
        event.device = getDevice(address);
@@ -176,7 +181,8 @@ public class LeAudioNativeInterface {
        sendMessageToService(event);
    }

    private void onAudioLocalCodecCapabilities(
    @VisibleForTesting
    void onAudioLocalCodecCapabilities(
                            BluetoothLeAudioCodecConfig[] localInputCodecCapabilities,
                            BluetoothLeAudioCodecConfig[] localOutputCodecCapabilities) {
        LeAudioStackEvent event =
@@ -192,7 +198,8 @@ public class LeAudioNativeInterface {
        sendMessageToService(event);
    }

    private void onAudioGroupCodecConf(int groupId, BluetoothLeAudioCodecConfig inputCodecConfig,
    @VisibleForTesting
    void onAudioGroupCodecConf(int groupId, BluetoothLeAudioCodecConfig inputCodecConfig,
                            BluetoothLeAudioCodecConfig outputCodecConfig,
                            BluetoothLeAudioCodecConfig [] inputSelectableCodecConfig,
                            BluetoothLeAudioCodecConfig [] outputSelectableCodecConfig) {
+2 −1
Original line number Diff line number Diff line
@@ -406,7 +406,8 @@ public class LeAudioService extends ProfileService {
        return sLeAudioService;
    }

    private static synchronized void setLeAudioService(LeAudioService instance) {
    @VisibleForTesting
    static synchronized void setLeAudioService(LeAudioService instance) {
        if (DBG) {
            Log.d(TAG, "setLeAudioService(): set to: " + instance);
        }
+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.le_audio;

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

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

import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothLeAudioCodecConfig;

import androidx.test.runner.AndroidJUnit4;

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

@RunWith(AndroidJUnit4.class)
public class LeAudioNativeInterfaceTest {
    @Mock
    private LeAudioService mMockService;

    private LeAudioNativeInterface mNativeInterface;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(mMockService.isAvailable()).thenReturn(true);
        LeAudioService.setLeAudioService(mMockService);
        mNativeInterface = LeAudioNativeInterface.getInstance();
    }

    @After
    public void tearDown() {
        LeAudioService.setLeAudioService(null);
    }

    @Test
    public void onConnectionStateChanged() {
        int state = LeAudioStackEvent.CONNECTION_STATE_CONNECTED;
        byte[] address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };

        mNativeInterface.onConnectionStateChanged(state, address);

        ArgumentCaptor<LeAudioStackEvent> event =
                ArgumentCaptor.forClass(LeAudioStackEvent.class);
        verify(mMockService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                LeAudioStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
    }

    @Test
    public void onGroupNodeStatus() {
        byte[] address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
        int groupId = 1;
        int nodeStatus = LeAudioStackEvent.GROUP_NODE_ADDED;

        mNativeInterface.onGroupNodeStatus(address, groupId, nodeStatus);

        ArgumentCaptor<LeAudioStackEvent> event =
                ArgumentCaptor.forClass(LeAudioStackEvent.class);
        verify(mMockService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                LeAudioStackEvent.EVENT_TYPE_GROUP_NODE_STATUS_CHANGED);
    }

    @Test
    public void onAudioConf() {
        int direction = 0;
        int groupId = 1;
        int sinkAudioLocation = BluetoothLeAudio.AUDIO_LOCATION_INVALID;
        int sourceAudioLocation = BluetoothLeAudio.AUDIO_LOCATION_INVALID;
        int availableContexts = 2;

        mNativeInterface.onAudioConf(
                direction, groupId, sinkAudioLocation, sourceAudioLocation, availableContexts);

        ArgumentCaptor<LeAudioStackEvent> event =
                ArgumentCaptor.forClass(LeAudioStackEvent.class);
        verify(mMockService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                LeAudioStackEvent.EVENT_TYPE_AUDIO_CONF_CHANGED);
    }

    @Test
    public void onSinkAudioLocationAvailable() {
        byte[] address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
        int sinkAudioLocation = BluetoothLeAudio.AUDIO_LOCATION_INVALID;

        mNativeInterface.onSinkAudioLocationAvailable(address, sinkAudioLocation);

        ArgumentCaptor<LeAudioStackEvent> event =
                ArgumentCaptor.forClass(LeAudioStackEvent.class);
        verify(mMockService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                LeAudioStackEvent.EVENT_TYPE_SINK_AUDIO_LOCATION_AVAILABLE);
    }

    @Test
    public void onAudioLocalCodecCapabilities() {
        BluetoothLeAudioCodecConfig emptyConfig =
                new BluetoothLeAudioCodecConfig.Builder().build();
        BluetoothLeAudioCodecConfig[] localInputCodecCapabilities =
                new BluetoothLeAudioCodecConfig[] { emptyConfig };
        BluetoothLeAudioCodecConfig[] localOutputCodecCapabilities =
                new BluetoothLeAudioCodecConfig[] { emptyConfig };

        mNativeInterface.onAudioLocalCodecCapabilities(
                localInputCodecCapabilities, localOutputCodecCapabilities);

        ArgumentCaptor<LeAudioStackEvent> event =
                ArgumentCaptor.forClass(LeAudioStackEvent.class);
        verify(mMockService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                LeAudioStackEvent.EVENT_TYPE_AUDIO_LOCAL_CODEC_CONFIG_CAPA_CHANGED);
    }

    @Test
    public void onAudioGroupCodecConf() {
        int groupId = 1;
        BluetoothLeAudioCodecConfig inputConfig =
                new BluetoothLeAudioCodecConfig.Builder().build();
        BluetoothLeAudioCodecConfig outputConfig =
                new BluetoothLeAudioCodecConfig.Builder().build();
        BluetoothLeAudioCodecConfig[] inputSelectableCodecConfig =
                new BluetoothLeAudioCodecConfig[] { inputConfig };
        BluetoothLeAudioCodecConfig[] outputSelectableCodecConfig =
                new BluetoothLeAudioCodecConfig[] { outputConfig };

        mNativeInterface.onAudioGroupCodecConf(groupId, inputConfig, outputConfig,
                inputSelectableCodecConfig, outputSelectableCodecConfig);

        ArgumentCaptor<LeAudioStackEvent> event =
                ArgumentCaptor.forClass(LeAudioStackEvent.class);
        verify(mMockService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                LeAudioStackEvent.EVENT_TYPE_AUDIO_GROUP_CODEC_CONFIG_CHANGED);
    }
}