1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.permission.cts; 18 19 import android.content.Context; 20 import android.media.AudioManager; 21 import android.test.AndroidTestCase; 22 import android.test.suitebuilder.annotation.SmallTest; 23 24 /** 25 * Verify the audio related operations require specific permissions. 26 */ 27 public class NoAudioPermissionTest extends AndroidTestCase { 28 private AudioManager mAudioManager; 29 private static final int MODE_COUNT = 3; 30 31 @Override 32 protected void setUp() throws Exception { 33 super.setUp(); 34 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 35 assertNotNull(mAudioManager); 36 } 37 38 /** 39 * Verify that AudioManager.setMicrophoneMute, AudioManager.setMode requires permissions. 40 * <p>Requires Permission: 41 * {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}. 42 */ 43 @SmallTest 44 public void testSetMicrophoneMute() { 45 boolean muteState = mAudioManager.isMicrophoneMute(); 46 int originalMode = mAudioManager.getMode(); 47 // If there is no permission of MODIFY_AUDIO_SETTINGS, setMicrophoneMute does nothing. 48 mAudioManager.setMicrophoneMute(!muteState); 49 assertEquals(muteState, mAudioManager.isMicrophoneMute()); 50 51 // If there is no permission of MODIFY_AUDIO_SETTINGS, setMode does nothing. 52 assertTrue(AudioManager.MODE_NORMAL != AudioManager.MODE_RINGTONE); 53 54 mAudioManager.setMode(AudioManager.MODE_NORMAL); 55 assertEquals(originalMode, mAudioManager.getMode()); 56 57 mAudioManager.setMode(AudioManager.MODE_RINGTONE); 58 assertEquals(originalMode, mAudioManager.getMode()); 59 } 60 61 /** 62 * Verify that AudioManager routing methods require permissions. 63 * <p>Requires Permission: 64 * {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}. 65 */ 66 @SuppressWarnings("deprecation") 67 @SmallTest 68 public void testRouting() { 69 70 // If there is no permission of MODIFY_AUDIO_SETTINGS, setSpeakerphoneOn does nothing. 71 boolean prevState = mAudioManager.isSpeakerphoneOn(); 72 mAudioManager.setSpeakerphoneOn(!prevState); 73 assertEquals(prevState, mAudioManager.isSpeakerphoneOn()); 74 75 // If there is no permission of MODIFY_AUDIO_SETTINGS, setBluetoothScoOn does nothing. 76 prevState = mAudioManager.isBluetoothScoOn(); 77 mAudioManager.setBluetoothScoOn(!prevState); 78 assertEquals(prevState, mAudioManager.isBluetoothScoOn()); 79 } 80 } 81