diff options
author | George Mount <mount@google.com> | 2015-05-19 13:35:37 -0700 |
---|---|---|
committer | George Mount <mount@google.com> | 2015-05-19 14:47:36 -0700 |
commit | b9e4aa96812692a7dcf468445e64bc5b30d3c79a (patch) | |
tree | a401fb72819defa15613bd397f4f71b09996db5f /integration-tests/TestApp/app | |
parent | 9399cb4e3d86a88507fb06ad57661105b1eaa69a (diff) | |
download | data-binding-b9e4aa96812692a7dcf468445e64bc5b30d3c79a.tar.gz |
Better handling of generics in BindingAdapters
Bug 21277338
Handle <T> adapter(View, T value)
Also test and fixes to support View<T> with multi-attribute
adapters.
Change-Id: I12bda5b235111762b55357fed7f163ccaf83838d
Diffstat (limited to 'integration-tests/TestApp/app')
4 files changed, 200 insertions, 1 deletions
diff --git a/integration-tests/TestApp/app/src/androidTest/java/android/databinding/testapp/GenericAdapterTest.java b/integration-tests/TestApp/app/src/androidTest/java/android/databinding/testapp/GenericAdapterTest.java new file mode 100644 index 00000000..bc83ba43 --- /dev/null +++ b/integration-tests/TestApp/app/src/androidTest/java/android/databinding/testapp/GenericAdapterTest.java @@ -0,0 +1,48 @@ +/* + * 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 android.databinding.testapp; + +import android.databinding.testapp.databinding.GenericAdapterBinding; +import android.test.UiThreadTest; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class GenericAdapterTest extends BaseDataBinderTest<GenericAdapterBinding> { + + public GenericAdapterTest() { + super(GenericAdapterBinding.class); + } + + @UiThreadTest + public void testGenericArgs() throws Throwable { + initBinder(); + + String[] arr = { "Hello", "World" }; + List<String> list = Arrays.asList(arr); + getBinder().setList(list); + getBinder().setArr(arr); + getBinder().executePendingBindings(); + assertEquals("Hello World", getBinder().textView1.getText().toString()); + assertEquals("Hello World", getBinder().textView2.getText().toString()); + assertEquals("Hello World", getBinder().textView3.getText().toString()); + assertEquals("Hello World", getBinder().textView4.getText().toString()); + assertEquals(list, getBinder().view5.getList()); + assertEquals(list, getBinder().view6.getList()); + assertEquals("Hello World", getBinder().textView7.getText().toString()); + } +} diff --git a/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/GenericView.java b/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/GenericView.java index 7a321725..cb4be706 100644 --- a/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/GenericView.java +++ b/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/GenericView.java @@ -22,6 +22,7 @@ import android.view.View; import java.util.List; public class GenericView<T> extends View { + private List<T> mList; public GenericView(Context context) { super(context); @@ -39,5 +40,11 @@ public class GenericView<T> extends View { super(context, attrs, defStyleAttr, defStyleRes); } - public void setList(List<T> list) {} + public void setList(List<T> list) { + mList = list; + } + + public List<T> getList() { + return mList; + } } diff --git a/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/adapter/GenericAdapter.java b/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/adapter/GenericAdapter.java new file mode 100644 index 00000000..4b627a9b --- /dev/null +++ b/integration-tests/TestApp/app/src/main/java/android/databinding/testapp/adapter/GenericAdapter.java @@ -0,0 +1,81 @@ +/* + * 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 android.databinding.testapp.adapter; + +import android.databinding.BindingAdapter; +import android.databinding.testapp.GenericView; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +public class GenericAdapter { + + @BindingAdapter("textList1") + public static <T> void setListText(TextView view, List<T> list) { + setText(view, list); + } + + @BindingAdapter("textList2") + public static <T> void setCollectionText(TextView view, Collection<T> list) { + setText(view, list); + } + + @BindingAdapter("textArray") + public static <T> void setArrayText(TextView view, T[] values) { + setText(view, Arrays.asList(values)); + } + + @BindingAdapter({"textList1", "textArray"}) + public static <T> void setListAndArray(TextView view, List<T> list, T[] values) { + setText(view, list); + } + + @BindingAdapter("list") + public static <T> void setGenericViewValue(GenericView<T> view, List<T> value) { + view.setList(value); + } + + @BindingAdapter({"list", "array"}) + public static <T> void setGenericListAndArray(GenericView<T> view, List<T> list, T[] values) { + view.setList(list); + } + + @BindingAdapter("textList3") + public static void setGenericList(TextView view, List<String> list) { + setText(view, list); + } + + @BindingAdapter("textList3") + public static void setGenericIntegerList(TextView view, List<Integer> list) { + } + + private static <T> void setText(TextView view, Collection<T> collection) { + StringBuilder stringBuilder = new StringBuilder(); + boolean isFirst = true; + for (T val : collection) { + if (isFirst) { + isFirst = false; + } else { + stringBuilder.append(' '); + } + stringBuilder.append(val.toString()); + } + view.setText(stringBuilder.toString()); + } +} diff --git a/integration-tests/TestApp/app/src/main/res/layout/generic_adapter.xml b/integration-tests/TestApp/app/src/main/res/layout/generic_adapter.xml new file mode 100644 index 00000000..32741c7f --- /dev/null +++ b/integration-tests/TestApp/app/src/main/res/layout/generic_adapter.xml @@ -0,0 +1,63 @@ +<?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. + --> + +<layout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:bind="http://schemas.android.com/apk/res-auto"> + <data> + <variable name="list" type="java.util.List<String>"/> + <variable name="arr" type="String[]"/> + </data> + <LinearLayout + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="match_parent"> + <TextView + android:id="@+id/textView1" + bind:textList1="@{list}" + android:layout_width="wrap_content" + android:layout_height="wrap_content"/> + <TextView + android:id="@+id/textView2" + bind:textList2="@{list}" + android:layout_width="wrap_content" + android:layout_height="wrap_content"/> + <TextView + android:id="@+id/textView3" + bind:textArray="@{arr}" + android:layout_width="wrap_content" + android:layout_height="wrap_content"/> + <TextView + android:id="@+id/textView4" + bind:textList1="@{list}" + bind:textArray="@{arr}" + android:layout_width="wrap_content" + android:layout_height="wrap_content"/> + <android.databinding.testapp.GenericView + android:id="@+id/view5" + bind:list="@{list}" + android:layout_width="wrap_content" + android:layout_height="wrap_content"/> + <android.databinding.testapp.GenericView + android:id="@+id/view6" + bind:list="@{list}" + bind:array="@{arr}" + android:layout_width="wrap_content" + android:layout_height="wrap_content"/> + <TextView + android:id="@+id/textView7" + bind:textList3="@{list}" + android:layout_width="wrap_content" + android:layout_height="wrap_content"/> + </LinearLayout> +</layout>
\ No newline at end of file |