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

Commit ed57f862 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "vc: Move VolumeControlOffsetDescriptor to separate file" into main am:...

Merge "vc: Move VolumeControlOffsetDescriptor to separate file" into main am: 5bf1a095 am: 1d8b1789

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/3251469



Change-Id: Iacf48264b20968079a6910e3980f089c1d2588c6
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents a20dd3e4 1d8b1789
Loading
Loading
Loading
Loading
+116 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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 com.android.bluetooth.btservice.ProfileService;

import java.util.HashMap;
import java.util.Map;

/*
 * Class representing Volume Control Offset on the remote device.
 * This is internal class for the VolumeControlService
 */
class VolumeControlOffsetDescriptor {
    final Map<Integer, Descriptor> mVolumeOffsets = new HashMap<>();

    private static class Descriptor {
        int mValue = 0;
        int mLocation = 0;
        String mDescription = null;
    }

    int size() {
        return mVolumeOffsets.size();
    }

    void add(int id) {
        if (!mVolumeOffsets.containsKey(id)) {
            mVolumeOffsets.put(id, new Descriptor());
        }
    }

    boolean setValue(int id, int value) {
        Descriptor desc = mVolumeOffsets.get(id);
        if (desc == null) {
            return false;
        }
        desc.mValue = value;
        return true;
    }

    int getValue(int id) {
        Descriptor desc = mVolumeOffsets.get(id);
        if (desc == null) {
            return 0;
        }
        return desc.mValue;
    }

    boolean setDescription(int id, String description) {
        Descriptor desc = mVolumeOffsets.get(id);
        if (desc == null) {
            return false;
        }
        desc.mDescription = description;
        return true;
    }

    String getDescription(int id) {
        Descriptor desc = mVolumeOffsets.get(id);
        if (desc == null) {
            return null;
        }
        return desc.mDescription;
    }

    boolean setLocation(int id, int location) {
        Descriptor desc = mVolumeOffsets.get(id);
        if (desc == null) {
            return false;
        }
        desc.mLocation = location;
        return true;
    }

    int getLocation(int id) {
        Descriptor desc = mVolumeOffsets.get(id);
        if (desc == null) {
            return 0;
        }
        return desc.mLocation;
    }

    void remove(int id) {
        mVolumeOffsets.remove(id);
    }

    void clear() {
        mVolumeOffsets.clear();
    }

