1 /* 2 * Copyright (C) 2023 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 com.android.server.soundtrigger; 18 19 import static android.hardware.soundtrigger.ConversionUtil.aidl2apiAudioFormatWithDefault; 20 import static android.hardware.soundtrigger.ConversionUtil.aidl2apiPhrase; 21 import static android.hardware.soundtrigger.ConversionUtil.aidl2apiRecognitionConfig; 22 import static android.hardware.soundtrigger.ConversionUtil.api2aidlPhrase; 23 import static android.hardware.soundtrigger.ConversionUtil.api2aidlRecognitionConfig; 24 import static android.hardware.soundtrigger.ConversionUtil.byteArrayToSharedMemory; 25 import static android.hardware.soundtrigger.ConversionUtil.sharedMemoryToByteArray; 26 import static android.hardware.soundtrigger.SoundTrigger.ConfidenceLevel; 27 import static android.hardware.soundtrigger.SoundTrigger.RECOGNITION_MODE_GENERIC; 28 import static android.hardware.soundtrigger.SoundTrigger.RECOGNITION_MODE_USER_AUTHENTICATION; 29 import static android.hardware.soundtrigger.SoundTrigger.RECOGNITION_MODE_USER_IDENTIFICATION; 30 import static android.hardware.soundtrigger.SoundTrigger.RECOGNITION_MODE_VOICE_TRIGGER; 31 32 import static org.junit.Assert.assertArrayEquals; 33 import static org.junit.Assert.assertEquals; 34 import static org.junit.Assert.assertNotNull; 35 36 import android.hardware.soundtrigger.ConversionUtil; 37 import android.hardware.soundtrigger.SoundTrigger; 38 39 import androidx.test.runner.AndroidJUnit4; 40 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 44 import java.util.Locale; 45 46 @RunWith(AndroidJUnit4.class) 47 public class ConversionUtilTest { 48 private static final String TAG = "ConversionUtilTest"; 49 50 @Test testDefaultAudioFormatConstruction()51 public void testDefaultAudioFormatConstruction() { 52 // This method should generate a real format when passed null 53 final var format = aidl2apiAudioFormatWithDefault( 54 null /** exercise default **/, 55 true /** isInput **/ 56 ); 57 assertNotNull(format); 58 } 59 60 @Test testRecognitionConfigRoundTrip()61 public void testRecognitionConfigRoundTrip() { 62 final int flags = SoundTrigger.ModuleProperties.AUDIO_CAPABILITY_ECHO_CANCELLATION 63 | SoundTrigger.ModuleProperties.AUDIO_CAPABILITY_NOISE_SUPPRESSION; 64 final var data = new byte[] {0x11, 0x22}; 65 final var keyphrases = new SoundTrigger.KeyphraseRecognitionExtra[2]; 66 keyphrases[0] = new SoundTrigger.KeyphraseRecognitionExtra(99, 67 RECOGNITION_MODE_VOICE_TRIGGER | RECOGNITION_MODE_USER_IDENTIFICATION, 13, 68 new ConfidenceLevel[] {new ConfidenceLevel(9999, 50), 69 new ConfidenceLevel(5000, 80)}); 70 keyphrases[1] = new SoundTrigger.KeyphraseRecognitionExtra(101, 71 RECOGNITION_MODE_GENERIC, 8, new ConfidenceLevel[] { 72 new ConfidenceLevel(7777, 30), 73 new ConfidenceLevel(2222, 60)}); 74 75 var apiconfig = new SoundTrigger.RecognitionConfig(true, false /** must be false **/, 76 keyphrases, data, flags); 77 assertEquals(apiconfig, aidl2apiRecognitionConfig(api2aidlRecognitionConfig(apiconfig))); 78 } 79 80 @Test testByteArraySharedMemRoundTrip()81 public void testByteArraySharedMemRoundTrip() { 82 final var data = new byte[] { 0x11, 0x22, 0x33, 0x44, 83 (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef }; 84 assertArrayEquals(data, sharedMemoryToByteArray(byteArrayToSharedMemory(data, "name"), 85 10000000)); 86 87 } 88 89 @Test testPhraseRoundTrip()90 public void testPhraseRoundTrip() { 91 final var users = new int[] {10001, 10002}; 92 final var apiphrase = new SoundTrigger.Keyphrase(17 /** id **/, 93 RECOGNITION_MODE_VOICE_TRIGGER | RECOGNITION_MODE_USER_AUTHENTICATION, 94 Locale.forLanguageTag("no_NO"), 95 "Hello Android", /** keyphrase **/ 96 users); 97 assertEquals(apiphrase, aidl2apiPhrase(api2aidlPhrase(apiphrase))); 98 } 99 } 100