CTS 组件配置

本文详细介绍了CTS框架的9个核心组件,包括cmd_options、device_requirements等,并展示了如何通过Configuration类进行配置。通过示例代码解释了配置过程,强调CTS只接受实现了特定接口的类配置,否则会报错。同时,通过Debug模式演示了配置加载的过程,从原始配置到CTS XML配置的替换机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cts框架分为9大部分:


cmd_options:命令行接受的参数选项,command包中。

device_requirements:设备相关要求,device包中

device_options:设备参数要求,device包中

builde_provider:版本提供者,build包中

target_preparer:预置条件准备,targetprep包中

test:测试类型,存在testtype包中

device_recovery:任务执行过程中设备异常后的设备恢复,device包中

logger:日志系统,log包中

result_reporter:结果统计报告,result包中


每一种任务都要配置好这9个组件,如果不配置,框架就采用自己的默认配置,但也是相当于配置了这几项,所以cts框架的核心在Configuration中。


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private static synchronized Map<String, ObjTypeInfo> getObjTypeMap() {  
  2.         if (sObjTypeMap == null) {  
  3.             sObjTypeMap = new HashMap<String, ObjTypeInfo>();  
  4.             sObjTypeMap.put(BUILD_PROVIDER_TYPE_NAME, new ObjTypeInfo(IBuildProvider.classfalse));  
  5.             sObjTypeMap.put(TARGET_PREPARER_TYPE_NAME, new ObjTypeInfo(ITargetPreparer.classtrue));  
  6.             sObjTypeMap.put(TEST_TYPE_NAME, new ObjTypeInfo(IRemoteTest.classtrue));  
  7.             sObjTypeMap.put(DEVICE_RECOVERY_TYPE_NAME, new ObjTypeInfo(IDeviceRecovery.classfalse));  
  8.             sObjTypeMap.put(LOGGER_TYPE_NAME, new ObjTypeInfo(ILeveledLogOutput.classfalse));  
  9.             sObjTypeMap.put(RESULT_REPORTER_TYPE_NAME, new ObjTypeInfo(ITestInvocationListener.class,  
  10.                     true));  
  11.             sObjTypeMap.put(CMD_OPTIONS_TYPE_NAME, new ObjTypeInfo(ICommandOptions.class,  
  12.                     false));  
  13.             sObjTypeMap.put(DEVICE_REQUIREMENTS_TYPE_NAME, new ObjTypeInfo(IDeviceSelection.class,  
  14.                     false));  
  15.             sObjTypeMap.put(DEVICE_OPTIONS_TYPE_NAME, new ObjTypeInfo(TestDeviceOptions.class,  
  16.                     false));  
  17.         }  
  18.         return sObjTypeMap;  
  19.     }  
  20.   
  21.     /** 
  22.      * Creates an {@link Configuration} with default config objects. 
  23.      */  
  24.     public Configuration(String name, String description) {  
  25.         mName = name;  
  26.         mDescription = description;  
  27.         mConfigMap = new LinkedHashMap<String, List<Object>>();  
  28.         setCommandOptions(new CommandOptions());  
  29.         setDeviceRequirements(new DeviceSelectionOptions());  
  30.         setDeviceOptions(new TestDeviceOptions());  
  31.         setBuildProvider(new StubBuildProvider());  
  32.         setTargetPreparer(new StubTargetPreparer());  
  33.         setTest(new StubTest());  
  34.         setDeviceRecovery(new WaitDeviceRecovery());  
  35.         setLogOutput(new StdoutLogger());  
  36.         setTestInvocationListener(new TextResultReporter());  
  37.     }  

在getObjTypeMap()可以看出来cts框架为这9个组件定义的接口,只要你的类实现了这个接口,cts就可以通过反射机制找到你的类。Configuration类的构造方法中设置了这几个接口默认的实现类,如果你没有配置其他的替代类,cts会默认去加载这些实现类。这些类都是实现了上面方法中对应关系的接口,才能被设置成配置项的。




所有关于类的配置,cts只认你在Configuration类接口的实现类,你如果写了一个类没有继承9大类中的一个接口,你添加了,也会报错的。从上图可以看出,cts默认配置了上面几项,没有配置的采用默认的。这个讲完了,我们就从文章开头的代码开始将它是如何一步一步完成这些配置的。


Debug


上一篇文章中的解析配置文件代码开始:


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. IConfiguration config = getConfigFactory().createConfigurationFromArgs(args);  




调用的是ConfigurationFactory.createConfigurationFromArgs方法:


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * {@inheritDoc} 
  3.      */  
  4.     @Override  
  5.     public IConfiguration createConfigurationFromArgs(String[] arrayArgs)  
  6.             throws ConfigurationException {  
  7.         List<String> listArgs = new ArrayList<String>(arrayArgs.length);  
  8.         IConfiguration config = internalCreateConfigurationFromArgs(arrayArgs, listArgs);  
  9.         config.setOptionsFromCommandLineArgs(listArgs);  
  10.   
  11.         return config;  
  12.     }  

