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 41 public static final int TESTID_NONE = 0; 42 public static final int TESTID_USBLOOPBACK = 1; 43 public static final int TESTID_VIRTUALLOOPBACK = 2; 44 public static final int TESTID_BTLOOPBACK = 3; 45 46 protected int mTestStatus = TESTSTATUS_NOTRUN; 47 48 protected int mDeviceType; 49 50 // The Test Peripheral 51 protected MidiIODevice mIODevice; 52 53 // Test State 54 protected final Object mTestLock = new Object(); 55 protected boolean mTestRunning; 56 57 // Timeout handling 58 protected static final int TEST_TIMEOUT_MS = 5000; 59 protected final Timer mTimeoutTimer = new Timer(); 60 protected int mTestCounter = 0; 61 MidiTestModule(int deviceType)62 public MidiTestModule(int deviceType) { 63 mIODevice = new MidiIODevice(deviceType); 64 mDeviceType = deviceType; 65 } 66 67 /** 68 * Starts the loop-back test 69 */ startLoopbackTest(int testID)70 public abstract void startLoopbackTest(int testID); 71 72 /** 73 * Returns whether the test has passed 74 */ hasTestPassed()75 public abstract boolean hasTestPassed(); 76 updateTestStateUIAbstract()77 protected abstract void updateTestStateUIAbstract(); showTimeoutMessageAbstract()78 protected abstract void showTimeoutMessageAbstract(); enableTestButtonsAbstract(boolean enable)79 protected abstract void enableTestButtonsAbstract(boolean enable); 80 getTestStatus()81 public int getTestStatus() { 82 return mTestStatus; 83 } 84 85 /** 86 * Returns whether the test is ready 87 */ isTestReady()88 public boolean isTestReady() { 89 return mIODevice.mReceiveDevInfo != null && mIODevice.mSendDevInfo != null; 90 } 91 92 /** 93 * Returns the input name of the IO device 94 */ getInputName()95 public String getInputName() { 96 return mIODevice.getInputName(); 97 } 98 99 /** 100 * Returns the output name of the IO device 101 */ getOutputName()102 public String getOutputName() { 103 return mIODevice.getOutputName(); 104 } 105 106 /** 107 * Scans an array of MidiDeviceInfo 108 */ scanDevices(Collection<MidiDeviceInfo> devInfos)109 public void scanDevices(Collection<MidiDeviceInfo> devInfos) { 110 mIODevice.scanDevices(devInfos); 111 } 112 113 /** 114 * Starts a timeout timer that updates the UI if the timeout is triggered 115 */ startTimeoutHandler()116 public void startTimeoutHandler() { 117 final int currentTestCounter = mTestCounter; 118 // Start the timeout timer 119 TimerTask task = new TimerTask() { 120 @Override 121 public void run() { 122 synchronized (mTestLock) { 123 if (mTestRunning && currentTestCounter == mTestCounter) { 124 // Timeout 125 showTimeoutMessageAbstract(); 126 enableTestButtonsAbstract(true); 127 } 128 } 129 } 130 }; 131 mTimeoutTimer.schedule(task, TEST_TIMEOUT_MS); 132 } 133 } 134