EventIdsTest.java revision 99a3d3102887d5b4c9c6713cb35d97b478088931
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 17package android.databinding.multimoduletestapp; 18 19import android.databinding.testlibrary.ObservableInLibrary; 20 21import android.databinding.Observable; 22import android.databinding.OnPropertyChangedListener; 23import android.os.Debug; 24import android.test.AndroidTestCase; 25 26import java.util.HashMap; 27import java.util.Map; 28 29import android.databinding.multimoduletestapp.BR; 30 31public class EventIdsTest extends AndroidTestCase { 32 public void testLibraryObservable() { 33 ObservableInLibrary observableInLibrary = new ObservableInLibrary(); 34 EventCounter ec = new EventCounter(); 35 observableInLibrary.addOnPropertyChangedListener(ec); 36 ec.assertProperty(BR.libField1, 0); 37 ec.assertProperty(BR.libField2, 0); 38 ec.assertProperty(BR.sharedField, 0); 39 40 observableInLibrary.setLibField1("a"); 41 ec.assertProperty(BR.libField1, 1); 42 ec.assertProperty(BR.libField2, 0); 43 ec.assertProperty(BR.sharedField, 0); 44 45 observableInLibrary.setLibField2("b"); 46 ec.assertProperty(BR.libField1, 1); 47 ec.assertProperty(BR.libField2, 1); 48 ec.assertProperty(BR.sharedField, 0); 49 50 observableInLibrary.setSharedField(3); 51 ec.assertProperty(BR.libField1, 1); 52 ec.assertProperty(BR.libField2, 1); 53 ec.assertProperty(BR.sharedField, 1); 54 } 55 56 public void testAppObservable() { 57 ObservableInMainApp observableInMainApp = new ObservableInMainApp(); 58 EventCounter ec = new EventCounter(); 59 observableInMainApp.addOnPropertyChangedListener(ec); 60 ec.assertProperty(BR.appField1, 0); 61 ec.assertProperty(BR.appField2, 0); 62 ec.assertProperty(BR.sharedField, 0); 63 64 observableInMainApp.setAppField2(3); 65 ec.assertProperty(BR.appField1, 0); 66 ec.assertProperty(BR.appField2, 1); 67 ec.assertProperty(BR.sharedField, 0); 68 69 observableInMainApp.setAppField1("b"); 70 ec.assertProperty(BR.appField1, 1); 71 ec.assertProperty(BR.appField2, 1); 72 ec.assertProperty(BR.sharedField, 0); 73 74 observableInMainApp.setSharedField(5); 75 ec.assertProperty(BR.appField1, 1); 76 ec.assertProperty(BR.appField2, 1); 77 ec.assertProperty(BR.sharedField, 1); 78 } 79 80 public void testExtendingObservable() { 81 ObservableExtendingLib observable = new ObservableExtendingLib(); 82 EventCounter ec = new EventCounter(); 83 observable.addOnPropertyChangedListener(ec); 84 85 ec.assertProperty(BR.childClassField, 0); 86 ec.assertProperty(BR.libField1, 0); 87 ec.assertProperty(BR.libField2, 0); 88 ec.assertProperty(BR.sharedField, 0); 89 90 observable.setChildClassField("a"); 91 ec.assertProperty(BR.childClassField, 1); 92 ec.assertProperty(BR.libField1, 0); 93 ec.assertProperty(BR.libField2, 0); 94 ec.assertProperty(BR.sharedField, 0); 95 96 observable.setLibField1("b"); 97 ec.assertProperty(BR.childClassField, 1); 98 ec.assertProperty(BR.libField1, 1); 99 ec.assertProperty(BR.libField2, 0); 100 ec.assertProperty(BR.sharedField, 0); 101 102 observable.setLibField2("c"); 103 ec.assertProperty(BR.childClassField, 1); 104 ec.assertProperty(BR.libField1, 1); 105 ec.assertProperty(BR.libField2, 1); 106 ec.assertProperty(BR.sharedField, 0); 107 108 observable.setSharedField(2); 109 ec.assertProperty(BR.childClassField, 1); 110 ec.assertProperty(BR.libField1, 1); 111 ec.assertProperty(BR.libField2, 1); 112 ec.assertProperty(BR.sharedField, 1); 113 } 114 115 private static class EventCounter implements OnPropertyChangedListener { 116 Map<Integer, Integer> mCounter = new HashMap<>(); 117 118 @Override 119 public void onPropertyChanged(Observable observable, int propertyId) { 120 mCounter.put(propertyId, get(propertyId) + 1); 121 } 122 123 public int get(int propertyId) { 124 Integer val = mCounter.get(propertyId); 125 return val == null ? 0 : val; 126 } 127 128 private void assertProperty(int propertyId, int value) { 129 assertEquals(get(propertyId), value); 130 } 131 } 132} 133