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 static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.settings.R;
28 import com.android.settings.testutils.FakeFeatureFactory;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Answers;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class EnterprisePrivacyPreferenceControllerTest {
40 
41     private static final String MANAGED_GENERIC = "managed by organization";
42     private static final String MANAGED_WITH_NAME = "managed by Foo, Inc.";
43     private static final String MANAGING_ORGANIZATION = "Foo, Inc.";
44     private static final String KEY_ENTERPRISE_PRIVACY = "enterprise_privacy";
45 
46     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
47     private Context mContext;
48     private FakeFeatureFactory mFeatureFactory;
49 
50     private EnterprisePrivacyPreferenceController mController;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         mFeatureFactory = FakeFeatureFactory.setupForTest();
56         mController = new EnterprisePrivacyPreferenceController(mContext);
57     }
58 
59     @Test
testUpdateState()60     public void testUpdateState() {
61         final Preference preference = new Preference(mContext, null, 0, 0);
62 
63         when(mContext.getString(R.string.enterprise_privacy_settings_summary_generic))
64                 .thenReturn(MANAGED_GENERIC);
65         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getDeviceOwnerOrganizationName())
66                 .thenReturn(null);
67         mController.updateState(preference);
68         assertThat(preference.getSummary()).isEqualTo(MANAGED_GENERIC);
69 
70         when(mContext.getResources().getString(
71                 R.string.enterprise_privacy_settings_summary_with_name, MANAGING_ORGANIZATION))
72                 .thenReturn(MANAGED_WITH_NAME);
73         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getDeviceOwnerOrganizationName())
74                 .thenReturn(MANAGING_ORGANIZATION);
75         mController.updateState(preference);
76         assertThat(preference.getSummary()).isEqualTo(MANAGED_WITH_NAME);
77     }
78 
79     @Test
testIsAvailable()80     public void testIsAvailable() {
81         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()).thenReturn(false);
82         assertThat(mController.isAvailable()).isFalse();
83 
84         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()).thenReturn(true);
85         assertThat(mController.isAvailable()).isTrue();
86     }
87 
88     @Test
testHandlePreferenceTreeClick()89     public void testHandlePreferenceTreeClick() {
90         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
91                 .isFalse();
92     }
93 
94     @Test
testGetPreferenceKey()95     public void testGetPreferenceKey() {
96         assertThat(mController.getPreferenceKey()).isEqualTo(KEY_ENTERPRISE_PRIVACY);
97     }
98 }
99