官方连接: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 testmActivity = getActivity();// Get a handle to the Activity object's main UI widget, a SpinnermSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01);// Set the Spinner to a known positionmActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);// Stop the activity - The onDestroy() method should save the state of the SpinnermActivity.finish();// Re-start the Activity - the onResume() method should restore the state of the SpinnermActivity = getActivity();// Get the Spinner's current positionint currentPosition = mActivity.getSpinnerPosition();// Assert that the current position is the same as the starting positionassertEquals(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

Instrumentation安卓官方简介(个人认为是HighLevel抽象出来的最简洁明了的阐述)相关推荐

  1. 安卓Launcher 简介

    转载请注明出处:安卓Launcher 简介_安卓launcher是什么_Mr_Leixiansheng的博客-CSDN博客 文章概述: 1.什么是Launcher 2.新建一个Launcher工程 3 ...

  2. 手机安卓系统简介及测试经验总结

    手机安卓系统简介及测试经验总结 一.Android简介 Android(安卓)系统是手机或一些平板电脑等终端的操作系统,可以说是现在最流行的系统之一.是目前最流行的手机智能平台,目前广泛的应用在智能手 ...

  3. android o vts测试项,安卓官方测试工具vts

    安卓官方测试工具vts 之前在审计android hal层源码时注意到存在很多test文件,但是又不能直接使用. 谷歌肯定通过某种方式将他们集成到一起,通过搜索得知为vts vts简介 vts是谷歌给 ...

  4. AndroidOrientation Sensor(方向传感器),新的替代方法详解(安卓官方提供)

    本文将带大家去解读下安卓官方关于方向传感器数据,提供的新方法.熟悉手机传感器开发的朋友对这段代码一定不会陌生吧. sm.registerListener(this, sm.getDefaultSens ...

  5. 移动端-安卓-接口测试简介

    移动端-安卓-接口测试简介 前言 一.接口测试测什么? 二.什么是接口测试? 三.接口测试和单元测试的区别 四.接口测试的价值和意义 总结 前言 直接入手大家可能没什么概念,迷迷糊糊,先抛出几个问题给 ...

  6. 最新安卓官方api文档完整版

    点击上方蓝字"优派编程"选择"加为星标",第一时间关注原创干货 原文地址 https://www.fang1688.cn/study-code/1840.htm ...

  7. android最新版本6,keep安卓官方最新版

    keep安卓官方最新版是一款非常好用的全方位健身app,keep安卓官方最新版有详细的视频教学,相当于你的私人移动健身教练,让你轻松获得完美身材,课程丰富,喜爱的朋友赶快下载体验吧! keep安卓官方 ...

  8. 安卓官方原生系统与AOSP区别

    AOSP,"Android Open-Source Project"的缩写,中文意为"Android开放源代码项目".在Android Open-Source ...

  9. 一键清理大师 v5.6.0 安卓官方版

    名称:一键清理大师 v5.6.0 安卓官方版 版本:v5.6.0 大小:2.62 MB 软件语言:简体中文 软件授权:免费版 系统要求:Android 2.1.x 以上 一键清理大师手机版是一款安卓平 ...

最新文章

  1. mysql分页的优势_数据库经典分页几种实例及各优缺点
  2. 设计模式-UML图简单介绍
  3. 高能玩家!硬核自制小程序云“肝”动森
  4. python长沙_长沙python
  5. linux使用ping命令ping本机,Linux下使用ping命令判断网络的好坏
  6. UNIX 高级环境编程 第10章 信号
  7. 这篇文章让你搞懂 SpringMVC 国际化!
  8. 小米路由器3是基于linux,小米路由器3(MI-3)刷华硕固件不用虚拟机刷华硕固件无需虚拟机方法...
  9. java基础回顾之Map中 TreeMap排序原理-二叉树
  10. 质数合数相关操作python代码合集(比较全面,欢迎补充)
  11. 年底看机会,欢迎加入Java大数据招聘群!
  12. ParrotSec 中文社区 QQ群认证 Openssl解密
  13. VMtools的安装教程
  14. 2-9课:树和图的现实意义、区别与联系
  15. 研究音频编解码要看什么书
  16. 绕过黑名单检查实现文件上传1 ——合天网安实验室学习笔记
  17. 程序员啊,他又加班了!
  18. 第五章 存储数据 web scraping with python
  19. 关于出现expected declaration or statement at end of input [solution.c]
  20. 寄存器(R0~R16)以及从SysTick系统时钟理解RTOS移植初始化

热门文章

  1. 文通科技:OCR API主要功能
  2. linux命令行还屏幕刷新率,Linux下显示器分辨率及刷新率设置
  3. Python3 源码加密工具 pysec
  4. 计算机与科学技术暑期社会实践,计算机学院暑期社会实践活动圆满结束
  5. HTML5期末大作业——抗击疫情感动人物-逆行者(6页)专题网页设计 HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计
  6. 099.尼科彻斯定理
  7. JS正则表达式断言和贪婪
  8. Java全栈系列笔记
  9. 数字化工厂的定义是什么?如何看待数字化工厂的发展?
  10. c++leetcode542,994