CameraActivityTest.java revision 32108707f57e12a5ccb1ab023e83abf8e0086330
1/* 2 * Copyright (C) 2012 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.activity; 18 19import com.android.camera.Camera; 20import com.android.camera.CameraDevice; 21import com.android.camera.CameraHolder; 22import com.android.camera.IntentExtras; 23import com.android.camera.MockCamera; 24import com.android.camera.R; 25 26import android.app.Instrumentation; 27import android.hardware.Camera.CameraInfo; 28import android.test.ActivityInstrumentationTestCase2; 29import android.test.suitebuilder.annotation.LargeTest; 30import android.view.MotionEvent; 31import android.view.View; 32 33public class CameraActivityTest extends ActivityInstrumentationTestCase2 <Camera> { 34 private static final String TAG = "CameraActivityTest"; 35 private CameraInfo mCameraInfo[]; 36 37 public CameraActivityTest() { 38 super(Camera.class); 39 } 40 41 @Override 42 protected void setUp() throws Exception { 43 super.setUp(); 44 mCameraInfo = new CameraInfo[2]; 45 mCameraInfo[0] = new CameraInfo(); 46 mCameraInfo[0].facing = CameraInfo.CAMERA_FACING_BACK; 47 mCameraInfo[1] = new CameraInfo(); 48 mCameraInfo[1].facing = CameraInfo.CAMERA_FACING_FRONT; 49 } 50 51 @Override 52 protected void tearDown() throws Exception { 53 super.tearDown(); 54 CameraHolder.injectMockCamera(null, null); 55 } 56 57 @LargeTest 58 public void testFailToConnect() throws Exception { 59 CameraHolder.injectMockCamera(mCameraInfo, null); 60 61 getActivity(); 62 Instrumentation inst = getInstrumentation(); 63 inst.waitForIdleSync(); 64 } 65 66 @LargeTest 67 public void testRestoreDefault() throws Exception { 68 MockCamera[] cameras = new MockCamera[2]; 69 cameras[0] = new MockCamera(); 70 cameras[1] = new MockCamera(); 71 CameraHolder.injectMockCamera(mCameraInfo, cameras); 72 73 getActivity(); 74 getInstrumentation().waitForIdleSync(); 75 performClick(R.id.second_level_indicator); 76 performClick(R.id.other_setting_indicator); 77 performClick(R.id.restore_default); 78 performClick(R.id.rotate_dialog_button1); 79 } 80 81 private void performClick(final int id) { 82 assertNotNull(getActivity().findViewById(id)); 83 Instrumentation inst = getInstrumentation(); 84 inst.runOnMainSync(new Runnable() { 85 @Override 86 public void run() { 87 View v = getActivity().findViewById(id); 88 float x = (v.getLeft() + v.getRight()) / 2; 89 float y = (v.getTop() + v.getBottom()) / 2; 90 MotionEvent down = MotionEvent.obtain(0, 0, 91 MotionEvent.ACTION_DOWN, x, y, 0, 0, 0, 0, 0, 0, 0); 92 MotionEvent up = MotionEvent.obtain(0, 0, 93 MotionEvent.ACTION_UP, x, y, 0, 0, 0, 0, 0, 0, 0); 94 View parent = (View) v.getParent(); 95 parent.dispatchTouchEvent(down); 96 parent.dispatchTouchEvent(up); 97 } 98 }); 99 inst.waitForIdleSync(); 100 } 101} 102