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

Commit 8a9d3762 authored by George Mount's avatar George Mount
Browse files

Add more tests.

parent 662d87aa
Loading
Loading
Loading
Loading
+84 −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.BaseObservable;
import com.android.databinding.testapp.generated.BasicBindingBinder;

import android.binding.Observable;
import android.binding.OnPropertyChangedListener;

import java.util.ArrayList;

public class BaseObservableTest extends BaseDataBinderTest<BasicBindingBinder> {
    private BaseObservable mObservable;
    private ArrayList<Integer> mNotifications = new ArrayList<>();
    private OnPropertyChangedListener mListener = new OnPropertyChangedListener() {
        @Override
        public void onPropertyChanged(Observable observable, int i) {
            assertEquals(mObservable, observable);
            mNotifications.add(i);
        }
    };

    public BaseObservableTest() {
        super(BasicBindingBinder.class, R.layout.basic_binding);
    }

    @Override
    protected void setUp() throws Exception {
        mNotifications.clear();
        mObservable = new BaseObservable();
    }

    public void testAddListener() {
        mObservable.notifyChange();
        assertTrue(mNotifications.isEmpty());
        mObservable.addOnPropertyChangedListener(mListener);
        mObservable.notifyChange();
        assertFalse(mNotifications.isEmpty());
    }

    public void testRemoveListener() {
        // test there is no exception when the listener isn't there
        mObservable.removeOnPropertyChangedListener(mListener);

        mObservable.addOnPropertyChangedListener(mListener);
        mObservable.notifyChange();
        mNotifications.clear();
        mObservable.removeOnPropertyChangedListener(mListener);
        mObservable.notifyChange();
        assertTrue(mNotifications.isEmpty());

        // test there is no exception when the listener isn't there
        mObservable.removeOnPropertyChangedListener(mListener);
    }

    public void testNotifyChange() {
        mObservable.addOnPropertyChangedListener(mListener);
        mObservable.notifyChange();
        assertEquals(1, mNotifications.size());
        assertEquals(0, (int) mNotifications.get(0));
    }

    public void testNotifyPropertyChanged() {
        final int expectedId = 100;
        mObservable.addOnPropertyChangedListener(mListener);
        mObservable.notifyPropertyChanged(expectedId);
        assertEquals(1, mNotifications.size());
        assertEquals(expectedId, (int) mNotifications.get(0));
    }
}
+250 −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.ObservableArrayList;
import com.android.databinding.testapp.generated.BasicBindingBinder;

import android.binding.ObservableList;
import android.binding.OnListChangedListener;

import java.util.ArrayList;

public class ObservableArrayListTest extends BaseDataBinderTest<BasicBindingBinder> {

    private static final int ALL = 0;

    private static final int CHANGE = 1;

    private static final int INSERT = 2;

    private static final int MOVE = 3;

    private static final int REMOVE = 4;

    private ObservableList<String> mObservable;

    private ArrayList<ListChange> mNotifications = new ArrayList<>();

    private OnListChangedListener mListener = new OnListChangedListener() {
        @Override
        public void onChanged() {
            mNotifications.add(new ListChange(ALL, 0, 0));
        }

        @Override
        public void onItemRangeChanged(int start, int count) {
            mNotifications.add(new ListChange(CHANGE, start, count));
        }

        @Override
        public void onItemRangeInserted(int start, int count) {
            mNotifications.add(new ListChange(INSERT, start, count));
        }

        @Override
        public void onItemRangeMoved(int from, int to, int count) {
            mNotifications.add(new ListChange(MOVE, from, to, count));
        }

        @Override
        public void onItemRangeRemoved(int start, int count) {
            mNotifications.add(new ListChange(REMOVE, start, count));
        }
    };

    private static class ListChange {

        public ListChange(int change, int start, int count) {
            this.start = start;
            this.count = count;
            this.from = 0;
            this.to = 0;
            this.change = change;
        }

        public ListChange(int change, int from, int to, int count) {
            this.from = from;
            this.to = to;
            this.count = count;
            this.start = 0;
            this.change = change;
        }

        public final int start;

        public final int count;

        public final int from;

        public final int to;

        public final int change;
    }

    public ObservableArrayListTest() {
        super(BasicBindingBinder.class, R.layout.basic_binding);
    }

    @Override
    protected void setUp() throws Exception {
        mNotifications.clear();
        mObservable = new ObservableArrayList<>();
    }

    public void testAddListener() {
        mObservable.add("Hello");
        assertTrue(mNotifications.isEmpty());
        mObservable.addOnListChangedListener(mListener);
        mObservable.add("World");
        assertFalse(mNotifications.isEmpty());
    }

