android instrumentation 原理,理解 android instrumentation

2023-10-27

instrumentation

先看activity的启动过程:

@Override

public void startActivity(Intent intent, @Nullable Bundle options) {

if (options != null) {

startActivityForResult(intent, -1, options);

} else {

// Note we want to go through this call for compatibility with

// applications that may have overridden the method.

startActivityForResult(intent, -1);

}

}

继续看startActivityForResult:

public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,

@Nullable Bundle options) {

if (mParent == null) {

Instrumentation.ActivityResult ar =

mInstrumentation.execStartActivity(

this, mMainThread.getApplicationThread(), mToken, this,

intent, requestCode, options);

if (ar != null) {

mMainThread.sendActivityResult(

mToken, mEmbeddedID, requestCode, ar.getResultCode(),

ar.getResultData());

}

if (requestCode >= 0) {

// If this start is requesting a result, we can avoid making

// the activity visible until the result is received. Setting

// this code during onCreate(Bundle savedInstanceState) or onResume() will keep the

// activity hidden during this time, to avoid flickering.

// This can only be done when a result is requested because

// that guarantees we will get information back when the

// activity is finished, no matter what happens to it.

mStartedActivity = true;

}

cancelInputsAndStartExitTransition(options);

// TODO Consider clearing/flushing other event sources and events for child windows.

} else {

if (options != null) {

mParent.startActivityFromChild(this, intent, requestCode, options);

} else {

// Note we want to go through this method for compatibility with

// existing applications that may have overridden it.

mParent.startActivityFromChild(this, intent, requestCode);

}

}

}

这里我们看到activity的启动是由 instrumentation的execStartActivity执行的。

这种设计即委托观察者模式。

public ActivityResult execStartActivity(

Context who, IBinder contextThread, IBinder token, Activity target,

Intent intent, int requestCode, Bundle options) {

IApplicationThread whoThread = (IApplicationThread) contextThread;

Uri referrer = target != null ? target.onProvideReferrer() : null;

if (referrer != null) {

intent.putExtra(Intent.EXTRA_REFERRER, referrer);

}

if (mActivityMonitors != null) {

synchronized (mSync) {

final int N = mActivityMonitors.size();

for (int i=0; i

final ActivityMonitor am = mActivityMonitors.get(i);

if (am.match(who, null, intent)) {

am.mHits++;

if (am.isBlocking()) {

return requestCode >= 0 ? am.getResult() : null;

}

break;

}

}

}

}

try {

intent.migrateExtraStreamToClipData();

intent.prepareToLeaveProcess(who);

int result = ActivityManagerNative.getDefault()

.startActivity(whoThread, who.getBasePackageName(), intent,

intent.resolveTypeIfNeeded(who.getContentResolver()),

token, target != null ? target.mEmbeddedID : null,

requestCode, 0, null, options);

checkStartActivityResult(result, intent);

} catch (RemoteException e) {

throw new RuntimeException("Failure from system", e);

}

return null;

}

自动化测试,instrumentaiontestcase:

testactivity = (testClass)getInstrumentation().startActivitySync(intent);

text = testactivity.findViewById(R.id.text1);

assertEquals("hello,world", text.getText().toString());

...不知道源码也可以的。

/** This testcase starts the same activity and sending a key event to that activity under test*/

