1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.util.List;
  25 
  26 import javax.sound.midi.MidiDevice;
  27 import javax.sound.midi.MidiSystem;
  28 import javax.sound.midi.MidiUnavailableException;
  29 import javax.sound.midi.Receiver;
  30 import javax.sound.midi.Transmitter;
  31 
  32 /**
  33  * @test
  34  * @bug 4931387
  35  * @summary Add methods to MidiDevice to get list of Transmitters and Receivers
  36  */
  37 public class MidiDeviceGetReceivers {
  38 
  39     private static boolean executed = false;
  40     private static boolean failed = false;
  41 
  42     public static void main(String[] args) throws Exception {
  43         out("unit test 4931387: Add methods to MidiDevice to get list of Transmitters and Receivers");
  44         doAllTests();
  45         if (executed) {
  46             if (failed) throw new Exception("Test FAILED!");
  47             out("Test PASSED.");
  48         } else {
  49             out("Test NOT failed.");
  50         }
  51     }
  52 
  53     private static void doAllTests() {
  54         MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
  55         for (int i = 0; i < infos.length; i++) {
  56             MidiDevice device = null;
  57             try {
  58                 device = MidiSystem.getMidiDevice(infos[i]);
  59                 doTest(device);
  60             } catch (MidiUnavailableException e) {
  61                 out("Exception occured when retrieving device "+infos[i]+": "+e);
  62             }
  63         }
  64         if (infos.length == 0) {
  65             out("No MIDI devices exist or sound drivers not installed!");
  66         }
  67     }
  68 
  69     private static boolean containsReceiver(MidiDevice dev, Receiver rec) {
  70         List<Receiver> recvs = dev.getReceivers();
  71         return recvs.contains(rec);
  72     }
  73 
  74     private static boolean containsTransmitter(MidiDevice dev, Transmitter tra) {
  75         List<Transmitter> tras = dev.getTransmitters();
  76         return tras.contains(tra);
  77     }
  78 
  79     private static void doTest(MidiDevice device) {
  80         boolean thisFailed = false;
  81         out1("Testing: " + device+"...");
  82         try {
  83             device.open();
  84         } catch (Exception e) {
  85             out2("device.open threw exception: "+e);
  86             out2("cannot test this device.");
  87             return;
  88         }
  89         if (device.getMaxReceivers() != 0) {
  90             // device offers receivers
  91             try {
  92                 List<Receiver> origList = device.getReceivers();
  93                 Receiver rec = device.getReceiver();
  94                 if (!containsReceiver(device, rec)) {
  95                     out2("Getting a receiver did not add it to device list!");
  96                     thisFailed = true;
  97                 }
  98                 if (origList.contains(rec)) {
  99                     out2("Original unmodifiable list was modified by adding a receiver!");
 100                     thisFailed = true;
 101                 }
 102                 rec.close();
 103                 if (containsReceiver(device, rec)) {
 104                     out2("Closing a receiver did not remove it from device list!");
 105                     thisFailed = true;
 106                 }
 107                 // add a new receiver so that the device.close will really test
 108                 // that the receiver is removed
 109                 rec = device.getReceiver();
 110                 if (!containsReceiver(device, rec)) {
 111                     out2("Getting a receiver again did not add it to device list!");
 112                     thisFailed = true;
 113                 }
 114             } catch (MidiUnavailableException e) {
 115                 out2("Exception on getting Receiver: " + e);
 116             }
 117         }
 118         if (device.getMaxTransmitters() != 0) {
 119             // device offers transmitters
 120             try {
 121                 List<Transmitter> origList = device.getTransmitters();
 122                 Transmitter tra = device.getTransmitter();
 123                 if (!containsTransmitter(device, tra)) {
 124                     out2("Getting a transmitter did not add it to device list!");
 125                     thisFailed = true;
 126                 }
 127                 if (origList.contains(tra)) {
 128                     out2("Original unmodifiable list was modified by adding a transmitter!");
 129                     thisFailed = true;
 130                 }
 131                 tra.close();
 132                 if (containsTransmitter(device, tra)) {
 133                     out2("Closing a transmitter did not remove it from device list!");
 134                     thisFailed = true;
 135                 }
 136                 tra = device.getTransmitter();
 137                 if (!containsTransmitter(device, tra)) {
 138                     out2("Getting a transmitter again did not add it to device list!");
 139                     thisFailed = true;
 140                 }
 141             } catch (MidiUnavailableException e) {
 142                 out("Exception on getting Transmitter: " + e);
 143             }
 144         }
 145         try {
 146             device.close();
 147             if (device.getTransmitters().size() > 0) {
 148                 out2(" Device still has transmitters after close() was called!");
 149                 thisFailed = true;
 150             }
 151             if (device.getReceivers().size() > 0) {
 152                 out2(" Device still has receivers after close() was called!");
 153                 thisFailed = true;
 154             }
 155         } catch (Exception e) {
 156             out2("device.close threw exception: "+e);
 157         }
 158         if (!thisFailed) {
 159             out("OK");
 160         } else {
 161             failed = true;
 162         }
 163         executed = true;
 164     }
 165 
 166     static boolean lfMissing = false;
 167 
 168     private static void out(String message) {
 169         lfMissing = true;
 170         System.out.println(message);
 171     }
 172 
 173     /* don't print LF at end */
 174     private static void out1(String message) {
 175         System.out.print(message);
 176         lfMissing = true;
 177     }
 178 
 179     /* print at a new line, indented */
 180     private static void out2(String message) {
 181         if (lfMissing) {
 182             System.out.println();
 183             lfMissing = false;
 184         }
 185         System.out.println("  "+message);
 186     }
 187 }