2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.functional;
19 import com.android.camera.CameraActivity;
20 import com.android.gallery3d.R;
22 import android.app.Activity;
23 import android.content.ContentResolver;
24 import android.content.Intent;
25 import android.database.Cursor;
26 import android.media.MediaMetadataRetriever;
27 import android.net.Uri;
28 import android.os.Environment;
29 import android.provider.MediaStore;
30 import android.provider.MediaStore.Video.VideoColumns;
31 import android.test.ActivityInstrumentationTestCase2;
32 import android.test.suitebuilder.annotation.LargeTest;
33 import android.util.Log;
34 import android.view.KeyEvent;
38 public class VideoCaptureIntentTest extends ActivityInstrumentationTestCase2 <CameraActivity> {
39 private static final String TAG = "VideoCaptureIntentTest";
40 private Intent mIntent;
41 private Uri mVideoUri;
42 private File mFile, mFile2;
44 public VideoCaptureIntentTest() {
45 super(CameraActivity.class);
49 protected void setUp() throws Exception {
51 mIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
55 protected void tearDown() throws Exception {
56 if (mVideoUri != null) {
57 ContentResolver resolver = getActivity().getContentResolver();
58 Uri query = mVideoUri.buildUpon().build();
59 String[] projection = new String[] {VideoColumns.DATA};
63 cursor = resolver.query(query, projection, null, null, null);
64 if (cursor != null && cursor.moveToFirst()) {
65 new File(cursor.getString(0)).delete();
68 if (cursor != null) cursor.close();
71 resolver.delete(mVideoUri, null, null);
73 if (mFile != null) mFile.delete();
74 if (mFile2 != null) mFile2.delete();
79 public void testNoExtraOutput() throws Exception {
80 setActivityIntent(mIntent);
86 Intent resultData = getActivity().getResultData();
87 mVideoUri = resultData.getData();
88 assertNotNull(mVideoUri);
89 verify(getActivity(), mVideoUri);
93 public void testExtraOutput() throws Exception {
94 mFile = new File(Environment.getExternalStorageDirectory(), "video.tmp");
96 Uri uri = Uri.fromFile(mFile);
97 mIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
98 setActivityIntent(mIntent);
104 verify(getActivity(), uri);
108 public void testCancel() throws Exception {
109 setActivityIntent(mIntent);
114 assertTrue(getActivity().isFinishing());
115 assertEquals(Activity.RESULT_CANCELED, getActivity().getResultCode());
119 public void testRecordCancel() throws Exception {
120 setActivityIntent(mIntent);
126 assertTrue(getActivity().isFinishing());
127 assertEquals(Activity.RESULT_CANCELED, getActivity().getResultCode());
131 public void testExtraSizeLimit() throws Exception {
132 mFile = new File(Environment.getExternalStorageDirectory(), "video.tmp");
133 final long sizeLimit = 500000; // bytes
135 Uri uri = Uri.fromFile(mFile);
136 mIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
137 mIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, sizeLimit);
138 mIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); // use low quality to speed up
139 setActivityIntent(mIntent);
145 verify(getActivity(), uri);
146 long length = mFile.length();
147 Log.v(TAG, "Video size is " + length + " bytes.");
148 assertTrue(length > 0);
149 assertTrue("Actual size=" + length, length <= sizeLimit);
153 public void testExtraDurationLimit() throws Exception {
154 mFile = new File(Environment.getExternalStorageDirectory(), "video.tmp");
155 final int durationLimit = 2; // seconds
157 Uri uri = Uri.fromFile(mFile);
158 mIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
159 mIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);
160 setActivityIntent(mIntent);
166 int duration = verify(getActivity(), uri);
167 // The duraion should be close to to the limit. The last video duration
168 // also has duration, so the total duration may exceeds the limit a
170 Log.v(TAG, "Video length is " + duration + " ms.");
171 assertTrue(duration < (durationLimit + 1) * 1000);
175 public void testExtraVideoQuality() throws Exception {
176 mFile = new File(Environment.getExternalStorageDirectory(), "video.tmp");
177 mFile2 = new File(Environment.getExternalStorageDirectory(), "video2.tmp");
179 Uri uri = Uri.fromFile(mFile);
180 mIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
181 mIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); // low quality
182 setActivityIntent(mIntent);
188 verify(getActivity(), uri);
191 uri = Uri.fromFile(mFile2);
192 mIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
193 mIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // high quality
194 setActivityIntent(mIntent);
200 verify(getActivity(), uri);
201 assertTrue(mFile.length() <= mFile2.length());
204 // Verify result code, result data, and the duration.
205 private int verify(CameraActivity activity, Uri uri) throws Exception {
206 assertTrue(activity.isFinishing());
207 assertEquals(Activity.RESULT_OK, activity.getResultCode());
209 // Verify the video file
210 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
211 retriever.setDataSource(activity, uri);
212 String duration = retriever.extractMetadata(
213 MediaMetadataRetriever.METADATA_KEY_DURATION);
214 assertNotNull(duration);
215 int durationValue = Integer.parseInt(duration);
216 Log.v(TAG, "Video duration is " + durationValue);
217 assertTrue(durationValue > 0);
218 return durationValue;
221 private void recordVideo(int ms) throws Exception {
222 getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_CAMERA);
224 getInstrumentation().runOnMainSync(new Runnable() {
227 // If recording is in progress, stop it. Run these atomically in
229 CameraActivity activity = getActivity();
230 if (activity.isRecording()) {
231 activity.findViewById(R.id.shutter_button).performClick();
237 private void recordVideo() throws Exception {
241 private void pressDone() {
242 getInstrumentation().runOnMainSync(new Runnable() {
245 getActivity().findViewById(R.id.btn_done).performClick();
250 private void pressCancel() {
251 getInstrumentation().runOnMainSync(new Runnable() {
254 getActivity().findViewById(R.id.btn_cancel).performClick();