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