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

Commit d1130c81 authored by Hyundo Moon's avatar Hyundo Moon
Browse files

Add AvrcpVolumeManagerTest

Also adds some test methods in BatteryServiceTest.

Bug: 237467631
Test: atest AvrcpVolumeManagerTest;
      atest BatteryServiceTest;
Change-Id: I01992e5f289bce2dfeaabfe644efdcc507a8555a
parent aea515f1
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.media.AudioManager;
import android.util.Log;

import com.android.bluetooth.audio_util.BTAudioEventLogger;
import com.android.internal.annotations.VisibleForTesting;

import java.util.HashMap;
import java.util.Map;
@@ -42,7 +43,9 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
    private static final String VOLUME_MAP = "bluetooth_volume_map";
    private static final String VOLUME_REJECTLIST = "absolute_volume_rejectlist";
    private static final String VOLUME_CHANGE_LOG_TITLE = "Volume Events";
    private static final int AVRCP_MAX_VOL = 127;

    @VisibleForTesting
    static final int AVRCP_MAX_VOL = 127;
    private static final int STREAM_MUSIC = AudioManager.STREAM_MUSIC;
    private static final int VOLUME_CHANGE_LOGGER_SIZE = 30;
    private static int sDeviceMaxVolume = 0;
+104 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 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.avrcp;

import static com.android.bluetooth.avrcp.AvrcpVolumeManager.AVRCP_MAX_VOL;

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

import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.eq;
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 android.media.AudioManager;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
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.Mock;
import org.mockito.MockitoAnnotations;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class AvrcpVolumeManagerTest {
    private static final String REMOTE_DEVICE_ADDRESS = "00:01:02:03:04:05";
    private static final int TEST_DEVICE_MAX_VOUME = 25;

    @Mock
    AvrcpNativeInterface mNativeInterface;

    @Mock
    AudioManager mAudioManager;

    Context mContext;
    BluetoothDevice mRemoteDevice;
    AvrcpVolumeManager mAvrcpVolumeManager;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mContext = InstrumentationRegistry.getTargetContext();
        when(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC))
                .thenReturn(TEST_DEVICE_MAX_VOUME);
        mRemoteDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(REMOTE_DEVICE_ADDRESS);
        mAvrcpVolumeManager = new AvrcpVolumeManager(mContext, mAudioManager, mNativeInterface);
    }

    @After
    public void tearDown() throws Exception {
        mAvrcpVolumeManager.removeStoredVolumeForDevice(mRemoteDevice);
    }

    @Test
    public void avrcpToSystemVolume() {
        assertThat(AvrcpVolumeManager.avrcpToSystemVolume(0)).isEqualTo(0);
        assertThat(AvrcpVolumeManager.avrcpToSystemVolume(AVRCP_MAX_VOL))
                .isEqualTo(TEST_DEVICE_MAX_VOUME);
    }

    @Test
    public void dump() {
        StringBuilder sb = new StringBuilder();
        mAvrcpVolumeManager.dump(sb);

        assertThat(sb.toString()).isNotEmpty();
    }

    @Test
    public void sendVolumeChanged() {
        mAvrcpVolumeManager.sendVolumeChanged(mRemoteDevice, TEST_DEVICE_MAX_VOUME);

        verify(mNativeInterface).sendVolumeChanged(REMOTE_DEVICE_ADDRESS, AVRCP_MAX_VOL);
    }

    @Test
    public void setVolume() {
        mAvrcpVolumeManager.setVolume(mRemoteDevice, AVRCP_MAX_VOL);

        verify(mAudioManager).setStreamVolume(eq(AudioManager.STREAM_MUSIC),
                eq(TEST_DEVICE_MAX_VOUME), anyInt());
    }
}
+25 −0
Original line number Diff line number Diff line
@@ -242,6 +242,31 @@ public class BatteryServiceTest {
        Assert.assertFalse("Connect expected to fail", mService.connect(mDevice));
    }

    @Test
    public void getConnectionState_whenNoDevicesAreConnected_returnsDisconnectedState() {
        Assert.assertEquals(mService.getConnectionState(mDevice),
                BluetoothProfile.STATE_DISCONNECTED);
    }

    @Test
    public void getDevices_whenNoDevicesAreConnected_returnsEmptyList() {
        Assert.assertTrue(mService.getDevices().isEmpty());
    }

    @Test
    public void getDevicesMatchingConnectionStates() {
        when(mAdapterService.getBondedDevices()).thenReturn(new BluetoothDevice[] {mDevice});
        int states[] = new int[] {BluetoothProfile.STATE_DISCONNECTED};

        Assert.assertTrue(mService.getDevicesMatchingConnectionStates(states).contains(mDevice));
    }

    @Test
    public void setConnectionPolicy() {
        Assert.assertTrue(mService.setConnectionPolicy(
                mDevice, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN));
    }

    /**
     *  Helper function to test okToConnect() method
     *