| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
package android.databinding.testapp; |
| 17 |
|
| 18 |
import android.databinding.ObservableArrayMap; |
| 19 |
import android.databinding.testapp.databinding.BasicBindingBinding; |
| 20 |
|
| 21 |
import android.databinding.ObservableMap; |
| 22 |
import android.databinding.ObservableMap.OnMapChangedCallback; |
| 23 |
import android.support.v4.util.ArrayMap; |
| 24 |
import android.support.v4.util.SimpleArrayMap; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.Map; |
| 28 |
|
| 29 |
public class ObservableArrayMapTest extends BaseDataBinderTest<BasicBindingBinding> { |
| 30 |
|
| 31 |
private ObservableArrayMap<String, String> mObservable; |
| 32 |
|
| 33 |
private ArrayList<String> mNotifications = new ArrayList<>(); |
| 34 |
|
| 35 |
private OnMapChangedCallback mListener = new OnMapChangedCallback() { |
| 36 |
@Override |
| 37 |
public void onMapChanged(ObservableMap observableMap, Object o) { |
| 38 |
assertEquals(mObservable, observableMap); |
| 39 |
mNotifications.add((String) o); |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
public ObservableArrayMapTest() { |
| 44 |
super(BasicBindingBinding.class); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
protected void setUp() throws Exception { |
| 49 |
mNotifications.clear(); |
| 50 |
mObservable = new ObservableArrayMap<>(); |
| 51 |
} |
| 52 |
|
| 53 |
public void testAddListener() { |
| 54 |
mObservable.put("Hello", "World"); |
| 55 |
assertTrue(mNotifications.isEmpty()); |
| 56 |
mObservable.addOnMapChangedCallback(mListener); |
| 57 |
mObservable.put("Hello", "Goodbye"); |
| 58 |
assertFalse(mNotifications.isEmpty()); |
| 59 |
} |
| 60 |
|
| 61 |
public void testRemoveListener() { |
| 62 |
// test there is no exception when the listener isn't there |
| 63 |
mObservable.removeOnMapChangedCallback(mListener); |
| 64 |
|
| 65 |
mObservable.addOnMapChangedCallback(mListener); |
| 66 |
mObservable.put("Hello", "World"); |
| 67 |
mNotifications.clear(); |
| 68 |
mObservable.removeOnMapChangedCallback(mListener); |
| 69 |
mObservable.put("World", "Hello"); |
| 70 |
assertTrue(mNotifications.isEmpty()); |
| 71 |
|
| 72 |
// test there is no exception when the listener isn't there |
| 73 |
mObservable.removeOnMapChangedCallback(mListener); |
| 74 |
} |
| 75 |
|
| 76 |
public void testClear() { |
| 77 |
mObservable.put("Hello", "World"); |
| 78 |
mObservable.put("World", "Hello"); |
| 79 |
mObservable.addOnMapChangedCallback(mListener); |
| 80 |
mObservable.clear(); |
| 81 |
assertEquals(1, mNotifications.size()); |
| 82 |
assertNull(mNotifications.get(0)); |
| 83 |
assertEquals(0, mObservable.size()); |
| 84 |
assertTrue(mObservable.isEmpty()); |
| 85 |
|
| 86 |
mObservable.clear(); |
| 87 |
// No notification when nothing is cleared. |
| 88 |
assertEquals(1, mNotifications.size()); |
| 89 |
} |
| 90 |
|
| 91 |
public void testPut() { |
| 92 |
mObservable.addOnMapChangedCallback(mListener); |
| 93 |
mObservable.put("Hello", "World"); |
| 94 |
assertEquals(1, mNotifications.size()); |
| 95 |
assertEquals("Hello", mNotifications.get(0)); |
| 96 |
assertEquals("World", mObservable.get("Hello")); |
| 97 |
|
| 98 |
mObservable.put("Hello", "World2"); |
| 99 |
assertEquals(2, mNotifications.size()); |
| 100 |
assertEquals("Hello", mNotifications.get(1)); |
| 101 |
assertEquals("World2", mObservable.get("Hello")); |
| 102 |
|
| 103 |
mObservable.put("World", "Hello"); |
| 104 |
assertEquals(3, mNotifications.size()); |
| 105 |
assertEquals("World", mNotifications.get(2)); |
| 106 |
assertEquals("Hello", mObservable.get("World")); |
| 107 |
} |
| 108 |
|
| 109 |
public void testPutAll() { |
| 110 |
Map<String, String> toAdd = new ArrayMap<>(); |
| 111 |
toAdd.put("Hello", "World"); |
| 112 |
toAdd.put("Goodbye", "Cruel World"); |
| 113 |
mObservable.put("Cruel", "World"); |
| 114 |
mObservable.addOnMapChangedCallback(mListener); |
| 115 |
mObservable.putAll(toAdd); |
| 116 |
assertEquals(3, mObservable.size()); |
| 117 |
assertEquals("World", mObservable.get("Hello")); |
| 118 |
assertEquals("Cruel World", mObservable.get("Goodbye")); |
| 119 |
assertEquals(2, mNotifications.size()); |
| 120 |
// order is not guaranteed |
| 121 |
assertTrue(mNotifications.contains("Hello")); |
| 122 |
assertTrue(mNotifications.contains("Goodbye")); |
| 123 |
} |
| 124 |
|
| 125 |
public void testPutAllSimpleArrayMap() { |
| 126 |
SimpleArrayMap<String, String> toAdd = new ArrayMap<>(); |
| 127 |
toAdd.put("Hello", "World"); |
| 128 |
toAdd.put("Goodbye", "Cruel World"); |
| 129 |
mObservable.put("Cruel", "World"); |
| 130 |
mObservable.addOnMapChangedCallback(mListener); |
| 131 |
mObservable.putAll(toAdd); |
| 132 |
assertEquals(3, mObservable.size()); |
| 133 |
assertEquals("World", mObservable.get("Hello")); |
| 134 |
assertEquals("Cruel World", mObservable.get("Goodbye")); |
| 135 |
assertEquals(2, mNotifications.size()); |
| 136 |
// order is not guaranteed |
| 137 |
assertTrue(mNotifications.contains("Hello")); |
| 138 |
assertTrue(mNotifications.contains("Goodbye")); |
| 139 |
} |
| 140 |
|
| 141 |
public void testRemove() { |
| 142 |
mObservable.put("Hello", "World"); |
| 143 |
mObservable.put("Goodbye", "Cruel World"); |
| 144 |
mObservable.addOnMapChangedCallback(mListener); |
| 145 |
assertEquals("World", mObservable.remove("Hello")); |
| 146 |
assertEquals(1, mNotifications.size()); |
| 147 |
assertEquals("Hello", mNotifications.get(0)); |
| 148 |
|
| 149 |
assertNull(mObservable.remove("Hello")); |
| 150 |
// nothing removed, don't notify |
| 151 |
assertEquals(1, mNotifications.size()); |
| 152 |
} |
| 153 |
|
| 154 |
public void testRemoveAll() { |
| 155 |
ArrayList<String> toRemove = new ArrayList<>(); |
| 156 |
toRemove.add("Hello"); |
| 157 |
toRemove.add("Goodbye"); |
| 158 |
mObservable.put("Hello", "World"); |
| 159 |
mObservable.put("Goodbye", "Cruel World"); |
| 160 |
mObservable.put("Cruel", "World"); |
| 161 |
mObservable.addOnMapChangedCallback(mListener); |
| 162 |
assertTrue(mObservable.removeAll(toRemove)); |
| 163 |
assertEquals(2, mNotifications.size()); |
| 164 |
// order is not guaranteed |
| 165 |
assertTrue(mNotifications.contains("Hello")); |
| 166 |
assertTrue(mNotifications.contains("Goodbye")); |
| 167 |
|
| 168 |
assertTrue(mObservable.containsKey("Cruel")); |
| 169 |
|
| 170 |
// Test nothing removed |
| 171 |
assertFalse(mObservable.removeAll(toRemove)); |
| 172 |
assertEquals(2, mNotifications.size()); |
| 173 |
} |
| 174 |
|
| 175 |
public void testRetainAll() { |
| 176 |
ArrayList<String> toRetain = new ArrayList<>(); |
| 177 |
toRetain.add("Hello"); |
| 178 |
toRetain.add("Goodbye"); |
| 179 |
mObservable.put("Hello", "World"); |
| 180 |
mObservable.put("Goodbye", "Cruel World"); |
| 181 |
mObservable.put("Cruel", "World"); |
| 182 |
mObservable.addOnMapChangedCallback(mListener); |
| 183 |
assertTrue(mObservable.retainAll(toRetain)); |
| 184 |
assertEquals(1, mNotifications.size()); |
| 185 |
assertEquals("Cruel", mNotifications.get(0)); |
| 186 |
assertTrue(mObservable.containsKey("Hello")); |
| 187 |
assertTrue(mObservable.containsKey("Goodbye")); |
| 188 |
|
| 189 |
// Test nothing removed |
| 190 |
assertFalse(mObservable.retainAll(toRetain)); |
| 191 |
assertEquals(1, mNotifications.size()); |
| 192 |
} |
| 193 |
|
| 194 |
public void testRemoveAt() { |
| 195 |
mObservable.put("Hello", "World"); |
| 196 |
mObservable.put("Goodbye", "Cruel World"); |
| 197 |
mObservable.addOnMapChangedCallback(mListener); |
| 198 |
String key = mObservable.keyAt(0); |
| 199 |
String value = mObservable.valueAt(0); |
| 200 |
assertTrue("Hello".equals(key) || "Goodbye".equals(key)); |
| 201 |
assertEquals(value, mObservable.removeAt(0)); |
| 202 |
assertEquals(1, mNotifications.size()); |
| 203 |
assertEquals(key, mNotifications.get(0)); |
| 204 |
} |
| 205 |
|
| 206 |
public void testSetValueAt() { |
| 207 |
mObservable.put("Hello", "World"); |
| 208 |
mObservable.addOnMapChangedCallback(mListener); |
| 209 |
assertEquals("World", mObservable.setValueAt(0, "Cruel World")); |
| 210 |
assertEquals(1, mNotifications.size()); |
| 211 |
assertEquals("Hello", mNotifications.get(0)); |
| 212 |
} |
| 213 |
} |