本文主要是介绍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 详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!