1/*
2 * Copyright (C) 2017 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 com.android.systemui.keyguard;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21
22import android.support.test.filters.SmallTest;
23import android.testing.AndroidTestingRunner;
24
25import com.android.systemui.SysuiTestCase;
26
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30
31import java.util.ArrayList;
32
33@RunWith(AndroidTestingRunner.class)
34@SmallTest
35public class LifecycleTest extends SysuiTestCase {
36
37    private final Object mObj1 = new Object();
38    private final Object mObj2 = new Object();
39
40    private Lifecycle<Object> mLifecycle;
41    private ArrayList<Object> mDispatchedObjects;
42
43    @Before
44    public void setUp() throws Exception {
45        mLifecycle = new Lifecycle<>();
46        mDispatchedObjects = new ArrayList<>();
47    }
48
49    @Test
50    public void addObserver_addsObserver() throws Exception {
51        mLifecycle.addObserver(mObj1);
52
53        mLifecycle.dispatch(mDispatchedObjects::add);
54
55        assertTrue(mDispatchedObjects.contains(mObj1));
56    }
57
58    @Test
59    public void removeObserver() throws Exception {
60        mLifecycle.addObserver(mObj1);
61        mLifecycle.removeObserver(mObj1);
62
63        mLifecycle.dispatch(mDispatchedObjects::add);
64
65        assertFalse(mDispatchedObjects.contains(mObj1));
66    }
67
68    @Test
69    public void dispatch() throws Exception {
70        mLifecycle.addObserver(mObj1);
71        mLifecycle.addObserver(mObj2);
72
73        mLifecycle.dispatch(mDispatchedObjects::add);
74
75        assertTrue(mDispatchedObjects.contains(mObj1));
76        assertTrue(mDispatchedObjects.contains(mObj2));
77    }
78
79}