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