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 package com.android.internal.telephony; 17 18 import static com.android.internal.telephony.TelephonyTestUtils.waitForMs; 19 20 import static org.junit.Assert.assertEquals; 21 import static org.mockito.Matchers.anyLong; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.times; 24 import static org.mockito.Mockito.verify; 25 26 import android.content.Intent; 27 import android.database.ContentObserver; 28 import android.net.Uri; 29 import android.os.AsyncResult; 30 import android.os.Handler; 31 import android.os.HandlerThread; 32 import android.os.Message; 33 import android.provider.Settings; 34 import android.provider.Telephony; 35 import android.telephony.CarrierConfigManager; 36 import android.test.mock.MockContentResolver; 37 import android.test.suitebuilder.annotation.SmallTest; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.mockito.ArgumentCaptor; 43 import org.mockito.Mock; 44 45 public class CarrierActionAgentTest extends TelephonyTest { 46 private CarrierActionAgent mCarrierActionAgentUT; 47 private FakeContentResolver mFakeContentResolver; 48 private static int DATA_CARRIER_ACTION_EVENT = 0; 49 private static int RADIO_CARRIER_ACTION_EVENT = 1; 50 private CarrierActionAgentHandler mCarrierActionAgentHandler; 51 @Mock 52 private Handler mDataActionHandler; 53 @Mock 54 private Handler mRadioActionHandler; 55 56 private class FakeContentResolver extends MockContentResolver { 57 @Override 58 public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) { 59 super.notifyChange(uri, observer, syncToNetwork); 60 logd("onChanged(uri=" + uri + ")" + observer); 61 if (observer != null) { 62 observer.dispatchChange(false, uri); 63 } else { 64 mCarrierActionAgentUT.getContentObserver().dispatchChange(false, uri); 65 } 66 } 67 } 68 69 private class CarrierActionAgentHandler extends HandlerThread { 70 71 private CarrierActionAgentHandler(String name) { 72 super(name); 73 } 74 75 @Override 76 public void onLooperPrepared() { 77 mCarrierActionAgentUT = new CarrierActionAgent(mPhone); 78 mCarrierActionAgentUT.registerForCarrierAction( 79 CarrierActionAgent.CARRIER_ACTION_SET_METERED_APNS_ENABLED, mDataActionHandler, 80 DATA_CARRIER_ACTION_EVENT, null, false); 81 mCarrierActionAgentUT.registerForCarrierAction( 82 CarrierActionAgent.CARRIER_ACTION_SET_RADIO_ENABLED, mRadioActionHandler, 83 RADIO_CARRIER_ACTION_EVENT, null, false); 84 setReady(true); 85 } 86 } 87 88 @Before 89 public void setUp() throws Exception { 90 logd("CarrierActionAgentTest +Setup!"); 91 super.setUp(getClass().getSimpleName()); 92 mFakeContentResolver = new FakeContentResolver(); 93 doReturn(mFakeContentResolver).when(mContext).getContentResolver(); 94 mCarrierActionAgentHandler = new CarrierActionAgentHandler(getClass().getSimpleName()); 95 mCarrierActionAgentHandler.start(); 96 waitUntilReady(); 97 logd("CarrierActionAgentTest -Setup!"); 98 } 99 100 @Test 101 @SmallTest 102 public void testCarrierActionResetOnAPM() { 103 // setting observer register at sim loading 104 final Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); 105 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE, 106 IccCardConstants.INTENT_VALUE_ICC_LOADED); 107 mContext.sendBroadcast(intent); 108 waitForMs(200); 109 110 // no carrier actions triggered from sim loading since there are same as the current one 111 ArgumentCaptor<Message> message = ArgumentCaptor.forClass(Message.class); 112 verify(mDataActionHandler, times(0)).sendMessageAtTime(message.capture(), anyLong()); 113 verify(mRadioActionHandler, times(0)).sendMessageAtTime(message.capture(), anyLong()); 114 115 // disable metered apns and radio 116 mCarrierActionAgentUT.carrierActionSetRadioEnabled(false); 117 mCarrierActionAgentUT.carrierActionSetMeteredApnsEnabled(false); 118 waitForMs(200); 119 verify(mDataActionHandler, times(1)).sendMessageAtTime(message.capture(), anyLong()); 120 assertEquals(DATA_CARRIER_ACTION_EVENT, message.getValue().what); 121 assertEquals(false, ((AsyncResult) message.getValue().obj).result); 122 verify(mRadioActionHandler, times(1)).sendMessageAtTime(message.capture(), anyLong()); 123 assertEquals(RADIO_CARRIER_ACTION_EVENT, message.getValue().what); 124 assertEquals(false, ((AsyncResult) message.getValue().obj).result); 125 126 // simulate APM change from off -> on 127 Settings.Global.putInt(mFakeContentResolver, Settings.Global.AIRPLANE_MODE_ON, 1); 128 mFakeContentResolver.notifyChange( 129 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), null); 130 waitForMs(200); 131 132 // carrier actions triggered from APM 133 verify(mDataActionHandler, times(2)).sendMessageAtTime(message.capture(), anyLong()); 134 assertEquals(DATA_CARRIER_ACTION_EVENT, message.getValue().what); 135 assertEquals(true, ((AsyncResult) message.getValue().obj).result); 136 137 verify(mRadioActionHandler, times(2)).sendMessageAtTime(message.capture(), anyLong()); 138 assertEquals(RADIO_CARRIER_ACTION_EVENT, message.getValue().what); 139 assertEquals(true, ((AsyncResult) message.getValue().obj).result); 140 } 141 142 @Test 143 @SmallTest 144 public void testCarrierActionResetOnAPNChange() { 145 // Setting observer register at sim loading 146 final Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED); 147 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE, 148 IccCardConstants.INTENT_VALUE_ICC_LOADED); 149 mContext.sendBroadcast(intent); 150 waitForMs(200); 151 152 // no carrier actions triggered from sim loading since there are same as the current one 153 ArgumentCaptor<Message> message = ArgumentCaptor.forClass(Message.class); 154 verify(mDataActionHandler, times(0)).sendMessageAtTime(message.capture(), anyLong()); 155 verify(mRadioActionHandler, times(0)).sendMessageAtTime(message.capture(), anyLong()); 156 157 // disable metered apns and radio 158 mCarrierActionAgentUT.carrierActionSetRadioEnabled(false); 159 mCarrierActionAgentUT.carrierActionSetMeteredApnsEnabled(false); 160 waitForMs(200); 161 162 verify(mDataActionHandler, times(1)).sendMessageAtTime(message.capture(), anyLong()); 163 assertEquals(DATA_CARRIER_ACTION_EVENT, message.getValue().what); 164 assertEquals(false, ((AsyncResult) message.getValue().obj).result); 165 166 verify(mRadioActionHandler, times(1)).sendMessageAtTime(message.capture(), anyLong()); 167 assertEquals(RADIO_CARRIER_ACTION_EVENT, message.getValue().what); 168 assertEquals(false, ((AsyncResult) message.getValue().obj).result); 169 170 // Simulate APN change 171 mFakeContentResolver.notifyChange(Telephony.Carriers.CONTENT_URI, null); 172 waitForMs(200); 173 174 // Carrier actions triggered from APN change 175 verify(mDataActionHandler, times(2)).sendMessageAtTime(message.capture(), anyLong()); 176 assertEquals(DATA_CARRIER_ACTION_EVENT, message.getValue().what); 177 assertEquals(true, ((AsyncResult) message.getValue().obj).result); 178 179 verify(mRadioActionHandler, times(2)).sendMessageAtTime(message.capture(), anyLong()); 180 assertEquals(RADIO_CARRIER_ACTION_EVENT, message.getValue().what); 181 assertEquals(true, ((AsyncResult) message.getValue().obj).result); 182 } 183 184 @After 185 public void tearDown() throws Exception { 186 Settings.Global.putInt(mFakeContentResolver, Settings.Global.AIRPLANE_MODE_ON, 0); 187 mCarrierActionAgentHandler.quit(); 188 super.tearDown(); 189 } 190 } 191