Home | History | Annotate | Download | only in metrics
      1 /*
      2  * Copyright (C) 2016 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.metrics;
     18 
     19 import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_LTE;
     20 import static android.telephony.ServiceState.ROAMING_TYPE_DOMESTIC;
     21 
     22 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_DEACTIVATE_DATA_CALL;
     23 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SEND_SMS;
     24 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_SETUP_DATA_CALL;
     25 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_ADDRESS;
     26 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_DNS;
     27 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_GATEWAY;
     28 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_IFNAME;
     29 import static com.android.internal.telephony.dataconnection.DcTrackerTest.FAKE_PCSCF_ADDRESS;
     30 import static com.android.internal.telephony.nano.TelephonyProto.PdpType.PDP_TYPE_IPV4V6;
     31 
     32 import static org.junit.Assert.assertArrayEquals;
     33 import static org.junit.Assert.assertEquals;
     34 import static org.junit.Assert.assertFalse;
     35 import static org.junit.Assert.assertTrue;
     36 import static org.mockito.Mockito.doReturn;
     37 
     38 import android.hardware.radio.V1_0.SetupDataCallResult;
     39 import android.telephony.ServiceState;
     40 import android.telephony.TelephonyManager;
     41 import android.telephony.ims.ImsCallSession;
     42 import android.telephony.ims.ImsReasonInfo;
     43 import android.telephony.ims.feature.MmTelFeature;
     44 import android.telephony.ims.stub.ImsRegistrationImplBase;
     45 import android.test.suitebuilder.annotation.SmallTest;
     46 import android.util.Base64;
     47 
     48 import com.android.internal.telephony.Call;
     49 import com.android.internal.telephony.GsmCdmaConnection;
     50 import com.android.internal.telephony.PhoneConstants;
     51 import com.android.internal.telephony.SmsResponse;
     52 import com.android.internal.telephony.TelephonyTest;
     53 import com.android.internal.telephony.UUSInfo;
     54 import com.android.internal.telephony.nano.TelephonyProto;
     55 import com.android.internal.telephony.nano.TelephonyProto.ImsConnectionState;
     56 import com.android.internal.telephony.nano.TelephonyProto.RadioAccessTechnology;
     57 import com.android.internal.telephony.nano.TelephonyProto.SmsSession;
     58 import com.android.internal.telephony.nano.TelephonyProto.TelephonyCallSession;
     59 import com.android.internal.telephony.nano.TelephonyProto.TelephonyCallSession.Event.CallState;
     60 import com.android.internal.telephony.nano.TelephonyProto.TelephonyCallSession.Event.ImsCommand;
     61 import com.android.internal.telephony.nano.TelephonyProto.TelephonyCallSession.Event.RilCall;
     62 import com.android.internal.telephony.nano.TelephonyProto.TelephonyEvent;
     63 import com.android.internal.telephony.nano.TelephonyProto.TelephonyLog;
     64 import com.android.internal.telephony.nano.TelephonyProto.TelephonyServiceState;
     65 import com.android.internal.telephony.nano.TelephonyProto.TelephonyServiceState.RoamingType;
     66 
     67 import org.junit.After;
     68 import org.junit.Before;
     69 import org.junit.Test;
     70 import org.mockito.Mock;
     71 
     72 import java.lang.reflect.Method;
     73 
     74 public class TelephonyMetricsTest extends TelephonyTest {
     75 
     76     @Mock
     77     private ImsCallSession mImsCallSession;
     78 
     79     @Mock
     80     private ServiceState mServiceState;
     81 
     82     @Mock
     83     private GsmCdmaConnection mConnection;
     84 
     85     private TelephonyMetrics mMetrics;
     86 
     87     private UUSInfo mUusInfo;
     88 
     89     private ImsReasonInfo mImsReasonInfo;
     90 
     91     @Before
     92     public void setUp() throws Exception {
     93         super.setUp(getClass().getSimpleName());
     94         mMetrics = new TelephonyMetrics();
     95         mUusInfo = new UUSInfo(1, 2, new byte[]{1, 2});
     96         doReturn("123").when(mImsCallSession).getCallId();
     97         mImsReasonInfo = new ImsReasonInfo();
     98         mImsReasonInfo.mExtraMessage = "extramessage";
     99         mImsReasonInfo.mCode = 123;
    100         mImsReasonInfo.mExtraCode = 456;
    101 
    102         doReturn(ROAMING_TYPE_DOMESTIC).when(mServiceState).getVoiceRoamingType();
    103         doReturn(ROAMING_TYPE_DOMESTIC).when(mServiceState).getDataRoamingType();
    104         doReturn("voiceshort").when(mServiceState).getVoiceOperatorAlphaShort();
    105         doReturn("voicelong").when(mServiceState).getVoiceOperatorAlphaLong();
    106         doReturn("datashort").when(mServiceState).getDataOperatorAlphaShort();
    107         doReturn("datalong").when(mServiceState).getDataOperatorAlphaLong();
    108         doReturn("123456").when(mServiceState).getVoiceOperatorNumeric();
    109         doReturn("123456").when(mServiceState).getDataOperatorNumeric();
    110         doReturn(RIL_RADIO_TECHNOLOGY_LTE).when(mServiceState).getRilVoiceRadioTechnology();
    111         doReturn(RIL_RADIO_TECHNOLOGY_LTE).when(mServiceState).getRilDataRadioTechnology();
    112     }
    113 
    114     @After
    115     public void tearDown() throws Exception {
    116         super.tearDown();
    117     }
    118 
    119     private TelephonyLog buildProto() throws Exception {
    120         Method method = TelephonyMetrics.class.getDeclaredMethod("buildProto");
    121         method.setAccessible(true);
    122         return (TelephonyLog) method.invoke(mMetrics);
    123     }
    124 
    125     private void reset() throws Exception {
    126         Method method = TelephonyMetrics.class.getDeclaredMethod("reset");
    127         method.setAccessible(true);
    128         method.invoke(mMetrics);
    129     }
    130 
    131     private String convertProtoToBase64String(TelephonyLog log) throws Exception {
    132         Class[] cArgs = new Class[1];
    133         cArgs[0] = TelephonyLog.class;
    134         Method method = TelephonyMetrics.class.getDeclaredMethod("convertProtoToBase64String",
    135                 cArgs);
    136         method.setAccessible(true);
    137         return (String) method.invoke(null, log);
    138     }
    139 
    140     @Test
    141     @SmallTest
    142     public void testEventDropped() throws Exception {
    143         for (int i = 0; i < 1001; i++) {
    144             mMetrics.writeDataStallEvent(mPhone.getPhoneId(), i);
    145         }
    146         TelephonyLog log = buildProto();
    147         assertEquals(1000, log.events.length);
    148         assertEquals(0, log.callSessions.length);
    149         assertEquals(0, log.smsSessions.length);
    150         assertTrue(log.eventsDropped);
    151         assertEquals(1, log.events[0].dataStallAction);
    152     }
    153 
    154     // Test write data stall event
    155     @Test
    156     @SmallTest
    157     public void testWriteDataStallEvent() throws Exception {
    158         mMetrics.writeDataStallEvent(mPhone.getPhoneId(), 3);
    159         TelephonyLog log = buildProto();
    160 
    161         assertEquals(1, log.events.length);
    162         assertEquals(0, log.callSessions.length);
    163         assertEquals(0, log.smsSessions.length);
    164         assertEquals(mPhone.getPhoneId(), log.events[0].phoneId);
    165         assertEquals(3, log.events[0].dataStallAction);
    166     }
    167 
    168     // Test write modem restart event
    169     @Test
    170     @SmallTest
    171     public void testModemRestartEvent() throws Exception {
    172         mMetrics.writeModemRestartEvent(mPhone.getPhoneId(), "Test");
    173         TelephonyLog log = buildProto();
    174 
    175         assertEquals(1, log.events.length);
    176         assertEquals(0, log.callSessions.length);
    177         assertEquals(0, log.smsSessions.length);
    178 
    179         assertEquals(mPhone.getPhoneId(), log.events[0].phoneId);
    180         assertEquals("Test", log.events[0].modemRestart.reason);
    181     }
    182 
    183     // Test write Carrier Identification matching event
    184     @Test
    185     @SmallTest
    186     public void testWriteCarrierIdMatchingEventWithInvalidMatchingScore() throws Exception {
    187 
    188         mMetrics.writeCarrierIdMatchingEvent(mPhone.getPhoneId(), 1,
    189                 TelephonyManager.UNKNOWN_CARRIER_ID, "mccmncTest", "gid1Test");
    190         TelephonyLog log = buildProto();
    191 
    192         assertEquals(1, log.events.length);
    193         assertEquals(0, log.callSessions.length);
    194         assertEquals(0, log.smsSessions.length);
    195 
    196         assertEquals(mPhone.getPhoneId(), log.events[0].phoneId);
    197         assertEquals(1, log.events[0].carrierIdMatching.cidTableVersion);
    198         assertEquals(TelephonyEvent.Type.CARRIER_ID_MATCHING, log.events[0].type);
    199         assertEquals("mccmncTest", log.events[0].carrierIdMatching.result.mccmnc);
    200         assertTrue(log.events[0].carrierIdMatching.result.gid1.isEmpty());
    201     }
    202 
    203     // Test write Carrier Identification matching event
    204     @Test
    205     @SmallTest
    206     public void testWriteCarrierIdMatchingEvent() throws Exception {
    207 
    208         mMetrics.writeCarrierIdMatchingEvent(mPhone.getPhoneId(), 1, 1, "mccmncTest", "gid1Test");
    209         TelephonyLog log = buildProto();
    210 
    211         assertEquals(1, log.events.length);
    212         assertEquals(0, log.callSessions.length);
    213         assertEquals(0, log.smsSessions.length);
    214 
    215         assertEquals(mPhone.getPhoneId(), log.events[0].phoneId);
    216         assertEquals(TelephonyEvent.Type.CARRIER_ID_MATCHING, log.events[0].type);
    217         assertEquals(1, log.events[0].carrierIdMatching.cidTableVersion);
    218         assertEquals(1, log.events[0].carrierIdMatching.result.carrierId);
    219         assertEquals("mccmncTest", log.events[0].carrierIdMatching.result.mccmnc);
    220         assertEquals("gid1Test", log.events[0].carrierIdMatching.result.gid1);
    221     }
    222 
    223     // Test write on IMS call start
    224     @Test
    225     @SmallTest
    226     public void testWriteOnImsCallStart() throws Exception {
    227         mMetrics.writeOnImsCallStart(mPhone.getPhoneId(), mImsCallSession);
    228         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.IDLE);
    229         TelephonyLog log = buildProto();
    230 
    231         assertEquals(0, log.events.length);
    232         assertEquals(1, log.callSessions.length);
    233         assertEquals(0, log.smsSessions.length);
    234 
    235         assertEquals(mPhone.getPhoneId(), log.callSessions[0].phoneId);
    236 
    237         assertFalse(log.callSessions[0].eventsDropped);
    238 
    239         assertEquals(1, log.callSessions[0].events.length);
    240 
    241         assertEquals(123, log.callSessions[0].events[0].callIndex);
    242 
    243         assertEquals(ImsCommand.IMS_CMD_START, log.callSessions[0].events[0].imsCommand);
    244     }
    245 
    246     // Test write on IMS call received
    247     @Test
    248     @SmallTest
    249     public void testWriteOnImsCallReceive() throws Exception {
    250         mMetrics.writeOnImsCallReceive(mPhone.getPhoneId(), mImsCallSession);
    251         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.IDLE);
    252         TelephonyLog log = buildProto();
    253 
    254         assertEquals(0, log.events.length);
    255         assertEquals(1, log.callSessions.length);
    256         assertEquals(0, log.smsSessions.length);
    257         assertEquals(mPhone.getPhoneId(), log.callSessions[0].phoneId);
    258 
    259         assertFalse(log.callSessions[0].eventsDropped);
    260 
    261         assertEquals(1, log.callSessions[0].events.length);
    262 
    263         assertEquals(123, log.callSessions[0].events[0].callIndex);
    264     }
    265 
    266     // Test write ims call state
    267     @Test
    268     @SmallTest
    269     public void testWriteImsCallState() throws Exception {
    270         mMetrics.writeOnImsCallStart(mPhone.getPhoneId(), mImsCallSession);
    271         mMetrics.writeImsCallState(mPhone.getPhoneId(), mImsCallSession, Call.State.ACTIVE);
    272         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.IDLE);
    273         TelephonyLog log = buildProto();
    274 
    275         assertEquals(0, log.events.length);
    276         assertEquals(1, log.callSessions.length);
    277         assertEquals(0, log.smsSessions.length);
    278         assertEquals(2, log.callSessions[0].events.length);
    279         assertFalse(log.callSessions[0].eventsDropped);
    280 
    281         assertEquals(123, log.callSessions[0].events[1].callIndex);
    282 
    283         assertEquals(CallState.CALL_ACTIVE, log.callSessions[0].events[1].callState);
    284     }
    285 
    286     // Test write ims set feature value
    287     @Test
    288     @SmallTest
    289     public void testWriteImsSetFeatureValue() throws Exception {
    290         mMetrics.writeOnImsCallStart(mPhone.getPhoneId(), mImsCallSession);
    291         mMetrics.writeImsSetFeatureValue(mPhone.getPhoneId(),
    292                 MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE,
    293                 ImsRegistrationImplBase.REGISTRATION_TECH_LTE, 1);
    294         mMetrics.writeImsSetFeatureValue(mPhone.getPhoneId(),
    295                 MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE,
    296                 ImsRegistrationImplBase.REGISTRATION_TECH_LTE, 1);
    297         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.IDLE);
    298         TelephonyLog log = buildProto();
    299 
    300         assertEquals(1, log.events.length);
    301         assertEquals(1, log.callSessions.length);
    302         assertEquals(0, log.smsSessions.length);
    303         assertEquals(2, log.callSessions[0].events.length);
    304         assertFalse(log.callSessions[0].eventsDropped);
    305         assertTrue(log.callSessions[0].events[1].settings.isEnhanced4GLteModeEnabled);
    306     }
    307 
    308     // Test write on ims call handover event
    309     @Test
    310     @SmallTest
    311     public void testWriteOnImsCallHandoverEvent() throws Exception {
    312         mMetrics.writeOnImsCallStart(mPhone.getPhoneId(), mImsCallSession);
    313         mMetrics.writeOnImsCallHandoverEvent(mPhone.getPhoneId(),
    314                 TelephonyCallSession.Event.Type.IMS_CALL_HANDOVER, mImsCallSession, 5, 6,
    315                 mImsReasonInfo);
    316         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.IDLE);
    317         TelephonyLog log = buildProto();
    318 
    319         assertEquals(0, log.events.length);
    320         assertEquals(1, log.callSessions.length);
    321         assertEquals(0, log.smsSessions.length);
    322         assertEquals(2, log.callSessions[0].events.length);
    323         assertFalse(log.callSessions[0].eventsDropped);
    324         assertEquals(TelephonyCallSession.Event.Type.IMS_CALL_HANDOVER,
    325                 log.callSessions[0].events[1].type);
    326         assertEquals(123, log.callSessions[0].events[1].callIndex);
    327         assertEquals(5, log.callSessions[0].events[1].srcAccessTech);
    328         assertEquals(6, log.callSessions[0].events[1].targetAccessTech);
    329 
    330         assertEquals("extramessage", log.callSessions[0].events[1].reasonInfo.extraMessage);
    331         assertEquals(456, log.callSessions[0].events[1].reasonInfo.extraCode);
    332         assertEquals(123, log.callSessions[0].events[1].reasonInfo.reasonCode);
    333     }
    334 
    335     // Test write on ims command
    336     @Test
    337     @SmallTest
    338     public void testWriteOnImsCommand() throws Exception {
    339         mMetrics.writeOnImsCallStart(mPhone.getPhoneId(), mImsCallSession);
    340         mMetrics.writeOnImsCommand(mPhone.getPhoneId(), mImsCallSession, 123);
    341         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.IDLE);
    342         TelephonyLog log = buildProto();
    343 
    344         assertEquals(0, log.events.length);
    345         assertEquals(1, log.callSessions.length);
    346         assertEquals(0, log.smsSessions.length);
    347         assertEquals(2, log.callSessions[0].events.length);
    348 
    349         assertFalse(log.callSessions[0].eventsDropped);
    350 
    351         assertEquals(TelephonyCallSession.Event.Type.IMS_COMMAND,
    352                 log.callSessions[0].events[1].type);
    353 
    354         assertEquals(123, log.callSessions[0].events[1].imsCommand);
    355 
    356         assertEquals(123, log.callSessions[0].events[1].callIndex);
    357     }
    358 
    359     // Test write on ims connection state
    360     @Test
    361     @SmallTest
    362     public void testWriteOnImsConnectionState() throws Exception {
    363         mMetrics.writeOnImsCallStart(mPhone.getPhoneId(), mImsCallSession);
    364         mMetrics.writeOnImsConnectionState(mPhone.getPhoneId(),
    365                 ImsConnectionState.State.CONNECTED, mImsReasonInfo);
    366         mMetrics.writeOnImsConnectionState(mPhone.getPhoneId(),
    367                 ImsConnectionState.State.CONNECTED, mImsReasonInfo);
    368         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.IDLE);
    369         TelephonyLog log = buildProto();
    370 
    371         assertEquals(1, log.events.length);
    372         assertEquals(1, log.callSessions.length);
    373         assertEquals(0, log.smsSessions.length);
    374         assertEquals(2, log.callSessions[0].events.length);
    375         assertFalse(log.eventsDropped);
    376         assertEquals(TelephonyEvent.Type.IMS_CONNECTION_STATE_CHANGED, log.events[0].type);
    377         assertEquals(ImsConnectionState.State.CONNECTED,
    378                 log.events[0].imsConnectionState.state);
    379         assertEquals(123, log.events[0].imsConnectionState.reasonInfo.reasonCode);
    380         assertEquals(456, log.events[0].imsConnectionState.reasonInfo.extraCode);
    381         assertEquals("extramessage", log.events[0].imsConnectionState.reasonInfo.extraMessage);
    382         assertFalse(log.callSessions[0].eventsDropped);
    383         assertEquals(TelephonyCallSession.Event.Type.IMS_CONNECTION_STATE_CHANGED,
    384                 log.callSessions[0].events[1].type);
    385         assertEquals(ImsConnectionState.State.CONNECTED,
    386                 log.callSessions[0].events[1].imsConnectionState.state);
    387     }
    388 
    389     // Test write on setup data call response
    390     @Test
    391     @SmallTest
    392     public void testWriteOnSetupDataCallResponse() throws Exception {
    393         SetupDataCallResult result = new SetupDataCallResult();
    394         result.status = 5;
    395         result.suggestedRetryTime = 6;
    396         result.cid = 7;
    397         result.active = 8;
    398         result.type = "IPV4V6";
    399         result.ifname = FAKE_IFNAME;
    400         result.addresses = FAKE_ADDRESS;
    401         result.dnses = FAKE_DNS;
    402         result.gateways = FAKE_GATEWAY;
    403         result.pcscf = FAKE_PCSCF_ADDRESS;
    404         result.mtu = 1440;
    405 
    406         mMetrics.writeOnRilSolicitedResponse(mPhone.getPhoneId(), 1, 2,
    407                 RIL_REQUEST_SETUP_DATA_CALL, result);
    408         TelephonyLog log = buildProto();
    409 
    410         assertEquals(1, log.events.length);
    411         assertEquals(0, log.callSessions.length);
    412         assertEquals(0, log.smsSessions.length);
    413         assertFalse(log.eventsDropped);
    414 
    415         TelephonyEvent.RilSetupDataCallResponse respProto = log.events[0].setupDataCallResponse;
    416 
    417         assertEquals(5, respProto.status);
    418         assertEquals(6, respProto.suggestedRetryTimeMillis);
    419         assertEquals(7, respProto.call.cid);
    420         assertEquals(PDP_TYPE_IPV4V6, respProto.call.type);
    421         assertEquals(FAKE_IFNAME, respProto.call.iframe);
    422     }
    423 
    424     // Test write on deactivate data call response
    425     @Test
    426     @SmallTest
    427     public void testWriteOnDeactivateDataCallResponse() throws Exception {
    428         mMetrics.writeOnRilSolicitedResponse(mPhone.getPhoneId(), 2, 3,
    429                 RIL_REQUEST_DEACTIVATE_DATA_CALL, null);
    430         TelephonyLog log = buildProto();
    431 
    432         assertEquals(1, log.events.length);
    433         assertEquals(0, log.callSessions.length);
    434         assertEquals(0, log.smsSessions.length);
    435         assertFalse(log.eventsDropped);
    436 
    437         assertEquals(TelephonyEvent.Type.DATA_CALL_DEACTIVATE_RESPONSE, log.events[0].type);
    438         assertEquals(4, log.events[0].error);
    439     }
    440 
    441     // Test write RIL send SMS
    442     @Test
    443     @SmallTest
    444     public void testWriteRilSendSms() throws Exception {
    445         mMetrics.writeRilSendSms(mPhone.getPhoneId(), 1, 2, 1);
    446         mMetrics.writeRilSendSms(mPhone.getPhoneId(), 4, 5, 2);
    447 
    448         SmsResponse response = new SmsResponse(0, null, 123);
    449 
    450         mMetrics.writeOnRilSolicitedResponse(mPhone.getPhoneId(), 1, 0, RIL_REQUEST_SEND_SMS,
    451                 response);
    452         response = new SmsResponse(0, null, 456);
    453         mMetrics.writeOnRilSolicitedResponse(mPhone.getPhoneId(), 4, 0, RIL_REQUEST_SEND_SMS,
    454                 response);
    455         TelephonyLog log = buildProto();
    456 
    457         assertEquals(0, log.events.length);
    458         assertEquals(0, log.callSessions.length);
    459         assertEquals(1, log.smsSessions.length);
    460         assertFalse(log.eventsDropped);
    461 
    462         SmsSession.Event[] events = log.smsSessions[0].events;
    463         assertEquals(4, events.length);
    464         assertEquals(SmsSession.Event.Type.SMS_SEND, events[0].type);
    465         assertEquals(1, events[0].rilRequestId);
    466         assertEquals(2, events[0].tech);
    467         assertEquals(1, events[0].format);
    468 
    469         assertEquals(SmsSession.Event.Type.SMS_SEND, events[1].type);
    470         assertEquals(4, events[1].rilRequestId);
    471         assertEquals(5, events[1].tech);
    472         assertEquals(2, events[1].format);
    473 
    474         assertEquals(SmsSession.Event.Type.SMS_SEND_RESULT, events[2].type);
    475         assertEquals(1, events[2].rilRequestId);
    476         assertEquals(1, events[2].error);
    477         assertEquals(123, events[2].errorCode);
    478 
    479         assertEquals(SmsSession.Event.Type.SMS_SEND_RESULT, events[3].type);
    480         assertEquals(4, events[3].rilRequestId);
    481         assertEquals(1, events[3].error);
    482         assertEquals(456, events[3].errorCode);
    483     }
    484 
    485     // Test write phone state
    486     @Test
    487     @SmallTest
    488     public void testWritePhoneState() throws Exception {
    489         mMetrics.writeOnImsCallStart(mPhone.getPhoneId(), mImsCallSession);
    490         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.OFFHOOK);
    491         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.IDLE);
    492         TelephonyLog log = buildProto();
    493 
    494         assertEquals(0, log.events.length);
    495         assertEquals(1, log.callSessions.length);
    496         assertEquals(0, log.smsSessions.length);
    497         assertFalse(log.eventsDropped);
    498 
    499         assertEquals(mPhone.getPhoneId(), log.callSessions[0].phoneId);
    500         assertEquals(2, log.callSessions[0].events.length);
    501         assertEquals(TelephonyCallSession.Event.Type.PHONE_STATE_CHANGED,
    502                 log.callSessions[0].events[1].type);
    503         assertEquals(TelephonyCallSession.Event.PhoneState.STATE_OFFHOOK,
    504                 log.callSessions[0].events[1].phoneState);
    505     }
    506 
    507     // Test write RIL dial and hangup
    508     @Test
    509     @SmallTest
    510     public void testWriteRilDialHangup() throws Exception {
    511         doReturn(Call.State.DIALING).when(mConnection).getState();
    512         mMetrics.writeRilDial(mPhone.getPhoneId(), mConnection, 2, mUusInfo);
    513         doReturn(Call.State.DISCONNECTED).when(mConnection).getState();
    514         mMetrics.writeRilHangup(mPhone.getPhoneId(), mConnection, 3);
    515         mMetrics.writePhoneState(mPhone.getPhoneId(), PhoneConstants.State.IDLE);
    516         TelephonyLog log = buildProto();
    517 
    518         assertEquals(0, log.events.length);
    519         assertEquals(1, log.callSessions.length);
    520         assertEquals(0, log.smsSessions.length);
    521         assertFalse(log.eventsDropped);
    522 
    523         TelephonyCallSession.Event[] events = log.callSessions[0].events;
    524 
    525         assertEquals(2, events.length);
    526         assertEquals(TelephonyCallSession.Event.Type.RIL_REQUEST, events[0].type);
    527 
    528         assertEquals(TelephonyCallSession.Event.RilRequest.RIL_REQUEST_DIAL,
    529                 events[0].rilRequest);
    530         RilCall[] calls = events[0].calls;
    531         assertEquals(CallState.CALL_DIALING, calls[0].state);
    532 
    533         assertEquals(TelephonyCallSession.Event.RilRequest.RIL_REQUEST_HANGUP,
    534                 events[1].rilRequest);
    535         calls = events[1].calls;
    536         assertEquals(3, calls[0].index);
    537         assertEquals(CallState.CALL_DISCONNECTED, calls[0].state);
    538     }
    539 
    540     // Test write RIL setup data call
    541     @Test
    542     @SmallTest
    543     public void testWriteRilSetupDataCall() throws Exception {
    544         mMetrics.writeSetupDataCall(
    545                 mPhone.getPhoneId(), 14, 3, "apn", "IPV4V6");
    546 
    547         TelephonyLog log = buildProto();
    548 
    549         assertEquals(1, log.events.length);
    550         assertEquals(0, log.callSessions.length);
    551         assertEquals(0, log.smsSessions.length);
    552 
    553         assertFalse(log.eventsDropped);
    554 
    555 
    556         assertEquals(TelephonyEvent.Type.DATA_CALL_SETUP, log.events[0].type);
    557 
    558         TelephonyEvent.RilSetupDataCall setupDataCall = log.events[0].setupDataCall;
    559 
    560         assertEquals("apn", setupDataCall.apn);
    561 
    562         assertEquals(14, setupDataCall.rat);
    563 
    564         assertEquals(4, setupDataCall.dataProfile);
    565 
    566         assertEquals(PDP_TYPE_IPV4V6, setupDataCall.type);
    567     }
    568 
    569     // Test write service state changed
    570     @Test
    571     @SmallTest
    572     public void testWriteServiceStateChanged() throws Exception {
    573         mMetrics.writeServiceStateChanged(mPhone.getPhoneId(), mServiceState);
    574         mMetrics.writeServiceStateChanged(mPhone.getPhoneId(), mServiceState);
    575         TelephonyLog log = buildProto();
    576 
    577         assertEquals(1, log.events.length);
    578         assertEquals(0, log.callSessions.length);
    579         assertEquals(0, log.smsSessions.length);
    580 
    581         assertFalse(log.eventsDropped);
    582 
    583         TelephonyEvent event = log.events[0];
    584 
    585         assertEquals(TelephonyEvent.Type.RIL_SERVICE_STATE_CHANGED, event.type);
    586 
    587         TelephonyServiceState state = event.serviceState;
    588 
    589         assertEquals(RadioAccessTechnology.RAT_LTE, state.voiceRat);
    590 
    591         assertEquals(RadioAccessTechnology.RAT_LTE, state.dataRat);
    592 
    593         assertEquals(RoamingType.ROAMING_TYPE_DOMESTIC, state.voiceRoamingType);
    594 
    595         assertEquals(RoamingType.ROAMING_TYPE_DOMESTIC, state.dataRoamingType);
    596 
    597         assertEquals("voicelong", state.voiceOperator.alphaLong);
    598 
    599         assertEquals("voiceshort", state.voiceOperator.alphaShort);
    600 
    601         assertEquals("123456", state.voiceOperator.numeric);
    602 
    603         assertEquals("datalong", state.dataOperator.alphaLong);
    604 
    605         assertEquals("datashort", state.dataOperator.alphaShort);
    606 
    607         assertEquals("123456", state.dataOperator.numeric);
    608     }
    609 
    610     // Test reset scenario
    611     @Test
    612     @SmallTest
    613     public void testReset() throws Exception {
    614         mMetrics.writeServiceStateChanged(mPhone.getPhoneId(), mServiceState);
    615         reset();
    616         TelephonyLog log = buildProto();
    617 
    618         assertEquals(1, log.events.length);
    619         assertEquals(0, log.callSessions.length);
    620         assertEquals(0, log.smsSessions.length);
    621 
    622         assertFalse(log.eventsDropped);
    623 
    624         TelephonyEvent event = log.events[0];
    625 
    626         assertEquals(TelephonyEvent.Type.RIL_SERVICE_STATE_CHANGED, event.type);
    627 
    628         TelephonyServiceState state = event.serviceState;
    629 
    630         assertEquals(RadioAccessTechnology.RAT_LTE, state.voiceRat);
    631 
    632         assertEquals(RadioAccessTechnology.RAT_LTE, state.dataRat);
    633 
    634         assertEquals(RoamingType.ROAMING_TYPE_DOMESTIC, state.voiceRoamingType);
    635 
    636         assertEquals(RoamingType.ROAMING_TYPE_DOMESTIC, state.dataRoamingType);
    637 
    638         assertEquals("voicelong", state.voiceOperator.alphaLong);
    639 
    640         assertEquals("voiceshort", state.voiceOperator.alphaShort);
    641 
    642         assertEquals("123456", state.voiceOperator.numeric);
    643 
    644         assertEquals("datalong", state.dataOperator.alphaLong);
    645 
    646         assertEquals("datashort", state.dataOperator.alphaShort);
    647 
    648         assertEquals("123456", state.dataOperator.numeric);
    649     }
    650 
    651     // Test Proto Encoding/Decoding
    652     @Test
    653     @SmallTest
    654     public void testProtoEncodingDecoding() throws Exception {
    655         mMetrics.writeServiceStateChanged(mPhone.getPhoneId(), mServiceState);
    656         TelephonyLog log = buildProto();
    657         String encodedString = convertProtoToBase64String(log);
    658 
    659         byte[] decodedString = Base64.decode(encodedString, Base64.DEFAULT);
    660         assertArrayEquals(TelephonyProto.TelephonyLog.toByteArray(log), decodedString);
    661     }
    662 
    663     // Test write ims capabilities changed
    664     @Test
    665     @SmallTest
    666     public void testWriteOnImsCapabilities() throws Exception {
    667         MmTelFeature.MmTelCapabilities caps1 = new MmTelFeature.MmTelCapabilities();
    668         caps1.addCapabilities(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE);
    669         caps1.addCapabilities(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT);
    670         mMetrics.writeOnImsCapabilities(mPhone.getPhoneId(),
    671                 ImsRegistrationImplBase.REGISTRATION_TECH_LTE, caps1);
    672         // The duplicate one should be filtered out.
    673         mMetrics.writeOnImsCapabilities(mPhone.getPhoneId(),
    674                 ImsRegistrationImplBase.REGISTRATION_TECH_LTE, caps1);
    675         MmTelFeature.MmTelCapabilities caps2 = new MmTelFeature.MmTelCapabilities();
    676         caps2.addCapabilities(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO);
    677         caps2.addCapabilities(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT);
    678         mMetrics.writeOnImsCapabilities(mPhone.getPhoneId(),
    679                 ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN, caps2);
    680         TelephonyLog log = buildProto();
    681 
    682         assertEquals(2, log.events.length);
    683         assertEquals(0, log.callSessions.length);
    684         assertEquals(0, log.smsSessions.length);
    685 
    686         TelephonyEvent event = log.events[0];
    687 
    688         assertEquals(TelephonyEvent.Type.IMS_CAPABILITIES_CHANGED, event.type);
    689         assertEquals(caps1.isCapable(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE),
    690                 event.imsCapabilities.voiceOverLte);
    691         assertEquals(caps1.isCapable(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO),
    692                 event.imsCapabilities.videoOverLte);
    693         assertEquals(caps1.isCapable(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT),
    694                 event.imsCapabilities.utOverLte);
    695         assertEquals(false, event.imsCapabilities.voiceOverWifi);
    696         assertEquals(false, event.imsCapabilities.videoOverWifi);
    697         assertEquals(false, event.imsCapabilities.utOverWifi);
    698 
    699         event = log.events[1];
    700 
    701         assertEquals(TelephonyEvent.Type.IMS_CAPABILITIES_CHANGED, event.type);
    702         assertEquals(caps2.isCapable(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE),
    703                 event.imsCapabilities.voiceOverWifi);
    704         assertEquals(caps2.isCapable(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO),
    705                 event.imsCapabilities.videoOverWifi);
    706         assertEquals(caps2.isCapable(MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT),
    707                 event.imsCapabilities.utOverWifi);
    708         assertEquals(false, event.imsCapabilities.voiceOverLte);
    709         assertEquals(false, event.imsCapabilities.videoOverLte);
    710         assertEquals(false, event.imsCapabilities.utOverLte);
    711     }
    712 }
    713