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 */
16package android.databinding.testapp;
17
18import android.databinding.BaseObservable;
19import android.databinding.testapp.databinding.BasicBindingBinding;
20
21import android.databinding.Observable;
22import android.databinding.Observable.OnPropertyChangedCallback;
23
24import java.util.ArrayList;
25
26public class BaseObservableTest extends BaseDataBinderTest<BasicBindingBinding> {
27    private BaseObservable mObservable;
28    private ArrayList<Integer> mNotifications = new ArrayList<>();
29    private OnPropertyChangedCallback mCallback = new OnPropertyChangedCallback() {
30        @Override
31        public void onPropertyChanged(Observable observable, int i) {
32            assertEquals(mObservable, observable);
33            mNotifications.add(i);
34        }
35    };
36
37    public BaseObservableTest() {
38        super(BasicBindingBinding.class);
39    }
40
41    @Override
42    protected void setUp() throws Exception {
43        mNotifications.clear();
44        mObservable = new BaseObservable();
45        initBinder(null);
46    }
47
48    public void testAddCallback() {
49        mObservable.notifyChange();
50        assertTrue(mNotifications.isEmpty());
51        mObservable.addOnPropertyChangedCallback(mCallback);
52        mObservable.notifyChange();
53        assertFalse(mNotifications.isEmpty());
54    }
55
56    public void testRemoveCallback() {
57        // test there is no exception when the Callback isn't there
58        mObservable.removeOnPropertyChangedCallback(mCallback);
59
60        mObservable.addOnPropertyChangedCallback(mCallback);
61        mObservable.notifyChange();
62        mNotifications.clear();
63        mObservable.removeOnPropertyChangedCallback(mCallback);
64        mObservable.notifyChange();
65        assertTrue(mNotifications.isEmpty());
66
67        // test there is no exception when the Callback isn't there
68        mObservable.removeOnPropertyChangedCallback(mCallback);
69    }
70
71    public void testNotifyChange() {
72        mObservable.addOnPropertyChangedCallback(mCallback);
73        mObservable.notifyChange();
74        assertEquals(1, mNotifications.size());
75        assertEquals(0, (int) mNotifications.get(0));
76    }
77
78    public void testNotifyPropertyChanged() {
79        final int expectedId = 100;
80        mObservable.addOnPropertyChangedCallback(mCallback);
81        mObservable.notifyPropertyChanged(expectedId);
82        assertEquals(1, mNotifications.size());
83        assertEquals(expectedId, (int) mNotifications.get(0));
84    }
85}
86