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