|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
21 | 21 | * questions.
|
22 | 22 | */
|
23 | 23 |
|
| 24 | +import java.awt.BorderLayout; |
| 25 | +import java.awt.FlowLayout; |
| 26 | +import java.awt.event.WindowAdapter; |
| 27 | +import java.awt.event.WindowEvent; |
| 28 | + |
| 29 | +import java.lang.reflect.InvocationTargetException; |
| 30 | +import java.util.concurrent.CountDownLatch; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | + |
24 | 33 | import javax.sound.midi.MidiDevice;
|
25 | 34 | import javax.sound.midi.MidiMessage;
|
26 | 35 | import javax.sound.midi.MidiSystem;
|
| 36 | +import javax.sound.midi.MidiUnavailableException; |
| 37 | +import javax.swing.JDialog; |
| 38 | +import javax.swing.SwingUtilities; |
| 39 | +import javax.swing.JTextArea; |
| 40 | +import javax.swing.JButton; |
| 41 | +import javax.swing.JLabel; |
| 42 | +import javax.swing.Timer; |
| 43 | +import javax.swing.JPanel; |
| 44 | +import javax.swing.WindowConstants; |
27 | 45 |
|
28 |
| -/** |
| 46 | +/* |
29 | 47 | * @test
|
30 | 48 | * @bug 6186488
|
31 | 49 | * @summary Tests that software Java Syntesizer processed
|
32 | 50 | * non-ShortMessage-derived messages
|
33 |
| - * @run main/manual=yesno bug6186488 |
| 51 | + * @run main/manual bug6186488 |
34 | 52 | */
|
35 | 53 | public class bug6186488 {
|
36 |
| - public static void main(String[] args) throws Exception { |
37 |
| - MidiDevice/*Synthesizer*/ synth = null; |
| 54 | + private static final CountDownLatch countDownLatch = new CountDownLatch(1); |
| 55 | + private static final int testTimeout = 300000; |
| 56 | + private static volatile String testFailureMsg; |
| 57 | + private static volatile boolean testPassed; |
| 58 | + private static volatile boolean testFinished; |
38 | 59 |
|
| 60 | + public static void main(String[] args) throws InterruptedException, InvocationTargetException { |
| 61 | + SwingUtilities.invokeAndWait(() -> createAndShowTestDialog()); |
39 | 62 | try {
|
40 |
| - synth = MidiSystem.getSynthesizer(); |
41 |
| - //synth = MidiSystem.getMidiDevice(infos[0]); |
| 63 | + if (!countDownLatch.await(testTimeout, TimeUnit.MILLISECONDS)) { |
| 64 | + throw new RuntimeException(String.format("Test timeout '%d ms' elapsed.", testTimeout)); |
| 65 | + } |
| 66 | + if (!testPassed) { |
| 67 | + String failureMsg = testFailureMsg; |
| 68 | + if ((failureMsg != null) && (!failureMsg.trim().isEmpty())) { |
| 69 | + throw new RuntimeException(failureMsg); |
| 70 | + } else { |
| 71 | + throw new RuntimeException("Test failed."); |
| 72 | + } |
| 73 | + } |
| 74 | + } catch (InterruptedException ie) { |
| 75 | + throw new RuntimeException(ie); |
| 76 | + } finally { |
| 77 | + testFinished = true; |
| 78 | + } |
| 79 | + } |
42 | 80 |
|
| 81 | + private static void pass() { |
| 82 | + testPassed = true; |
| 83 | + countDownLatch.countDown(); |
| 84 | + } |
| 85 | + |
| 86 | + private static void fail(String failureMsg) { |
| 87 | + testFailureMsg = failureMsg; |
| 88 | + testPassed = false; |
| 89 | + countDownLatch.countDown(); |
| 90 | + } |
| 91 | + |
| 92 | + private static String convertMillisToTimeStr(int millis) { |
| 93 | + if (millis < 0) { |
| 94 | + return "00:00:00"; |
| 95 | + } |
| 96 | + int hours = millis / 3600000; |
| 97 | + int minutes = (millis - hours * 3600000) / 60000; |
| 98 | + int seconds = (millis - hours * 3600000 - minutes * 60000) / 1000; |
| 99 | + return String.format("%02d:%02d:%02d", hours, minutes, seconds); |
| 100 | + } |
| 101 | + |
| 102 | + private static void createAndShowTestDialog() { |
| 103 | + String testInstruction = "This test verify that software Java Syntesizer processed non-ShortMessage-derived messages.\n" + |
| 104 | + "Close all other programs that may use the sound card.\n" + |
| 105 | + "Make sure that the speakers are connected and the volume is up.\n" + |
| 106 | + "Click on 'Start Test' button. If you listen a sound then test pass else test fail."; |
| 107 | + |
| 108 | + final JDialog dialog = new JDialog(); |
| 109 | + dialog.setTitle("Test Sound"); |
| 110 | + dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 111 | + dialog.addWindowListener(new WindowAdapter() { |
| 112 | + @Override |
| 113 | + public void windowClosing(WindowEvent e) { |
| 114 | + dialog.dispose(); |
| 115 | + fail("Main dialog was closed."); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + final JLabel testTimeoutLabel = new JLabel(String.format("Test timeout: %s", convertMillisToTimeStr(testTimeout))); |
| 120 | + final long startTime = System.currentTimeMillis(); |
| 121 | + final Timer timer = new Timer(0, null); |
| 122 | + timer.setDelay(1000); |
| 123 | + timer.addActionListener((e) -> { |
| 124 | + int leftTime = testTimeout - (int) (System.currentTimeMillis() - startTime); |
| 125 | + if ((leftTime < 0) || testFinished) { |
| 126 | + timer.stop(); |
| 127 | + dialog.dispose(); |
| 128 | + } |
| 129 | + testTimeoutLabel.setText(String.format("Test timeout: %s", convertMillisToTimeStr(leftTime))); |
| 130 | + }); |
| 131 | + timer.start(); |
| 132 | + |
| 133 | + JTextArea textArea = new JTextArea(testInstruction); |
| 134 | + textArea.setEditable(false); |
| 135 | + |
| 136 | + final JButton startTestButton = new JButton("Start Test"); |
| 137 | + final JButton passButton = new JButton("PASS"); |
| 138 | + final JButton failButton = new JButton("FAIL"); |
| 139 | + startTestButton.addActionListener((e) -> { |
| 140 | + new Thread(() -> { |
| 141 | + try { |
| 142 | + doTest(); |
| 143 | + |
| 144 | + SwingUtilities.invokeLater(() -> { |
| 145 | + passButton.setEnabled(true); |
| 146 | + failButton.setEnabled(true); |
| 147 | + }); |
| 148 | + } catch (Throwable t) { |
| 149 | + t.printStackTrace(); |
| 150 | + dialog.dispose(); |
| 151 | + fail("Exception occurred in a thread executing the test."); |
| 152 | + } |
| 153 | + }).start(); |
| 154 | + }); |
| 155 | + passButton.setEnabled(false); |
| 156 | + passButton.addActionListener((e) -> { |
| 157 | + dialog.dispose(); |
| 158 | + pass(); |
| 159 | + }); |
| 160 | + failButton.setEnabled(false); |
| 161 | + failButton.addActionListener((e) -> { |
| 162 | + dialog.dispose(); |
| 163 | + fail("Expected that sound will be heard but did not hear sound"); |
| 164 | + }); |
| 165 | + |
| 166 | + JPanel mainPanel = new JPanel(new BorderLayout()); |
| 167 | + JPanel labelPanel = new JPanel(new FlowLayout()); |
| 168 | + labelPanel.add(testTimeoutLabel); |
| 169 | + mainPanel.add(labelPanel, BorderLayout.NORTH); |
| 170 | + mainPanel.add(textArea, BorderLayout.CENTER); |
| 171 | + JPanel buttonPanel = new JPanel(new FlowLayout()); |
| 172 | + buttonPanel.add(startTestButton); |
| 173 | + buttonPanel.add(passButton); |
| 174 | + buttonPanel.add(failButton); |
| 175 | + mainPanel.add(buttonPanel, BorderLayout.SOUTH); |
| 176 | + dialog.add(mainPanel); |
| 177 | + dialog.pack(); |
| 178 | + dialog.setVisible(true); |
| 179 | + } |
| 180 | + |
| 181 | + public static void waitForSynToOpen(MidiDevice synth) throws InterruptedException { |
| 182 | + int count = 0; |
| 183 | + do { |
| 184 | + if (synth.isOpen()) { |
| 185 | + System.out.println("synth is opened"); |
| 186 | + return; |
| 187 | + } |
| 188 | + TimeUnit.SECONDS.sleep(1); |
| 189 | + } while( ++count >= 5); |
| 190 | + throw new RuntimeException(synth + " did not open even after 5 seconds"); |
| 191 | + } |
| 192 | + |
| 193 | + private static void doTest() throws MidiUnavailableException, InterruptedException { |
| 194 | + try (MidiDevice synth = MidiSystem.getSynthesizer()) { |
43 | 195 | System.out.println("Synthesizer: " + synth.getDeviceInfo());
|
44 | 196 | synth.open();
|
| 197 | + waitForSynToOpen(synth); |
45 | 198 | MidiMessage msg = new GenericMidiMessage(0x90, 0x3C, 0x40);
|
46 |
| - //ShortMessage msg = new ShortMessage(); |
47 |
| - //msg.setMessage(0x90, 0x3C, 0x40); |
48 |
| - |
49 | 199 | synth.getReceiver().send(msg, 0);
|
50 | 200 | Thread.sleep(2000);
|
51 |
| - |
52 |
| - } catch (Exception ex) { |
53 |
| - ex.printStackTrace(); |
54 |
| - throw ex; |
55 |
| - } finally { |
56 |
| - if (synth != null && synth.isOpen()) |
57 |
| - synth.close(); |
58 |
| - } |
59 |
| - System.out.print("Did you heard a note? (enter 'y' or 'n') "); |
60 |
| - int result = System.in.read(); |
61 |
| - System.in.skip(1000); |
62 |
| - if (result == 'y' || result == 'Y') |
63 |
| - { |
64 |
| - System.out.println("Test passed sucessfully."); |
65 |
| - } |
66 |
| - else |
67 |
| - { |
68 |
| - System.out.println("Test FAILED."); |
69 |
| - throw new RuntimeException("Test failed."); |
70 | 201 | }
|
71 | 202 | }
|
72 | 203 |
|
|
0 commit comments