1/*
2 * Copyright (C) 2014 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
17package android.hardware.soundtrigger;
18
19import android.hardware.soundtrigger.SoundTrigger;
20import android.hardware.soundtrigger.SoundTrigger.ConfidenceLevel;
21import android.hardware.soundtrigger.SoundTrigger.Keyphrase;
22import android.hardware.soundtrigger.SoundTrigger.KeyphraseRecognitionEvent;
23import android.hardware.soundtrigger.SoundTrigger.KeyphraseRecognitionExtra;
24import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel;
25import android.hardware.soundtrigger.SoundTrigger.RecognitionEvent;
26import android.media.AudioFormat;
27import android.os.Parcel;
28import android.test.InstrumentationTestCase;
29import android.test.suitebuilder.annotation.LargeTest;
30import android.test.suitebuilder.annotation.SmallTest;
31
32import java.util.Arrays;
33import java.util.Random;
34import java.util.UUID;
35
36public class SoundTriggerTest extends InstrumentationTestCase {
37    private Random mRandom = new Random();
38
39    @SmallTest
40    public void testKeyphraseParcelUnparcel_noUsers() throws Exception {
41        Keyphrase keyphrase = new Keyphrase(1, 0, "en-US", "hello", null);
42
43        // Write to a parcel
44        Parcel parcel = Parcel.obtain();
45        keyphrase.writeToParcel(parcel, 0);
46
47        // Read from it
48        parcel.setDataPosition(0);
49        Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel);
50
51        // Verify that they are the same
52        assertEquals(keyphrase.id, unparceled.id);
53        assertNull(unparceled.users);
54        assertEquals(keyphrase.locale, unparceled.locale);
55        assertEquals(keyphrase.text, unparceled.text);
56    }
57
58    @SmallTest
59    public void testKeyphraseParcelUnparcel_zeroUsers() throws Exception {
60        Keyphrase keyphrase = new Keyphrase(1, 0, "en-US", "hello", new int[0]);
61
62        // Write to a parcel
63        Parcel parcel = Parcel.obtain();
64        keyphrase.writeToParcel(parcel, 0);
65
66        // Read from it
67        parcel.setDataPosition(0);
68        Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel);
69
70        // Verify that they are the same
71        assertEquals(keyphrase.id, unparceled.id);
72        assertTrue(Arrays.equals(keyphrase.users, unparceled.users));
73        assertEquals(keyphrase.locale, unparceled.locale);
74        assertEquals(keyphrase.text, unparceled.text);
75    }
76
77    @SmallTest
78    public void testKeyphraseParcelUnparcel_pos() throws Exception {
79        Keyphrase keyphrase = new Keyphrase(1, 0, "en-US", "hello", new int[] {1, 2, 3, 4, 5});
80
81        // Write to a parcel
82        Parcel parcel = Parcel.obtain();
83        keyphrase.writeToParcel(parcel, 0);
84
85        // Read from it
86        parcel.setDataPosition(0);
87        Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel);
88
89        // Verify that they are the same
90        assertEquals(keyphrase.id, unparceled.id);
91        assertTrue(Arrays.equals(keyphrase.users, unparceled.users));
92        assertEquals(keyphrase.locale, unparceled.locale);
93        assertEquals(keyphrase.text, unparceled.text);
94    }
95
96    @SmallTest
97    public void testKeyphraseSoundModelParcelUnparcel_noData() throws Exception {
98        Keyphrase[] keyphrases = new Keyphrase[2];
99        keyphrases[0] = new Keyphrase(1, 0, "en-US", "hello", new int[] {0});
100        keyphrases[1] = new Keyphrase(2, 0, "fr-FR", "there", new int[] {1, 2});
101        KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), UUID.randomUUID(),
102                null, keyphrases);
103
104        // Write to a parcel
105        Parcel parcel = Parcel.obtain();
106        ksm.writeToParcel(parcel, 0);
107
108        // Read from it
109        parcel.setDataPosition(0);
110        KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel);
111
112        // Verify that they are the same
113        assertEquals(ksm.uuid, unparceled.uuid);
114        assertNull(unparceled.data);
115        assertEquals(ksm.type, unparceled.type);
116        assertTrue(Arrays.equals(keyphrases, unparceled.keyphrases));
117    }
118
119    @SmallTest
120    public void testKeyphraseSoundModelParcelUnparcel_zeroData() throws Exception {
121        Keyphrase[] keyphrases = new Keyphrase[2];
122        keyphrases[0] = new Keyphrase(1, 0, "en-US", "hello", new int[] {0});
123        keyphrases[1] = new Keyphrase(2, 0, "fr-FR", "there", new int[] {1, 2});
124        KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), UUID.randomUUID(),
125                new byte[0], keyphrases);
126
127        // Write to a parcel
128        Parcel parcel = Parcel.obtain();
129        ksm.writeToParcel(parcel, 0);
130
131        // Read from it
132        parcel.setDataPosition(0);
133        KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel);
134
135        // Verify that they are the same
136        assertEquals(ksm.uuid, unparceled.uuid);
137        assertEquals(ksm.type, unparceled.type);
138        assertTrue(Arrays.equals(ksm.keyphrases, unparceled.keyphrases));
139        assertTrue(Arrays.equals(ksm.data, unparceled.data));
140    }
141
142    @SmallTest
143    public void testKeyphraseSoundModelParcelUnparcel_noKeyphrases() throws Exception {
144        byte[] data = new byte[10];
145        mRandom.nextBytes(data);
146        KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), UUID.randomUUID(),
147                data, null);
148
149        // Write to a parcel
150        Parcel parcel = Parcel.obtain();
151        ksm.writeToParcel(parcel, 0);
152
153        // Read from it
154        parcel.setDataPosition(0);
155        KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel);
156
157        // Verify that they are the same
158        assertEquals(ksm.uuid, unparceled.uuid);
159        assertEquals(ksm.type, unparceled.type);
160        assertNull(unparceled.keyphrases);
161        assertTrue(Arrays.equals(ksm.data, unparceled.data));
162    }
163
164    @SmallTest
165    public void testKeyphraseSoundModelParcelUnparcel_zeroKeyphrases() throws Exception {
166        byte[] data = new byte[10];
167        mRandom.nextBytes(data);
168        KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), UUID.randomUUID(),
169                data, new Keyphrase[0]);
170
171        // Write to a parcel
172        Parcel parcel = Parcel.obtain();
173        ksm.writeToParcel(parcel, 0);
174
175        // Read from it
176        parcel.setDataPosition(0);
177        KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel);
178
179        // Verify that they are the same
180        assertEquals(ksm.uuid, unparceled.uuid);
181        assertEquals(ksm.type, unparceled.type);
182        assertTrue(Arrays.equals(ksm.keyphrases, unparceled.keyphrases));
183        assertTrue(Arrays.equals(ksm.data, unparceled.data));
184    }
185
186    @LargeTest
187    public void testKeyphraseSoundModelParcelUnparcel_largeData() throws Exception {
188        Keyphrase[] keyphrases = new Keyphrase[2];
189        keyphrases[0] = new Keyphrase(1, 0, "en-US", "hello", new int[] {0});
190        keyphrases[1] = new Keyphrase(2, 0, "fr-FR", "there", new int[] {1, 2});
191        byte[] data = new byte[200 * 1024];
192        mRandom.nextBytes(data);
193        KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), UUID.randomUUID(),
194                data, keyphrases);
195
196        // Write to a parcel
197        Parcel parcel = Parcel.obtain();
198        ksm.writeToParcel(parcel, 0);
199
200        // Read from it
201        parcel.setDataPosition(0);
202        KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel);
203
204        // Verify that they are the same
205        assertEquals(ksm.uuid, unparceled.uuid);
206        assertEquals(ksm.type, unparceled.type);
207        assertTrue(Arrays.equals(ksm.data, unparceled.data));
208        assertTrue(Arrays.equals(ksm.keyphrases, unparceled.keyphrases));
209    }
210
211    @SmallTest
212    public void testRecognitionEventParcelUnparcel_noData() throws Exception {
213        RecognitionEvent re = new RecognitionEvent(SoundTrigger.RECOGNITION_STATUS_SUCCESS, 1,
214                true, 2, 3, 4, false, null, null);
215
216        // Write to a parcel
217        Parcel parcel = Parcel.obtain();
218        re.writeToParcel(parcel, 0);
219
220        // Read from it
221        parcel.setDataPosition(0);
222        RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel);
223
224        // Verify that they are the same
225        assertEquals(re, unparceled);
226    }
227
228    @SmallTest
229    public void testRecognitionEventParcelUnparcel_zeroData() throws Exception {
230        RecognitionEvent re = new RecognitionEvent(SoundTrigger.RECOGNITION_STATUS_FAILURE, 1,
231                true, 2, 3, 4, false, null, new byte[1]);
232
233        // Write to a parcel
234        Parcel parcel = Parcel.obtain();
235        re.writeToParcel(parcel, 0);
236
237        // Read from it
238        parcel.setDataPosition(0);
239        RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel);
240
241        // Verify that they are the same
242        assertEquals(re, unparceled);
243    }
244
245    @SmallTest
246    public void testRecognitionEventParcelUnparcel_largeData() throws Exception {
247        byte[] data = new byte[200 * 1024];
248        mRandom.nextBytes(data);
249        RecognitionEvent re = new RecognitionEvent(SoundTrigger.RECOGNITION_STATUS_ABORT, 1,
250                false, 2, 3, 4, false, null, data);
251
252        // Write to a parcel
253        Parcel parcel = Parcel.obtain();
254        re.writeToParcel(parcel, 0);
255
256        // Read from it
257        parcel.setDataPosition(0);
258        RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel);
259
260        // Verify that they are the same
261        assertEquals(re, unparceled);
262    }
263
264    @SmallTest
265    public void testRecognitionEventParcelUnparcel_largeAudioData() throws Exception {
266        byte[] data = new byte[200 * 1024];
267        mRandom.nextBytes(data);
268        RecognitionEvent re = new RecognitionEvent(SoundTrigger.RECOGNITION_STATUS_ABORT, 1,
269                false, 2, 3, 4, true,
270                (new AudioFormat.Builder())
271                .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
272                .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
273                .setSampleRate(16000)
274                .build(),
275                data);
276
277        // Write to a parcel
278        Parcel parcel = Parcel.obtain();
279        re.writeToParcel(parcel, 0);
280
281        // Read from it
282        parcel.setDataPosition(0);
283        RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel);
284
285        // Verify that they are the same
286        assertEquals(re, unparceled);
287    }
288
289    @SmallTest
290    public void testKeyphraseRecognitionEventParcelUnparcel_noKeyphrases() throws Exception {
291        KeyphraseRecognitionEvent re = new KeyphraseRecognitionEvent(
292                SoundTrigger.RECOGNITION_STATUS_SUCCESS, 1, true, 2, 3, 4, false, null, null, null);
293
294        // Write to a parcel
295        Parcel parcel = Parcel.obtain();
296        re.writeToParcel(parcel, 0);
297
298        // Read from it
299        parcel.setDataPosition(0);
300        KeyphraseRecognitionEvent unparceled =
301                KeyphraseRecognitionEvent.CREATOR.createFromParcel(parcel);
302
303        // Verify that they are the same
304        assertEquals(re, unparceled);
305    }
306
307    @SmallTest
308    public void testKeyphraseRecognitionEventParcelUnparcel_zeroData() throws Exception {
309        KeyphraseRecognitionExtra[] kpExtra = new KeyphraseRecognitionExtra[0];
310        KeyphraseRecognitionEvent re = new KeyphraseRecognitionEvent(
311                SoundTrigger.RECOGNITION_STATUS_FAILURE, 2, true, 2, 3, 4, false, null, new byte[1],
312                kpExtra);
313
314        // Write to a parcel
315        Parcel parcel = Parcel.obtain();
316        re.writeToParcel(parcel, 0);
317
318        // Read from it
319        parcel.setDataPosition(0);
320        KeyphraseRecognitionEvent unparceled =
321                KeyphraseRecognitionEvent.CREATOR.createFromParcel(parcel);
322
323        // Verify that they are the same
324        assertEquals(re, unparceled);
325    }
326
327    @LargeTest
328    public void testKeyphraseRecognitionEventParcelUnparcel_largeData() throws Exception {
329        byte[] data = new byte[200 * 1024];
330        mRandom.nextBytes(data);
331        KeyphraseRecognitionExtra[] kpExtra = new KeyphraseRecognitionExtra[4];
332        ConfidenceLevel cl1 = new ConfidenceLevel(1, 90);
333        ConfidenceLevel cl2 = new ConfidenceLevel(2, 30);
334        kpExtra[0] = new KeyphraseRecognitionExtra(1,
335                SoundTrigger.RECOGNITION_MODE_USER_IDENTIFICATION, 0,
336                new ConfidenceLevel[] {cl1, cl2});
337        kpExtra[1] = new KeyphraseRecognitionExtra(1,
338                SoundTrigger.RECOGNITION_MODE_VOICE_TRIGGER, 0,
339                new ConfidenceLevel[] {cl2});
340        kpExtra[2] = new KeyphraseRecognitionExtra(1,
341                SoundTrigger.RECOGNITION_MODE_VOICE_TRIGGER, 0, null);
342        kpExtra[3] = new KeyphraseRecognitionExtra(1,
343                SoundTrigger.RECOGNITION_MODE_VOICE_TRIGGER, 0,
344                new ConfidenceLevel[0]);
345
346        KeyphraseRecognitionEvent re = new KeyphraseRecognitionEvent(
347                SoundTrigger.RECOGNITION_STATUS_FAILURE, 1, true, 2, 3, 4, false, null, data,
348                kpExtra);
349
350        // Write to a parcel
351        Parcel parcel = Parcel.obtain();
352        re.writeToParcel(parcel, 0);
353
354        // Read from it
355        parcel.setDataPosition(0);
356        KeyphraseRecognitionEvent unparceled =
357                KeyphraseRecognitionEvent.CREATOR.createFromParcel(parcel);
358
359        // Verify that they are the same
360        assertEquals(re, unparceled);
361    }
362}
363