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