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 android.content.Context;
20 import android.content.res.Resources;
21 import android.support.v7.preference.Preference;
22 
23 import com.android.settings.R;
24 import com.android.settings.SettingsRobolectricTestRunner;
25 import com.android.settings.TestConfig;
26 import com.android.settings.testutils.FakeFeatureFactory;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Answers;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.annotation.Config;
35 
36 import static com.google.common.truth.Truth.assertThat;
37 import static org.mockito.Mockito.when;
38 
39 /**
40  * Tests for {@link ManageDeviceAdminPreferenceController}.
41  */
42 @RunWith(SettingsRobolectricTestRunner.class)
43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
44 public final class ManageDeviceAdminPreferenceControllerTest {
45 
46     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
47     private Context mContext;
48     private FakeFeatureFactory mFeatureFactory;
49 
50     private ManageDeviceAdminPreferenceController mController;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         FakeFeatureFactory.setupForTest(mContext);
56         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
57         mController = new ManageDeviceAdminPreferenceController(mContext);
58     }
59 
60     @Test
testUpdateState()61     public void testUpdateState() {
62         final Preference preference = new Preference(mContext, null, 0, 0);
63 
64         when(mFeatureFactory.enterprisePrivacyFeatureProvider
65                 .getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile()).thenReturn(0);
66         when(mContext.getResources().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(mContext.getResources().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
testIsAvailable()80     public void testIsAvailable() {
81         assertThat(mController.isAvailable()).isTrue();
82     }
83 
84     @Test
testHandlePreferenceTreeClick()85     public void testHandlePreferenceTreeClick() {
86         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
87                 .isFalse();
88     }
89 
90     @Test
testGetPreferenceKey()91     public void testGetPreferenceKey() {
92         assertThat(mController.getPreferenceKey()).isEqualTo("manage_device_admin");
93     }
94 }
95