Merge "Add tests for getResource*Name()" into nyc-dev

This commit is contained in:
Deepanshu Gupta
2016-03-25 20:52:51 +00:00
committed by Android (Google) Code Review
5 changed files with 103 additions and 77 deletions

View File

@@ -647,15 +647,15 @@ public class Resources_Delegate {
static String getResourceName(Resources resources, int resid) throws NotFoundException { static String getResourceName(Resources resources, int resid) throws NotFoundException {
boolean[] platformOut = new boolean[1]; boolean[] platformOut = new boolean[1];
Pair<ResourceType, String> resourceInfo = getResourceInfo(resources, resid, platformOut); Pair<ResourceType, String> resourceInfo = getResourceInfo(resources, resid, platformOut);
String namespace; String packageName;
if (resourceInfo != null) { if (resourceInfo != null) {
if (platformOut[0]) { if (platformOut[0]) {
namespace = SdkConstants.ANDROID_NS_NAME; packageName = SdkConstants.ANDROID_NS_NAME;
} else { } else {
namespace = resources.mContext.getPackageName(); packageName = resources.mContext.getPackageName();
namespace = namespace == null ? SdkConstants.APP_PREFIX : namespace; packageName = packageName == null ? SdkConstants.APP_PREFIX : packageName;
} }
return namespace + ':' + resourceInfo.getFirst().getName() + '/' + return packageName + ':' + resourceInfo.getFirst().getName() + '/' +
resourceInfo.getSecond(); resourceInfo.getSecond();
} }
throwException(resid, null); throwException(resid, null);

View File

@@ -122,7 +122,7 @@ public abstract class RenderAction<T extends RenderParams> extends FrameworkReso
// build the context // build the context
mContext = new BridgeContext(mParams.getProjectKey(), metrics, resources, mContext = new BridgeContext(mParams.getProjectKey(), metrics, resources,
mParams.getAssets(), mParams.getLayoutlibCallback(), getConfiguration(), mParams.getAssets(), mParams.getLayoutlibCallback(), getConfiguration(mParams),
mParams.getTargetSdkVersion(), mParams.isRtlSupported()); mParams.getTargetSdkVersion(), mParams.isRtlSupported());
setUp(); setUp();
@@ -130,7 +130,6 @@ public abstract class RenderAction<T extends RenderParams> extends FrameworkReso
return SUCCESS.createResult(); return SUCCESS.createResult();
} }
/** /**
* Prepares the scene for action. * Prepares the scene for action.
* <p> * <p>
@@ -320,10 +319,11 @@ public abstract class RenderAction<T extends RenderParams> extends FrameworkReso
} }
} }
private Configuration getConfiguration() { // VisibleForTesting
public static Configuration getConfiguration(RenderParams params) {
Configuration config = new Configuration(); Configuration config = new Configuration();
HardwareConfig hardwareConfig = mParams.getHardwareConfig(); HardwareConfig hardwareConfig = params.getHardwareConfig();
ScreenSize screenSize = hardwareConfig.getScreenSize(); ScreenSize screenSize = hardwareConfig.getScreenSize();
if (screenSize != null) { if (screenSize != null) {
@@ -392,7 +392,7 @@ public abstract class RenderAction<T extends RenderParams> extends FrameworkReso
} else { } else {
config.screenLayout |= Configuration.SCREENLAYOUT_ROUND_UNDEFINED; config.screenLayout |= Configuration.SCREENLAYOUT_ROUND_UNDEFINED;
} }
String locale = getParams().getLocale(); String locale = params.getLocale();
if (locale != null && !locale.isEmpty()) config.locale = new Locale(locale); if (locale != null && !locale.isEmpty()) config.locale = new Locale(locale);
// TODO: fill in more config info. // TODO: fill in more config info.

View File

@@ -29,11 +29,14 @@ import com.android.ide.common.resources.ResourceResolver;
import com.android.ide.common.resources.configuration.FolderConfiguration; import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.io.FolderWrapper; import com.android.io.FolderWrapper;
import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.android.BridgeContext;
import com.android.layoutlib.bridge.impl.RenderAction;
import com.android.layoutlib.bridge.intensive.setup.ConfigGenerator; import com.android.layoutlib.bridge.intensive.setup.ConfigGenerator;
import com.android.layoutlib.bridge.intensive.setup.LayoutLibTestCallback; import com.android.layoutlib.bridge.intensive.setup.LayoutLibTestCallback;
import com.android.layoutlib.bridge.intensive.setup.LayoutPullParser; import com.android.layoutlib.bridge.intensive.setup.LayoutPullParser;
import com.android.resources.Density; import com.android.resources.Density;
import com.android.resources.Navigation; import com.android.resources.Navigation;
import com.android.resources.ResourceType;
import com.android.utils.ILogger; import com.android.utils.ILogger;
import org.junit.AfterClass; import org.junit.AfterClass;
@@ -42,13 +45,15 @@ import org.junit.Test;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import java.io.File; import java.io.File;
import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -160,13 +165,8 @@ public class Main {
if (!host.isDirectory()) { if (!host.isDirectory()) {
return null; return null;
} }
File[] hosts = host.listFiles(new FileFilter() { File[] hosts = host.listFiles(path -> path.isDirectory() &&
@Override (path.getName().startsWith("linux-") || path.getName().startsWith("darwin-")));
public boolean accept(File path) {
return path.isDirectory() && (path.getName().startsWith("linux-") || path.getName()
.startsWith("darwin-"));
}
});
for (File hostOut : hosts) { for (File hostOut : hosts) {
String platformDir = getPlatformDirFromHostOut(hostOut); String platformDir = getPlatformDirFromHostOut(hostOut);
if (platformDir != null) { if (platformDir != null) {
@@ -184,12 +184,9 @@ public class Main {
if (!sdkDir.isDirectory()) { if (!sdkDir.isDirectory()) {
return null; return null;
} }
File[] sdkDirs = sdkDir.listFiles(new FileFilter() { File[] sdkDirs = sdkDir.listFiles(path -> {
@Override
public boolean accept(File path) {
// We need to search for $TARGET_PRODUCT (usually, sdk_phone_armv7) // We need to search for $TARGET_PRODUCT (usually, sdk_phone_armv7)
return path.isDirectory() && path.getName().startsWith("sdk"); return path.isDirectory() && path.getName().startsWith("sdk");
}
}); });
for (File dir : sdkDirs) { for (File dir : sdkDirs) {
String platformDir = getPlatformDirFromHostOutSdkSdk(dir); String platformDir = getPlatformDirFromHostOutSdkSdk(dir);
@@ -201,27 +198,16 @@ public class Main {
} }
private static String getPlatformDirFromHostOutSdkSdk(File sdkDir) { private static String getPlatformDirFromHostOutSdkSdk(File sdkDir) {
File[] possibleSdks = sdkDir.listFiles(new FileFilter() { File[] possibleSdks = sdkDir.listFiles(
@Override path -> path.isDirectory() && path.getName().contains("android-sdk"));
public boolean accept(File path) {
return path.isDirectory() && path.getName().contains("android-sdk");
}
});
for (File possibleSdk : possibleSdks) { for (File possibleSdk : possibleSdks) {
File platformsDir = new File(possibleSdk, "platforms"); File platformsDir = new File(possibleSdk, "platforms");
File[] platforms = platformsDir.listFiles(new FileFilter() { File[] platforms = platformsDir.listFiles(
@Override path -> path.isDirectory() && path.getName().startsWith("android-"));
public boolean accept(File path) {
return path.isDirectory() && path.getName().startsWith("android-");
}
});
if (platforms == null || platforms.length == 0) { if (platforms == null || platforms.length == 0) {
continue; continue;
} }
Arrays.sort(platforms, new Comparator<File>() { Arrays.sort(platforms, (o1, o2) -> {
// Codenames before ints. Higher APIs precede lower.
@Override
public int compare(File o1, File o2) {
final int MAX_VALUE = 1000; final int MAX_VALUE = 1000;
String suffix1 = o1.getName().substring("android-".length()); String suffix1 = o1.getName().substring("android-".length());
String suffix2 = o2.getName().substring("android-".length()); String suffix2 = o2.getName().substring("android-".length());
@@ -240,7 +226,6 @@ public class Main {
return suff2 - suff1; return suff2 - suff1;
} }
return suffix2.compareTo(suffix1); return suffix2.compareTo(suffix1);
}
}); });
return platforms[0].getAbsolutePath(); return platforms[0].getAbsolutePath();
} }
@@ -261,6 +246,7 @@ public class Main {
return null; return null;
} }
} }
/** /**
* Initialize the bridge and the resource maps. * Initialize the bridge and the resource maps.
*/ */
@@ -325,8 +311,7 @@ public class Main {
@Test @Test
public void testExpand() throws ClassNotFoundException { public void testExpand() throws ClassNotFoundException {
// Create the layout pull parser. // Create the layout pull parser.
LayoutPullParser parser = new LayoutPullParser(APP_TEST_RES + "/layout/" + LayoutPullParser parser = createLayoutPullParser("expand_vert_layout.xml");
"expand_vert_layout.xml");
// Create LayoutLibCallback. // Create LayoutLibCallback.
LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger()); LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger());
layoutLibCallback.initResources(); layoutLibCallback.initResources();
@@ -348,8 +333,7 @@ public class Main {
.setScreenHeight(300) .setScreenHeight(300)
.setDensity(Density.XHIGH) .setDensity(Density.XHIGH)
.setNavigation(Navigation.NONAV); .setNavigation(Navigation.NONAV);
parser = new LayoutPullParser(APP_TEST_RES + "/layout/" + parser = createLayoutPullParser("expand_horz_layout.xml");
"expand_horz_layout.xml");
params = getSessionParams(parser, customConfigGenerator, params = getSessionParams(parser, customConfigGenerator,
layoutLibCallback, "Theme.Material.Light.NoActionBar.Fullscreen", false, layoutLibCallback, "Theme.Material.Light.NoActionBar.Fullscreen", false,
RenderingMode.H_SCROLL, 22); RenderingMode.H_SCROLL, 22);
@@ -361,8 +345,7 @@ public class Main {
@Test @Test
public void testVectorAnimation() throws ClassNotFoundException { public void testVectorAnimation() throws ClassNotFoundException {
// Create the layout pull parser. // Create the layout pull parser.
LayoutPullParser parser = new LayoutPullParser(APP_TEST_RES + "/layout/" + LayoutPullParser parser = createLayoutPullParser("indeterminate_progressbar.xml");
"indeterminate_progressbar.xml");
// Create LayoutLibCallback. // Create LayoutLibCallback.
LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger()); LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger());
layoutLibCallback.initResources(); layoutLibCallback.initResources();
@@ -373,8 +356,7 @@ public class Main {
renderAndVerify(params, "animated_vector.png", TimeUnit.SECONDS.toNanos(2)); renderAndVerify(params, "animated_vector.png", TimeUnit.SECONDS.toNanos(2));
parser = new LayoutPullParser(APP_TEST_RES + "/layout/" + parser = createLayoutPullParser("indeterminate_progressbar.xml");
"indeterminate_progressbar.xml");
params = getSessionParams(parser, ConfigGenerator.NEXUS_5, params = getSessionParams(parser, ConfigGenerator.NEXUS_5,
layoutLibCallback, "Theme.Material.NoActionBar.Fullscreen", false, layoutLibCallback, "Theme.Material.NoActionBar.Fullscreen", false,
RenderingMode.V_SCROLL, 22); RenderingMode.V_SCROLL, 22);
@@ -388,8 +370,7 @@ public class Main {
@Test @Test
public void testVectorDrawable() throws ClassNotFoundException { public void testVectorDrawable() throws ClassNotFoundException {
// Create the layout pull parser. // Create the layout pull parser.
LayoutPullParser parser = new LayoutPullParser(APP_TEST_RES + "/layout/" + LayoutPullParser parser = createLayoutPullParser("vector_drawable.xml");
"vector_drawable.xml");
// Create LayoutLibCallback. // Create LayoutLibCallback.
LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger()); LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger());
layoutLibCallback.initResources(); layoutLibCallback.initResources();
@@ -405,8 +386,7 @@ public class Main {
@Test @Test
public void testScrolling() throws ClassNotFoundException { public void testScrolling() throws ClassNotFoundException {
// Create the layout pull parser. // Create the layout pull parser.
LayoutPullParser parser = new LayoutPullParser(APP_TEST_RES + "/layout/" + LayoutPullParser parser = createLayoutPullParser("scrolled.xml");
"scrolled.xml");
// Create LayoutLibCallback. // Create LayoutLibCallback.
LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger()); LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger());
layoutLibCallback.initResources(); layoutLibCallback.initResources();
@@ -435,6 +415,39 @@ public class Main {
assertEquals(690, rootLayout.getChildren().get(5).getChildren().get(0).getRight()); assertEquals(690, rootLayout.getChildren().get(5).getChildren().get(0).getRight());
} }
@Test
public void testGetResourceNameVariants() throws Exception {
// Setup
SessionParams params = createSessionParams("", ConfigGenerator.NEXUS_4);
AssetManager assetManager = AssetManager.getSystem();
DisplayMetrics metrics = new DisplayMetrics();
Configuration configuration = RenderAction.getConfiguration(params);
Resources resources = new Resources(assetManager, metrics, configuration);
resources.mLayoutlibCallback = params.getLayoutlibCallback();
resources.mContext =
new BridgeContext(params.getProjectKey(), metrics, params.getResources(),
params.getAssets(), params.getLayoutlibCallback(), configuration,
params.getTargetSdkVersion(), params.isRtlSupported());
// Test
assertEquals("android:style/ButtonBar",
resources.getResourceName(android.R.style.ButtonBar));
assertEquals("android", resources.getResourcePackageName(android.R.style.ButtonBar));
assertEquals("ButtonBar", resources.getResourceEntryName(android.R.style.ButtonBar));
assertEquals("style", resources.getResourceTypeName(android.R.style.ButtonBar));
int id = resources.mLayoutlibCallback.getResourceId(ResourceType.STRING, "app_name");
assertEquals("com.android.layoutlib.test.myapplication:string/app_name",
resources.getResourceName(id));
assertEquals("com.android.layoutlib.test.myapplication",
resources.getResourcePackageName(id));
assertEquals("string", resources.getResourceTypeName(id));
assertEquals("app_name", resources.getResourceEntryName(id));
}
@NonNull
private LayoutPullParser createLayoutPullParser(String layoutPath) {
return new LayoutPullParser(APP_TEST_RES + "/layout/" + layoutPath);
}
/** /**
* Create a new rendering session and test that rendering the given layout doesn't throw any * Create a new rendering session and test that rendering the given layout doesn't throw any
* exceptions and matches the provided image. * exceptions and matches the provided image.
@@ -505,16 +518,21 @@ public class Main {
private RenderResult renderAndVerify(String layoutFileName, String goldenFileName, private RenderResult renderAndVerify(String layoutFileName, String goldenFileName,
ConfigGenerator deviceConfig) ConfigGenerator deviceConfig)
throws ClassNotFoundException { throws ClassNotFoundException {
SessionParams params = createSessionParams(layoutFileName, deviceConfig);
return renderAndVerify(params, goldenFileName);
}
private SessionParams createSessionParams(String layoutFileName, ConfigGenerator deviceConfig)
throws ClassNotFoundException {
// Create the layout pull parser. // Create the layout pull parser.
LayoutPullParser parser = new LayoutPullParser(APP_TEST_RES + "/layout/" + layoutFileName); LayoutPullParser parser = createLayoutPullParser(layoutFileName);
// Create LayoutLibCallback. // Create LayoutLibCallback.
LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger()); LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger());
layoutLibCallback.initResources(); layoutLibCallback.initResources();
// TODO: Set up action bar handler properly to test menu rendering. // TODO: Set up action bar handler properly to test menu rendering.
// Create session params. // Create session params.
SessionParams params = getSessionParams(parser, deviceConfig, return getSessionParams(parser, deviceConfig,
layoutLibCallback, "AppTheme", true, RenderingMode.NORMAL, 22); layoutLibCallback, "AppTheme", true, RenderingMode.NORMAL, 22);
return renderAndVerify(params, goldenFileName);
} }
/** /**

View File

@@ -24,7 +24,9 @@ import com.android.ide.common.rendering.api.LayoutlibCallback;
import com.android.ide.common.rendering.api.ParserFactory; import com.android.ide.common.rendering.api.ParserFactory;
import com.android.ide.common.rendering.api.ResourceReference; import com.android.ide.common.rendering.api.ResourceReference;
import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.ResourceValue;
import com.android.ide.common.rendering.api.SessionParams.Key;
import com.android.ide.common.resources.IntArrayWrapper; import com.android.ide.common.resources.IntArrayWrapper;
import com.android.layoutlib.bridge.android.RenderParamsFlags;
import com.android.resources.ResourceType; import com.android.resources.ResourceType;
import com.android.util.Pair; import com.android.util.Pair;
import com.android.utils.ILogger; import com.android.utils.ILogger;
@@ -176,4 +178,12 @@ public class LayoutLibTestCallback extends LayoutlibCallback {
} }
}; };
} }
@Override
public <T> T getFlag(Key<T> key) {
if (key.equals(RenderParamsFlags.FLAG_KEY_APPLICATION_PACKAGE)) {
return (T) PACKAGE_NAME;
}
return null;
}
} }

View File

@@ -56,9 +56,7 @@ public class LayoutPullParser extends KXmlParser implements ILayoutPullParser{
public LayoutPullParser(File layoutFile) { public LayoutPullParser(File layoutFile) {
try { try {
init(new FileInputStream(layoutFile)); init(new FileInputStream(layoutFile));
} catch (XmlPullParserException e) { } catch (XmlPullParserException | FileNotFoundException e) {
throw new IOError(e);
} catch (FileNotFoundException e) {
throw new IOError(e); throw new IOError(e);
} }
} }