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