From f551c128658608d51517a7897855f3ddb5d8e895 Mon Sep 17 00:00:00 2001 From: Weilin Xu Date: Fri, 7 Oct 2022 22:22:02 +0000 Subject: [PATCH] Add unit tests for ProgramInfo and Announcement Bug: 249602599 Test: atest RadioManagerTest RadioAnnouncementTest Change-Id: I19920151701b25e34a1c82af073759d50e3bcc77 --- .../android/hardware/radio/Announcement.java | 6 +- .../unittests/RadioAnnouncementTest.java | 86 ++++++++++ .../tests/unittests/RadioManagerTest.java | 158 ++++++++++++++++++ 3 files changed, 247 insertions(+), 3 deletions(-) create mode 100644 core/tests/BroadcastRadioTests/src/android/hardware/radio/tests/unittests/RadioAnnouncementTest.java diff --git a/core/java/android/hardware/radio/Announcement.java b/core/java/android/hardware/radio/Announcement.java index 8febed3fb2a07..3ba3ebceeb187 100644 --- a/core/java/android/hardware/radio/Announcement.java +++ b/core/java/android/hardware/radio/Announcement.java @@ -85,9 +85,9 @@ public final class Announcement implements Parcelable { /** @hide */ public Announcement(@NonNull ProgramSelector selector, @Type int type, @NonNull Map vendorInfo) { - mSelector = Objects.requireNonNull(selector); - mType = Objects.requireNonNull(type); - mVendorInfo = Objects.requireNonNull(vendorInfo); + mSelector = Objects.requireNonNull(selector, "Program selector cannot be null"); + mType = type; + mVendorInfo = Objects.requireNonNull(vendorInfo, "Vendor info cannot be null"); } private Announcement(@NonNull Parcel in) { diff --git a/core/tests/BroadcastRadioTests/src/android/hardware/radio/tests/unittests/RadioAnnouncementTest.java b/core/tests/BroadcastRadioTests/src/android/hardware/radio/tests/unittests/RadioAnnouncementTest.java new file mode 100644 index 0000000000000..42143b92e9d8a --- /dev/null +++ b/core/tests/BroadcastRadioTests/src/android/hardware/radio/tests/unittests/RadioAnnouncementTest.java @@ -0,0 +1,86 @@ +/* + * 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.hardware.radio.Announcement; +import android.hardware.radio.ProgramSelector; +import android.util.ArrayMap; + +import org.junit.Test; + +import java.util.Map; + +public final class RadioAnnouncementTest { + private static final ProgramSelector.Identifier FM_IDENTIFIER = new ProgramSelector.Identifier( + ProgramSelector.IDENTIFIER_TYPE_AMFM_FREQUENCY, /* value= */ 90500); + private static final ProgramSelector FM_PROGRAM_SELECTOR = new ProgramSelector( + ProgramSelector.PROGRAM_TYPE_FM, FM_IDENTIFIER, /* secondaryIds= */ null, + /* vendorIds= */ null); + private static final int TRAFFIC_ANNOUNCEMENT_TYPE = Announcement.TYPE_TRAFFIC; + private static final Map VENDOR_INFO = createVendorInfo(); + private static final Announcement TEST_ANNOUNCEMENT = + new Announcement(FM_PROGRAM_SELECTOR, TRAFFIC_ANNOUNCEMENT_TYPE, VENDOR_INFO); + + @Test + public void constructor_withNullSelector_fails() { + NullPointerException thrown = assertThrows(NullPointerException.class, () -> { + new Announcement(/* selector= */ null, TRAFFIC_ANNOUNCEMENT_TYPE, VENDOR_INFO); + }); + + assertWithMessage("Exception for null program selector in announcement constructor") + .that(thrown).hasMessageThat().contains("Program selector cannot be null"); + } + + @Test + public void constructor_withNullVendorInfo_fails() { + NullPointerException thrown = assertThrows(NullPointerException.class, () -> { + new Announcement(FM_PROGRAM_SELECTOR, TRAFFIC_ANNOUNCEMENT_TYPE, + /* vendorInfo= */ null); + }); + + assertWithMessage("Exception for null vendor info in announcement constructor") + .that(thrown).hasMessageThat().contains("Vendor info cannot be null"); + } + + @Test + public void getSelector() { + assertWithMessage("Radio announcement selector") + .that(TEST_ANNOUNCEMENT.getSelector()).isEqualTo(FM_PROGRAM_SELECTOR); + } + + @Test + public void getType() { + assertWithMessage("Radio announcement type") + .that(TEST_ANNOUNCEMENT.getType()).isEqualTo(TRAFFIC_ANNOUNCEMENT_TYPE); + } + + @Test + public void getVendorInfo() { + assertWithMessage("Radio announcement vendor info") + .that(TEST_ANNOUNCEMENT.getVendorInfo()).isEqualTo(VENDOR_INFO); + } + + private static Map createVendorInfo() { + Map vendorInfo = new ArrayMap<>(); + vendorInfo.put("vendorKeyMock", "vendorValueMock"); + return vendorInfo; + } +} diff --git a/core/tests/BroadcastRadioTests/src/android/hardware/radio/tests/unittests/RadioManagerTest.java b/core/tests/BroadcastRadioTests/src/android/hardware/radio/tests/unittests/RadioManagerTest.java index 259a118527846..9bfa2fba6948e 100644 --- a/core/tests/BroadcastRadioTests/src/android/hardware/radio/tests/unittests/RadioManagerTest.java +++ b/core/tests/BroadcastRadioTests/src/android/hardware/radio/tests/unittests/RadioManagerTest.java @@ -20,9 +20,12 @@ import static com.google.common.truth.Truth.assertWithMessage; import android.hardware.radio.ProgramSelector; import android.hardware.radio.RadioManager; +import android.hardware.radio.RadioMetadata; import org.junit.Test; +import java.util.Arrays; + public final class RadioManagerTest { private static final int REGION = RadioManager.REGION_ITU_2; @@ -63,6 +66,32 @@ public final class RadioManagerTest { private static final RadioManager.AmBandConfig AM_BAND_CONFIG = createAmBandConfig(); private static final RadioManager.ModuleProperties AMFM_PROPERTIES = createAmFmProperties(); + /** + * Info flags with live, tuned and stereo enabled + */ + private static final int INFO_FLAGS = 0b110001; + private static final int SIGNAL_QUALITY = 2; + private static final ProgramSelector.Identifier DAB_SID_EXT_IDENTIFIER = + new ProgramSelector.Identifier(ProgramSelector.IDENTIFIER_TYPE_DAB_SID_EXT, + /* value= */ 0x10000111); + private static final ProgramSelector.Identifier DAB_SID_EXT_IDENTIFIER_RELATED = + new ProgramSelector.Identifier(ProgramSelector.IDENTIFIER_TYPE_DAB_SID_EXT, + /* value= */ 0x10000113); + private static final ProgramSelector.Identifier DAB_ENSEMBLE_IDENTIFIER = + new ProgramSelector.Identifier(ProgramSelector.IDENTIFIER_TYPE_DAB_ENSEMBLE, + /* value= */ 0x1013); + private static final ProgramSelector.Identifier DAB_FREQUENCY_IDENTIFIER = + new ProgramSelector.Identifier(ProgramSelector.IDENTIFIER_TYPE_DAB_FREQUENCY, + /* value= */ 95500); + private static final ProgramSelector DAB_SELECTOR = + new ProgramSelector(ProgramSelector.PROGRAM_TYPE_DAB, DAB_SID_EXT_IDENTIFIER, + new ProgramSelector.Identifier[]{ + DAB_ENSEMBLE_IDENTIFIER, DAB_FREQUENCY_IDENTIFIER}, + /* vendorIds= */ null); + private static final RadioMetadata METADATA = createMetadata(); + private static final RadioManager.ProgramInfo DAB_PROGRAM_INFO = + createDabProgramInfo(DAB_SELECTOR); + @Test public void getType_forBandDescriptor() { RadioManager.BandDescriptor bandDescriptor = createAmBandDescriptor(); @@ -460,6 +489,123 @@ public final class RadioManagerTest { .that(AMFM_PROPERTIES).isNotEqualTo(propertiesDab); } + @Test + public void getSelector_forProgramInfo() { + assertWithMessage("Selector of DAB program info") + .that(DAB_PROGRAM_INFO.getSelector()).isEqualTo(DAB_SELECTOR); + } + + @Test + public void getLogicallyTunedTo_forProgramInfo() { + assertWithMessage("Identifier logically tuned to in DAB program info") + .that(DAB_PROGRAM_INFO.getLogicallyTunedTo()).isEqualTo(DAB_FREQUENCY_IDENTIFIER); + } + + @Test + public void getPhysicallyTunedTo_forProgramInfo() { + assertWithMessage("Identifier physically tuned to DAB program info") + .that(DAB_PROGRAM_INFO.getPhysicallyTunedTo()).isEqualTo(DAB_SID_EXT_IDENTIFIER); + } + + @Test + public void getRelatedContent_forProgramInfo() { + assertWithMessage("Related contents of DAB program info") + .that(DAB_PROGRAM_INFO.getRelatedContent()) + .containsExactly(DAB_SID_EXT_IDENTIFIER_RELATED); + } + + @Test + public void getChannel_forProgramInfo() { + assertWithMessage("Main channel of DAB program info") + .that(DAB_PROGRAM_INFO.getChannel()).isEqualTo(0); + } + + @Test + public void getSubChannel_forProgramInfo() { + assertWithMessage("Sub channel of DAB program info") + .that(DAB_PROGRAM_INFO.getSubChannel()).isEqualTo(0); + } + + @Test + public void isTuned_forProgramInfo() { + assertWithMessage("Tuned status of DAB program info") + .that(DAB_PROGRAM_INFO.isTuned()).isTrue(); + } + + @Test + public void isStereo_forProgramInfo() { + assertWithMessage("Stereo support in DAB program info") + .that(DAB_PROGRAM_INFO.isStereo()).isTrue(); + } + + @Test + public void isDigital_forProgramInfo() { + assertWithMessage("Digital DAB program info") + .that(DAB_PROGRAM_INFO.isDigital()).isTrue(); + } + + @Test + public void isLive_forProgramInfo() { + assertWithMessage("Live status of DAB program info") + .that(DAB_PROGRAM_INFO.isLive()).isTrue(); + } + + @Test + public void isMuted_forProgramInfo() { + assertWithMessage("Muted status of DAB program info") + .that(DAB_PROGRAM_INFO.isMuted()).isFalse(); + } + + @Test + public void isTrafficProgram_forProgramInfo() { + assertWithMessage("Traffic program support in DAB program info") + .that(DAB_PROGRAM_INFO.isTrafficProgram()).isFalse(); + } + + @Test + public void isTrafficAnnouncementActive_forProgramInfo() { + assertWithMessage("Active traffic announcement for DAB program info") + .that(DAB_PROGRAM_INFO.isTrafficAnnouncementActive()).isFalse(); + } + + @Test + public void getSignalStrength_forProgramInfo() { + assertWithMessage("Signal strength of DAB program info") + .that(DAB_PROGRAM_INFO.getSignalStrength()).isEqualTo(SIGNAL_QUALITY); + } + + @Test + public void getMetadata_forProgramInfo() { + assertWithMessage("Metadata of DAB program info") + .that(DAB_PROGRAM_INFO.getMetadata()).isEqualTo(METADATA); + } + + @Test + public void getVendorInfo_forProgramInfo() { + assertWithMessage("Vendor info of DAB program info") + .that(DAB_PROGRAM_INFO.getVendorInfo()).isEmpty(); + } + + @Test + public void equals_withSameProgramInfo_returnsTrue() { + RadioManager.ProgramInfo dabProgramInfoCompared = createDabProgramInfo(DAB_SELECTOR); + + assertWithMessage("The same program info") + .that(dabProgramInfoCompared).isEqualTo(DAB_PROGRAM_INFO); + } + + @Test + public void equals_withSameProgramInfoOfDifferentSecondaryIdSelectors_returnsFalse() { + ProgramSelector dabSelectorCompared = new ProgramSelector( + ProgramSelector.PROGRAM_TYPE_DAB, DAB_SID_EXT_IDENTIFIER, + new ProgramSelector.Identifier[]{DAB_FREQUENCY_IDENTIFIER}, + /* vendorIds= */ null); + RadioManager.ProgramInfo dabProgramInfoCompared = createDabProgramInfo(dabSelectorCompared); + + assertWithMessage("Program info with different secondary id selectors") + .that(DAB_PROGRAM_INFO).isNotEqualTo(dabProgramInfoCompared); + } + private static RadioManager.ModuleProperties createAmFmProperties() { return new RadioManager.ModuleProperties(PROPERTIES_ID, SERVICE_NAME, CLASS_ID, IMPLEMENTOR, PRODUCT, VERSION, SERIAL, NUM_TUNERS, NUM_AUDIO_SOURCES, @@ -487,4 +633,16 @@ public final class RadioManagerTest { private static RadioManager.AmBandConfig createAmBandConfig() { return new RadioManager.AmBandConfig(createAmBandDescriptor()); } + + private static RadioMetadata createMetadata() { + RadioMetadata.Builder metadataBuilder = new RadioMetadata.Builder(); + return metadataBuilder.putString(RadioMetadata.METADATA_KEY_ARTIST, "artistTest").build(); + } + + private static RadioManager.ProgramInfo createDabProgramInfo(ProgramSelector selector) { + return new RadioManager.ProgramInfo(selector, DAB_FREQUENCY_IDENTIFIER, + DAB_SID_EXT_IDENTIFIER, Arrays.asList(DAB_SID_EXT_IDENTIFIER_RELATED), INFO_FLAGS, + SIGNAL_QUALITY, METADATA, /* vendorInfo= */ null); + } + }