diff options
author | Yigit Boyar <yboyar@google.com> | 2016-02-16 16:11:30 -0800 |
---|---|---|
committer | Yigit Boyar <yboyar@google.com> | 2016-02-16 16:22:06 -0800 |
commit | 09f8e10e2a49a075da8bb2f64853377c5e37045a (patch) | |
tree | eea7acd39e38da3b9f4cc7e12a7dc83dc22df74c | |
parent | 6047998943beebd81e0ae1068df39c0cbee38628 (diff) | |
download | data-binding-09f8e10e2a49a075da8bb2f64853377c5e37045a.tar.gz |
Allow ObservableFields to be Bindable.
This CL fixes a bug where if a field/accessor is Observable and
Bindable, it would not be assigned a BR id, resulting in
malformed generated code.
Bug: 26922185
Change-Id: Ia3d11204460fc1967f0a7b60ed7cba1d3698098d
4 files changed, 223 insertions, 0 deletions
diff --git a/compiler/src/main/java/android/databinding/tool/expr/FieldAccessExpr.java b/compiler/src/main/java/android/databinding/tool/expr/FieldAccessExpr.java index 810f363d..5d850b5d 100644 --- a/compiler/src/main/java/android/databinding/tool/expr/FieldAccessExpr.java +++ b/compiler/src/main/java/android/databinding/tool/expr/FieldAccessExpr.java @@ -299,6 +299,9 @@ public class FieldAccessExpr extends Expr { FieldAccessExpr observableField = getModel().observableField(child, mName); observableField.mGetter = mGetter; + if (hasBindableAnnotations()) { + observableField.mBrName = ExtKt.br(BrNameUtil.brKey(mGetter)); + } getChildren().add(observableField); observableField.getParents().add(this); diff --git a/integration-tests/TestApp/app/src/androidTestApi7/java/android/databinding/testapp/BindableObservablesTest.java b/integration-tests/TestApp/app/src/androidTestApi7/java/android/databinding/testapp/BindableObservablesTest.java new file mode 100644 index 00000000..07e50754 --- /dev/null +++ b/integration-tests/TestApp/app/src/androidTestApi7/java/android/databinding/testapp/BindableObservablesTest.java @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2016 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 android.databinding.testapp; + +import android.annotation.TargetApi; +import android.databinding.ObservableField; +import android.databinding.ObservableInt; +import android.databinding.testapp.BR; +import android.databinding.testapp.databinding.BindableObservablesBinding; +import android.databinding.testapp.databinding.CallbacksBinding; +import android.databinding.testapp.vo.ViewModel; +import android.os.Build; +import android.support.test.runner.AndroidJUnit4; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.CoreMatchers.*; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class BindableObservablesTest { + @Rule + public DataBindingTestRule<BindableObservablesBinding> mBindingRule = new DataBindingTestRule<>( + R.layout.bindable_observables + ); + + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) + @Test + public void publicBinding() { + ViewModel model = new ViewModel(); + model.publicObservable.set(40); + BindableObservablesBinding binding = mBindingRule.getBinding(); + binding.setModel(model); + mBindingRule.executePending(); + assertThat(binding.view1.getMaxLines(), is(40)); + model.publicObservable.set(20); + mBindingRule.executePending(); + assertThat(binding.view1.getMaxLines(), is(20)); + } + + @Test + public void fieldBinding() { + ViewModel model = new ViewModel(); + model.getFieldObservable().set("abc"); + BindableObservablesBinding binding = mBindingRule.getBinding(); + binding.setModel(model); + mBindingRule.executePending(); + assertThat(binding.view2.getText().toString(), is("abc")); + model.getFieldObservable().set("def"); + mBindingRule.executePending(); + assertThat(binding.view2.getText().toString(), is("def")); + } + + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) + @Test + public void methodBinding() { + ViewModel model = new ViewModel(); + model.getMethodObservable().set(30); + BindableObservablesBinding binding = mBindingRule.getBinding(); + binding.setModel(model); + mBindingRule.executePending(); + assertThat(binding.view3.getMaxLines(), is(30)); + model.getMethodObservable().set(15); + mBindingRule.executePending(); + assertThat(binding.view3.getMaxLines(), is(15)); + } + + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) + @Test + public void publicBindingChangeObservable() { + ViewModel model = new ViewModel(); + model.publicObservable.set(40); + BindableObservablesBinding binding = mBindingRule.getBinding(); + binding.setModel(model); + mBindingRule.executePending(); + assertThat(binding.view1.getMaxLines(), is(40)); + + model.publicObservable = new ObservableInt(20); + + mBindingRule.executePending(); + assertThat(binding.view1.getMaxLines(), is(40)); + + model.notifyPropertyChanged(BR.publicObservable); + mBindingRule.executePending(); + assertThat(binding.view1.getMaxLines(), is(20)); + + } + + @Test + public void fieldBindingChangeObservable() { + ViewModel model = new ViewModel(); + model.getFieldObservable().set("abc"); + BindableObservablesBinding binding = mBindingRule.getBinding(); + binding.setModel(model); + mBindingRule.executePending(); + assertThat(binding.view2.getText().toString(), is("abc")); + + model.setFieldObservable(new ObservableField<String>("def")); + + mBindingRule.executePending(); + assertThat(binding.view2.getText().toString(), is("abc")); + + model.notifyPropertyChanged(BR.fieldObservable); + mBindingRule.executePending(); + assertThat(binding.view2.getText().toString(), is("def")); + } + + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) + @Test + public void methodBindingChangeObservable() { + ViewModel model = new ViewModel(); + model.getMethodObservable().set(30); + BindableObservablesBinding binding = mBindingRule.getBinding(); + binding.setModel(model); + mBindingRule.executePending(); + assertThat(binding.view3.getMaxLines(), is(30)); + + model.setMethodObservable(new ObservableInt(15)); + + mBindingRule.executePending(); + assertThat(binding.view3.getMaxLines(), is(30)); + + model.notifyPropertyChanged(BR.methodObservable); + mBindingRule.executePending(); + assertThat(binding.view3.getMaxLines(), is(15)); + } +} diff --git a/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/vo/ViewModel.java b/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/vo/ViewModel.java new file mode 100644 index 00000000..2454fb9d --- /dev/null +++ b/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/vo/ViewModel.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2016 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 android.databinding.testapp.vo; + +import android.databinding.BaseObservable; +import android.databinding.Bindable; +import android.databinding.ObservableField; +import android.databinding.ObservableInt; + +public class ViewModel extends BaseObservable { + @Bindable + public ObservableInt publicObservable = new ObservableInt(); + + @Bindable + private ObservableField<String> fieldObservable = new ObservableField<>(); + + private ObservableInt methodObservable = new ObservableInt(); + + + public ObservableField<String> getFieldObservable() { + return fieldObservable; + } + + @Bindable + public ObservableInt getMethodObservable() { + return methodObservable; + } + + public void setFieldObservable(ObservableField<String> fieldObservable) { + this.fieldObservable = fieldObservable; + } + + public void setMethodObservable(ObservableInt methodObservable) { + this.methodObservable = methodObservable; + } +} diff --git a/integration-tests/TestApp/app/src/main/res/layout/bindable_observables.xml b/integration-tests/TestApp/app/src/main/res/layout/bindable_observables.xml new file mode 100644 index 00000000..c325a23a --- /dev/null +++ b/integration-tests/TestApp/app/src/main/res/layout/bindable_observables.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright (C) 2016 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. + --> +<layout xmlns:android="http://schemas.android.com/apk/res/android"> + <data> + <variable name="model" type="android.databinding.testapp.vo.ViewModel"/> + </data> + <FrameLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + <TextView android:id="@+id/view1" android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:maxLines="@{model.publicObservable}"/> + <TextView android:id="@+id/view2" android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@{model.fieldObservable}"/> + <TextView android:id="@+id/view3" android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:maxLines="@{model.methodObservable}"/> + + </FrameLayout> +</layout> |