    public void testRemoveListener() {
        // test there is no exception when the listener isn't there
        mObservable.removeOnListChangedListener(mListener);

        mObservable.addOnListChangedListener(mListener);
        mObservable.add("Hello");
        mNotifications.clear();
        mObservable.removeOnListChangedListener(mListener);
        mObservable.add("World");
        assertTrue(mNotifications.isEmpty());

        // test there is no exception when the listener isn't there
        mObservable.removeOnListChangedListener(mListener);
    }

    public void testAdd() {
        mObservable.addOnListChangedListener(mListener);
        mObservable.add("Hello");
        assertEquals(1, mNotifications.size());
        ListChange change = mNotifications.get(0);
        assertEquals(INSERT, change.change);
        assertEquals(0, change.start);
        assertEquals(1, change.count);
        assertEquals("Hello", mObservable.get(0));
    }

    public void testInsert() {
        mObservable.addOnListChangedListener(mListener);
        mObservable.add("Hello");
        mObservable.add(0, "World");
        mObservable.add(1, "Dang");
        mObservable.add(3, "End");
        assertEquals(4, mObservable.size());
        assertEquals("World", mObservable.get(0));
        assertEquals("Dang", mObservable.get(1));
        assertEquals("Hello", mObservable.get(2));
        assertEquals("End", mObservable.get(3));
        assertEquals(4, mNotifications.size());
        ListChange change = mNotifications.get(1);
        assertEquals(INSERT, change.change);
        assertEquals(0, change.start);
        assertEquals(1, change.count);
    }

    public void testAddAll() {
        ArrayList<String> toAdd = new ArrayList<>();
        toAdd.add("Hello");
        toAdd.add("World");
        mObservable.add("First");
        mObservable.addOnListChangedListener(mListener);
        mObservable.addAll(toAdd);
        assertEquals(3, mObservable.size());
        assertEquals("Hello", mObservable.get(1));
        assertEquals("World", mObservable.get(2));
        assertEquals(1, mNotifications.size());
        ListChange change = mNotifications.get(0);
        assertEquals(INSERT, change.change);
        assertEquals(1, change.start);
        assertEquals(2, change.count);
    }

    public void testInsertAll() {
        ArrayList<String> toAdd = new ArrayList<>();
        toAdd.add("Hello");
        toAdd.add("World");
        mObservable.add("First");
        mObservable.addOnListChangedListener(mListener);
        mObservable.addAll(0, toAdd);
        assertEquals(3, mObservable.size());
        assertEquals("Hello", mObservable.get(0));
        assertEquals("World", mObservable.get(1));
        assertEquals(1, mNotifications.size());
        ListChange change = mNotifications.get(0);
        assertEquals(INSERT, change.change);
        assertEquals(0, change.start);
        assertEquals(2, change.count);
    }

    public void testClear() {
        mObservable.add("Hello");
        mObservable.add("World");
        mObservable.addOnListChangedListener(mListener);
        mObservable.clear();
        assertEquals(1, mNotifications.size());
        ListChange change = mNotifications.get(0);
        assertEquals(REMOVE, change.change);
        assertEquals(0, change.start);
        assertEquals(2, change.count);

        mObservable.clear();
        // No notification when nothing is cleared.
        assertEquals(1, mNotifications.size());
    }

    public void testRemoveIndex() {
        mObservable.add("Hello");
        mObservable.add("World");
        mObservable.addOnListChangedListener(mListener);
        assertEquals("Hello", mObservable.remove(0));
        assertEquals(1, mNotifications.size());
        ListChange change = mNotifications.get(0);
        assertEquals(REMOVE, change.change);
        assertEquals(0, change.start);
        assertEquals(1, change.count);
    }

    public void testRemoveObject() {
        mObservable.add("Hello");
        mObservable.add("World");
        mObservable.addOnListChangedListener(mListener);
        assertTrue(mObservable.remove("Hello"));
        assertEquals(1, mNotifications.size());
        ListChange change = mNotifications.get(0);
        assertEquals(REMOVE, change.change);
        assertEquals(0, change.start);
        assertEquals(1, change.count);

        assertFalse(mObservable.remove("Hello"));
        // nothing removed, don't notify
        assertEquals(1, mNotifications.size());
    }

    public void testSet() {
        mObservable.add("Hello");
        mObservable.add("World");
        mObservable.addOnListChangedListener(mListener);
        assertEquals("Hello", mObservable.set(0, "Goodbye"));
        assertEquals("Goodbye", mObservable.get(0));
        assertEquals(2, mObservable.size());
        ListChange change = mNotifications.get(0);
        assertEquals(CHANGE, change.change);
        assertEquals(0, change.start);
        assertEquals(1, change.count);
    }
}
+213 −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.ObservableArrayMap;
import com.android.databinding.testapp.generated.BasicBindingBinder;

