blob: fa711f1cf9a2e751b33c47f779c9203ddb07a8ab [file] [log] [blame]
Jason Monk8e4e4cb2018-12-04 11:10:24 -05001/*
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
17package com.android.systemui.statusbar.policy;
18
19import static org.mockito.ArgumentMatchers.eq;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.verify;
22
Jason Monk8e4e4cb2018-12-04 11:10:24 -050023import android.testing.AndroidTestingRunner;
24import android.testing.TestableLooper.RunWithLooper;
25
26import androidx.lifecycle.Lifecycle;
27import androidx.lifecycle.Lifecycle.Event;
28import androidx.lifecycle.LifecycleEventObserver;
29import androidx.lifecycle.LifecycleOwner;
Brett Chabot84151d92019-02-27 15:37:59 -080030import androidx.test.filters.SmallTest;
Jason Monk8e4e4cb2018-12-04 11:10:24 -050031
32import com.android.systemui.SysuiTestCase;
33
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.mockito.ArgumentCaptor;
37
38@RunWithLooper
39@RunWith(AndroidTestingRunner.class)
40@SmallTest
41public class CallbackControllerTest extends SysuiTestCase {
42
43 @Test
44 public void testAddCallback() {
45 Lifecycle lifecycle = mock(Lifecycle.class);
46 LifecycleOwner owner = () -> lifecycle;
47 Object callback = new Object();
48 Controller controller = mock(Controller.class);
49
50 // observe and get the lifecycle observer that gets registered.
51 ArgumentCaptor<LifecycleEventObserver> observer =
52 ArgumentCaptor.forClass(LifecycleEventObserver.class);
53 controller.observe(owner, callback);
54 verify(lifecycle).addObserver(observer.capture());
55
56 // move to resume state and make sure the callback gets registered.
57 observer.getValue().onStateChanged(owner, Event.ON_RESUME);
58 verify(controller).addCallback(eq(callback));
59 }
60
61 @Test
62 public void testRemoveCallback() {
63 Lifecycle lifecycle = mock(Lifecycle.class);
64 LifecycleOwner owner = () -> lifecycle;
65 Object callback = new Object();
66 Controller controller = mock(Controller.class);
67
68 // observe and get the lifecycle observer that gets registered.
69 ArgumentCaptor<LifecycleEventObserver> observer =
70 ArgumentCaptor.forClass(LifecycleEventObserver.class);
71 controller.observe(owner, callback);
72 verify(lifecycle).addObserver(observer.capture());
73
74 // move to pause state and make sure the callback gets unregistered.
75 observer.getValue().onStateChanged(owner, Event.ON_PAUSE);
76 verify(controller).removeCallback(eq(callback));
77 }
78
79 private static class Controller implements CallbackController<Object> {
80 @Override
81 public void addCallback(Object listener) {
82 }
83
84 @Override
85 public void removeCallback(Object listener) {
86 }
87 }
88}