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