Path: blob/master/test/jdk/javax/sound/midi/Devices/MidiOutGetMicrosecondPositionBug.java
41152 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.sound.midi.MidiDevice;24import javax.sound.midi.MidiSystem;25import javax.sound.midi.Sequencer;26import javax.sound.midi.Synthesizer;2728/**29* @test30* @bug 490378631* @summary MIDI OUT does not implement getMicrosecondPosition() consistently32*/33public class MidiOutGetMicrosecondPositionBug {34static int successfulTests = 0;3536private static void testDevice(MidiDevice device) throws Exception {37boolean timestampsAvailable = false;38boolean timestampPrecisionOk = false;39try {40// expected behaviour if not opened?41device.open();42/* First, we're testing if timestamps are provided at all.43Returning -1 (unsupported), while allowed by the API44specification, is not sufficient to pass this test. */45long timestamp = device.getMicrosecondPosition();46timestampsAvailable = (timestamp != -1);4748/* Then, we're testing the precision. Note that the system time49is measured in milliseconds, while the device time is measured50in microseconds. */5152long systemTime1 = System.currentTimeMillis();53long deviceTime1 = device.getMicrosecondPosition();54// rest for 5 seconds55Thread.sleep(5000);56long systemTime2 = System.currentTimeMillis();57long deviceTime2 = device.getMicrosecondPosition();5859// now both period measurements are calculated in milliseconds.60long systemDuration = systemTime2 - systemTime1;61long deviceDuration = (deviceTime2 - deviceTime1) / 1000;62long delta = Math.abs(systemDuration - deviceDuration);63// a deviation of 0.5 seconds (= 500 ms) is allowed.64timestampPrecisionOk = (delta <= 500);65} catch (Throwable t) {66System.out.println(" - Caught exception. Not failed.");67System.out.println(" - " + t.toString());68return;69} finally {70device.close();71}72if (! timestampsAvailable) {73throw new Exception("timestamps are not supported");74}75if (! timestampPrecisionOk) {76throw new Exception("device timer not precise enough");77}78successfulTests++;79}8081private static void doAll() throws Exception {82MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();83for (int i=0; i < infos.length; i++) {84MidiDevice device = MidiSystem.getMidiDevice(infos[i]);85if ((! (device instanceof Sequencer)) &&86(! (device instanceof Synthesizer)) &&87(device.getMaxReceivers() > 0 || device.getMaxReceivers() == -1)) {8889System.out.println("--------------");90System.out.println("Testing MIDI device: " + infos[i]);91testDevice(device);92}93if (infos.length==0) {94System.out.println("No MIDI devices available!");95}96}97}9899public static void main(String[] args) throws Exception {100if (!isMidiInstalled()) {101return;102}103doAll();104if (successfulTests==0) {105System.out.println("Could not execute any of the tests. Test NOT failed.");106} else {107System.out.println("Test PASSED.");108}109}110111/**112* Returns true if at least one MIDI (port) device is correctly installed on113* the system.114*/115public static boolean isMidiInstalled() {116boolean result = false;117MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();118for (int i = 0; i < devices.length; i++) {119try {120MidiDevice device = MidiSystem.getMidiDevice(devices[i]);121result = ! (device instanceof Sequencer) && ! (device instanceof Synthesizer);122} catch (Exception e1) {123System.err.println(e1);124}125if (result)126break;127}128if (!result) {129System.err.println("Soundcard does not exist or sound drivers not installed!");130System.err.println("This test requires sound drivers for execution.");131}132return result;133}134}135136137