1 /*
2  * Copyright (C) 2018 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.media.audio.cts;
18 
19 import static org.junit.Assert.assertNotEquals;
20 
21 import android.icu.util.ULocale;
22 import android.media.AudioPresentation;
23 import android.os.Parcel;
24 import android.platform.test.annotations.AppModeSdkSandbox;
25 
26 import com.android.compatibility.common.util.CtsAndroidTestCase;
27 import com.android.compatibility.common.util.NonMainlineTest;
28 
29 import java.util.HashMap;
30 import java.util.Locale;
31 import java.util.Map;
32 
33 @NonMainlineTest
34 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).")
35 public class AudioPresentationTest extends CtsAndroidTestCase {
36     private String TAG = "AudioPresentationTest";
37     private static final String REPORT_LOG_NAME = "CtsMediaAudioTestCases";
38 
testGetters(boolean fromParcelable)39     private void testGetters(boolean fromParcelable) throws Exception {
40         final int PRESENTATION_ID = 42;
41         final int PROGRAM_ID = 43;
42         final Map<Locale, CharSequence> LABELS = generateLabels();
43         final Locale LOCALE = Locale.US;
44         final int MASTERING_INDICATION = AudioPresentation.MASTERED_FOR_STEREO;
45         final boolean HAS_AUDIO_DESCRIPTION = false;
46         final boolean HAS_SPOKEN_SUBTITLES = true;
47         final boolean HAS_DIALOGUE_ENHANCEMENT = true;
48 
49         AudioPresentation presentation = (new AudioPresentation.Builder(PRESENTATION_ID)
50                 .setProgramId(PROGRAM_ID)
51                 .setLocale(ULocale.forLocale(LOCALE))
52                 .setLabels(localeToULocale(LABELS))
53                 .setMasteringIndication(MASTERING_INDICATION)
54                 .setHasAudioDescription(HAS_AUDIO_DESCRIPTION)
55                 .setHasSpokenSubtitles(HAS_SPOKEN_SUBTITLES)
56                 .setHasDialogueEnhancement(HAS_DIALOGUE_ENHANCEMENT)).build();
57         if (fromParcelable) {
58             Parcel p = Parcel.obtain();
59             presentation.writeToParcel(p, 0);
60             p.setDataPosition(0);
61             presentation = AudioPresentation.CREATOR.createFromParcel(p);
62         }
63         assertEquals(PRESENTATION_ID, presentation.getPresentationId());
64         assertEquals(PROGRAM_ID, presentation.getProgramId());
65         assertEquals(LABELS, presentation.getLabels());
66         assertEquals(LOCALE, presentation.getLocale());
67         assertEquals(MASTERING_INDICATION, presentation.getMasteringIndication());
68         assertEquals(HAS_AUDIO_DESCRIPTION, presentation.hasAudioDescription());
69         assertEquals(HAS_SPOKEN_SUBTITLES, presentation.hasSpokenSubtitles());
70         assertEquals(HAS_DIALOGUE_ENHANCEMENT, presentation.hasDialogueEnhancement());
71     }
72 
testGetters()73     public void testGetters() throws Exception {
74         testGetters(false);
75     }
76 
testParcelable()77     public void testParcelable() throws Exception {
78         testGetters(true);
79     }
80 
testEqualsAndHashCode()81     public void testEqualsAndHashCode() throws Exception {
82         final int PRESENTATION_ID = 42;
83         final int PROGRAM_ID = 43;
84         final Map<Locale, CharSequence> LABELS = generateLabels();
85         final Locale LOCALE = Locale.US;
86         final Locale LOCALE_3 = Locale.FRENCH;
87         final int MASTERING_INDICATION = AudioPresentation.MASTERED_FOR_STEREO;
88         final int MASTERING_INDICATION_3 = AudioPresentation.MASTERED_FOR_HEADPHONE;
89         final boolean HAS_AUDIO_DESCRIPTION = false;
90         final boolean HAS_SPOKEN_SUBTITLES = true;
91         final boolean HAS_DIALOGUE_ENHANCEMENT = true;
92 
93         {
94             AudioPresentation presentation1 = (new AudioPresentation.Builder(PRESENTATION_ID))
95                     .build();
96             assertEquals(presentation1, presentation1);
97             assertNotEquals(presentation1, null);
98             assertNotEquals(presentation1, new Object());
99             AudioPresentation presentation2 = (new AudioPresentation.Builder(PRESENTATION_ID))
100                     .build();
101             assertEquals(presentation1, presentation2);
102             assertEquals(presentation2, presentation1);
103             assertEquals(presentation1.hashCode(), presentation2.hashCode());
104             AudioPresentation presentation3 = (new AudioPresentation.Builder(PRESENTATION_ID + 1))
105                     .build();
106             assertNotEquals(presentation1, presentation3);
107             assertNotEquals(presentation1.hashCode(), presentation3.hashCode());
108         }
109         {
110             AudioPresentation presentation1 = (new AudioPresentation.Builder(PRESENTATION_ID)
111                     .setProgramId(PROGRAM_ID)).build();
112             AudioPresentation presentation2 = (new AudioPresentation.Builder(PRESENTATION_ID)
113                     .setProgramId(PROGRAM_ID)).build();
114             assertEquals(presentation1, presentation2);
115             assertEquals(presentation2, presentation1);
116             assertEquals(presentation1.hashCode(), presentation2.hashCode());
117             AudioPresentation presentation3 = (new AudioPresentation.Builder(PRESENTATION_ID)
118                     .setProgramId(PROGRAM_ID + 1)).build();
119             assertNotEquals(presentation1, presentation3);
120             assertNotEquals(presentation1.hashCode(), presentation3.hashCode());
121         }
122         {
123             AudioPresentation presentation1 = (new AudioPresentation.Builder(PRESENTATION_ID)
124                     .setLocale(ULocale.forLocale(LOCALE))).build();
125             AudioPresentation presentation2 = (new AudioPresentation.Builder(PRESENTATION_ID)
126                     .setLocale(ULocale.forLocale(LOCALE))).build();
127             assertEquals(presentation1, presentation2);
128             assertEquals(presentation2, presentation1);
129             assertEquals(presentation1.hashCode(), presentation2.hashCode());
130             AudioPresentation presentation3 = (new AudioPresentation.Builder(PRESENTATION_ID)
131                     .setLocale(ULocale.forLocale(LOCALE_3))).build();
132             assertNotEquals(presentation1, presentation3);
133             assertNotEquals(presentation1.hashCode(), presentation3.hashCode());
134         }
135         {
136             AudioPresentation presentation1 = (new AudioPresentation.Builder(PRESENTATION_ID)
137                     .setLabels(localeToULocale(LABELS))).build();
138             AudioPresentation presentation2 = (new AudioPresentation.Builder(PRESENTATION_ID)
139                     .setLabels(localeToULocale(LABELS))).build();
140             assertEquals(presentation1, presentation2);
141             assertEquals(presentation2, presentation1);
142             assertEquals(presentation1.hashCode(), presentation2.hashCode());
143             AudioPresentation presentation3 = (new AudioPresentation.Builder(PRESENTATION_ID)
144                     .setLabels(new HashMap<ULocale, CharSequence>())).build();
145             assertNotEquals(presentation1, presentation3);
146             assertNotEquals(presentation1.hashCode(), presentation3.hashCode());
147         }
148         {
149             AudioPresentation presentation1 = (new AudioPresentation.Builder(PRESENTATION_ID)
150                     .setMasteringIndication(MASTERING_INDICATION)).build();
151             AudioPresentation presentation2 = (new AudioPresentation.Builder(PRESENTATION_ID)
152                     .setMasteringIndication(MASTERING_INDICATION)).build();
153             assertEquals(presentation1, presentation2);
154             assertEquals(presentation2, presentation1);
155             assertEquals(presentation1.hashCode(), presentation2.hashCode());
156             AudioPresentation presentation3 = (new AudioPresentation.Builder(PRESENTATION_ID)
157                     .setMasteringIndication(MASTERING_INDICATION_3)).build();
158             assertNotEquals(presentation1, presentation3);
159             assertNotEquals(presentation1.hashCode(), presentation3.hashCode());
160         }
161         {
162             AudioPresentation presentation1 = (new AudioPresentation.Builder(PRESENTATION_ID)
163                     .setHasAudioDescription(HAS_AUDIO_DESCRIPTION)).build();
164             AudioPresentation presentation2 = (new AudioPresentation.Builder(PRESENTATION_ID)
165                     .setHasAudioDescription(HAS_AUDIO_DESCRIPTION)).build();
166             assertEquals(presentation1, presentation2);
167             assertEquals(presentation2, presentation1);
168             assertEquals(presentation1.hashCode(), presentation2.hashCode());
169             AudioPresentation presentation3 = (new AudioPresentation.Builder(PRESENTATION_ID)
170                     .setHasAudioDescription(!HAS_AUDIO_DESCRIPTION)).build();
171             assertNotEquals(presentation1, presentation3);
172             assertNotEquals(presentation1.hashCode(), presentation3.hashCode());
173         }
174         {
175             AudioPresentation presentation1 = (new AudioPresentation.Builder(PRESENTATION_ID)
176                     .setHasSpokenSubtitles(HAS_SPOKEN_SUBTITLES)).build();
177             AudioPresentation presentation2 = (new AudioPresentation.Builder(PRESENTATION_ID)
178                     .setHasSpokenSubtitles(HAS_SPOKEN_SUBTITLES)).build();
179             assertEquals(presentation1, presentation2);
180             assertEquals(presentation2, presentation1);
181             assertEquals(presentation1.hashCode(), presentation2.hashCode());
182             AudioPresentation presentation3 = (new AudioPresentation.Builder(PRESENTATION_ID)
183                     .setHasSpokenSubtitles(!HAS_SPOKEN_SUBTITLES)).build();
184             assertNotEquals(presentation1, presentation3);
185             assertNotEquals(presentation1.hashCode(), presentation3.hashCode());
186         }
187         {
188             AudioPresentation presentation1 = (new AudioPresentation.Builder(PRESENTATION_ID)
189                     .setHasDialogueEnhancement(HAS_DIALOGUE_ENHANCEMENT)).build();
190             AudioPresentation presentation2 = (new AudioPresentation.Builder(PRESENTATION_ID)
191                     .setHasDialogueEnhancement(HAS_DIALOGUE_ENHANCEMENT)).build();
192             assertEquals(presentation1, presentation2);
193             assertEquals(presentation2, presentation1);
194             assertEquals(presentation1.hashCode(), presentation2.hashCode());
195             AudioPresentation presentation3 = (new AudioPresentation.Builder(PRESENTATION_ID)
196                     .setHasDialogueEnhancement(!HAS_DIALOGUE_ENHANCEMENT)).build();
197             assertNotEquals(presentation1, presentation3);
198             assertNotEquals(presentation1.hashCode(), presentation3.hashCode());
199         }
200     }
201 
generateLabels()202     private static Map<Locale, CharSequence> generateLabels() {
203         Map<Locale, CharSequence> result = new HashMap<Locale, CharSequence>();
204         result.put(Locale.US, Locale.US.getDisplayLanguage());
205         result.put(Locale.FRENCH, Locale.FRENCH.getDisplayLanguage());
206         result.put(Locale.GERMAN, Locale.GERMAN.getDisplayLanguage());
207         return result;
208     }
209 
localeToULocale(Map<Locale, CharSequence> locales)210     private static Map<ULocale, CharSequence> localeToULocale(Map<Locale, CharSequence> locales) {
211         Map<ULocale, CharSequence> ulocaleLabels = new HashMap<ULocale, CharSequence>();
212         for (Map.Entry<Locale, CharSequence> entry : locales.entrySet()) {
213             ulocaleLabels.put(ULocale.forLocale(entry.getKey()), entry.getValue());
214         }
215         return ulocaleLabels;
216     }
217 }
218