1 /*
2  * Copyright (C) 2017 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.enterprise;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.admin.DevicePolicyManager;
28 import android.content.Context;
29 
30 import androidx.preference.Preference;
31 
32 import com.android.settings.R;
33 import com.android.settings.testutils.FakeFeatureFactory;
34 import com.android.settingslib.utils.StringUtil;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Answers;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 
42 /**
43  * Base test class for testing {@link CaCertsPreferenceControllerBase}'s subclass.
44  */
45 public abstract class CaCertsPreferenceControllerTestBase {
46 
47     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
48     protected Context mContext;
49     protected FakeFeatureFactory mFeatureFactory;
50     protected CaCertsPreferenceControllerBase mController;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         doReturn(mock(DevicePolicyManager.class)).when(mContext)
56                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
57         mFeatureFactory = FakeFeatureFactory.setupForTest();
58         mController = createController();
59     }
60 
61     @Test
testUpdateState()62     public void testUpdateState() {
63         final Preference preference = new Preference(mContext, null, 0, 0);
64 
65         when(mContext.getResources().getString(R.string.enterprise_privacy_number_ca_certs))
66                 .thenReturn("# certs");
67         when(StringUtil.getIcuPluralsString(mContext, 10,
68                 R.string.enterprise_privacy_number_ca_certs)).thenReturn("10 certs");
69         mockGetNumberOfCaCerts(10);
70         mController.updateState(preference);
71         assertThat(preference.getSummary()).isEqualTo("10 certs");
72     }
73 
74     @Test
testIsAvailable()75     public void testIsAvailable() {
76         mockGetNumberOfCaCerts(0);
77         assertThat(mController.isAvailable()).isFalse();
78 
79         mockGetNumberOfCaCerts(10);
80         assertThat(mController.isAvailable()).isTrue();
81     }
82 
83     @Test
testHandlePreferenceTreeClick()84     public void testHandlePreferenceTreeClick() {
85         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
86                 .isFalse();
87     }
88 
89     @Test
testGetPreferenceKey()90     public void testGetPreferenceKey() {
91         assertThat(mController.getPreferenceKey()).isEqualTo(getPreferenceKey());
92     }
93 
mockGetNumberOfCaCerts(int numOfCaCerts)94     abstract void mockGetNumberOfCaCerts(int numOfCaCerts);
95 
getPreferenceKey()96     abstract String getPreferenceKey();
97 
createController()98     abstract CaCertsPreferenceControllerBase createController();
99 }
100