1 /*
2  * Copyright (C) 2019 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.settings.notification;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.Mockito.RETURNS_SMART_NULLS;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 
31 import android.content.ComponentName;
32 import android.content.Context;
33 import android.content.pm.PackageManager;
34 import android.content.pm.ServiceInfo;
35 
36 import com.android.settingslib.widget.CandidateInfo;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.mockito.invocation.InvocationOnMock;
44 import org.robolectric.RobolectricTestRunner;
45 
46 import java.util.ArrayList;
47 import java.util.List;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class NotificationAssistantPickerTest {
51 
52     private static final String TEST_PKG = "test.package";
53     private static final String TEST_SRV = "test.component";
54     private static final String TEST_CMP = TEST_PKG + "/" + TEST_SRV;
55     private static final String TEST_NAME = "Test name";
56     private static final ComponentName TEST_COMPONENT = ComponentName.unflattenFromString(TEST_CMP);
57     private NotificationAssistantPicker mFragment;
58     @Mock
59     private Context mContext;
60     @Mock
61     private PackageManager mPackageManager;
62     @Mock
63     private NotificationBackend mNotificationBackend;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         when(mContext.getPackageManager()).thenReturn(mPackageManager);
69         mFragment = new TestNotificationAssistantPicker(mContext, mPackageManager,
70                 mNotificationBackend);
71     }
72 
73     @Test
getCurrentAssistant()74     public void getCurrentAssistant() {
75         when(mNotificationBackend.getAllowedNotificationAssistant()).thenReturn(TEST_COMPONENT);
76         String key = mFragment.getDefaultKey();
77         assertEquals(key, TEST_CMP);
78     }
79 
80     @Test
getCurrentAssistant_None()81     public void getCurrentAssistant_None() {
82         when(mNotificationBackend.getAllowedNotificationAssistant()).thenReturn(null);
83         String key = mFragment.getDefaultKey();
84         assertEquals(key, "");
85     }
86 
87     @Test
setAssistant()88     public void setAssistant() {
89         mFragment.setDefaultKey(TEST_CMP);
90         verify(mNotificationBackend).setNotificationAssistantGranted(TEST_COMPONENT);
91     }
92 
93     @Test
setAssistant_None()94     public void setAssistant_None() {
95         mFragment.setDefaultKey("");
96         verify(mNotificationBackend).setNotificationAssistantGranted(null);
97     }
98 
99     @Test
candidateListHasNoneAtEnd()100     public void candidateListHasNoneAtEnd() {
101         List<ServiceInfo> list = new ArrayList<>();
102         ServiceInfo serviceInfo = mock(ServiceInfo.class, RETURNS_SMART_NULLS);
103         serviceInfo.packageName = TEST_PKG;
104         serviceInfo.name = TEST_SRV;
105         list.add(serviceInfo);
106         mFragment.onServicesReloaded(list);
107         List<? extends CandidateInfo> candidates = mFragment.getCandidates();
108         assertTrue(candidates.size() > 0);
109         assertEquals(candidates.get(candidates.size() - 1).getKey(), "");
110     }
111 
112     @Test
candidateListHasCorrectCandidate()113     public void candidateListHasCorrectCandidate() {
114         List<ServiceInfo> list = new ArrayList<>();
115         ServiceInfo serviceInfo = mock(ServiceInfo.class, RETURNS_SMART_NULLS);
116         serviceInfo.packageName = TEST_PKG;
117         serviceInfo.name = TEST_SRV;
118         list.add(serviceInfo);
119         mFragment.onServicesReloaded(list);
120         List<? extends CandidateInfo> candidates = mFragment.getCandidates();
121         boolean found = false;
122         for (CandidateInfo c : candidates) {
123             if (TEST_CMP.equals(c.getKey())) {
124                 found = true;
125                 break;
126             }
127         }
128         if (!found) fail();
129     }
130 
131     @Test
noDialogOnNoAssistantSelected()132     public void noDialogOnNoAssistantSelected() {
133         when(mContext.getString(anyInt(), anyString())).thenAnswer(
134                 (InvocationOnMock invocation) -> {
135                     return invocation.getArgument(1);
136                 });
137         assertNull(mFragment.getConfirmationMessage(
138                 new NotificationAssistantPicker.CandidateNone(mContext)));
139     }
140 
141     @Test
dialogTextHasAssistantName()142     public void dialogTextHasAssistantName() {
143         CandidateInfo c = mock(CandidateInfo.class);
144         when(mContext.getString(anyInt(), anyString())).thenAnswer(
145                 (InvocationOnMock invocation) -> {
146                     return invocation.getArgument(1);
147                 });
148         when(c.loadLabel()).thenReturn(TEST_NAME);
149         when(c.getKey()).thenReturn(TEST_CMP);
150         CharSequence text = mFragment.getConfirmationMessage(c);
151         assertNotNull(text);
152         assertTrue(text.toString().contains(TEST_NAME));
153     }
154 
155 
156     private static class TestNotificationAssistantPicker extends NotificationAssistantPicker {
TestNotificationAssistantPicker(Context context, PackageManager packageManager, NotificationBackend notificationBackend)157         TestNotificationAssistantPicker(Context context, PackageManager packageManager,
158                 NotificationBackend notificationBackend) {
159             mContext = context;
160             mPm = packageManager;
161             mNotificationBackend = notificationBackend;
162         }
163     }
164 
165 }
166