1 /* 2 * Copyright (C) 2024 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.media.audio.cts; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 import static org.junit.Assume.assumeFalse; 22 23 import android.media.audiofx.AudioEffect; 24 25 import androidx.test.platform.app.InstrumentationRegistry; 26 27 import org.junit.rules.ExternalResource; 28 29 import java.util.HashMap; 30 import java.util.Map; 31 import java.util.UUID; 32 33 /** 34 * Simple before and after rule for audio effect tests 35 */ 36 final class EffectBeforeAfterRule extends ExternalResource { 37 private static final String TAG = EffectBeforeAfterRule.class.getSimpleName(); 38 39 private static final int CONTROL_PRIORITY = 100; 40 41 private Map<UUID, Boolean> mOriginalEffectState = new HashMap<>(); 42 43 /** 44 * All potential effects under test are disabled at setup. 45 * Initial state is backuped, restored in tearDown 46 * @throws Exception 47 */ 48 @Override before()49 protected void before() { 50 51 final UUID[] effectTypes = { 52 AudioEffect.EFFECT_TYPE_BASS_BOOST, 53 AudioEffect.EFFECT_TYPE_EQUALIZER, 54 AudioEffect.EFFECT_TYPE_VIRTUALIZER, 55 AudioEffect.EFFECT_TYPE_PRESET_REVERB, 56 AudioEffect.EFFECT_TYPE_ENV_REVERB, 57 }; 58 for (UUID effectType : effectTypes) { 59 try { 60 if (AudioEffect.isEffectTypeAvailable(effectType)) { 61 AudioEffect effect = new AudioEffect(effectType, 62 AudioEffect.EFFECT_TYPE_NULL, 63 CONTROL_PRIORITY, 64 0); 65 assertTrue("effect does not have control", effect.hasControl()); 66 mOriginalEffectState.put(effectType, effect.getEnabled()); 67 68 effect.setEnabled(false); 69 assertFalse("Could not disable effect", effect.getEnabled()); 70 effect.release(); 71 } 72 } catch (IllegalStateException e) { 73 } catch (IllegalArgumentException e) { 74 } catch (UnsupportedOperationException e) { 75 } catch (RuntimeException e) { 76 assumeFalse("skipping for instant", 77 InstrumentationRegistry.getInstrumentation() 78 .getContext().getPackageManager().isInstantApp()); 79 } 80 } 81 } 82 83 @Override after()84 protected void after() { 85 for (Map.Entry<UUID, Boolean> entry : mOriginalEffectState.entrySet()) { 86 try { 87 AudioEffect effect = new AudioEffect(entry.getKey(), 88 AudioEffect.EFFECT_TYPE_NULL, 89 CONTROL_PRIORITY, 90 0); 91 assertTrue("effect does not have control", effect.hasControl()); 92 effect.setEnabled(entry.getValue()); 93 effect.release(); 94 } catch (IllegalStateException e) { 95 } catch (IllegalArgumentException e) { 96 } catch (UnsupportedOperationException e) { 97 } 98 } 99 } 100 } 101