use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class BundleConfigValidatorTest method masterResources_undefinedResourceId_throws.
@Test
public void masterResources_undefinedResourceId_throws() throws Exception {
ResourceTable resourceTable = new ResourceTableBuilder().addPackage("com.test.app").addStringResource("label", "Hello World").build();
int nonExistentResourceId = ResourcesUtils.entries(resourceTable).findFirst().get().getResourceId().getFullResourceId() + 1;
AppBundle appBundle = createAppBundleBuilder(BundleConfigBuilder.create().addResourcePinnedToMasterSplit(nonExistentResourceId)).addModule("feature", module -> module.setResourceTable(resourceTable).setManifest(androidManifest("com.test.app"))).build();
InvalidBundleException e = assertThrows(InvalidBundleException.class, () -> new BundleConfigValidator().validateBundle(appBundle));
assertThat(e).hasMessageThat().contains("The Master Resources list contains resource IDs not defined in any module. " + "For example: 0x7f010001");
}
use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class LanguageResourcesSplitterTest method resourcesPinnedToMasterSplit_mixedWithDefaultStrings.
@Test
public void resourcesPinnedToMasterSplit_mixedWithDefaultStrings() throws Exception {
ResourceTable resourceTable = new ResourceTableBuilder().addPackage("com.test.app").addStringResource("default_label", "label").addStringResourceForMultipleLocales("pinned_label", ImmutableMap.of("en", "yes")).addStringResourceForMultipleLocales("split_label", ImmutableMap.of("en", "no")).build();
BundleModule module = new BundleModuleBuilder("testModule").setResourceTable(resourceTable).setManifest(androidManifest("com.test.app")).build();
ModuleSplit baseSplit = ModuleSplit.forResources(module);
LanguageResourcesSplitter languageSplitter = new LanguageResourcesSplitter(// pinned_label
resource -> resource.getResourceId().getFullResourceId() == 0x7f010001);
Collection<ModuleSplit> languageSplits = languageSplitter.split(baseSplit);
assertThat(languageSplits).hasSize(2);
ModuleSplit masterSplit = languageSplits.stream().filter(split -> split.isMasterSplit()).collect(onlyElement());
assertThat(masterSplit.getResourceTable()).hasValue(new ResourceTableBuilder().addPackage("com.test.app").addStringResource("default_label", "label").addStringResourceForMultipleLocales("pinned_label", ImmutableMap.of("en", "yes")).build());
ModuleSplit configSplit = languageSplits.stream().filter(split -> !split.isMasterSplit()).collect(onlyElement());
assertThat(configSplit.getApkTargeting()).isEqualTo(apkLanguageTargeting("en"));
assertThat(configSplit.getResourceTable()).hasValue(getStringResourceTable("split_label", 0x02, ImmutableList.of(value("no", locale("en")))));
}
use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class LanguageResourcesSplitterTest method resourcesPinnedToMasterSplit_noSplitting_fileReferences.
@Test
public void resourcesPinnedToMasterSplit_noSplitting_fileReferences() throws Exception {
ResourceTable resourceTable = new ResourceTableBuilder().addPackage("com.test.app").addResource("drawable", "image", fileReference("res/drawable-en-hdpi/image.jpg", mergeConfigs(locale("en"), HDPI)), fileReference("res/drawable-fr-hdpi/image.jpg", mergeConfigs(locale("fr"), HDPI))).addResource("drawable", "image2", fileReference("res/drawable-en-hdpi/image2.jpg", mergeConfigs(locale("en"), HDPI)), fileReference("res/drawable-fr-hdpi/image2.jpg", mergeConfigs(locale("fr"), HDPI))).build();
BundleModule module = new BundleModuleBuilder("testModule").setResourceTable(resourceTable).setManifest(androidManifest("com.test.app")).addFile("res/drawable-en-hdpi/image.jpg").addFile("res/drawable-fr-hdpi/image.jpg").addFile("res/drawable-en-hdpi/image2.jpg").addFile("res/drawable-fr-hdpi/image2.jpg").build();
ModuleSplit baseSplit = ModuleSplit.forResources(module);
LanguageResourcesSplitter languageSplitter = new LanguageResourcesSplitter(resource -> resource.getResourceId().getFullResourceId() == 0x7f010000);
Collection<ModuleSplit> languageSplits = languageSplitter.split(baseSplit);
ModuleSplit masterSplit = languageSplits.stream().filter(split -> split.isMasterSplit()).collect(onlyElement());
assertThat(masterSplit.getResourceTable()).hasValue(new ResourceTableBuilder().addPackage("com.test.app").addResource("drawable", "image", fileReference("res/drawable-en-hdpi/image.jpg", mergeConfigs(locale("en"), HDPI)), fileReference("res/drawable-fr-hdpi/image.jpg", mergeConfigs(locale("fr"), HDPI))).build());
assertThat(extractPaths(masterSplit.getEntries())).containsExactly("res/drawable-en-hdpi/image.jpg", "res/drawable-fr-hdpi/image.jpg");
ImmutableList<ModuleSplit> configSplits = languageSplits.stream().filter(split -> !split.isMasterSplit()).collect(toImmutableList());
ImmutableMap<ApkTargeting, ModuleSplit> configSplitMap = Maps.uniqueIndex(configSplits, ModuleSplit::getApkTargeting);
assertThat(configSplitMap.keySet()).containsExactly(apkLanguageTargeting("en"), apkLanguageTargeting("fr"));
ModuleSplit enSplit = configSplitMap.get(apkLanguageTargeting("en"));
assertThat(enSplit.getResourceTable()).hasValue(ResourcesTableFactory.createResourceTable(/* entryId= */
0x01, "image2", fileReference("res/drawable-en-hdpi/image2.jpg", mergeConfigs(locale("en"), HDPI))));
assertThat(extractPaths(enSplit.getEntries())).containsExactly("res/drawable-en-hdpi/image2.jpg");
ModuleSplit frSplit = configSplitMap.get(apkLanguageTargeting("fr"));
assertThat(frSplit.getResourceTable()).hasValue(ResourcesTableFactory.createResourceTable(/* entryId= */
0x01, "image2", fileReference("res/drawable-fr-hdpi/image2.jpg", mergeConfigs(locale("fr"), HDPI))));
assertThat(extractPaths(frSplit.getEntries())).containsExactly("res/drawable-fr-hdpi/image2.jpg");
}
use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class ScreenDensityResourcesSplitterTest method lowestDensityConfigsPinnedToMaster_mixedConfigsInSameDensityBucket.
@Test
public void lowestDensityConfigsPinnedToMaster_mixedConfigsInSameDensityBucket() throws Exception {
BundleModule testModule = new BundleModuleBuilder("testModule").addFile("res/drawable-ldpi/image.jpg").addFile("res/drawable-ldpi-v21/image.jpg").addFile("res/drawable-ldpi-v24/image.jpg").addFile("res/drawable-xxhdpi/image.jpg").addFile("res/drawable-xxhdpi-v21/image.jpg").addFile("res/drawable-xxhdpi-v24/image.jpg").setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addFileResourceForMultipleConfigs("drawable", "image", ImmutableMap.<Configuration, String>builder().put(mergeConfigs(LDPI), "res/drawable-ldpi/image.jpg").put(mergeConfigs(LDPI, sdk(21)), "res/drawable-ldpi-v21/image.jpg").put(mergeConfigs(LDPI, sdk(24)), "res/drawable-ldpi-v24/image.jpg").put(mergeConfigs(XXXHDPI), "res/drawable-xxhdpi/image.jpg").put(mergeConfigs(XXHDPI, sdk(21)), "res/drawable-xxhdpi-v21/image.jpg").put(mergeConfigs(XXHDPI, sdk(24)), "res/drawable-xxhdpi-v24/image.jpg").build()).build()).setManifest(androidManifest("com.test.app")).build();
// 0x7f010000 is the "drawable/image" resource.
Predicate<ResourceId> pinnedLowDensityResourcesPredicate = resourceId -> resourceId.getFullResourceId() == 0x7f010000;
ScreenDensityResourcesSplitter splitter = new ScreenDensityResourcesSplitter(BundleToolVersion.getCurrentVersion(), NO_RESOURCES_PINNED_TO_MASTER, pinnedLowDensityResourcesPredicate, /* pinLowestBucketOfStylesToMaster= */
false);
ImmutableCollection<ModuleSplit> densitySplits = splitter.split(ModuleSplit.forResources(testModule));
ModuleSplit masterSplit = densitySplits.stream().filter(split -> split.isMasterSplit()).collect(onlyElement());
assertThat(extractPaths(masterSplit.getEntries())).containsExactly("res/drawable-ldpi/image.jpg", "res/drawable-ldpi-v21/image.jpg", "res/drawable-ldpi-v24/image.jpg");
ImmutableList<ModuleSplit> configSplits = densitySplits.stream().filter(split -> !split.isMasterSplit()).collect(toImmutableList());
assertThat(configSplits).isNotEmpty();
for (ModuleSplit configSplit : configSplits) {
assertThat(extractPaths(configSplit.getEntries())).containsNoneOf("res/drawable-ldpi/image.jpg", "res/drawable-ldpi-v21/image.jpg", "res/drawable-ldpi-v24/image.jpg");
}
}
use of com.android.tools.build.bundletool.testing.ResourceTableBuilder in project bundletool by google.
the class ScreenDensityResourcesSplitterTest method lowestDensityStylesPinnedToMaster_disabled.
@Test
public void lowestDensityStylesPinnedToMaster_disabled() throws Exception {
BundleModule testModule = new BundleModuleBuilder("testModule").setResourceTable(new ResourceTableBuilder().addPackage("com.test.app").addResource("style", "title_text_size", configValueWithDensity("320dens", Optional.of(XHDPI)), configValueWithDensity("default", Optional.empty())).build()).setManifest(androidManifest("com.test.app")).build();
ScreenDensityResourcesSplitter splitter = new ScreenDensityResourcesSplitter(BundleToolVersion.getCurrentVersion(), NO_RESOURCES_PINNED_TO_MASTER, NO_LOW_DENSITY_CONFIG_PINNED_TO_MASTER, /* pinLowestBucketOfStylesToMaster= */
false);
ImmutableCollection<ModuleSplit> splits = splitter.split(ModuleSplit.forResources(testModule));
ModuleSplit masterSplit = splits.stream().filter(ModuleSplit::isMasterSplit).collect(onlyElement());
assertThat(extractStyleValues(masterSplit, "title_text_size")).isEmpty();
ModuleSplit xhdpiSplit = extractDensityTargetingModule(splits, DensityAlias.XXHDPI).get();
assertThat(extractStyleValues(xhdpiSplit, "title_text_size")).containsExactly("320dens");
ModuleSplit mdpiSplit = extractDensityTargetingModule(splits, DensityAlias.MDPI).get();
assertThat(extractStyleValues(mdpiSplit, "title_text_size")).containsExactly("default");
}
Aggregations