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.security;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.admin.DevicePolicyManager;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.os.UserManager;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceFragmentCompat;
28 
29 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 import org.robolectric.shadows.ShadowApplication;
39 import org.robolectric.shadows.androidx.fragment.FragmentController;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class EncryptionAndCredentialTest {
43 
44     @Mock
45     private UserManager mUserManager;
46     @Mock
47     private DevicePolicyManager mDevicePolicyManager;
48 
49     private Context mContext;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         ShadowApplication application = ShadowApplication.getInstance();
55         application.setSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager);
56         application.setSystemService(Context.USER_SERVICE, mUserManager);
57         mContext = RuntimeEnvironment.application;
58     }
59 
60     @Test
getMetricsCategory_shouldReturnEncryptionAndCredential()61     public void getMetricsCategory_shouldReturnEncryptionAndCredential() {
62         EncryptionAndCredential fragment = new EncryptionAndCredential();
63         assertThat(fragment.getMetricsCategory()).isEqualTo(MetricsEvent.ENCRYPTION_AND_CREDENTIAL);
64     }
65 
66     @Test
isSelectable_encryptionPreferenceStatus_isNotSelectable()67     public void isSelectable_encryptionPreferenceStatus_isNotSelectable() {
68         final PreferenceFragmentCompat fragment =
69                 FragmentController.of(new TestFragment(), new Bundle())
70                 .create()
71                 .start()
72                 .resume()
73                 .get();
74         final Preference preference =
75                 fragment.findPreference("encryption_and_credentials_encryption_status");
76 
77         assertThat(preference.isSelectable()).isFalse();
78     }
79 
80     public static class TestFragment extends PreferenceFragmentCompat {
81         @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)82         public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
83             addPreferencesFromResource(com.android.settings.R.xml.encryption_and_credential);
84         }
85     }
86 }
87