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