CTS test for surround sound APIs setEncodedSurroundMode and setSurroundFormatEnabled

Bug: 180997010
Test: atest AudioManagerTest
Change-Id: I840218421e71c489d3773fa541ed5370515446f5
diff --git a/tests/tests/media/src/android/media/cts/AudioManagerTest.java b/tests/tests/media/src/android/media/cts/AudioManagerTest.java
index 8b768ee..0c7fa25 100644
--- a/tests/tests/media/src/android/media/cts/AudioManagerTest.java
+++ b/tests/tests/media/src/android/media/cts/AudioManagerTest.java
@@ -44,6 +44,7 @@
 import static android.media.AudioManager.VIBRATE_TYPE_RINGER;
 import static android.provider.Settings.System.SOUND_EFFECTS_ENABLED;
 
+import android.Manifest;
 import android.app.NotificationChannel;
 import android.app.NotificationManager;
 import android.content.BroadcastReceiver;
@@ -55,6 +56,7 @@
 import android.media.AudioAttributes;
 import android.media.AudioDeviceAttributes;
 import android.media.AudioDeviceInfo;
+import android.media.AudioFormat;
 import android.media.AudioManager;
 import android.media.AudioProfile;
 import android.media.MediaPlayer;
@@ -72,11 +74,16 @@
 import android.util.Log;
 import android.view.SoundEffectConstants;
 
+import androidx.test.InstrumentationRegistry;
+
 import com.android.compatibility.common.util.ApiLevelUtil;
 import com.android.compatibility.common.util.CddTest;
 import com.android.compatibility.common.util.MediaUtils;
+import com.android.compatibility.common.util.SettingsStateKeeperRule;
 import com.android.internal.annotations.GuardedBy;
 
+import org.junit.ClassRule;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -120,6 +127,16 @@
     private boolean mDoNotCheckUnmute;
     private boolean mAppsBypassingDnd;
 
+    @ClassRule
+    public static final SettingsStateKeeperRule mSurroundSoundFormatsSettingsKeeper =
+            new SettingsStateKeeperRule(InstrumentationRegistry.getTargetContext(),
+                    Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS);
+
+    @ClassRule
+    public static final SettingsStateKeeperRule mSurroundSoundModeSettingsKeeper =
+            new SettingsStateKeeperRule(InstrumentationRegistry.getTargetContext(),
+                    Settings.Global.ENCODED_SURROUND_OUTPUT);
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -367,6 +384,36 @@
         assertEquals(MODE_NORMAL, mAudioManager.getMode());
     }
 
+    public void testSetSurroundFormatEnabled() throws Exception {
+        getInstrumentation().getUiAutomation().adoptShellPermissionIdentity(
+                Manifest.permission.WRITE_SETTINGS);
+
+        int audioFormat = AudioFormat.ENCODING_DTS;
+
+        mAudioManager.setSurroundFormatEnabled(audioFormat, true /*enabled*/);
+        assertTrue(mAudioManager.isSurroundFormatEnabled(audioFormat));
+
+        mAudioManager.setSurroundFormatEnabled(audioFormat, false /*enabled*/);
+        assertFalse(mAudioManager.isSurroundFormatEnabled(audioFormat));
+
+        getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
+    }
+
+    public void testSetEncodedSurroundMode() throws Exception {
+        getInstrumentation().getUiAutomation().adoptShellPermissionIdentity(
+                Manifest.permission.WRITE_SETTINGS);
+
+        int expectedSurroundFormatsMode = Settings.Global.ENCODED_SURROUND_OUTPUT_MANUAL;
+        mAudioManager.setEncodedSurroundMode(expectedSurroundFormatsMode);
+        assertEquals(expectedSurroundFormatsMode, mAudioManager.getEncodedSurroundMode());
+
+        expectedSurroundFormatsMode = Settings.Global.ENCODED_SURROUND_OUTPUT_NEVER;
+        mAudioManager.setEncodedSurroundMode(expectedSurroundFormatsMode);
+        assertEquals(expectedSurroundFormatsMode, mAudioManager.getEncodedSurroundMode());
+
+        getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
+    }
+
     @SuppressWarnings("deprecation")
     @AppModeFull(reason = "Instant apps cannot hold android.permission.MODIFY_AUDIO_SETTINGS")
     public void testRouting() throws Exception {