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.media.template;
18 
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.config.Option.Importance;
21 import com.android.ddmlib.testrunner.IRemoteAndroidTestRunner;
22 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
23 import com.android.tradefed.device.DeviceNotAvailableException;
24 import com.android.tradefed.device.ITestDevice;
25 import com.android.tradefed.log.LogUtil.CLog;
26 import com.android.tradefed.result.ITestInvocationListener;
27 import com.android.tradefed.testtype.IDeviceTest;
28 import com.android.tradefed.testtype.IRemoteTest;
29 
30 import org.junit.Assert;
31 
32 import java.util.concurrent.TimeUnit;
33 
34 /**
35  * Template test for Audio Test Harness.
36  *
37  * This template test involves iteraction with devices connected host and interfaces to invoke
38  * Android APIs for testing.
39  */
40 public class AudioTestHarnessTemplateTest implements IDeviceTest, IRemoteTest {
41   // These params can be passed in from config files and will pass to
42   // AudioTestHarnessTemplateAndroidTest class.
43     @Option(name = "audio-file", description = "Audio file to be played by device",
44             importance = Importance.ALWAYS)
45     String mAudioFile = "/system/product/media/audio/ringtones/Lollipop.ogg";
46 
47     @Option(name = "audio-play-duration", description = "Milliseconds for the audio to be played", importance = Importance.ALWAYS)
48     String mAudioPlayDuration = "5000";
49 
50     ITestDevice mTestDevice = null;
51 
52     //Max test timeout - 2 hrs
53     private static final int MAX_TEST_TIMEOUT = 2 * 60 * 60 * 1000;
54 
55     // Constants for running the tests
56     private static final String TEST_CLASS_NAME =
57             "com.android.mediaframeworktest.template.AudioTestHarnessTemplateAndroidTest";
58     private static final String TEST_PACKAGE_NAME = "com.android.mediaframeworktest";
59     private static final String TEST_RUNNER_NAME = ".AudioTestHarnessTemplateRunner";
60     private static final String AUDIO_FILE_KEY  = "audioFile";
61     private static final String AUDIO_PLAY_DURATION_KEY  = "audioPlayDuration";
62 
63 
64     @Override
run(ITestInvocationListener listener)65     public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
66         Assert.assertNotNull(mTestDevice);
67         CLog.i("Starting test with Audio Test Harness Template.");
68         IRemoteAndroidTestRunner runner = new RemoteAndroidTestRunner(TEST_PACKAGE_NAME,
69                 TEST_RUNNER_NAME, mTestDevice.getIDevice());
70         runner.addInstrumentationArg(AUDIO_FILE_KEY, mAudioFile);
71         runner.addInstrumentationArg(AUDIO_PLAY_DURATION_KEY, mAudioPlayDuration);
72         CLog.i("Playing audio file %s for %s milliseconds", mAudioFile, mAudioPlayDuration);
73         runner.setClassName(TEST_CLASS_NAME);
74         runner.setMaxTimeToOutputResponse(MAX_TEST_TIMEOUT, TimeUnit.MILLISECONDS);
75 
76         mTestDevice.runInstrumentationTests(runner, listener);
77     }
78 
79 
80     @Override
setDevice(ITestDevice device)81     public void setDevice(ITestDevice device) {
82         mTestDevice = device;
83     }
84 
85     @Override
getDevice()86     public ITestDevice getDevice() {
87         return mTestDevice;
88     }
89 }
90