Home | History | Annotate | Download | only in functional
      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.CameraActivity;
     20 
     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;
     29 
     30 import java.io.File;
     31 import java.lang.ref.WeakReference;
     32 import java.util.ArrayList;
     33 
     34 public class CameraTest extends InstrumentationTestCase {
     35     @LargeTest
     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"));
     47         }
     48     }
     49 
     50     @LargeTest
     51     public void testActivityLeak() throws Exception {
     52         checkActivityLeak(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
     53         checkActivityLeak(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
     54     }
     55 
     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));
     67             activity.finish();
     68             getInstrumentation().waitForIdleSync();
     69             activity = null;
     70         }
     71         Runtime.getRuntime().gc();
     72         Runtime.getRuntime().runFinalization();
     73         Runtime.getRuntime().gc();
     74         int refCount = 0;
     75         for (WeakReference<Activity> c: refs) {
     76             if (c.get() != null) refCount++;
     77         }
     78         // If applications are leaking activity, every reference is reachable.
     79         assertTrue(refCount != TEST_COUNT);
     80     }
     81 }
     82