EmergencyBroadcastPreferenceControllerTest.java revision 6c2cf0dbc58caced6803a77e055e19b614dd2148
16c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling/*
26c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling * Copyright (C) 2016 The Android Open Source Project
36c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling *
46c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling * Licensed under the Apache License, Version 2.0 (the "License");
56c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling * you may not use this file except in compliance with the License.
66c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling * You may obtain a copy of the License at
76c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling *
86c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling *      http://www.apache.org/licenses/LICENSE-2.0
96c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling *
106c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling * Unless required by applicable law or agreed to in writing, software
116c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling * distributed under the License is distributed on an "AS IS" BASIS,
126c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling * See the License for the specific language governing permissions and
146c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling * limitations under the License.
156c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling */
166c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
176c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingpackage com.android.settings.notification;
186c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
196c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport android.content.Context;
206c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport android.content.pm.PackageManager;
216c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport android.os.UserManager;
226c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
236c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport com.android.settings.SettingsRobolectricTestRunner;
246c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport com.android.settings.TestConfig;
256c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport com.android.settings.accounts.AccountRestrictionHelper;
266c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport com.android.settingslib.RestrictedPreference;
276c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
286c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport org.junit.Before;
296c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport org.junit.Test;
306c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport org.junit.runner.RunWith;
316c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport org.mockito.Answers;
326c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport org.mockito.Mock;
336c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport org.mockito.MockitoAnnotations;
346c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport org.robolectric.annotation.Config;
356c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
366c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport static com.google.common.truth.Truth.assertThat;
376c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport static org.mockito.Mockito.anyInt;
386c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport static org.mockito.Mockito.anyString;
396c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport static org.mockito.Mockito.eq;
406c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport static org.mockito.Mockito.verify;
416c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingimport static org.mockito.Mockito.when;
426c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
436c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling@RunWith(SettingsRobolectricTestRunner.class)
446c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
456c2cf0dbc58caced6803a77e055e19b614dd2148Doris Lingpublic class EmergencyBroadcastPreferenceControllerTest {
466c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
476c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
486c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private Context mContext;
496c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock
506c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private AccountRestrictionHelper mAccountHelper;
516c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock
526c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private PackageManager mPackageManager;
536c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock
546c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private UserManager mUserManager;
556c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock
566c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private RestrictedPreference mPreference;
576c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
586c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private EmergencyBroadcastPreferenceController mController;
596c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
606c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Before
616c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void setUp() {
626c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        MockitoAnnotations.initMocks(this);
636c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
646c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getPackageManager()).thenReturn(mPackageManager);
656c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        mController = new EmergencyBroadcastPreferenceController(mContext, mAccountHelper);
666c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
676c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
686c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
696c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void updateState_shouldCheckRestriction() {
706c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        mController.updateState(mPreference);
716c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
726c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        verify(mPreference).checkRestrictionAndSetDisabled(anyString());
736c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
746c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
756c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
766c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void isAvailable_notAdminUser_shouldReturnFalse() {
776c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mUserManager.isAdminUser()).thenReturn(false);
786c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getResources().getBoolean(
796c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling            com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
806c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mPackageManager.getApplicationEnabledSetting(anyString()))
816c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling            .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
826c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
836c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
846c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        assertThat(mController.isAvailable()).isFalse();
856c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
866c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
876c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
886c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void isAvailable_hasConfigCellBroadcastRestriction_shouldReturnFalse() {
896c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mUserManager.isAdminUser()).thenReturn(true);
906c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getResources().getBoolean(
916c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling            com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
926c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mPackageManager.getApplicationEnabledSetting(anyString()))
936c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling            .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
946c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mAccountHelper.hasBaseUserRestriction(
956c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling            eq(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS), anyInt())).thenReturn(true);
966c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
976c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        assertThat(mController.isAvailable()).isFalse();
986c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
996c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1006c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
1016c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void isAvailable_cellBroadcastAppLinkDisabled_shouldReturnFalse() {
1026c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mUserManager.isAdminUser()).thenReturn(true);
1036c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getResources().getBoolean(
1046c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling            com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(false);
1056c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mPackageManager.getApplicationEnabledSetting(anyString()))
1066c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling            .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
1076c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
1086c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1096c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        assertThat(mController.isAvailable()).isFalse();
1106c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
1116c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1126c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
1136c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void isAvailable_cellBroadcastReceiverDisabled_shouldReturnFalse() {
1146c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mUserManager.isAdminUser()).thenReturn(true);
1156c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getResources().getBoolean(
1166c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling            com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
1176c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mPackageManager.getApplicationEnabledSetting(anyString()))
1186c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling            .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
1196c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
1206c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1216c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        assertThat(mController.isAvailable()).isFalse();
1226c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
1236c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1246c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling}
125