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

Commit d8b55d4a authored by Weilin Xu's avatar Weilin Xu Committed by Android (Google) Code Review
Browse files

Merge "Add unit test for public methods in radio tuner"

parents 657812fb 9d2260ab
Loading
Loading
Loading
Loading
+171 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 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 android.hardware.radio.tests.unittests;

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

import static org.junit.Assert.assertThrows;

import android.graphics.Bitmap;
import android.hardware.radio.ProgramList;
import android.hardware.radio.ProgramSelector;
import android.hardware.radio.RadioManager;
import android.hardware.radio.RadioTuner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public final class DefaultRadioTunerTest {

    private static final RadioTuner DEFAULT_RADIO_TUNER = new RadioTuner() {
        @Override
        public void close() {}

        @Override
        public int setConfiguration(RadioManager.BandConfig config) {
            return 0;
        }

        @Override
        public int getConfiguration(RadioManager.BandConfig[] config) {
            return 0;
        }

        @Override
        public int setMute(boolean mute) {
            return 0;
        }

        @Override
        public boolean getMute() {
            return false;
        }

        @Override
        public int step(int direction, boolean skipSubChannel) {
            return 0;
        }

        @Override
        public int scan(int direction, boolean skipSubChannel) {
            return 0;
        }

        @Override
        public int tune(int channel, int subChannel) {
            return 0;
        }

        @Override
        public void tune(@NonNull ProgramSelector selector) {}

        @Override
        public int cancel() {
            return 0;
        }

        @Override
        public void cancelAnnouncement() {}

        @Override
        public int getProgramInformation(RadioManager.ProgramInfo[] info) {
            return 0;
        }

        @Nullable
        @Override
        public Bitmap getMetadataImage(int id) {
            return null;
        }

        @Override
        public boolean startBackgroundScan() {
            return false;
        }

        @NonNull
        @Override
        public List<RadioManager.ProgramInfo> getProgramList(
                @Nullable Map<String, String> vendorFilter) {
            return new ArrayList<>();
        }

        @Override
        public boolean isAnalogForced() {
            return false;
        }

        @Override
        public void setAnalogForced(boolean isForced) {}

        @Override
        public boolean isAntennaConnected() {
            return false;
        }

        @Override
        public boolean hasControl() {
            return false;
        }
    };

    @Test
    public void getDynamicProgramList_forRadioTuner_returnsNull() {
        assertWithMessage("Dynamic program list obtained from default radio tuner")
                .that(DEFAULT_RADIO_TUNER.getDynamicProgramList(new ProgramList.Filter())).isNull();
    }

    @Test
    public void isConfigFlagSupported_forRadioTuner_throwsException() {
        assertWithMessage("Dynamic program list obtained from default radio tuner")
                .that(DEFAULT_RADIO_TUNER.isConfigFlagSupported(/* flag= */ 1)).isFalse();
    }

    @Test
    public void isConfigFlagSet_forRadioTuner_throwsException() {
        assertThrows(UnsupportedOperationException.class, () -> {
            DEFAULT_RADIO_TUNER.isConfigFlagSet(/* flag= */ 1);
        });
    }

    @Test
    public void setConfigFlag_forRadioTuner_throwsException() {
        assertThrows(UnsupportedOperationException.class, () -> {
            DEFAULT_RADIO_TUNER.setConfigFlag(/* flag= */ 1, /* value= */ false);
        });
    }

    @Test
    public void setParameters_forRadioTuner_throwsException() {
        assertThrows(UnsupportedOperationException.class, () -> {
            DEFAULT_RADIO_TUNER.setParameters(Map.of("testKey", "testValue"));
        });
    }

    @Test
    public void getParameters_forRadioTuner_throwsException() {
        assertThrows(UnsupportedOperationException.class, () -> {
            DEFAULT_RADIO_TUNER.getParameters(List.of("testKey"));
        });
    }
}
+19 −2
Original line number Diff line number Diff line
@@ -360,17 +360,34 @@ public final class ProgramListTest {
    }

    @Test
    public void registerListCallback_withExecutor() throws Exception {
    public void onItemChanged_forListCallbackRegisteredWithExecutor_invokesWhenIdAdded()
            throws Exception {
        createRadioTuner();
        mProgramList = mRadioTuner.getDynamicProgramList(TEST_FILTER);
        ProgramList.ListCallback listCallbackMock = mock(ProgramList.ListCallback.class);

        mProgramList.registerListCallback(mExecutor, listCallbackMock);

        mTunerCallback.onProgramListUpdated(FM_ADD_INCOMPLETE_CHUNK);

        verify(listCallbackMock, CALLBACK_TIMEOUT).onItemChanged(FM_IDENTIFIER);
    }

    @Test
    public void onItemRemoved_forListCallbackRegisteredWithExecutor_invokesWhenIdRemoved()
            throws Exception {
        ProgramList.Chunk purgeChunk = new ProgramList.Chunk(/* purge= */ true,
                /* complete= */ true, new ArraySet<>(), new ArraySet<>());
        createRadioTuner();
        mProgramList = mRadioTuner.getDynamicProgramList(TEST_FILTER);
        ProgramList.ListCallback listCallbackMock = mock(ProgramList.ListCallback.class);
        mProgramList.registerListCallback(mExecutor, listCallbackMock);
        mTunerCallback.onProgramListUpdated(FM_ADD_INCOMPLETE_CHUNK);

        mTunerCallback.onProgramListUpdated(purgeChunk);

        verify(listCallbackMock, CALLBACK_TIMEOUT).onItemRemoved(FM_IDENTIFIER);
    }

    @Test
    public void onProgramListUpdated_withMultipleListCallBacks() throws Exception {
        int numCallbacks = 3;