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