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.internal.telephony; 18 19 import static com.android.internal.telephony.TelephonyTestUtils.waitForMs; 20 21 import static org.mockito.Mockito.any; 22 import static org.mockito.Mockito.atLeast; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.eq; 25 import static org.mockito.Mockito.isA; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.app.Notification; 31 import android.app.NotificationManager; 32 import android.content.Context; 33 import android.content.Intent; 34 import android.content.pm.ApplicationInfo; 35 import android.database.ContentObserver; 36 import android.net.Uri; 37 import android.os.HandlerThread; 38 import android.os.Message; 39 import android.os.PersistableBundle; 40 import android.provider.Settings; 41 import android.telephony.CarrierConfigManager; 42 import android.telephony.ServiceState; 43 import android.test.mock.MockContentResolver; 44 import android.test.suitebuilder.annotation.SmallTest; 45 46 import org.junit.After; 47 import org.junit.Before; 48 import org.junit.Test; 49 import org.mockito.MockitoAnnotations; 50 51 import java.util.Map; 52 53 /** 54 * Unit tests for {@link com.android.internal.telephony.CarrierServiceStateTracker}. 55 */ 56 public class CarrierServiceStateTrackerTest extends TelephonyTest { 57 public static final String LOG_TAG = "CSST"; 58 public static final int TEST_TIMEOUT = 5000; 59 60 private CarrierServiceStateTracker mSpyCarrierSST; 61 private CarrierServiceStateTracker mCarrierSST; 62 private CarrierServiceStateTrackerTestHandler mCarrierServiceStateTrackerTestHandler; 63 private FakeContentResolver mFakeContentResolver; 64 65 NotificationManager mNotificationManager; 66 PersistableBundle mBundle; 67 68 private class FakeContentResolver extends MockContentResolver { 69 @Override 70 public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) { 71 super.notifyChange(uri, observer, syncToNetwork); 72 logd("onChanged(uri=" + uri + ")" + observer); 73 if (observer != null) { 74 observer.dispatchChange(false, uri); 75 } 76 } 77 } 78 79 private class CarrierServiceStateTrackerTestHandler extends HandlerThread { 80 private CarrierServiceStateTrackerTestHandler(String name) { 81 super(name); 82 } 83 84 @Override 85 public void onLooperPrepared() { 86 mCarrierSST = new CarrierServiceStateTracker(mPhone, mSST); 87 mSpyCarrierSST = spy(mCarrierSST); 88 setReady(true); 89 } 90 } 91 92 @Before 93 public void setUp() throws Exception { 94 MockitoAnnotations.initMocks(this); 95 logd(LOG_TAG + "Setup!"); 96 super.setUp(getClass().getSimpleName()); 97 mBundle = mContextFixture.getCarrierConfigBundle(); 98 when(mPhone.getSubId()).thenReturn(1); 99 mCarrierServiceStateTrackerTestHandler = 100 new CarrierServiceStateTrackerTestHandler(getClass().getSimpleName()); 101 mCarrierServiceStateTrackerTestHandler.start(); 102 mFakeContentResolver = new CarrierServiceStateTrackerTest.FakeContentResolver(); 103 104 when(mPhone.getContext().getContentResolver()).thenReturn(mFakeContentResolver); 105 106 doReturn(new ApplicationInfo()).when(mContext).getApplicationInfo(); 107 108 mNotificationManager = (NotificationManager) mContext.getSystemService( 109 Context.NOTIFICATION_SERVICE); 110 111 setDefaultValues(); 112 waitUntilReady(); 113 } 114 115 private void setDefaultValues() { 116 mBundle.putInt(CarrierConfigManager.KEY_PREF_NETWORK_NOTIFICATION_DELAY_INT, 117 0); 118 mBundle.putInt(CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT, 119 0); 120 } 121 122 @After 123 public void tearDown() throws Exception { 124 mCarrierServiceStateTrackerTestHandler.quit(); 125 super.tearDown(); 126 } 127 128 @Test 129 @SmallTest 130 public void testCancelBothNotifications() { 131 logd(LOG_TAG + ":testCancelBothNotifications()"); 132 Message notificationMsg = mSpyCarrierSST.obtainMessage( 133 CarrierServiceStateTracker.CARRIER_EVENT_DATA_REGISTRATION, null); 134 doReturn(false).when(mSpyCarrierSST).evaluateSendingMessage(any()); 135 doReturn(mNotificationManager).when(mSpyCarrierSST).getNotificationManager(any()); 136 mSpyCarrierSST.handleMessage(notificationMsg); 137 waitForHandlerAction(mSpyCarrierSST, TEST_TIMEOUT); 138 verify(mNotificationManager).cancel( 139 CarrierServiceStateTracker.NOTIFICATION_EMERGENCY_NETWORK); 140 verify(mNotificationManager).cancel( 141 CarrierServiceStateTracker.NOTIFICATION_PREF_NETWORK); 142 } 143 144 @Test 145 @SmallTest 146 public void testSendBothNotifications() { 147 logd(LOG_TAG + ":testSendBothNotifications()"); 148 Notification.Builder mNotificationBuilder = new Notification.Builder(mContext); 149 Message notificationMsg = mSpyCarrierSST.obtainMessage( 150 CarrierServiceStateTracker.CARRIER_EVENT_DATA_DEREGISTRATION, null); 151 doReturn(true).when(mSpyCarrierSST).evaluateSendingMessage(any()); 152 doReturn(false).when(mSpyCarrierSST).isRadioOffOrAirplaneMode(); 153 doReturn(0).when(mSpyCarrierSST).getDelay(any()); 154 doReturn(mNotificationBuilder).when(mSpyCarrierSST).getNotificationBuilder(any()); 155 doReturn(mNotificationManager).when(mSpyCarrierSST).getNotificationManager(any()); 156 mSpyCarrierSST.handleMessage(notificationMsg); 157 waitForHandlerAction(mSpyCarrierSST, TEST_TIMEOUT); 158 verify(mNotificationManager).notify( 159 eq(CarrierServiceStateTracker.NOTIFICATION_PREF_NETWORK), isA(Notification.class)); 160 verify(mNotificationManager).notify( 161 eq(CarrierServiceStateTracker.NOTIFICATION_EMERGENCY_NETWORK), any()); 162 } 163 164 @Test 165 @SmallTest 166 public void testSendPrefNetworkNotification() { 167 logd(LOG_TAG + ":testSendPrefNetworkNotification()"); 168 Intent intent = new Intent().setAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); 169 mContext.sendBroadcast(intent); 170 waitForMs(300); 171 172 Map<Integer, CarrierServiceStateTracker.NotificationType> notificationTypeMap = 173 mCarrierSST.getNotificationTypeMap(); 174 CarrierServiceStateTracker.NotificationType prefNetworkNotification = 175 notificationTypeMap.get(CarrierServiceStateTracker.NOTIFICATION_PREF_NETWORK); 176 CarrierServiceStateTracker.NotificationType spyPrefNetworkNotification = spy( 177 prefNetworkNotification); 178 notificationTypeMap.put(CarrierServiceStateTracker.NOTIFICATION_PREF_NETWORK, 179 spyPrefNetworkNotification); 180 Notification.Builder mNotificationBuilder = new Notification.Builder(mContext); 181 doReturn(ServiceState.STATE_OUT_OF_SERVICE).when(mSST.mSS).getVoiceRegState(); 182 doReturn(ServiceState.STATE_OUT_OF_SERVICE).when(mSST.mSS).getDataRegState(); 183 doReturn(true).when(mSST).isRadioOn(); 184 doReturn(mNotificationBuilder).when(spyPrefNetworkNotification).getNotificationBuilder(); 185 186 String prefNetworkMode = Settings.Global.PREFERRED_NETWORK_MODE + mPhone.getSubId(); 187 Settings.Global.putInt(mFakeContentResolver, prefNetworkMode, 188 RILConstants.NETWORK_MODE_LTE_CDMA_EVDO); 189 mFakeContentResolver.notifyChange( 190 Settings.Global.getUriFor(prefNetworkMode), mSpyCarrierSST.getContentObserver()); 191 waitForMs(500); 192 verify(mNotificationManager).notify( 193 eq(CarrierServiceStateTracker.NOTIFICATION_PREF_NETWORK), isA(Notification.class)); 194 195 Settings.Global.putInt(mFakeContentResolver, prefNetworkMode, 196 RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA); 197 mFakeContentResolver.notifyChange( 198 Settings.Global.getUriFor(prefNetworkMode), mSpyCarrierSST.getContentObserver()); 199 waitForMs(500); 200 verify(mNotificationManager, atLeast(1)).cancel( 201 CarrierServiceStateTracker.NOTIFICATION_PREF_NETWORK); 202 } 203 } 204