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