diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/adaptive_icon.xml b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/adaptive_icon.xml deleted file mode 100644 index ca9fa55d54246..0000000000000 --- a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/adaptive_icon.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/indeterminate_progressbar.xml b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/indeterminate_progressbar.xml deleted file mode 100644 index 70d739692e29b..0000000000000 --- a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/indeterminate_progressbar.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/insets.xml b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/insets.xml deleted file mode 100644 index ff06d79dd00a1..0000000000000 --- a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/insets.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/simple_activity.xml b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/simple_activity.xml deleted file mode 100644 index 14b93f364a357..0000000000000 --- a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/simple_activity.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/vector_drawable.xml b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/vector_drawable.xml deleted file mode 100644 index 2ce4f4cce9198..0000000000000 --- a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/vector_drawable.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/vector_drawable_android.xml b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/vector_drawable_android.xml deleted file mode 100644 index 3b01ea0931229..0000000000000 --- a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/vector_drawable_android.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/PerformanceTests.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/PerformanceTests.java index c90c26aad8ec8..230e116a3c264 100644 --- a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/PerformanceTests.java +++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/PerformanceTests.java @@ -26,6 +26,8 @@ import org.junit.runner.RunWith; import android.annotation.NonNull; +import java.io.FileNotFoundException; + /** * Set of render tests */ @@ -38,18 +40,19 @@ public class PerformanceTests extends RenderTestBase { } - private void render(@NonNull String layoutFileName) throws ClassNotFoundException { + private void render(@NonNull String layoutFileName) + throws ClassNotFoundException, FileNotFoundException { SessionParams params = createSessionParams(layoutFileName, ConfigGenerator.NEXUS_5); render(params, 250); } @Test - public void testActivity() throws ClassNotFoundException { + public void testActivity() throws ClassNotFoundException, FileNotFoundException { render("activity.xml"); } @Test - public void testAllWidgets() throws ClassNotFoundException { + public void testAllWidgets() throws ClassNotFoundException, FileNotFoundException { render("allwidgets.xml"); } } diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTestBase.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTestBase.java index 8739b7fade9a3..62a803c157505 100644 --- a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTestBase.java +++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTestBase.java @@ -51,6 +51,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.util.ArrayList; @@ -483,8 +484,9 @@ public class RenderTestBase { } @NonNull - protected LayoutPullParser createLayoutPullParser(String layoutPath) { - return new LayoutPullParser(APP_TEST_RES + "/layout/" + layoutPath); + protected LayoutPullParser createParserFromPath(String layoutPath) + throws FileNotFoundException { + return LayoutPullParser.createFromPath(APP_TEST_RES + "/layout/" + layoutPath); } /** @@ -493,7 +495,7 @@ public class RenderTestBase { */ @Nullable protected RenderResult renderAndVerify(String layoutFileName, String goldenFileName) - throws ClassNotFoundException { + throws ClassNotFoundException, FileNotFoundException { return renderAndVerify(layoutFileName, goldenFileName, ConfigGenerator.NEXUS_5); } @@ -503,15 +505,15 @@ public class RenderTestBase { */ @Nullable protected RenderResult renderAndVerify(String layoutFileName, String goldenFileName, - ConfigGenerator deviceConfig) throws ClassNotFoundException { + ConfigGenerator deviceConfig) throws ClassNotFoundException, FileNotFoundException { SessionParams params = createSessionParams(layoutFileName, deviceConfig); return renderAndVerify(params, goldenFileName); } protected SessionParams createSessionParams(String layoutFileName, ConfigGenerator deviceConfig) - throws ClassNotFoundException { + throws ClassNotFoundException, FileNotFoundException { // Create the layout pull parser. - LayoutPullParser parser = createLayoutPullParser(layoutFileName); + LayoutPullParser parser = createParserFromPath(layoutFileName); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java index 2b5e0f9dd5fae..833652a3a60b6 100644 --- a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java +++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java @@ -38,6 +38,7 @@ import android.content.res.Resources; import android.util.DisplayMetrics; import android.util.TypedValue; +import java.io.FileNotFoundException; import java.lang.reflect.Field; import java.util.concurrent.TimeUnit; @@ -54,17 +55,32 @@ import static org.junit.Assert.assertTrue; public class RenderTests extends RenderTestBase { @Test - public void testActivity() throws ClassNotFoundException { + public void testActivity() throws ClassNotFoundException, FileNotFoundException { renderAndVerify("activity.xml", "activity.png"); } @Test - public void testActivityOnOldTheme() throws ClassNotFoundException { + public void testActivityOnOldTheme() throws ClassNotFoundException, FileNotFoundException { LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); layoutLibCallback.initResources(); - LayoutPullParser parser = createLayoutPullParser("simple_activity.xml"); + LayoutPullParser parser = LayoutPullParser.createFromString( + "\n" + + " \n" + + ""); SessionParams params = getSessionParams(parser, ConfigGenerator.NEXUS_5, layoutLibCallback, "Theme.NoTitleBar", false, RenderingMode.NORMAL, 22); @@ -73,24 +89,24 @@ public class RenderTests extends RenderTestBase { } @Test - public void testTranslucentBars() throws ClassNotFoundException { + public void testTranslucentBars() throws ClassNotFoundException, FileNotFoundException { LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); layoutLibCallback.initResources(); - LayoutPullParser parser = createLayoutPullParser("four_corners.xml"); + LayoutPullParser parser = createParserFromPath("four_corners.xml"); SessionParams params = getSessionParams(parser, ConfigGenerator.NEXUS_5, layoutLibCallback, "Theme.Material.Light.NoActionBar.TranslucentDecor", false, RenderingMode.NORMAL, 22); renderAndVerify(params, "four_corners_translucent.png"); - parser = createLayoutPullParser("four_corners.xml"); + parser = createParserFromPath("four_corners.xml"); params = getSessionParams(parser, ConfigGenerator.NEXUS_5_LAND, layoutLibCallback, "Theme.Material.Light.NoActionBar.TranslucentDecor", false, RenderingMode.NORMAL, 22); renderAndVerify(params, "four_corners_translucent_land.png"); - parser = createLayoutPullParser("four_corners.xml"); + parser = createParserFromPath("four_corners.xml"); params = getSessionParams(parser, ConfigGenerator.NEXUS_5, layoutLibCallback, "Theme.Material.Light.NoActionBar", false, RenderingMode.NORMAL, 22); @@ -98,7 +114,7 @@ public class RenderTests extends RenderTestBase { } @Test - public void testAllWidgets() throws ClassNotFoundException { + public void testAllWidgets() throws ClassNotFoundException, FileNotFoundException { renderAndVerify("allwidgets.xml", "allwidgets.png"); // We expect fidelity warnings for Path.isConvex. Fail for anything else. @@ -106,12 +122,12 @@ public class RenderTests extends RenderTestBase { } @Test - public void testArrayCheck() throws ClassNotFoundException { + public void testArrayCheck() throws ClassNotFoundException, FileNotFoundException { renderAndVerify("array_check.xml", "array_check.png"); } @Test - public void testAllWidgetsTablet() throws ClassNotFoundException { + public void testAllWidgetsTablet() throws ClassNotFoundException, FileNotFoundException { renderAndVerify("allwidgets.xml", "allwidgets_tab.png", ConfigGenerator.NEXUS_7_2012); // We expect fidelity warnings for Path.isConvex. Fail for anything else. @@ -120,7 +136,23 @@ public class RenderTests extends RenderTestBase { @Test public void testActivityActionBar() throws ClassNotFoundException { - LayoutPullParser parser = createLayoutPullParser("simple_activity.xml"); + String simpleActivity = + "\n" + + " \n" + + ""; + + LayoutPullParser parser = LayoutPullParser.createFromString(simpleActivity); LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); layoutLibCallback.initResources(); @@ -131,7 +163,7 @@ public class RenderTests extends RenderTestBase { renderAndVerify(params, "simple_activity_noactionbar.png"); - parser = createLayoutPullParser("simple_activity.xml"); + parser = LayoutPullParser.createFromString(simpleActivity); params = getSessionParams(parser, ConfigGenerator.NEXUS_5, layoutLibCallback, "Theme.Material.Light", false, RenderingMode.V_SCROLL, 22); @@ -140,7 +172,7 @@ public class RenderTests extends RenderTestBase { // This also tests that a theme with "NoActionBar" DOES HAVE an action bar when we are // displaying menus. - parser = createLayoutPullParser("simple_activity.xml"); + parser = LayoutPullParser.createFromString(simpleActivity); params = getSessionParams(parser, ConfigGenerator.NEXUS_5, layoutLibCallback, "Theme.Material.Light.NoActionBar", false, RenderingMode.V_SCROLL, 22); @@ -158,7 +190,17 @@ public class RenderTests extends RenderTestBase { Field field = insetsWidgetClass.getDeclaredField("sApplyInsetsCalled"); assertFalse((Boolean)field.get(null)); - LayoutPullParser parser = createLayoutPullParser("insets.xml"); + LayoutPullParser parser = LayoutPullParser.createFromString( + "\n" + "\n" + + " \n" + "\n"); LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); layoutLibCallback.initResources(); @@ -174,9 +216,9 @@ public class RenderTests extends RenderTestBase { /** Test expand_layout.xml */ @Test - public void testExpand() throws ClassNotFoundException { + public void testExpand() throws ClassNotFoundException, FileNotFoundException { // Create the layout pull parser. - LayoutPullParser parser = createLayoutPullParser("expand_vert_layout.xml"); + LayoutPullParser parser = createParserFromPath("expand_vert_layout.xml"); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); @@ -199,7 +241,7 @@ public class RenderTests extends RenderTestBase { .setScreenHeight(300) .setDensity(Density.XHIGH) .setNavigation(Navigation.NONAV); - parser = createLayoutPullParser("expand_horz_layout.xml"); + parser = createParserFromPath("expand_horz_layout.xml"); params = getSessionParams(parser, customConfigGenerator, layoutLibCallback, "Theme.Material.Light.NoActionBar.Fullscreen", false, RenderingMode.H_SCROLL, 22); @@ -210,8 +252,18 @@ public class RenderTests extends RenderTestBase { /** Test indeterminate_progressbar.xml */ @Test public void testVectorAnimation() throws ClassNotFoundException { + String layout = "\n" + + "\n" + "\n" + + " \n" + "\n" + + "\n"; + // Create the layout pull parser. - LayoutPullParser parser = createLayoutPullParser("indeterminate_progressbar.xml"); + LayoutPullParser parser = LayoutPullParser.createFromString(layout); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); @@ -223,7 +275,7 @@ public class RenderTests extends RenderTestBase { renderAndVerify(params, "animated_vector.png", TimeUnit.SECONDS.toNanos(2)); - parser = createLayoutPullParser("indeterminate_progressbar.xml"); + parser = LayoutPullParser.createFromString(layout); params = getSessionParams(parser, ConfigGenerator.NEXUS_5, layoutLibCallback, "Theme.Material.NoActionBar.Fullscreen", false, RenderingMode.V_SCROLL, 22); @@ -237,7 +289,17 @@ public class RenderTests extends RenderTestBase { @Test public void testVectorDrawable() throws ClassNotFoundException { // Create the layout pull parser. - LayoutPullParser parser = createLayoutPullParser("vector_drawable.xml"); + LayoutPullParser parser = LayoutPullParser.createFromString( + "\n" + + " \n" + "\n" + + ""); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); @@ -256,7 +318,21 @@ public class RenderTests extends RenderTestBase { @Test public void testVectorDrawable91383() throws ClassNotFoundException { // Create the layout pull parser. - LayoutPullParser parser = createLayoutPullParser("vector_drawable_android.xml"); + LayoutPullParser parser = LayoutPullParser.createFromString( + "\n" + + " \n" + + " \n" + + ""); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); @@ -271,9 +347,9 @@ public class RenderTests extends RenderTestBase { /** Test activity.xml */ @Test - public void testScrollingAndMeasure() throws ClassNotFoundException { + public void testScrollingAndMeasure() throws ClassNotFoundException, FileNotFoundException { // Create the layout pull parser. - LayoutPullParser parser = createLayoutPullParser("scrolled.xml"); + LayoutPullParser parser = createParserFromPath("scrolled.xml"); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); @@ -307,7 +383,7 @@ public class RenderTests extends RenderTestBase { assertEquals(690, rootLayout.getChildren().get(5).getChildren().get(0).getRight()); // Do a full render pass - parser = createLayoutPullParser("scrolled.xml"); + parser = createParserFromPath("scrolled.xml"); params = getSessionParams(parser, ConfigGenerator.NEXUS_5, layoutLibCallback, "Theme.Material.NoActionBar.Fullscreen", false, @@ -326,7 +402,7 @@ public class RenderTests extends RenderTestBase { // Setup // Create the layout pull parser for our resources (empty.xml can not be part of the test // app as it won't compile). - LayoutPullParser parser = new LayoutPullParser("/empty.xml"); + LayoutPullParser parser = LayoutPullParser.createFromPath("/empty.xml"); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); @@ -363,7 +439,7 @@ public class RenderTests extends RenderTestBase { // Setup // Create the layout pull parser for our resources (empty.xml can not be part of the test // app as it won't compile). - LayoutPullParser parser = new LayoutPullParser("/empty.xml"); + LayoutPullParser parser = LayoutPullParser.createFromPath("/empty.xml"); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(RenderTestBase.getLogger(), mDefaultClassLoader); @@ -390,15 +466,25 @@ public class RenderTests extends RenderTestBase { } @Test - public void testFonts() throws ClassNotFoundException { + public void testFonts() throws ClassNotFoundException, FileNotFoundException { // TODO: styles seem to be broken in TextView renderAndVerify("fonts_test.xml", "font_test.png"); } @Test - public void testAdaptiveIcon() throws ClassNotFoundException { + public void testAdaptiveIcon() throws ClassNotFoundException, FileNotFoundException { // Create the layout pull parser. - LayoutPullParser parser = createLayoutPullParser("adaptive_icon.xml"); + LayoutPullParser parser = LayoutPullParser.createFromString( + "\n" + + " \n" + + "\n"); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(getLogger(), mDefaultClassLoader); @@ -416,7 +502,7 @@ public class RenderTests extends RenderTestBase { // Setup // Create the layout pull parser for our resources (empty.xml can not be part of the test // app as it won't compile). - LayoutPullParser parser = new LayoutPullParser("/empty.xml"); + LayoutPullParser parser = LayoutPullParser.createFromPath("/empty.xml"); // Create LayoutLibCallback. LayoutLibTestCallback layoutLibCallback = new LayoutLibTestCallback(RenderTestBase.getLogger(), mDefaultClassLoader); diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutLibTestCallback.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutLibTestCallback.java index 8ebfc659bbe76..75145d7e81dee 100644 --- a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutLibTestCallback.java +++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutLibTestCallback.java @@ -39,6 +39,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import java.io.File; +import java.io.FileNotFoundException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -147,7 +148,11 @@ public class LayoutLibTestCallback extends LayoutlibCallback { @Override public ILayoutPullParser getParser(ResourceValue layoutResource) { - return new LayoutPullParser(new File(layoutResource.getValue())); + try { + return LayoutPullParser.createFromFile(new File(layoutResource.getValue())); + } catch (FileNotFoundException e) { + return null; + } } @Override diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutPullParser.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutPullParser.java index bc8083f9c40fa..526613f5a9549 100644 --- a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutPullParser.java +++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutPullParser.java @@ -21,11 +21,15 @@ import com.android.ide.common.rendering.api.ILayoutPullParser; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParserException; +import android.annotation.NonNull; + +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOError; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; @@ -37,37 +41,40 @@ import static com.android.SdkConstants.SPINNER; import static com.android.SdkConstants.TOOLS_URI; public class LayoutPullParser extends KXmlParser implements ILayoutPullParser{ + @NonNull + public static LayoutPullParser createFromFile(@NonNull File layoutFile) + throws FileNotFoundException { + return new LayoutPullParser(new FileInputStream(layoutFile)); + } /** * @param layoutPath Must start with '/' and be relative to test resources. */ - public LayoutPullParser(String layoutPath) { + @NonNull + public static LayoutPullParser createFromPath(@NonNull String layoutPath) { if (layoutPath.startsWith("/")) { layoutPath = layoutPath.substring(1); } + + return new LayoutPullParser(LayoutPullParser.class.getClassLoader().getResourceAsStream + (layoutPath)); + } + + @NonNull + public static LayoutPullParser createFromString(@NonNull String contents) { + return new LayoutPullParser(new ByteArrayInputStream( + contents.getBytes(Charset.forName("UTF-8")))); + } + + private LayoutPullParser(@NonNull InputStream inputStream) { try { - init(getClass().getClassLoader().getResourceAsStream(layoutPath)); + setFeature(FEATURE_PROCESS_NAMESPACES, true); + setInput(inputStream, null); } catch (XmlPullParserException e) { throw new IOError(e); } } - /** - * @param layoutFile Path of the layout xml file on disk. - */ - public LayoutPullParser(File layoutFile) { - try { - init(new FileInputStream(layoutFile)); - } catch (XmlPullParserException | FileNotFoundException e) { - throw new IOError(e); - } - } - - private void init(InputStream stream) throws XmlPullParserException { - setFeature(FEATURE_PROCESS_NAMESPACES, true); - setInput(stream, null); - } - @Override public Object getViewCookie() { // TODO: Implement this properly.