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 com.android.cts.verifier.audio.midilib; 18 19 import android.media.midi.MidiDeviceInfo; 20 21 import java.util.Collection; 22 import java.util.Timer; 23 import java.util.TimerTask; 24 25 /** 26 * A test module that tests MIDI 27 */ 28 public abstract class MidiTestModule { 29 private static final String TAG = "MidiTestModule"; 30 private static final boolean DEBUG = true; 31 32 // Test Status 33 public static final int TESTSTATUS_NOTRUN = 0; 34 public static final int TESTSTATUS_PASSED = 1; 35 public static final int TESTSTATUS_FAILED_MISMATCH = 2; 36 public static final int TESTSTATUS_FAILED_TIMEOUT = 3; 37 public static final int TESTSTATUS_FAILED_OVERRUN = 4; 38 public static final int TESTSTATUS_FAILED_DEVICE = 5; 39 public static final int TESTSTATUS_FAILED_JNI = 6; 40 public static final int TESTSTATUS_FAILED_SETUP = 7; 41 public static final int TESTSTATUS_FAILED_SEND = 8; 42 43 public static final int TESTID_NONE = 0; 44 public static final int TESTID_USBLOOPBACK = 1; 45 public static final int TESTID_VIRTUALLOOPBACK = 2; 46 public static final int TESTID_BTLOOPBACK = 3; 47 48 protected int mTestStatus = TESTSTATUS_NOTRUN; 49 50 protected int mDeviceType; 51 52 // The Test Peripheral 53 protected MidiIODevice mIODevice; 54 55 // Test State 56 protected final Object mTestLock = new Object(); 57 protected boolean mTestRunning; 58 59 // Timeout handling 60 protected static final int TEST_TIMEOUT_MS = 5000; 61 protected final Timer mTimeoutTimer = new Timer(); 62 protected int mTestCounter = 0; 63 MidiTestModule(int deviceType)64 public MidiTestModule(int deviceType) { 65 mIODevice = new MidiIODevice(deviceType); 66 mDeviceType = deviceType; 67 } 68 69 /** 70 * Starts the loop-back test 71 */ startLoopbackTest(int testID)72 public abstract void startLoopbackTest(int testID); 73 74 /** 75 * Returns whether the test has passed 76 */ hasTestPassed()77 public abstract boolean hasTestPassed(); 78 updateTestStateUIAbstract()79 protected abstract void updateTestStateUIAbstract(); showTimeoutMessageAbstract()80 protected abstract void showTimeoutMessageAbstract(); enableTestButtonsAbstract(boolean enable)81 protected abstract void enableTestButtonsAbstract(boolean enable); 82 getTestStatus()83 public int getTestStatus() { 84 return mTestStatus; 85 } 86 87 /** 88 * Returns whether the test is ready 89 */ isTestReady()90 public boolean isTestReady() { 91 return mIODevice.mReceiveDevInfo != null && mIODevice.mSendDevInfo != null; 92 } 93 94 /** 95 * Returns the input name of the IO device 96 */ getInputName()97 public String getInputName() { 98 return mIODevice.getInputName(); 99 } 100 101 /** 102 * Returns the output name of the IO device 103 */ getOutputName()104 public String getOutputName() { 105 return mIODevice.getOutputName(); 106 } 107 108 /** 109 * Scans an array of MidiDeviceInfo 110 */ scanDevices(Collection<MidiDeviceInfo> devInfos)111 public void scanDevices(Collection<MidiDeviceInfo> devInfos) { 112 mIODevice.scanDevices(devInfos); 113 } 114 115 /** 116 * Starts a timeout timer that updates the UI if the timeout is triggered 117 */ startTimeoutHandler()118 public void startTimeoutHandler() { 119 final int currentTestCounter = mTestCounter; 120 // Start the timeout timer 121 TimerTask task = new TimerTask() { 122 @Override 123 public void run() { 124 synchronized (mTestLock) { 125 if (mTestRunning && currentTestCounter == mTestCounter) { 126 // Timeout 127 showTimeoutMessageAbstract(); 128 enableTestButtonsAbstract(true); 129 } 130 } 131 } 132 }; 133 mTimeoutTimer.schedule(task, TEST_TIMEOUT_MS); 134 } 135 } 136