• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.development;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyBoolean;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.os.RemoteException;
29 import android.os.storage.IStorageManager;
30 import android.sysprop.CryptoProperties;
31 import androidx.preference.Preference;
32 import androidx.preference.PreferenceScreen;
33 
34 import com.android.settings.R;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.util.ReflectionHelpers;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class FileEncryptionPreferenceControllerTest {
47 
48     @Mock
49     private Preference mPreference;
50     @Mock
51     private PreferenceScreen mPreferenceScreen;
52     @Mock
53     private IStorageManager mStorageManager;
54 
55     private Context mContext;
56     private FileEncryptionPreferenceController mController;
57 
58     @Before
setup()59     public void setup() {
60         MockitoAnnotations.initMocks(this);
61         mContext = RuntimeEnvironment.application;
62         mController = new FileEncryptionPreferenceController(mContext);
63         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
64             .thenReturn(mPreference);
65     }
66 
67     @Test
isAvailable_storageManagerNull_shouldBeFalse()68     public void isAvailable_storageManagerNull_shouldBeFalse() {
69         ReflectionHelpers.setField(mController, "mStorageManager", null);
70 
71         assertThat(mController.isAvailable()).isFalse();
72     }
73 
74     @Test
isAvailable_notConvertibleToFBE_shouldBeFalse()75     public void isAvailable_notConvertibleToFBE_shouldBeFalse() throws RemoteException {
76         ReflectionHelpers.setField(mController, "mStorageManager", mStorageManager);
77         when(mStorageManager.isConvertibleToFBE()).thenReturn(false);
78 
79         assertThat(mController.isAvailable()).isFalse();
80     }
81 
82     @Test
isAvailable_convertibleToFBE_shouldBeTrue()83     public void isAvailable_convertibleToFBE_shouldBeTrue() throws RemoteException {
84         ReflectionHelpers.setField(mController, "mStorageManager", mStorageManager);
85         when(mStorageManager.isConvertibleToFBE()).thenReturn(true);
86 
87         assertThat(mController.isAvailable()).isTrue();
88     }
89 
90     @Test
updateState_settingIsNotFile_shouldDoNothing()91     public void updateState_settingIsNotFile_shouldDoNothing() throws RemoteException {
92         ReflectionHelpers.setField(mController, "mStorageManager", mStorageManager);
93         when(mStorageManager.isConvertibleToFBE()).thenReturn(true);
94         mController.displayPreference(mPreferenceScreen);
95         CryptoProperties.type(CryptoProperties.type_values.NONE);
96 
97         mController.updateState(mPreference);
98 
99         verify(mPreference, never()).setEnabled(anyBoolean());
100         verify(mPreference, never()).setSummary(anyString());
101     }
102 
103     @Test
updateState_settingIsFile_shouldSetSummaryAndDisablePreference()104     public void updateState_settingIsFile_shouldSetSummaryAndDisablePreference()
105             throws RemoteException {
106         ReflectionHelpers.setField(mController, "mStorageManager", mStorageManager);
107         when(mStorageManager.isConvertibleToFBE()).thenReturn(true);
108         mController.displayPreference(mPreferenceScreen);
109         CryptoProperties.type(CryptoProperties.type_values.FILE);
110 
111         mController.updateState(mPreference);
112 
113         verify(mPreference).setEnabled(false);
114         verify(mPreference).setSummary(mContext.getString(R.string.convert_to_file_encryption_done));
115     }
116 }
117