Android应用开发之android自动化测试CTS源码分析
凌雪 2018-10-24 来源 :网络 阅读 773 评论 0

摘要:本文将带你了解Android应用开发之android自动化测试CTS源码分析,希望本文对大家学Android有所帮助。

本文将带你了解Android应用开发之android自动化测试CTS源码分析,希望本文对大家学Android有所帮助。


android自动化测试CTS源码分析
1, 概述
CTS(Compatibility Test   Suite)全名兼容性测试,主要目的就是让Android设备开发商能够开发出兼容性更好的android设备。
本文的CTS源码基于android   5.1,和其他自动化测试指令一样,CTS测试命令也是通过脚本运行的。
CTS源码中的脚本路径: CTS/tools/tradefed-host/etc/   cts-tradefed
Linux环境中也是运行cts-tradefed脚本进行测试。
cts-tradefed脚本最后一行如下,
[html]view plaincopy
java$RDBG_FLAG\
-cp${JAR_PATH}-DCTS_ROOT=${CTS_ROOT}com.android.cts.tradefed.command.CtsConsole"$@"
由此可见,android系统中测试的入口为CtsConsole.java类的main方法。
2,main方法
main方法的流程图如下,
   
   
    CtsConsole的main方法如下,
[html]view plaincopy
publicstaticvoidmain(String[]args)throwsInterruptedException,ConfigurationException{
Consoleconsole=newCtsConsole();//构造CtsConsole对象
Console.startConsole(console,args);//调用父类的startConsole方法
}
CtsConsole继承于Console,而Console继承于Thread类。
startConsole方法如下,
[html]view plaincopy
publicstaticvoidstartConsole(Consoleconsole,String[]args)
throwsInterruptedException,ConfigurationException
{
//将命令参数复制到ArrayList中
ListnonGlobalArgs=GlobalConfiguration.createGlobalConfiguration(args);
console.setArgs(nonGlobalArgs);//将命令参数保存在变量mMainArgs中
console.setCommandScheduler(GlobalConfiguration.getInstance().getCommandScheduler());
console.setDaemon(true);
console.start();//启动线程调用run方法
console.awaitScheduler();
}
}
CtsConsole的run方法如下
[html]view plaincopy
@Override
publicvoidrun(){
printLine(String.format("AndroidCTS%s",CtsBuildProvider.CTS_BUILD_VERSION));
super.run();
}
首先看看打印信息的printLine方法,在父类Console中实现。
[html]view plaincopy
protectedvoidprintLine(Stringoutput)
{
System.out.print(output);
System.out.println();
}
Console的run方法会做一些检查,最后会调用executeCmdRunnable方法。然后回调内部类ArgRunnable的run方法。
2.1 任务添加
Console的run方法会还会调用CommandScheduler的start方法启动线程。
[html]view plaincopy
this.mScheduler.start();
this.mScheduler.await();
ArgRunnable的run方法会调用CommandScheduler的addCommand方法添加任务。
addCommand调用流程图如下
   
   
    internalAddCommand方法首先调用ConfigurationFactory的createConfigurationFromArgs方法解析配置文件,
