1 /* 2 * Copyright (C) 2020 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 com.android.mediaframeworktest.template; 18 19 import android.media.AudioAttributes; 20 import android.media.MediaPlayer; 21 import android.os.Bundle; 22 import android.test.ActivityInstrumentationTestCase2; 23 import android.test.suitebuilder.annotation.LargeTest; 24 import android.util.Log; 25 26 import com.android.mediaframeworktest.AudioTestHarnessTemplateRunner; 27 import com.android.mediaframeworktest.MediaFrameworkTest; 28 29 import java.io.IOException; 30 31 /** 32 * Junit / Instrumentation test case for Audio Test Harness. 33 * 34 * This test class is the place where Android APIs are invoked. Any public method that starts with 35 * prefix test will be added to test suite and get executed. 36 */ 37 public class AudioTestHarnessTemplateAndroidTest extends 38 ActivityInstrumentationTestCase2<MediaFrameworkTest> { 39 40 private static final String TAG = "AudioTestHarnessTemplateAndroidTest"; 41 42 private static final String AUDIO_FILE_KEY = "audioFile"; 43 private static final String AUDIO_PLAY_DURATION_KEY = "audioPlayDuration"; 44 45 private String mAudioFile = ""; 46 private int mAudioPlayDuration; 47 AudioTestHarnessTemplateAndroidTest()48 public AudioTestHarnessTemplateAndroidTest() { 49 super("com.android.mediaframeworktest", MediaFrameworkTest.class); 50 } 51 52 @Override setUp()53 protected void setUp() throws Exception { 54 super.setUp(); 55 extractArguments(); 56 } 57 58 59 // Extracts test params from passed in arguments. extractArguments()60 private void extractArguments() { 61 AudioTestHarnessTemplateRunner runner = 62 (AudioTestHarnessTemplateRunner) getInstrumentation(); 63 Bundle arguments = runner.getArguments(); 64 mAudioFile = arguments.getString(AUDIO_FILE_KEY); 65 mAudioPlayDuration = Integer.parseInt(arguments.getString(AUDIO_PLAY_DURATION_KEY)); 66 Log.i(TAG, String 67 .format("Extracted arguments from runner. Audio file: %s, play duration: %d", 68 mAudioFile, 69 mAudioPlayDuration)); 70 } 71 72 @Override tearDown()73 protected void tearDown() throws Exception { 74 super.tearDown(); 75 // Here is where you can put custom methods at tearDown method. 76 } 77 78 /** 79 * Plays audio file for given amount of time. 80 * 81 * Instantiates a MediaPlayer and plays the passed in audioFile for audioPlayDuration 82 * milliseconds. If the player fails to instantiate or any exception happened during the play, 83 * the test will fail. 84 */ playAudioFile(String audioFile, int audioPlayDuration)85 private static void playAudioFile(String audioFile, int audioPlayDuration) { 86 Log.v(TAG, String.format("Playing audio file: %s", audioFile)); 87 MediaPlayer mp = new MediaPlayer(); 88 try { 89 mp.setAudioAttributes( 90 new AudioAttributes.Builder() 91 .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) 92 .setUsage(AudioAttributes.USAGE_MEDIA) 93 .build()); 94 mp.setDataSource(audioFile); 95 mp.prepare(); 96 int duration = mp.getDuration(); 97 if (duration <= 0) { 98 Log.e(TAG, "Failed to grab duration from audio file."); 99 fail("AudioFileWithNegativeDuration"); 100 } 101 mp.start(); 102 // This test demonstrates how to play the audio file from device for certain amount of 103 // time, and the test actually runs on host machine so the listener is not adapted here. 104 Log.v(TAG, 105 String.format("Wait for audio file to play for duration: %d", 106 audioPlayDuration)); 107 Thread.sleep(audioPlayDuration); 108 } catch (IOException | InterruptedException e) { 109 Log.e(TAG, String.format("Exception happened while playing audio file: %s", audioFile), 110 e); 111 fail("FailedToPlayAudioFile"); 112 } finally { 113 if (mp != null) { 114 Log.v(TAG, "Release media player."); 115 mp.release(); 116 } 117 } 118 } 119 120 // This test method will play the audioFile for audioPlayDuration milliseconds as passed in from 121 // AudioTestHarnessTemplateTest class. 122 @LargeTest testPlayAudioFile()123 public void testPlayAudioFile() throws Exception { 124 playAudioFile(mAudioFile, mAudioPlayDuration); 125 } 126 127 } 128