import android.binding.ObservableMap;
import android.binding.OnMapChangedListener;
import android.support.v4.util.ArrayMap;
import android.support.v4.util.SimpleArrayMap;

import java.util.ArrayList;
import java.util.Map;

public class ObservableArrayMapTest extends BaseDataBinderTest<BasicBindingBinder> {

    private ObservableArrayMap<String, String> mObservable;

    private ArrayList<String> mNotifications = new ArrayList<>();

    private OnMapChangedListener mListener = new OnMapChangedListener() {
        @Override
        public void onMapChanged(ObservableMap observableMap, Object o) {
            assertEquals(mObservable, observableMap);
            mNotifications.add((String) o);
        }
    };

    public ObservableArrayMapTest() {
        super(BasicBindingBinder.class, R.layout.basic_binding);
    }

    @Override
    protected void setUp() throws Exception {
        mNotifications.clear();
        mObservable = new ObservableArrayMap<>();
    }

    public void testAddListener() {
        mObservable.put("Hello", "World");
        assertTrue(mNotifications.isEmpty());
        mObservable.addOnMapChangedListener(mListener);
        mObservable.put("Hello", "Goodbye");
        assertFalse(mNotifications.isEmpty());
    }

    public void testRemoveListener() {
        // test there is no exception when the listener isn't there
        mObservable.removeOnMapChangedListener(mListener);

        mObservable.addOnMapChangedListener(mListener);
        mObservable.put("Hello", "World");
        mNotifications.clear();
        mObservable.removeOnMapChangedListener(mListener);
        mObservable.put("World", "Hello");
        assertTrue(mNotifications.isEmpty());

        // test there is no exception when the listener isn't there
        mObservable.removeOnMapChangedListener(mListener);
    }

    public void testClear() {
        mObservable.put("Hello", "World");
        mObservable.put("World", "Hello");
        mObservable.addOnMapChangedListener(mListener);
        mObservable.clear();
        assertEquals(1, mNotifications.size());
        assertNull(mNotifications.get(0));
        assertEquals(0, mObservable.size());
        assertTrue(mObservable.isEmpty());

        mObservable.clear();
        // No notification when nothing is cleared.
        assertEquals(1, mNotifications.size());
    }

    public void testPut() {
        mObservable.addOnMapChangedListener(mListener);
        mObservable.put("Hello", "World");
        assertEquals(1, mNotifications.size());
        assertEquals("Hello", mNotifications.get(0));
        assertEquals("World", mObservable.get("Hello"));

        mObservable.put("Hello", "World2");
        assertEquals(2, mNotifications.size());
        assertEquals("Hello", mNotifications.get(1));
        assertEquals("World2", mObservable.get("Hello"));

        mObservable.put("World", "Hello");
        assertEquals(3, mNotifications.size());
        assertEquals("World", mNotifications.get(2));
        assertEquals("Hello", mObservable.get("World"));
    }

    public void testPutAll() {
        Map<String, String> toAdd = new ArrayMap<>();
        toAdd.put("Hello", "World");
        toAdd.put("Goodbye", "Cruel World");
        mObservable.put("Cruel", "World");
        mObservable.addOnMapChangedListener(mListener);
        mObservable.putAll(toAdd);
        assertEquals(3, mObservable.size());
        assertEquals("World", mObservable.get("Hello"));
        assertEquals("Cruel World", mObservable.get("Goodbye"));
        assertEquals(2, mNotifications.size());
        // order is not guaranteed
        assertTrue(mNotifications.contains("Hello"));
        assertTrue(mNotifications.contains("Goodbye"));
    }

    public void testPutAllSimpleArrayMap() {
        SimpleArrayMap<String, String> toAdd = new ArrayMap<>();
        toAdd.put("Hello", "World");
        toAdd.put("Goodbye", "Cruel World");
        mObservable.put("Cruel", "World");
        mObservable.addOnMapChangedListener(mListener);
        mObservable.putAll(toAdd);
        assertEquals(3, mObservable.size());
        assertEquals("World", mObservable.get("Hello"));
        assertEquals("Cruel World", mObservable.get("Goodbye"));
        assertEquals(2, mNotifications.size());
        // order is not guaranteed
        assertTrue(mNotifications.contains("Hello"));
        assertTrue(mNotifications.contains("Goodbye"));
    }

