Fixed testAudioRestriction for managed profile owner.

Disabled testAudioRestriction for managed profile, because
DISALLOW_UNMUTE_MICROPHONE can only be set by device owners
and profile owners on the primary user. Added/enabled
tests for DISALLOW_ADJUST_VOLUME. Also added permission
to run AudioManager.setMicrophoneMute().

BUG:27743508
Change-Id: Ieb5aeb07fc352bd1de7b67c61a46b0198ba38237
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/latest/AndroidManifest.xml b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/latest/AndroidManifest.xml
index 5bd902c..eb337a3 100644
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/latest/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/latest/AndroidManifest.xml
@@ -23,6 +23,7 @@
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
     <uses-permission android:name="android.permission.SET_WALLPAPER" />
+    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
 
     <application>
         <uses-library android:name="android.test.runner" />
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/AudioRestrictionTest.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/AudioRestrictionTest.java
index 0c60fa4..838580b 100644
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/AudioRestrictionTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/AudioRestrictionTest.java
@@ -27,6 +27,12 @@
 public class AudioRestrictionTest extends BaseDeviceAdminTest {
 
     private AudioManager mAudioManager;
+    private final Callable<Boolean> mCheckIfMasterVolumeMuted = new Callable<Boolean>() {
+        @Override
+        public Boolean call() throws Exception {
+            return mDevicePolicyManager.isMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT);
+        }
+    };
 
     @Override
     protected void setUp() throws Exception {
@@ -34,7 +40,37 @@
         mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
     }
 
-    private void disallowAdjustVolumeTestInternal(boolean setByAddUserRestiction) throws Exception {
+    // Here we test that DISALLOW_ADJUST_VOLUME disallows to unmute volume.
+    public void testDisallowAdjustVolume_muted() throws Exception {
+        // If we check that some value did not change, we must wait until the action is applied.
+        // Method waitUntil() may check old value before changes took place.
+        final int WAIT_TIME_MS = 1000;
+        final boolean initVolumeMuted =
+                mDevicePolicyManager.isMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT);
+        try {
+            // Unmute volume, if necessary.
+            if (initVolumeMuted) {
+                mDevicePolicyManager.setMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT, false);
+                waitUntil(false, mCheckIfMasterVolumeMuted);
+            }
+
+            // DISALLOW_ADJUST_VOLUME must mute volume.
+            mDevicePolicyManager.addUserRestriction(ADMIN_RECEIVER_COMPONENT,
+                    UserManager.DISALLOW_ADJUST_VOLUME);
+            waitUntil(true, mCheckIfMasterVolumeMuted);
+
+            // Unmute should not have effect because the restriction does not allow this.
+            mDevicePolicyManager.setMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT, false);
+            Thread.sleep(WAIT_TIME_MS);
+            assertTrue(mDevicePolicyManager.isMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT));
+        } finally {
+            mDevicePolicyManager.clearUserRestriction(ADMIN_RECEIVER_COMPONENT,
+                    UserManager.DISALLOW_ADJUST_VOLUME);
+            mDevicePolicyManager.setMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT, initVolumeMuted);
+        }
+    }
+
+    public void testDisallowAdjustVolume() throws Exception {
         try {
             // Set volume of ringtone to be 1.
             mAudioManager.setStreamVolume(AudioManager.STREAM_RING, 1, /* flag= */ 0);
@@ -42,12 +78,8 @@
             // Disallow adjusting volume.
             mDevicePolicyManager.addUserRestriction(ADMIN_RECEIVER_COMPONENT,
                     UserManager.DISALLOW_ADJUST_VOLUME);
-            waitUntil(true, new Callable<Boolean>() {
-                @Override
-                public Boolean call() throws Exception {
-                    return mDevicePolicyManager.isMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT);
-                }
-            });
+            waitUntil(true, mCheckIfMasterVolumeMuted);
+
             // Verify that volume can't be changed.
             mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, /* flag= */ 0);
             assertEquals(1, mAudioManager.getStreamVolume(AudioManager.STREAM_RING));
@@ -55,12 +87,8 @@
             // Allowing adjusting volume.
             mDevicePolicyManager.clearUserRestriction(ADMIN_RECEIVER_COMPONENT,
                     UserManager.DISALLOW_ADJUST_VOLUME);
-            waitUntil(false, new Callable<Boolean>() {
-                @Override
-                public Boolean call() throws Exception {
-                    return mDevicePolicyManager.isMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT);
-                }
-            });
+            waitUntil(false, mCheckIfMasterVolumeMuted);
+
             // Verify the volume can be changed now.
             mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE,  /* flag= */ 0);
             waitUntil(2, new Callable<Integer>() {
@@ -73,12 +101,7 @@
             // Clear the restriction.
             mDevicePolicyManager.clearUserRestriction(ADMIN_RECEIVER_COMPONENT,
                     UserManager.DISALLOW_ADJUST_VOLUME);
-            waitUntil(false, new Callable<Boolean>() {
-                @Override
-                public Boolean call() throws Exception {
-                    return mDevicePolicyManager.isMasterVolumeMuted(ADMIN_RECEIVER_COMPONENT);
-                }
-            });
+            waitUntil(false, mCheckIfMasterVolumeMuted);
         }
     }
 
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedManagedProfileOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedManagedProfileOwnerTest.java
index 32110de..0f90959 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedManagedProfileOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedManagedProfileOwnerTest.java
@@ -91,4 +91,10 @@
     public void testDisallowSetWallpaper_allowed() throws Exception {
         // Managed profile doesn't have wallpaper.
     }
+
+    @Override
+    public void testAudioRestriction() throws Exception {
+        // DISALLOW_UNMUTE_MICROPHONE and DISALLOW_ADJUST_VOLUME can only be set by device owners
+        // and profile owners on the primary user.
+    }
 }