1 /*
2  * Copyright (C) 2009 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 android.hardware.Camera;
20 import android.os.Environment;
21 import android.test.AndroidTestCase;
22 import android.test.suitebuilder.annotation.MediumTest;
23 
24 import java.io.FileOutputStream;
25 
26 /**
27  * Tests for camera-related Permissions. Currently, this means
28  * android.permission.CAMERA.
29  */
30 public class CameraPermissionTest extends AndroidTestCase {
31 
32     private static String PATH_PREFIX = Environment.getExternalStorageDirectory().toString();
33     private static String CAMERA_IMAGE_PATH = PATH_PREFIX + "this-should-not-exist.jpg";
34 
35     @Override
setUp()36     protected void setUp() throws Exception {
37         super.setUp();
38     }
39 
40     class ShutterCallback implements Camera.ShutterCallback {
onShutter()41         public void onShutter() { }
42     }
43 
44     class RawPictureCallback implements Camera.PictureCallback {
onPictureTaken(byte [] rawData, Camera camera)45         public void onPictureTaken(byte [] rawData, Camera camera) { }
46     }
47 
48     class JpegPictureCallback implements Camera.PictureCallback {
onPictureTaken(byte [] jpegData, Camera camera)49         public void onPictureTaken(byte [] jpegData, Camera camera) {
50             if (jpegData == null) {
51                 // TODO: Is this good (= expected, = correct), or weird, or bad?
52                 return;
53             }
54 
55             try {
56                 FileOutputStream s = new FileOutputStream(CAMERA_IMAGE_PATH);
57                 s.write(jpegData);
58                 s.flush();
59             }
60             catch (SecurityException e) {
61                 // Sure, NOW they tell us (NOTE: this could be a side effect
62                 // of the upcoming WRITE_EXTERNAL_STORAGE permission).
63             } catch (Exception e) {
64                 // We didn't really need to save it anyway, did we?
65             }
66 
67             fail("Successfully captured an image of " + jpegData.length +
68                  " bytes, and saved it to " + CAMERA_IMAGE_PATH);
69         }
70     }
71 
72     /**
73      * Attempt to take a picture. Requires Permission:
74      * {@link android.Manifest.permission#CAMERA}.
75      */
76     @MediumTest
testCamera()77     public void testCamera() {
78         try {
79             (Camera.open()).takePicture(new ShutterCallback(),
80                                         new RawPictureCallback(),
81                                         new JpegPictureCallback());
82             fail("Was able to take a picture with the camera with no permission");
83         }
84         catch (SecurityException e) {
85             // expected
86         } catch (RuntimeException e) {
87             // expected
88             // The JNI layer isn't translating the EPERM error status into
89             // a SecurityException.
90         }
91     }
92 
93 }
94 
95