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