Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2014 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 android.permission.cts;
     18 
     19 import static com.android.ex.camera2.blocking.BlockingStateCallback.*;
     20 
     21 import android.content.Context;
     22 import android.hardware.camera2.CameraDevice;
     23 import android.hardware.camera2.CameraManager;
     24 import android.os.Handler;
     25 import android.os.HandlerThread;
     26 import android.platform.test.annotations.Presubmit;
     27 import android.test.AndroidTestCase;
     28 import android.util.Log;
     29 
     30 import com.android.ex.camera2.blocking.BlockingCameraManager;
     31 import com.android.ex.camera2.blocking.BlockingStateCallback;
     32 
     33 /**
     34  * Tests for Camera2 API related Permissions. Currently, this means
     35  * android.permission.CAMERA.
     36  */
     37 public class Camera2PermissionTest extends AndroidTestCase {
     38     private static final String TAG = "CameraDeviceTest";
     39     private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
     40     private static final int CAMERA_CLOSE_TIMEOUT_MS = 2000;
     41 
     42     private CameraManager mCameraManager;
     43     private CameraDevice mCamera;
     44     private BlockingStateCallback mCameraListener;
     45     private String[] mCameraIds;
     46     protected Handler mHandler;
     47     protected HandlerThread mHandlerThread;
     48 
     49     @Override
     50     public void setContext(Context context) {
     51         super.setContext(context);
     52         mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
     53         assertNotNull("Can't connect to camera manager!", mCameraManager);
     54     }
     55 
     56     /**
     57      * Set up the camera2 test case required environments, including CameraManager,
     58      * HandlerThread, Camera IDs, and CameraStateCallback etc.
     59      */
     60     @Override
     61     protected void setUp() throws Exception {
     62         super.setUp();
     63         mCameraIds = mCameraManager.getCameraIdList();
     64         assertNotNull("Camera ids shouldn't be null", mCameraIds);
     65         mHandlerThread = new HandlerThread(TAG);
     66         mHandlerThread.start();
     67         mHandler = new Handler(mHandlerThread.getLooper());
     68         mCameraListener = new BlockingStateCallback();
     69     }
     70 
     71     @Override
     72     protected void tearDown() throws Exception {
     73         mHandlerThread.quitSafely();
     74         mHandler = null;
     75 
     76         super.tearDown();
     77     }
     78 
     79     /**
     80      * Attempt to open camera. Requires Permission:
     81      * {@link android.Manifest.permission#CAMERA}.
     82      */
     83     public void testCameraOpen() throws Exception {
     84         for (String id : mCameraIds) {
     85             try {
     86                 openCamera(id);
     87                 fail("Was able to open camera " + id + " with no permission");
     88             }
     89             catch (SecurityException e) {
     90                 // expected
     91             } finally {
     92                 closeCamera();
     93             }
     94         }
     95     }
     96 
     97     /**
     98      * Add and remove availability listeners should work without permission.
     99      */
    100     @Presubmit
    101     public void testAvailabilityCallback() throws Exception {
    102         DummyCameraListener availabilityListener = new DummyCameraListener();
    103         // Remove a not-registered listener is a no-op.
    104         mCameraManager.unregisterAvailabilityCallback(availabilityListener);
    105         mCameraManager.registerAvailabilityCallback(availabilityListener, mHandler);
    106         mCameraManager.unregisterAvailabilityCallback(availabilityListener);
    107         mCameraManager.registerAvailabilityCallback(availabilityListener, mHandler);
    108         mCameraManager.registerAvailabilityCallback(availabilityListener, mHandler);
    109         mCameraManager.unregisterAvailabilityCallback(availabilityListener);
    110         // Remove a previously-added listener second time is a no-op.
    111         mCameraManager.unregisterAvailabilityCallback(availabilityListener);
    112     }
    113 
    114     private class DummyCameraListener extends CameraManager.AvailabilityCallback {
    115         @Override
    116         public void onCameraAvailable(String cameraId) {
    117         }
    118 
    119         @Override
    120         public void onCameraUnavailable(String cameraId) {
    121         }
    122     }
    123 
    124     private void openCamera(String cameraId) throws Exception {
    125         mCamera = (new BlockingCameraManager(mCameraManager)).openCamera(
    126                 cameraId, mCameraListener, mHandler);
    127     }
    128 
    129     private void closeCamera() {
    130         if (mCamera != null) {
    131             mCamera.close();
    132             mCameraListener.waitForState(STATE_CLOSED, CAMERA_CLOSE_TIMEOUT_MS);
    133             mCamera = null;
    134         }
    135     }
    136 }
    137