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 android.content.Context;
20 import android.content.res.Resources;
21 import com.android.settings.R;
22 import android.support.v7.preference.Preference;
23 
24 import com.android.settings.SettingsRobolectricTestRunner;
25 import com.android.settings.TestConfig;
26 import com.android.settings.core.PreferenceAvailabilityObserver;
27 import com.android.settings.testutils.FakeFeatureFactory;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Answers;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.annotation.Config;
36 
37 import static com.google.common.truth.Truth.assertThat;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
40 
41 /**
42  * Tests for {@link CaCertsPreferenceController}.
43  */
44 @RunWith(SettingsRobolectricTestRunner.class)
45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
46 public final class CaCertsPreferenceControllerTest {
47 
48     private static final String KEY_CA_CERTS = "ca_certs";
49 
50     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
51     private Context mContext;
52     private FakeFeatureFactory mFeatureFactory;
53     @Mock private PreferenceAvailabilityObserver mObserver;
54 
55     private CaCertsPreferenceController mController;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         FakeFeatureFactory.setupForTest(mContext);
61         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
62         mController = new CaCertsPreferenceController(mContext, null /* lifecycle */);
63         mController.setAvailabilityObserver(mObserver);
64     }
65 
66     @Test
testGetAvailabilityObserver()67     public void testGetAvailabilityObserver() {
68         assertThat(mController.getAvailabilityObserver()).isEqualTo(mObserver);
69     }
70 
71     @Test
testUpdateState()72     public void testUpdateState() {
73         final Preference preference = new Preference(mContext, null, 0, 0);
74 
75         when(mContext.getResources().getQuantityString(R.plurals.enterprise_privacy_number_ca_certs,
76                 10, 10)).thenReturn("10 certs");
77         when(mFeatureFactory.enterprisePrivacyFeatureProvider
78                 .getNumberOfOwnerInstalledCaCertsForCurrentUserAndManagedProfile()).thenReturn(10);
79         mController.updateState(preference);
80         assertThat(preference.getSummary()).isEqualTo("10 certs");
81     }
82 
83     @Test
testIsAvailable()84     public void testIsAvailable() {
85         when(mFeatureFactory.enterprisePrivacyFeatureProvider
86                 .getNumberOfOwnerInstalledCaCertsForCurrentUserAndManagedProfile()).thenReturn(0);
87         assertThat(mController.isAvailable()).isFalse();
88         verify(mObserver).onPreferenceAvailabilityUpdated(KEY_CA_CERTS, false);
89 
90         when(mFeatureFactory.enterprisePrivacyFeatureProvider
91                 .getNumberOfOwnerInstalledCaCertsForCurrentUserAndManagedProfile()).thenReturn(10);
92         assertThat(mController.isAvailable()).isTrue();
93         verify(mObserver).onPreferenceAvailabilityUpdated(KEY_CA_CERTS, true);
94     }
95 
96     @Test
testHandlePreferenceTreeClick()97     public void testHandlePreferenceTreeClick() {
98         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
99                 .isFalse();
100     }
101 
102     @Test
testGetPreferenceKey()103     public void testGetPreferenceKey() {
104         assertThat(mController.getPreferenceKey()).isEqualTo(KEY_CA_CERTS);
105     }
106 }
107