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