该方法又调用了internalCreateConfigurationFromArgs方法,传入的是参数run cts --plan Signature字符串数组和一个空字符串。


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private IConfiguration internalCreateConfigurationFromArgs(String[] arrayArgs,  
  2.            List<String> optionArgsRef) throws ConfigurationException {  
  3.        if (arrayArgs.length == 0) {  
  4.            throw new ConfigurationException("Configuration to run was not specified");  
  5.        }  
  6.        optionArgsRef.addAll(Arrays.asList(arrayArgs));  
  7.        // first arg is config name  
  8.        final String configName = optionArgsRef.remove(0);  
  9.        ConfigurationDef configDef = getConfigurationDef(configName, false);  
  10.        return configDef.createConfiguration();  
  11.    }  

该方法中取出第一个参数cts然后传入getConfigurationDef方法中,获取ConfigurationDef对象。首先看看ConfigurationDef对象是什么?很简单,一个ConfigurationDef代表一个配置文件。



[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class ConfigurationDef {  
  2.   
  3.     /** a map of object type names to config object class name(s). */  
  4.     private final Map<String, List<String>> mObjectClassMap;  
  5.     /** a list of option name/value pairs. */  
  6.     private final List<OptionDef> mOptionList;  
  7.     /** a cache of the frequency of every classname */  
  8.     private final Map<String, Integer> mClassFrequency;  
  9.   
  10.     static class OptionDef {  
  11.         final String name;  
  12.         final String key;  
  13.         final String value;  
  14.   
  15.         OptionDef(String optionName, String optionValue) {  
  16.             this(optionName, null, optionValue);  
  17.         }  
  18.   
  19.         OptionDef(String optionName, String optionKey, String optionValue) {  
  20.             this.name = optionName;  
  21.             this.key = optionKey;  
  22.             this.value = optionValue;  
  23.         }  
  24.     }  
  25.   
  26.     /** the unique name of the configuration definition */  
  27.     private final String mName;  
  28.   
  29.     /** a short description of the configuration definition */  
  30.     private String mDescription = "";  
  31.   
  32.     public ConfigurationDef(String name) {  
  33.         mName = name;  
  34.         // use LinkedHashMap to keep objects in same order they were added.  
  35.         mObjectClassMap = new LinkedHashMap<String, List<String>>();  
  36.         mOptionList = new ArrayList<OptionDef>();  
  37.         mClassFrequency = new HashMap<String, Integer>();  
  38.     }  

主要关注下面2个属性:


mObjectClassMap:保存9大组件的,例如上面的build_provider、device_recovery、test、logger、result_reporter这些标签对应的类都保持该map对象中。

mOptionList:保存option标签的值,例如上面的enable-root的值。


现在进入debug模式验证一下是不是用上面两个属性保存的。将断点打在ConfigurationDef configDef = getConfigurationDef(configName, false);上,删除其他断点。重启debug。按F6跳到下一行,来看一下Variables一栏中值:




可以看到mObjectClassMap的值为:


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. {build_provider=[com.android.cts.tradefed.build.CtsBuildProvider], device_recovery=[com.android.tradefed.device.WaitDeviceRecovery],   
  2. test=[com.android.cts.tradefed.testtype.CtsTest], logger=[com.android.tradefed.log.FileLogger],  
  3. result_reporter=[com.android.cts.tradefed.result.CtsXmlResultReporter, com.android.cts.tradefed.result.IssueReporter]}  


和配置文件里的是一样的,mOptionList也保存了xml中option对应的值:




然后通过configDef.createConfiguration();来创建Configuration对象。所以我们进入该方法它是如何将原生的替换为cts.xml中配置的:




将debug点跳转到for循环中,然后在Variables一栏中分别查看this中的mObjectClassMap和config中mConfigMap的值:




可以看到mConfigMap是原生的,也就是Configuration的构造方法中添加的接口类,mObjectClassMap保存就是cts.xml配置文件里的接口类。然后根据Map的key值不能重复的特性用cts.xml配置的接口类替换掉原生的接口类。所以把断点打在返回语句return上,按F8看结果:




这个时候可以看到mConfigMap的值变了:


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. {cmd_options=[com.android.tradefed.command.CommandOptions@7b2390],   
  2. device_requirements=[com.android.tradefed.device.DeviceSelectionOptions@5bd18b],   
  3. device_options=[com.android.tradefed.device.TestDeviceOptions@1a29fe],   
  4. target_preparer=[com.android.tradefed.targetprep.StubTargetPreparer@1beaa92],   
  5. build_provider=[com.android.cts.tradefed.build.CtsBuildProvider@c4f317],   
  6. device_recovery=[com.android.tradefed.device.WaitDeviceRecovery@33967b],   
  7. test=[com.android.cts.tradefed.testtype.CtsTest@7263d0],   
  8. logger=[com.android.tradefed.log.FileLogger@1a7422e],   
  9. result_reporter=[com.android.cts.tradefed.result.CtsXmlResultReporter@1750ae1, com.android.cts.tradefed.result.IssueReporter@b279f3]}  

9大项全部配置完成。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值