SoundTriggerTest.java revision 39c12fab49075b715c253c68c84b5c10c3150197
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.Keyphrase; 21import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel; 22import android.hardware.soundtrigger.SoundTrigger.RecognitionEvent; 23import android.os.Parcel; 24import android.test.InstrumentationTestCase; 25import android.test.suitebuilder.annotation.LargeTest; 26import android.test.suitebuilder.annotation.SmallTest; 27 28import java.util.Arrays; 29import java.util.Random; 30import java.util.UUID; 31 32public class SoundTriggerTest extends InstrumentationTestCase { 33 private Random mRandom = new Random(); 34 35 @SmallTest 36 public void testKeyphraseParcelUnparcel_noUsers() throws Exception { 37 Keyphrase keyphrase = new Keyphrase(1, 0, "en-US", "hello", null); 38 39 // Write to a parcel 40 Parcel parcel = Parcel.obtain(); 41 keyphrase.writeToParcel(parcel, 0); 42 43 // Read from it 44 parcel.setDataPosition(0); 45 Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel); 46 47 // Verify that they are the same 48 assertEquals(keyphrase.id, unparceled.id); 49 assertNull(unparceled.users); 50 assertEquals(keyphrase.locale, unparceled.locale); 51 assertEquals(keyphrase.text, unparceled.text); 52 } 53 54 @SmallTest 55 public void testKeyphraseParcelUnparcel_zeroUsers() throws Exception { 56 Keyphrase keyphrase = new Keyphrase(1, 0, "en-US", "hello", new int[0]); 57 58 // Write to a parcel 59 Parcel parcel = Parcel.obtain(); 60 keyphrase.writeToParcel(parcel, 0); 61 62 // Read from it 63 parcel.setDataPosition(0); 64 Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel); 65 66 // Verify that they are the same 67 assertEquals(keyphrase.id, unparceled.id); 68 assertTrue(Arrays.equals(keyphrase.users, unparceled.users)); 69 assertEquals(keyphrase.locale, unparceled.locale); 70 assertEquals(keyphrase.text, unparceled.text); 71 } 72 73 @SmallTest 74 public void testKeyphraseParcelUnparcel_pos() throws Exception { 75 Keyphrase keyphrase = new Keyphrase(1, 0, "en-US", "hello", new int[] {1, 2, 3, 4, 5}); 76 77 // Write to a parcel 78 Parcel parcel = Parcel.obtain(); 79 keyphrase.writeToParcel(parcel, 0); 80 81 // Read from it 82 parcel.setDataPosition(0); 83 Keyphrase unparceled = Keyphrase.CREATOR.createFromParcel(parcel); 84 85 // Verify that they are the same 86 assertEquals(keyphrase.id, unparceled.id); 87 assertTrue(Arrays.equals(keyphrase.users, unparceled.users)); 88 assertEquals(keyphrase.locale, unparceled.locale); 89 assertEquals(keyphrase.text, unparceled.text); 90 } 91 92 @SmallTest 93 public void testKeyphraseSoundModelParcelUnparcel_noData() throws Exception { 94 Keyphrase[] keyphrases = new Keyphrase[2]; 95 keyphrases[0] = new Keyphrase(1, 0, "en-US", "hello", new int[] {0}); 96 keyphrases[1] = new Keyphrase(2, 0, "fr-FR", "there", new int[] {1, 2}); 97 KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), null, keyphrases); 98 99 // Write to a parcel 100 Parcel parcel = Parcel.obtain(); 101 ksm.writeToParcel(parcel, 0); 102 103 // Read from it 104 parcel.setDataPosition(0); 105 KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); 106 107 // Verify that they are the same 108 assertEquals(ksm.uuid, unparceled.uuid); 109 assertNull(unparceled.data); 110 assertEquals(ksm.type, unparceled.type); 111 assertTrue(Arrays.equals(keyphrases, unparceled.keyphrases)); 112 } 113 114 @SmallTest 115 public void testKeyphraseSoundModelParcelUnparcel_zeroData() throws Exception { 116 Keyphrase[] keyphrases = new Keyphrase[2]; 117 keyphrases[0] = new Keyphrase(1, 0, "en-US", "hello", new int[] {0}); 118 keyphrases[1] = new Keyphrase(2, 0, "fr-FR", "there", new int[] {1, 2}); 119 KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), new byte[0], 120 keyphrases); 121 122 // Write to a parcel 123 Parcel parcel = Parcel.obtain(); 124 ksm.writeToParcel(parcel, 0); 125 126 // Read from it 127 parcel.setDataPosition(0); 128 KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); 129 130 // Verify that they are the same 131 assertEquals(ksm.uuid, unparceled.uuid); 132 assertEquals(ksm.type, unparceled.type); 133 assertTrue(Arrays.equals(ksm.keyphrases, unparceled.keyphrases)); 134 assertTrue(Arrays.equals(ksm.data, unparceled.data)); 135 } 136 137 @SmallTest 138 public void testKeyphraseSoundModelParcelUnparcel_noKeyphrases() throws Exception { 139 byte[] data = new byte[10]; 140 mRandom.nextBytes(data); 141 KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), data, null); 142 143 // Write to a parcel 144 Parcel parcel = Parcel.obtain(); 145 ksm.writeToParcel(parcel, 0); 146 147 // Read from it 148 parcel.setDataPosition(0); 149 KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); 150 151 // Verify that they are the same 152 assertEquals(ksm.uuid, unparceled.uuid); 153 assertEquals(ksm.type, unparceled.type); 154 assertNull(unparceled.keyphrases); 155 assertTrue(Arrays.equals(ksm.data, unparceled.data)); 156 } 157 158 @SmallTest 159 public void testKeyphraseSoundModelParcelUnparcel_zeroKeyphrases() throws Exception { 160 byte[] data = new byte[10]; 161 mRandom.nextBytes(data); 162 KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), data, 163 new Keyphrase[0]); 164 165 // Write to a parcel 166 Parcel parcel = Parcel.obtain(); 167 ksm.writeToParcel(parcel, 0); 168 169 // Read from it 170 parcel.setDataPosition(0); 171 KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); 172 173 // Verify that they are the same 174 assertEquals(ksm.uuid, unparceled.uuid); 175 assertEquals(ksm.type, unparceled.type); 176 assertTrue(Arrays.equals(ksm.keyphrases, unparceled.keyphrases)); 177 assertTrue(Arrays.equals(ksm.data, unparceled.data)); 178 } 179 180 @LargeTest 181 public void testKeyphraseSoundModelParcelUnparcel_largeData() throws Exception { 182 Keyphrase[] keyphrases = new Keyphrase[2]; 183 keyphrases[0] = new Keyphrase(1, 0, "en-US", "hello", new int[] {0}); 184 keyphrases[1] = new Keyphrase(2, 0, "fr-FR", "there", new int[] {1, 2}); 185 byte[] data = new byte[200 * 1024]; 186 mRandom.nextBytes(data); 187 KeyphraseSoundModel ksm = new KeyphraseSoundModel(UUID.randomUUID(), data, keyphrases); 188 189 // Write to a parcel 190 Parcel parcel = Parcel.obtain(); 191 ksm.writeToParcel(parcel, 0); 192 193 // Read from it 194 parcel.setDataPosition(0); 195 KeyphraseSoundModel unparceled = KeyphraseSoundModel.CREATOR.createFromParcel(parcel); 196 197 // Verify that they are the same 198 assertEquals(ksm.uuid, unparceled.uuid); 199 assertEquals(ksm.type, unparceled.type); 200 assertTrue(Arrays.equals(ksm.data, unparceled.data)); 201 assertTrue(Arrays.equals(ksm.keyphrases, unparceled.keyphrases)); 202 } 203 204 @SmallTest 205 public void testRecognitionEventParcelUnparcel_noData() throws Exception { 206 RecognitionEvent re = new RecognitionEvent(SoundTrigger.RECOGNITION_STATUS_SUCCESS, 1, 207 true, 2, 3, 4, null); 208 209 // Write to a parcel 210 Parcel parcel = Parcel.obtain(); 211 re.writeToParcel(parcel, 0); 212 213 // Read from it 214 parcel.setDataPosition(0); 215 RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel); 216 217 // Verify that they are the same 218 assertEquals(re, unparceled); 219 } 220 221 @SmallTest 222 public void testRecognitionEventParcelUnparcel_zeroData() throws Exception { 223 RecognitionEvent re = new RecognitionEvent(SoundTrigger.RECOGNITION_STATUS_FAILURE, 1, 224 true, 2, 3, 4, new byte[1]); 225 226 // Write to a parcel 227 Parcel parcel = Parcel.obtain(); 228 re.writeToParcel(parcel, 0); 229 230 // Read from it 231 parcel.setDataPosition(0); 232 RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel); 233 234 // Verify that they are the same 235 assertEquals(re, unparceled); 236 } 237 238 @SmallTest 239 public void testRecognitionEventParcelUnparcel_largeData() throws Exception { 240 byte[] data = new byte[200 * 1024]; 241 mRandom.nextBytes(data); 242 RecognitionEvent re = new RecognitionEvent(SoundTrigger.RECOGNITION_STATUS_ABORT, 1, 243 false, 2, 3, 4, data); 244 245 // Write to a parcel 246 Parcel parcel = Parcel.obtain(); 247 re.writeToParcel(parcel, 0); 248 249 // Read from it 250 parcel.setDataPosition(0); 251 RecognitionEvent unparceled = RecognitionEvent.CREATOR.createFromParcel(parcel); 252 253 // Verify that they are the same 254 assertEquals(re, unparceled); 255 } 256} 257