1 /*
2  * Copyright (C) 2020 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.controls.controller
18 
19 import android.os.RemoteException
20 import android.service.controls.IControlsActionCallback
21 import android.service.controls.IControlsProvider
22 import android.service.controls.IControlsSubscriber
23 import android.service.controls.IControlsSubscription
24 import android.service.controls.actions.ControlAction
25 import android.service.controls.actions.ControlActionWrapper
26 import android.testing.AndroidTestingRunner
27 import androidx.test.filters.SmallTest
28 import com.android.systemui.SysuiTestCase
29 import org.junit.Assert.assertEquals
30 import org.junit.Assert.assertFalse
31 import org.junit.Assert.assertTrue
32 import org.junit.Before
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 import org.mockito.ArgumentCaptor
36 import org.mockito.ArgumentMatchers.eq
37 import org.mockito.Captor
38 import org.mockito.Mock
39 import org.mockito.Mockito.any
40 import org.mockito.Mockito.`when`
41 import org.mockito.Mockito.verify
42 import org.mockito.MockitoAnnotations
43 
44 @SmallTest
45 @RunWith(AndroidTestingRunner::class)
46 class ServiceWrapperTest : SysuiTestCase() {
47 
48     @Mock
49     private lateinit var service: IControlsProvider
50 
51     @Mock
52     private lateinit var subscription: IControlsSubscription
53 
54     @Mock
55     private lateinit var subscriber: IControlsSubscriber
56 
57     @Mock
58     private lateinit var actionCallback: IControlsActionCallback
59 
60     @Captor
61     private lateinit var wrapperCaptor: ArgumentCaptor<ControlActionWrapper>
62 
63     private val exception = RemoteException()
64 
65     private lateinit var wrapper: ServiceWrapper
66 
67     companion object {
68         fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture()
69     }
70 
71     @Before
72     fun setUp() {
73         MockitoAnnotations.initMocks(this)
74 
75         wrapper = ServiceWrapper(service)
76     }
77 
78     @Test
79     fun testLoad_happyPath() {
80         val result = wrapper.load(subscriber)
81 
82         assertTrue(result)
83         verify(service).load(subscriber)
84     }
85 
86     @Test
87     fun testLoad_error() {
88         `when`(service.load(any())).thenThrow(exception)
89         val result = wrapper.load(subscriber)
90 
91         assertFalse(result)
92     }
93 
94     @Test
95     fun testLoadSuggested_happyPath() {
96         val result = wrapper.loadSuggested(subscriber)
97 
98         assertTrue(result)
99         verify(service).loadSuggested(subscriber)
100     }
101 
102     @Test
103     fun testLoadSuggested_error() {
104         `when`(service.loadSuggested(any())).thenThrow(exception)
105         val result = wrapper.loadSuggested(subscriber)
106 
107         assertFalse(result)
108     }
109 
110     @Test
111     fun testSubscribe_happyPath() {
112         val list = listOf("TEST_ID")
113         val result = wrapper.subscribe(list, subscriber)
114 
115         assertTrue(result)
116         verify(service).subscribe(list, subscriber)
117     }
118 
119     @Test
120     fun testSubscribe_error() {
121         `when`(service.subscribe(any(), any())).thenThrow(exception)
122 
123         val list = listOf("TEST_ID")
124         val result = wrapper.subscribe(list, subscriber)
125 
126         assertFalse(result)
127     }
128 
129     @Test
130     fun testCancel_happyPath() {
131         val result = wrapper.cancel(subscription)
132 
133         assertTrue(result)
134         verify(subscription).cancel()
135     }
136 
137     @Test
138     fun testCancel_error() {
139         `when`(subscription.cancel()).thenThrow(exception)
140         val result = wrapper.cancel(subscription)
141 
142         assertFalse(result)
143     }
144 
145     @Test
146     fun testOnAction_happyPath() {
147         val id = "TEST_ID"
148         val action = ControlAction.ERROR_ACTION
149 
150         val result = wrapper.action(id, action, actionCallback)
151 
152         assertTrue(result)
153         verify(service).action(eq(id), capture(wrapperCaptor),
154                 eq(actionCallback))
155         assertEquals(action, wrapperCaptor.getValue().getWrappedAction())
156     }
157 
158     @Test
159     fun testOnAction_error() {
160         `when`(service.action(any(), any(), any())).thenThrow(exception)
161 
162         val id = "TEST_ID"
163         val action = ControlAction.ERROR_ACTION
164 
165         val result = wrapper.action(id, action, actionCallback)
166 
167         assertFalse(result)
168     }
169 }
170