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 javax.sound.midi.MidiEvent;
  25 import javax.sound.midi.Sequence;
  26 import javax.sound.midi.ShortMessage;
  27 import javax.sound.midi.Track;
  28 
  29 /**
  30  * @test
  31  * @bug 4941944
  32  * @summary Track may not have a determined order for inserting events at same
  33  *          tick time
  34  */
  35 public class TrackAddSameTick {
  36 
  37     static boolean failed = false;
  38     static MidiEvent[] evs = new MidiEvent[10];
  39 
  40     public static void main(String argv[]) throws Exception {
  41         Sequence seq = new Sequence(Sequence.PPQ, 240);
  42         Track t = seq.createTrack();
  43 
  44         log("add 10 events in random order");
  45         t.add(createEvent(10, 5));
  46         t.add(createEvent(0, 0));
  47         t.add(createEvent(10, 6));
  48         t.add(createEvent(11, 8));
  49         t.add(createEvent(10, 7));
  50         t.add(createEvent(0, 1));
  51         t.add(createEvent(0, 2));
  52         t.add(createEvent(15, 9));
  53         t.add(createEvent(0, 3));
  54         t.add(createEvent(1, 4));
  55 
  56         // now compare the events.
  57         // The note param will tell us the
  58         // the expected position
  59         long lastTick = 0;
  60         for (int i = 0; i < t.size(); i++) {
  61             MidiEvent ev = t.get(i);
  62             if (ev.getMessage() instanceof ShortMessage) {
  63                 ShortMessage msg = (ShortMessage) ev.getMessage();
  64                 log(""+i+": ShortMessage at tick "+ev.getTick()
  65                     +" with expected position "+msg.getData1());
  66                 if (ev.getTick() < lastTick) {
  67                     log("  FAILED: last tick is larger than this event's tick!");
  68                     failed = true;
  69                 }
  70                 if (i != msg.getData1()) {
  71                     log("  FAILED: Track did not order correctly.");
  72                     failed = true;
  73                 }
  74             }
  75         }
  76 
  77         if (failed) throw new Exception("Test FAILED!");
  78         log("Test passed.");
  79     }
  80 
  81     public static MidiEvent createEvent(long tick, int expectedPos)
  82         throws Exception {
  83         ShortMessage msg = new ShortMessage();
  84         msg.setMessage(0x90, (int) expectedPos, 00);
  85         MidiEvent ev = new MidiEvent(msg, tick);
  86         return ev;
  87     }
  88 
  89     public static void log(String s) {
  90         System.out.println(s);
  91     }
  92 
  93     public static void log(Exception e) {
  94         //System.out.println(s);
  95         e.printStackTrace();
  96     }
  97 }