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