• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_DEFAULT;
20 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.when;
25 
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ComponentName;
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 
35 import org.junit.Before;
36 import org.junit.Ignore;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Answers;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class PrivacyPreferenceControllerHelperTest {
46 
47     private static final String MANAGED_GENERIC = "managed by organization";
48     private static final String MANAGED_WITH_NAME = "managed by Foo, Inc.";
49     private static final String MANAGING_ORGANIZATION = "Foo, Inc.";
50     private static final ComponentName DEVICE_OWNER_COMPONENT =
51             new ComponentName("com.android.foo", "bar");
52 
53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
54     private Context mContext;
55     @Mock
56     private DevicePolicyManager mDevicePolicyManager;
57     private FakeFeatureFactory mFeatureFactory;
58     private PrivacyPreferenceControllerHelper mPrivacyPreferenceControllerHelper;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mFeatureFactory = FakeFeatureFactory.setupForTest();
64 
65         when((Object) mContext.getSystemService(DevicePolicyManager.class))
66                 .thenReturn(mDevicePolicyManager);
67         mPrivacyPreferenceControllerHelper = new PrivacyPreferenceControllerHelper(mContext);
68 
69         when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
70         when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser())
71                 .thenReturn(DEVICE_OWNER_COMPONENT);
72         when(mDevicePolicyManager.getDeviceOwnerType(DEVICE_OWNER_COMPONENT))
73                 .thenReturn(DEVICE_OWNER_TYPE_DEFAULT);
74     }
75 
76     @Test
77     @Ignore
testUpdateState_noDeviceOwnerName_useGenericPreferenceSummary()78     public void testUpdateState_noDeviceOwnerName_useGenericPreferenceSummary() {
79         final Preference preference = new Preference(mContext, null, 0, 0);
80         when(mContext.getString(R.string.enterprise_privacy_settings_summary_generic))
81                 .thenReturn(MANAGED_GENERIC);
82         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getDeviceOwnerOrganizationName())
83                 .thenReturn(null);
84 
85         mPrivacyPreferenceControllerHelper.updateState(preference);
86 
87         assertThat(preference.getSummary().toString()).isEqualTo(MANAGED_GENERIC);
88     }
89 
90     @Test
91     @Ignore
testUpdateState_deviceOwnerName_usePreferenceSummaryWithDeviceOwnerName()92     public void testUpdateState_deviceOwnerName_usePreferenceSummaryWithDeviceOwnerName() {
93         final Preference preference = new Preference(mContext, null, 0, 0);
94         when(mContext.getResources().getString(
95                 R.string.enterprise_privacy_settings_summary_with_name, MANAGING_ORGANIZATION))
96                 .thenReturn(MANAGED_WITH_NAME);
97         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getDeviceOwnerOrganizationName())
98                 .thenReturn(MANAGING_ORGANIZATION);
99 
100         mPrivacyPreferenceControllerHelper.updateState(preference);
101 
102         assertThat(preference.getSummary().toString()).isEqualTo(MANAGED_WITH_NAME);
103     }
104 
105     @Test
hasDeviceOwner_deviceOwner_returnsTrue()106     public void hasDeviceOwner_deviceOwner_returnsTrue() {
107         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()).thenReturn(true);
108 
109         assertThat(mPrivacyPreferenceControllerHelper.hasDeviceOwner()).isTrue();
110     }
111 
112     @Test
hasDeviceOwner_noDeviceOwner_returnsFalse()113     public void hasDeviceOwner_noDeviceOwner_returnsFalse() {
114         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()).thenReturn(false);
115 
116         assertThat(mPrivacyPreferenceControllerHelper.hasDeviceOwner()).isFalse();
117     }
118 
119     @Test
isFinancedDevice_deviceNotManaged_returnsFalse()120     public void isFinancedDevice_deviceNotManaged_returnsFalse() {
121         when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
122 
123         assertThat(mPrivacyPreferenceControllerHelper.isFinancedDevice()).isFalse();
124     }
125 
126     @Test
isFinancedDevice_deviceManaged_defaultDeviceOwnerType_returnsFalse()127     public void isFinancedDevice_deviceManaged_defaultDeviceOwnerType_returnsFalse() {
128         assertThat(mPrivacyPreferenceControllerHelper.isFinancedDevice()).isFalse();
129     }
130 
131     @Test
isFinancedDevice_deviceManaged_financedDeviceOwnerType_returnsTrue()132     public void isFinancedDevice_deviceManaged_financedDeviceOwnerType_returnsTrue() {
133         when(mDevicePolicyManager.getDeviceOwnerType(DEVICE_OWNER_COMPONENT))
134                 .thenReturn(DEVICE_OWNER_TYPE_FINANCED);
135 
136         assertThat(mPrivacyPreferenceControllerHelper.isFinancedDevice()).isTrue();
137     }
138 }
139