private void testcase3() {

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra(“STRING”, “Hai”);

intent.setClassName(getTargetContext(), "MyActivity.class");

MyActivity activity = (MyActivity)startActivitySync(_intent);

act = new ActivityRunnable(activity);

runOnMainSync(act);

package com.mycompany;

import android.app.*;

import android.content.*;

import android.test.*;

import android.test.suitebuilder.annotation.*;

import android.util.*;

import android.view.*;

import android.widget.*;

import static org.hamcrest.core.Is.*;

import static org.hamcrest.core.IsNull.*;

import static org.hamcrest.core.IsInstanceOf.instanceOf;

import static org.junit.Assert.*;

import static com.mycompany.R.id.*;

public class LoginTests extends InstrumentationTestCase {

@MediumTest

public void testAValidUserCanLogIn() {

Instrumentation instrumentation = getInstrumentation();

// Register we are interested in the authentication activiry...

Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(AuthenticateActivity.class.getName(), null, false);

// Start the authentication activity as the first activity...

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.setClassName(instrumentation.getTargetContext(), AuthenticateActivity.class.getName());

instrumentation.startActivitySync(intent);

// Wait for it to start...

Activity currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);

assertThat(currentActivity, is(notNullValue()));

// Type into the username field...

View currentView = currentActivity.findViewById(username_field);

assertThat(currentView, is(notNullValue()));

assertThat(currentView, instanceOf(EditText.class));

TouchUtils.clickView(this, currentView);

instrumentation.sendStringSync("MyUsername");

// Type into the password field...

currentView = currentActivity.findViewById(password_field);

assertThat(currentView, is(notNullValue()));

assertThat(currentView, instanceOf(EditText.class));

TouchUtils.clickView(this, currentView);

instrumentation.sendStringSync("MyPassword");

// Register we are interested in the welcome activity...

// this has to be done before we do something that will send us to that

// activity...

instrumentation.removeMonitor(monitor);

monitor = instrumentation.addMonitor(WelcomeActivity.class.getName(), null, false);

// Click the login button...

currentView = currentActivity.findViewById(login_button;

assertThat(currentView, is(notNullValue()));

assertThat(currentView, instanceOf(Button.class));

TouchUtils.clickView(this, currentView);

// Wait for the welcome page to start...

currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);

assertThat(currentActivity, is(notNullValue()));

// Make sure we are logged in...

currentView = currentActivity.findViewById(welcome_message);

assertThat(currentView, is(notNullValue()));

assertThat(currentView, instanceOf(TextView.class));

assertThat(((TextView)currentView).getText().toString(), is("Welcome, MyUsername!"));

}

}

uiautomator

简单看一下uiautomator,首先,instrumentation 用来做UI测试受制于android的安全策略,本质上是一个APP测试,需要和被测APP在相同签名下,相同的进程下,才能互相信任,获取控件状态。因此,如果不知道源码,不要对APP重签名;另外,instrumentaion比较底层,封装程度不高,因此不适合直接拿来测试。从android API LEVEL 16开始,android引入了新的测试框架uiautomator,它对UI测试做了高度封装,同时不依赖于instrumentation,不需要做签名校验,可直接通过系统调用,获取被测应用控件。非常方便。以后详细介绍。

来自微信公众帐号:myersguo

参考资料

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android instrumentation 原理,理解 android instrumentation 的相关文章

