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