Fix for CTS LoudnessEnhancer test using Visualizer

Added missing test to use Visualizer effect to measure actual
gain applied by LoudnessEnhancer.

bug: 10967161
Change-Id: I15b83e05239bb805a43d807387755b40a4df2155
diff --git a/tests/tests/media/src/android/media/cts/LoudnessEnhancerTest.java b/tests/tests/media/src/android/media/cts/LoudnessEnhancerTest.java
index c19102b..5b4d1aa 100644
--- a/tests/tests/media/src/android/media/cts/LoudnessEnhancerTest.java
+++ b/tests/tests/media/src/android/media/cts/LoudnessEnhancerTest.java
@@ -24,6 +24,9 @@
 import android.media.AudioManager;
 import android.media.MediaPlayer;
 import android.media.audiofx.LoudnessEnhancer;
+import java.util.UUID;
+import android.media.audiofx.Visualizer;
+import android.media.audiofx.Visualizer.MeasurementPeakRms;
 import android.os.Looper;
 import android.test.AndroidTestCase;
 import android.util.Log;
@@ -77,6 +80,113 @@
         }
     }
 
+  //Test case 2.0: test loudness gain change in audio
+    public void test2_0MeasureGainChange() throws Exception {
+        if (!hasAudioOutput()) {
+            return;
+        }
+        AudioEffect vc = null;
+        Visualizer visualizer = null;
+        MediaPlayer mp = null;
+        try {
+            // this test will play a 1kHz sine wave with peaks at -40dB, and apply 6 db gain
+            // using loudness enhancement
+            mp = MediaPlayer.create(getContext(), R.raw.sine1khzm40db);
+            final int LOUDNESS_GAIN = 600;
+            final int MAX_MEASUREMENT_ERROR_MB = 200;
+            assertNotNull("null MediaPlayer", mp);
+
+            // creating a volume controller on output mix ensures that ro.audio.silent mutes
+            // audio after the effects and not before
+            vc = new AudioEffect(
+                    AudioEffect.EFFECT_TYPE_NULL,
+                    UUID.fromString(BUNDLE_VOLUME_EFFECT_UUID),
+                    0,
+                    mp.getAudioSessionId());
+            vc.setEnabled(true);
+
+            AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
+            assertNotNull("null AudioManager", am);
+            int originalVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
+            am.setStreamVolume(AudioManager.STREAM_MUSIC,
+                    am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
+            int sessionId = mp.getAudioSessionId();
+
+            getLoudnessEnhancer(sessionId);
+            visualizer = new Visualizer(sessionId);
+
+            mp.setLooping(true);
+            mp.start();
+
+            visualizer.setEnabled(true);
+            assertTrue("visualizer not enabled", visualizer.getEnabled());
+            Thread.sleep(100);
+            int status = visualizer.setMeasurementMode(Visualizer.MEASUREMENT_MODE_PEAK_RMS);
+            assertEquals("setMeasurementMode() for PEAK_RMS doesn't report success",
+                    Visualizer.SUCCESS, status);
+            // make sure we're playing long enough so the measurement is valid
+            int currentPosition = mp.getCurrentPosition();
+            int maxTry = 100;
+            int tryCount = 0;
+            while (currentPosition < 200 && tryCount < maxTry) {
+                Thread.sleep(50);
+                currentPosition = mp.getCurrentPosition();
+                tryCount++;
+            }
+            assertTrue("MediaPlayer not ready", tryCount < maxTry);
+
+            MeasurementPeakRms measurement = new MeasurementPeakRms();
+            status = visualizer.getMeasurementPeakRms(measurement);
+
+            //run for a new set of 3 seconds, get new measurement
+            mLE.setTargetGain(LOUDNESS_GAIN);
+            assertEquals("target gain differs from value set", (float)LOUDNESS_GAIN,
+                    mLE.getTargetGain());
+
+            mLE.setEnabled(true);
+
+            visualizer.setMeasurementMode(Visualizer.MEASUREMENT_MODE_PEAK_RMS);
+            currentPosition = mp.getCurrentPosition();
+            maxTry = 5;
+            tryCount = 0;
+            while (tryCount < maxTry) {
+                Thread.sleep(50);
+                tryCount++;
+            }
+
+            MeasurementPeakRms measurement2 = new MeasurementPeakRms();
+            status = visualizer.getMeasurementPeakRms(measurement2);
+
+            //compare both measurements
+            am.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 0);
+            assertEquals("getMeasurementPeakRms() reports failure",
+                    Visualizer.SUCCESS, status);
+            Log.i("VisTest", "peak="+measurement.mPeak+"  rms="+measurement.mRms);
+            Log.i("VisTest", "peak2="+measurement2.mPeak+"  rms2="+measurement2.mRms);
+
+            int deltaPeak = Math.abs(measurement2.mPeak - (measurement.mPeak + LOUDNESS_GAIN) );
+            assertTrue("peak deviation in mB = "+deltaPeak, deltaPeak < MAX_MEASUREMENT_ERROR_MB);
+
+        } catch (IllegalStateException e) {
+            fail("method called in wrong state");
+        } catch (InterruptedException e) {
+            fail("sleep() interrupted");
+        } finally {
+            if (mp != null) {
+                mp.stop();
+                mp.release();
+            }
+
+            if (vc != null)
+                vc.release();
+
+            if (visualizer != null)
+                visualizer.release();
+
+            releaseLoudnessEnhancer();
+        }
+    }
+
     //-----------------------------------------------------------------
     // private methods
     //----------------------------------