| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
| 40 |
public void testKeyphraseParcelUnparcel_noUsers() throws Exception { |
| 41 |
Keyphrase keyphrase = new Keyphrase(1, 0, |
| 42 |
Locale.forLanguageTag("en-US"), "hello", null); |
| 43 |
|
| 44 |
|
| 45 |
Parcel parcel = Parcel.obtain(); |
| 46 |
keyphrase.writeToParcel(parcel, 0); |
| 47 |
|
| 48 |
|
| 49 |
parcel.setDataPosition(0); |
| 50 |
Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel); |
| 51 |
|
| 52 |
|
| 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 |
| 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 |
|
| 65 |
Parcel parcel = Parcel.obtain(); |
| 66 |
keyphrase.writeToParcel(parcel, 0); |
| 67 |
|
| 68 |
|
| 69 |
parcel.setDataPosition(0); |
| 70 |
Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel); |
| 71 |
|
| 72 |
|
| 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 |
| 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 |
|
| 85 |
Parcel parcel = Parcel.obtain(); |
| 86 |
keyphrase.writeToParcel(parcel, 0); |
| 87 |
|
| 88 |
|
| 89 |
parcel.setDataPosition(0); |
| 90 |
Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel); |
| 91 |
|
| 92 |
|
| 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 |
| 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 |
|
| 110 |
Parcel parcel = Parcel.obtain(); |
| 111 |
ksm.writeToParcel(parcel, 0); |
| 112 |
|
| 113 |
|
| 114 |
parcel.setDataPosition(0); |
| 115 |
KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); |
| 116 |
|
| 117 |
|
| 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 |
| 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 |
|
| 135 |
Parcel parcel = Parcel.obtain(); |
| 136 |
ksm.writeToParcel(parcel, 0); |
| 137 |
|
| 138 |
|
| 139 |
parcel.setDataPosition(0); |
| 140 |
KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); |
| 141 |
|
| 142 |
|
| 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 |
| 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 |
|
| 157 |
Parcel parcel = Parcel.obtain(); |
| 158 |
ksm.writeToParcel(parcel, 0); |
| 159 |
|
| 160 |
|
| 161 |
parcel.setDataPosition(0); |
| 162 |
KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); |
| 163 |
|
| 164 |
|
| 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 |
| 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 |
|
| 179 |
Parcel parcel = Parcel.obtain(); |
| 180 |
ksm.writeToParcel(parcel, 0); |
| 181 |
|
| 182 |
|
| 183 |
parcel.setDataPosition(0); |
| 184 |
KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); |
| 185 |
|
| 186 |
|
| 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 |
| 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 |
|
| 206 |
Parcel parcel = Parcel.obtain(); |
| 207 |
ksm.writeToParcel(parcel, 0); |
| 208 |
|
| 209 |
|
| 210 |
parcel.setDataPosition(0); |
| 211 |
KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); |
| 212 |
|
| 213 |
|
| 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 |
| 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 |
|
| 226 |
Parcel parcel = Parcel.obtain(); |
| 227 |
re.writeToParcel(parcel, 0); |
| 228 |
|
| 229 |
|
| 230 |
parcel.setDataPosition(0); |
| 231 |
RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel); |
| 232 |
|
| 233 |
|
| 234 |
assertEquals(re, unparceled); |
| 235 |
} |
| 236 |
|
| 237 |
@SmallTest |
| 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 |
|
| 243 |
Parcel parcel = Parcel.obtain(); |
| 244 |
re.writeToParcel(parcel, 0); |
| 245 |
|
| 246 |
|
| 247 |
parcel.setDataPosition(0); |
| 248 |
RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel); |
| 249 |
|
| 250 |
|
| 251 |
assertEquals(re, unparceled); |
| 252 |
} |
| 253 |
|
| 254 |
@SmallTest |
| 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 |
|
| 262 |
Parcel parcel = Parcel.obtain(); |
| 263 |
re.writeToParcel(parcel, 0); |
| 264 |
|
| 265 |
|
| 266 |
parcel.setDataPosition(0); |
| 267 |
RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel); |
| 268 |
|
| 269 |
|
| 270 |
assertEquals(re, unparceled); |
| 271 |
} |
| 272 |
|
| 273 |
@SmallTest |
| 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 |
|
| 287 |
Parcel parcel = Parcel.obtain(); |
| 288 |
re.writeToParcel(parcel, 0); |
| 289 |
|
| 290 |
|
| 291 |
parcel.setDataPosition(0); |
| 292 |
RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel); |
| 293 |
|
| 294 |
|
| 295 |
assertEquals(re, unparceled); |
| 296 |
} |
| 297 |
|
| 298 |
@SmallTest |
| 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 |
|
| 304 |
Parcel parcel = Parcel.obtain(); |
| 305 |
re.writeToParcel(parcel, 0); |
| 306 |
|
| 307 |
|
| 308 |
parcel.setDataPosition(0); |
| 309 |
KeyphraseRecognitionEvent unparceled = |
| 310 |
KeyphraseRecognitionEvent.CREATOR.createFromParcel(parcel); |
| 311 |
|
| 312 |
|
| 313 |
assertEquals(re, unparceled); |
| 314 |
} |
| 315 |
|
| 316 |
@SmallTest |
| 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 |
|
| 324 |
Parcel parcel = Parcel.obtain(); |
| 325 |
re.writeToParcel(parcel, 0); |
| 326 |
|
| 327 |
|
| 328 |
parcel.setDataPosition(0); |
| 329 |
KeyphraseRecognitionEvent unparceled = |
| 330 |
KeyphraseRecognitionEvent.CREATOR.createFromParcel(parcel); |
| 331 |
|
| 332 |
|
| 333 |
assertEquals(re, unparceled); |
| 334 |
} |
| 335 |
|
| 336 |
@LargeTest |
| 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 |
|
| 360 |
Parcel parcel = Parcel.obtain(); |
| 361 |
re.writeToParcel(parcel, 0); |
| 362 |
|
| 363 |
|
| 364 |
parcel.setDataPosition(0); |
| 365 |
KeyphraseRecognitionEvent unparceled = |
| 366 |
KeyphraseRecognitionEvent.CREATOR.createFromParcel(parcel); |
| 367 |
|
| 368 |
|
| 369 |
assertEquals(re, unparceled); |
| 370 |
} |
| 371 |
} |