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