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

Commit ed742dd0 authored by Sungsoo Lim's avatar Sungsoo Lim Committed by Automerger Merge Worker
Browse files

Merge "Add VolumeControlNativeInterfaceTest" am: 48196184

parents 57611b29 48196184
Loading
Loading
Loading
Loading
+14 −8
Original line number Original line Diff line number Diff line
@@ -260,8 +260,8 @@ public class VolumeControlNativeInterface {
    // Callbacks from the native stack back into the Java framework.
    // Callbacks from the native stack back into the Java framework.
    // All callbacks are routed via the Service which will disambiguate which
    // All callbacks are routed via the Service which will disambiguate which
    // state machine the message should be routed to.
    // state machine the message should be routed to.

    @VisibleForTesting
    private void onConnectionStateChanged(int state, byte[] address) {
    void onConnectionStateChanged(int state, byte[] address) {
        VolumeControlStackEvent event =
        VolumeControlStackEvent event =
                new VolumeControlStackEvent(
                new VolumeControlStackEvent(
                        VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
                        VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
@@ -274,7 +274,8 @@ public class VolumeControlNativeInterface {
        sendMessageToService(event);
        sendMessageToService(event);
    }
    }


    private void onVolumeStateChanged(int volume, boolean mute, byte[] address,
    @VisibleForTesting
    void onVolumeStateChanged(int volume, boolean mute, byte[] address,
            boolean isAutonomous) {
            boolean isAutonomous) {
        VolumeControlStackEvent event =
        VolumeControlStackEvent event =
                new VolumeControlStackEvent(
                new VolumeControlStackEvent(
@@ -291,7 +292,8 @@ public class VolumeControlNativeInterface {
        sendMessageToService(event);
        sendMessageToService(event);
    }
    }


    private void onGroupVolumeStateChanged(int volume, boolean mute, int groupId,
    @VisibleForTesting
    void onGroupVolumeStateChanged(int volume, boolean mute, int groupId,
            boolean isAutonomous) {
            boolean isAutonomous) {
        VolumeControlStackEvent event =
        VolumeControlStackEvent event =
                new VolumeControlStackEvent(
                new VolumeControlStackEvent(
@@ -308,7 +310,8 @@ public class VolumeControlNativeInterface {
        sendMessageToService(event);
        sendMessageToService(event);
    }
    }


    private void onDeviceAvailable(int numOfExternalOutputs,
    @VisibleForTesting
    void onDeviceAvailable(int numOfExternalOutputs,
                                   byte[] address) {
                                   byte[] address) {
        VolumeControlStackEvent event =
        VolumeControlStackEvent event =
                new VolumeControlStackEvent(
                new VolumeControlStackEvent(
@@ -322,7 +325,8 @@ public class VolumeControlNativeInterface {
        sendMessageToService(event);
        sendMessageToService(event);
    }
    }


    private void onExtAudioOutVolumeOffsetChanged(int externalOutputId, int offset,
    @VisibleForTesting
    void onExtAudioOutVolumeOffsetChanged(int externalOutputId, int offset,
                                               byte[] address) {
                                               byte[] address) {
        VolumeControlStackEvent event =
        VolumeControlStackEvent event =
                new VolumeControlStackEvent(
                new VolumeControlStackEvent(
@@ -337,7 +341,8 @@ public class VolumeControlNativeInterface {
        sendMessageToService(event);
        sendMessageToService(event);
    }
    }


    private void onExtAudioOutLocationChanged(int externalOutputId, int location,
    @VisibleForTesting
    void onExtAudioOutLocationChanged(int externalOutputId, int location,
                                               byte[] address) {
                                               byte[] address) {
        VolumeControlStackEvent event =
        VolumeControlStackEvent event =
                new VolumeControlStackEvent(
                new VolumeControlStackEvent(
@@ -352,7 +357,8 @@ public class VolumeControlNativeInterface {
        sendMessageToService(event);
        sendMessageToService(event);
    }
    }


    private void onExtAudioOutDescriptionChanged(int externalOutputId, String descr,
    @VisibleForTesting
    void onExtAudioOutDescriptionChanged(int externalOutputId, String descr,
                                               byte[] address) {
                                               byte[] address) {
        VolumeControlStackEvent event =
        VolumeControlStackEvent event =
                new VolumeControlStackEvent(
                new VolumeControlStackEvent(
+2 −1
Original line number Original line Diff line number Diff line
@@ -345,7 +345,8 @@ public class VolumeControlService extends ProfileService {
        return sVolumeControlService;
        return sVolumeControlService;
    }
    }


    private static synchronized void setVolumeControlService(VolumeControlService instance) {
    @VisibleForTesting
    static synchronized void setVolumeControlService(VolumeControlService instance) {
        if (DBG) {
        if (DBG) {
            Log.d(TAG, "setVolumeControlService(): set to: " + instance);
            Log.d(TAG, "setVolumeControlService(): set to: " + instance);
        }
        }
+159 −0
Original line number Original line 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.vc;

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

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

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;

import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;

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 VolumeControlNativeInterfaceTest {
    @Mock
    private VolumeControlService mService;

    private VolumeControlNativeInterface mNativeInterface;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(mService.isAvailable()).thenReturn(true);
        VolumeControlService.setVolumeControlService(mService);
        mNativeInterface = VolumeControlNativeInterface.getInstance();
    }

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

        mNativeInterface.onConnectionStateChanged(state, address);

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

    @Test
    public void onVolumeStateChanged() {
        int volume = 3;
        boolean mute = false;
        byte[] address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
        boolean isAutonomous = false;

        mNativeInterface.onVolumeStateChanged(volume, mute, address, isAutonomous);

        ArgumentCaptor<VolumeControlStackEvent> event =
                ArgumentCaptor.forClass(VolumeControlStackEvent.class);
        verify(mService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                VolumeControlStackEvent.EVENT_TYPE_VOLUME_STATE_CHANGED);
    }

    @Test
    public void onGroupVolumeStateChanged() {
        int volume = 3;
        boolean mute = false;
        int groupId = 1;
        boolean isAutonomous = false;

        mNativeInterface.onGroupVolumeStateChanged(volume, mute, groupId, isAutonomous);

        ArgumentCaptor<VolumeControlStackEvent> event =
                ArgumentCaptor.forClass(VolumeControlStackEvent.class);
        verify(mService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                VolumeControlStackEvent.EVENT_TYPE_VOLUME_STATE_CHANGED);
        assertThat(event.getValue().valueInt1).isEqualTo(groupId);
    }

    @Test
    public void onDeviceAvailable() {
        int numOfExternalOutputs = 3;
        byte[] address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };

        mNativeInterface.onDeviceAvailable(numOfExternalOutputs, address);

        ArgumentCaptor<VolumeControlStackEvent> event =
                ArgumentCaptor.forClass(VolumeControlStackEvent.class);
        verify(mService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                VolumeControlStackEvent.EVENT_TYPE_DEVICE_AVAILABLE);
    }
    @Test
    public void onExtAudioOutVolumeOffsetChanged() {
        int externalOutputId = 2;
        int offset = 0;
        byte[] address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };

        mNativeInterface.onExtAudioOutVolumeOffsetChanged(externalOutputId, offset, address);

        ArgumentCaptor<VolumeControlStackEvent> event =
                ArgumentCaptor.forClass(VolumeControlStackEvent.class);
        verify(mService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                VolumeControlStackEvent.EVENT_TYPE_EXT_AUDIO_OUT_VOL_OFFSET_CHANGED);
    }

    @Test
    public void onExtAudioOutLocationChanged() {
        int externalOutputId = 2;
        int location = 100;
        byte[] address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };

        mNativeInterface.onExtAudioOutLocationChanged(externalOutputId, location, address);

        ArgumentCaptor<VolumeControlStackEvent> event =
                ArgumentCaptor.forClass(VolumeControlStackEvent.class);
        verify(mService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                VolumeControlStackEvent.EVENT_TYPE_EXT_AUDIO_OUT_LOCATION_CHANGED);
    }

    @Test
    public void onExtAudioOutDescriptionChanged() {
        int externalOutputId = 2;
        String descr = "test-descr";
        byte[] address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };

        mNativeInterface.onExtAudioOutDescriptionChanged(externalOutputId, descr, address);

        ArgumentCaptor<VolumeControlStackEvent> event =
                ArgumentCaptor.forClass(VolumeControlStackEvent.class);
        verify(mService).messageFromNative(event.capture());
        assertThat(event.getValue().type).isEqualTo(
                VolumeControlStackEvent.EVENT_TYPE_EXT_AUDIO_OUT_DESCRIPTION_CHANGED);
    }
}