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

Commit 8323229b authored by Yigit Boyar's avatar Yigit Boyar
Browse files

Support include tags as inner binders

Change-Id: I2d35c9887678a3547937ad384b9c0dc7603f4f60
parent 5d97aa8a
Loading
Loading
Loading
Loading
+45 −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.LayoutWithIncludeBinder;
import com.android.databinding.testapp.vo.NotBindableVo;

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

public class IncludeTagTest extends BaseDataBinderTest<LayoutWithIncludeBinder> {

    public IncludeTagTest() {
        super(LayoutWithIncludeBinder.class, R.layout.layout_with_include);
    }

    @UiThreadTest
    public void testIncludeTag() {
        NotBindableVo vo = new NotBindableVo(3, "a");
        mBinder.setOuterObject(vo);
        mBinder.rebindDirty();
        final TextView outerText = (TextView) mBinder.getRoot().findViewById(R.id.outerTextView);
        assertEquals("a", outerText.getText());
        final TextView innerText = (TextView) mBinder.getRoot().findViewById(R.id.innerTextView);
        assertEquals("modified 3a", innerText.getText().toString());

        vo.setIntValue(5);
        vo.setStringValue("b");
        mBinder.invalidateAll();
        mBinder.rebindDirty();
        assertEquals("b", outerText.getText());
        assertEquals("modified 5b", innerText.getText().toString());
    }
}
+26 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ 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.
  -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:bind="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <variable name="innerObject" type="com.android.databinding.testapp.vo.NotBindableVo"/>
    <variable name="innerValue" type="java.lang.String"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:id="@+id/innerTextView"
              android:text="{innerValue + innerObject.stringValue}"
            />
</LinearLayout>
 No newline at end of file
+29 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ 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.
  -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:bind="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <variable name="outerObject" type="com.android.databinding.testapp.vo.NotBindableVo"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:id="@+id/outerTextView"
              android:text="{outerObject.stringValue}"/>
    <!-- TODO test id collision-->
    <include layout="@layout/included_layout" android:id="@+id/includedLayout"
             bind:innerObject="{outerObject}"
             bind:innerValue="{`modified ` + outerObject.intValue}"
            />
</LinearLayout>
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ public class BindingTarget {
    List<Binding> mBindings = new ArrayList<>();
    ExprModel mModel;
    Class mResolvedClass;
    String mIncludedLayout;

    public BindingTarget(Node node, String id, String viewClass) {
        mNode = node;
@@ -61,6 +62,18 @@ public class BindingTarget {
        return mResolvedClass;
    }

    public String getIncludedLayout() {
        return mIncludedLayout;
    }

    public boolean isBinder() {
        return mIncludedLayout != null;
    }

    public void setIncludedLayout(String includedLayout) {
        mIncludedLayout = includedLayout;
    }

    public List<Binding> getBindings() {
        return mBindings;
    }
+8 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ public class ClassAnalyzer {
    private static final String OBSERVABLE_LIST_CLASS_NAME = "android.binding.ObservableList";
    private static final String OBSERVABLE_MAP_CLASS_NAME = "android.binding.ObservableMap";
    private static final String BINDABLE_ANNOTATION_NAME = "android.binding.Bindable";
    private static final String I_VIEW_DATA_BINDER = "com.android.databinding.library.IViewDataBinder";

    private static Map<String, String> sTestClassNameMapping = ImmutableMap.of(
            OBSERVABLE_CLASS_NAME, "com.android.databinding.MockObservable",
@@ -63,10 +64,13 @@ public class ClassAnalyzer {

    private final boolean mTestMode;

    private final Class mIViewDataBinder;

    private ClassAnalyzer(ClassLoader classLoader, boolean testMode) {
        mClassLoader = classLoader;
        mTestMode = testMode;
        try {
            mIViewDataBinder = classLoader.loadClass(getClassName(I_VIEW_DATA_BINDER));
            mObservable = classLoader.loadClass(getClassName(OBSERVABLE_CLASS_NAME));
            mObservableList = classLoader.loadClass(getClassName(OBSERVABLE_LIST_CLASS_NAME));
            mObservableMap = classLoader.loadClass(getClassName(OBSERVABLE_MAP_CLASS_NAME));
@@ -101,6 +105,10 @@ public class ClassAnalyzer {
        return name;
    }

    public boolean isDataBinder(Class klass) {
        return mIViewDataBinder.isAssignableFrom(klass);
    }

    static String toCodeName(Class klass) {
        return klass.getName().replace("$", ".");
    }
Loading