GTS 中 testNumberOfHeadedApplications fail 详解

2023-12-02 13:10

本文主要是介绍GTS 中 testNumberOfHeadedApplications fail 详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

来源:

https://blog.csdn.net/jingerppp/article/details/81569196

 

GTS 中 测试case armeabi-v7a GtsPlacementTestCases 的时候会出现下面的异常,本文总结一下。

com.google.android.placement.gts.PreloadHeadedAppsTest#testNumberOfHeadedApplications

对于第 1 个case,可以看 GTS 中testCoreGmsAppsPermissionsWhitelisted fail 详解,本文主要总结第 2 个case。

 

先来看下出现异常的 host log:

07-30 23:30:11 I/ModuleListener: [16/20] com.google.android.placement.gts.PreloadHeadedAppsTest#testNumberOfHeadedApplications fail:
java.lang.AssertionError: Number of total preloaded apps exceeded: actual 9 > max 7at org.junit.Assert.fail(Assert.java:88)at org.junit.Assert.assertTrue(Assert.java:41)at com.google.android.placement.gts.PreloadHeadedAppsTest.assertAppCount(PreloadHeadedAppsTest.java:330)at com.google.android.placement.gts.PreloadHeadedAppsTest.assertRulePasses(PreloadHeadedAppsTest.java:325)at com.google.android.placement.gts.PreloadHeadedAppsTest.testNumberOfHeadedApplications(PreloadHeadedAppsTest.java:123)at java.lang.reflect.Method.invoke(Native Method)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:52)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:148)at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:142)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at java.lang.Thread.run(Thread.java:764)

 

