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