From 0b76a465155c7ce72f9e349461ad033750c6d330 Mon Sep 17 00:00:00 2001 From: Adnan Begovic Date: Tue, 14 Jun 2016 18:07:55 -0700 Subject: [PATCH] cmsdk: Provide test coverage for remainder PartnerInterface's. TICKET: CYNGNOS-3051 Change-Id: I1c15516dc03d3c0897cdd35f22281b25eedb84de --- tests/AndroidManifest.xml | 2 + .../settings/unit/CMPartnerInterfaceTest.java | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml index 3560431..fe3099a 100644 --- a/tests/AndroidManifest.xml +++ b/tests/AndroidManifest.xml @@ -8,6 +8,8 @@ + + diff --git a/tests/src/org/cyanogenmod/tests/settings/unit/CMPartnerInterfaceTest.java b/tests/src/org/cyanogenmod/tests/settings/unit/CMPartnerInterfaceTest.java index c1ac0de..8fb0ff4 100644 --- a/tests/src/org/cyanogenmod/tests/settings/unit/CMPartnerInterfaceTest.java +++ b/tests/src/org/cyanogenmod/tests/settings/unit/CMPartnerInterfaceTest.java @@ -18,6 +18,11 @@ package org.cyanogenmod.tests.settings.unit; import android.app.INotificationManager; import android.content.Context; +import android.media.AudioAttributes; +import android.media.AudioFormat; +import android.media.AudioManager; +import android.media.AudioRecord; +import android.media.MediaRecorder; import android.net.Uri; import android.os.RemoteException; import android.os.ServiceManager; @@ -194,6 +199,54 @@ public class CMPartnerInterfaceTest extends AndroidTestCase { assertEquals(PartnerInterface.ZEN_MODE_OFF, getZenMode()); } + private final static int BUFFER_ELEMENTS_TO_REC = 1024; + private final static int BYTES_PER_ELEMENT = 2; + private static final int RECORDER_SAMPLERATE = 41000; + private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO; + private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT; + + private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 }; + public AudioRecord findAudioRecord() { + for (int rate : mSampleRates) { + for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, + AudioFormat.ENCODING_PCM_16BIT }) { + for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, + AudioFormat.CHANNEL_IN_STEREO }) { + try { + Log.d(TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: " + + channelConfig); + int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat); + + if (bufferSize != AudioRecord.ERROR_BAD_VALUE) { + AudioRecord recorder = new AudioRecord( + cyanogenmod.media.MediaRecorder.AudioSource.HOTWORD, + rate, channelConfig, audioFormat, bufferSize); + + if (recorder.getState() == AudioRecord.STATE_INITIALIZED) + return recorder; + } + } catch (Exception e) { + Log.e(TAG, rate + "Exception, keep trying.",e); + } + } + } + } + return null; + } + + @SmallTest + public void testGetCurrentHotwordPackageName() { + // make sure no one is actively stealing this as we attempt to + assertNull(mPartnerInterface.getCurrentHotwordPackageName()); + + // find first viable audio record + final AudioRecord audioRecorder = findAudioRecord(); + + audioRecorder.startRecording(); + assertEquals(mContext.getPackageName(), mPartnerInterface.getCurrentHotwordPackageName()); + audioRecorder.stop(); + } + /** * testZenModeWithDuration: sets Zen Mode with duration and blocks * until the duration is verified. Use with care! This does not