1 /*
2  * Copyright (C) 2010 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 com.android.camera.functional;
18 
19 import com.android.camera.Camera;
20 import com.android.camera.R;
21 import com.android.camera.VideoCamera;
22 
23 import android.app.Activity;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.os.Environment;
27 import android.os.Process;
28 import android.provider.MediaStore;
29 import android.test.InstrumentationTestCase;
30 import android.test.suitebuilder.annotation.LargeTest;
31 import android.util.Log;
32 import android.view.KeyEvent;
33 
34 import java.io.File;
35 import java.lang.ref.WeakReference;
36 import java.util.ArrayList;
37 
38 public class CameraTest extends InstrumentationTestCase {
39     private static final String TAG = "CameraTest";
40 
41     @LargeTest
testVideoCaptureIntentFdLeak()42     public void testVideoCaptureIntentFdLeak() throws Exception {
43         Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
44         intent.setClass(getInstrumentation().getTargetContext(), VideoCamera.class);
45         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
46         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse("file://"
47                 + Environment.getExternalStorageDirectory().toString()
48                 + "test_fd_leak.3gp"));
49         getInstrumentation().startActivitySync(intent).finish();
50         // Test if the fd is closed.
51         for (File f: new File("/proc/" + Process.myPid() + "/fd").listFiles()) {
52             assertEquals(-1, f.getCanonicalPath().indexOf("test_fd_leak.3gp"));
53         }
54     }
55 
56     @LargeTest
testActivityLeak()57     public void testActivityLeak() throws Exception {
58         checkActivityLeak(Camera.class);
59         checkActivityLeak(VideoCamera.class);
60     }
61 
checkActivityLeak(Class<?> cls)62     private void checkActivityLeak(Class<?> cls) throws Exception {
63         final int TEST_COUNT = 5;
64         Intent intent = new Intent();
65         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
66         intent.setClass(getInstrumentation().getTargetContext(), cls);
67         ArrayList<WeakReference<Activity>> refs =
68                 new ArrayList<WeakReference<Activity>>();
69         for (int i = 0; i < TEST_COUNT; i++) {
70             Activity activity = getInstrumentation().startActivitySync(intent);
71             refs.add(new WeakReference<Activity>(activity));
72             activity.finish();
73             getInstrumentation().waitForIdleSync();
74             activity = null;
75         }
76         Runtime.getRuntime().gc();
77         Runtime.getRuntime().runFinalization();
78         Runtime.getRuntime().gc();
79         int refCount = 0;
80         for (WeakReference<Activity> c: refs) {
81             if (c.get() != null) refCount++;
82         }
83         // If applications are leaking activity, every reference is reachable.
84         assertTrue(refCount != TEST_COUNT);
85     }
86 }
87