1 /* 2 * Copyright (C) 2012 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 package android.mediastress.cts; 17 18 import android.app.Instrumentation; 19 import android.content.Intent; 20 import android.media.MediaFormat; 21 import android.media.MediaRecorder.AudioEncoder; 22 import android.media.MediaRecorder.VideoEncoder; 23 import android.os.Environment; 24 import android.test.ActivityInstrumentationTestCase2; 25 import android.util.Log; 26 27 import com.android.compatibility.common.util.MediaUtils; 28 29 import junit.framework.Assert; 30 31 public class NativeMediaTest extends ActivityInstrumentationTestCase2<NativeMediaActivity> { 32 private static final String TAG = "NativeMediaTest"; 33 private static final String MIME_TYPE = MediaFormat.MIMETYPE_VIDEO_AVC; 34 private static final int VIDEO_CODEC = VideoEncoder.H264; 35 private static final int NUMBER_PLAY_PAUSE_REPEATITIONS = 10; 36 private static final long PLAY_WAIT_TIME_MS = 4000; 37 38 public NativeMediaTest() { 39 super(NativeMediaActivity.class); 40 } 41 42 public void test1080pPlay() throws InterruptedException { 43 runPlayTest(1920, 1080); 44 } 45 46 public void test720pPlay() throws InterruptedException { 47 runPlayTest(1280, 720); 48 } 49 50 public void test480pPlay() throws InterruptedException { 51 runPlayTest(720, 480); 52 } 53 54 public void testDefaultPlay() throws InterruptedException { 55 runPlayTest(480, 360); 56 } 57 58 private void runPlayTest(int width, int height) throws InterruptedException { 59 MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, width, height); 60 // Don't run the test if the codec isn't supported. 61 if (!MediaUtils.canDecode(format)) { 62 return; // skip 63 } 64 65 Intent intent = new Intent(); 66 intent.putExtra(NativeMediaActivity.EXTRA_VIDEO_HEIGHT, height); 67 setActivityIntent(intent); 68 final NativeMediaActivity activity = getActivity(); 69 final Instrumentation instrumentation = getInstrumentation(); 70 waitForNativeMediaLifeCycle(activity, true); 71 Thread.sleep(PLAY_WAIT_TIME_MS); // let it play for some time 72 for (int i = 0; i < NUMBER_PLAY_PAUSE_REPEATITIONS; i++) { 73 instrumentation.runOnMainSync(() -> { 74 instrumentation.callActivityOnPause(activity); 75 }); 76 instrumentation.waitForIdleSync(); 77 waitForNativeMediaLifeCycle(activity, false); 78 instrumentation.runOnMainSync(() -> { 79 instrumentation.callActivityOnResume(activity); 80 }); 81 waitForNativeMediaLifeCycle(activity, true); 82 Thread.sleep(PLAY_WAIT_TIME_MS); // let it play for some time 83 } 84 } 85 86 /** 87 * wait until life cycle change and checks if the current status is in line with expectation 88 * @param activity 89 * @param expectAlive expected status, true if it should be alive. 90 * @throws InterruptedException 91 */ 92 private void waitForNativeMediaLifeCycle(NativeMediaActivity activity, boolean expectAlive) 93 throws InterruptedException { 94 Boolean status = activity.waitForNativeMediaLifeCycle(); 95 Assert.assertNotNull(status); // null means time-out 96 Assert.assertEquals(expectAlive, status.booleanValue()); 97 } 98 } 99