1/*
2 * Copyright (c) 2005, 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
24import java.io.ByteArrayInputStream;
25
26import javax.sound.sampled.AudioFormat;
27import javax.sound.sampled.AudioInputStream;
28import javax.sound.sampled.AudioSystem;
29import javax.sound.sampled.Clip;
30import javax.sound.sampled.DataLine;
31import javax.sound.sampled.LineUnavailableException;
32import javax.sound.sampled.Mixer;
33
34/**
35 * @test
36 * @bug 4385928
37 * @summary Verify that an endpoint -1 in Clip does not throw an exception
38 */
39//public class test048 extends TRTest
40public class ClipSetEndPoint {
41
42    private Clip theClip;
43
44    boolean testPassed = true;
45
46    //_______________________________________________
47    //      Method: runTest
48    //_______________________________________________
49    public boolean runTest() {
50        AudioInputStream theAudioInputStream = new AudioInputStream(
51                new ByteArrayInputStream(new byte[2000]),
52                new AudioFormat(8000.0f, 8, 1, false, false), 2000); //
53
54        AudioFormat theAudioFormat = theAudioInputStream.getFormat();
55
56        DataLine.Info info = new DataLine.Info(Clip.class, theAudioFormat,
57                                               AudioSystem.NOT_SPECIFIED);
58        try {
59            theClip = (Clip) AudioSystem.getLine(info);
60            theClip.open(theAudioInputStream);
61
62            int theStartLoopPoint = 0;
63            int theEndLoopPoint = -1;       //      -1 signifies the last frame
64
65            theClip.setLoopPoints(theStartLoopPoint, theEndLoopPoint);
66            //theClip.start();
67        } catch (LineUnavailableException e) {
68            e.printStackTrace();
69            testPassed = true;
70        } catch (Exception e) {
71            e.printStackTrace();
72            testPassed = false;
73        }
74        return testPassed;
75    }
76
77    //_______________________________________________
78    //      Method: main
79    //_______________________________________________
80    public static void main(String[] args) throws Exception {
81        if (isSoundcardInstalled()) {
82            ClipSetEndPoint thisTest = new ClipSetEndPoint();
83            boolean testResult = thisTest.runTest();
84            if (testResult) {
85                System.out.println("Test passed");
86            } else {
87                System.out.println("Test failed");
88                throw new Exception("Test failed");
89            }
90        }
91    }
92
93    /**
94     * Returns true if at least one soundcard is correctly installed
95     * on the system.
96     */
97    public static boolean isSoundcardInstalled() {
98        boolean result = false;
99        try {
100            Mixer.Info[] mixers = AudioSystem.getMixerInfo();
101            if (mixers.length > 0) {
102                result = AudioSystem.getSourceDataLine(null) != null;
103            }
104        } catch (Exception e) {
105            System.err.println("Exception occured: " + e);
106        }
107        if (!result) {
108            System.err.println(
109                    "Soundcard does not exist or sound drivers not installed!");
110            System.err.println(
111                    "This test requires sound drivers for execution.");
112        }
113        return result;
114    }
115}
116