Add CTS tests for capture audio output permission.

Bug: 10265163
Change-Id: Ie44a6b0050fc473b16a453bd75087382b9d04d09
diff --git a/tests/tests/permission/src/android/permission/cts/NoCapturePermissionTest.java b/tests/tests/permission/src/android/permission/cts/NoCaptureVideoPermissionTest.java
similarity index 95%
rename from tests/tests/permission/src/android/permission/cts/NoCapturePermissionTest.java
rename to tests/tests/permission/src/android/permission/cts/NoCaptureVideoPermissionTest.java
index 5fa52d2..f84079a 100644
--- a/tests/tests/permission/src/android/permission/cts/NoCapturePermissionTest.java
+++ b/tests/tests/permission/src/android/permission/cts/NoCaptureVideoPermissionTest.java
@@ -17,7 +17,6 @@
 package android.permission.cts;
 
 import android.content.Context;
-import android.graphics.ImageFormat;
 import android.graphics.PixelFormat;
 import android.hardware.display.DisplayManager;
 import android.hardware.display.VirtualDisplay;
@@ -27,9 +26,9 @@
 import android.util.DisplayMetrics;
 
 /**
- * Verify the capture system audio output and video output permission requirements.
+ * Verify the capture system video output permission requirements.
  */
-public class NoCapturePermissionTest extends AndroidTestCase {
+public class NoCaptureVideoPermissionTest extends AndroidTestCase {
     private static final String NAME = "VirtualDisplayTest";
     private static final int WIDTH = 720;
     private static final int HEIGHT = 480;
diff --git a/tests/tests/permission2/AndroidManifest.xml b/tests/tests/permission2/AndroidManifest.xml
index 8cc7737..01ccb97 100755
--- a/tests/tests/permission2/AndroidManifest.xml
+++ b/tests/tests/permission2/AndroidManifest.xml
@@ -42,6 +42,9 @@
 
     <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
 
+    <!-- need app that has RECORD_AUDIO but not CAPTURE_AUDIO_OUTPUT -->
+    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
+
     <instrumentation android:name="android.test.InstrumentationCtsTestRunner"
                      android:targetPackage="com.android.cts.permission2"
                      android:label="More CTS tests for permissions"/>
diff --git a/tests/tests/permission2/src/android/permission2/cts/NoCaptureAudioOutputPermissionTest.java b/tests/tests/permission2/src/android/permission2/cts/NoCaptureAudioOutputPermissionTest.java
new file mode 100644
index 0000000..b4e2855
--- /dev/null
+++ b/tests/tests/permission2/src/android/permission2/cts/NoCaptureAudioOutputPermissionTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.permission2.cts;
+
+import android.media.AudioFormat;
+import android.media.AudioRecord;
+import android.media.MediaRecorder.AudioSource;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+/**
+ * Verify the capture system video output permission requirements.
+ */
+public class NoCaptureAudioOutputPermissionTest extends AndroidTestCase {
+    /**
+     * Verify that the AudioRecord constructor fails to create a recording object
+     * when the app does not have permission to capture audio output.
+     * For the purposes of this test, the app must already have the normal audio
+     * record permission, just not the capture audio output permission.
+     * <p>Requires permission:
+     *    {@link android.Manifest.permission#RECORD_AUDIO} and
+     *    {@link android.Manifest.permission#CAPTURE_VIDEO_OUTPUT}.
+     */
+    @SmallTest
+    public void testCreateAudioRecord() {
+        final int bufferSize = AudioRecord.getMinBufferSize(44100,
+                AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
+
+        // The attempt to create the AudioRecord object succeeds even if the
+        // app does not have permission, but the object is not usable.
+        // The API should probably throw SecurityException but it was not originally
+        // designed to do that and it's not clear we can change it now.
+        AudioRecord record = new AudioRecord(AudioSource.REMOTE_SUBMIX, 44100,
+                AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
+        try {
+            assertTrue("AudioRecord state should not be INITIALIZED because the application"
+                    + "does not have permission to access the remote submix source",
+                    record.getState() != AudioRecord.STATE_INITIALIZED);
+        } finally {
+            record.release();
+        }
+    }
+}