Ensure that loading packages can be extended

Collections.emptySet() creates an immutable collection which will prevent
us to add more packages to the loading packages set.

Instead of using the emptySet, create a new set everytime, even if it's
empty.

Test: PackageDexUseTest
Bug: 148774920
Change-Id: If274d0380d494d31768da24c10153810ef8bf513
This commit is contained in:
Calin Juravle
2020-03-31 19:51:34 -07:00
parent 7c8fd29787
commit c2deeefef2
2 changed files with 19 additions and 6 deletions

View File

@@ -446,15 +446,12 @@ public class PackageDexUsage extends AbstractStatsBase<Void> {
if (line == null) {
throw new IllegalStateException("Could not find the loadingPackages line.");
}
// We expect that most of the times the list of loading packages will be empty.
if (line.length() == LOADING_PACKAGE_CHAR.length()) {
return Collections.emptySet();
} else {
Set<String> result = new HashSet<>();
Set<String> result = new HashSet<>();
if (line.length() != LOADING_PACKAGE_CHAR.length()) {
Collections.addAll(result,
line.substring(LOADING_PACKAGE_CHAR.length()).split(SPLIT_CHAR));
return result;
}
return result;
}
/**

View File

@@ -575,6 +575,22 @@ public class PackageDexUsageTests {
assertPackageDexUsage(mBarBaseUser0);
}
@Test
public void testEnsureLoadingPackagesCanBeExtended() {
String isa = VMRuntime.getInstructionSet(Build.SUPPORTED_ABIS[0]);
String content = "PACKAGE_MANAGER__PACKAGE_DEX_USAGE__2\n"
+ "com.google.foo\n"
+ "+/data/app/com.google.foo/split-2.apk\n"
+ "@\n";
PackageDexUsage packageDexUsage = new PackageDexUsage();
try {
packageDexUsage.read(new StringReader(content));
} catch (IOException e) {
fail();
}
record(packageDexUsage, mFooSplit2UsedByOtherApps0, mFooSplit2UsedByOtherApps0.getUsedBy());
}
private void assertPackageDexUsage(TestData primary, TestData... secondaries) {
assertPackageDexUsage(mPackageDexUsage, null, primary, secondaries);
}