1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.hardware.radio;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import static org.junit.Assert.assertThrows;
22 
23 import android.os.Parcel;
24 import android.util.ArrayMap;
25 
26 import org.junit.Test;
27 
28 import java.util.Map;
29 
30 public final class RadioAnnouncementTest {
31     private static final ProgramSelector.Identifier FM_IDENTIFIER = new ProgramSelector.Identifier(
32             ProgramSelector.IDENTIFIER_TYPE_AMFM_FREQUENCY, /* value= */ 90500);
33     private static final ProgramSelector FM_PROGRAM_SELECTOR = new ProgramSelector(
34             ProgramSelector.PROGRAM_TYPE_FM, FM_IDENTIFIER, /* secondaryIds= */ null,
35             /* vendorIds= */ null);
36     private static final int TRAFFIC_ANNOUNCEMENT_TYPE = Announcement.TYPE_TRAFFIC;
37     private static final Map<String, String> VENDOR_INFO = createVendorInfo();
38     private static final Announcement TEST_ANNOUNCEMENT =
39             new Announcement(FM_PROGRAM_SELECTOR, TRAFFIC_ANNOUNCEMENT_TYPE, VENDOR_INFO);
40 
41     @Test
constructor_withNullSelector_fails()42     public void constructor_withNullSelector_fails() {
43         NullPointerException thrown = assertThrows(NullPointerException.class, () -> {
44             new Announcement(/* selector= */ null, TRAFFIC_ANNOUNCEMENT_TYPE, VENDOR_INFO);
45         });
46 
47         assertWithMessage("Exception for null program selector in announcement constructor")
48                 .that(thrown).hasMessageThat().contains("Program selector cannot be null");
49     }
50 
51     @Test
constructor_withNullVendorInfo_fails()52     public void constructor_withNullVendorInfo_fails() {
53         NullPointerException thrown = assertThrows(NullPointerException.class, () -> {
54             new Announcement(FM_PROGRAM_SELECTOR, TRAFFIC_ANNOUNCEMENT_TYPE,
55                     /* vendorInfo= */ null);
56         });
57 
58         assertWithMessage("Exception for null vendor info in announcement constructor")
59                 .that(thrown).hasMessageThat().contains("Vendor info cannot be null");
60     }
61 
62     @Test
getSelector()63     public void getSelector() {
64         assertWithMessage("Radio announcement selector")
65                 .that(TEST_ANNOUNCEMENT.getSelector()).isEqualTo(FM_PROGRAM_SELECTOR);
66     }
67 
68     @Test
getType()69     public void getType() {
70         assertWithMessage("Radio announcement type")
71                 .that(TEST_ANNOUNCEMENT.getType()).isEqualTo(TRAFFIC_ANNOUNCEMENT_TYPE);
72     }
73 
74     @Test
getVendorInfo()75     public void getVendorInfo() {
76         assertWithMessage("Radio announcement vendor info")
77                 .that(TEST_ANNOUNCEMENT.getVendorInfo()).isEqualTo(VENDOR_INFO);
78     }
79 
createVendorInfo()80     private static Map<String, String> createVendorInfo() {
81         Map<String, String> vendorInfo = new ArrayMap<>();
82         vendorInfo.put("vendorKeyMock", "vendorValueMock");
83         return vendorInfo;
84     }
85 
86     @Test
describeContents_forAnnouncement()87     public void describeContents_forAnnouncement() {
88         assertWithMessage("Radio announcement contents")
89                 .that(TEST_ANNOUNCEMENT.describeContents()).isEqualTo(0);
90     }
91 
92     @Test
newArray_forAnnouncementCreator()93     public void newArray_forAnnouncementCreator() {
94         int sizeExpected = 2;
95 
96         Announcement[] announcements = Announcement.CREATOR.newArray(sizeExpected);
97 
98         assertWithMessage("Announcements").that(announcements).hasLength(sizeExpected);
99     }
100 
101     @Test
writeToParcel_forAnnouncement()102     public void writeToParcel_forAnnouncement() {
103         Parcel parcel = Parcel.obtain();
104 
105         TEST_ANNOUNCEMENT.writeToParcel(parcel, /* flags= */ 0);
106         parcel.setDataPosition(0);
107 
108         Announcement announcementFromParcel = Announcement.CREATOR.createFromParcel(parcel);
109         assertWithMessage("Selector of announcement created from parcel")
110                 .that(announcementFromParcel.getSelector()).isEqualTo(FM_PROGRAM_SELECTOR);
111         assertWithMessage("Type of announcement created from parcel")
112                 .that(announcementFromParcel.getType()).isEqualTo(TRAFFIC_ANNOUNCEMENT_TYPE);
113         assertWithMessage("Vendor info of announcement created from parcel")
114                 .that(announcementFromParcel.getVendorInfo()).isEqualTo(VENDOR_INFO);
115     }
116 }
117