1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.media.cts; 18 19 import android.media.cts.R; 20 21 22 import android.content.res.AssetFileDescriptor; 23 import android.media.JetPlayer; 24 import android.media.JetPlayer.OnJetEventListener; 25 import android.os.Environment; 26 import android.os.Handler; 27 import android.os.Looper; 28 import android.platform.test.annotations.AppModeFull; 29 import android.test.AndroidTestCase; 30 31 import java.io.File; 32 import java.io.FileOutputStream; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 37 public class JetPlayerTest extends AndroidTestCase { 38 private OnJetEventListener mOnJetEventListener; 39 private boolean mOnJetUserIdUpdateCalled; 40 private boolean mOnJetPauseUpdateCalled; 41 private boolean mOnJetNumQueuedSegmentUpdateCalled; 42 private boolean mOnJetEventCalled; 43 private String mJetFile; 44 /* JetPlayer and Handler will be on the Main Looper */ 45 private Handler mHandler = new Handler(Looper.getMainLooper()); 46 private final JetPlayer mJetPlayer = JetPlayer.getJetPlayer(); 47 48 @Override 49 protected void setUp() throws Exception { 50 super.setUp(); 51 mOnJetEventListener = new MockOnJetEventListener(); 52 mJetFile = 53 new File(Environment.getExternalStorageDirectory(), "test.jet").getAbsolutePath(); 54 assertTrue(JetPlayer.getMaxTracks() > 0); 55 } 56 57 @Override 58 protected void tearDown() throws Exception { 59 // Prevent tests from failing with EAS_ERROR_FILE_ALREADY_OPEN 60 // after a previous test fails. 61 mJetPlayer.closeJetFile(); 62 63 File jetFile = new File(mJetFile); 64 if (jetFile.exists()) { 65 jetFile.delete(); 66 } 67 super.tearDown(); 68 } 69 70 @AppModeFull(reason = "Instant apps cannot access the SD card") 71 public void testLoadJetFromPath() throws Throwable { 72 assertTrue(mJetPlayer.clearQueue()); 73 prepareFile(); 74 mJetPlayer.setEventListener(mOnJetEventListener); 75 assertTrue(mJetPlayer.loadJetFile(mJetFile)); 76 runJet(); 77 } 78 79 public void testLoadJetFromFd() throws Throwable { 80 assertTrue(mJetPlayer.clearQueue()); 81 mJetPlayer.setEventListener(mOnJetEventListener, mHandler); 82 assertTrue(mJetPlayer.loadJetFile(mContext.getResources().openRawResourceFd(R.raw.test_jet))); 83 runJet(); 84 } 85 86 public void testQueueJetSegmentMuteArray() throws Throwable { 87 assertTrue(mJetPlayer.clearQueue()); 88 mJetPlayer.setEventListener(mOnJetEventListener, mHandler); 89 assertTrue(mJetPlayer.loadJetFile(mContext.getResources().openRawResourceFd(R.raw.test_jet))); 90 byte userID = 0; 91 int segmentNum = 3; 92 int libNum = -1; 93 int repeatCount = 0; 94 int transpose = 0; 95 boolean[] muteFlags = new boolean[32]; 96 assertTrue(mJetPlayer.queueJetSegmentMuteArray(segmentNum, libNum, 97 repeatCount, transpose, 98 muteFlags, userID)); 99 assertTrue(mJetPlayer.play()); 100 for (int i = 0; i < muteFlags.length; i++) { 101 muteFlags[i] = true; 102 } 103 muteFlags[8] = false; 104 muteFlags[9] = false; 105 muteFlags[10] = false; 106 assertTrue(mJetPlayer.queueJetSegmentMuteArray(segmentNum, libNum, 107 repeatCount, transpose, 108 muteFlags, userID)); 109 Thread.sleep(20000); 110 assertTrue(mJetPlayer.pause()); 111 assertTrue(mJetPlayer.clearQueue()); 112 assertFalse(mJetPlayer.play()); 113 assertTrue(mJetPlayer.closeJetFile()); 114 } 115 116 private void runJet() throws Throwable { 117 byte userID = 0; 118 int segmentNum = 3; 119 int libNum = -1; 120 int repeatCount = 1; 121 int transpose = 0; 122 int muteFlags = 0; 123 assertTrue(mJetPlayer.queueJetSegment(segmentNum, libNum, repeatCount, 124 transpose, muteFlags, userID)); 125 126 segmentNum = 6; 127 repeatCount = 1; 128 transpose = -1; 129 assertTrue(mJetPlayer.queueJetSegment(segmentNum, libNum, repeatCount, 130 transpose, muteFlags, userID)); 131 132 segmentNum = 7; 133 transpose = 0; 134 assertTrue(mJetPlayer.queueJetSegment(segmentNum, libNum, repeatCount, 135 transpose, muteFlags, userID)); 136 137 for (int i = 0; i < 7; i++) { 138 assertTrue(mJetPlayer.triggerClip(i)); 139 } 140 assertTrue(mJetPlayer.play()); 141 Thread.sleep(10000); 142 assertTrue(mJetPlayer.pause()); 143 assertFalse(mJetPlayer.setMuteArray(new boolean[40], false)); 144 boolean[] muteArray = new boolean[32]; 145 for (int i = 0; i < muteArray.length; i++) { 146 muteArray[i] = true; 147 } 148 muteArray[8] = false; 149 muteArray[9] = false; 150 muteArray[10] = false; 151 assertTrue(mJetPlayer.setMuteArray(muteArray, true)); 152 Thread.sleep(1000); 153 assertTrue(mJetPlayer.play()); 154 Thread.sleep(1000); 155 assertTrue(mJetPlayer.setMuteFlag(9, true, true)); 156 Thread.sleep(1000); 157 assertTrue(mJetPlayer.setMuteFlags(0, false)); 158 Thread.sleep(1000); 159 assertTrue(mJetPlayer.setMuteFlags(0xffffffff, false)); 160 Thread.sleep(1000); 161 assertTrue(mJetPlayer.setMuteFlags(0, false)); 162 Thread.sleep(30000); 163 assertTrue(mJetPlayer.pause()); 164 assertTrue(mJetPlayer.closeJetFile()); 165 assertTrue(mOnJetEventCalled); 166 assertTrue(mOnJetPauseUpdateCalled); 167 assertTrue(mOnJetNumQueuedSegmentUpdateCalled); 168 assertTrue(mOnJetUserIdUpdateCalled); 169 } 170 171 public void testClone() throws Exception { 172 try { 173 mJetPlayer.clone(); 174 fail("should throw CloneNotSupportedException"); 175 } catch (CloneNotSupportedException e) { 176 // expect here 177 } 178 } 179 180 private void prepareFile() throws IOException { 181 InputStream source = null; 182 OutputStream target = null; 183 try { 184 source = mContext.getResources().openRawResource(R.raw.test_jet); 185 target = new FileOutputStream(mJetFile); 186 byte[] buffer = new byte[1024]; 187 int length; 188 while ((length = source.read(buffer)) != -1) { 189 target.write(buffer, 0, length); 190 } 191 } finally { 192 if (source != null) { 193 source.close(); 194 } 195 if (target != null) { 196 target.close(); 197 } 198 } 199 } 200 201 private class MockOnJetEventListener implements OnJetEventListener { 202 203 public void onJetEvent(JetPlayer player, short segment, byte track, byte channel, 204 byte controller, byte value) { 205 mOnJetEventCalled = true; 206 } 207 208 public void onJetNumQueuedSegmentUpdate(JetPlayer player, int nbSegments) { 209 mOnJetNumQueuedSegmentUpdateCalled = true; 210 } 211 212 public void onJetPauseUpdate(JetPlayer player, int paused) { 213 mOnJetPauseUpdateCalled = true; 214 } 215 216 public void onJetUserIdUpdate(JetPlayer player, int userId, int repeatCount) { 217 mOnJetUserIdUpdateCalled = true; 218 } 219 } 220 } 221