OSDN Git Service

Do not log errors while loading string arrays
authorDiego Perez <diegoperez@google.com>
Fri, 2 Dec 2016 11:16:26 +0000 (11:16 +0000)
committerDiego Perez <diegoperez@google.com>
Mon, 16 Jan 2017 15:36:41 +0000 (15:36 +0000)
When arrays contain references, if we can not resolve them, just return
the reference string. This matches the behaviour of resource resolution
in BridgeTypeArray.

Bug: http://b.android.com/228620
Test: Updates existing tests
Change-Id: Ia324f18d9128b8abf2ba676a83d4b424914e2c88

tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java
tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/values/arrays.xml
tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/Main.java

index c3d4cef..f3af5aa 100644 (file)
@@ -405,9 +405,6 @@ public class Resources_Delegate {
             rv = resources.mContext.getRenderResources().resolveResValue(rv);
             if (rv != null) {
                 return rv.getValue();
-            } else {
-                Bridge.getLog().error(LayoutLog.TAG_RESOURCES_RESOLVE,
-                        "Unable to resolve resource " + ref, null);
             }
         }
         // Not a reference.
index f6e14d2..5f58d39 100644 (file)
@@ -17,6 +17,7 @@
         <!-- theme ref in android NS. value = @string/candidates_style = <u>candidates</u> -->
         <item>?android:attr/candidatesTextStyleSpans</item>
         <item>@android:string/unknownName</item>  <!-- value = Unknown -->
+        <item>?EC</item>
     </string-array>
 
     <!-- resources that the above array can refer to -->
index cdcae89..ecaecd3 100644 (file)
@@ -69,6 +69,7 @@ import java.util.concurrent.TimeUnit;
 
 import com.google.android.collect.Lists;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -313,6 +314,12 @@ public class Main {
         sBridge = new Bridge();
         sBridge.init(ConfigGenerator.loadProperties(buildProp), fontLocation,
                 ConfigGenerator.getEnumMap(attrs), getLayoutLog());
+        Bridge.getLock().lock();
+        try {
+            Bridge.setLog(getLayoutLog());
+        } finally {
+            Bridge.getLock().unlock();
+        }
     }
 
     @Before
@@ -624,6 +631,36 @@ public class Main {
         assertEquals("app_name", resources.getResourceEntryName(id));
     }
 
+    @Test
+    public void testStringEscaping() throws Exception {
+        // 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");
+        // Create LayoutLibCallback.
+        LayoutLibTestCallback layoutLibCallback =
+                new LayoutLibTestCallback(getLogger(), mDefaultClassLoader);
+        layoutLibCallback.initResources();
+        SessionParams params = getSessionParams(parser, ConfigGenerator.NEXUS_4,
+                layoutLibCallback, "AppTheme", true, RenderingMode.NORMAL, 22);
+        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());
+
+        int id = resources.mLayoutlibCallback.getResourceId(ResourceType.ARRAY, "string_array");
+        String[] strings = resources.getStringArray(id);
+        assertArrayEquals(
+                new String[]{"mystring", "Hello world!", "candidates", "Unknown", "?EC"},
+                strings);
+        assertTrue(sRenderMessages.isEmpty());
+    }
+
     @NonNull
     private LayoutPullParser createLayoutPullParser(String layoutPath) {
         return new LayoutPullParser(APP_TEST_RES + "/layout/" + layoutPath);