1 /* 2 * Copyright (C) 2016 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.notification; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.anyInt; 21 import static org.mockito.Mockito.anyString; 22 import static org.mockito.Mockito.eq; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.pm.PackageManager; 28 import android.os.UserManager; 29 30 import com.android.settings.accounts.AccountRestrictionHelper; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 32 import com.android.settingslib.RestrictedPreference; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Answers; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 41 @RunWith(SettingsRobolectricTestRunner.class) 42 public class EmergencyBroadcastPreferenceControllerTest { 43 44 private static final String PREF_TEST_KEY = "test_key"; 45 46 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 47 private Context mContext; 48 @Mock 49 private AccountRestrictionHelper mAccountHelper; 50 @Mock 51 private PackageManager mPackageManager; 52 @Mock 53 private UserManager mUserManager; 54 @Mock 55 private RestrictedPreference mPreference; 56 57 private EmergencyBroadcastPreferenceController mController; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 63 when(mContext.getPackageManager()).thenReturn(mPackageManager); 64 mController = 65 new EmergencyBroadcastPreferenceController(mContext, mAccountHelper, PREF_TEST_KEY); 66 } 67 68 @Test updateState_shouldCheckRestriction()69 public void updateState_shouldCheckRestriction() { 70 mController.updateState(mPreference); 71 72 verify(mPreference).checkRestrictionAndSetDisabled(anyString()); 73 } 74 75 @Test getPreferenceKey_shouldReturnKeyDefinedInConstructor()76 public void getPreferenceKey_shouldReturnKeyDefinedInConstructor() { 77 assertThat(mController.getPreferenceKey()).isEqualTo(PREF_TEST_KEY); 78 } 79 80 @Test isAvailable_notAdminUser_shouldReturnFalse()81 public void isAvailable_notAdminUser_shouldReturnFalse() { 82 when(mUserManager.isAdminUser()).thenReturn(false); 83 when(mContext.getResources().getBoolean( 84 com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true); 85 when(mPackageManager.getApplicationEnabledSetting(anyString())) 86 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED); 87 when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false); 88 89 assertThat(mController.isAvailable()).isFalse(); 90 } 91 92 @Test isAvailable_hasConfigCellBroadcastRestriction_shouldReturnFalse()93 public void isAvailable_hasConfigCellBroadcastRestriction_shouldReturnFalse() { 94 when(mUserManager.isAdminUser()).thenReturn(true); 95 when(mContext.getResources().getBoolean( 96 com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true); 97 when(mPackageManager.getApplicationEnabledSetting(anyString())) 98 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED); 99 when(mAccountHelper.hasBaseUserRestriction( 100 eq(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS), anyInt())).thenReturn(true); 101 102 assertThat(mController.isAvailable()).isFalse(); 103 } 104 105 @Test isAvailable_cellBroadcastAppLinkDisabled_shouldReturnFalse()106 public void isAvailable_cellBroadcastAppLinkDisabled_shouldReturnFalse() { 107 when(mUserManager.isAdminUser()).thenReturn(true); 108 when(mContext.getResources().getBoolean( 109 com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(false); 110 when(mPackageManager.getApplicationEnabledSetting(anyString())) 111 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED); 112 when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false); 113 114 assertThat(mController.isAvailable()).isFalse(); 115 } 116 117 @Test isAvailable_cellBroadcastReceiverDisabled_shouldReturnFalse()118 public void isAvailable_cellBroadcastReceiverDisabled_shouldReturnFalse() { 119 when(mUserManager.isAdminUser()).thenReturn(true); 120 when(mContext.getResources().getBoolean( 121 com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true); 122 when(mPackageManager.getApplicationEnabledSetting(anyString())) 123 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED); 124 when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false); 125 126 assertThat(mController.isAvailable()).isFalse(); 127 } 128 } 129