Instrumentation安卓官方简介(我的认为是HighLevel抽象出来的最简洁明了的阐述)

官方链接:http://developer.android.com/tools/testing/testing_android.html中间Instrumentation段落html

(百度出来的Instrumentation的阐述大部分不是通过阉割就是过于冗长,看得人云里雾去的,此文翻译了官方的简介,从高层把Instrumentation框架作的阐述,以Q&A的思路说明白了究竟Instrumentation是怎么一回事。但愿能对Instrumentation的初学者有所帮助)android

Instrumentation

Android instrumentation is a set of control methods or "hooks" in the Android system. These hooks control an Android component independently of its normal lifecycle. They also control how Android loads applications.微信

Android instrumentation是Android系统里面的一套控制方法或者”钩子“。这些钩子能够在正常的生命周期(正常是由操做系统控制的)以外控制Android控件的运行。它们同时能够控制Android如何加载应用程序。app

Normally, an Android component runs in a lifecycle determined by the system. For example, an Activity object's lifecycle starts when the Activity is activated by an Intent. The object's onCreate() method is called, followed by onResume(). When the user starts another application, the onPause() method is called. If the Activity code calls the finish() method, theonDestroy() method is called. The Android framework API does not provide a way for your code to invoke these callback methods directly, but you can do so using instrumentation.框架

正常来讲,一个Android控件运行的生命周期是由操做系统决定的。好比,一个Activity对象的生命周期开始于被一个Intent启动的时候,这时候该Activity对象的onCreate()方法就会被调用,紧跟着就是onResume();当用户启动另一个应用的时候,该Activity的onPause()方法就会被调用;若是该Activity的代码调用了finish()方法,那么onDestroy()方法就会被调用。ide

Android的API框架并无为你的代码提供一种方法去直接调用这些回调函数,可是经过instrumentation你就能够作到这一点。函数

Also, the system runs all the components of an application into the same process. You can allow some components, such as content providers, to run in a separate process, but you can't force an application to run in the same process as another application that is already running.测试

再者,操做系统把一个应用的全部控件都运行在同一个进程里面。你能够容许一些(特别的)控件运行在不一样的进程中,好比content providers,可是你没办法把一个应用和另一个已经在运行的应用运行在同一个进程里面。this

With Android instrumentation, though, you can invoke callback methods in your test code. This allows you to run through the lifecycle of a component step by step, as if you were debugging the component. The following test code snippet demonstrates how to use this to test that an Activity saves and restores its state:spa

若是使用了instrumentation,你就能够在你的测试代码中调用这些回调函数。这将容许你就像在调试该控件同样一步一步的把该控件的整个生命周期跑个遍。如下代码片断将会演示如何使用instrumentation去测试控制一个Activity保存和恢复其状态的:

    // Start the main activity of the application under test
    mActivity = getActivity();

    // Get a handle to the Activity object's main UI widget, a Spinner
    mSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01);

    // Set the Spinner to a known position
    mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);

    // Stop the activity - The onDestroy() method should save the state of the Spinner
    mActivity.finish();

    // Re-start the Activity - the onResume() method should restore the state of the Spinner
    mActivity = getActivity();

    // Get the Spinner's current position
    int currentPosition = mActivity.getSpinnerPosition();

    // Assert that the current position is the same as the starting position
    assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition);

The key method used here is getActivity(), which is a part of the instrumentation API. The Activity under test is not started until you call this method. You can set up the test fixture in advance, and then call this method to start the Activity.

这里用到的关键方法是instrumentation API里面的getActivity(),该被测的Activity在你没有调用此方法的时候是不会启动的。你也能够先把测试须要的环境配置好,而后再调用此方法来启动该Activity。

Also, instrumentation can load both a test package and the application under test into the same process. Since the application components and their tests are in the same process, the tests can invoke methods in the components, and modify and examine fields in the components.

同时,instrumentation能够把测试包和被测应用加载到同一个进程中运行。既然各个控件和测试代码都运行在同一个进程中了,测试代码固然就能够调用这些控件的方法了,同时修改和验证这些控件的一些项也不在话下了。


 

做者

自主博客

微信

CSDN

天地会珠海分舵

http://techgogogo.com


服务号:TechGoGoGo

扫描码:

http://blog.csdn.net/zhubaitian