Instrumentation安卓官方简介(个人认为是HighLevel抽象出来的最简介明了的阐述)
官方连接:http://developer.android.com/tools/testing/testing_android.html中间Instrumentation段落
(百度出来的Instrumentation的阐述大部分不是经过阉割就是过于冗长,看得人云里雾去的,此文翻译了官方的简介,从高层把Instrumentation框架做的阐述,以Q&A的思路说明白了究竟Instrumentation是怎么一回事。希望能对Instrumentation的初学者有所帮助)
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如何加载应用程序。
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()方法就会被调用。
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,但是你没办法把一个应用和另外一个已经在运行的应用运行在同一个进程里面。
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:
如果使用了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可以把测试包和被测应用加载到同一个进程中运行。既然各个控件和测试代码都运行在同一个进程中了,测试代码当然就可以调用这些控件的方法了,同时修改和验证这些控件的一些项也不在话下了。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。