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

Commit 9fa966f3 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Add HearingAidNativeInterfaceTest

Bug: 237467631
Test: atest BluetoothInstrumentationTests:HearingAidNativeInterfaceTest
Change-Id: I02a96484f9a2bba4af658ab47257c057abf00a7c
Merged-In: I02a96484f9a2bba4af658ab47257c057abf00a7c
(cherry picked from commit 041b57e9)
parent 0db97b88
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -128,14 +128,16 @@ public class HearingAidNativeInterface {
        return mAdapter.getRemoteDevice(address);
    }

    private byte[] getByteAddress(BluetoothDevice device) {
    @VisibleForTesting
    byte[] getByteAddress(BluetoothDevice device) {
        if (device == null) {
            return Utils.getBytesFromAddress("00:00:00:00:00:00");
        }
        return Utils.getBytesFromAddress(device.getAddress());
    }

    private void sendMessageToService(HearingAidStackEvent event) {
    @VisibleForTesting
    void sendMessageToService(HearingAidStackEvent event) {
        HearingAidService service = HearingAidService.getHearingAidService();
        if (service != null) {
            service.messageFromNative(event);
@@ -148,7 +150,8 @@ public class HearingAidNativeInterface {
    // All callbacks are routed via the Service which will disambiguate which
    // state machine the message should be routed to.

    private void onConnectionStateChanged(int state, byte[] address) {
    @VisibleForTesting
    void onConnectionStateChanged(int state, byte[] address) {
        HearingAidStackEvent event =
                new HearingAidStackEvent(HearingAidStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
        event.device = getDevice(address);
@@ -160,7 +163,8 @@ public class HearingAidNativeInterface {
        sendMessageToService(event);
    }

    private void onDeviceAvailable(byte capabilities, long hiSyncId, byte[] address) {
    @VisibleForTesting
    void onDeviceAvailable(byte capabilities, long hiSyncId, byte[] address) {
        HearingAidStackEvent event = new HearingAidStackEvent(
                HearingAidStackEvent.EVENT_TYPE_DEVICE_AVAILABLE);
        event.device = getDevice(address);
+2 −1
Original line number Diff line number Diff line
@@ -252,7 +252,8 @@ public class HearingAidService extends ProfileService {
        return sHearingAidService;
    }

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

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

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

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;

import com.android.bluetooth.TestUtils;
import com.android.bluetooth.Utils;

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

public class HearingAidNativeInterfaceTest {

    @Mock private HearingAidService mService;

    private HearingAidNativeInterface mNativeInterface;
    private BluetoothAdapter mAdapter;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(mService.isAvailable()).thenReturn(true);
        HearingAidService.setHearingAidService(mService);
        mNativeInterface = HearingAidNativeInterface.getInstance();
        mAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    @After
    public void tearDown() {
        HearingAidService.setHearingAidService(null);
    }

    @Test
    public void getByteAddress() {
        assertThat(mNativeInterface.getByteAddress(null))
                .isEqualTo(Utils.getBytesFromAddress("00:00:00:00:00:00"));

        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        assertThat(mNativeInterface.getByteAddress(device))
                .isEqualTo(Utils.getBytesFromAddress(device.getAddress()));
    }

    @Test
    public void onConnectionStateChanged() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        mNativeInterface.onConnectionStateChanged(BluetoothProfile.STATE_CONNECTED,
                mNativeInterface.getByteAddress(device));

        ArgumentCaptor<HearingAidStackEvent> event =
                ArgumentCaptor.forClass(HearingAidStackEvent.class);
        verify(mService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                HearingAidStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
        assertThat(event.getValue().valueInt1).isEqualTo(BluetoothProfile.STATE_CONNECTED);

        Mockito.clearInvocations(mService);
        HearingAidService.setHearingAidService(null);
        mNativeInterface.onConnectionStateChanged(BluetoothProfile.STATE_CONNECTED,
                mNativeInterface.getByteAddress(device));
        verify(mService, never()).messageFromNative(any());
    }

    @Test
    public void onDeviceAvailable() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        byte capabilities = 0;
        long hiSyncId = 100;
        mNativeInterface.onDeviceAvailable(capabilities, hiSyncId,
                mNativeInterface.getByteAddress(device));

        ArgumentCaptor<HearingAidStackEvent> event =
                ArgumentCaptor.forClass(HearingAidStackEvent.class);
        verify(mService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                HearingAidStackEvent.EVENT_TYPE_DEVICE_AVAILABLE);
        assertThat(event.getValue().valueInt1).isEqualTo(capabilities);
        assertThat(event.getValue().valueLong2).isEqualTo(hiSyncId);

        Mockito.clearInvocations(mService);
        HearingAidService.setHearingAidService(null);
        mNativeInterface.onDeviceAvailable(capabilities, hiSyncId,
                mNativeInterface.getByteAddress(device));
        verify(mService, never()).messageFromNative(any());
    }
}