summaryrefslogtreecommitdiff
path: root/integration-tests/MultiModuleTestApp/app/src/androidTest/java/com/android/databinding/multimoduletestapp/EventIdsTest.java
blob: 9a04368cdaff9cafb62ff0ba74760c01dab3419c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * 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 android.databinding.multimoduletestapp;

import android.databinding.testlibrary.ObservableInLibrary;

import android.databinding.Observable;
import android.databinding.Observable.OnPropertyChangedCallback;
import android.os.Debug;
import android.test.AndroidTestCase;

import java.util.HashMap;
import java.util.Map;

import android.databinding.multimoduletestapp.BR;

public class EventIdsTest extends AndroidTestCase {
    public void testLibraryObservable() {
        ObservableInLibrary observableInLibrary = new ObservableInLibrary();
        EventCounter ec = new EventCounter();
        observableInLibrary.addOnPropertyChangedCallback(ec);
        ec.assertProperty(BR.libField1, 0);
        ec.assertProperty(BR.libField2, 0);
        ec.assertProperty(BR.sharedField, 0);

        observableInLibrary.setLibField1("a");
        ec.assertProperty(BR.libField1, 1);
        ec.assertProperty(BR.libField2, 0);
        ec.assertProperty(BR.sharedField, 0);

        observableInLibrary.setLibField2("b");
        ec.assertProperty(BR.libField1, 1);
        ec.assertProperty(BR.libField2, 1);
        ec.assertProperty(BR.sharedField, 0);

        observableInLibrary.setSharedField(3);
        ec.assertProperty(BR.libField1, 1);
        ec.assertProperty(BR.libField2, 1);
        ec.assertProperty(BR.sharedField, 1);
    }

    public void testAppObservable() {
        ObservableInMainApp observableInMainApp = new ObservableInMainApp();
        EventCounter ec = new EventCounter();
        observableInMainApp.addOnPropertyChangedCallback(ec);
        ec.assertProperty(BR.appField1, 0);
        ec.assertProperty(BR.appField2, 0);
        ec.assertProperty(BR.sharedField, 0);

        observableInMainApp.setAppField2(3);
        ec.assertProperty(BR.appField1, 0);
        ec.assertProperty(BR.appField2, 1);
        ec.assertProperty(BR.sharedField, 0);

        observableInMainApp.setAppField1("b");
        ec.assertProperty(BR.appField1, 1);
        ec.assertProperty(BR.appField2, 1);
        ec.assertProperty(BR.sharedField, 0);

        observableInMainApp.setSharedField(5);
        ec.assertProperty(BR.appField1, 1);
        ec.assertProperty(BR.appField2, 1);
        ec.assertProperty(BR.sharedField, 1);
    }

    public void testExtendingObservable() {
        ObservableExtendingLib observable = new ObservableExtendingLib();
        EventCounter ec = new EventCounter();
        observable.addOnPropertyChangedCallback(ec);

        ec.assertProperty(BR.childClassField, 0);
        ec.assertProperty(BR.libField1, 0);
        ec.assertProperty(BR.libField2, 0);
        ec.assertProperty(BR.sharedField, 0);

        observable.setChildClassField("a");
        ec.assertProperty(BR.childClassField, 1);
        ec.assertProperty(BR.libField1, 0);
        ec.assertProperty(BR.libField2, 0);
        ec.assertProperty(BR.sharedField, 0);

        observable.setLibField1("b");
        ec.assertProperty(BR.childClassField, 1);
        ec.assertProperty(BR.libField1, 1);
        ec.assertProperty(BR.libField2, 0);
        ec.assertProperty(BR.sharedField, 0);

        observable.setLibField2("c");
        ec.assertProperty(BR.childClassField, 1);
        ec.assertProperty(BR.libField1, 1);
        ec.assertProperty(BR.libField2, 1);
        ec.assertProperty(BR.sharedField, 0);

        observable.setSharedField(2);
        ec.assertProperty(BR.childClassField, 1);
        ec.assertProperty(BR.libField1, 1);
        ec.assertProperty(BR.libField2, 1);
        ec.assertProperty(BR.sharedField, 1);
    }

    private static class EventCounter extends OnPropertyChangedCallback {
        Map<Integer, Integer> mCounter = new HashMap<>();

        @Override
        public void onPropertyChanged(Observable observable, int propertyId) {
            mCounter.put(propertyId, get(propertyId) + 1);
        }

        public int get(int propertyId) {
            Integer val = mCounter.get(propertyId);
            return val == null ? 0 : val;
        }

        private void assertProperty(int propertyId, int value) {
            assertEquals(get(propertyId), value);
        }
    }
}