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