本文共 20654 字,大约阅读时间需要 68 分钟。
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); } }
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{ 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); } } catch (Exception e) { Log.v(TAG, e.toString()); } assertTrue("testImageCapture", true); } @LargeTest public void testVideoCapture() { Instrumentation inst = getInstrumentation(); //Switch to the video mode inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); try { for (int i = 0; i < TOTAL_NUMBER_OF_VIDEOCAPTURE; i++) { Thread.sleep(WAIT_FOR_PREVIEW); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP); //record an video inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); Thread.sleep(WAIT_FOR_VIDEO_CAPTURE_TO_BE_TAKEN); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); Thread.sleep(WAIT_FOR_PREVIEW); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); } } catch (Exception e) { Log.v(TAG, e.toString()); } assertTrue("testVideoCapture", true); } }
package com.android.camera.stress; import com.android.camera.VideoCamera; 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.SwitchPreview \ * -w com.android.camera.tests/com.android.camera.CameraStressTestRunner * */ public class SwitchPreview extends ActivityInstrumentationTestCase2{ private String TAG = "SwitchPreview"; private static final int TOTAL_NUMBER_OF_SWITCHING = 200; private static final long WAIT_FOR_PREVIEW = 2000; public SwitchPreview() { super("com.android.camera", VideoCamera.class); } @Override protected void setUp() throws Exception { getActivity(); super.setUp(); } @Override protected void tearDown() throws Exception { getActivity().finish(); super.tearDown(); } @LargeTest public void testSwitchMode() { //Switching the video and the video recorder mode Instrumentation inst = getInstrumentation(); try{ for (int i=0; i< TOTAL_NUMBER_OF_SWITCHING; i++) { Thread.sleep(WAIT_FOR_PREVIEW); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_LEFT); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); Thread.sleep(WAIT_FOR_PREVIEW); } } catch (Exception e){ Log.v(TAG, e.toString()); } assertTrue("testSwitchMode",true); } }
import android.app.Activity; import android.os.Bundle; public class AndroidUT extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public int add(int a, int b) { return a + b; } }
import com.android.ut.AndroidUT; import android.test.ActivityInstrumentationTestCase; public class TestApp extends ActivityInstrumentationTestCase{ public TestApp() { super("com.android.ut", AndroidUT.class); } public void testSum() { assertEquals(5, getActivity().add(2, 3)); } }
需要注意的是,在这里面我加上了:
adb shell am instrument -e class com.android.ut.test.TestApp -wcom.android.ut/android.test.InstrumentationTestRunner
# am instrument -e class com.cn.test.TestApp -w com.cn/android.test.InstrumentationTestRunner
D/AndroidRuntime( 941): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< D/AndroidRuntime( 941): CheckJNI is ON D/AndroidRuntime( 941): --- registering native functions --- D/FileBackupHelper_native( 941): register_android_backup_FileBackupHelper D/ActivityManager( 581): Uninstalling process com.cn I/ActivityManager( 581): Start proc com.cn for added application com.cn: pid=948 uid=10013 gids={} I/TestRunner( 948): started: testSum(com.cn.test.TestApp) //启动add()测试方法 I/ActivityManager( 581): Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.cn/.AndroidUT } I/ActivityManager( 581): Displayed activity com.cn/.AndroidUT: 645 ms (total 645 ms) I/TestRunner( 948): finished: testSum(com.cn.test.TestApp) I/TestRunner( 948): passed: testSum(com.cn.test.TestApp) I/TestRunner( 948): started: testActivityTestCaseSetUpProperly(com.cn.test.TestApp) I/ActivityManager( 581): Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.cn/.AndroidUT } I/ActivityManager( 581): Displayed activity com.cn/.AndroidUT: 412 ms (total 412 ms) I/TestRunner( 948): finished: testActivityTestCaseSetUpProperly(com.cn.test.TestApp) I/TestRunner( 948): passed: testActivityTestCaseSetUpProperly(com.cn.test.TestApp) D/ActivityManager( 581): Uninstalling process com.cn D/ActivityManager( 581): Force removing process ProcessRecord{43851fa0 948:com.cn/10013} (com.cn/10013) D/AndroidRuntime( 941): Shutting down VM
# # An unexpected error has been detected by Java Runtime Environment: # # Internal Error (classFileParser.cpp:2924), pid=4900, tid=4476 #Error: ShouldNotReachHere() # # Java VM: Java HotSpot(TM) Client VM (10.0-b19 mixed mode windows-x86) # An error report file with more information is saved as: # E:\Mydoc\EclipseWorkspace\TestAndroid\hs_err_pid4900.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp #
这样,在启动程序的时候就会先启动一个Application,然后在此Application运行过程中根据情况加载相应的 Activity,而Activity是需要一个界面的。但是Instrumentation并不是这样的。你可以将Instrumentation理解为一种没有图形界面的,具有启动能力的,用于监控其他类(用Target Package声明)的工具类。任何想成为Instrumentation的类必须继承android.app.Instrumentation。下面是这个类的解释:……
adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner当然,也可以利用adb shell先进入android命令行模式,再直接写am instrument –w XXXXXXX。下面将具体介绍如何将根据需要加载一组单元测试。 如何在Android中利用Instrumentation来进行测试?
adb shell am instrument -e class com.android.foo.MyTestSuite -w com.android.foo/android.test.InstrumentationTestRunner
adb shell am instrument -e class com.android.foo.MyTestCase -w com.android.foo/android.test.InstrumentationTestRunner
adb shell am instrument -e class com.android.foo.MyTestCase#testFoo -w com.android.foo/android.test.InstrumentationTestRunner然后,所有的测试结果会输出到控制台,并会做一系列统计,如标记为E的是Error,标记为F的是Failure,Success的测试则会标记为一个点。这和JUnit的语义一致。如果希望断点调试你的测试,只需要直接在代码上加上断点,然后将运行命令参数的-e后边附加上debug true后运行即可。更加详细的内容可以看InstrumentationTestRunner的Javadoc。我希望Android能尽快有正式的文档来介绍这个内容。 如何在Android的单元测试中做标记?
Package Name: com.android.testapp Activity Name: MainActivity Application Name: TestApp 以下是MainActivity的源代码: package com.android.testapp; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public int sum(int a, int b) { return a + b; } public int substract(int a, int b) { return b - a; } }其中,我故意将减法的a – b写成了b – a。 2. 编写测试程序
package com.android.testapp.test; import com.android.testapp.MainActivity; import android.test.ActivityInstrumentationTestCase; import android.test.suitebuilder.annotation.MediumTest; public class TestMainActivity extends ActivityInstrumentationTestCase我继承了ActivityInstrumentationTestCase。这个TestCase在执行时会自动帮我启动相应的Activity。{ public TestMainActivity() { super("com.android.testapp", MainActivity.class); } public TestMainActivity(String pkg, Class activityClass) { super(pkg, activityClass); } @MediumTest public void testSum() { assertEquals(3, getActivity().sum(1, 2)); } @MediumTest public void testSubstract() { assertEquals(-1, getActivity().substract(1, 2)); } }
在这个文件中,我将Activity和Instrumentation的声明写到了一起,而没有像Apis Demo那样分开。请注意里面的<uses-library>标签。如果没有那句,在运行测试时会报告找不到TestRunner。这是由于 Android在build的时候只把需要的东西打包,所以你必须明确的告诉Android Builder这一点。 3. Build和Install
adb shell pm list packages
adb shell am instrument –e class com.android.testapp.test.TestMainActivity –w com.android.testapp/android.test.InstrumentationTestRunner
* *三、被测试的Activity代码* * *
# package cc.androidos.activity; # import android.app.Activity; # import android.os.Bundle; # import android.widget.Button; # public class NewActivity extends Activity { # /** Called when the activity is first created. */ # @Override # public void onCreate(Bundle savedInstanceState) { # super.onCreate(savedInstanceState); # setContentView(R.layout.main); # } # public int add(int a , int b){ # return a+b; # } # }
* import cc.androidos.activity.NewActivity; * import cc.androidos.activity.R; * import android.test.ActivityInstrumentationTestCase2; * import android.util.Log; * import android.view.KeyEvent; * import android.widget.Button; * public class TestNewActivity extends * ActivityInstrumentationTestCase2{ * private Button button1 = null; * private Button button2 = null; * private NewActivity newActivity = null; * public TestNewActivity() { * super("cc.androidos.activity", NewActivity.class); * //This first parameter should the Activity package * //if other , the junit give us the exception: unable resolve the activity * } * * @Override * protected void setUp() throws Exception { * String tag = "setUp"; * Log.e(tag, "init all var...."); * newActivity = getActivity(); * button1 = (Button) newActivity.findViewById(R.id.Button01); * button2 = (Button) newActivity.findViewById(R.id.Button02); * } * /** * * Testing the button is focused or not * */ * public void testButtonFocus() { * String tag = "testButtonFocus"; * Log.e(tag, "start the button focus..."); * assertTrue("Button1 is focused", button1.isFocused()); * Log.e(tag, "send the down key event..."); * sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); * assertTrue("Button2 is focused", button2.isFocused()); * } * * /** * * Testing the add method in actvity * */ * public void testAdd(){ * String tag ="testAdd"; * Log.e(tag, "Test the add method in NewActivity..."); * int i = newActivity.add(2, 5); * assertEquals(7, i); * } * * }
* *Java代码* * ** ** ** * * ** * * * * *
转载地址:http://lnczl.baihongyu.com/