1 /*
2  * Copyright (C) 2022 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 package com.android.systemui.dreams
17 
18 import android.testing.AndroidTestingRunner
19 import androidx.test.filters.SmallTest
20 import com.android.systemui.SysuiTestCase
21 import com.google.common.truth.Truth.assertThat
22 import org.junit.Before
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 import org.mockito.Mock
26 import org.mockito.Mockito.never
27 import org.mockito.Mockito.reset
28 import org.mockito.Mockito.times
29 import org.mockito.Mockito.verify
30 import org.mockito.MockitoAnnotations
31 
32 @SmallTest
33 @RunWith(AndroidTestingRunner::class)
34 class DreamOverlayCallbackControllerTest : SysuiTestCase() {
35 
36     @Mock private lateinit var callback: DreamOverlayCallbackController.Callback
37 
38     private lateinit var underTest: DreamOverlayCallbackController
39 
40     @Before
41     fun setUp() {
42         MockitoAnnotations.initMocks(this)
43         underTest = DreamOverlayCallbackController()
44     }
45 
46     @Test
47     fun onWakeUpInvokesCallback() {
48         underTest.onStartDream()
49         assertThat(underTest.isDreaming).isEqualTo(true)
50 
51         underTest.addCallback(callback)
52         underTest.onWakeUp()
53         verify(callback).onWakeUp()
54         assertThat(underTest.isDreaming).isEqualTo(false)
55 
56         // Adding twice should not invoke twice
57         reset(callback)
58         underTest.addCallback(callback)
59         underTest.onWakeUp()
60         verify(callback, times(1)).onWakeUp()
61 
62         // After remove, no call to callback
63         reset(callback)
64         underTest.removeCallback(callback)
65         underTest.onWakeUp()
66         verify(callback, never()).onWakeUp()
67     }
68 
69     @Test
70     fun onStartDreamInvokesCallback() {
71         underTest.addCallback(callback)
72 
73         assertThat(underTest.isDreaming).isEqualTo(false)
74 
75         underTest.onStartDream()
76         verify(callback).onStartDream()
77         assertThat(underTest.isDreaming).isEqualTo(true)
78 
79         // Adding twice should not invoke twice
80         reset(callback)
81         underTest.addCallback(callback)
82         underTest.onStartDream()
83         verify(callback, times(1)).onStartDream()
84 
85         // After remove, no call to callback
86         reset(callback)
87         underTest.removeCallback(callback)
88         underTest.onStartDream()
89         verify(callback, never()).onStartDream()
90     }
91 }
92