2 * Copyright (C) 2010 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;
21 import android.app.Activity;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Environment;
25 import android.os.Process;
26 import android.provider.MediaStore;
27 import android.test.InstrumentationTestCase;
28 import android.test.suitebuilder.annotation.LargeTest;
31 import java.lang.ref.WeakReference;
32 import java.util.ArrayList;
34 public class CameraTest extends InstrumentationTestCase {
36 public void testVideoCaptureIntentFdLeak() throws Exception {
37 Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
38 intent.setClass(getInstrumentation().getTargetContext(), CameraActivity.class);
39 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
40 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse("file://"
41 + Environment.getExternalStorageDirectory().toString()
42 + "test_fd_leak.3gp"));
43 getInstrumentation().startActivitySync(intent).finish();
44 // Test if the fd is closed.
45 for (File f: new File("/proc/" + Process.myPid() + "/fd").listFiles()) {
46 assertEquals(-1, f.getCanonicalPath().indexOf("test_fd_leak.3gp"));
51 public void testActivityLeak() throws Exception {
52 checkActivityLeak(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
53 checkActivityLeak(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
56 private void checkActivityLeak(String action) throws Exception {
57 final int TEST_COUNT = 5;
58 Intent intent = new Intent(action);
59 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
60 intent.setClass(getInstrumentation().getTargetContext(),
61 CameraActivity.class);
62 ArrayList<WeakReference<Activity>> refs =
63 new ArrayList<WeakReference<Activity>>();
64 for (int i = 0; i < TEST_COUNT; i++) {
65 Activity activity = getInstrumentation().startActivitySync(intent);
66 refs.add(new WeakReference<Activity>(activity));
68 getInstrumentation().waitForIdleSync();
71 Runtime.getRuntime().gc();
72 Runtime.getRuntime().runFinalization();
73 Runtime.getRuntime().gc();
75 for (WeakReference<Activity> c: refs) {
76 if (c.get() != null) refCount++;
78 // If applications are leaking activity, every reference is reachable.
79 assertTrue(refCount != TEST_COUNT);