    public void testRemove() {
        mObservable.put("Hello", "World");
        mObservable.put("Goodbye", "Cruel World");
        mObservable.addOnMapChangedListener(mListener);
        assertEquals("World", mObservable.remove("Hello"));
        assertEquals(1, mNotifications.size());
        assertEquals("Hello", mNotifications.get(0));

        assertNull(mObservable.remove("Hello"));
        // nothing removed, don't notify
        assertEquals(1, mNotifications.size());
    }

    public void testRemoveAll() {
        ArrayList<String> toRemove = new ArrayList<>();
        toRemove.add("Hello");
        toRemove.add("Goodbye");
        mObservable.put("Hello", "World");
        mObservable.put("Goodbye", "Cruel World");
        mObservable.put("Cruel", "World");
        mObservable.addOnMapChangedListener(mListener);
        assertTrue(mObservable.removeAll(toRemove));
        assertEquals(2, mNotifications.size());
        // order is not guaranteed
        assertTrue(mNotifications.contains("Hello"));
        assertTrue(mNotifications.contains("Goodbye"));

        assertTrue(mObservable.containsKey("Cruel"));

        // Test nothing removed
        assertFalse(mObservable.removeAll(toRemove));
        assertEquals(2, mNotifications.size());
    }

    public void testRetainAll() {
        ArrayList<String> toRetain = new ArrayList<>();
        toRetain.add("Hello");
        toRetain.add("Goodbye");
        mObservable.put("Hello", "World");
        mObservable.put("Goodbye", "Cruel World");
        mObservable.put("Cruel", "World");
        mObservable.addOnMapChangedListener(mListener);
        assertTrue(mObservable.retainAll(toRetain));
        assertEquals(1, mNotifications.size());
        assertEquals("Cruel", mNotifications.get(0));
        assertTrue(mObservable.containsKey("Hello"));
        assertTrue(mObservable.containsKey("Goodbye"));

        // Test nothing removed
        assertFalse(mObservable.retainAll(toRetain));
        assertEquals(1, mNotifications.size());
    }

    public void testRemoveAt() {
        mObservable.put("Hello", "World");
        mObservable.put("Goodbye", "Cruel World");
        mObservable.addOnMapChangedListener(mListener);
        String key = mObservable.keyAt(0);
        String value = mObservable.valueAt(0);
        assertTrue("Hello".equals(key) || "Goodbye".equals(key));
        assertEquals(value, mObservable.removeAt(0));
        assertEquals(1, mNotifications.size());
        assertEquals(key, mNotifications.get(0));
    }

    public void testSetValueAt() {
        mObservable.put("Hello", "World");
        mObservable.addOnMapChangedListener(mListener);
        assertEquals("World", mObservable.setValueAt(0, "Cruel World"));
        assertEquals(1, mNotifications.size());
        assertEquals("Hello", mNotifications.get(0));
    }
}
+6 −26
Original line number Diff line number Diff line
@@ -46,8 +46,12 @@ public class ObservableArrayMap<K, V> extends ArrayMap<K, V> implements Observab

    @Override
    public void clear() {
        boolean wasEmpty = isEmpty();
        if (!wasEmpty) {
            super.clear();
            notifyChange(null);
        }
    }

    public V put(K k, V v) {
        V val = super.put(k, v);
@@ -55,21 +59,6 @@ public class ObservableArrayMap<K, V> extends ArrayMap<K, V> implements Observab
        return v;
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> map) {
        super.putAll(map);
        for (K key : map.keySet()) {
            notifyChange(key);
        }
    }

    @Override
    public V remove(Object o) {
        V val = super.remove(o);
        notifyChange(o);
        return val;
    }

    @Override
    public boolean removeAll(Collection<?> collection) {
        boolean removed = false;
@@ -86,7 +75,7 @@ public class ObservableArrayMap<K, V> extends ArrayMap<K, V> implements Observab
    @Override
    public boolean retainAll(Collection<?> collection) {
        boolean removed = false;
        for (int i = size(); i >= 0; i--) {
        for (int i = size() - 1; i >= 0; i--) {
            Object key = keyAt(i);
            if (!collection.contains(key)) {
                removeAt(i);
@@ -96,15 +85,6 @@ public class ObservableArrayMap<K, V> extends ArrayMap<K, V> implements Observab
        return removed;
    }

    @Override
    public void putAll(SimpleArrayMap<? extends K, ? extends V> array) {
        super.putAll(array);
        for (int i = array.size(); i >= 0; i--) {
            K key = array.keyAt(i);
            notifyChange(key);
        }
    }

    @Override
    public V removeAt(int index) {
        K key = keyAt(index);