1 /* 2 * Copyright (C) 2017 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.app.Activity; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.support.v7.preference.DropDownPreference; 23 import android.support.v7.preference.PreferenceScreen; 24 import android.provider.Settings.Global; 25 import android.telephony.TelephonyManager; 26 27 import com.android.settings.testutils.SettingsRobolectricTestRunner; 28 import com.android.settings.TestConfig; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.annotation.Config; 36 import org.robolectric.shadows.ShadowApplication; 37 38 import static com.google.common.truth.Truth.assertThat; 39 import static org.mockito.Mockito.doReturn; 40 import static org.mockito.Mockito.when; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 44 public class EmergencyTonePreferenceControllerTest { 45 46 @Mock 47 private TelephonyManager mTelephonyManager; 48 @Mock 49 private PreferenceScreen mScreen; 50 @Mock 51 private Activity mActivity; 52 @Mock 53 private ContentResolver mContentResolver; 54 @Mock 55 private SoundSettings mSetting; 56 @Mock 57 private Context mContext; 58 59 private EmergencyTonePreferenceController mController; 60 private DropDownPreference mPreference; 61 62 @Before 63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 final Context appContext = ShadowApplication.getInstance().getApplicationContext(); 66 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 67 when(mTelephonyManager.getCurrentPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA); 68 when(mSetting.getActivity()).thenReturn(mActivity); 69 when(mActivity.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 70 when(mActivity.getContentResolver()).thenReturn(mContentResolver); 71 when(mActivity.getResources()).thenReturn(appContext.getResources()); 72 mPreference = new DropDownPreference(appContext); 73 mController = new EmergencyTonePreferenceController(mContext, mSetting, null); 74 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 75 doReturn(mScreen).when(mSetting).getPreferenceScreen(); 76 } 77 78 @Test 79 public void isAvailable_cdma_shouldReturnTrue() { 80 assertThat(mController.isAvailable()).isTrue(); 81 } 82 83 @Test 84 public void isAvailable_notCdma_shouldReturnFalse() { 85 when(mTelephonyManager.getCurrentPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM); 86 87 assertThat(mController.isAvailable()).isFalse(); 88 } 89 90 @Test 91 public void displayPreference_emergencyToneOff_shouldSelectFirstItem() { 92 Global.putInt(mContentResolver, Global.EMERGENCY_TONE, 0); 93 94 mController.displayPreference(mScreen); 95 96 assertThat(mPreference.getValue()).isEqualTo("0"); 97 } 98 99 @Test 100 public void displayPreference_emergencyToneAlert_shouldSelectSecondItem() { 101 Global.putInt(mContentResolver, Global.EMERGENCY_TONE, 1); 102 103 mController.displayPreference(mScreen); 104 105 assertThat(mPreference.getValue()).isEqualTo("1"); 106 } 107 108 @Test 109 public void displayPreference_emergencyToneVibrate_shouldSelectThirdItem() { 110 Global.putInt(mContentResolver, Global.EMERGENCY_TONE, 2); 111 112 mController.displayPreference(mScreen); 113 114 assertThat(mPreference.getValue()).isEqualTo("2"); 115 } 116 117 @Test 118 public void onPreferenceChanged_firstItemSelected_shouldSetEmergencyToneToOff() { 119 mController.displayPreference(mScreen); 120 121 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "0"); 122 123 assertThat(Global.getInt(mContentResolver, Global.EMERGENCY_TONE, 0)).isEqualTo(0); 124 } 125 126 @Test 127 public void onPreferenceChanged_secondItemSelected_shouldSetEmergencyToneToAlert() { 128 mController.displayPreference(mScreen); 129 130 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "1"); 131 132 assertThat(Global.getInt(mContentResolver, Global.EMERGENCY_TONE, 0)).isEqualTo(1); 133 } 134 135 @Test 136 public void onPreferenceChanged_thirdItemSelected_shouldSetEmergencyToneToVibrate() { 137 mController.displayPreference(mScreen); 138 139 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, "2"); 140 141 assertThat(Global.getInt(mContentResolver, Global.EMERGENCY_TONE, 0)).isEqualTo(2); 142 } 143 } 144