Migrate audio policy test to AndroidJUnit4
Bug: 247536210 Test: atest com.android.audiopolicytest Change-Id: Ia200a17884c22f461df24b342fbf279090e724c5
This commit is contained in:
committed by
Ján Sebechlebský
parent
6f677a36ec
commit
d6f0bdb840
@@ -10,15 +10,11 @@ package {
|
||||
android_test {
|
||||
name: "audiopolicytest",
|
||||
srcs: ["**/*.java"],
|
||||
libs: [
|
||||
"android.test.runner",
|
||||
"android.test.base",
|
||||
],
|
||||
static_libs: [
|
||||
"mockito-target-minus-junit4",
|
||||
"androidx.test.ext.junit",
|
||||
"androidx.test.rules",
|
||||
"android-ex-camera2",
|
||||
"testng",
|
||||
"guava",
|
||||
"platform-test-annotations",
|
||||
],
|
||||
platform_apis: true,
|
||||
certificate: "platform",
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
<application>
|
||||
<uses-library android:name="android.test.runner" />
|
||||
<activity android:label="@string/app_name" android:name="AudioPolicyTest"
|
||||
<activity android:label="@string/app_name" android:name="AudioPolicyTestActivity"
|
||||
android:screenOrientation="landscape" android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
@@ -33,11 +33,6 @@
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
<!--instrumentation android:name=".AudioPolicyTestRunner"
|
||||
android:targetPackage="com.android.audiopolicytest"
|
||||
android:label="AudioManager policy oriented integration tests InstrumentationRunner">
|
||||
</instrumentation-->
|
||||
|
||||
<instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
|
||||
android:targetPackage="com.android.audiopolicytest"
|
||||
android:label="AudioManager policy oriented integration tests InstrumentationRunner">
|
||||
|
||||
@@ -16,28 +16,57 @@
|
||||
|
||||
package com.android.audiopolicytest;
|
||||
|
||||
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
|
||||
|
||||
import static com.android.audiopolicytest.AudioVolumeTestUtil.DEFAULT_ATTRIBUTES;
|
||||
import static com.android.audiopolicytest.AudioVolumeTestUtil.incrementVolumeIndex;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.testng.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.media.AudioSystem;
|
||||
import android.media.audiopolicy.AudioProductStrategy;
|
||||
import android.media.audiopolicy.AudioVolumeGroup;
|
||||
import android.platform.test.annotations.Presubmit;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AudioManagerTest extends AudioVolumesTestBase {
|
||||
@Presubmit
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class AudioManagerTest {
|
||||
private static final String TAG = "AudioManagerTest";
|
||||
|
||||
private AudioManager mAudioManager;
|
||||
|
||||
@Rule
|
||||
public final AudioVolumesTestRule rule = new AudioVolumesTestRule();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mAudioManager = getApplicationContext().getSystemService(AudioManager.class);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Test getAudioProductStrategies and validate strategies
|
||||
//-----------------------------------------------------------------
|
||||
public void testGetAndValidateProductStrategies() throws Exception {
|
||||
@Test
|
||||
public void testGetAndValidateProductStrategies() {
|
||||
List<AudioProductStrategy> audioProductStrategies =
|
||||
mAudioManager.getAudioProductStrategies();
|
||||
assertTrue(audioProductStrategies.size() > 0);
|
||||
@@ -101,8 +130,8 @@ public class AudioManagerTest extends AudioVolumesTestBase {
|
||||
//-----------------------------------------------------------------
|
||||
// Test getAudioVolumeGroups and validate volume groups
|
||||
//-----------------------------------------------------------------
|
||||
|
||||
public void testGetAndValidateVolumeGroups() throws Exception {
|
||||
@Test
|
||||
public void testGetAndValidateVolumeGroups() {
|
||||
List<AudioVolumeGroup> audioVolumeGroups = mAudioManager.getAudioVolumeGroups();
|
||||
assertTrue(audioVolumeGroups.size() > 0);
|
||||
|
||||
@@ -118,7 +147,7 @@ public class AudioManagerTest extends AudioVolumesTestBase {
|
||||
// for each volume group attributes, find the matching product strategy and ensure
|
||||
// it is linked the considered volume group
|
||||
for (final AudioAttributes aa : avgAttributes) {
|
||||
if (aa.equals(sDefaultAttributes)) {
|
||||
if (aa.equals(DEFAULT_ATTRIBUTES)) {
|
||||
// Some volume groups may not have valid attributes, used for internal
|
||||
// volume management like patch/rerouting
|
||||
// so bailing out strategy retrieval from attributes
|
||||
@@ -180,6 +209,7 @@ public class AudioManagerTest extends AudioVolumesTestBase {
|
||||
//-----------------------------------------------------------------
|
||||
// Test Volume per Attributes setter/getters
|
||||
//-----------------------------------------------------------------
|
||||
@Test
|
||||
public void testSetGetVolumePerAttributesWithInvalidAttributes() throws Exception {
|
||||
AudioAttributes nullAttributes = null;
|
||||
|
||||
@@ -197,7 +227,8 @@ public class AudioManagerTest extends AudioVolumesTestBase {
|
||||
nullAttributes, 0 /*index*/, 0/*flags*/));
|
||||
}
|
||||
|
||||
public void testSetGetVolumePerAttributes() throws Exception {
|
||||
@Test
|
||||
public void testSetGetVolumePerAttributes() {
|
||||
for (int usage : AudioAttributes.SDK_USAGES) {
|
||||
if (usage == AudioAttributes.USAGE_UNKNOWN) {
|
||||
continue;
|
||||
@@ -248,12 +279,14 @@ public class AudioManagerTest extends AudioVolumesTestBase {
|
||||
//-----------------------------------------------------------------
|
||||
// Test register/unregister VolumeGroupCallback
|
||||
//-----------------------------------------------------------------
|
||||
public void testVolumeGroupCallback() throws Exception {
|
||||
@Test
|
||||
public void testVolumeGroupCallback() {
|
||||
List<AudioVolumeGroup> audioVolumeGroups = mAudioManager.getAudioVolumeGroups();
|
||||
assertTrue(audioVolumeGroups.size() > 0);
|
||||
|
||||
AudioVolumeGroupCallbackHelper vgCbReceiver = new AudioVolumeGroupCallbackHelper();
|
||||
mAudioManager.registerVolumeGroupCallback(mContext.getMainExecutor(), vgCbReceiver);
|
||||
mAudioManager.registerVolumeGroupCallback(getApplicationContext().getMainExecutor(),
|
||||
vgCbReceiver);
|
||||
|
||||
final List<Integer> publicStreams = Ints.asList(AudioManager.getPublicStreamTypes());
|
||||
try {
|
||||
@@ -273,7 +306,7 @@ public class AudioManagerTest extends AudioVolumesTestBase {
|
||||
|
||||
// Set the volume per attributes (if valid) and wait the callback
|
||||
for (final AudioAttributes aa : avgAttributes) {
|
||||
if (aa.equals(sDefaultAttributes)) {
|
||||
if (aa.equals(DEFAULT_ATTRIBUTES)) {
|
||||
// Some volume groups may not have valid attributes, used for internal
|
||||
// volume management like patch/rerouting
|
||||
// so bailing out strategy retrieval from attributes
|
||||
|
||||
@@ -19,9 +19,9 @@ package com.android.audiopolicytest;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
|
||||
public class AudioPolicyTest extends Activity {
|
||||
public class AudioPolicyTestActivity extends Activity {
|
||||
|
||||
public AudioPolicyTest() {
|
||||
public AudioPolicyTestActivity() {
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@@ -16,25 +16,43 @@
|
||||
|
||||
package com.android.audiopolicytest;
|
||||
|
||||
import static com.android.audiopolicytest.AudioVolumeTestUtil.INVALID_ATTRIBUTES;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.media.AudioSystem;
|
||||
import android.media.audiopolicy.AudioProductStrategy;
|
||||
import android.media.audiopolicy.AudioVolumeGroup;
|
||||
import android.platform.test.annotations.Presubmit;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AudioProductStrategyTest extends AudioVolumesTestBase {
|
||||
@Presubmit
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class AudioProductStrategyTest {
|
||||
private static final String TAG = "AudioProductStrategyTest";
|
||||
|
||||
@Rule
|
||||
public final AudioVolumesTestRule rule = new AudioVolumesTestRule();
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Test getAudioProductStrategies and validate strategies
|
||||
//-----------------------------------------------------------------
|
||||
public void testGetProductStrategies() throws Exception {
|
||||
@Test
|
||||
public void testGetProductStrategies() {
|
||||
List<AudioProductStrategy> audioProductStrategies =
|
||||
AudioProductStrategy.getAudioProductStrategies();
|
||||
|
||||
@@ -65,6 +83,7 @@ public class AudioProductStrategyTest extends AudioVolumesTestBase {
|
||||
//-----------------------------------------------------------------
|
||||
// Test stream to/from attributes conversion
|
||||
//-----------------------------------------------------------------
|
||||
@Test
|
||||
public void testAudioAttributesFromStreamTypes() throws Exception {
|
||||
List<AudioProductStrategy> audioProductStrategies =
|
||||
AudioProductStrategy.getAudioProductStrategies();
|
||||
@@ -81,7 +100,7 @@ public class AudioProductStrategyTest extends AudioVolumesTestBase {
|
||||
// hosting this stream type; Bailing out the test, just ensure that any request
|
||||
// for reciproque API with the unknown attributes would return default stream
|
||||
// for volume control, aka STREAM_MUSIC.
|
||||
if (aaFromStreamType.equals(sInvalidAttributes)) {
|
||||
if (aaFromStreamType.equals(INVALID_ATTRIBUTES)) {
|
||||
assertEquals(AudioSystem.STREAM_MUSIC,
|
||||
AudioProductStrategy.getLegacyStreamTypeForStrategyWithAudioAttributes(
|
||||
aaFromStreamType));
|
||||
@@ -139,7 +158,8 @@ public class AudioProductStrategyTest extends AudioVolumesTestBase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testAudioAttributesToStreamTypes() throws Exception {
|
||||
@Test
|
||||
public void testAudioAttributesToStreamTypes() {
|
||||
List<AudioProductStrategy> audioProductStrategies =
|
||||
AudioProductStrategy.getAudioProductStrategies();
|
||||
|
||||
|
||||
@@ -16,21 +16,48 @@
|
||||
|
||||
package com.android.audiopolicytest;
|
||||
|
||||
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
|
||||
|
||||
import static com.android.audiopolicytest.AudioVolumeTestUtil.DEFAULT_ATTRIBUTES;
|
||||
import static com.android.audiopolicytest.AudioVolumeTestUtil.incrementVolumeIndex;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.media.audiopolicy.AudioVolumeGroup;
|
||||
import android.media.audiopolicy.AudioVolumeGroupChangeHandler;
|
||||
import android.platform.test.annotations.Presubmit;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AudioVolumeGroupChangeHandlerTest extends AudioVolumesTestBase {
|
||||
@Presubmit
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class AudioVolumeGroupChangeHandlerTest {
|
||||
private static final String TAG = "AudioVolumeGroupChangeHandlerTest";
|
||||
|
||||
public void testRegisterInvalidCallback() throws Exception {
|
||||
@Rule
|
||||
public final AudioVolumesTestRule rule = new AudioVolumesTestRule();
|
||||
|
||||
private AudioManager mAudioManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mAudioManager = getApplicationContext().getSystemService(AudioManager.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterInvalidCallback() {
|
||||
final AudioVolumeGroupChangeHandler audioAudioVolumeGroupChangedHandler =
|
||||
new AudioVolumeGroupChangeHandler();
|
||||
|
||||
@@ -42,7 +69,8 @@ public class AudioVolumeGroupChangeHandlerTest extends AudioVolumesTestBase {
|
||||
});
|
||||
}
|
||||
|
||||
public void testUnregisterInvalidCallback() throws Exception {
|
||||
@Test
|
||||
public void testUnregisterInvalidCallback() {
|
||||
final AudioVolumeGroupChangeHandler audioAudioVolumeGroupChangedHandler =
|
||||
new AudioVolumeGroupChangeHandler();
|
||||
|
||||
@@ -58,7 +86,8 @@ public class AudioVolumeGroupChangeHandlerTest extends AudioVolumesTestBase {
|
||||
audioAudioVolumeGroupChangedHandler.unregisterListener(cb);
|
||||
}
|
||||
|
||||
public void testRegisterUnregisterCallback() throws Exception {
|
||||
@Test
|
||||
public void testRegisterUnregisterCallback() {
|
||||
final AudioVolumeGroupChangeHandler audioAudioVolumeGroupChangedHandler =
|
||||
new AudioVolumeGroupChangeHandler();
|
||||
|
||||
@@ -72,7 +101,8 @@ public class AudioVolumeGroupChangeHandlerTest extends AudioVolumesTestBase {
|
||||
audioAudioVolumeGroupChangedHandler.unregisterListener(validCb);
|
||||
}
|
||||
|
||||
public void testCallbackReceived() throws Exception {
|
||||
@Test
|
||||
public void testCallbackReceived() {
|
||||
final AudioVolumeGroupChangeHandler audioAudioVolumeGroupChangedHandler =
|
||||
new AudioVolumeGroupChangeHandler();
|
||||
|
||||
@@ -90,7 +120,7 @@ public class AudioVolumeGroupChangeHandlerTest extends AudioVolumesTestBase {
|
||||
|
||||
List<AudioAttributes> avgAttributes = audioVolumeGroup.getAudioAttributes();
|
||||
// Set the volume per attributes (if valid) and wait the callback
|
||||
if (avgAttributes.size() == 0 || avgAttributes.get(0).equals(sDefaultAttributes)) {
|
||||
if (avgAttributes.size() == 0 || avgAttributes.get(0).equals(DEFAULT_ATTRIBUTES)) {
|
||||
// Some volume groups may not have valid attributes, used for internal
|
||||
// volume management like patch/rerouting
|
||||
// so bailing out strategy retrieval from attributes
|
||||
@@ -118,7 +148,8 @@ public class AudioVolumeGroupChangeHandlerTest extends AudioVolumesTestBase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testMultipleCallbackReceived() throws Exception {
|
||||
@Test
|
||||
public void testMultipleCallbackReceived() {
|
||||
|
||||
final AudioVolumeGroupChangeHandler audioAudioVolumeGroupChangedHandler =
|
||||
new AudioVolumeGroupChangeHandler();
|
||||
@@ -144,7 +175,7 @@ public class AudioVolumeGroupChangeHandlerTest extends AudioVolumesTestBase {
|
||||
|
||||
List<AudioAttributes> avgAttributes = audioVolumeGroup.getAudioAttributes();
|
||||
// Set the volume per attributes (if valid) and wait the callback
|
||||
if (avgAttributes.size() == 0 || avgAttributes.get(0).equals(sDefaultAttributes)) {
|
||||
if (avgAttributes.size() == 0 || avgAttributes.get(0).equals(DEFAULT_ATTRIBUTES)) {
|
||||
// Some volume groups may not have valid attributes, used for internal
|
||||
// volume management like patch/rerouting
|
||||
// so bailing out strategy retrieval from attributes
|
||||
|
||||
@@ -16,22 +16,51 @@
|
||||
|
||||
package com.android.audiopolicytest;
|
||||
|
||||
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
|
||||
|
||||
import static com.android.audiopolicytest.AudioVolumeTestUtil.DEFAULT_ATTRIBUTES;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.media.AudioSystem;
|
||||
import android.media.audiopolicy.AudioProductStrategy;
|
||||
import android.media.audiopolicy.AudioVolumeGroup;
|
||||
import android.platform.test.annotations.Presubmit;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AudioVolumeGroupTest extends AudioVolumesTestBase {
|
||||
@Presubmit
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class AudioVolumeGroupTest {
|
||||
private static final String TAG = "AudioVolumeGroupTest";
|
||||
|
||||
@Rule
|
||||
public final AudioVolumesTestRule rule = new AudioVolumesTestRule();
|
||||
|
||||
private AudioManager mAudioManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mAudioManager = getApplicationContext().getSystemService(AudioManager.class);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
// Test getAudioVolumeGroups and validate groud id
|
||||
//-----------------------------------------------------------------
|
||||
public void testGetVolumeGroupsFromNonServiceCaller() throws Exception {
|
||||
@Test
|
||||
public void testGetVolumeGroupsFromNonServiceCaller() {
|
||||
// The transaction behind getAudioVolumeGroups will fail. Check is done at binder level
|
||||
// with policy service. Error is not reported, the list is just empty.
|
||||
// Request must come from service components
|
||||
@@ -44,7 +73,8 @@ public class AudioVolumeGroupTest extends AudioVolumesTestBase {
|
||||
//-----------------------------------------------------------------
|
||||
// Test getAudioVolumeGroups and validate groud id
|
||||
//-----------------------------------------------------------------
|
||||
public void testGetVolumeGroups() throws Exception {
|
||||
@Test
|
||||
public void testGetVolumeGroups() {
|
||||
// Through AudioManager, the transaction behind getAudioVolumeGroups will succeed
|
||||
final List<AudioVolumeGroup> audioVolumeGroup = mAudioManager.getAudioVolumeGroups();
|
||||
assertNotNull(audioVolumeGroup);
|
||||
@@ -67,7 +97,7 @@ public class AudioVolumeGroupTest extends AudioVolumesTestBase {
|
||||
// for each volume group attributes, find the matching product strategy and ensure
|
||||
// it is linked the considered volume group
|
||||
for (final AudioAttributes aa : avgAttributes) {
|
||||
if (aa.equals(sDefaultAttributes)) {
|
||||
if (aa.equals(DEFAULT_ATTRIBUTES)) {
|
||||
// Some volume groups may not have valid attributes, used for internal
|
||||
// volume management like patch/rerouting
|
||||
// so bailing out strategy retrieval from attributes
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.audiopolicytest;
|
||||
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.audiopolicy.AudioProductStrategy;
|
||||
|
||||
class AudioVolumeTestUtil {
|
||||
// Default matches the invalid (empty) attributes from native.
|
||||
// The difference is the input source default which is not aligned between native and java
|
||||
public static final AudioAttributes DEFAULT_ATTRIBUTES =
|
||||
AudioProductStrategy.getDefaultAttributes();
|
||||
public static final AudioAttributes INVALID_ATTRIBUTES = new AudioAttributes.Builder().build();
|
||||
|
||||
public static int resetVolumeIndex(int indexMin, int indexMax) {
|
||||
return (indexMax + indexMin) / 2;
|
||||
}
|
||||
|
||||
public static int incrementVolumeIndex(int index, int indexMin, int indexMax) {
|
||||
return (index + 1 > indexMax) ? resetVolumeIndex(indexMin, indexMax) : ++index;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,35 +16,36 @@
|
||||
|
||||
package com.android.audiopolicytest;
|
||||
|
||||
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
|
||||
|
||||
import static com.android.audiopolicytest.AudioVolumeTestUtil.DEFAULT_ATTRIBUTES;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioManager;
|
||||
import android.media.audiopolicy.AudioProductStrategy;
|
||||
import android.media.audiopolicy.AudioVolumeGroup;
|
||||
import android.test.ActivityInstrumentationTestCase2;
|
||||
|
||||
import androidx.test.core.app.ActivityScenario;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.rules.ExternalResource;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class AudioVolumesTestBase extends ActivityInstrumentationTestCase2<AudioPolicyTest> {
|
||||
public AudioManager mAudioManager;
|
||||
Context mContext;
|
||||
final class AudioVolumesTestRule extends ExternalResource {
|
||||
private AudioManager mAudioManager;
|
||||
private Context mContext;
|
||||
private Map<Integer, Integer> mOriginalStreamVolumes = new HashMap<>();
|
||||
private Map<Integer, Integer> mOriginalVolumeGroupVolumes = new HashMap<>();
|
||||
|
||||
// Default matches the invalid (empty) attributes from native.
|
||||
// The difference is the input source default which is not aligned between native and java
|
||||
public static final AudioAttributes sDefaultAttributes =
|
||||
AudioProductStrategy.getDefaultAttributes();
|
||||
|
||||
public static final AudioAttributes sInvalidAttributes = new AudioAttributes.Builder().build();
|
||||
|
||||
public AudioVolumesTestBase() {
|
||||
super("com.android.audiopolicytest", AudioPolicyTest.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Note: must be called with shell permission (MODIFY_AUDIO_ROUTING)
|
||||
*/
|
||||
@@ -56,14 +57,14 @@ public class AudioVolumesTestBase extends ActivityInstrumentationTestCase2<Audio
|
||||
// like rerouting/patch since these groups are internal to audio policy manager
|
||||
continue;
|
||||
}
|
||||
AudioAttributes avgAttributes = sDefaultAttributes;
|
||||
AudioAttributes avgAttributes = DEFAULT_ATTRIBUTES;
|
||||
for (final AudioAttributes aa : avg.getAudioAttributes()) {
|
||||
if (!aa.equals(AudioProductStrategy.getDefaultAttributes())) {
|
||||
avgAttributes = aa;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (avgAttributes.equals(sDefaultAttributes)) {
|
||||
if (avgAttributes.equals(DEFAULT_ATTRIBUTES)) {
|
||||
// This shall not happen, however, not purpose of this base class.
|
||||
// so bailing out.
|
||||
continue;
|
||||
@@ -82,14 +83,14 @@ public class AudioVolumesTestBase extends ActivityInstrumentationTestCase2<Audio
|
||||
for (final AudioVolumeGroup avg : audioVolumeGroups) {
|
||||
if (avg.getId() == e.getKey()) {
|
||||
assertTrue(!avg.getAudioAttributes().isEmpty());
|
||||
AudioAttributes avgAttributes = sDefaultAttributes;
|
||||
AudioAttributes avgAttributes = DEFAULT_ATTRIBUTES;
|
||||
for (final AudioAttributes aa : avg.getAudioAttributes()) {
|
||||
if (!aa.equals(AudioProductStrategy.getDefaultAttributes())) {
|
||||
avgAttributes = aa;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(!avgAttributes.equals(sDefaultAttributes));
|
||||
assertTrue(!avgAttributes.equals(DEFAULT_ATTRIBUTES));
|
||||
mAudioManager.setVolumeIndexForAttributes(
|
||||
avgAttributes, e.getValue(), AudioManager.FLAG_ALLOW_RINGER_MODES);
|
||||
}
|
||||
@@ -97,11 +98,11 @@ public class AudioVolumesTestBase extends ActivityInstrumentationTestCase2<Audio
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
ActivityScenario.launch(AudioPolicyTestActivity.class);
|
||||
|
||||
mContext = getActivity();
|
||||
mContext = getApplicationContext();
|
||||
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
|
||||
|
||||
assertEquals(PackageManager.PERMISSION_GRANTED,
|
||||
@@ -117,10 +118,8 @@ public class AudioVolumesTestBase extends ActivityInstrumentationTestCase2<Audio
|
||||
storeAllVolumes();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
// Recover the volume and the ringer mode that the test may have overwritten.
|
||||
for (Map.Entry<Integer, Integer> e : mOriginalStreamVolumes.entrySet()) {
|
||||
mAudioManager.setStreamVolume(e.getKey(), e.getValue(),
|
||||
@@ -131,11 +130,5 @@ public class AudioVolumesTestBase extends ActivityInstrumentationTestCase2<Audio
|
||||
restoreAllVolumes();
|
||||
}
|
||||
|
||||
public static int resetVolumeIndex(int indexMin, int indexMax) {
|
||||
return (indexMax + indexMin) / 2;
|
||||
}
|
||||
|
||||
public static int incrementVolumeIndex(int index, int indexMin, int indexMax) {
|
||||
return (index + 1 > indexMax) ? resetVolumeIndex(indexMin, indexMax) : ++index;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user