Loading tools/data-binding/TestApp/src/androidTest/java/com/android/databinding/testapp/ResourceTest.java +20 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,8 @@ public class ResourceTest extends BaseDataBinderTest<ResourceTestBinder> { mBinder.setCount(0); mBinder.setTitle("Mrs."); mBinder.setLastName("Doubtfire"); mBinder.setBase(2); mBinder.setPbase(3); try { runTestOnUiThread(new Runnable() { @Override Loading Loading @@ -63,4 +65,22 @@ public class ResourceTest extends BaseDataBinderTest<ResourceTestBinder> { mBinder.rebindDirty(); assertEquals("orange", view.getText().toString()); } @UiThreadTest public void testFractionNoParameters() throws Throwable { TextView view = mBinder.getFractionNoParameters(); assertEquals("1.5", view.getText().toString()); } @UiThreadTest public void testFractionOneParameter() throws Throwable { TextView view = mBinder.getFractionOneParameter(); assertEquals("3.0", view.getText().toString()); } @UiThreadTest public void testFractionTwoParameters() throws Throwable { TextView view = mBinder.getFractionTwoParameters(); assertEquals("9.0", view.getText().toString()); } } tools/data-binding/TestApp/src/main/res/layout/resource_test.xml +17 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,8 @@ <variable name="count" type="int"/> <variable name="title" type="String"/> <variable name="lastName" type="String"/> <variable name="base" type="int"/> <variable name="pbase" type="int"/> <TextView android:id="@+id/textView0" Loading @@ -20,4 +22,19 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{@plurals/orange(count)}"/> <TextView android:id="@+id/fractionNoParameters" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`` + @fraction/myFraction}"/> <TextView android:id="@+id/fractionOneParameter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`` + @fraction/myFraction(base)}"/> <TextView android:id="@+id/fractionTwoParameters" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`` + @fraction/myParentFraction(base, pbase)}"/> </LinearLayout> No newline at end of file tools/data-binding/TestApp/src/main/res/values/fractions.xml 0 → 100644 +20 −0 Original line number Diff line number Diff line <!-- ~ Copyright (C) 2015 The Android Open Source Project ~ Licensed under the Apache License, Version 2.0 (the "License"); ~ you may not use this file except in compliance with the License. ~ You may obtain a copy of the License at ~ http://www.apache.org/licenses/LICENSE-2.0 ~ Unless required by applicable law or agreed to in writing, software ~ distributed under the License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ See the License for the specific language governing permissions and ~ limitations under the License. --> <resources> <!-- Base application theme. --> <fraction name="myFraction">150%</fraction> <fraction name="myParentFraction">300%p</fraction> </resources> tools/data-binding/compiler/src/main/java/com/android/databinding/expr/ResourceExpr.java +14 −1 Original line number Diff line number Diff line Loading @@ -149,7 +149,12 @@ public class ResourceExpr extends Expr { case "dimenOffset": return resources + ".getDimensionPixelOffset(" + resourceName + ")"; case "dimenSize": return resources + ".getDimensionPixelSize(" + resourceName + ")"; case "drawable": return resources + ".getDrawable(" + resourceName + ")"; case "fraction": return resources + ".getFraction(" + resourceName + ", 1, 1)"; case "fraction": { String base = getChildCode(0, "1"); String pbase = getChildCode(1, "1"); return resources + ".getFraction(" + resourceName + ", " + base + ", " + pbase + ")"; } case "id": return resourceName; case "intArray": return resources + ".getIntArray(" + resourceName + ")"; case "integer": return resources + ".getInteger(" + resourceName + ")"; Loading @@ -174,6 +179,14 @@ public class ResourceExpr extends Expr { } private String getChildCode(int childIndex, String defaultValue) { if (getChildren().size() <= childIndex) { return defaultValue; } else { return WriterPackage.toCode(getChildren().get(childIndex), false).generate(); } } private String makeParameterCall(String resourceName, String methodCall) { StringBuilder sb = new StringBuilder("getRoot().getResources()."); sb.append(methodCall).append("(").append(resourceName); Loading Loading
tools/data-binding/TestApp/src/androidTest/java/com/android/databinding/testapp/ResourceTest.java +20 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,8 @@ public class ResourceTest extends BaseDataBinderTest<ResourceTestBinder> { mBinder.setCount(0); mBinder.setTitle("Mrs."); mBinder.setLastName("Doubtfire"); mBinder.setBase(2); mBinder.setPbase(3); try { runTestOnUiThread(new Runnable() { @Override Loading Loading @@ -63,4 +65,22 @@ public class ResourceTest extends BaseDataBinderTest<ResourceTestBinder> { mBinder.rebindDirty(); assertEquals("orange", view.getText().toString()); } @UiThreadTest public void testFractionNoParameters() throws Throwable { TextView view = mBinder.getFractionNoParameters(); assertEquals("1.5", view.getText().toString()); } @UiThreadTest public void testFractionOneParameter() throws Throwable { TextView view = mBinder.getFractionOneParameter(); assertEquals("3.0", view.getText().toString()); } @UiThreadTest public void testFractionTwoParameters() throws Throwable { TextView view = mBinder.getFractionTwoParameters(); assertEquals("9.0", view.getText().toString()); } }
tools/data-binding/TestApp/src/main/res/layout/resource_test.xml +17 −0 Original line number Diff line number Diff line Loading @@ -8,6 +8,8 @@ <variable name="count" type="int"/> <variable name="title" type="String"/> <variable name="lastName" type="String"/> <variable name="base" type="int"/> <variable name="pbase" type="int"/> <TextView android:id="@+id/textView0" Loading @@ -20,4 +22,19 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{@plurals/orange(count)}"/> <TextView android:id="@+id/fractionNoParameters" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`` + @fraction/myFraction}"/> <TextView android:id="@+id/fractionOneParameter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`` + @fraction/myFraction(base)}"/> <TextView android:id="@+id/fractionTwoParameters" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`` + @fraction/myParentFraction(base, pbase)}"/> </LinearLayout> No newline at end of file
tools/data-binding/TestApp/src/main/res/values/fractions.xml 0 → 100644 +20 −0 Original line number Diff line number Diff line <!-- ~ Copyright (C) 2015 The Android Open Source Project ~ Licensed under the Apache License, Version 2.0 (the "License"); ~ you may not use this file except in compliance with the License. ~ You may obtain a copy of the License at ~ http://www.apache.org/licenses/LICENSE-2.0 ~ Unless required by applicable law or agreed to in writing, software ~ distributed under the License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ See the License for the specific language governing permissions and ~ limitations under the License. --> <resources> <!-- Base application theme. --> <fraction name="myFraction">150%</fraction> <fraction name="myParentFraction">300%p</fraction> </resources>
tools/data-binding/compiler/src/main/java/com/android/databinding/expr/ResourceExpr.java +14 −1 Original line number Diff line number Diff line Loading @@ -149,7 +149,12 @@ public class ResourceExpr extends Expr { case "dimenOffset": return resources + ".getDimensionPixelOffset(" + resourceName + ")"; case "dimenSize": return resources + ".getDimensionPixelSize(" + resourceName + ")"; case "drawable": return resources + ".getDrawable(" + resourceName + ")"; case "fraction": return resources + ".getFraction(" + resourceName + ", 1, 1)"; case "fraction": { String base = getChildCode(0, "1"); String pbase = getChildCode(1, "1"); return resources + ".getFraction(" + resourceName + ", " + base + ", " + pbase + ")"; } case "id": return resourceName; case "intArray": return resources + ".getIntArray(" + resourceName + ")"; case "integer": return resources + ".getInteger(" + resourceName + ")"; Loading @@ -174,6 +179,14 @@ public class ResourceExpr extends Expr { } private String getChildCode(int childIndex, String defaultValue) { if (getChildren().size() <= childIndex) { return defaultValue; } else { return WriterPackage.toCode(getChildren().get(childIndex), false).generate(); } } private String makeParameterCall(String resourceName, String methodCall) { StringBuilder sb = new StringBuilder("getRoot().getResources()."); sb.append(methodCall).append("(").append(resourceName); Loading