1 /* 2 * Copyright (C) 2015 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 android.media.cts; 18 19 import android.platform.test.annotations.AppModeFull; 20 import java.io.IOException; 21 22 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.media.midi.MidiDevice; 26 import android.media.midi.MidiDevice.MidiConnection; 27 import android.media.midi.MidiDeviceInfo; 28 import android.media.midi.MidiDeviceStatus; 29 import android.media.midi.MidiInputPort; 30 import android.media.midi.MidiManager; 31 import android.media.midi.MidiReceiver; 32 import android.media.midi.MidiSender; 33 import android.os.Handler; 34 import android.os.Looper; 35 36 import com.android.compatibility.common.util.CtsAndroidTestCase; 37 38 /** 39 * Test MIDI when there may be no MIDI devices available. There is not much we 40 * can test without a device. 41 */ 42 @AppModeFull(reason = "TODO: evaluate and port to instant") 43 public class MidiSoloTest extends CtsAndroidTestCase { 44 private static final String TAG = "MidiSoloTest"; 45 private final static int LOCAL_STORAGE_SIZE = 256; 46 47 // Store received data so we can check it later. 48 class MyMidiReceiver extends MidiReceiver { 49 public int byteCount; 50 public byte[] data = new byte[LOCAL_STORAGE_SIZE]; 51 52 public MyMidiReceiver(int maxMessageSize) { 53 super(maxMessageSize); 54 } 55 56 @Override 57 // Abstract method declared in MidiReceiver 58 public void onSend(byte[] msg, int offset, int count, long timestamp) 59 throws IOException { 60 assertTrue("Message too large.", (count <= getMaxMessageSize())); 61 try { 62 System.arraycopy(msg, offset, data, byteCount, count); 63 } catch (ArrayIndexOutOfBoundsException e) { 64 throw new IOException("Exceeded local storage.", e); 65 } 66 byteCount += count; 67 } 68 69 @Override 70 public void onFlush() { 71 byteCount = 0; 72 } 73 } 74 75 @Override 76 protected void setUp() throws Exception { 77 // setup for each test case. 78 super.setUp(); 79 } 80 81 @Override 82 protected void tearDown() throws Exception { 83 // Test case clean up. 84 super.tearDown(); 85 } 86 87 public void testMidiManager() throws Exception { 88 PackageManager pm = getContext().getPackageManager(); 89 if (!pm.hasSystemFeature(PackageManager.FEATURE_MIDI)) { 90 return; // Not supported so don't test it. 91 } 92 93 MidiManager midiManager = (MidiManager) getContext().getSystemService( 94 Context.MIDI_SERVICE); 95 assertTrue("MidiManager not supported.", midiManager != null); 96 97 MidiDeviceInfo[] infos = midiManager.getDevices(); 98 assertTrue("Device list was null.", infos != null); 99 100 MidiManager.DeviceCallback callback = new MidiManager.DeviceCallback(); 101 102 // These should not crash. 103 midiManager.unregisterDeviceCallback(callback); 104 midiManager.registerDeviceCallback(callback, null); 105 midiManager.unregisterDeviceCallback(callback); 106 midiManager.registerDeviceCallback(callback, new Handler(Looper.getMainLooper())); 107 midiManager.registerDeviceCallback(callback, new Handler(Looper.getMainLooper())); 108 midiManager.unregisterDeviceCallback(callback); 109 midiManager.unregisterDeviceCallback(callback); 110 midiManager.unregisterDeviceCallback(callback); 111 } 112 113 public void testMidiReceiver() throws Exception { 114 PackageManager pm = getContext().getPackageManager(); 115 if (!pm.hasSystemFeature(PackageManager.FEATURE_MIDI)) { 116 return; // Not supported so don't test it. 117 } 118 119 MidiReceiver receiver = new MidiReceiver() { 120 @Override 121 public void onSend(byte[] msg, int offset, int count, 122 long timestamp) throws IOException { 123 } 124 }; 125 assertEquals("MidiReceiver default size wrong.", Integer.MAX_VALUE, 126 receiver.getMaxMessageSize()); 127 128 int maxSize = 11; 129 MyMidiReceiver myReceiver = new MyMidiReceiver(maxSize); 130 assertEquals("MidiReceiver set size wrong.", maxSize, 131 myReceiver.getMaxMessageSize()); 132 133 // Fill array with predictable data. 134 byte[] bar = new byte[200]; 135 for (int i = 0; i < bar.length; i++) { 136 bar[i] = (byte) (i ^ 15); 137 } 138 // Small message with no offset. 139 int offset = 0; 140 int count = 3; 141 checkReceivedData(myReceiver, bar, offset, count); 142 143 // Small with an offset. 144 offset = 50; 145 count = 3; 146 checkReceivedData(myReceiver, bar, offset, count); 147 148 // Entire array. 149 offset = 0; 150 count = bar.length; 151 checkReceivedData(myReceiver, bar, offset, count); 152 153 offset = 20; 154 count = 100; 155 checkReceivedData(myReceiver, bar, offset, count); 156 } 157 158 public void testMidiReceiverException() throws Exception { 159 PackageManager pm = getContext().getPackageManager(); 160 if (!pm.hasSystemFeature(PackageManager.FEATURE_MIDI)) { 161 return; // Not supported so don't test it. 162 } 163 164 int maxSize = 11; 165 MyMidiReceiver myReceiver = new MyMidiReceiver(maxSize); 166 assertEquals("MidiReceiver set size wrong.", maxSize, 167 myReceiver.getMaxMessageSize()); 168 169 // Fill array with predictable data. 170 byte[] bar = new byte[200]; 171 int offset = 0; 172 int count = bar.length; 173 myReceiver.flush(); // reset byte counter 174 IOException exception = null; 175 // Send too much data and intentionally cause an IOException. 176 try { 177 int sent = 0; 178 while (sent < LOCAL_STORAGE_SIZE) { 179 myReceiver.send(bar, offset, count); 180 sent += count; 181 } 182 } catch (IOException e) { 183 exception = e; 184 } 185 assertTrue("We should have caught an IOException", exception != null); 186 } 187 188 // Does the data we sent match the data received by the MidiReceiver? 189 private void checkReceivedData(MyMidiReceiver myReceiver, byte[] bar, 190 int offset, int count) throws IOException { 191 myReceiver.flush(); // reset byte counter 192 assertEquals("MidiReceiver flush ", 0, myReceiver.byteCount); 193 myReceiver.send(bar, offset, count); 194 // Did we get all the data 195 assertEquals("MidiReceiver count ", count, myReceiver.byteCount); 196 for (int i = 0; i < count; i++) { 197 assertEquals("MidiReceiver byte " + i + " + " + offset, 198 bar[i + offset], myReceiver.data[i]); 199 } 200 } 201 } 202