1 /* 2 * Copyright (C) 2023 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.assertEquals; 20 21 import android.content.Context; 22 import android.media.AudioAttributes; 23 import android.media.audiopolicy.AudioProductStrategy; 24 import android.os.PowerManager; 25 26 import java.util.function.IntSupplier; 27 28 class AudioTestUtil { 29 // Default matches the invalid (empty) attributes from native. 30 // The difference is the input source default which is not aligned between native and java 31 public static final AudioAttributes DEFAULT_ATTRIBUTES = 32 AudioProductStrategy.getDefaultAttributes(); 33 public static final AudioAttributes INVALID_ATTRIBUTES = new AudioAttributes.Builder().build(); 34 resetVolumeIndex(int indexMin, int indexMax)35 public static int resetVolumeIndex(int indexMin, int indexMax) { 36 return (indexMax + indexMin) / 2; 37 } 38 incrementVolumeIndex(int index, int indexMin, int indexMax)39 public static int incrementVolumeIndex(int index, int indexMin, int indexMax) { 40 return (index + 1 > indexMax) ? resetVolumeIndex(indexMin, indexMax) : ++index; 41 } 42 43 //----------------------------------------------------------------------------------- 44 45 /** 46 * A test helper class to help compare an expected int against the result of an IntSupplier 47 * lambda. It supports specifying a max wait time, broken down into Thread.sleep() of the 48 * given period. The expected value is compared against the result of the lambda every period. 49 * It will assert if the expected value is never returned after the maximum specified time. 50 * Example of how to use: 51 * <pre> 52 * final SleepAssertIntEquals test = new SleepAssertIntEquals( 53 * 5000, // max sleep duration is 5s 54 * 100, // test condition will be checked every 100ms 55 * getContext()); // strictly for the wakelock hold 56 * // do the operation under test 57 * mAudioManager.setStreamVolume(STREAM_MUSIC, 58 * mAudioManager.getMinStreamVolume(STREAM_MUSIC), 0); 59 * // sleep and check until the volume has changed to what the test expects, 60 * // it will throw an Exception if that doesn't happen within 5s 61 * test.assertEqualsSleep( mAudioManager.getMinStreamVolume(STREAM_MUSIC), // expected value 62 * () -> mAudioManager.getStreamVolume(STREAM_MUSIC), 63 * "Observed volume not at min for MUSIC"); 64 * </pre> 65 */ 66 public static class SleepAssertIntEquals { 67 final long mMaxWaitMs; 68 final long mPeriodMs; 69 private PowerManager.WakeLock mWakeLock; 70 71 /** 72 * Constructor for the test utility 73 * @param maxWaitMs the maximum time this test will ever wait 74 * @param periodMs the period to sleep for in between test attempts, 75 * must be less than maxWaitMs 76 * @param context not retained, just for obtaining a partial wakelock from PowerManager 77 */ SleepAssertIntEquals(int maxWaitMs, int periodMs, Context context)78 SleepAssertIntEquals(int maxWaitMs, int periodMs, Context context) { 79 if (periodMs >= maxWaitMs) { 80 throw new IllegalArgumentException("Period must be lower than max wait time"); 81 } 82 mMaxWaitMs = maxWaitMs; 83 mPeriodMs = periodMs; 84 PowerManager pm = context.getSystemService(PowerManager.class); 85 mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SleepAssertIntEquals"); 86 } 87 88 /** 89 * Compares the expected against the result of the lambda until they're equals, or unless 90 * the max wait time has elapsed, whichever happens first. On a timeout (int result wasn't 91 * as expected), the method asserts. 92 * @param expected the expected int value in the test 93 * @param result the function returning an int under test 94 * @param message the message to display when asserting 95 * @throws InterruptedException 96 */ assertEqualsSleep(int expected, IntSupplier result, String message)97 public void assertEqualsSleep(int expected, IntSupplier result, String message) 98 throws InterruptedException { 99 final long endMs = System.currentTimeMillis() + mMaxWaitMs; 100 try { 101 mWakeLock.acquire(); 102 int actual = Integer.MIN_VALUE; 103 while (System.currentTimeMillis() < endMs) { 104 actual = result.getAsInt(); 105 if (actual == expected) { 106 // test successful, stop 107 return; 108 } else { 109 // wait some more before expecting the test to be successful 110 Thread.sleep(mPeriodMs); 111 } 112 } 113 assertEquals(message, expected, actual); 114 } finally { 115 mWakeLock.release(); 116 } 117 } 118 } 119 120 } 121