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