public void test_remove() throws Exception {
/*
* create an empty Track
*/
Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
Track tr = seq.createTrack();
byte[] bt = new byte[] {1, 2, 3};
MidiMessage1 message = new MidiMessage1(bt);
MidiEvent event1 = new MidiEvent(message, 10L);
MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 0);
MidiEvent event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1}), 6L);
tr.add(event1);
tr.add(event2);
tr.add(event3);
assertEquals(4, tr.size());
assertTrue(tr.remove(event3));
assertFalse(tr.remove(event3));
assertEquals(3, tr.size());
assertFalse(tr.remove(new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 0)));
assertTrue(tr.remove(event2));
assertFalse(tr.remove(null));
/*
* I can remove meta-event End of Track
*/
MidiEvent event4 = tr.get(1);
assertTrue(tr.remove(event4));
assertTrue(tr.add(event4));
assertTrue(tr.remove(event4));
assertTrue(tr.remove(event1));
/*
* If I remove meta-event End of Track and I have some
* events in Track still, and I try to add new event, it's
* all right, it adds, and so meta-event End of Track adds too;
* if Track doesn't contain any events, i.e. method size()
* return 0, if I try to add meta-event End of Track, it's
* all right, but if I try to add not meta-event, I have some problem.
* In the first place, meta-event adds to the Track, but event
* I want to add doesn't add, i.e. size() return 1. But after it
* I use method add(MidiEvent), and
* method add(<the_same_event_I_want_to_add>) return 'false'!!!
* And method remove(<the_same_event_I_want_to_add>) return
* 'false' too! And only now method add(<the_same_event_I_want_to_add>)
* return 'true', and this event adds to Track;
* Other events accrue to Track correctly.
*/
assertEquals(0, tr.size());
assertTrue(tr.add(event4)); //add meta-event to empty track; it's OK
assertTrue(tr.add(event1)); //add some event
assertEquals(2, tr.size());
assertTrue(tr.remove(event4)); //remove meta-event End of Track
assertTrue(tr.add(event2)); //add some event...
assertEquals(3, tr.size()); //...and size of Track is 3; meta-event
//was added
assertTrue(tr.remove(tr.get(2)));
assertTrue(tr.remove(event2));
assertTrue(tr.remove(event1)); //now Track is empty
try {
tr.add(event2); //add some event; I catch exception
fail("ArrayIndexOutOfBoundsException expected");
} catch (ArrayIndexOutOfBoundsException e) {}
assertEquals(1, tr.size()); //size of Track is 1; it contain meta-event
assertTrue(tr.add(event1)); //I can add event1,...
assertFalse(tr.add(event2)); //...but I can't add event2...
assertFalse(tr.remove(event2)); //...and remove it too!
assertTrue(tr.add(event2)); //but now it's all right!
assertEquals(3, tr.size()); //Track contain two my events and meta-event
}