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 
17 package com.android.systemui.keyguard;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.testing.AndroidTestingRunner;
22 
23 import androidx.test.filters.SmallTest;
24 
25 import com.android.systemui.SysuiTestCase;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 import java.util.ArrayList;
32 
33 @RunWith(AndroidTestingRunner.class)
34 @SmallTest
35 public 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
setUp()44     public void setUp() throws Exception {
45         mLifecycle = new Lifecycle<>();
46         mDispatchedObjects = new ArrayList<>();
47     }
48 
49     @Test
addObserver_addsObserver()50     public void addObserver_addsObserver() throws Exception {
51         mLifecycle.addObserver(mObj1);
52 
53         mLifecycle.dispatch(mDispatchedObjects::add);
54         assertThat(mDispatchedObjects).contains(mObj1);
55     }
56 
57     @Test
removeObserver()58     public void removeObserver() throws Exception {
59         mLifecycle.addObserver(mObj1);
60         mLifecycle.removeObserver(mObj1);
61 
62         mLifecycle.dispatch(mDispatchedObjects::add);
63 
64         assertThat(mDispatchedObjects).isEmpty();
65     }
66 
67     @Test
dispatch()68     public void dispatch() throws Exception {
69         mLifecycle.addObserver(mObj1);
70         mLifecycle.addObserver(mObj2);
71 
72         mLifecycle.dispatch(mDispatchedObjects::add);
73 
74         assertThat(mDispatchedObjects).containsExactly(mObj1, mObj2);
75     }
76 
77 }