1 /*
2  * Copyright (C) 2011 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.Camera;
20 import com.android.camera.R;
21 
22 import android.app.Activity;
23 import android.app.Instrumentation;
24 import android.content.Intent;
25 import android.graphics.Bitmap;
26 import android.graphics.BitmapFactory;
27 import android.net.Uri;
28 import android.os.Environment;
29 import android.os.Process;
30 import android.provider.MediaStore;
31 import android.test.ActivityInstrumentationTestCase2;
32 import android.test.suitebuilder.annotation.LargeTest;
33 import android.test.UiThreadTest;
34 import android.util.Log;
35 import android.view.KeyEvent;
36 
37 import java.io.BufferedInputStream;
38 import java.io.File;
39 import java.io.FileInputStream;
40 
41 public class ImageCaptureIntentTest extends ActivityInstrumentationTestCase2 <Camera> {
42     private static final String TAG = "ImageCaptureIntentTest";
43     private Intent mIntent;
44 
ImageCaptureIntentTest()45     public ImageCaptureIntentTest() {
46         super(Camera.class);
47     }
48 
49     @Override
setUp()50     protected void setUp() throws Exception {
51         super.setUp();
52         mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
53     }
54 
55     @LargeTest
testNoExtraOutput()56     public void testNoExtraOutput() throws Exception {
57         setActivityIntent(mIntent);
58         getActivity();
59 
60         takePicture();
61         pressDone();
62 
63         assertTrue(getActivity().isFinishing());
64         assertEquals(Activity.RESULT_OK, getActivity().getResultCode());
65         Intent resultData = getActivity().getResultData();
66         Bitmap bitmap = (Bitmap) resultData.getParcelableExtra("data");
67         assertNotNull(bitmap);
68         assertTrue(bitmap.getWidth() > 0);
69         assertTrue(bitmap.getHeight() > 0);
70     }
71 
72     @LargeTest
testExtraOutput()73     public void testExtraOutput() throws Exception {
74         File file = new File(Environment.getExternalStorageDirectory(),
75             "test.jpg");
76         BufferedInputStream stream = null;
77         byte[] jpegData;
78 
79         try {
80             mIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
81             setActivityIntent(mIntent);
82             getActivity();
83 
84             takePicture();
85             pressDone();
86 
87             assertTrue(getActivity().isFinishing());
88             assertEquals(Activity.RESULT_OK, getActivity().getResultCode());
89 
90             // Verify the jpeg file
91             int fileLength = (int) file.length();
92             assertTrue(fileLength > 0);
93             jpegData = new byte[fileLength];
94             stream = new BufferedInputStream(new FileInputStream(file));
95             stream.read(jpegData);
96         } finally {
97             if (stream != null) stream.close();
98             file.delete();
99         }
100 
101         Bitmap b = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
102         assertTrue(b.getWidth() > 0);
103         assertTrue(b.getHeight() > 0);
104     }
105 
106     @LargeTest
testRetake()107     public void testRetake() throws Exception {
108         setActivityIntent(mIntent);
109         getActivity();
110 
111         takePicture();
112         pressRetake();
113         takePicture();
114         pressDone();
115 
116         assertTrue(getActivity().isFinishing());
117         assertEquals(Activity.RESULT_OK, getActivity().getResultCode());
118         Intent resultData = getActivity().getResultData();
119         Bitmap bitmap = (Bitmap) resultData.getParcelableExtra("data");
120         assertNotNull(bitmap);
121         assertTrue(bitmap.getWidth() > 0);
122         assertTrue(bitmap.getHeight() > 0);
123     }
124 
125     @LargeTest
testCancel()126     public void testCancel() throws Exception {
127         setActivityIntent(mIntent);
128         getActivity();
129 
130         pressCancel();
131 
132         assertTrue(getActivity().isFinishing());
133         assertEquals(Activity.RESULT_CANCELED, getActivity().getResultCode());
134     }
135 
136     @LargeTest
testSnapshotCancel()137     public void testSnapshotCancel() throws Exception {
138         setActivityIntent(mIntent);
139         getActivity();
140 
141         takePicture();
142         pressCancel();
143 
144         assertTrue(getActivity().isFinishing());
145         assertEquals(Activity.RESULT_CANCELED, getActivity().getResultCode());
146     }
147 
takePicture()148     private void takePicture() throws Exception {
149         getInstrumentation().sendKeySync(
150                 new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_FOCUS));
151         getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_CAMERA);
152         Thread.sleep(4000);
153     }
154 
pressDone()155     private void pressDone() {
156         getInstrumentation().runOnMainSync(new Runnable() {
157             public void run() {
158                 getActivity().findViewById(R.id.btn_done).performClick();
159             }
160         });
161     }
162 
pressRetake()163     private void pressRetake() {
164         getInstrumentation().runOnMainSync(new Runnable() {
165             public void run() {
166                 getActivity().findViewById(R.id.btn_retake).performClick();
167             }
168         });
169     }
170 
pressCancel()171     private void pressCancel() {
172         getInstrumentation().runOnMainSync(new Runnable() {
173             public void run() {
174                 getActivity().findViewById(R.id.btn_cancel).performClick();
175             }
176         });
177     }
178 }
179