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 junit.framework.TestCase.assertEquals;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.pm.PackageManager;
26 import android.graphics.drawable.Drawable;
27 import android.os.Debug;
28 
29 import com.android.settingslib.widget.CandidateInfo;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Answers;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class NotificationAssistantPreferenceControllerTest {
42 
43     private static final String KEY = "TEST_KEY";
44     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
45     private Context mContext;
46     @Mock
47     private NotificationBackend mBackend;
48     private NotificationAssistantPreferenceController mPreferenceController;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53         mContext = RuntimeEnvironment.application;
54         mPreferenceController = new TestPreferenceController(mContext, mBackend);
55     }
56 
57     @Test
testGetSummary_noAssistant()58     public void testGetSummary_noAssistant() {
59         when(mBackend.getAllowedNotificationAssistant()).thenReturn(null);
60         CharSequence noneLabel = new NotificationAssistantPicker.CandidateNone(mContext)
61                 .loadLabel();
62         assertEquals(noneLabel, mPreferenceController.getSummary());
63     }
64 
65     @Test
testGetSummary_TestAssistant()66     public void testGetSummary_TestAssistant() {
67         String testName = "test_pkg/test_cls";
68         when(mBackend.getAllowedNotificationAssistant()).thenReturn(
69                 ComponentName.unflattenFromString(testName));
70         assertEquals(testName, mPreferenceController.getSummary());
71     }
72 
73     private final class TestPreferenceController extends NotificationAssistantPreferenceController {
74 
TestPreferenceController(Context context, NotificationBackend backend)75         private TestPreferenceController(Context context, NotificationBackend backend) {
76             super(context, KEY);
77             mNotificationBackend = backend;
78         }
79 
80         @Override
getPreferenceKey()81         public String getPreferenceKey() {
82             return KEY;
83         }
84 
85         @Override
createCandidateInfo(ComponentName cn)86         protected CandidateInfo createCandidateInfo(ComponentName cn) {
87             return new CandidateInfo(true) {
88                 @Override
89                 public CharSequence loadLabel() { return cn.flattenToString(); }
90 
91                 @Override
92                 public Drawable loadIcon() { return null; }
93 
94                 @Override
95                 public String getKey() { return null; }
96             };
97         }
98     }
99 
100 }
101