use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class ModuleResourceRepositoryTest method testOverlays.
public void testOverlays() {
myFixture.copyFileToProject(LAYOUT, "res/layout/layout1.xml");
myFixture.copyFileToProject(LAYOUT_OVERLAY, "res2/layout/layout1.xml");
myFixture.copyFileToProject(LAYOUT_IDS_1, "res2/layout/layout_ids1.xml");
myFixture.copyFileToProject(LAYOUT_IDS_2, "res2/layout/layout_ids2.xml");
VirtualFile res1 = myFixture.copyFileToProject(VALUES, "res/values/values.xml").getParent().getParent();
VirtualFile res2 = myFixture.copyFileToProject(VALUES_OVERLAY1, "res2/values/values.xml").getParent().getParent();
VirtualFile res3 = myFixture.copyFileToProject(VALUES_OVERLAY2, "res3/values/nameDoesNotMatter.xml").getParent().getParent();
myFixture.copyFileToProject(VALUES_OVERLAY2_NO, "res3/values-no/values.xml");
assertNotSame(res1, res2);
assertNotSame(res1, res3);
assertNotSame(res2, res3);
ModuleResourceRepository resources = ModuleResourceRepository.createForTest(myFacet, Arrays.asList(res1, res2, res3));
// Check that values are handled correctly. First a plain value (not overridden anywhere).
assertStringIs(resources, "title_layout_changes", "Layout Changes");
// Check that an overridden key (overridden in just one flavor) is picked up
// Overridden in res2
assertStringIs(resources, "title_crossfade", "Complex Crossfade");
// Overridden in res3
assertStringIs(resources, "title_zoom", "Zoom!");
// Make sure that new/unique strings from flavors are available
// Overridden in res2
assertStringIs(resources, "unique_string", "Unique");
// Overridden in res3
assertStringIs(resources, "another_unique_string", "Another Unique", false);
// Check that an overridden key (overridden in multiple flavors) picks the last one
// res3 (not unique because we have a values-no item too)
assertStringIs(resources, "app_name", "Very Different App Name", false);
// Layouts: Should only be offered id's from the overriding layout (plus those defined in values.xml)
// from values.xml
assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
// from res2 layout1.xml
assertTrue(resources.hasResourceItem(ResourceType.ID, "noteArea"));
// Layout masking does not currently work. I'm not 100% certain what the intended behavior is
// here (e.g. res1's layout1 contains @+id/button1, res2's layout1 does not; should @+id/button1 be visible?)
//assertFalse(resources.hasResourceItem(ResourceType.ID, "btn_title_refresh")); // masked in res1 by res2's layout replacement
// Check that localized lookup (qualifier matching works)
List<ResourceItem> stringList = resources.getResourceItem(ResourceType.STRING, "another_unique_string");
assertNotNull(stringList);
assertSize(2, stringList);
FolderConfiguration valueConfig = FolderConfiguration.getConfigForFolder("values-no");
assertNotNull(valueConfig);
ResourceValue stringValue = resources.getConfiguredResources(ResourceType.STRING, valueConfig).get("another_unique_string");
assertNotNull(stringValue);
assertEquals("En Annen", stringValue.getValue());
// Change flavor order and make sure things are updated and work correctly
resources.updateRoots(Arrays.asList(res1, res3, res2));
// Should now be picking app_name from res2 rather than res3 since it's now last
// res2
assertStringIs(resources, "app_name", "Different App Name", false);
// Sanity check other merging
assertStringIs(resources, "title_layout_changes", "Layout Changes");
// Overridden in res2
assertStringIs(resources, "title_crossfade", "Complex Crossfade");
// Overridden in res3
assertStringIs(resources, "title_zoom", "Zoom!");
// Overridden in res2
assertStringIs(resources, "unique_string", "Unique");
// Overridden in res3
assertStringIs(resources, "another_unique_string", "Another Unique", false);
// Hide a resource root (res2)
resources.updateRoots(Arrays.asList(res1, res3));
// No longer aliasing the main layout
// res1 layout1.xml
assertTrue(resources.hasResourceItem(ResourceType.ID, "btn_title_refresh"));
// from res1 layout1.xml
assertTrue(resources.hasResourceItem(ResourceType.ID, "noteArea"));
// from values.xml
assertTrue(resources.hasResourceItem(ResourceType.ID, "action_next"));
// No longer overridden in res2
assertStringIs(resources, "title_crossfade", "Simple Crossfade");
// Finally ensure that we can switch roots repeatedly (had some earlier bugs related to root unregistration)
resources.updateRoots(Arrays.asList(res1, res3, res2));
resources.updateRoots(Arrays.asList(res1));
resources.updateRoots(Arrays.asList(res1, res3, res2));
resources.updateRoots(Arrays.asList(res1));
resources.updateRoots(Arrays.asList(res1, res3, res2));
resources.updateRoots(Arrays.asList(res2));
resources.updateRoots(Arrays.asList(res1));
resources.updateRoots(Arrays.asList(res1, res2, res3));
assertStringIs(resources, "title_layout_changes", "Layout Changes");
// Make sure I get all the resource ids (there can be multiple; these are not replaced via overlays)
List<ResourceItem> ids = resources.getResourceItem(ResourceType.ID, "my_id");
assertNotNull(ids);
assertSize(2, ids);
Collections.sort(ids, new Comparator<ResourceItem>() {
@SuppressWarnings("ConstantConditions")
@Override
public int compare(ResourceItem item1, ResourceItem item2) {
return item1.getSource().getFile().getName().compareTo(item2.getSource().getFile().getName());
}
});
//noinspection ConstantConditions
assertEquals("layout_ids1.xml", ids.get(0).getSource().getFile().getName());
//noinspection ConstantConditions
assertEquals("layout_ids2.xml", ids.get(1).getSource().getFile().getName());
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class ResourceFolderRepositoryTest method testSerializeDensityBasedResourceValues.
public void testSerializeDensityBasedResourceValues() throws Exception {
myFixture.copyFileToProject(STRINGS, "res/values/strings.xml");
myFixture.copyFileToProject(DRAWABLE_ID_SCAN, "res/drawable-hdpi/drawable_foo.xml");
myFixture.copyFileToProject(DRAWABLE_ID_SCAN, "res/drawable-xhdpi/drawable_foo.xml");
myFixture.copyFileToProject(DRAWABLE_ID_SCAN, "res/drawable-fr/drawable_foo.xml");
final ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
FolderConfiguration config = FolderConfiguration.getConfigForFolder("drawable-xhdpi");
assertNotNull(config);
// For drawable xml, the ResourceValue#getValue is the file path.
ResourceValue value = resources.getConfiguredValue(ResourceType.DRAWABLE, "drawable_foo", config);
assertNotNull(value);
String valueString = value.getValue();
assertNotNull(valueString);
assertTrue(valueString.endsWith("drawable_foo.xml"));
assertTrue(valueString.contains("drawable-xhdpi"));
DensityBasedResourceValue densityValue = (DensityBasedResourceValue) value;
assertEquals(Density.XHIGH, densityValue.getResourceDensity());
resources.saveStateToFile();
ResourceFolderRegistry.reset();
final ResourceFolderRepository fromBlob = createRepository();
assertNotNull(fromBlob);
// We don't count files that we explicitly skip against freshness.
assertTrue(fromBlob.hasFreshFileCache());
assertNotSame(resources, fromBlob);
assertTrue(fromBlob.equalFilesItems(resources));
value = fromBlob.getConfiguredValue(ResourceType.DRAWABLE, "drawable_foo", config);
assertNotNull(value);
valueString = value.getValue();
assertNotNull(valueString);
assertTrue(valueString.endsWith("drawable_foo.xml"));
assertTrue(valueString.contains("drawable-xhdpi"));
// Make sure that the resource value is still of type DensityBasedResourceValue.
densityValue = (DensityBasedResourceValue) value;
assertEquals(Density.XHIGH, densityValue.getResourceDensity());
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android_frameworks_base by DirtyUnicorns.
the class ConfigGenerator method getFolderConfig.
public FolderConfiguration getFolderConfig() {
FolderConfiguration config = new FolderConfiguration();
config.createDefault();
config.setDensityQualifier(new DensityQualifier(mDensity));
config.setNavigationMethodQualifier(new NavigationMethodQualifier(mNavigation));
if (mScreenWidth > mScreenHeight) {
config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenWidth, mScreenHeight));
} else {
config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenHeight, mScreenWidth));
}
config.setScreenRatioQualifier(new ScreenRatioQualifier(mRatio));
config.setScreenSizeQualifier(new ScreenSizeQualifier(mSize));
config.setTextInputMethodQualifier(new TextInputMethodQualifier(mKeyboard));
config.setTouchTypeQualifier(new TouchScreenQualifier(mTouchScreen));
config.setKeyboardStateQualifier(new KeyboardStateQualifier(mKeyboardState));
config.setScreenOrientationQualifier(new ScreenOrientationQualifier(mOrientation));
config.updateScreenWidthAndHeight();
// some default qualifiers.
config.setUiModeQualifier(new UiModeQualifier(UiMode.NORMAL));
config.setNightModeQualifier(new NightModeQualifier(NightMode.NOTNIGHT));
config.setCountryCodeQualifier(new CountryCodeQualifier());
config.setLayoutDirectionQualifier(new LayoutDirectionQualifier());
config.setNetworkCodeQualifier(new NetworkCodeQualifier());
config.setLocaleQualifier(new LocaleQualifier());
config.setVersionQualifier(new VersionQualifier());
return config;
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android_frameworks_base by crdroidandroid.
the class Main method getSessionParams.
/**
* Uses Theme.Material and Target sdk version as 22.
*/
private SessionParams getSessionParams(LayoutPullParser layoutParser, ConfigGenerator configGenerator, LayoutLibTestCallback layoutLibCallback, String themeName, boolean isProjectTheme, RenderingMode renderingMode, int targetSdk) {
FolderConfiguration config = configGenerator.getFolderConfig();
ResourceResolver resourceResolver = ResourceResolver.create(sProjectResources.getConfiguredResources(config), sFrameworkRepo.getConfiguredResources(config), themeName, isProjectTheme);
SessionParams sessionParams = new SessionParams(layoutParser, renderingMode, null, /*used for caching*/
configGenerator.getHardwareConfig(), resourceResolver, layoutLibCallback, 0, targetSdk, getLayoutLog());
sessionParams.setFlag(RenderParamsFlags.FLAG_DO_NOT_RENDER_ON_CREATE, true);
return sessionParams;
}
Aggregations