Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 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 | |
Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 23 | import android.testing.AndroidTestingRunner; |
| 24 | import android.testing.TestableLooper.RunWithLooper; |
| 25 | |
| 26 | import androidx.lifecycle.Lifecycle; |
| 27 | import androidx.lifecycle.Lifecycle.Event; |
| 28 | import androidx.lifecycle.LifecycleEventObserver; |
| 29 | import androidx.lifecycle.LifecycleOwner; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 30 | import androidx.test.filters.SmallTest; |
Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 31 | |
| 32 | import com.android.systemui.SysuiTestCase; |
| 33 | |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.mockito.ArgumentCaptor; |
| 37 | |
| 38 | @RunWithLooper |
| 39 | @RunWith(AndroidTestingRunner.class) |
| 40 | @SmallTest |
| 41 | public 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 | } |