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 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.doCallRealMethod; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.content.Context; 30 import android.os.UserManager; 31 32 import androidx.preference.Preference; 33 34 import com.android.settings.accounts.AccountRestrictionHelper; 35 import com.android.settingslib.RestrictedPreference; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class AdjustVolumeRestrictedPreferenceControllerTest { 47 48 private static final String KEY = "key"; 49 @Mock 50 private AccountRestrictionHelper mAccountHelper; 51 52 private Context mContext; 53 private AdjustVolumeRestrictedPreferenceControllerTestable mController; 54 55 @Before setUp()56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mContext = spy(RuntimeEnvironment.application); 59 mController = 60 new AdjustVolumeRestrictedPreferenceControllerTestable(mContext, mAccountHelper, KEY); 61 } 62 63 @Test updateState_hasBaseRestriction_shouldDisable()64 public void updateState_hasBaseRestriction_shouldDisable() { 65 RestrictedPreference preference = mock(RestrictedPreference.class); 66 when(mAccountHelper.hasBaseUserRestriction( 67 eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt())).thenReturn(true); 68 69 mController.updateState(preference); 70 71 assertThat(preference.isEnabled()).isFalse(); 72 } 73 74 @Test updateState_NoBaseRestriction_shouldCheckRestriction()75 public void updateState_NoBaseRestriction_shouldCheckRestriction() { 76 RestrictedPreference preference = spy(new RestrictedPreference(mContext)); 77 78 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(null); 79 when(mAccountHelper.hasBaseUserRestriction( 80 eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt())).thenReturn(false); 81 doCallRealMethod().when(mAccountHelper).enforceRestrictionOnPreference( 82 eq(preference), eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt()); 83 84 mController.updateState(preference); 85 86 verify(preference).checkRestrictionAndSetDisabled( 87 eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt()); 88 } 89 90 private class AdjustVolumeRestrictedPreferenceControllerTestable 91 extends AdjustVolumeRestrictedPreferenceController { 92 AdjustVolumeRestrictedPreferenceControllerTestable(Context context, AccountRestrictionHelper helper, String key)93 private AdjustVolumeRestrictedPreferenceControllerTestable(Context context, 94 AccountRestrictionHelper helper, String key) { 95 super(context, helper, key); 96 } 97 98 @Override getAvailabilityStatus()99 public int getAvailabilityStatus() { 100 return 0; 101 } 102 103 @Override getPreferenceKey()104 public String getPreferenceKey() { 105 return KEY; 106 } 107 108 @Override handlePreferenceTreeClick(Preference preference)109 public boolean handlePreferenceTreeClick(Preference preference) { 110 return false; 111 } 112 113 @Override getSliderPosition()114 public int getSliderPosition() { 115 return 0; 116 } 117 118 @Override setSliderPosition(int position)119 public boolean setSliderPosition(int position) { 120 return false; 121 } 122 123 @Override getMax()124 public int getMax() { 125 return 0; 126 } 127 128 @Override getMin()129 public int getMin() { 130 return 0; 131 } 132 } 133 } 134