1 /*
2  * Copyright (C) 2018 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.statusbar.policy;
18 
19 import static org.mockito.ArgumentMatchers.eq;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 
23 import android.testing.AndroidTestingRunner;
24 import android.testing.TestableLooper.RunWithLooper;
25 
26 import androidx.annotation.NonNull;
27 import androidx.lifecycle.Lifecycle;
28 import androidx.lifecycle.Lifecycle.Event;
29 import androidx.lifecycle.LifecycleEventObserver;
30 import androidx.lifecycle.LifecycleOwner;
31 import androidx.lifecycle.LifecycleRegistry;
32 import androidx.test.filters.SmallTest;
33 
34 import com.android.systemui.SysuiTestCase;
35 
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.ArgumentCaptor;
39 
40 @RunWithLooper(setAsMainLooper = true)
41 @RunWith(AndroidTestingRunner.class)
42 @SmallTest
43 public class CallbackControllerTest extends SysuiTestCase {
44 
45     @Test
testAddCallback()46     public void testAddCallback() {
47         Lifecycle lifecycle = mock(Lifecycle.class);
48         LifecycleOwner owner = () -> lifecycle;
49         Object callback = new Object();
50         Controller controller = mock(Controller.class);
51 
52         // observe and get the lifecycle observer that gets registered.
53         ArgumentCaptor<LifecycleEventObserver> observer =
54                 ArgumentCaptor.forClass(LifecycleEventObserver.class);
55         controller.observe(owner, callback);
56         verify(lifecycle).addObserver(observer.capture());
57 
58         // move to resume state and make sure the callback gets registered.
59         observer.getValue().onStateChanged(owner, Event.ON_RESUME);
60         verify(controller).addCallback(eq(callback));
61     }
62 
63     @Test
testRemoveCallback()64     public void testRemoveCallback() {
65         Lifecycle lifecycle = mock(Lifecycle.class);
66         LifecycleOwner owner = () -> lifecycle;
67         Object callback = new Object();
68         Controller controller = mock(Controller.class);
69 
70         // observe and get the lifecycle observer that gets registered.
71         ArgumentCaptor<LifecycleEventObserver> observer =
72                 ArgumentCaptor.forClass(LifecycleEventObserver.class);
73         controller.observe(owner, callback);
74         verify(lifecycle).addObserver(observer.capture());
75 
76         // move to pause state and make sure the callback gets unregistered.
77         observer.getValue().onStateChanged(owner, Event.ON_PAUSE);
78         verify(controller).removeCallback(eq(callback));
79     }
80 
81     @Test
testCallbackIsRemovedOnDestroy()82     public void testCallbackIsRemovedOnDestroy() {
83         SimpleLifecycleOwner owner = new SimpleLifecycleOwner();
84 
85         Object callback = new Object();
86         Controller controller = mock(Controller.class);
87         controller.observe(owner, callback);
88 
89         owner.setState(Lifecycle.State.RESUMED);
90         verify(controller).addCallback(callback);
91 
92         owner.setState(Lifecycle.State.DESTROYED);
93         verify(controller).removeCallback(callback);
94     }
95 
96     private static class SimpleLifecycleOwner implements LifecycleOwner {
97         LifecycleRegistry mLifecycle = new LifecycleRegistry(this);
98         @NonNull
99         @Override
getLifecycle()100         public Lifecycle getLifecycle() {
101             return mLifecycle;
102         }
103 
setState(Lifecycle.State state)104         public void setState(Lifecycle.State state) {
105             mLifecycle.setCurrentState(state);
106         }
107     }
108 
109     private static class Controller implements CallbackController<Object> {
110         @Override
addCallback(Object listener)111         public void addCallback(Object listener) {
112         }
113 
114         @Override
removeCallback(Object listener)115         public void removeCallback(Object listener) {
116         }
117     }
118 }
119