随机推荐

  • Tensorflow 下测试gpu是否可用

    import tensorflow as tf tf test is gpu available
  • servlet和java区别_java – servlet和Web服务之间的区别

    Web服务是一种使用REST编程范例或SOAP协议为其客户端提供服务方法以进行通信的服务 有几种方法来实现Web服务 编写Web服务的最简单的方法是编写一个类并使用javax jws中的 WebService和 WebMethod注释进行注
  • php 导出excel的三种方法

    1 PHPExcel thinkphp PHPExcel放到这个目录下 导出excel表格 function exportExcel data filename xlsTitle iconv utf 8 gb2312 filename 文件
  • BI工程师日记

    从17年十月份入职BI工程师半年多的时间里 已熟练使用Tableau及SQL 越来越感觉到tableau的天花板 思考了好久如何打破天花板 想从几个方面入手 1 学习Python数据处理 神经网络庫的使用 结合tableau和Python
  • kubeadm 安装集群 1.16.12

    kubeadm 安装集群 添加源 mirror https mirrors aliyun com mirror https mirrors ustc edu cn docker 源 curl fsSL mirror docker ce li
  • 【喊7的次数重排】

    题目描述 喊7的次数重排 喊7是一个传统的聚会游戏 N个人围成一圈 按顺时针从1到N编号 编号为1的人从1开始喊数 下一个人喊的数字为上一个人的数字加1 但是当将要喊出来的数字是7的倍数或者数字本身含有7的话 不能把这个数字直接喊出来 而是
  • 对串级PID控温算法的解析

    目录 前言 单级PID 串级PID 系统分析 算法分析 总结 前言 笔者在做项目的过程中 需要对一个目标物体做精确控温 精度要求 1 需要在两分钟内使用电阻发热贴将温度由20控制到41 且无超调 根据控制原理 做精确控温应该使用PID算法
  • 026-从零搭建微服务-文件服务(二)

    写在最前 如果这个项目让你有所收获 记得 Star 关注哦 这对我是非常不错的鼓励与支持 源码地址 后端 https gitee com csps mingyue 源码地址 前端 https gitee com csps mingyue u
  • 【点云】PointCNN: Convolution On X-Transformed Points

    目录 测试 1 摘要 2 介绍 3 PointCNN 3 1 分层卷积 3 2 X Conv操作 3 3 PointCNN结构 4 参考 4 1 ELU激活函数 4 2 BatchNorm 测试 在Semantic3D上跑了PointCNN
  • jitter概念理解

    一般 PLL等时钟产生模块 都会有RMS jitter的描述 根据这个参数 可以计算出相关时钟的clock jitter 方便设置综合sdc的时钟约束 为什么只有RMS jitter 因为期望值就是理论周期值 根据期望值 RMS 均方差 就
  • 数据运营-计算留存率和转化率(漏斗分析&Python)

    一 案例数据 在数据运营中 留存率分析和转化率 漏斗 分析是经常用到的 本文结合具体案例总结了如何利用python求n日留存率以及各环节间转化率 指标释义 案例数据集介绍 本文是利用淘宝app的运营数据进行分析的 数据集中包含以下字段 部分
  • vue项目+el-tree,树结构展示,非常完整的代码,包含调接口拿真实数据渲染

    温故而知新 最近项目中频繁使用树形结构展示效果 因为不熟悉 备受折磨 代码贴出来 再复习一次 代码太长了 想看分析的可以搜我下一篇文章 下一篇会详细的写如何在项目中使用 效果 左侧是树 右侧是表格 点击任何一行都会展示对应的表格 完整代码如
  • Acwing 898. 数字三角形

    f i j 表示所有从起点开始 走到 i j 的路径中的和的最大值 自上而下到达 i j 有两种路径 一个是左上方 一个是正上方 f i j max f i 1 j 1 a i j f i 1 j a i j 注意边界情况 边界初始化为 I
  • 单机网游架设必学:Win7安装虚拟机详细步骤

    1 下载虚拟机软件 VMware VMware Workstation 中文正式版 10 0 1 137977 2 查了很多资料说虚拟机不会占用磁盘 但是我感觉应该是有影响的 所以这里我特意分出来一个空白的60G盘做测试用 分盘用 Acro
  • PDB文件详解

    PDB文件的介绍 PDB Program Data Base 意即程序的基本数据 是VS编译链接时生成的文件 DPB文件主要存储了VS调试程序时所需要的基本信息 主要包括源文件名 变量名 函数名 FPO 帧指针 对应的行号等等 因为存储的是
  • 强化学习算法 DQN 解决 CartPole 问题,代码逐条详解

    本文内容源自百度强化学习 7 日入门课程学习整理 感谢百度 PARL 团队李科浇老师的课程讲解 强化学习算法 DQN 解决 CartPole 问题 移动小车使得车上的摆杆保持直立 这个游戏环境可以说是强化学习中的 Hello World 大
  • Visual C++ ADO数据库编程入门(1)

    ADO 是目前在Windows环境中比较流行的客户端数据库编程技术 ADO是建立在OLE DB底层技术之上的高级编程接口 因而它兼具有强大的数据处理功能 处理各种不同类型的数据源 分布式的数据处理等等 和极其简单 易用的编程接口 因而得到了
  • 分布式系统详解--框架(Hadoop--RPC协议)

    分布式系统详解 框架 Hadoop RPC协议 在之前的一篇文章当中已经写过一篇文章是关于RPC通信协议的文章 是 分布式系统详解 基础知识 通信 是讲的关于网络通信的最基本的知识 这篇文章也简单讲一下关于hadoop内部的网络通信 一 h
  • 蓝桥杯第四届题目答案

    蓝桥杯第四届省赛javaB组题目答案 第一题 标题 世纪末的星期 曾有邪教称1999年12月31日是世界末日 当然该谣言已经不攻自破 还有人称今后的某个世纪末的12月31日 如果是星期一则会 有趣的是 任何一个世纪末的年份的12月31日都不
  • android instrumentation 原理,理解 android instrumentation

    instrumentation 先看activity的启动过程 Override public void startActivity Intent intent Nullable Bundle options if options null