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