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 17package com.android.camera.functional; 18 19import com.android.camera.Camera; 20import com.android.camera.R; 21 22import android.app.Activity; 23import android.app.Instrumentation; 24import android.content.Intent; 25import android.graphics.Bitmap; 26import android.graphics.BitmapFactory; 27import android.net.Uri; 28import android.os.Environment; 29import android.os.Process; 30import android.provider.MediaStore; 31import android.test.ActivityInstrumentationTestCase2; 32import android.test.suitebuilder.annotation.LargeTest; 33import android.test.UiThreadTest; 34import android.util.Log; 35import android.view.KeyEvent; 36 37import java.io.BufferedInputStream; 38import java.io.File; 39import java.io.FileInputStream; 40 41public class ImageCaptureIntentTest extends ActivityInstrumentationTestCase2 <Camera> { 42 private static final String TAG = "ImageCaptureIntentTest"; 43 private Intent mIntent; 44 45 public ImageCaptureIntentTest() { 46 super(Camera.class); 47 } 48 49 @Override 50 protected void setUp() throws Exception { 51 super.setUp(); 52 mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 53 } 54 55 @LargeTest 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 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 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 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 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 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 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 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 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