官方连接: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可以把测试包和被测应用加载到同一个进程中运行。既然各个控件和测试代码都运行在同一个进程中了,测试代码当然就可以调用这些控件的方法了,同时修改和验证这些控件的一些项也不在话下了。


 

作者

自主博客

微信

CSDN

天地会珠海分舵

http://techgogogo.com


服务号:TechGoGoGo

扫描码:

©著作权归作者所有:来自51CTO博客作者zhukev的原创作品,如需转载,请注明出处,否则将追究法律责任

andriod Instrumentation robotium

0

收藏

上一篇:MonkeyRunner创建一个... 下一篇:android基础知识12:an...
noavatar_middle.gif
zhukev

117篇文章,7W+人气,0粉丝

noavatar_middle.gif

Ctrl+Enter 发布

发布

取消

猜你喜欢

我的友情链接 最新HTML BroadcastChannel API引荐 Matrix源码分析之第一篇 使用LiveDataBus进行组件通信,让你永无后顾之忧 React Native集成到Android项目当中 面试经历:记一次Android面试 Android:实现模块 api 化 在Android中集成flutter 带你深入理解Android中的自定义属性!!! 程序员笔记|JSBridge框架解决通信问题实现移动端跨平台开发 Java注解实战之APT构建模块化的第一步 怎么让软键盘弹出时,部分控件上移 MVVM 架构与数据绑定库 Android自定义Dialog及与Activity的交互 开发十年,就只剩下这套架构体系了! Flutter交互实战-即刻App探索页下拉&拖拽效果 Fragments片段
left-qr.jpg

扫一扫,领取大礼包

0

分享
zhukev
noavatar_middle.gif