MediaPlayerStressTest.java revision 003a7569745640d700c318a463385ca5feb08728
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
17package com.android.mediaframeworktest.stress;
18
19import com.android.mediaframeworktest.MediaFrameworkTest;
20
21import android.app.Activity;
22import android.app.Instrumentation;
23import android.content.Intent;
24import android.hardware.Camera;
25import android.media.MediaPlayer;
26import android.media.MediaRecorder;
27import android.test.ActivityInstrumentationTestCase2;
28import android.test.suitebuilder.annotation.LargeTest;
29import android.util.Log;
30import android.view.SurfaceHolder;
31
32import com.android.mediaframeworktest.MediaNames;
33import com.android.mediaframeworktest.functional.CodecTest;
34
35import java.io.BufferedWriter;
36import java.io.File;
37import java.io.FileWriter;
38import java.io.Writer;
39
40import android.test.AndroidTestCase;
41import android.test.InstrumentationTestCase;
42
43/**
44 * Junit / Instrumentation test case for the media player
45 */
46public class MediaPlayerStressTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> {
47    private String TAG = "MediaPlayerStressTest";
48
49    public MediaPlayerStressTest() {
50        super("com.android.mediaframeworktest", MediaFrameworkTest.class);
51    }
52
53    protected void setUp() throws Exception {
54        //Insert a 2 second before launching the test activity. This is
55        //the workaround for the race condition of requesting the updated surface.
56        Thread.sleep(2000);
57        getActivity();
58        super.setUp();
59    }
60
61    private int mTotalPlaybackError = 0;
62    private int mTotalComplete = 0;
63    private int mTotalInfoUnknown = 0;
64    private int mTotalVideoTrackLagging = 0;
65    private int mTotalBadInterleaving = 0;
66    private int mTotalNotSeekable = 0;
67    private int mTotalMetaDataUpdate = 0;
68
69    private void writeTestOutput(String filename, Writer output) throws Exception{
70        output.write("File Name: " + filename);
71        output.write(" Complete: " + CodecTest.onCompleteSuccess);
72        output.write(" Error: " + CodecTest.mPlaybackError);
73        output.write(" Unknown Info: " + CodecTest.mMediaInfoUnknownCount);
74        output.write(" Track Lagging: " +  CodecTest.mMediaInfoVideoTrackLaggingCount);
75        output.write(" Bad Interleaving: " + CodecTest.mMediaInfoBadInterleavingCount);
76        output.write(" Not Seekable: " + CodecTest.mMediaInfoNotSeekableCount);
77        output.write(" Info Meta data update: " + CodecTest.mMediaInfoMetdataUpdateCount);
78        output.write("\n");
79    }
80
81    private void writeTestSummary(Writer output) throws Exception{
82        output.write("Total Result:\n");
83        output.write("Total Complete: " + mTotalComplete + "\n");
84        output.write("Total Error: " + mTotalPlaybackError + "\n");
85        output.write("Total Unknown Info: " + mTotalInfoUnknown + "\n");
86        output.write("Total Track Lagging: " + mTotalVideoTrackLagging + "\n" );
87        output.write("Total Bad Interleaving: " + mTotalBadInterleaving + "\n");
88        output.write("Total Not Seekable: " + mTotalNotSeekable + "\n");
89        output.write("Total Info Meta data update: " + mTotalMetaDataUpdate + "\n");
90        output.write("\n");
91    }
92
93    private void updateTestResult(){
94        if (CodecTest.onCompleteSuccess){
95            mTotalComplete++;
96        }
97        else if (CodecTest.mPlaybackError){
98            mTotalPlaybackError++;
99        }
100        mTotalInfoUnknown += CodecTest.mMediaInfoUnknownCount;
101        mTotalVideoTrackLagging += CodecTest.mMediaInfoVideoTrackLaggingCount;
102        mTotalBadInterleaving += CodecTest.mMediaInfoBadInterleavingCount;
103        mTotalNotSeekable += CodecTest.mMediaInfoNotSeekableCount;
104        mTotalMetaDataUpdate += CodecTest.mMediaInfoMetdataUpdateCount;
105    }
106
107    //Test that will start the playback for all the videos
108    //under the samples folder
109    @LargeTest
110    public void testVideoPlayback() throws Exception {
111        String fileWithError = "Filename:\n";
112        File playbackOutput = new File("/sdcard/PlaybackTestResult.txt");
113        Writer output = new BufferedWriter(new FileWriter(playbackOutput, true));
114
115        boolean testResult = true;
116        // load directory files
117        boolean onCompleteSuccess = false;
118        File dir = new File(MediaNames.MEDIA_SAMPLE_POOL);
119
120        Instrumentation inst = getInstrumentation();
121        Intent intent = new Intent();
122
123        intent.setClass(getInstrumentation().getTargetContext(), MediaFrameworkTest.class);
124        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
125
126        String[] children = dir.list();
127        if (children == null) {
128            Log.v("MediaPlayerApiTest:testMediaSamples", "dir is empty");
129            return;
130        } else {
131            for (int i = 0; i < children.length; i++) {
132                Activity act = inst.startActivitySync(intent);
133                //Get filename of directory
134                String filename = children[i];
135                onCompleteSuccess =
136                    CodecTest.playMediaSamples(dir + "/" + filename);
137                if (!onCompleteSuccess){
138                    //Don't fail the test right away, print out the failure file.
139                    fileWithError += filename + '\n';
140                    Log.v(TAG, "Failure File : " + fileWithError);
141                    testResult = false;
142                }
143                Thread.sleep(3000);
144                //Call onCreat to recreate the surface
145                act.finish();
146                //Write test result to an output file
147                writeTestOutput(filename,output);
148                //Get the summary
149                updateTestResult();
150            }
151            writeTestSummary(output);
152            output.close();
153            assertTrue("testMediaSamples", testResult);
154       }
155    }
156}