然后根据配置文件的信息调用addExecCommandToQueue将执行计划放入到队列中。
[html]view plaincopy
privatebooleaninternalAddCommand(String[]args,longtotalExecTime,StringcmdFilePath)
throwsConfigurationException
{
assertStarted();
IConfigurationconfig=getConfigFactory().createConfigurationFromArgs(args);
config.validateOptions();
if(config.getCommandOptions().runOnAllDevices())
{
addCommandForAllDevices(totalExecTime,args,cmdFilePath);
}
else
{
CommandTrackercmdTracker=createCommandTracker(args,cmdFilePath);
cmdTracker.incrementExecTime(totalExecTime);
ExecutableCommandcmdInstance=createExecutableCommand(cmdTracker,config,false);
addExecCommandToQueue(cmdInstance,0L);
}
returntrue;
}
returnfalse;
}
addExecCommandToQueue方法如下,
[html]view plaincopy
privatesynchronizedbooleanaddExecCommandToQueue(finalExecutableCommandcmd,longdelayTime)
{
if(isShutdown()){
returnfalse;
}
if(delayTime>0L)
{
this.mSleepingCommands.add(cmd);
RunnabledelayCommand=newRunnable()
{
publicvoidrun()
{
synchronized(CommandScheduler.this)
{
if(CommandScheduler.this.mSleepingCommands.remove(cmd))
{
CommandScheduler.this.mReadyCommands.add(cmd);
CommandScheduler.this.mCommandProcessWait.signalEventReceived();
}
}
}
};
this.mCommandTimer.schedule(delayCommand,delayTime,TimeUnit.MILLISECONDS);
}
else
{
this.mReadyCommands.add(cmd);
this.mCommandProcessWait.signalEventReceived();
}
returntrue;
}
另外开一个线程利用回调添加任务,添加到mReadyCommands中
[html]view plaincopy
privateListmReadyCommands;
2.2 组件配置
internalCreateConfigurationFromArgs方法如下,
[html]view plaincopy
privateIConfigurationinternalCreateConfigurationFromArgs(String[]arrayArgs,ListoptionArgsRef)
throwsConfigurationException
{
if(arrayArgs.length==0){
thrownewConfigurationException("Configurationtorunwasnotspecified");
}
optionArgsRef.addAll(Arrays.asList(arrayArgs));
StringconfigName=(String)optionArgsRef.remove(0);
ConfigurationDefconfigDef=getConfigurationDef(configName,false);
returnconfigDef.createConfiguration();
}
该方法中取出第一个参数cts然后传入getConfigurationDef方法中,获取ConfigurationDef对象。
ConfigurationDef根据测试指令配置相关属性,然后保存在变量中
[html]view plaincopy
privatefinalMap>mObjectClassMap;
privatefinalListmOptionList;
privatefinalMapmClassFrequency;
privatefinalStringmName;
属性值的定义在Configuration中。
这样属性也配置好了,如果使用脚本执行CTS测试,每个CTS测试任务都会放入队列中,然后取出依次执行,直到所有执行完为止。
android自动化测试CTS源码分析之一
2017年04月15日 22:21:58阅读数:1409
1, 概述
CTS(Compatibility Test   Suite)全名兼容性测试,主要目的就是让Android设备开发商能够开发出兼容性更好的android设备。
本文的CTS源码基于android   5.1,和其他自动化测试指令一样,CTS测试命令也是通过脚本运行的。
CTS源码中的脚本路径: CTS/tools/tradefed-host/etc/   cts-tradefed
Linux环境中也是运行cts-tradefed脚本进行测试。
cts-tradefed脚本最后一行如下,
[html]view plaincopy
java$RDBG_FLAG\
-cp${JAR_PATH}-DCTS_ROOT=${CTS_ROOT}com.android.cts.tradefed.command.CtsConsole"$@"
由此可见,android系统中测试的入口为CtsConsole.java类的main方法。
2,main方法
main方法的流程图如下,
   
   
    CtsConsole的main方法如下,
