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

Commit 99dfc75d authored by George Mount's avatar George Mount
Browse files

Added tests for BindingAdapters

Fixed a few small bugs as well.

Change-Id: Ie50afc6be457b293ce69508452bb38ea1ab75b41
parent 6bd7cd42
Loading
Loading
Loading
Loading
+68 −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.AbsListViewAdapterTestBinder;
import com.android.databinding.testapp.vo.AbsListViewBindingObject;

import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.widget.ListView;

public class AbsListViewBindingAdapterTest
        extends BindingAdapterTestBase<AbsListViewAdapterTestBinder, AbsListViewBindingObject> {

    ListView mView;

    public AbsListViewBindingAdapterTest() {
        super(AbsListViewAdapterTestBinder.class, AbsListViewBindingObject.class,
                R.layout.abs_list_view_adapter_test);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mView = mBinder.getView();
    }

    public void testListSelector() throws Throwable {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            assertEquals(mBindingObject.getListSelector().getColor(),
                    ((ColorDrawable) mView.getSelector()).getColor());

            changeValues();

            assertEquals(mBindingObject.getListSelector().getColor(),
                    ((ColorDrawable) mView.getSelector()).getColor());
        }
    }

    public void testScrollingCache() throws Throwable {
        assertEquals(mBindingObject.isScrollingCache(), mView.isScrollingCacheEnabled());

        changeValues();

        assertEquals(mBindingObject.isScrollingCache(), mView.isScrollingCacheEnabled());
    }

    public void testSmoothScrollbar() throws Throwable {
        assertEquals(mBindingObject.isSmoothScrollbar(), mView.isSmoothScrollbarEnabled());

        changeValues();

        assertEquals(mBindingObject.isSmoothScrollbar(), mView.isSmoothScrollbarEnabled());
    }
}
+49 −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.AbsSeekBarAdapterTestBinder;
import com.android.databinding.testapp.vo.AbsSeekBarBindingObject;

import android.os.Build;
import android.widget.SeekBar;

public class AbsSeekBarBindingAdapterTest
        extends BindingAdapterTestBase<AbsSeekBarAdapterTestBinder, AbsSeekBarBindingObject> {

    SeekBar mView;

    public AbsSeekBarBindingAdapterTest() {
        super(AbsSeekBarAdapterTestBinder.class, AbsSeekBarBindingObject.class,
                R.layout.abs_seek_bar_adapter_test);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mView = mBinder.getView();
    }

    public void testThumbTint() throws Throwable {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            assertEquals(mBindingObject.getThumbTint(), mView.getThumbTintList().getDefaultColor());

            changeValues();

            assertEquals(mBindingObject.getThumbTint(), mView.getThumbTintList().getDefaultColor());
        }
    }
}
+61 −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.AutoCompleteTextViewAdapterTestBinder;
import com.android.databinding.testapp.vo.AutoCompleteTextViewBindingObject;

import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.widget.AutoCompleteTextView;

public class AutoCompleteTextViewBindingAdapterTest extends
        BindingAdapterTestBase<AutoCompleteTextViewAdapterTestBinder,
                AutoCompleteTextViewBindingObject> {

    AutoCompleteTextView mView;

    public AutoCompleteTextViewBindingAdapterTest() {
        super(AutoCompleteTextViewAdapterTestBinder.class, AutoCompleteTextViewBindingObject.class,
                R.layout.auto_complete_text_view_adapter_test);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mView = mBinder.getView();
    }

    public void testCompletionThreshold() throws Throwable {
        assertEquals(mBindingObject.getCompletionThreshold(), mView.getThreshold());

        changeValues();

        assertEquals(mBindingObject.getCompletionThreshold(), mView.getThreshold());
    }

    public void testPopupBackground() throws Throwable {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            assertEquals(mBindingObject.getPopupBackground(),
                    ((ColorDrawable) mView.getDropDownBackground()).getColor());

            changeValues();

            assertEquals(mBindingObject.getPopupBackground(),
                    ((ColorDrawable) mView.getDropDownBackground()).getColor());
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ import android.test.ActivityInstrumentationTestCase2;

public class BaseDataBinderTest<T extends IViewDataBinder>
        extends ActivityInstrumentationTestCase2<TestActivity> {
    private Class<T> mBinderClass;
    protected Class<T> mBinderClass;
    private int mLayoutId;
    private int mOrientation;
    protected T mBinder;
+76 −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.library.IViewDataBinder;
import com.android.databinding.testapp.vo.BindingAdapterBindingObject;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BindingAdapterTestBase<T extends IViewDataBinder, V extends BindingAdapterBindingObject>
        extends BaseDataBinderTest<T> {
    private Class<V> mBindingObjectClass;

    protected V mBindingObject;

    private Method mSetMethod;

    public BindingAdapterTestBase(Class<T> binderClass, Class<V> observableClass, int layoutId) {
        super(binderClass, layoutId);
        mBindingObjectClass = observableClass;
        try {
            mSetMethod = binderClass.getDeclaredMethod("setObj", observableClass);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        mBindingObject = mBindingObjectClass.newInstance();
                        mSetMethod.invoke(mBinder, mBindingObject);
                        mBinder.rebindDirty();
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    } catch (InvocationTargetException e) {
                        throw new RuntimeException(e);
                    } catch (InstantiationException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        } catch (Throwable throwable) {
            throw new Exception(throwable);
        }
    }

    protected void changeValues() throws Throwable {
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBindingObject.changeValues();
                mBinder.rebindDirty();
            }
        });
    }
}
Loading