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