Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9a1918ff authored by George Mount's avatar George Mount
Browse files

Add quantity and format string easy formats.

parent 25ac81d8
Loading
Loading
Loading
Loading
+66 −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.
 */
package com.android.databinding.testapp;

import com.android.databinding.testapp.generated.ResourceTestBinder;

import android.test.UiThreadTest;
import android.widget.TextView;

public class ResourceTest extends BaseDataBinderTest<ResourceTestBinder> {

    public ResourceTest() {
        super(ResourceTestBinder.class, R.layout.resource_test);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mBinder.setCount(0);
        mBinder.setTitle("Mrs.");
        mBinder.setLastName("Doubtfire");
        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mBinder.rebindDirty();
                }
            });
        } catch (Throwable throwable) {
            throw new Exception(throwable);
        }
    }

    @UiThreadTest
    public void testStringFormat() throws Throwable {
        TextView view = mBinder.getTextView0();
        assertEquals("Mrs. Doubtfire", view.getText().toString());

        mBinder.setTitle("Mr.");
        mBinder.rebindDirty();
        assertEquals("Mr. Doubtfire", view.getText().toString());
    }

    @UiThreadTest
    public void testQuantityString() throws Throwable {
        TextView view = mBinder.getTextView1();
        assertEquals("oranges", view.getText().toString());

        mBinder.setCount(1);
        mBinder.rebindDirty();
        assertEquals("orange", view.getText().toString());
    }
}
+23 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
        >
    <variable name="count" type="int"/>
    <variable name="title" type="String"/>
    <variable name="lastName" type="String"/>

    <TextView
            android:id="@+id/textView0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{@string/nameWithTitle(title, lastName)}"/>

    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{@plurals/orange(count)}"/>
</LinearLayout>
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
@@ -14,4 +14,9 @@
<resources>
    <string name="app_name">TestApp</string>
    <string name="rain">Rain</string>
    <string name="nameWithTitle">%1$s %2$s</string>
    <plurals name="orange">
        <item quantity="one">orange</item>
        <item quantity="other">oranges</item>
    </plurals>
</resources>
+20 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@@ -150,8 +151,25 @@ public class ExpressionVisitor extends BindingExpressionBaseVisitor<Expr> {
    }

    @Override
    public Expr visitResource(@NotNull BindingExpressionParser.ResourceContext ctx) {
        return mModel.resourceExpr(ctx.getText());
    public Expr visitResources(@NotNull BindingExpressionParser.ResourcesContext ctx) {
        final List<Expr> args = new ArrayList<>();
        if (ctx.resourceParameters() != null) {
            for (ParseTree item : ctx.resourceParameters().expressionList().children) {
                if (Objects.equals(item.getText(), ",")) {
                    continue;
                }
                args.add(item.accept(this));
            }
        }
        final String resourceReference = ctx.ResourceReference().getText();
        final int colonIndex = resourceReference.indexOf(':');
        final int slashIndex = resourceReference.indexOf('/');
        final String packageName = colonIndex < 0 ? null :
                resourceReference.substring(1, colonIndex).trim();
        final int startIndex = Math.max(1, colonIndex + 1);
        final String resourceType = resourceReference.substring(startIndex, slashIndex).trim();
        final String resourceName = resourceReference.substring(slashIndex + 1).trim();
        return mModel.resourceExpr(packageName, resourceType, resourceName, args);
    }

    @Override
+3 −2
Original line number Diff line number Diff line
@@ -140,8 +140,9 @@ public class ExprModel {
        return register(new GroupExpr(grouped));
    }

    public Expr resourceExpr(String resourceText) {
        return register(new ResourceExpr(resourceText));
    public Expr resourceExpr(String packageName, String resourceType, String resourceName,
            List<Expr> args) {
        return register(new ResourceExpr(packageName, resourceType, resourceName, args));
    }

    public Expr bracketExpr(Expr variableExpr, Expr argExpr) {
Loading