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