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 android.content.Context; 20 import android.os.UserManager; 21 import android.support.v7.preference.Preference; 22 23 import com.android.settings.SettingsRobolectricTestRunner; 24 import com.android.settings.TestConfig; 25 import com.android.settings.accounts.AccountRestrictionHelper; 26 import com.android.settingslib.RestrictedPreference; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.annotation.Config; 34 import org.robolectric.shadows.ShadowApplication; 35 36 import static com.google.common.truth.Truth.assertThat; 37 import static org.mockito.Matchers.anyInt; 38 import static org.mockito.Matchers.eq; 39 import static org.mockito.Mockito.doCallRealMethod; 40 import static org.mockito.Mockito.mock; 41 import static org.mockito.Mockito.spy; 42 import static org.mockito.Mockito.verify; 43 import static org.mockito.Mockito.when; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 47 public class AdjustVolumeRestrictedPreferenceControllerTest { 48 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 ShadowApplication shadowContext = ShadowApplication.getInstance(); 59 mContext = spy(shadowContext.getApplicationContext()); 60 mController = 61 new AdjustVolumeRestrictedPreferenceControllerTestable(mContext, mAccountHelper); 62 } 63 64 @Test updateState_hasBaseRestriction_shouldDisable()65 public void updateState_hasBaseRestriction_shouldDisable() { 66 RestrictedPreference preference = mock(RestrictedPreference.class); 67 when(mAccountHelper.hasBaseUserRestriction( 68 eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt())).thenReturn(true); 69 70 mController.updateState(preference); 71 72 assertThat(preference.isEnabled()).isFalse(); 73 } 74 75 @Test updateState_NoBaseRestriction_shouldCheckRestriction()76 public void updateState_NoBaseRestriction_shouldCheckRestriction() { 77 RestrictedPreference preference = spy(new RestrictedPreference(mContext)); 78 79 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(null); 80 when(mAccountHelper.hasBaseUserRestriction( 81 eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt())).thenReturn(false); 82 doCallRealMethod().when(mAccountHelper).enforceRestrictionOnPreference( 83 eq(preference), eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt()); 84 85 mController.updateState(preference); 86 87 verify(preference).checkRestrictionAndSetDisabled( 88 eq(UserManager.DISALLOW_ADJUST_VOLUME), anyInt()); 89 } 90 91 private class AdjustVolumeRestrictedPreferenceControllerTestable extends 92 AdjustVolumeRestrictedPreferenceController { AdjustVolumeRestrictedPreferenceControllerTestable(Context context, AccountRestrictionHelper helper)93 AdjustVolumeRestrictedPreferenceControllerTestable(Context context, 94 AccountRestrictionHelper helper) { 95 super(context, helper); 96 } 97 98 @Override getPreferenceKey()99 public String getPreferenceKey() { 100 return null; 101 } 102 103 @Override handlePreferenceTreeClick(Preference preference)104 public boolean handlePreferenceTreeClick(Preference preference) { 105 return false; 106 } 107 108 @Override isAvailable()109 public boolean isAvailable() { 110 return true; 111 } 112 } 113 114 } 115