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