android源代码中每个app下中都自带了一个test用例,下面主要介绍下camra单元测试用例
在AndroidManifest.xml中标明了测试用例instrumentation函数入口
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <!-- Copyright (C) 2008 The Android Open Source Project
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.camera.tests">
- <application>
- <uses-library android:name="android.test.runner" />
- </application>
- <instrumentation android:name="CameraLaunchPerformance"
- android:targetPackage="com.android.camera"
- android:label="Camera Launch Performance">
- </instrumentation>
- <instrumentation android:name="com.android.camera.CameraStressTestRunner"
- android:targetPackage="com.android.camera"
- android:label="Camera Stress Test InstrumentationRunner">
- </instrumentation>
- <instrumentation android:name="android.test.InstrumentationTestRunner"
- android:targetPackage="com.android.camera"
- android:label="Tests for Camera application."/>
- </manifest>
camera启动性能测试
Java代码
- package com.android.camera;
- import android.app.Activity;
- import android.os.Bundle;
- import android.test.LaunchPerformanceBase;
- /**
- * Instrumentation class for Camera launch performance testing.
- */
- public class CameraLaunchPerformance extends LaunchPerformanceBase {
- public static final String LOG_TAG = "CameraLaunchPerformance";
- public CameraLaunchPerformance() {
- super();
- }
- @Override
- public void onCreate(Bundle arguments) {
- super.onCreate(arguments);
- mIntent.setClassName(getTargetContext(), "com.android.camera.Camera");
- start();
- }
- /**
- * Calls LaunchApp and finish.
- */
- @Override
- public void onStart() {
- super.onStart();
- LaunchApp();
- finish(Activity.RESULT_OK, mResults);
- }
- }
camera拍照压力测试,参数设定为反复拍照100次
Java代码
- package com.android.camera.stress;
- import com.android.camera.Camera;
- import android.app.Instrumentation;
- import android.test.ActivityInstrumentationTestCase2;
- import android.test.suitebuilder.annotation.LargeTest;
- import android.util.Log;
- import android.view.KeyEvent;
- /**
- * Junit / Instrumentation test case for camera test
- *
- * Running the test suite:
- *
- * adb shell am instrument \
- * -e class com.android.camera.stress.ImageCapture \
- * -w com.android.camera.tests/com.android.camera.CameraStressTestRunner
- *
- */
- public class ImageCapture extends ActivityInstrumentationTestCase2 <Camera> {
- private String TAG = "ImageCapture";
- private static final int TOTAL_NUMBER_OF_IMAGECAPTURE = 100;
- private static final int TOTAL_NUMBER_OF_VIDEOCAPTURE = 100;
- private static final long WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN = 1000;
- private static final long WAIT_FOR_VIDEO_CAPTURE_TO_BE_TAKEN = 50000; //50seconds
- private static final long WAIT_FOR_PREVIEW = 1000; //1 seconds
- public ImageCapture() {
- super("com.android.camera", Camera.class);
- }
- @Override
- protected void setUp() throws Exception {
- getActivity();
- super.setUp();
- }
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
- }
- @LargeTest
- public void testImageCapture() {
- Instrumentation inst = getInstrumentation();
- try {
- for (int i = 0; i < TOTAL_NUMBER_OF_IMAGECAPTURE; i++) {
- Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
- inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
- inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
- Thread.sleep(WAIT_FOR_IMAGE_CAPTURE_TO_BE_TAKEN);
- }
- }