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
239f1e911759dc6fedaac9fa65afb79f6a93022bf4Andrew Sappersteinimport com.android.settings.testutils.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
470335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang    private static final String PREF_TEST_KEY = "test_key";
480335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang
496c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
506c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private Context mContext;
516c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock
526c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private AccountRestrictionHelper mAccountHelper;
536c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock
546c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private PackageManager mPackageManager;
556c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock
566c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private UserManager mUserManager;
576c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Mock
586c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private RestrictedPreference mPreference;
596c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
606c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    private EmergencyBroadcastPreferenceController mController;
616c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
626c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Before
636c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void setUp() {
646c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        MockitoAnnotations.initMocks(this);
656c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
666c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getPackageManager()).thenReturn(mPackageManager);
670335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang        mController = new EmergencyBroadcastPreferenceController(mContext, mAccountHelper,
680335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                PREF_TEST_KEY);
696c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
706c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
716c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
726c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void updateState_shouldCheckRestriction() {
736c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        mController.updateState(mPreference);
746c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
756c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        verify(mPreference).checkRestrictionAndSetDisabled(anyString());
766c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
776c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
786c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
790335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang    public void getPreferenceKey_shouldReturnKeyDefinedInConstructor() {
800335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang        assertThat(mController.getPreferenceKey()).isEqualTo(PREF_TEST_KEY);
810335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang    }
820335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang
830335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang    @Test
846c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void isAvailable_notAdminUser_shouldReturnFalse() {
856c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mUserManager.isAdminUser()).thenReturn(false);
866c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getResources().getBoolean(
870335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
886c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mPackageManager.getApplicationEnabledSetting(anyString()))
890335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
906c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
916c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
926c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        assertThat(mController.isAvailable()).isFalse();
936c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
946c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
956c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
966c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void isAvailable_hasConfigCellBroadcastRestriction_shouldReturnFalse() {
976c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mUserManager.isAdminUser()).thenReturn(true);
986c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getResources().getBoolean(
990335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
1006c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mPackageManager.getApplicationEnabledSetting(anyString()))
1010335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
1026c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mAccountHelper.hasBaseUserRestriction(
1030335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                eq(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS), anyInt())).thenReturn(true);
1046c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1056c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        assertThat(mController.isAvailable()).isFalse();
1066c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
1076c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1086c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
1096c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void isAvailable_cellBroadcastAppLinkDisabled_shouldReturnFalse() {
1106c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mUserManager.isAdminUser()).thenReturn(true);
1116c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getResources().getBoolean(
1120335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(false);
1136c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mPackageManager.getApplicationEnabledSetting(anyString()))
1140335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
1156c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
1166c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1176c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        assertThat(mController.isAvailable()).isFalse();
1186c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
1196c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1206c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    @Test
1216c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    public void isAvailable_cellBroadcastReceiverDisabled_shouldReturnFalse() {
1226c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mUserManager.isAdminUser()).thenReturn(true);
1236c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mContext.getResources().getBoolean(
1240335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                com.android.internal.R.bool.config_cellBroadcastAppLinks)).thenReturn(true);
1256c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mPackageManager.getApplicationEnabledSetting(anyString()))
1260335cd1e9b428e78c37835a52c0dd92032de2c9eFan Zhang                .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
1276c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        when(mAccountHelper.hasBaseUserRestriction(anyString(), anyInt())).thenReturn(false);
1286c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1296c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling        assertThat(mController.isAvailable()).isFalse();
1306c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling    }
1316c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling
1326c2cf0dbc58caced6803a77e055e19b614dd2148Doris Ling}
133