[html]view plaincopy
publicstaticvoidmain(String[]args)throwsInterruptedException,ConfigurationException{
Consoleconsole=newCtsConsole();//构造CtsConsole对象
Console.startConsole(console,args);//调用父类的startConsole方法
}
CtsConsole继承于Console,而Console继承于Thread类。
startConsole方法如下,
[html]view plaincopy
publicstaticvoidstartConsole(Consoleconsole,String[]args)
throwsInterruptedException,ConfigurationException
{
//将命令参数复制到ArrayList中
ListnonGlobalArgs=GlobalConfiguration.createGlobalConfiguration(args);
console.setArgs(nonGlobalArgs);//将命令参数保存在变量mMainArgs中
console.setCommandScheduler(GlobalConfiguration.getInstance().getCommandScheduler());
console.setDaemon(true);
console.start();//启动线程调用run方法
console.awaitScheduler();
}
}
CtsConsole的run方法如下
[html]view plaincopy
@Override
publicvoidrun(){
printLine(String.format("AndroidCTS%s",CtsBuildProvider.CTS_BUILD_VERSION));
super.run();
}
首先看看打印信息的printLine方法,在父类Console中实现。
[html]view plaincopy
protectedvoidprintLine(Stringoutput)
{
System.out.print(output);
System.out.println();
}
Console的run方法会做一些检查,最后会调用executeCmdRunnable方法。然后回调内部类ArgRunnable的run方法。
2.1 任务添加
Console的run方法会还会调用CommandScheduler的start方法启动线程。
[html]view plaincopy
this.mScheduler.start();
this.mScheduler.await();
ArgRunnable的run方法会调用CommandScheduler的addCommand方法添加任务。
addCommand调用流程图如下
   
   
    internalAddCommand方法首先调用ConfigurationFactory的createConfigurationFromArgs方法解析配置文件,
然后根据配置文件的信息调用addExecCommandToQueue将执行计划放入到队列中。
[html]view plaincopy
privatebooleaninternalAddCommand(String[]args,longtotalExecTime,StringcmdFilePath)
throwsConfigurationException
{
assertStarted();
IConfigurationconfig=getConfigFactory().createConfigurationFromArgs(args);
config.validateOptions();
if(config.getCommandOptions().runOnAllDevices())
{
addCommandForAllDevices(totalExecTime,args,cmdFilePath);
}
else
{
CommandTrackercmdTracker=createCommandTracker(args,cmdFilePath);
cmdTracker.incrementExecTime(totalExecTime);
ExecutableCommandcmdInstance=createExecutableCommand(cmdTracker,config,false);
addExecCommandToQueue(cmdInstance,0L);
}
returntrue;
}
returnfalse;
}
addExecCommandToQueue方法如下,
[html]view plaincopy
privatesynchronizedbooleanaddExecCommandToQueue(finalExecutableCommandcmd,longdelayTime)
{
if(isShutdown()){
returnfalse;
}
if(delayTime>0L)
{
this.mSleepingCommands.add(cmd);
RunnabledelayCommand=newRunnable()
{
publicvoidrun()
{
synchronized(CommandScheduler.this)
{
if(CommandScheduler.this.mSleepingCommands.remove(cmd))
{
CommandScheduler.this.mReadyCommands.add(cmd);
CommandScheduler.this.mCommandProcessWait.signalEventReceived();
}
}
}
};
this.mCommandTimer.schedule(delayCommand,delayTime,TimeUnit.MILLISECONDS);
}
else
{
this.mReadyCommands.add(cmd);
this.mCommandProcessWait.signalEventReceived();
}
returntrue;
}
另外开一个线程利用回调添加任务,添加到mReadyCommands中
[html]view plaincopy
privateListmReadyCommands;
2.2 组件配置
internalCreateConfigurationFromArgs方法如下,
[html]view plaincopy
privateIConfigurationinternalCreateConfigurationFromArgs(String[]arrayArgs,ListoptionArgsRef)
throwsConfigurationException
{
if(arrayArgs.length==0){
thrownewConfigurationException("Configurationtorunwasnotspecified");
}
optionArgsRef.addAll(Arrays.asList(arrayArgs));
StringconfigName=(String)optionArgsRef.remove(0);
ConfigurationDefconfigDef=getConfigurationDef(configName,false);
returnconfigDef.createConfiguration();
}
该方法中取出第一个参数cts然后传入getConfigurationDef方法中,获取ConfigurationDef对象。
ConfigurationDef根据测试指令配置相关属性,然后保存在变量中
[html]view plaincopy
privatefinalMap>mObjectClassMap;
privatefinalListmOptionList;
privatefinalMapmClassFrequency;
privatefinalStringmName;
属性值的定义在Configuration中。
这样属性也配置好了,如果使用脚本执行CTS测试,每个CTS测试任务都会放入队列中,然后取出依次执行,直到所有执行完为止。    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程