1 /* 2 * Copyright (C) 2019 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 package com.android.settings.sim; 17 18 import static android.app.NotificationManager.IMPORTANCE_HIGH; 19 import static android.provider.Settings.ENABLE_MMS_DATA_REQUEST_REASON_INCOMING_MMS; 20 import static android.provider.Settings.ENABLE_MMS_DATA_REQUEST_REASON_OUTGOING_MMS; 21 import static android.provider.Settings.EXTRA_ENABLE_MMS_DATA_REQUEST_REASON; 22 import static android.provider.Settings.EXTRA_SUB_ID; 23 import static android.telephony.TelephonyManager.EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE; 24 import static android.telephony.TelephonyManager.EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_DATA; 25 import static android.telephony.TelephonyManager.EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_DISMISS; 26 import static android.telephony.TelephonyManager.EXTRA_SIM_COMBINATION_NAMES; 27 import static android.telephony.TelephonyManager.EXTRA_SIM_COMBINATION_WARNING_TYPE; 28 import static android.telephony.TelephonyManager.EXTRA_SIM_COMBINATION_WARNING_TYPE_DUAL_CDMA; 29 import static android.telephony.data.ApnSetting.TYPE_MMS; 30 31 import static com.android.settings.sim.SimDialogActivity.DATA_PICK; 32 import static com.android.settings.sim.SimDialogActivity.INVALID_PICK; 33 import static com.android.settings.sim.SimDialogActivity.PICK_DISMISS; 34 import static com.android.settings.sim.SimSelectNotification.ENABLE_MMS_NOTIFICATION_CHANNEL; 35 import static com.android.settings.sim.SimSelectNotification.ENABLE_MMS_NOTIFICATION_ID; 36 import static com.android.settings.sim.SimSelectNotification.SIM_WARNING_NOTIFICATION_CHANNEL; 37 import static com.android.settings.sim.SimSelectNotification.SIM_WARNING_NOTIFICATION_ID; 38 39 import static com.google.common.truth.Truth.assertThat; 40 41 import static org.mockito.ArgumentMatchers.any; 42 import static org.mockito.ArgumentMatchers.anyInt; 43 import static org.mockito.ArgumentMatchers.eq; 44 import static org.mockito.Mockito.clearInvocations; 45 import static org.mockito.Mockito.never; 46 import static org.mockito.Mockito.verify; 47 import static org.mockito.Mockito.when; 48 49 import android.app.Notification; 50 import android.app.NotificationChannel; 51 import android.app.NotificationManager; 52 import android.content.Context; 53 import android.content.Intent; 54 import android.content.pm.ApplicationInfo; 55 import android.content.pm.PackageManager; 56 import android.content.res.Resources; 57 import android.provider.Settings; 58 import android.telephony.SubscriptionInfo; 59 import android.telephony.SubscriptionManager; 60 import android.telephony.TelephonyManager; 61 import android.util.DisplayMetrics; 62 63 import com.android.settings.R; 64 import com.android.settings.network.SubscriptionUtil; 65 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; 66 67 import org.junit.Before; 68 import org.junit.Test; 69 import org.junit.runner.RunWith; 70 import org.mockito.ArgumentCaptor; 71 import org.mockito.Mock; 72 import org.mockito.MockitoAnnotations; 73 import org.robolectric.RobolectricTestRunner; 74 import org.robolectric.annotation.Config; 75 76 import java.util.Arrays; 77 78 @RunWith(RobolectricTestRunner.class) 79 @Config(shadows = ShadowAlertDialogCompat.class) 80 public class SimSelectNotificationTest { 81 @Mock 82 private Context mContext; 83 @Mock 84 private NotificationManager mNotificationManager; 85 @Mock 86 private TelephonyManager mTelephonyManager; 87 @Mock 88 private SubscriptionManager mSubscriptionManager; 89 @Mock 90 private PackageManager mPackageManager; 91 @Mock 92 private Resources mResources; 93 @Mock 94 private SubscriptionInfo mSubInfo; 95 @Mock 96 private DisplayMetrics mDisplayMetrics; 97 98 private final String mFakeDisplayName = "fake_display_name"; 99 private final CharSequence mFakeNotificationChannelTitle = "fake_notification_channel_title"; 100 private final CharSequence mFakeNotificationTitle = "fake_notification_title"; 101 private final String mFakeNotificationSummary = "fake_notification_Summary"; 102 103 // Dual CDMA combination notification. 104 private final String mFakeDualCdmaWarningChannelTitle = "fake_dual_cdma_warning_channel_title"; 105 private final String mFakeDualCdmaWarningTitle = "fake_dual_cdma_warning_title"; 106 private final String mFakeDualCdmaWarningSummary = "fake_dual_cdma_warning_summary"; 107 private final String mSimCombinationName = " carrier1 & carrier 2"; 108 109 private int mSubId = 1; 110 111 SimSelectNotification mSimSelectNotification = new SimSelectNotification(); 112 113 @Before setUp()114 public void setUp() { 115 MockitoAnnotations.initMocks(this); 116 when(mContext.getSystemService(Context.NOTIFICATION_SERVICE)) 117 .thenReturn(mNotificationManager); 118 when(mContext.getSystemService(NotificationManager.class)) 119 .thenReturn(mNotificationManager); 120 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)) 121 .thenReturn(mTelephonyManager); 122 when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager); 123 when(mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)) 124 .thenReturn(mSubscriptionManager); 125 when(mContext.getApplicationInfo()).thenReturn(new ApplicationInfo()); 126 when(mContext.getPackageManager()).thenReturn(mPackageManager); 127 when(mPackageManager.checkPermission(any(), any())) 128 .thenReturn(PackageManager.PERMISSION_GRANTED); 129 130 when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager); 131 when(mTelephonyManager.isDataEnabledForApn(TYPE_MMS)).thenReturn(false); 132 SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSubInfo)); 133 SubscriptionUtil.setActiveSubscriptionsForTesting(Arrays.asList(mSubInfo)); 134 when(mSubscriptionManager.isActiveSubscriptionId(mSubId)).thenReturn(true); 135 when(mSubscriptionManager.getActiveSubscriptionInfo(mSubId)).thenReturn(mSubInfo); 136 when(mSubInfo.getSubscriptionId()).thenReturn(mSubId); 137 when(mSubInfo.getDisplayName()).thenReturn(mFakeDisplayName); 138 when(mContext.getResources()).thenReturn(mResources); 139 140 when(mResources.getText(R.string.enable_sending_mms_notification_title)) 141 .thenReturn(mFakeNotificationTitle); 142 when(mResources.getText(R.string.enable_mms_notification_channel_title)) 143 .thenReturn(mFakeNotificationChannelTitle); 144 when(mResources.getString(R.string.enable_mms_notification_summary, 145 mFakeDisplayName)).thenReturn(mFakeNotificationSummary); 146 147 when(mResources.getText(R.string.dual_cdma_sim_warning_notification_channel_title)) 148 .thenReturn(mFakeDualCdmaWarningChannelTitle); 149 when(mResources.getText(R.string.sim_combination_warning_notification_title)) 150 .thenReturn(mFakeDualCdmaWarningTitle); 151 when(mResources.getString(R.string.dual_cdma_sim_warning_notification_summary, 152 mSimCombinationName)).thenReturn(mFakeDualCdmaWarningSummary); 153 154 when(mResources.getDisplayMetrics()).thenReturn(mDisplayMetrics); 155 mDisplayMetrics.density = 1.5f; 156 } 157 158 @Test onReceiveEnableMms_notificationShouldSend()159 public void onReceiveEnableMms_notificationShouldSend() { 160 Intent intent = new Intent(Settings.ACTION_ENABLE_MMS_DATA_REQUEST); 161 intent.putExtra(EXTRA_SUB_ID, mSubId); 162 intent.putExtra(EXTRA_ENABLE_MMS_DATA_REQUEST_REASON, 163 ENABLE_MMS_DATA_REQUEST_REASON_OUTGOING_MMS); 164 165 mSimSelectNotification.onReceive(mContext, intent); 166 167 // Capture the notification channel created and verify its fields. 168 ArgumentCaptor<NotificationChannel> nc = ArgumentCaptor.forClass(NotificationChannel.class); 169 verify(mNotificationManager).createNotificationChannel(nc.capture()); 170 171 assertThat(nc.getValue().getId()).isEqualTo(ENABLE_MMS_NOTIFICATION_CHANNEL); 172 assertThat(nc.getValue().getName()).isEqualTo(mFakeNotificationChannelTitle); 173 assertThat(nc.getValue().getImportance()).isEqualTo(IMPORTANCE_HIGH); 174 175 // Capture the notification it notifies and verify its fields. 176 ArgumentCaptor<Notification> notification = ArgumentCaptor.forClass(Notification.class); 177 verify(mNotificationManager).notify( 178 eq(ENABLE_MMS_NOTIFICATION_ID), notification.capture()); 179 assertThat(notification.getValue().extras.getCharSequence(Notification.EXTRA_TITLE)) 180 .isEqualTo(mFakeNotificationTitle); 181 assertThat(notification.getValue().extras.getCharSequence(Notification.EXTRA_BIG_TEXT)) 182 .isEqualTo(mFakeNotificationSummary); 183 assertThat(notification.getValue().contentIntent).isNotNull(); 184 } 185 186 @Test onReceiveEnableMms_NoExtra_notificationShouldNotSend()187 public void onReceiveEnableMms_NoExtra_notificationShouldNotSend() { 188 Intent intent = new Intent(Settings.ACTION_ENABLE_MMS_DATA_REQUEST); 189 190 // EXTRA_SUB_ID and EXTRA_ENABLE_MMS_DATA_REQUEST_REASON are required. 191 mSimSelectNotification.onReceive(mContext, intent); 192 verify(mNotificationManager, never()).createNotificationChannel(any()); 193 } 194 195 @Test onReceiveEnableMms_MmsDataAlreadyEnabled_notificationShouldNotSend()196 public void onReceiveEnableMms_MmsDataAlreadyEnabled_notificationShouldNotSend() { 197 when(mTelephonyManager.isDataEnabledForApn(TYPE_MMS)).thenReturn(true); 198 Intent intent = new Intent(Settings.ACTION_ENABLE_MMS_DATA_REQUEST); 199 intent.putExtra(EXTRA_SUB_ID, mSubId); 200 intent.putExtra(EXTRA_ENABLE_MMS_DATA_REQUEST_REASON, 201 ENABLE_MMS_DATA_REQUEST_REASON_INCOMING_MMS); 202 203 // If MMS data is already enabled, there's no need to trigger the notification. 204 mSimSelectNotification.onReceive(mContext, intent); 205 verify(mNotificationManager, never()).createNotificationChannel(any()); 206 } 207 208 @Test onReceivePrimarySubListChange_NoExtra_notificationShouldNotSend()209 public void onReceivePrimarySubListChange_NoExtra_notificationShouldNotSend() { 210 Intent intent = new Intent(TelephonyManager.ACTION_PRIMARY_SUBSCRIPTION_LIST_CHANGED); 211 212 // EXTRA_SUB_ID and EXTRA_ENABLE_MMS_DATA_REQUEST_REASON are required. 213 mSimSelectNotification.onReceive(mContext, intent); 214 verify(mNotificationManager, never()).createNotificationChannel(any()); 215 } 216 217 @Test onReceivePrimarySubListChange_WithDataPickExtra_shouldStartActivity()218 public void onReceivePrimarySubListChange_WithDataPickExtra_shouldStartActivity() { 219 Intent intent = new Intent(TelephonyManager.ACTION_PRIMARY_SUBSCRIPTION_LIST_CHANGED); 220 intent.putExtra(EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE, 221 EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_DATA); 222 223 mSimSelectNotification.onReceive(mContext, intent); 224 225 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 226 verify(mContext).startActivity(intentCaptor.capture()); 227 Intent capturedIntent = intentCaptor.getValue(); 228 assertThat(capturedIntent).isNotNull(); 229 assertThat(capturedIntent.getComponent().getClassName()).isEqualTo( 230 SimDialogActivity.class.getName()); 231 assertThat(capturedIntent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) 232 .isNotEqualTo(0); 233 assertThat(capturedIntent.getIntExtra(SimDialogActivity.DIALOG_TYPE_KEY, INVALID_PICK)) 234 .isEqualTo(DATA_PICK); 235 } 236 237 @Test onReceivePrimarySubListChange_WithDismissExtra_shouldDismiss()238 public void onReceivePrimarySubListChange_WithDismissExtra_shouldDismiss() { 239 Intent intent = new Intent(TelephonyManager.ACTION_PRIMARY_SUBSCRIPTION_LIST_CHANGED); 240 intent.putExtra(EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE, 241 EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_DATA); 242 243 mSimSelectNotification.onReceive(mContext, intent); 244 clearInvocations(mContext); 245 246 // Dismiss. 247 intent.putExtra(EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE, 248 EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_DISMISS); 249 mSimSelectNotification.onReceive(mContext, intent); 250 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 251 verify(mContext).startActivity(intentCaptor.capture()); 252 Intent capturedIntent = intentCaptor.getValue(); 253 assertThat(capturedIntent).isNotNull(); 254 assertThat(capturedIntent.getComponent().getClassName()).isEqualTo( 255 SimDialogActivity.class.getName()); 256 assertThat(capturedIntent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) 257 .isNotEqualTo(0); 258 assertThat(capturedIntent.getIntExtra(SimDialogActivity.DIALOG_TYPE_KEY, INVALID_PICK)) 259 .isEqualTo(PICK_DISMISS); 260 } 261 @Test onReceivePrimarySubListChange_DualCdmaWarning_notificationShouldSend()262 public void onReceivePrimarySubListChange_DualCdmaWarning_notificationShouldSend() { 263 Intent intent = new Intent(TelephonyManager.ACTION_PRIMARY_SUBSCRIPTION_LIST_CHANGED); 264 265 intent.putExtra(EXTRA_SIM_COMBINATION_NAMES, mSimCombinationName); 266 intent.putExtra(EXTRA_SIM_COMBINATION_WARNING_TYPE, 267 EXTRA_SIM_COMBINATION_WARNING_TYPE_DUAL_CDMA); 268 269 mSimSelectNotification.onReceive(mContext, intent); 270 271 // Capture the notification channel created and verify its fields. 272 ArgumentCaptor<NotificationChannel> nc = ArgumentCaptor.forClass(NotificationChannel.class); 273 verify(mNotificationManager).createNotificationChannel(nc.capture()); 274 275 assertThat(nc.getValue().getId()).isEqualTo(SIM_WARNING_NOTIFICATION_CHANNEL); 276 assertThat(nc.getValue().getName()).isEqualTo(mFakeDualCdmaWarningChannelTitle); 277 assertThat(nc.getValue().getImportance()).isEqualTo(IMPORTANCE_HIGH); 278 279 // Capture the notification it notifies and verify its fields. 280 ArgumentCaptor<Notification> notification = ArgumentCaptor.forClass(Notification.class); 281 verify(mNotificationManager).notify( 282 eq(SIM_WARNING_NOTIFICATION_ID), notification.capture()); 283 assertThat(notification.getValue().extras.getCharSequence(Notification.EXTRA_TITLE)) 284 .isEqualTo(mFakeDualCdmaWarningTitle); 285 assertThat(notification.getValue().extras.getCharSequence(Notification.EXTRA_BIG_TEXT)) 286 .isEqualTo(mFakeDualCdmaWarningSummary); 287 assertThat(notification.getValue().contentIntent).isNotNull(); 288 } 289 } 290