1 /*
2  * Copyright (C) 2011 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.camera.mediaeffects.tests.functional;
18 
19 import android.app.Activity;
20 import android.app.Instrumentation;
21 import android.content.Intent;
22 import android.media.MediaMetadataRetriever;
23 import android.media.filterfw.samples.CameraEffectsRecordingSample;
24 import android.net.Uri;
25 import android.os.Environment;
26 import android.test.ActivityInstrumentationTestCase2;
27 import android.util.Log;
28 import android.view.KeyEvent;
29 
30 import androidx.test.filters.LargeTest;
31 
32 import java.io.File;
33 
34 public class EffectsVideoCapture extends ActivityInstrumentationTestCase2
35                                                <CameraEffectsRecordingSample> {
36     private static final String TAG = "EffectsVideoCaptureTest";
37     private static final long WAIT_FOR_PREVIEW = 4 * 1000; // 4 seconds
38 
EffectsVideoCapture()39     public EffectsVideoCapture() {
40         super(CameraEffectsRecordingSample.class);
41     }
42 
captureVideos(String reportTag, Instrumentation inst)43     private void captureVideos(String reportTag, Instrumentation inst) throws Exception{
44         int total_num_of_videos = 1;
45         int video_duration = 4 * 1000; // 4 seconds
46 
47         Log.v(TAG, reportTag);
48         for (int i = 0; i < total_num_of_videos; i++) {
49             Thread.sleep(WAIT_FOR_PREVIEW);
50             // record a video
51             inst.sendCharacterSync(KeyEvent.KEYCODE_CAMERA);
52             Thread.sleep(video_duration);
53             inst.sendCharacterSync(KeyEvent.KEYCODE_CAMERA);
54         }
55     }
56 
57     @LargeTest
testBackEffectsVideoCapture()58     public void testBackEffectsVideoCapture() throws Exception {
59         Instrumentation inst = getInstrumentation();
60 
61         Intent intent = new Intent();
62         intent.setClass(getInstrumentation().getTargetContext(),
63                 CameraEffectsRecordingSample.class);
64         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
65         intent.putExtra("OUTPUT_FILENAME", Environment.getExternalStorageDirectory().toString()
66                 + "/CameraEffectsRecordingTest.mp4");
67         Activity act = inst.startActivitySync(intent);
68         captureVideos("Back Camera Video Capture\n", inst);
69         act.finish();
70 
71         // Verification
72         File file = new File(Environment.getExternalStorageDirectory(),
73                 "CameraEffectsRecordingTest.mp4");
74         Uri uri = Uri.fromFile(file);
75         verify(getActivity(), uri);
76     }
77 
78     // Verify result code, result data, and the duration.
verify(CameraEffectsRecordingSample activity, Uri uri)79     private void verify(CameraEffectsRecordingSample activity, Uri uri) throws Exception {
80         assertNotNull(uri);
81         // Verify the video file
82         MediaMetadataRetriever retriever = new MediaMetadataRetriever();
83         retriever.setDataSource(activity, uri);
84         String duration = retriever.extractMetadata(
85                 MediaMetadataRetriever.METADATA_KEY_DURATION);
86         assertNotNull(duration);
87         int durationValue = Integer.parseInt(duration);
88         Log.v(TAG, "Video duration is " + durationValue);
89         assertTrue(durationValue > 0);
90     }
91 }
92