来看下code:

    @Testpublic void testNumberOfHeadedApplications() throws Exception {// 获取所有的launch appsSet<String> packageNames = getLaunchableApps();// 排除掉gms 的appsexemptWhitelistedGmsApps(packageNames);// 排除掉部分特殊categories 的appsexemptAppsByCategories(packageNames);// 排除特殊appsexemptAppsWithoutIntent(packageNames);Pair<Integer, Integer> numApps = countUserAndSystemApps(packageNames);assertRulePasses(calculatePreloadRule(), ((Integer) numApps.first).intValue(), ((Integer) numApps.second).intValue());}

如上面注释,首先会获取所有launch 的apps,也就是category 为 android.intent.category.LAUNCHER,详细看下面的code:

    private Set<String> getLaunchableApps() throws Exception {Intent intent = new Intent("android.intent.action.MAIN");intent.addCategory("android.intent.category.LAUNCHER");List<ResolveInfo> infos = this.mPackageManager.queryIntentActivities(intent, 0);Set<String> packageNames = new HashSet();for (ResolveInfo r : infos) {packageNames.add(r.activityInfo.packageName);}packageNames.addAll(getLauncherLikeApps());this.mReportLog.addValues(KEY_LAUNCHABLE_APPS, Arrays.asList((String[]) packageNames.toArray(new String[packageNames.size()])), ResultType.NEUTRAL, ResultUnit.NONE);String str = TAG;StringBuilder stringBuilder = new StringBuilder();stringBuilder.append("Launchable apps: ");stringBuilder.append(packageNames);Log.d(str, stringBuilder.toString());return packageNames;}

注意:

其中会有mReportLog 这个变量,后面会讲解到,目前先理解为会将一些log 信息存放到一个文件中,这里launch 的app,会存在在key 为launchable_apps 下面。

 

接着上面code,获取launch 的apps 之后,会将一些特殊的apps 从list 中remove 掉,这些包括几个gms apps、特殊的categories的apps、特殊apps 。最终的packageNames 会经过函数countUserAndSystemApps() 计算出user apps 和system apps,这个计算函数是这个case 的关键了,如果计算出错,那么很容易出现本文说的这个case fail 现象。来看下code:

    private Pair<Integer, Integer> countUserAndSystemApps(Set<String> packageNames) {List user = new ArrayList();List system = new ArrayList();for (String name : packageNames) {if (PackageUtil.isSystemApp(name)) {system.add(name);} else {user.add(name);}}this.mReportLog.addValues(USER_APPS_KEY, user, ResultType.NEUTRAL, ResultUnit.NONE);String str = TAG;StringBuilder stringBuilder = new StringBuilder();stringBuilder.append("User apps: ");stringBuilder.append(user);Log.d(str, stringBuilder.toString());this.mReportLog.addValues(SYSTEM_APPS_KEY, system, ResultType.NEUTRAL, ResultUnit.NONE);str = TAG;stringBuilder = new StringBuilder();stringBuilder.append("System apps: ");stringBuilder.append(system);Log.d(str, stringBuilder.toString());return new Pair(Integer.valueOf(user.size()), Integer.valueOf(system.size()));}

主要通过PackageUtil。isSystemApp() 来确认是否为system apps,并将这个log 保存在mReportLog 中,key 分别是user_apps 和system_apps。

 

接着看code,在计算完成后会将计算的结果给numApps:

Pair<Integer, Integer> numApps = countUserAndSystemApps(packageNames);

然后开始进入assert:

assertRulePasses(calculatePreloadRule(), ((Integer) numApps.first).intValue(), ((Integer) numApps.second).intValue());
    private void assertRulePasses(PreloadRule rule, int numUser, int numSystem) throws Exception {if (rule.mShouldCountSystem) {assertAppCount(APP_COUNT_EXCEED_MSG, "system", numSystem, rule.mNumSystem);}assertAppCount(APP_COUNT_EXCEED_MSG, "total", numUser + numSystem, rule.numTotal());}

这个assert 为false 就出现了最开始的log,要求的是numUser + numSystem 必须要 <= rule.numTotal()

而这里的rule 是通过上面的calculatePreloadRule() 得来的:

    private PreloadRule calculatePreloadRule() throws Exception {List<String> sizeLimits = this.mDcds.getValues(STORAGE_LIMIT_SIZES_KEY);List<String> maxUserApps = this.mDcds.getValues(MAX_ALLOWED_USER_APPS_KEY);List<String> maxSystemApps = this.mDcds.getValues(MAX_ALLOWED_SYSTEM_APPS_KEY);StorageStatsManager ssm = (StorageStatsManager) this.mContext.getSystemService(StorageStatsManager.class);Assert.assertNotNull("StorageStatsManager should not be null", ssm);long totalBytesOnVolume = ssm.getTotalBytes(StorageManager.UUID_DEFAULT);boolean shouldCountSystem = false;int i = 0;while (i < sizeLimits.size() && totalBytesOnVolume > new Long((String) sizeLimits.get(i)).longValue()) {i++;}if (i == sizeLimits.size()) {i--;shouldCountSystem = true;}this.mReportLog.addValue(KEY_SIZE_LIMIT, (String) sizeLimits.get(i), ResultType.NEUTRAL, ResultUnit.NONE);return new PreloadRule(Integer.valueOf((String) maxUserApps.get(i)).intValue(), Integer.valueOf((String) maxSystemApps.get(i)).intValue(), shouldCountSystem);}

这段code 大致就是说上面获取的user apps 和system apps 必须要跟GTS 的配置信息一致。配置信息如下:

    <entry key="max_allowed_user_apps"><value>0</value><value>0</value><value>7</value></entry><entry key="max_allowed_system_apps"><value>7</value><value>7</value><value>7</value></entry>

显然,system apps 要求是7个,而这个assert 也是true的(不然assertRulePasses()最开始的case 就会报错),结合log 可以判断出user apps 要求是 0 个,但是countUserAndSystemApps() 计算出来的却是 2 个。这多出来的 2 个就是该case 出现fail 的根本原因。

 

如何知道多出来的 2 个user apps 是什么呢?这就要看mReportLog 中存了什么了,来看下这个变量是什么:

    private DeviceReportLog mReportLog;
    public void setUp() throws Exception {this.mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();this.mPackageManager = this.mContext.getPackageManager();this.mDcds = new DynamicConfigDeviceSide("GtsPlacementTestCases");this.mReportLog = new DeviceReportLog("GtsPlacementTestCases", STREAM_NAME);}
    public DeviceReportLog(String reportLogName, String streamName) {this(reportLogName, streamName, new File(Environment.getExternalStorageDirectory(), "report-log-files"));}public DeviceReportLog(String reportLogName, String streamName, File logDirectory) {super(reportLogName, streamName);try {if (Environment.getExternalStorageState().equals("mounted")) {if (logDirectory.exists() || logDirectory.mkdirs()) {if (logDirectory.exists()) {if (logDirectory.isDirectory()) {}}StringBuilder stringBuilder = new StringBuilder();stringBuilder.append(this.mReportLogName);stringBuilder.append(".reportlog.json");this.store = new ReportLogDeviceInfoStore(new File(logDirectory, stringBuilder.toString()), this.mStreamName);this.store.open();return;}throw new IOException("Cannot create directory for device info files");}throw new IOException("External storage is not mounted");} catch (Exception e) {Log.e(TAG, "Could not create report log file.", e);}}

code 比较简单,最后就是保存在/sdcard/report-log-files/GtsPlacementTestCases.reportlog.json 中(最后的测试报告里面也应该会有这个文件),大概如下:

    "launchable_apps":["com.google.android.apps.messaging","com.google.android.gm.lite","com.alfacart.apps","com.google.android.apps.youtube.mango","id.meteor.alfamind","com.fajarsiddiq.snapshop","com.qiku.android.filebrowser","com.android.music","com.telkomsel.telkomselcm","com.qiku.android.xtime","com.qiku.android.contacts","com.alfamart.alfagift","com.finallyclean.booster.cleaner","com.caf.fmradio","com.google.android.apps.searchlite","com.android.vending","com.hola.weather","com.android.gallery3d","com.android.calculator2","com.android.chrome","com.android.video","com.google.android.apps.mapslite","com.android.camera","com.mhn.ponta","com.google.android.apps.assistant","com.android.settings","com.qiku.android.launcher3","com.android.soundrecorder","com.google.android.calendar"],"user_apps":["com.fajarsiddiq.snapshop","com.telkomsel.telkomselcm","com.alfamart.alfagift","com.mhn.ponta"],"system_apps":["com.alfacart.apps","id.meteor.alfamind","com.qiku.android.filebrowser","com.finallyclean.booster.cleaner","com.android.video"],"size_limit":"8000000000"}]}

或多或少是可以看出点东西,例如上面的log 文件就可以看出有几个user apps,这个是需要确认为何出现?什么时候安装?

 

结论:

通过测试报告中的GtsPlacementTestCases.reportlog.json 文件分析user_apps 和system_apps 是否与dynamic 文件中配置相符。

 

更多GTS 测试的case 见:

CTS/GTS 常见问题汇总

 

 

 

这篇关于GTS 中 testNumberOfHeadedApplications fail 详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/445468

相关文章

详解MySQL中DISTINCT去重的核心注意事项

《详解MySQL中DISTINCT去重的核心注意事项》为了实现查询不重复的数据,MySQL提供了DISTINCT关键字,它的主要作用就是对数据表中一个或多个字段重复的数据进行过滤,只返回其中的一条数据... 目录DISTINCT 六大注意事项1. 作用范围:所有 SELECT 字段2. NULL 值的特殊处

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可