    void dump(StringBuilder sb) {
        for (Map.Entry<Integer, Descriptor> entry : mVolumeOffsets.entrySet()) {
            Descriptor descriptor = entry.getValue();
            Integer id = entry.getKey();
            ProfileService.println(sb, "        Id: " + id);
            ProfileService.println(sb, "        value: " + descriptor.mValue);
            ProfileService.println(sb, "        location: " + descriptor.mLocation);
            ProfileService.println(sb, "        description: " + descriptor.mDescription);
        }
    }
}
+0 −103
Original line number Diff line number Diff line
@@ -87,109 +87,6 @@ public class VolumeControlService extends ProfileService {
    final RemoteCallbackList<IBluetoothVolumeControlCallback> mCallbacks =
            new RemoteCallbackList<>();

    @VisibleForTesting
    static class VolumeControlOffsetDescriptor {
        Map<Integer, Descriptor> mVolumeOffsets;

        private static class Descriptor {
            Descriptor() {
                mValue = 0;
                mLocation = 0;
                mDescription = null;
            }

            int mValue;
            int mLocation;
            String mDescription;
        }
        ;

        VolumeControlOffsetDescriptor() {
            mVolumeOffsets = new HashMap<>();
        }

        int size() {
            return mVolumeOffsets.size();
        }

        void add(int id) {
            Descriptor d = mVolumeOffsets.get(id);
            if (d == null) {
                mVolumeOffsets.put(id, new Descriptor());
            }
        }

        boolean setValue(int id, int value) {
            Descriptor d = mVolumeOffsets.get(id);
            if (d == null) {
                return false;
            }
            d.mValue = value;
            return true;
        }

        int getValue(int id) {
            Descriptor d = mVolumeOffsets.get(id);
            if (d == null) {
                return 0;
            }
            return d.mValue;
        }

        boolean setDescription(int id, String desc) {
            Descriptor d = mVolumeOffsets.get(id);
            if (d == null) {
                return false;
            }
            d.mDescription = desc;
            return true;
        }

        String getDescription(int id) {
            Descriptor d = mVolumeOffsets.get(id);
            if (d == null) {
                return null;
            }
            return d.mDescription;
        }

        boolean setLocation(int id, int location) {
            Descriptor d = mVolumeOffsets.get(id);
            if (d == null) {
                return false;
            }
            d.mLocation = location;
            return true;
        }

        int getLocation(int id) {
            Descriptor d = mVolumeOffsets.get(id);
            if (d == null) {
                return 0;
            }
            return d.mLocation;
        }

        void remove(int id) {
            mVolumeOffsets.remove(id);
        }

        void clear() {
            mVolumeOffsets.clear();
        }

        void dump(StringBuilder sb) {
            for (Map.Entry<Integer, Descriptor> entry : mVolumeOffsets.entrySet()) {
                Descriptor descriptor = entry.getValue();
                Integer id = entry.getKey();
                ProfileService.println(sb, "        Id: " + id);
                ProfileService.println(sb, "        value: " + descriptor.mValue);
                ProfileService.println(sb, "        location: " + descriptor.mLocation);
                ProfileService.println(sb, "        description: " + descriptor.mDescription);
            }
        }
    }

    VolumeControlNativeInterface mVolumeControlNativeInterface;
    @VisibleForTesting AudioManager mAudioManager;

+127 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.*;

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

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

@MediumTest
@RunWith(AndroidJUnit4.class)
public class VolumeControlOffsetDescriptorTest {

    @Before
    public void setUp() throws Exception {
        // placeholder
    }

    @After
    public void tearDown() throws Exception {
        // placeholder
    }

    @Test
    public void testVolumeControlOffsetDescriptorInvalidIdOperations() {
        VolumeControlOffsetDescriptor descriptor = new VolumeControlOffsetDescriptor();
        int invalidId = -1;
        int testValue = 100;
        String testDesc = "testDescription";
        int testLocation = 10000;

        // Verify API operations on invalid ID
        assertThat(descriptor.setValue(invalidId, testValue)).isFalse();
        assertThat(descriptor.getValue(invalidId)).isEqualTo(0);
        assertThat(descriptor.setDescription(invalidId, testDesc)).isFalse();

        assertThat(descriptor.getDescription(invalidId)).isNull();
        assertThat(descriptor.setLocation(invalidId, testLocation)).isFalse();

        assertThat(descriptor.getLocation(invalidId)).isEqualTo(0);
    }

    @Test
    public void testVolumeControlOffsetDescriptorMultipleInstanceAdded() {
        VolumeControlOffsetDescriptor descriptor = new VolumeControlOffsetDescriptor();

        int validId = 10;

        // Verify adding input
        assertThat(descriptor.size()).isEqualTo(0);
        descriptor.add(validId);
        assertThat(descriptor.size()).isEqualTo(1);

        // Verify adding same instance will not increase number of descriptors
        descriptor.add(validId);
        assertThat(descriptor.size()).isEqualTo(1);
    }

    @Test
    public void testVolumeControlOffsetDescriptorInstanceRemoveAndClear() {
        VolumeControlOffsetDescriptor descriptor = new VolumeControlOffsetDescriptor();
        int id_1 = 10;
        int id_2 = 20;
        int invalidId = 1;

        descriptor.add(id_1);
        descriptor.add(id_2);
        assertThat(descriptor.size()).isEqualTo(2);

        // Check remove api
        descriptor.remove(id_1);
        assertThat(descriptor.size()).isEqualTo(1);

        // Check remove api with invalid id
        descriptor.remove(invalidId);
        assertThat(descriptor.size()).isEqualTo(1);

        // Check clear API
        descriptor.clear();
        assertThat(descriptor.size()).isEqualTo(0);
    }

    @Test
    public void testVolumeControlOffsetDescriptorAllValidApiCalls() {
        VolumeControlOffsetDescriptor descriptor = new VolumeControlOffsetDescriptor();

        int validId = 10;
        int testValue = 100;
        String testDesc = "testDescription";
        int testLocation = 10000;

        descriptor.add(validId);

        // Verify operations on valid id
        assertThat(descriptor.setValue(validId, testValue)).isTrue();
        assertThat(descriptor.getValue(validId)).isEqualTo(testValue);
        assertThat(descriptor.setDescription(validId, testDesc)).isTrue();
        assertThat(descriptor.getDescription(validId)).isEqualTo(testDesc);
        assertThat(descriptor.setLocation(validId, testLocation)).isTrue();
        assertThat(descriptor.getLocation(validId)).isEqualTo(testLocation);

        // Verify debug dump
        StringBuilder sb = new StringBuilder();
        descriptor.dump(sb);
        assertThat(sb.toString().contains(testDesc)).isTrue();
    }
}
+0 −41
Original line number Diff line number Diff line
@@ -1446,47 +1446,6 @@ public class VolumeControlServiceTest {
        verify(mNativeInterface).unmuteGroup(groupId);
    }

    @Test
    public void testVolumeControlOffsetDescriptor() {
        VolumeControlService.VolumeControlOffsetDescriptor descriptor =
                new VolumeControlService.VolumeControlOffsetDescriptor();
        int invalidId = -1;
        int validId = 10;
        int testValue = 100;
        String testDesc = "testDescription";
        int testLocation = 10000;

        Assert.assertEquals(0, descriptor.size());
        descriptor.add(validId);
        Assert.assertEquals(1, descriptor.size());

        Assert.assertFalse(descriptor.setValue(invalidId, testValue));
        Assert.assertTrue(descriptor.setValue(validId, testValue));
        Assert.assertEquals(0, descriptor.getValue(invalidId));
        Assert.assertEquals(testValue, descriptor.getValue(validId));

        Assert.assertFalse(descriptor.setDescription(invalidId, testDesc));
        Assert.assertTrue(descriptor.setDescription(validId, testDesc));
        Assert.assertEquals(null, descriptor.getDescription(invalidId));
        Assert.assertEquals(testDesc, descriptor.getDescription(validId));

        Assert.assertFalse(descriptor.setLocation(invalidId, testLocation));
        Assert.assertTrue(descriptor.setLocation(validId, testLocation));
        Assert.assertEquals(0, descriptor.getLocation(invalidId));
        Assert.assertEquals(testLocation, descriptor.getLocation(validId));

        StringBuilder sb = new StringBuilder();
        descriptor.dump(sb);
        Assert.assertTrue(sb.toString().contains(testDesc));

        descriptor.add(validId + 1);
        Assert.assertEquals(2, descriptor.size());
        descriptor.remove(validId);
        Assert.assertEquals(1, descriptor.size());
        descriptor.clear();
        Assert.assertEquals(0, descriptor.size());
    }

    @Test
    public void testDump_doesNotCrash() throws Exception {
        connectDevice(mDevice);