• 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.enterprise;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.content.res.Resources;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.testutils.FakeFeatureFactory;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.annotation.Config;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class ManageDeviceAdminPreferenceControllerTest {
43 
44     @Mock
45     private Resources mResources;
46 
47     private Context mContext;
48     private FakeFeatureFactory mFeatureFactory;
49     private ManageDeviceAdminPreferenceController mController;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mContext = spy(RuntimeEnvironment.application);
55         mFeatureFactory = FakeFeatureFactory.setupForTest();
56         mController = new ManageDeviceAdminPreferenceController(mContext, "testkey");
57     }
58 
59     @Test
testUpdateState()60     public void testUpdateState() {
61         final Preference preference = new Preference(mContext, null, 0, 0);
62 
63         when(mFeatureFactory.enterprisePrivacyFeatureProvider
64                 .getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile()).thenReturn(0);
65         when(mContext.getResources()).thenReturn(mResources);
66         when(mResources.getString(R.string.number_of_device_admins_none))
67                 .thenReturn("no apps");
68         mController.updateState(preference);
69         assertThat(preference.getSummary()).isEqualTo("no apps");
70 
71         when(mFeatureFactory.enterprisePrivacyFeatureProvider
72                 .getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile()).thenReturn(5);
73         when(mResources.getQuantityString(R.plurals.number_of_device_admins, 5, 5))
74                 .thenReturn("5 active apps");
75         mController.updateState(preference);
76         assertThat(preference.getSummary()).isEqualTo("5 active apps");
77     }
78 
79     @Test
isAvailable_byDefault_isTrue()80     public void isAvailable_byDefault_isTrue() {
81         assertThat(mController.isAvailable()).isTrue();
82     }
83 
84     @Test
85     @Config(qualifiers = "mcc999")
isAvailable_whenNotVisible_isFalse()86     public void isAvailable_whenNotVisible_isFalse() {
87         assertThat(mController.isAvailable()).isFalse();
88     }
89 }
90