• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.SmsManager.RESULT_ERROR_NONE;
20 import static android.telephony.SmsManager.RESULT_RIL_SMS_SEND_FAIL_RETRY;
21 import static android.telephony.TelephonyManager.NETWORK_TYPE_BITMASK_GPRS;
22 import static android.telephony.TelephonyManager.NETWORK_TYPE_BITMASK_GSM;
23 import static android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_SUCCESS;
24 import static android.text.format.DateUtils.DAY_IN_MILLIS;
25 import static android.text.format.DateUtils.HOUR_IN_MILLIS;
26 
27 import static com.android.internal.telephony.SmsResponse.NO_ERROR_CODE;
28 import static com.android.internal.telephony.TelephonyStatsLog.GBA_EVENT__FAILED_REASON__FEATURE_NOT_READY;
29 import static com.android.internal.telephony.TelephonyStatsLog.GBA_EVENT__FAILED_REASON__UNKNOWN;
30 import static com.android.internal.telephony.TelephonyStatsLog.INCOMING_SMS__ERROR__SMS_ERROR_GENERIC;
31 import static com.android.internal.telephony.TelephonyStatsLog.INCOMING_SMS__ERROR__SMS_SUCCESS;
32 import static com.android.internal.telephony.TelephonyStatsLog.INCOMING_SMS__SMS_FORMAT__SMS_FORMAT_3GPP;
33 import static com.android.internal.telephony.TelephonyStatsLog.INCOMING_SMS__SMS_FORMAT__SMS_FORMAT_3GPP2;
34 import static com.android.internal.telephony.TelephonyStatsLog.INCOMING_SMS__SMS_TECH__SMS_TECH_CS_3GPP;
35 import static com.android.internal.telephony.TelephonyStatsLog.INCOMING_SMS__SMS_TECH__SMS_TECH_CS_3GPP2;
36 import static com.android.internal.telephony.TelephonyStatsLog.INCOMING_SMS__SMS_TYPE__SMS_TYPE_NORMAL;
37 import static com.android.internal.telephony.TelephonyStatsLog.INCOMING_SMS__SMS_TYPE__SMS_TYPE_SMS_PP;
38 import static com.android.internal.telephony.TelephonyStatsLog.OUTGOING_SMS__SEND_RESULT__SMS_SEND_RESULT_ERROR;
39 import static com.android.internal.telephony.TelephonyStatsLog.OUTGOING_SMS__SEND_RESULT__SMS_SEND_RESULT_SUCCESS;
40 import static com.android.internal.telephony.TelephonyStatsLog.RCS_ACS_PROVISIONING_STATS__RESPONSE_TYPE__ERROR;
41 import static com.android.internal.telephony.TelephonyStatsLog.RCS_ACS_PROVISIONING_STATS__RESPONSE_TYPE__PROVISIONING_XML;
42 import static com.android.internal.telephony.TelephonyStatsLog.RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT;
43 import static com.android.internal.telephony.TelephonyStatsLog.RCS_CLIENT_PROVISIONING_STATS__EVENT__TRIGGER_RCS_RECONFIGURATION;
44 import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_CS;
45 import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
46 import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__DIRECTION__CALL_DIRECTION_MO;
47 import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION__DIRECTION__CALL_DIRECTION_MT;
48 import static com.android.internal.telephony.satellite.SatelliteConstants.ACCESS_CONTROL_TYPE_CURRENT_LOCATION;
49 import static com.android.internal.telephony.satellite.SatelliteConstants.ACCESS_CONTROL_TYPE_NETWORK_COUNTRY_CODE;
50 import static com.android.internal.telephony.satellite.SatelliteConstants.CONFIG_DATA_SOURCE_CONFIG_UPDATER;
51 import static com.android.internal.telephony.satellite.SatelliteConstants.CONFIG_DATA_SOURCE_DEVICE_CONFIG;
52 
53 import static org.junit.Assert.assertArrayEquals;
54 import static org.junit.Assert.assertEquals;
55 import static org.junit.Assert.assertNotNull;
56 import static org.junit.Assert.assertNull;
57 import static org.junit.Assert.assertTrue;
58 import static org.mockito.Mockito.anyInt;
59 import static org.mockito.Mockito.anyString;
60 import static org.mockito.Mockito.doReturn;
61 import static org.mockito.Mockito.eq;
62 import static org.mockito.Mockito.inOrder;
63 import static org.mockito.Mockito.mock;
64 import static org.mockito.Mockito.times;
65 
66 import android.annotation.Nullable;
67 import android.content.Context;
68 import android.os.Build;
69 import android.telephony.DisconnectCause;
70 import android.telephony.PreciseDataConnectionState;
71 import android.telephony.SatelliteProtoEnums;
72 import android.telephony.ServiceState;
73 import android.telephony.SignalStrength;
74 import android.telephony.TelephonyManager;
75 import android.telephony.TelephonyProtoEnums;
76 import android.telephony.data.ApnSetting;
77 import android.telephony.ims.ImsReasonInfo;
78 import android.telephony.ims.SipDelegateManager;
79 
80 import androidx.test.filters.SmallTest;
81 
82 import com.android.internal.telephony.TelephonyStatsLog;
83 import com.android.internal.telephony.TelephonyTest;
84 import com.android.internal.telephony.nano.PersistAtomsProto.CarrierRoamingSatelliteControllerStats;
85 import com.android.internal.telephony.nano.PersistAtomsProto.CarrierRoamingSatelliteSession;
86 import com.android.internal.telephony.nano.PersistAtomsProto.CellularDataServiceSwitch;
87 import com.android.internal.telephony.nano.PersistAtomsProto.CellularServiceState;
88 import com.android.internal.telephony.nano.PersistAtomsProto.DataCallSession;
89 import com.android.internal.telephony.nano.PersistAtomsProto.DataNetworkValidation;
90 import com.android.internal.telephony.nano.PersistAtomsProto.GbaEvent;
91 import com.android.internal.telephony.nano.PersistAtomsProto.ImsDedicatedBearerEvent;
92 import com.android.internal.telephony.nano.PersistAtomsProto.ImsDedicatedBearerListenerEvent;
93 import com.android.internal.telephony.nano.PersistAtomsProto.ImsRegistrationFeatureTagStats;
94 import com.android.internal.telephony.nano.PersistAtomsProto.ImsRegistrationServiceDescStats;
95 import com.android.internal.telephony.nano.PersistAtomsProto.ImsRegistrationStats;
96 import com.android.internal.telephony.nano.PersistAtomsProto.ImsRegistrationTermination;
97 import com.android.internal.telephony.nano.PersistAtomsProto.IncomingSms;
98 import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingShortCodeSms;
99 import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingSms;
100 import com.android.internal.telephony.nano.PersistAtomsProto.PersistAtoms;
101 import com.android.internal.telephony.nano.PersistAtomsProto.PresenceNotifyEvent;
102 import com.android.internal.telephony.nano.PersistAtomsProto.RcsAcsProvisioningStats;
103 import com.android.internal.telephony.nano.PersistAtomsProto.RcsClientProvisioningStats;
104 import com.android.internal.telephony.nano.PersistAtomsProto.SatelliteAccessController;
105 import com.android.internal.telephony.nano.PersistAtomsProto.SatelliteConfigUpdater;
106 import com.android.internal.telephony.nano.PersistAtomsProto.SatelliteController;
107 import com.android.internal.telephony.nano.PersistAtomsProto.SatelliteEntitlement;
108 import com.android.internal.telephony.nano.PersistAtomsProto.SatelliteIncomingDatagram;
109 import com.android.internal.telephony.nano.PersistAtomsProto.SatelliteOutgoingDatagram;
110 import com.android.internal.telephony.nano.PersistAtomsProto.SatelliteProvision;
111 import com.android.internal.telephony.nano.PersistAtomsProto.SatelliteSession;
112 import com.android.internal.telephony.nano.PersistAtomsProto.SatelliteSosMessageRecommender;
113 import com.android.internal.telephony.nano.PersistAtomsProto.SipDelegateStats;
114 import com.android.internal.telephony.nano.PersistAtomsProto.SipMessageResponse;
115 import com.android.internal.telephony.nano.PersistAtomsProto.SipTransportFeatureTagStats;
116 import com.android.internal.telephony.nano.PersistAtomsProto.SipTransportSession;
117 import com.android.internal.telephony.nano.PersistAtomsProto.UceEventStats;
118 import com.android.internal.telephony.nano.PersistAtomsProto.VoiceCallRatUsage;
119 import com.android.internal.telephony.nano.PersistAtomsProto.VoiceCallSession;
120 import com.android.internal.telephony.nano.TelephonyProto.TelephonyCallSession.Event.AudioCodec;
121 import com.android.internal.telephony.protobuf.nano.MessageNano;
122 
123 import org.junit.After;
124 import org.junit.Before;
125 import org.junit.Rule;
126 import org.junit.Test;
127 import org.junit.rules.TemporaryFolder;
128 import org.mockito.ArgumentCaptor;
129 import org.mockito.InOrder;
130 
131 import java.io.File;
132 import java.io.FileOutputStream;
133 import java.nio.charset.StandardCharsets;
134 import java.util.Arrays;
135 import java.util.Comparator;
136 import java.util.LinkedList;
137 import java.util.Queue;
138 import java.util.concurrent.TimeUnit;
139 
140 public class PersistAtomsStorageTest extends TelephonyTest {
141     private static final String TEST_FILE = "PersistAtomsStorageTest.pb";
142     private static final int MAX_NUM_CALL_SESSIONS = 50;
143     private static final long START_TIME_MILLIS = 2000L;
144     private static final int CARRIER1_ID = 1;
145     private static final int CARRIER2_ID = 1187;
146     private static final int CARRIER3_ID = 1435;
147     private static final int SLOT_ID1 = 1;
148     private static final int SLOT_ID2 = 2;
149     private static final int REGISTRATION1_TECH = 1;
150     private static final int REGISTRATION2_TECH = 2;
151 
152     // Mocked classes
153     private FileOutputStream mTestFileOutputStream;
154 
155     @Rule public TemporaryFolder mFolder = new TemporaryFolder();
156 
157     private File mTestFile;
158 
159     // call with SRVCC
160     private VoiceCallSession mCall1Proto;
161 
162     // call held after another incoming call, ended before the other call
163     private VoiceCallSession mCall2Proto;
164     private VoiceCallSession mCall3Proto;
165 
166     // failed call
167     private VoiceCallSession mCall4Proto;
168 
169     private VoiceCallRatUsage mCarrier1LteUsageProto;
170     private VoiceCallRatUsage mCarrier1UmtsUsageProto;
171     private VoiceCallRatUsage mCarrier2LteUsageProto;
172     private VoiceCallRatUsage mCarrier3LteUsageProto;
173     private VoiceCallRatUsage mCarrier3GsmUsageProto;
174 
175     private VoiceCallSession[] mVoiceCallSessions;
176     private VoiceCallRatUsage[] mVoiceCallRatUsages;
177 
178     // data service state switch for slot 0 and 1
179     private CellularDataServiceSwitch mServiceSwitch1Proto;
180     private CellularDataServiceSwitch mServiceSwitch2Proto;
181 
182     // Service states for slot 0 and 1
183     private CellularServiceState mServiceState1Proto;
184     private CellularServiceState mServiceState2Proto;
185     private CellularServiceState mServiceState3Proto;
186     private CellularServiceState mServiceState4Proto;
187     private CellularServiceState mServiceState5Proto;
188 
189     private CellularDataServiceSwitch[] mServiceSwitches;
190     private CellularServiceState[] mServiceStates;
191 
192     // IMS registrations for slot 0 and 1
193     private ImsRegistrationStats mImsRegStatsUnregisteredLte0;
194     private ImsRegistrationStats mImsRegStatsRegisteringLte0;
195     private ImsRegistrationStats mImsRegistrationStatsLte0;
196     private ImsRegistrationStats mImsRegistrationStatsWifi0;
197     private ImsRegistrationStats mImsRegistrationStatsLte1;
198 
199     // IMS registration terminations for slot 0 and 1
200     private ImsRegistrationTermination mImsRegistrationTerminationLte;
201     private ImsRegistrationTermination mImsRegistrationTerminationWifi;
202 
203     private ImsRegistrationStats[] mImsRegistrationStats;
204     private ImsRegistrationTermination[] mImsRegistrationTerminations;
205 
206     // Data call sessions
207     private DataCallSession mDataCallSession0;
208     private DataCallSession mDataCallSession1;
209 
210     // RCS registration feature tags for slot 0 and 1
211     private ImsRegistrationFeatureTagStats mImsRegistrationFeatureTagStats1Proto;
212     private ImsRegistrationFeatureTagStats mImsRegistrationFeatureTagStats2Proto;
213     private ImsRegistrationFeatureTagStats[] mImsRegistrationFeatureTagStatses;
214 
215     // RCS provisioning client stats for slot 0 and 1
216     private RcsClientProvisioningStats mRcsClientProvisioningStats1Proto;
217     private RcsClientProvisioningStats mRcsClientProvisioningStats2Proto;
218     private RcsClientProvisioningStats[] mRcsClientProvisioningStatses;
219 
220     // RCS provisioning ACS stats for slot 0 and 1
221     private RcsAcsProvisioningStats mRcsAcsProvisioningStats1Proto;
222     private RcsAcsProvisioningStats mRcsAcsProvisioningStats2Proto;
223     private RcsAcsProvisioningStats[] mRcsAcsProvisioningStatses;
224 
225     private ImsRegistrationServiceDescStats mImsRegistrationServiceIm;
226     private ImsRegistrationServiceDescStats mImsRegistrationServiceFt;
227     private ImsRegistrationServiceDescStats[] mImsRegistrationServiceDescStats;
228 
229     // IMS dedicated bearer listener event stats for slot 0 and 1
230     private ImsDedicatedBearerListenerEvent mImsDedicatedBearerListenerEvent1;
231     private ImsDedicatedBearerListenerEvent mImsDedicatedBearerListenerEvent2;
232     private ImsDedicatedBearerListenerEvent[] mImsDedicatedBearerListenerEvents;
233 
234     // IMS dedicated bearer event stats for slot 0 and 1
235     private ImsDedicatedBearerEvent mImsDedicatedBearerEvent1;
236     private ImsDedicatedBearerEvent mImsDedicatedBearerEvent2;
237     private ImsDedicatedBearerEvent[] mImsDedicatedBearerEvents;
238 
239     private UceEventStats mUceEventStats1;
240     private UceEventStats mUceEventStats2;
241     private UceEventStats[] mUceEventStatses;
242 
243     private PresenceNotifyEvent mPresenceNotifyEvent1;
244     private PresenceNotifyEvent mPresenceNotifyEvent2;
245     private PresenceNotifyEvent[] mPresenceNotifyEvents;
246 
247     private SipTransportFeatureTagStats mSipTransportFeatureTagStats1;
248     private SipTransportFeatureTagStats mSipTransportFeatureTagStats2;
249     private SipTransportFeatureTagStats[] mSipTransportFeatureTagStatsArray;
250 
251     private SipDelegateStats mSipDelegateStats1;
252     private SipDelegateStats mSipDelegateStats2;
253     private SipDelegateStats mSipDelegateStats3;
254     private SipDelegateStats[] mSipDelegateStatsArray;
255 
256     private GbaEvent mGbaEvent1;
257     private GbaEvent mGbaEvent2;
258     private GbaEvent[] mGbaEvent;
259 
260     private SipMessageResponse mSipMessageResponse1;
261     private SipMessageResponse mSipMessageResponse2;
262     private SipMessageResponse[] mSipMessageResponse;
263 
264     private SipTransportSession mSipTransportSession1;
265     private SipTransportSession mSipTransportSession2;
266     private SipTransportSession[] mSipTransportSession;
267 
268     private IncomingSms mIncomingSms1;
269     private IncomingSms mIncomingSms2;
270     private IncomingSms[] mIncomingSms;
271 
272     private OutgoingSms mOutgoingSms1;
273     private OutgoingSms mOutgoingSms2;
274     private OutgoingSms[] mOutgoingSms;
275 
276     private OutgoingShortCodeSms mOutgoingShortCodeSms1;
277     private OutgoingShortCodeSms mOutgoingShortCodeSms2;
278     private OutgoingShortCodeSms[] mOutgoingShortCodeSms;
279 
280     private SatelliteController mSatelliteController1;
281     private SatelliteController mSatelliteController2;
282     private SatelliteController[] mSatelliteControllers;
283 
284     private SatelliteSession mSatelliteSession1;
285     private SatelliteSession mSatelliteSession2;
286     private SatelliteSession[] mSatelliteSessions;
287 
288     private SatelliteIncomingDatagram mSatelliteIncomingDatagram1;
289     private SatelliteIncomingDatagram mSatelliteIncomingDatagram2;
290     private SatelliteIncomingDatagram[] mSatelliteIncomingDatagrams;
291 
292     private SatelliteOutgoingDatagram mSatelliteOutgoingDatagram1;
293     private SatelliteOutgoingDatagram mSatelliteOutgoingDatagram2;
294     private SatelliteOutgoingDatagram[] mSatelliteOutgoingDatagrams;
295 
296     private SatelliteProvision mSatelliteProvision1;
297     private SatelliteProvision mSatelliteProvision2;
298     private SatelliteProvision[] mSatelliteProvisions;
299 
300     private SatelliteSosMessageRecommender mSatelliteSosMessageRecommender1;
301     private SatelliteSosMessageRecommender mSatelliteSosMessageRecommender2;
302     private SatelliteSosMessageRecommender[] mSatelliteSosMessageRecommenders;
303 
304     private DataNetworkValidation mDataNetworkValidationLte1;
305     private DataNetworkValidation mDataNetworkValidationLte2;
306     private DataNetworkValidation mDataNetworkValidationIwlan1;
307     private DataNetworkValidation mDataNetworkValidationIwlan2;
308     private DataNetworkValidation[] mDataNetworkValidations;
309 
310     private CarrierRoamingSatelliteSession mCarrierRoamingSatelliteSession1;
311     private CarrierRoamingSatelliteSession mCarrierRoamingSatelliteSession2;
312     private CarrierRoamingSatelliteSession[] mCarrierRoamingSatelliteSessions;
313 
314     private CarrierRoamingSatelliteControllerStats mCarrierRoamingSatelliteControllerStats1;
315     private CarrierRoamingSatelliteControllerStats mCarrierRoamingSatelliteControllerStats2;
316     private CarrierRoamingSatelliteControllerStats[] mCarrierRoamingSatelliteControllerStats;
317 
318     private SatelliteEntitlement mSatelliteEntitlement1;
319     private SatelliteEntitlement mSatelliteEntitlement2;
320     private SatelliteEntitlement[] mSatelliteEntitlements;
321 
322     private SatelliteConfigUpdater mSatelliteConfigUpdater1;
323     private SatelliteConfigUpdater mSatelliteConfigUpdater2;
324     private SatelliteConfigUpdater[] mSatelliteConfigUpdaters;
325 
326     private SatelliteAccessController mSatelliteAccessController1;
327     private SatelliteAccessController mSatelliteAccessController2;
328     private SatelliteAccessController[] mSatelliteAccessControllers;
329 
makeTestData()330     private void makeTestData() {
331         // MO call with SRVCC (LTE to UMTS)
332         mCall1Proto = new VoiceCallSession();
333         mCall1Proto.bearerAtStart = VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
334         mCall1Proto.bearerAtEnd = VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_CS;
335         mCall1Proto.direction = VOICE_CALL_SESSION__DIRECTION__CALL_DIRECTION_MO;
336         mCall1Proto.setupFailed = false;
337         mCall1Proto.disconnectReasonCode = DisconnectCause.LOCAL;
338         mCall1Proto.disconnectExtraCode = 0;
339         mCall1Proto.disconnectExtraMessage = "";
340         mCall1Proto.ratAtStart = TelephonyManager.NETWORK_TYPE_LTE;
341         mCall1Proto.ratAtEnd = TelephonyManager.NETWORK_TYPE_UMTS;
342         mCall1Proto.ratSwitchCount = 1L;
343         mCall1Proto.codecBitmask =
344                 (1 << AudioCodec.AUDIO_CODEC_EVS_SWB) | (1 << AudioCodec.AUDIO_CODEC_AMR);
345         mCall1Proto.concurrentCallCountAtStart = 0;
346         mCall1Proto.concurrentCallCountAtEnd = 0;
347         mCall1Proto.simSlotIndex = 0;
348         mCall1Proto.isMultiSim = false;
349         mCall1Proto.isEsim = false;
350         mCall1Proto.carrierId = CARRIER1_ID;
351         mCall1Proto.srvccCompleted = true;
352         mCall1Proto.srvccFailureCount = 0L;
353         mCall1Proto.srvccCancellationCount = 0L;
354         mCall1Proto.rttEnabled = false;
355         mCall1Proto.isEmergency = false;
356         mCall1Proto.isRoaming = false;
357 
358         // VoLTE MT call on DSDS/eSIM, hanged up by remote
359         // concurrent with mCall3Proto, started first and ended first
360         mCall2Proto = new VoiceCallSession();
361         mCall2Proto.bearerAtStart = VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
362         mCall2Proto.bearerAtEnd = VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
363         mCall2Proto.direction = VOICE_CALL_SESSION__DIRECTION__CALL_DIRECTION_MT;
364         mCall2Proto.setupFailed = false;
365         mCall2Proto.disconnectReasonCode = ImsReasonInfo.CODE_USER_TERMINATED_BY_REMOTE;
366         mCall2Proto.disconnectExtraCode = 0;
367         mCall2Proto.disconnectExtraMessage = "normal call clearing";
368         mCall2Proto.ratAtStart = TelephonyManager.NETWORK_TYPE_LTE;
369         mCall2Proto.ratAtEnd = TelephonyManager.NETWORK_TYPE_LTE;
370         mCall2Proto.ratSwitchCount = 0L;
371         mCall2Proto.codecBitmask = (1 << AudioCodec.AUDIO_CODEC_EVS_SWB);
372         mCall2Proto.concurrentCallCountAtStart = 0;
373         mCall2Proto.concurrentCallCountAtEnd = 1;
374         mCall2Proto.simSlotIndex = 1;
375         mCall2Proto.isMultiSim = true;
376         mCall2Proto.isEsim = true;
377         mCall2Proto.carrierId = CARRIER2_ID;
378         mCall2Proto.srvccCompleted = false;
379         mCall2Proto.srvccFailureCount = 0L;
380         mCall2Proto.srvccCancellationCount = 0L;
381         mCall2Proto.rttEnabled = false;
382         mCall2Proto.isEmergency = false;
383         mCall2Proto.isRoaming = false;
384 
385         // VoLTE MT call on DSDS/eSIM, hanged up by local, with RTT
386         // concurrent with mCall2Proto, started last and ended last
387         mCall3Proto = new VoiceCallSession();
388         mCall3Proto.bearerAtStart = VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
389         mCall3Proto.bearerAtEnd = VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_IMS;
390         mCall3Proto.direction = VOICE_CALL_SESSION__DIRECTION__CALL_DIRECTION_MT;
391         mCall3Proto.setupFailed = false;
392         mCall3Proto.disconnectReasonCode = ImsReasonInfo.CODE_USER_TERMINATED;
393         mCall3Proto.disconnectExtraCode = 0;
394         mCall3Proto.disconnectExtraMessage = "normal call clearing";
395         mCall3Proto.ratAtStart = TelephonyManager.NETWORK_TYPE_LTE;
396         mCall3Proto.ratAtEnd = TelephonyManager.NETWORK_TYPE_LTE;
397         mCall3Proto.ratSwitchCount = 0L;
398         mCall3Proto.codecBitmask = (1 << AudioCodec.AUDIO_CODEC_EVS_SWB);
399         mCall3Proto.concurrentCallCountAtStart = 1;
400         mCall3Proto.concurrentCallCountAtEnd = 0;
401         mCall3Proto.simSlotIndex = 1;
402         mCall3Proto.isMultiSim = true;
403         mCall3Proto.isEsim = true;
404         mCall3Proto.carrierId = CARRIER2_ID;
405         mCall3Proto.srvccCompleted = false;
406         mCall3Proto.srvccFailureCount = 0L;
407         mCall3Proto.srvccCancellationCount = 0L;
408         mCall3Proto.rttEnabled = true;
409         mCall3Proto.isEmergency = false;
410         mCall3Proto.isRoaming = false;
411 
412         // CS MO emergency call while camped on LTE
413         mCall4Proto = new VoiceCallSession();
414         mCall4Proto.bearerAtStart = VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_CS;
415         mCall4Proto.bearerAtEnd = VOICE_CALL_SESSION__BEARER_AT_END__CALL_BEARER_CS;
416         mCall4Proto.direction = VOICE_CALL_SESSION__DIRECTION__CALL_DIRECTION_MO;
417         mCall4Proto.setupFailed = true;
418         mCall4Proto.disconnectReasonCode = DisconnectCause.NORMAL;
419         mCall4Proto.disconnectExtraCode = 0;
420         mCall4Proto.disconnectExtraMessage = "";
421         mCall4Proto.ratAtStart = TelephonyManager.NETWORK_TYPE_LTE;
422         mCall4Proto.ratAtEnd = TelephonyManager.NETWORK_TYPE_GSM;
423         mCall4Proto.ratSwitchCount = 1L;
424         mCall4Proto.codecBitmask = (1 << AudioCodec.AUDIO_CODEC_AMR);
425         mCall4Proto.concurrentCallCountAtStart = 0;
426         mCall4Proto.concurrentCallCountAtEnd = 0;
427         mCall4Proto.simSlotIndex = 0;
428         mCall4Proto.isMultiSim = true;
429         mCall4Proto.isEsim = false;
430         mCall4Proto.carrierId = CARRIER3_ID;
431         mCall4Proto.srvccCompleted = false;
432         mCall4Proto.srvccFailureCount = 0L;
433         mCall4Proto.srvccCancellationCount = 0L;
434         mCall4Proto.rttEnabled = false;
435         mCall4Proto.isEmergency = true;
436         mCall4Proto.isRoaming = true;
437 
438         mCarrier1LteUsageProto = new VoiceCallRatUsage();
439         mCarrier1LteUsageProto.carrierId = CARRIER1_ID;
440         mCarrier1LteUsageProto.rat = TelephonyManager.NETWORK_TYPE_LTE;
441         mCarrier1LteUsageProto.callCount = 1L;
442         mCarrier1LteUsageProto.totalDurationMillis = 8000L;
443 
444         mCarrier1UmtsUsageProto = new VoiceCallRatUsage();
445         mCarrier1UmtsUsageProto.carrierId = CARRIER1_ID;
446         mCarrier1UmtsUsageProto.rat = TelephonyManager.NETWORK_TYPE_UMTS;
447         mCarrier1UmtsUsageProto.callCount = 1L;
448         mCarrier1UmtsUsageProto.totalDurationMillis = 6000L;
449 
450         mCarrier2LteUsageProto = new VoiceCallRatUsage();
451         mCarrier2LteUsageProto.carrierId = CARRIER2_ID;
452         mCarrier2LteUsageProto.rat = TelephonyManager.NETWORK_TYPE_LTE;
453         mCarrier2LteUsageProto.callCount = 2L;
454         mCarrier2LteUsageProto.totalDurationMillis = 20000L;
455 
456         mCarrier3LteUsageProto = new VoiceCallRatUsage();
457         mCarrier3LteUsageProto.carrierId = CARRIER3_ID;
458         mCarrier3LteUsageProto.rat = TelephonyManager.NETWORK_TYPE_LTE;
459         mCarrier3LteUsageProto.callCount = 1L;
460         mCarrier3LteUsageProto.totalDurationMillis = 1000L;
461 
462         mCarrier3GsmUsageProto = new VoiceCallRatUsage();
463         mCarrier3GsmUsageProto.carrierId = CARRIER3_ID;
464         mCarrier3GsmUsageProto.rat = TelephonyManager.NETWORK_TYPE_GSM;
465         mCarrier3GsmUsageProto.callCount = 1L;
466         mCarrier3GsmUsageProto.totalDurationMillis = 100000L;
467 
468         mVoiceCallRatUsages =
469                 new VoiceCallRatUsage[] {
470                     mCarrier1UmtsUsageProto,
471                     mCarrier1LteUsageProto,
472                     mCarrier2LteUsageProto,
473                     mCarrier3LteUsageProto,
474                     mCarrier3GsmUsageProto
475                 };
476         mVoiceCallSessions =
477                 new VoiceCallSession[] {mCall1Proto, mCall2Proto, mCall3Proto, mCall4Proto};
478 
479         // OOS to LTE on slot 0
480         mServiceSwitch1Proto = new CellularDataServiceSwitch();
481         mServiceSwitch1Proto.ratFrom = TelephonyManager.NETWORK_TYPE_UNKNOWN;
482         mServiceSwitch1Proto.ratTo = TelephonyManager.NETWORK_TYPE_LTE;
483         mServiceSwitch1Proto.simSlotIndex = 0;
484         mServiceSwitch1Proto.isMultiSim = true;
485         mServiceSwitch1Proto.carrierId = CARRIER1_ID;
486         mServiceSwitch1Proto.switchCount = 1;
487 
488         // LTE to UMTS on slot 1
489         mServiceSwitch2Proto = new CellularDataServiceSwitch();
490         mServiceSwitch2Proto.ratFrom = TelephonyManager.NETWORK_TYPE_LTE;
491         mServiceSwitch2Proto.ratTo = TelephonyManager.NETWORK_TYPE_UMTS;
492         mServiceSwitch2Proto.simSlotIndex = 0;
493         mServiceSwitch2Proto.isMultiSim = true;
494         mServiceSwitch2Proto.carrierId = CARRIER2_ID;
495         mServiceSwitch2Proto.switchCount = 2;
496 
497         // OOS on slot 0
498         mServiceState1Proto = new CellularServiceState();
499         mServiceState1Proto.voiceRat = TelephonyManager.NETWORK_TYPE_UNKNOWN;
500         mServiceState1Proto.dataRat = TelephonyManager.NETWORK_TYPE_UNKNOWN;
501         mServiceState1Proto.voiceRoamingType = ServiceState.ROAMING_TYPE_NOT_ROAMING;
502         mServiceState1Proto.dataRoamingType = ServiceState.ROAMING_TYPE_NOT_ROAMING;
503         mServiceState1Proto.isEndc = false;
504         mServiceState1Proto.simSlotIndex = 0;
505         mServiceState1Proto.isMultiSim = true;
506         mServiceState1Proto.carrierId = CARRIER1_ID;
507         mServiceState1Proto.totalTimeMillis = 5000L;
508         mServiceState1Proto.isEmergencyOnly = false;
509 
510         // LTE with ENDC on slot 0
511         mServiceState2Proto = new CellularServiceState();
512         mServiceState2Proto.voiceRat = TelephonyManager.NETWORK_TYPE_LTE;
513         mServiceState2Proto.dataRat = TelephonyManager.NETWORK_TYPE_LTE;
514         mServiceState2Proto.voiceRoamingType = ServiceState.ROAMING_TYPE_NOT_ROAMING;
515         mServiceState2Proto.dataRoamingType = ServiceState.ROAMING_TYPE_NOT_ROAMING;
516         mServiceState2Proto.isEndc = true;
517         mServiceState2Proto.simSlotIndex = 0;
518         mServiceState2Proto.isMultiSim = true;
519         mServiceState2Proto.carrierId = CARRIER1_ID;
520         mServiceState2Proto.totalTimeMillis = 15000L;
521         mServiceState2Proto.isEmergencyOnly = false;
522 
523         // LTE with WFC and roaming on slot 1
524         mServiceState3Proto = new CellularServiceState();
525         mServiceState3Proto.voiceRat = TelephonyManager.NETWORK_TYPE_IWLAN;
526         mServiceState3Proto.dataRat = TelephonyManager.NETWORK_TYPE_LTE;
527         mServiceState3Proto.voiceRoamingType = ServiceState.ROAMING_TYPE_INTERNATIONAL;
528         mServiceState3Proto.dataRoamingType = ServiceState.ROAMING_TYPE_INTERNATIONAL;
529         mServiceState3Proto.isEndc = false;
530         mServiceState3Proto.simSlotIndex = 1;
531         mServiceState3Proto.isMultiSim = true;
532         mServiceState3Proto.carrierId = CARRIER2_ID;
533         mServiceState3Proto.totalTimeMillis = 10000L;
534         mServiceState3Proto.isEmergencyOnly = false;
535 
536         // UMTS with roaming on slot 1
537         mServiceState4Proto = new CellularServiceState();
538         mServiceState4Proto.voiceRat = TelephonyManager.NETWORK_TYPE_UMTS;
539         mServiceState4Proto.dataRat = TelephonyManager.NETWORK_TYPE_UMTS;
540         mServiceState4Proto.voiceRoamingType = ServiceState.ROAMING_TYPE_INTERNATIONAL;
541         mServiceState4Proto.dataRoamingType = ServiceState.ROAMING_TYPE_INTERNATIONAL;
542         mServiceState4Proto.isEndc = false;
543         mServiceState4Proto.simSlotIndex = 1;
544         mServiceState4Proto.isMultiSim = true;
545         mServiceState4Proto.carrierId = CARRIER2_ID;
546         mServiceState4Proto.totalTimeMillis = 10000L;
547         mServiceState4Proto.isEmergencyOnly = false;
548 
549         // Limited service on slot 0
550         mServiceState5Proto = new CellularServiceState();
551         mServiceState5Proto.voiceRat = TelephonyManager.NETWORK_TYPE_UNKNOWN;
552         mServiceState5Proto.dataRat = TelephonyManager.NETWORK_TYPE_UNKNOWN;
553         mServiceState5Proto.voiceRoamingType = ServiceState.ROAMING_TYPE_NOT_ROAMING;
554         mServiceState5Proto.dataRoamingType = ServiceState.ROAMING_TYPE_NOT_ROAMING;
555         mServiceState5Proto.isEndc = false;
556         mServiceState5Proto.simSlotIndex = 0;
557         mServiceState5Proto.isMultiSim = true;
558         mServiceState5Proto.carrierId = CARRIER1_ID;
559         mServiceState5Proto.totalTimeMillis = 15000L;
560         mServiceState5Proto.isEmergencyOnly = true;
561 
562         mServiceSwitches =
563                 new CellularDataServiceSwitch[] {mServiceSwitch1Proto, mServiceSwitch2Proto};
564         mServiceStates =
565                 new CellularServiceState[] {
566                     mServiceState1Proto,
567                     mServiceState2Proto,
568                     mServiceState3Proto,
569                     mServiceState4Proto,
570                     mServiceState5Proto
571                 };
572 
573         // IMS over LTE on slot 0, unregistered for 5 seconds at the registered state
574         mImsRegStatsUnregisteredLte0 = new ImsRegistrationStats();
575         mImsRegStatsUnregisteredLte0.carrierId = CARRIER1_ID;
576         mImsRegStatsUnregisteredLte0.simSlotIndex = 0;
577         mImsRegStatsUnregisteredLte0.rat = TelephonyManager.NETWORK_TYPE_LTE;
578         mImsRegStatsUnregisteredLte0.registeredMillis = 0;
579         mImsRegStatsUnregisteredLte0.voiceCapableMillis = 5000L;
580         mImsRegStatsUnregisteredLte0.voiceAvailableMillis = 5000L;
581         mImsRegStatsUnregisteredLte0.smsCapableMillis = 5000L;
582         mImsRegStatsUnregisteredLte0.smsAvailableMillis = 5000L;
583         mImsRegStatsUnregisteredLte0.videoCapableMillis = 5000L;
584         mImsRegStatsUnregisteredLte0.videoAvailableMillis = 5000L;
585         mImsRegStatsUnregisteredLte0.utCapableMillis = 5000L;
586         mImsRegStatsUnregisteredLte0.utAvailableMillis = 5000L;
587         mImsRegStatsUnregisteredLte0.registeringMillis = 0;
588         mImsRegStatsUnregisteredLte0.unregisteredMillis = 5000L;
589         mImsRegStatsUnregisteredLte0.registeredTimes = 0;
590 
591         // IMS over LTE on slot 0, registering for 5 seconds at the registered state
592         mImsRegStatsRegisteringLte0 = new ImsRegistrationStats();
593         mImsRegStatsRegisteringLte0.carrierId = CARRIER1_ID;
594         mImsRegStatsRegisteringLte0.simSlotIndex = 0;
595         mImsRegStatsRegisteringLte0.rat = TelephonyManager.NETWORK_TYPE_LTE;
596         mImsRegStatsRegisteringLte0.registeredMillis = 0;
597         mImsRegStatsRegisteringLte0.voiceCapableMillis = 5000L;
598         mImsRegStatsRegisteringLte0.voiceAvailableMillis = 5000L;
599         mImsRegStatsRegisteringLte0.smsCapableMillis = 5000L;
600         mImsRegStatsRegisteringLte0.smsAvailableMillis = 5000L;
601         mImsRegStatsRegisteringLte0.videoCapableMillis = 5000L;
602         mImsRegStatsRegisteringLte0.videoAvailableMillis = 5000L;
603         mImsRegStatsRegisteringLte0.utCapableMillis = 5000L;
604         mImsRegStatsRegisteringLte0.utAvailableMillis = 5000L;
605         mImsRegStatsRegisteringLte0.registeringMillis = 5000L;
606         mImsRegStatsRegisteringLte0.unregisteredMillis = 0;
607         mImsRegStatsRegisteringLte0.registeredTimes = 1;
608 
609         // IMS over LTE on slot 0, registered for 5 seconds
610         mImsRegistrationStatsLte0 = new ImsRegistrationStats();
611         mImsRegistrationStatsLte0.carrierId = CARRIER1_ID;
612         mImsRegistrationStatsLte0.simSlotIndex = 0;
613         mImsRegistrationStatsLte0.rat = TelephonyManager.NETWORK_TYPE_LTE;
614         mImsRegistrationStatsLte0.registeredMillis = 5000L;
615         mImsRegistrationStatsLte0.voiceCapableMillis = 5000L;
616         mImsRegistrationStatsLte0.voiceAvailableMillis = 5000L;
617         mImsRegistrationStatsLte0.smsCapableMillis = 5000L;
618         mImsRegistrationStatsLte0.smsAvailableMillis = 5000L;
619         mImsRegistrationStatsLte0.videoCapableMillis = 5000L;
620         mImsRegistrationStatsLte0.videoAvailableMillis = 5000L;
621         mImsRegistrationStatsLte0.utCapableMillis = 5000L;
622         mImsRegistrationStatsLte0.utAvailableMillis = 5000L;
623         mImsRegistrationStatsLte0.registeringMillis = 0;
624         mImsRegistrationStatsLte0.unregisteredMillis = 0;
625         mImsRegistrationStatsLte0.registeredTimes = 0;
626 
627         // IMS over WiFi on slot 0, registered for 10 seconds (voice only)
628         mImsRegistrationStatsWifi0 = new ImsRegistrationStats();
629         mImsRegistrationStatsWifi0.carrierId = CARRIER2_ID;
630         mImsRegistrationStatsWifi0.simSlotIndex = 0;
631         mImsRegistrationStatsWifi0.rat = TelephonyManager.NETWORK_TYPE_IWLAN;
632         mImsRegistrationStatsWifi0.registeredMillis = 10000L;
633         mImsRegistrationStatsWifi0.voiceCapableMillis = 10000L;
634         mImsRegistrationStatsWifi0.voiceAvailableMillis = 10000L;
635 
636         // IMS over LTE on slot 1, registered for 20 seconds
637         mImsRegistrationStatsLte1 = new ImsRegistrationStats();
638         mImsRegistrationStatsLte1.carrierId = CARRIER1_ID;
639         mImsRegistrationStatsLte1.simSlotIndex = 0;
640         mImsRegistrationStatsLte1.rat = TelephonyManager.NETWORK_TYPE_LTE;
641         mImsRegistrationStatsLte1.registeredMillis = 20000L;
642         mImsRegistrationStatsLte1.voiceCapableMillis = 20000L;
643         mImsRegistrationStatsLte1.voiceAvailableMillis = 20000L;
644         mImsRegistrationStatsLte1.smsCapableMillis = 20000L;
645         mImsRegistrationStatsLte1.smsAvailableMillis = 20000L;
646         mImsRegistrationStatsLte1.videoCapableMillis = 20000L;
647         mImsRegistrationStatsLte1.videoAvailableMillis = 20000L;
648         mImsRegistrationStatsLte1.utCapableMillis = 20000L;
649         mImsRegistrationStatsLte1.utAvailableMillis = 20000L;
650         mImsRegistrationStatsLte1.registeringMillis = 0;
651         mImsRegistrationStatsLte1.unregisteredMillis = 0;
652         mImsRegistrationStatsLte1.registeredTimes = 0;
653 
654         // IMS terminations on LTE
655         mImsRegistrationTerminationLte = new ImsRegistrationTermination();
656         mImsRegistrationTerminationLte.carrierId = CARRIER1_ID;
657         mImsRegistrationTerminationLte.isMultiSim = true;
658         mImsRegistrationTerminationLte.ratAtEnd = TelephonyManager.NETWORK_TYPE_LTE;
659         mImsRegistrationTerminationLte.setupFailed = false;
660         mImsRegistrationTerminationLte.reasonCode = ImsReasonInfo.CODE_REGISTRATION_ERROR;
661         mImsRegistrationTerminationLte.extraCode = 999;
662         mImsRegistrationTerminationLte.extraMessage = "Request Timeout";
663         mImsRegistrationTerminationLte.count = 2;
664 
665         // IMS terminations on WiFi
666         mImsRegistrationTerminationWifi = new ImsRegistrationTermination();
667         mImsRegistrationTerminationWifi.carrierId = CARRIER2_ID;
668         mImsRegistrationTerminationWifi.isMultiSim = true;
669         mImsRegistrationTerminationWifi.ratAtEnd = TelephonyManager.NETWORK_TYPE_IWLAN;
670         mImsRegistrationTerminationWifi.setupFailed = false;
671         mImsRegistrationTerminationWifi.reasonCode = ImsReasonInfo.CODE_REGISTRATION_ERROR;
672         mImsRegistrationTerminationWifi.extraCode = 0;
673         mImsRegistrationTerminationWifi.extraMessage = "";
674         mImsRegistrationTerminationWifi.count = 1;
675 
676         mImsRegistrationStats =
677                 new ImsRegistrationStats[] {
678                     mImsRegStatsUnregisteredLte0,
679                     mImsRegStatsRegisteringLte0,
680                     mImsRegistrationStatsLte0,
681                     mImsRegistrationStatsWifi0,
682                     mImsRegistrationStatsLte1
683                 };
684 
685         mImsRegistrationTerminations =
686                 new ImsRegistrationTermination[] {
687                     mImsRegistrationTerminationLte, mImsRegistrationTerminationWifi
688                 };
689 
690         mDataCallSession0 = new DataCallSession();
691         mDataCallSession0.dimension = 111;
692         mDataCallSession0.carrierId = CARRIER1_ID;
693         mDataCallSession0.oosAtEnd = false;
694         mDataCallSession0.ratSwitchCount = 3L;
695         mDataCallSession0.setupFailed = false;
696         mDataCallSession0.durationMinutes = 20;
697         mDataCallSession0.ongoing = true;
698         mDataCallSession0.handoverFailureCauses = new int[]{3, 2, 1};
699         mDataCallSession0.handoverFailureRat = new int[]{5, 5, 6};
700 
701         mDataCallSession1 = new DataCallSession();
702         mDataCallSession1.dimension = 222;
703         mDataCallSession1.carrierId = CARRIER2_ID;
704         mDataCallSession1.oosAtEnd = true;
705         mDataCallSession1.ratSwitchCount = 1L;
706         mDataCallSession1.setupFailed = false;
707         mDataCallSession1.durationMinutes = 5;
708         mDataCallSession1.ongoing = false;
709 
710         // RCS registrtion feature tag slot 0
711         mImsRegistrationFeatureTagStats1Proto = new ImsRegistrationFeatureTagStats();
712         mImsRegistrationFeatureTagStats1Proto.carrierId = CARRIER1_ID;
713         mImsRegistrationFeatureTagStats1Proto.slotId = 0;
714         mImsRegistrationFeatureTagStats1Proto.featureTagName = 1;
715         mImsRegistrationFeatureTagStats1Proto.registrationTech = TelephonyManager.NETWORK_TYPE_LTE;
716         mImsRegistrationFeatureTagStats1Proto.registeredMillis = 3600L;
717 
718         // RCS registrtion feature tag slot 1
719         mImsRegistrationFeatureTagStats2Proto = new ImsRegistrationFeatureTagStats();
720         mImsRegistrationFeatureTagStats2Proto.carrierId = CARRIER2_ID;
721         mImsRegistrationFeatureTagStats2Proto.slotId = 1;
722         mImsRegistrationFeatureTagStats2Proto.featureTagName = 0;
723         mImsRegistrationFeatureTagStats2Proto.registrationTech = TelephonyManager.NETWORK_TYPE_LTE;
724         mImsRegistrationFeatureTagStats2Proto.registeredMillis = 3600L;
725 
726         mImsRegistrationFeatureTagStatses =
727                 new ImsRegistrationFeatureTagStats[] {
728                         mImsRegistrationFeatureTagStats1Proto,
729                         mImsRegistrationFeatureTagStats2Proto
730                 };
731 
732         // RCS client provisioning stats slot 0
733         mRcsClientProvisioningStats1Proto = new RcsClientProvisioningStats();
734         mRcsClientProvisioningStats1Proto.carrierId = CARRIER1_ID;
735         mRcsClientProvisioningStats1Proto.slotId = 0;
736         mRcsClientProvisioningStats1Proto.event =
737                 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT;
738         mRcsClientProvisioningStats1Proto.count = 1;
739 
740         // RCS client provisioning stats slot 1
741         mRcsClientProvisioningStats2Proto = new RcsClientProvisioningStats();
742         mRcsClientProvisioningStats2Proto.carrierId = CARRIER2_ID;
743         mRcsClientProvisioningStats2Proto.slotId = 1;
744         mRcsClientProvisioningStats2Proto.event =
745                 RCS_CLIENT_PROVISIONING_STATS__EVENT__TRIGGER_RCS_RECONFIGURATION;
746         mRcsClientProvisioningStats2Proto.count = 1;
747 
748         mRcsClientProvisioningStatses =
749                 new RcsClientProvisioningStats[] {
750                         mRcsClientProvisioningStats1Proto,
751                         mRcsClientProvisioningStats2Proto
752                 };
753 
754         // RCS ACS provisioning stats : error response
755         mRcsAcsProvisioningStats1Proto = new RcsAcsProvisioningStats();
756         mRcsAcsProvisioningStats1Proto.carrierId = CARRIER1_ID;
757         mRcsAcsProvisioningStats1Proto.slotId = 0;
758         mRcsAcsProvisioningStats1Proto.responseCode = 401;
759         mRcsAcsProvisioningStats1Proto.responseType =
760                 RCS_ACS_PROVISIONING_STATS__RESPONSE_TYPE__ERROR;
761         mRcsAcsProvisioningStats1Proto.isSingleRegistrationEnabled = true;
762         mRcsAcsProvisioningStats1Proto.count = 1;
763         mRcsAcsProvisioningStats1Proto.stateTimerMillis = START_TIME_MILLIS;
764 
765         // RCS ACS provisioning stats : xml
766         mRcsAcsProvisioningStats2Proto = new RcsAcsProvisioningStats();
767         mRcsAcsProvisioningStats2Proto.carrierId = CARRIER1_ID;
768         mRcsAcsProvisioningStats2Proto.slotId = 0;
769         mRcsAcsProvisioningStats2Proto.responseCode = 200;
770         mRcsAcsProvisioningStats2Proto.responseType =
771                 RCS_ACS_PROVISIONING_STATS__RESPONSE_TYPE__PROVISIONING_XML;
772         mRcsAcsProvisioningStats2Proto.isSingleRegistrationEnabled = true;
773         mRcsAcsProvisioningStats2Proto.count = 1;
774         mRcsAcsProvisioningStats2Proto.stateTimerMillis = START_TIME_MILLIS;
775 
776         mRcsAcsProvisioningStatses =
777                 new RcsAcsProvisioningStats[] {
778                         mRcsAcsProvisioningStats1Proto,
779                         mRcsAcsProvisioningStats2Proto
780                 };
781 
782         mImsRegistrationServiceIm = new ImsRegistrationServiceDescStats();
783         mImsRegistrationServiceIm.carrierId = CARRIER1_ID;
784         mImsRegistrationServiceIm.slotId = SLOT_ID1;
785         mImsRegistrationServiceIm.serviceIdName = 0;
786         mImsRegistrationServiceIm.serviceIdVersion = 1.0f;
787         mImsRegistrationServiceIm.registrationTech = REGISTRATION1_TECH;
788         mImsRegistrationServiceIm.publishedMillis = START_TIME_MILLIS;
789 
790         mImsRegistrationServiceFt = new ImsRegistrationServiceDescStats();
791         mImsRegistrationServiceFt.carrierId = CARRIER2_ID;
792         mImsRegistrationServiceFt.slotId = SLOT_ID2;
793         mImsRegistrationServiceFt.serviceIdName = 1;
794         mImsRegistrationServiceFt.serviceIdVersion = 2.0f;
795         mImsRegistrationServiceFt.registrationTech = REGISTRATION2_TECH;
796         mImsRegistrationServiceIm.publishedMillis = START_TIME_MILLIS;
797 
798         mImsRegistrationServiceDescStats =
799             new ImsRegistrationServiceDescStats[] {
800                 mImsRegistrationServiceIm, mImsRegistrationServiceFt
801             };
802 
803 
804         mImsDedicatedBearerListenerEvent1 = new ImsDedicatedBearerListenerEvent();
805         mImsDedicatedBearerListenerEvent1.carrierId = CARRIER1_ID;
806         mImsDedicatedBearerListenerEvent1.slotId = SLOT_ID1;
807         mImsDedicatedBearerListenerEvent1.ratAtEnd = TelephonyManager.NETWORK_TYPE_LTE;
808         mImsDedicatedBearerListenerEvent1.qci = 5;
809         mImsDedicatedBearerListenerEvent1.dedicatedBearerEstablished = true;
810         mImsDedicatedBearerListenerEvent1.eventCount = 1;
811 
812         mImsDedicatedBearerListenerEvent2 = new ImsDedicatedBearerListenerEvent();
813         mImsDedicatedBearerListenerEvent2.carrierId = CARRIER2_ID;
814         mImsDedicatedBearerListenerEvent2.slotId = SLOT_ID1;
815         mImsDedicatedBearerListenerEvent2.ratAtEnd = TelephonyManager.NETWORK_TYPE_NR;
816         mImsDedicatedBearerListenerEvent2.qci = 6;
817         mImsDedicatedBearerListenerEvent2.dedicatedBearerEstablished = true;
818         mImsDedicatedBearerListenerEvent2.eventCount = 1;
819 
820         mImsDedicatedBearerListenerEvents =
821                 new ImsDedicatedBearerListenerEvent[] {
822                     mImsDedicatedBearerListenerEvent1, mImsDedicatedBearerListenerEvent2
823                 };
824 
825 
826         mImsDedicatedBearerEvent1 = new ImsDedicatedBearerEvent();
827         mImsDedicatedBearerEvent1.carrierId = CARRIER1_ID;
828         mImsDedicatedBearerEvent1.slotId = SLOT_ID1;
829         mImsDedicatedBearerEvent1.ratAtEnd = TelephonyManager.NETWORK_TYPE_LTE;
830         mImsDedicatedBearerEvent1.qci = 5;
831         mImsDedicatedBearerEvent1.bearerState =
832                 TelephonyStatsLog.IMS_DEDICATED_BEARER_EVENT__BEARER_STATE__STATE_ADDED;
833         mImsDedicatedBearerEvent1.localConnectionInfoReceived = true;
834         mImsDedicatedBearerEvent1.remoteConnectionInfoReceived = true;
835         mImsDedicatedBearerEvent1.hasListeners = true;
836         mImsDedicatedBearerEvent1.count = 1;
837 
838         mImsDedicatedBearerEvent2 = new ImsDedicatedBearerEvent();
839         mImsDedicatedBearerEvent2.carrierId = CARRIER1_ID;
840         mImsDedicatedBearerEvent2.slotId = SLOT_ID1;
841         mImsDedicatedBearerEvent2.ratAtEnd = TelephonyManager.NETWORK_TYPE_NR;
842         mImsDedicatedBearerEvent2.qci = 6;
843         mImsDedicatedBearerEvent2.bearerState =
844                 TelephonyStatsLog.IMS_DEDICATED_BEARER_EVENT__BEARER_STATE__STATE_MODIFIED;
845         mImsDedicatedBearerEvent2.localConnectionInfoReceived = true;
846         mImsDedicatedBearerEvent2.remoteConnectionInfoReceived = true;
847         mImsDedicatedBearerEvent2.hasListeners = true;
848         mImsDedicatedBearerEvent2.count = 1;
849 
850         mImsDedicatedBearerEvents =
851                 new ImsDedicatedBearerEvent[] {
852                     mImsDedicatedBearerEvent1, mImsDedicatedBearerEvent2
853                 };
854 
855 
856         mUceEventStats1 = new UceEventStats();
857         mUceEventStats1.carrierId = CARRIER1_ID;
858         mUceEventStats1.slotId = SLOT_ID1;
859         mUceEventStats1.type = 1;
860         mUceEventStats1.successful = true;
861         mUceEventStats1.commandCode = 0;
862         mUceEventStats1.networkResponse = 200;
863         mUceEventStats1.count = 1;
864 
865         mUceEventStats2 = new UceEventStats();
866         mUceEventStats2.carrierId = CARRIER2_ID;
867         mUceEventStats2.slotId = SLOT_ID2;
868         mUceEventStats2.type = 2;
869         mUceEventStats2.successful = false;
870         mUceEventStats2.commandCode = 2;
871         mUceEventStats2.networkResponse = 0;
872         mUceEventStats2.count = 1;
873         mUceEventStatses = new UceEventStats[] {mUceEventStats1, mUceEventStats2};
874 
875         mPresenceNotifyEvent1 = new PresenceNotifyEvent();
876         mPresenceNotifyEvent1.carrierId = CARRIER1_ID;
877         mPresenceNotifyEvent1.slotId = SLOT_ID1;
878         mPresenceNotifyEvent1.reason = 1;
879         mPresenceNotifyEvent1.contentBodyReceived = true;
880         mPresenceNotifyEvent1.rcsCapsCount = 1;
881         mPresenceNotifyEvent1.mmtelCapsCount = 1;
882         mPresenceNotifyEvent1.noCapsCount = 0;
883         mPresenceNotifyEvent1.count = 1;
884 
885         mPresenceNotifyEvent2 = new PresenceNotifyEvent();
886         mPresenceNotifyEvent2.carrierId = CARRIER2_ID;
887         mPresenceNotifyEvent2.slotId = SLOT_ID2;
888         mPresenceNotifyEvent2.reason = 1;
889         mPresenceNotifyEvent2.contentBodyReceived = false;
890         mPresenceNotifyEvent2.rcsCapsCount = 0;
891         mPresenceNotifyEvent2.mmtelCapsCount = 0;
892         mPresenceNotifyEvent2.noCapsCount = 1;
893         mPresenceNotifyEvent2.count = 1;
894         mPresenceNotifyEvents = new PresenceNotifyEvent[] {mPresenceNotifyEvent1,
895                 mPresenceNotifyEvent2};
896 
897         //A destroyed SipDelegate
898         mSipDelegateStats1 = new SipDelegateStats();
899         mSipDelegateStats1.carrierId = CARRIER1_ID;
900         mSipDelegateStats1.slotId = SLOT_ID1;
901         mSipDelegateStats1.uptimeMillis = 1000L;
902         mSipDelegateStats1.destroyReason =
903                 SipDelegateManager.SIP_DELEGATE_DESTROY_REASON_REQUESTED_BY_APP;
904 
905         //An active SipDelegate
906         mSipDelegateStats2 = new SipDelegateStats();
907         mSipDelegateStats2.carrierId = CARRIER1_ID;
908         mSipDelegateStats2.slotId = SLOT_ID1;
909         mSipDelegateStats2.uptimeMillis = 1000L;
910         mSipDelegateStats2.destroyReason =
911                 SipDelegateManager.SIP_DELEGATE_DESTROY_REASON_SERVICE_DEAD;
912 
913         //An active SipDelegate
914         mSipDelegateStats3 = new SipDelegateStats();
915         mSipDelegateStats3.carrierId = CARRIER2_ID;
916         mSipDelegateStats3.slotId = SLOT_ID2;
917         mSipDelegateStats3.uptimeMillis = 3000L;
918         mSipDelegateStats3.destroyReason =
919                 SipDelegateManager.SIP_DELEGATE_DESTROY_REASON_SUBSCRIPTION_TORN_DOWN;
920 
921         //A registered SipTransportFeatureTag
922         mSipTransportFeatureTagStats1 = new SipTransportFeatureTagStats();
923         mSipTransportFeatureTagStats1.carrierId = CARRIER1_ID;
924         mSipTransportFeatureTagStats1.slotId = SLOT_ID1;
925         mSipTransportFeatureTagStats1.featureTagName = TelephonyProtoEnums.IMS_FEATURE_TAG_CHAT_IM;
926         mSipTransportFeatureTagStats1.sipTransportDeniedReason =  RcsStats.NONE;
927         mSipTransportFeatureTagStats1.sipTransportDeregisteredReason = RcsStats.NONE;
928         mSipTransportFeatureTagStats1.associatedMillis = 1000L;
929 
930         //A denied SipTransportFeatureTag
931         mSipTransportFeatureTagStats2 = new SipTransportFeatureTagStats();
932         mSipTransportFeatureTagStats2.carrierId = CARRIER1_ID;
933         mSipTransportFeatureTagStats2.slotId = SLOT_ID1;
934         mSipTransportFeatureTagStats2.featureTagName = 1;
935         mSipTransportFeatureTagStats2.sipTransportDeniedReason =
936                 SipDelegateManager.DENIED_REASON_IN_USE_BY_ANOTHER_DELEGATE;
937         mSipTransportFeatureTagStats2.sipTransportDeregisteredReason = RcsStats.NONE;
938         mSipTransportFeatureTagStats2.associatedMillis = 1000L;
939 
940         mSipDelegateStatsArray = new SipDelegateStats[]{mSipDelegateStats2, mSipDelegateStats3};
941 
942         mSipTransportFeatureTagStatsArray = new SipTransportFeatureTagStats[]
943                 {mSipTransportFeatureTagStats1, mSipTransportFeatureTagStats2};
944 
945         mGbaEvent1 = new GbaEvent();
946         mGbaEvent1.carrierId = CARRIER1_ID;
947         mGbaEvent1.slotId = SLOT_ID1;
948         mGbaEvent1.successful = true;
949         mGbaEvent1.failedReason = GBA_EVENT__FAILED_REASON__UNKNOWN;
950         mGbaEvent1.count = 1;
951 
952         mGbaEvent2 = new GbaEvent();
953         mGbaEvent2.carrierId = CARRIER2_ID;
954         mGbaEvent2.slotId = SLOT_ID2;
955         mGbaEvent2.successful = false;
956         mGbaEvent2.failedReason = GBA_EVENT__FAILED_REASON__FEATURE_NOT_READY;
957         mGbaEvent2.count = 1;
958 
959         mGbaEvent = new GbaEvent[] {mGbaEvent1, mGbaEvent2};
960 
961         //stats slot 0
962         mSipMessageResponse1 = new SipMessageResponse();
963         mSipMessageResponse1.carrierId = CARRIER1_ID;
964         mSipMessageResponse1.slotId = SLOT_ID1;
965         //"INVITE"
966         mSipMessageResponse1.sipMessageMethod = 2;
967         mSipMessageResponse1.sipMessageResponse = 200;
968         mSipMessageResponse1.sipMessageDirection = 1;
969         mSipMessageResponse1.messageError = 0;
970         mSipMessageResponse1.count = 1;
971 
972         //stats slot 1
973         mSipMessageResponse2 = new SipMessageResponse();
974         mSipMessageResponse2.carrierId = CARRIER2_ID;
975         mSipMessageResponse2.slotId = SLOT_ID2;
976         //"INVITE"
977         mSipMessageResponse2.sipMessageMethod = 2;
978         mSipMessageResponse2.sipMessageResponse = 200;
979         mSipMessageResponse2.sipMessageDirection = 0;
980         mSipMessageResponse2.messageError = 0;
981         mSipMessageResponse2.count = 1;
982 
983         mSipMessageResponse =
984                 new SipMessageResponse[] {mSipMessageResponse1, mSipMessageResponse2};
985 
986         // stats slot 0
987         mSipTransportSession1 = new SipTransportSession();
988         mSipTransportSession1.carrierId = CARRIER1_ID;
989         mSipTransportSession1.slotId = SLOT_ID1;
990         //"INVITE"
991         mSipTransportSession1.sessionMethod = 2;
992         mSipTransportSession1.sipMessageDirection = 1;
993         mSipTransportSession1.sipResponse = 200;
994         mSipTransportSession1.sessionCount = 1;
995         mSipTransportSession1.endedGracefullyCount = 1;
996         mSipTransportSession1.isEndedGracefully = true;
997 
998         // stats slot 1
999         mSipTransportSession2 = new SipTransportSession();
1000         mSipTransportSession2.carrierId = CARRIER2_ID;
1001         mSipTransportSession2.slotId = SLOT_ID2;
1002         //"INVITE"
1003         mSipTransportSession2.sessionMethod = 2;
1004         mSipTransportSession2.sipMessageDirection = 0;
1005         mSipTransportSession2.sipResponse = 200;
1006         mSipTransportSession2.sessionCount = 1;
1007         mSipTransportSession2.endedGracefullyCount = 1;
1008         mSipTransportSession2.isEndedGracefully = true;
1009 
1010         mSipTransportSession =
1011                 new SipTransportSession[] {mSipTransportSession1, mSipTransportSession2};
1012 
1013         generateTestSmsData();
1014         generateTestSatelliteData();
1015     }
1016 
generateTestSmsData()1017     private void generateTestSmsData() {
1018         mIncomingSms1 = new IncomingSms();
1019         mIncomingSms1.smsFormat = INCOMING_SMS__SMS_FORMAT__SMS_FORMAT_3GPP;
1020         mIncomingSms1.smsTech = INCOMING_SMS__SMS_TECH__SMS_TECH_CS_3GPP;
1021         mIncomingSms1.rat = TelephonyManager.NETWORK_TYPE_LTE;
1022         mIncomingSms1.smsType = INCOMING_SMS__SMS_TYPE__SMS_TYPE_NORMAL;
1023         mIncomingSms1.totalParts = 1;
1024         mIncomingSms1.receivedParts = 1;
1025         mIncomingSms1.blocked = false;
1026         mIncomingSms1.error = INCOMING_SMS__ERROR__SMS_SUCCESS;
1027         mIncomingSms1.isRoaming = false;
1028         mIncomingSms1.simSlotIndex = 0;
1029         mIncomingSms1.isMultiSim = false;
1030         mIncomingSms1.isEsim = false;
1031         mIncomingSms1.carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
1032         mIncomingSms1.messageId = 0;
1033         mIncomingSms1.count = 1;
1034         mIncomingSms1.isManagedProfile = false;
1035         mIncomingSms1.isNtn = false;
1036         mIncomingSms1.isEmergency = true;
1037 
1038         mIncomingSms2 = new IncomingSms();
1039         mIncomingSms2.smsFormat = INCOMING_SMS__SMS_FORMAT__SMS_FORMAT_3GPP2;
1040         mIncomingSms2.smsTech = INCOMING_SMS__SMS_TECH__SMS_TECH_CS_3GPP2;
1041         mIncomingSms2.rat = TelephonyManager.NETWORK_TYPE_CDMA;
1042         mIncomingSms2.smsType = INCOMING_SMS__SMS_TYPE__SMS_TYPE_SMS_PP;
1043         mIncomingSms2.totalParts = 2;
1044         mIncomingSms2.receivedParts = 2;
1045         mIncomingSms2.blocked = true;
1046         mIncomingSms2.error = INCOMING_SMS__ERROR__SMS_ERROR_GENERIC;
1047         mIncomingSms2.isRoaming = true;
1048         mIncomingSms2.simSlotIndex = 1;
1049         mIncomingSms2.isMultiSim = true;
1050         mIncomingSms2.isEsim = true;
1051         mIncomingSms2.carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
1052         mIncomingSms2.messageId = 1;
1053         mIncomingSms2.count = 2;
1054         mIncomingSms2.isManagedProfile = true;
1055         mIncomingSms2.isNtn = true;
1056         mIncomingSms2.isEmergency = true;
1057 
1058         mIncomingSms = new IncomingSms[] {mIncomingSms1, mIncomingSms2};
1059 
1060         mOutgoingSms1 = new OutgoingSms();
1061         mOutgoingSms1.smsFormat = INCOMING_SMS__SMS_FORMAT__SMS_FORMAT_3GPP;
1062         mOutgoingSms1.smsTech = INCOMING_SMS__SMS_TECH__SMS_TECH_CS_3GPP;
1063         mOutgoingSms1.rat = TelephonyManager.NETWORK_TYPE_LTE;
1064         mOutgoingSms1.sendResult = OUTGOING_SMS__SEND_RESULT__SMS_SEND_RESULT_SUCCESS;
1065         mOutgoingSms1.errorCode = NO_ERROR_CODE;
1066         mOutgoingSms1.isRoaming = false;
1067         mOutgoingSms1.isFromDefaultApp = true;
1068         mOutgoingSms1.simSlotIndex = 0;
1069         mOutgoingSms1.isMultiSim = false;
1070         mOutgoingSms1.carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
1071         mOutgoingSms1.messageId = 0;
1072         mOutgoingSms1.retryId = 0;
1073         mOutgoingSms1.intervalMillis = 0;
1074         mOutgoingSms1.count = 1;
1075         mOutgoingSms1.sendErrorCode = RESULT_ERROR_NONE;
1076         mOutgoingSms1.networkErrorCode = NO_ERROR_CODE;
1077         mOutgoingSms1.isManagedProfile = false;
1078         mOutgoingSms1.isEmergency = false;
1079         mOutgoingSms1.isNtn = false;
1080 
1081         mOutgoingSms2 = new OutgoingSms();
1082         mOutgoingSms2.smsFormat = INCOMING_SMS__SMS_FORMAT__SMS_FORMAT_3GPP2;
1083         mOutgoingSms2.smsTech = INCOMING_SMS__SMS_TECH__SMS_TECH_CS_3GPP2;
1084         mOutgoingSms2.rat = TelephonyManager.NETWORK_TYPE_CDMA;
1085         mOutgoingSms2.sendResult = OUTGOING_SMS__SEND_RESULT__SMS_SEND_RESULT_ERROR;
1086         mOutgoingSms2.errorCode = NO_ERROR_CODE;
1087         mOutgoingSms2.isRoaming = true;
1088         mOutgoingSms2.isFromDefaultApp = false;
1089         mOutgoingSms2.simSlotIndex = 1;
1090         mOutgoingSms2.isMultiSim = true;
1091         mOutgoingSms2.carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
1092         mOutgoingSms2.messageId = 1;
1093         mOutgoingSms2.retryId = 1;
1094         mOutgoingSms2.intervalMillis = 10;
1095         mOutgoingSms2.count = 2;
1096         mOutgoingSms2.sendErrorCode = RESULT_RIL_SMS_SEND_FAIL_RETRY;
1097         mOutgoingSms2.networkErrorCode = NO_ERROR_CODE;
1098         mOutgoingSms2.isManagedProfile = true;
1099         mOutgoingSms2.isEmergency = true;
1100         mOutgoingSms2.isNtn = true;
1101 
1102         mOutgoingSms = new OutgoingSms[] {mOutgoingSms1, mOutgoingSms2};
1103 
1104         mOutgoingShortCodeSms1 = new OutgoingShortCodeSms();
1105         mOutgoingShortCodeSms1.category = 1;
1106         mOutgoingShortCodeSms1.xmlVersion = 30;
1107         mOutgoingShortCodeSms1.shortCodeSmsCount = 1;
1108 
1109         mOutgoingShortCodeSms2 = new OutgoingShortCodeSms();
1110         mOutgoingShortCodeSms2.category = 2;
1111         mOutgoingShortCodeSms2.xmlVersion  = 31;
1112         mOutgoingShortCodeSms2.shortCodeSmsCount = 1;
1113 
1114         mOutgoingShortCodeSms = new OutgoingShortCodeSms[] {mOutgoingShortCodeSms1,
1115                 mOutgoingShortCodeSms2};
1116 
1117         generateTestSatelliteData();
1118 
1119         generateTestDataNetworkValidationsData();
1120     }
1121 
generateTestSatelliteData()1122     private void generateTestSatelliteData() {
1123         mSatelliteController1 = new SatelliteController();
1124         mSatelliteController1.countOfSatelliteServiceEnablementsSuccess = 2;
1125         mSatelliteController1.countOfSatelliteServiceEnablementsFail = 0;
1126         mSatelliteController1.countOfOutgoingDatagramSuccess = 8;
1127         mSatelliteController1.countOfOutgoingDatagramFail = 9;
1128         mSatelliteController1.countOfIncomingDatagramSuccess = 10;
1129         mSatelliteController1.countOfIncomingDatagramFail = 11;
1130         mSatelliteController1.countOfDatagramTypeSosSmsSuccess = 5;
1131         mSatelliteController1.countOfDatagramTypeSosSmsFail = 5;
1132         mSatelliteController1.countOfDatagramTypeLocationSharingSuccess = 6;
1133         mSatelliteController1.countOfDatagramTypeLocationSharingFail = 6;
1134         mSatelliteController1.countOfProvisionSuccess = 3;
1135         mSatelliteController1.countOfProvisionFail = 4;
1136         mSatelliteController1.countOfDeprovisionSuccess = 5;
1137         mSatelliteController1.countOfDeprovisionFail = 6;
1138         mSatelliteController1.totalServiceUptimeSec = 60 * 60 * 24 * 7;
1139         mSatelliteController1.totalBatteryConsumptionPercent = 7;
1140         mSatelliteController1.totalBatteryChargedTimeSec = 60 * 60 * 3 * 1;
1141         mSatelliteController1.countOfDemoModeSatelliteServiceEnablementsSuccess = 3;
1142         mSatelliteController1.countOfDemoModeSatelliteServiceEnablementsFail = 1;
1143         mSatelliteController1.countOfDemoModeOutgoingDatagramSuccess = 4;
1144         mSatelliteController1.countOfDemoModeOutgoingDatagramFail = 2;
1145         mSatelliteController1.countOfDemoModeIncomingDatagramSuccess = 3;
1146         mSatelliteController1.countOfDemoModeIncomingDatagramFail = 2;
1147         mSatelliteController1.countOfDatagramTypeKeepAliveSuccess = 1;
1148         mSatelliteController1.countOfDatagramTypeKeepAliveFail = 2;
1149         mSatelliteController1.isProvisioned = true;
1150         mSatelliteController1.carrierId = 1;
1151         mSatelliteController1.countOfSuccessfulLocationQueries = 3;
1152         mSatelliteController1.countOfFailedLocationQueries = 5;
1153         mSatelliteController1.countOfP2PSmsAvailableNotificationShown = 3;
1154         mSatelliteController1.countOfP2PSmsAvailableNotificationRemoved = 5;
1155         mSatelliteController1.isNtnOnlyCarrier = false;
1156         mSatelliteController1.versionOfSatelliteAccessConfig = 10;
1157         mSatelliteController1.countOfIncomingDatagramTypeSosSmsSuccess = 1;
1158         mSatelliteController1.countOfIncomingDatagramTypeSosSmsFail = 2;
1159         mSatelliteController1.countOfOutgoingDatagramTypeSmsSuccess = 3;
1160         mSatelliteController1.countOfOutgoingDatagramTypeSmsFail = 4;
1161         mSatelliteController1.countOfIncomingDatagramTypeSmsSuccess = 5;
1162         mSatelliteController1.countOfIncomingDatagramTypeSmsFail = 6;
1163 
1164         mSatelliteController2 = new SatelliteController();
1165         mSatelliteController2.countOfSatelliteServiceEnablementsSuccess = 2 + 1;
1166         mSatelliteController2.countOfSatelliteServiceEnablementsFail = 0 + 1;
1167         mSatelliteController2.countOfOutgoingDatagramSuccess = 8 + 1;
1168         mSatelliteController2.countOfOutgoingDatagramFail = 9 + 1;
1169         mSatelliteController2.countOfIncomingDatagramSuccess = 10 + 1;
1170         mSatelliteController2.countOfIncomingDatagramFail = 11 + 1;
1171         mSatelliteController2.countOfDatagramTypeSosSmsSuccess = 5 + 1;
1172         mSatelliteController2.countOfDatagramTypeSosSmsFail = 5 + 1;
1173         mSatelliteController2.countOfDatagramTypeLocationSharingSuccess = 6 + 1;
1174         mSatelliteController2.countOfDatagramTypeLocationSharingFail = 6 + 1;
1175         mSatelliteController2.countOfProvisionSuccess = 13;
1176         mSatelliteController2.countOfProvisionFail = 14;
1177         mSatelliteController2.countOfDeprovisionSuccess = 15;
1178         mSatelliteController2.countOfDeprovisionFail = 16;
1179         mSatelliteController2.totalServiceUptimeSec = 60 * 60 * 12;
1180         mSatelliteController2.totalBatteryConsumptionPercent = 14;
1181         mSatelliteController2.totalBatteryChargedTimeSec = 60 * 60 * 3;
1182         mSatelliteController2.countOfDemoModeSatelliteServiceEnablementsSuccess = 5;
1183         mSatelliteController2.countOfDemoModeSatelliteServiceEnablementsFail = 4;
1184         mSatelliteController2.countOfDemoModeOutgoingDatagramSuccess = 3;
1185         mSatelliteController2.countOfDemoModeOutgoingDatagramFail = 7;
1186         mSatelliteController2.countOfDemoModeIncomingDatagramSuccess = 2;
1187         mSatelliteController2.countOfDemoModeIncomingDatagramFail = 3;
1188         mSatelliteController2.countOfDatagramTypeKeepAliveSuccess = 4;
1189         mSatelliteController2.countOfDatagramTypeKeepAliveFail = 5;
1190         mSatelliteController2.isProvisioned = false;
1191         mSatelliteController2.carrierId = 10;
1192         mSatelliteController2.countOfSuccessfulLocationQueries = 30;
1193         mSatelliteController2.countOfFailedLocationQueries = 50;
1194         mSatelliteController2.countOfP2PSmsAvailableNotificationShown = 30;
1195         mSatelliteController2.countOfP2PSmsAvailableNotificationRemoved = 50;
1196         mSatelliteController2.isNtnOnlyCarrier = true;
1197         mSatelliteController2.versionOfSatelliteAccessConfig = 12;
1198         mSatelliteController2.countOfIncomingDatagramTypeSosSmsSuccess = 11;
1199         mSatelliteController2.countOfIncomingDatagramTypeSosSmsFail = 12;
1200         mSatelliteController2.countOfOutgoingDatagramTypeSmsSuccess = 31;
1201         mSatelliteController2.countOfOutgoingDatagramTypeSmsFail = 14;
1202         mSatelliteController2.countOfIncomingDatagramTypeSmsSuccess = 15;
1203         mSatelliteController2.countOfIncomingDatagramTypeSmsFail = 16;
1204 
1205         // SatelliteController atom has one data point
1206         mSatelliteControllers =
1207                 new SatelliteController[] {
1208                         mSatelliteController1
1209                 };
1210 
1211         mSatelliteSession1 = new SatelliteSession();
1212         mSatelliteSession1.satelliteServiceInitializationResult =
1213                 SatelliteProtoEnums.SATELLITE_RESULT_SUCCESS;
1214         mSatelliteSession1.satelliteTechnology =
1215                 SatelliteProtoEnums.NT_RADIO_TECHNOLOGY_PROPRIETARY;
1216         mSatelliteSession1.count = 1;
1217         mSatelliteSession1.satelliteServiceTerminationResult =
1218                 SatelliteProtoEnums.SATELLITE_RESULT_SUCCESS;
1219         mSatelliteSession1.initializationProcessingTimeMillis = 100;
1220         mSatelliteSession1.terminationProcessingTimeMillis = 200;
1221         mSatelliteSession1.sessionDurationSeconds = 3;
1222         mSatelliteSession1.countOfOutgoingDatagramSuccess = 1;
1223         mSatelliteSession1.countOfOutgoingDatagramFailed = 0;
1224         mSatelliteSession1.countOfIncomingDatagramSuccess = 1;
1225         mSatelliteSession1.countOfIncomingDatagramFailed = 0;
1226         mSatelliteSession1.isDemoMode = false;
1227         mSatelliteSession1.maxNtnSignalStrengthLevel = 2;
1228         mSatelliteSession1.carrierId = 2;
1229         mSatelliteSession1.countOfSatelliteNotificationDisplayed = 4;
1230         mSatelliteSession1.countOfAutoExitDueToScreenOff = 6;
1231         mSatelliteSession1.countOfAutoExitDueToTnNetwork = 7;
1232         mSatelliteSession1.isEmergency = true;
1233         mSatelliteSession1.maxInactivityDurationSec = 8;
1234 
1235         mSatelliteSession2 = new SatelliteSession();
1236         mSatelliteSession2.satelliteServiceInitializationResult =
1237                 SatelliteProtoEnums.SATELLITE_RESULT_MODEM_ERROR;
1238         mSatelliteSession2.satelliteTechnology =
1239                 SatelliteProtoEnums.NT_RADIO_TECHNOLOGY_NB_IOT_NTN;
1240         mSatelliteSession2.count = 1;
1241         mSatelliteSession2.satelliteServiceTerminationResult =
1242                 SatelliteProtoEnums.SATELLITE_RESULT_SUCCESS;
1243         mSatelliteSession2.initializationProcessingTimeMillis = 300;
1244         mSatelliteSession2.terminationProcessingTimeMillis = 100;
1245         mSatelliteSession2.sessionDurationSeconds = 10;
1246         mSatelliteSession2.countOfOutgoingDatagramSuccess = 0;
1247         mSatelliteSession2.countOfOutgoingDatagramFailed = 2;
1248         mSatelliteSession2.countOfIncomingDatagramSuccess = 0;
1249         mSatelliteSession2.countOfIncomingDatagramFailed = 1;
1250         mSatelliteSession2.isDemoMode = true;
1251         mSatelliteSession2.maxNtnSignalStrengthLevel = 4;
1252         mSatelliteSession2.carrierId = 20;
1253         mSatelliteSession2.countOfSatelliteNotificationDisplayed = 40;
1254         mSatelliteSession2.countOfAutoExitDueToScreenOff = 60;
1255         mSatelliteSession2.countOfAutoExitDueToTnNetwork = 70;
1256         mSatelliteSession2.isEmergency = false;
1257         mSatelliteSession2.maxInactivityDurationSec = 80;
1258 
1259         mSatelliteSessions =
1260                 new SatelliteSession[] {
1261                         mSatelliteSession1, mSatelliteSession2
1262                 };
1263 
1264         mSatelliteIncomingDatagram1 = new SatelliteIncomingDatagram();
1265         mSatelliteIncomingDatagram1.resultCode = SatelliteProtoEnums.SATELLITE_RESULT_SUCCESS;
1266         mSatelliteIncomingDatagram1.datagramSizeBytes = 1 * 1024;
1267         mSatelliteIncomingDatagram1.datagramTransferTimeMillis = 3 * 1000;
1268         mSatelliteIncomingDatagram1.isDemoMode = false;
1269         mSatelliteIncomingDatagram1.carrierId = 1;
1270 
1271         mSatelliteIncomingDatagram2 = new SatelliteIncomingDatagram();
1272         mSatelliteIncomingDatagram2.resultCode = SatelliteProtoEnums.SATELLITE_RESULT_MODEM_ERROR;
1273         mSatelliteIncomingDatagram2.datagramSizeBytes = 512;
1274         mSatelliteIncomingDatagram2.datagramTransferTimeMillis = 1 * 1000;
1275         mSatelliteIncomingDatagram2.isDemoMode = true;
1276         mSatelliteIncomingDatagram2.carrierId = 10;
1277 
1278         mSatelliteIncomingDatagrams =
1279                 new SatelliteIncomingDatagram[] {
1280                         mSatelliteIncomingDatagram1, mSatelliteIncomingDatagram2
1281                 };
1282 
1283         mSatelliteOutgoingDatagram1 = new SatelliteOutgoingDatagram();
1284         mSatelliteOutgoingDatagram1.datagramType =
1285                 SatelliteProtoEnums.DATAGRAM_TYPE_LOCATION_SHARING;
1286         mSatelliteOutgoingDatagram1.resultCode = SatelliteProtoEnums.SATELLITE_RESULT_SUCCESS;
1287         mSatelliteOutgoingDatagram1.datagramSizeBytes = 1 * 1024;
1288         mSatelliteOutgoingDatagram1.datagramTransferTimeMillis = 3 * 1000;
1289         mSatelliteOutgoingDatagram1.isDemoMode = false;
1290         mSatelliteOutgoingDatagram1.carrierId = 1;
1291 
1292         mSatelliteOutgoingDatagram2 = new SatelliteOutgoingDatagram();
1293         mSatelliteOutgoingDatagram2.datagramType =
1294                 SatelliteProtoEnums.DATAGRAM_TYPE_SOS_MESSAGE;
1295         mSatelliteOutgoingDatagram2.resultCode = SatelliteProtoEnums.SATELLITE_RESULT_MODEM_ERROR;
1296         mSatelliteOutgoingDatagram2.datagramSizeBytes = 512;
1297         mSatelliteOutgoingDatagram2.datagramTransferTimeMillis = 1 * 1000;
1298         mSatelliteOutgoingDatagram2.isDemoMode = true;
1299         mSatelliteOutgoingDatagram2.carrierId = 10;
1300 
1301         mSatelliteOutgoingDatagrams =
1302                 new SatelliteOutgoingDatagram[] {
1303                         mSatelliteOutgoingDatagram1, mSatelliteOutgoingDatagram2
1304                 };
1305 
1306         mSatelliteProvision1 = new SatelliteProvision();
1307         mSatelliteProvision1.resultCode = SatelliteProtoEnums.SATELLITE_RESULT_SUCCESS;
1308         mSatelliteProvision1.provisioningTimeSec = 3 * 60;
1309         mSatelliteProvision1.isProvisionRequest = true;
1310         mSatelliteProvision1.isCanceled = false;
1311         mSatelliteProvision1.carrierId = 1;
1312 
1313         mSatelliteProvision2 = new SatelliteProvision();
1314         mSatelliteProvision2.resultCode =
1315                 SatelliteProtoEnums.SATELLITE_RESULT_SERVICE_NOT_PROVISIONED;
1316         mSatelliteProvision2.provisioningTimeSec = 0;
1317         mSatelliteProvision2.isProvisionRequest = false;
1318         mSatelliteProvision2.isCanceled = true;
1319         mSatelliteProvision2.carrierId = 10;
1320 
1321         mSatelliteProvisions =
1322                 new SatelliteProvision[] {
1323                         mSatelliteProvision1, mSatelliteProvision2
1324                 };
1325 
1326         mSatelliteSosMessageRecommender1 = new SatelliteSosMessageRecommender();
1327         mSatelliteSosMessageRecommender1.isDisplaySosMessageSent = true;
1328         mSatelliteSosMessageRecommender1.countOfTimerStarted = 5;
1329         mSatelliteSosMessageRecommender1.isImsRegistered = false;
1330         mSatelliteSosMessageRecommender1.cellularServiceState =
1331                 TelephonyProtoEnums.SERVICE_STATE_OUT_OF_SERVICE;
1332         mSatelliteSosMessageRecommender1.isMultiSim = true;
1333         mSatelliteSosMessageRecommender1.recommendingHandoverType = 1;
1334         mSatelliteSosMessageRecommender1.isSatelliteAllowedInCurrentLocation = true;
1335         mSatelliteSosMessageRecommender1.isWifiConnected = true;
1336         mSatelliteSosMessageRecommender1.carrierId = 1;
1337         mSatelliteSosMessageRecommender1.count = 1;
1338 
1339         mSatelliteSosMessageRecommender2 = new SatelliteSosMessageRecommender();
1340         mSatelliteSosMessageRecommender2.isDisplaySosMessageSent = false;
1341         mSatelliteSosMessageRecommender2.countOfTimerStarted = 3;
1342         mSatelliteSosMessageRecommender2.isImsRegistered = true;
1343         mSatelliteSosMessageRecommender2.cellularServiceState =
1344                 TelephonyProtoEnums.SERVICE_STATE_POWER_OFF;
1345         mSatelliteSosMessageRecommender2.isMultiSim = false;
1346         mSatelliteSosMessageRecommender2.recommendingHandoverType = 0;
1347         mSatelliteSosMessageRecommender2.isSatelliteAllowedInCurrentLocation = true;
1348         mSatelliteSosMessageRecommender2.isWifiConnected = false;
1349         mSatelliteSosMessageRecommender2.carrierId = 2;
1350 
1351         mSatelliteSosMessageRecommender2.count = 1;
1352 
1353         mSatelliteSosMessageRecommenders =
1354                 new SatelliteSosMessageRecommender[] {
1355                         mSatelliteSosMessageRecommender1, mSatelliteSosMessageRecommender2
1356                 };
1357 
1358         mCarrierRoamingSatelliteSession1 = new CarrierRoamingSatelliteSession();
1359         mCarrierRoamingSatelliteSession1.carrierId = 1;
1360         mCarrierRoamingSatelliteSession1.isNtnRoamingInHomeCountry = false;
1361         mCarrierRoamingSatelliteSession1.totalSatelliteModeTimeSec = 60;
1362         mCarrierRoamingSatelliteSession1.numberOfSatelliteConnections = 3;
1363         mCarrierRoamingSatelliteSession1.avgDurationOfSatelliteConnectionSec = 20;
1364         mCarrierRoamingSatelliteSession1.satelliteConnectionGapMinSec = 2;
1365         mCarrierRoamingSatelliteSession1.satelliteConnectionGapAvgSec = 5;
1366         mCarrierRoamingSatelliteSession1.satelliteConnectionGapMaxSec = 8;
1367         mCarrierRoamingSatelliteSession1.rsrpAvg = 3;
1368         mCarrierRoamingSatelliteSession1.rsrpMedian = 2;
1369         mCarrierRoamingSatelliteSession1.rssnrAvg = 5;
1370         mCarrierRoamingSatelliteSession1.rssnrMedian = 3;
1371         mCarrierRoamingSatelliteSession1.countOfIncomingSms = 2;
1372         mCarrierRoamingSatelliteSession1.countOfOutgoingSms = 4;
1373         mCarrierRoamingSatelliteSession1.countOfIncomingMms = 1;
1374         mCarrierRoamingSatelliteSession1.countOfOutgoingMms = 1;
1375         mCarrierRoamingSatelliteSession1.isMultiSim = false;
1376         mCarrierRoamingSatelliteSession1.isNbIotNtn = false;
1377 
1378         mCarrierRoamingSatelliteSession2 = new CarrierRoamingSatelliteSession();
1379         mCarrierRoamingSatelliteSession2.carrierId = 2;
1380         mCarrierRoamingSatelliteSession2.isNtnRoamingInHomeCountry = true;
1381         mCarrierRoamingSatelliteSession2.totalSatelliteModeTimeSec = 120;
1382         mCarrierRoamingSatelliteSession2.numberOfSatelliteConnections = 5;
1383         mCarrierRoamingSatelliteSession2.avgDurationOfSatelliteConnectionSec = 20;
1384         mCarrierRoamingSatelliteSession2.satelliteConnectionGapMinSec = 2;
1385         mCarrierRoamingSatelliteSession2.satelliteConnectionGapAvgSec = 5;
1386         mCarrierRoamingSatelliteSession2.satelliteConnectionGapMaxSec = 8;
1387         mCarrierRoamingSatelliteSession2.rsrpAvg = 3;
1388         mCarrierRoamingSatelliteSession2.rsrpMedian = 2;
1389         mCarrierRoamingSatelliteSession2.rssnrAvg = 8;
1390         mCarrierRoamingSatelliteSession2.rssnrMedian = 15;
1391         mCarrierRoamingSatelliteSession2.countOfIncomingSms = 2;
1392         mCarrierRoamingSatelliteSession2.countOfOutgoingSms = 4;
1393         mCarrierRoamingSatelliteSession2.countOfIncomingMms = 1;
1394         mCarrierRoamingSatelliteSession2.countOfOutgoingMms = 1;
1395         mCarrierRoamingSatelliteSession2.isMultiSim = true;
1396         mCarrierRoamingSatelliteSession2.isNbIotNtn = true;
1397 
1398         mCarrierRoamingSatelliteSessions = new CarrierRoamingSatelliteSession[] {
1399                 mCarrierRoamingSatelliteSession1, mCarrierRoamingSatelliteSession2};
1400 
1401         mCarrierRoamingSatelliteControllerStats1 = new CarrierRoamingSatelliteControllerStats();
1402         mCarrierRoamingSatelliteControllerStats1.configDataSource =
1403                 SatelliteProtoEnums.CONFIG_DATA_SOURCE_ENTITLEMENT;
1404         mCarrierRoamingSatelliteControllerStats1.countOfEntitlementStatusQueryRequest = 2;
1405         mCarrierRoamingSatelliteControllerStats1.countOfSatelliteConfigUpdateRequest = 1;
1406         mCarrierRoamingSatelliteControllerStats1.countOfSatelliteNotificationDisplayed = 1;
1407         mCarrierRoamingSatelliteControllerStats1.satelliteSessionGapMinSec = 2;
1408         mCarrierRoamingSatelliteControllerStats1.satelliteSessionGapAvgSec = 3;
1409         mCarrierRoamingSatelliteControllerStats1.satelliteSessionGapMaxSec = 4;
1410         mCarrierRoamingSatelliteControllerStats1.carrierId = 1;
1411         mCarrierRoamingSatelliteControllerStats1.isDeviceEntitled = true;
1412         mCarrierRoamingSatelliteControllerStats1.isMultiSim = false;
1413         mCarrierRoamingSatelliteControllerStats1.countOfSatelliteSessions = 1;
1414         mCarrierRoamingSatelliteControllerStats1.isNbIotNtn = false;
1415 
1416         mCarrierRoamingSatelliteControllerStats2 = new CarrierRoamingSatelliteControllerStats();
1417         mCarrierRoamingSatelliteControllerStats2.configDataSource =
1418                 SatelliteProtoEnums.CONFIG_DATA_SOURCE_CONFIG_UPDATER;
1419         mCarrierRoamingSatelliteControllerStats2.countOfEntitlementStatusQueryRequest = 4;
1420         mCarrierRoamingSatelliteControllerStats2.countOfSatelliteConfigUpdateRequest = 1;
1421         mCarrierRoamingSatelliteControllerStats2.countOfSatelliteNotificationDisplayed = 1;
1422         mCarrierRoamingSatelliteControllerStats2.satelliteSessionGapMinSec = 5;
1423         mCarrierRoamingSatelliteControllerStats2.satelliteSessionGapAvgSec = 10;
1424         mCarrierRoamingSatelliteControllerStats2.satelliteSessionGapMaxSec = 15;
1425         mCarrierRoamingSatelliteControllerStats2.carrierId = 10;
1426         mCarrierRoamingSatelliteControllerStats2.isDeviceEntitled = false;
1427         mCarrierRoamingSatelliteControllerStats2.isMultiSim = true;
1428         mCarrierRoamingSatelliteControllerStats2.countOfSatelliteSessions = 2;
1429         mCarrierRoamingSatelliteControllerStats2.isNbIotNtn = true;
1430 
1431         // CarrierRoamingSatelliteController has one data point
1432         mCarrierRoamingSatelliteControllerStats = new CarrierRoamingSatelliteControllerStats[] {
1433                 mCarrierRoamingSatelliteControllerStats1};
1434 
1435         mSatelliteEntitlement1 = new SatelliteEntitlement();
1436         mSatelliteEntitlement1.carrierId = 1;
1437         mSatelliteEntitlement1.result = 0;
1438         mSatelliteEntitlement1.entitlementStatus =
1439                 SatelliteProtoEnums.SATELLITE_ENTITLEMENT_STATUS_ENABLED;
1440         mSatelliteEntitlement1.isRetry = false;
1441         mSatelliteEntitlement1.count = 1;
1442 
1443         mSatelliteEntitlement2 = new SatelliteEntitlement();
1444         mSatelliteEntitlement2.carrierId = 2;
1445         mSatelliteEntitlement2.result = 1;
1446         mSatelliteEntitlement2.entitlementStatus =
1447                 SatelliteProtoEnums.SATELLITE_ENTITLEMENT_STATUS_DISABLED;
1448         mSatelliteEntitlement2.isRetry = true;
1449         mSatelliteEntitlement2.count = 1;
1450 
1451         mSatelliteEntitlements = new SatelliteEntitlement[] {mSatelliteEntitlement1,
1452                 mSatelliteEntitlement2};
1453 
1454         mSatelliteConfigUpdater1 = new SatelliteConfigUpdater();
1455         mSatelliteConfigUpdater1.configVersion = 1;
1456         mSatelliteConfigUpdater1.oemConfigResult = SatelliteProtoEnums.CONFIG_UPDATE_RESULT_SUCCESS;
1457         mSatelliteConfigUpdater1.carrierConfigResult =
1458                 SatelliteProtoEnums.CONFIG_UPDATE_RESULT_CARRIER_DATA_INVALID_PLMN;
1459         mSatelliteConfigUpdater1.count = 1;
1460 
1461         mSatelliteConfigUpdater2 = new SatelliteConfigUpdater();
1462         mSatelliteConfigUpdater2.configVersion = 2;
1463         mSatelliteConfigUpdater2.oemConfigResult =
1464                 SatelliteProtoEnums.CONFIG_UPDATE_RESULT_DEVICE_DATA_INVALID_COUNTRY_CODE;
1465         mSatelliteConfigUpdater2.carrierConfigResult =
1466                 SatelliteProtoEnums.CONFIG_UPDATE_RESULT_SUCCESS;
1467         mSatelliteConfigUpdater2.count = 1;
1468 
1469         mSatelliteConfigUpdaters = new SatelliteConfigUpdater[] {mSatelliteConfigUpdater1,
1470                 mSatelliteConfigUpdater2};
1471 
1472         mSatelliteAccessController1 = new SatelliteAccessController();
1473         mSatelliteAccessController1.accessControlType = ACCESS_CONTROL_TYPE_NETWORK_COUNTRY_CODE;
1474         mSatelliteAccessController1.locationQueryTimeMillis = TimeUnit.SECONDS.toMillis(1);
1475         mSatelliteAccessController1.onDeviceLookupTimeMillis = TimeUnit.SECONDS.toMillis(2);
1476         mSatelliteAccessController1.totalCheckingTimeMillis = TimeUnit.SECONDS.toMillis(3);
1477         mSatelliteAccessController1.isAllowed = true;
1478         mSatelliteAccessController1.isEmergency = false;
1479         mSatelliteAccessController1.resultCode = SATELLITE_RESULT_SUCCESS;
1480         mSatelliteAccessController1.countryCodes = new String[]{"AB", "CD"};
1481         mSatelliteAccessController1.configDataSource = CONFIG_DATA_SOURCE_DEVICE_CONFIG;
1482         mSatelliteAccessController1.carrierId = 1;
1483 
1484         mSatelliteAccessController2 = new SatelliteAccessController();
1485         mSatelliteAccessController2.accessControlType = ACCESS_CONTROL_TYPE_CURRENT_LOCATION;
1486         mSatelliteAccessController2.locationQueryTimeMillis = TimeUnit.SECONDS.toMillis(4);
1487         mSatelliteAccessController2.onDeviceLookupTimeMillis = TimeUnit.SECONDS.toMillis(5);
1488         mSatelliteAccessController2.totalCheckingTimeMillis = TimeUnit.SECONDS.toMillis(6);
1489         mSatelliteAccessController2.isAllowed = false;
1490         mSatelliteAccessController2.isEmergency = true;
1491         mSatelliteAccessController2.resultCode = SATELLITE_RESULT_SUCCESS;
1492         mSatelliteAccessController2.countryCodes = new String[]{"EF", "GH"};
1493         mSatelliteAccessController2.configDataSource = CONFIG_DATA_SOURCE_CONFIG_UPDATER;
1494         mSatelliteAccessController2.carrierId = 10;
1495 
1496         mSatelliteAccessControllers = new SatelliteAccessController[]{
1497                 mSatelliteAccessController1, mSatelliteAccessController2
1498         };
1499     }
1500 
generateTestDataNetworkValidationsData()1501     private void generateTestDataNetworkValidationsData() {
1502 
1503         // for LTE #1
1504         mDataNetworkValidationLte1 = new DataNetworkValidation();
1505         mDataNetworkValidationLte1.networkType = TelephonyManager.NETWORK_TYPE_LTE;
1506         mDataNetworkValidationLte1.apnTypeBitmask = ApnSetting.TYPE_IMS;
1507         mDataNetworkValidationLte1.signalStrength = SignalStrength.SIGNAL_STRENGTH_GREAT;
1508         mDataNetworkValidationLte1.validationResult =
1509                 PreciseDataConnectionState.NETWORK_VALIDATION_SUCCESS;
1510         mDataNetworkValidationLte1.elapsedTimeInMillis = 100L;
1511         mDataNetworkValidationLte1.handoverAttempted = false;
1512         mDataNetworkValidationLte1.networkValidationCount = 1;
1513 
1514         // for LTE #2
1515         mDataNetworkValidationLte2 = new DataNetworkValidation();
1516         mDataNetworkValidationLte2.networkType = TelephonyManager.NETWORK_TYPE_LTE;
1517         mDataNetworkValidationLte2.apnTypeBitmask = ApnSetting.TYPE_IMS;
1518         mDataNetworkValidationLte2.signalStrength = SignalStrength.SIGNAL_STRENGTH_GREAT;
1519         mDataNetworkValidationLte2.validationResult =
1520                 PreciseDataConnectionState.NETWORK_VALIDATION_SUCCESS;
1521         mDataNetworkValidationLte2.elapsedTimeInMillis = 100L;
1522         mDataNetworkValidationLte2.handoverAttempted = false;
1523         mDataNetworkValidationLte2.networkValidationCount = 1;
1524 
1525         // for IWLAN #1
1526         mDataNetworkValidationIwlan1 = new DataNetworkValidation();
1527         mDataNetworkValidationIwlan1.networkType = TelephonyManager.NETWORK_TYPE_IWLAN;
1528         mDataNetworkValidationIwlan1.apnTypeBitmask = ApnSetting.TYPE_IMS;
1529         mDataNetworkValidationIwlan1.signalStrength = SignalStrength.SIGNAL_STRENGTH_POOR;
1530         mDataNetworkValidationIwlan1.validationResult =
1531                 PreciseDataConnectionState.NETWORK_VALIDATION_FAILURE;
1532         mDataNetworkValidationIwlan1.elapsedTimeInMillis = 10000L;
1533         mDataNetworkValidationIwlan1.handoverAttempted = false;
1534         mDataNetworkValidationIwlan1.networkValidationCount = 1;
1535 
1536         // for IWLAN #2
1537         mDataNetworkValidationIwlan2 = new DataNetworkValidation();
1538         mDataNetworkValidationIwlan2.networkType = TelephonyManager.NETWORK_TYPE_IWLAN;
1539         mDataNetworkValidationIwlan2.apnTypeBitmask = ApnSetting.TYPE_IMS;
1540         mDataNetworkValidationIwlan2.signalStrength = SignalStrength.SIGNAL_STRENGTH_POOR;
1541         mDataNetworkValidationIwlan2.validationResult =
1542                 PreciseDataConnectionState.NETWORK_VALIDATION_FAILURE;
1543         mDataNetworkValidationIwlan2.elapsedTimeInMillis = 30000L;
1544         mDataNetworkValidationIwlan2.handoverAttempted = false;
1545         mDataNetworkValidationIwlan2.networkValidationCount = 1;
1546 
1547         mDataNetworkValidations =
1548                 new DataNetworkValidation[] {
1549                         mDataNetworkValidationLte1,
1550                         mDataNetworkValidationLte1,
1551                         mDataNetworkValidationIwlan1,
1552                         mDataNetworkValidationIwlan2,
1553                 };
1554     }
1555 
1556     private static class TestablePersistAtomsStorage extends PersistAtomsStorage {
1557         private long mTimeMillis = START_TIME_MILLIS;
1558 
TestablePersistAtomsStorage(Context context)1559         TestablePersistAtomsStorage(Context context) {
1560             super(context);
1561             // Remove delay for saving to persistent storage during tests.
1562             mSaveImmediately = true;
1563         }
1564 
1565         @Override
getWallTimeMillis()1566         protected long getWallTimeMillis() {
1567             // NOTE: super class constructor will be executed before private field is set, which
1568             // gives the wrong start time (mTimeMillis will have its default value of 0L)
1569             return mTimeMillis == 0L ? START_TIME_MILLIS : mTimeMillis;
1570         }
1571 
setTimeMillis(long timeMillis)1572         private void setTimeMillis(long timeMillis) {
1573             mTimeMillis = timeMillis;
1574         }
1575 
incTimeMillis(long timeMillis)1576         private void incTimeMillis(long timeMillis) {
1577             mTimeMillis += timeMillis;
1578         }
1579 
getAtomsProto()1580         private PersistAtoms getAtomsProto() {
1581             // NOTE: unlike other methods in PersistAtomsStorage, this is not synchronized, but
1582             // should be fine since the test is single-threaded
1583             return mAtoms;
1584         }
1585     }
1586 
1587     private TestablePersistAtomsStorage mPersistAtomsStorage;
1588 
1589     private static final Comparator<MessageNano> sProtoComparator =
1590             new Comparator<>() {
1591                 @Override
1592                 public int compare(MessageNano o1, MessageNano o2) {
1593                     if (o1 == o2) {
1594                         return 0;
1595                     }
1596                     if (o1 == null) {
1597                         return -1;
1598                     }
1599                     if (o2 == null) {
1600                         return 1;
1601                     }
1602                     assertEquals(o1.getClass(), o2.getClass());
1603                     return o1.toString().compareTo(o2.toString());
1604                 }
1605             };
1606 
1607     @Before
setUp()1608     public void setUp() throws Exception {
1609         super.setUp(getClass().getSimpleName());
1610         mTestFileOutputStream = mock(FileOutputStream.class);
1611         makeTestData();
1612 
1613         // by default, test loading with real file IO and saving with mocks
1614         mTestFile = mFolder.newFile(TEST_FILE);
1615         doReturn(mTestFileOutputStream).when(mContext).openFileOutput(anyString(), anyInt());
1616         doReturn(mTestFile).when(mContext).getFileStreamPath(anyString());
1617     }
1618 
1619     @After
tearDown()1620     public void tearDown() throws Exception {
1621         mTestFile.delete();
1622         mTestFile = null;
1623         mFolder = null;
1624         mCall1Proto = null;
1625         mCall2Proto = null;
1626         mCall3Proto = null;
1627         mCall4Proto = null;
1628         mCarrier1LteUsageProto = null;
1629         mCarrier1UmtsUsageProto = null;
1630         mCarrier2LteUsageProto = null;
1631         mCarrier3LteUsageProto = null;
1632         mCarrier3GsmUsageProto = null;
1633         mVoiceCallRatUsages = null;
1634         mServiceSwitch1Proto = null;
1635         mServiceSwitch2Proto = null;
1636         mServiceState1Proto = null;
1637         mServiceState2Proto = null;
1638         mServiceState3Proto = null;
1639         mServiceState4Proto = null;
1640         mServiceState5Proto = null;
1641         mServiceSwitches = null;
1642         mServiceStates = null;
1643         mImsRegStatsUnregisteredLte0 = null;
1644         mImsRegStatsRegisteringLte0 = null;
1645         mImsRegistrationStatsLte0 = null;
1646         mImsRegistrationStatsWifi0 = null;
1647         mImsRegistrationStatsLte1 = null;
1648         mImsRegistrationTerminationLte = null;
1649         mImsRegistrationTerminationWifi = null;
1650         mImsRegistrationStats = null;
1651         mImsRegistrationTerminations = null;
1652         mDataCallSession0 = null;
1653         mDataCallSession1 = null;
1654         mImsRegistrationFeatureTagStats1Proto = null;
1655         mImsRegistrationFeatureTagStats2Proto = null;
1656         mImsRegistrationFeatureTagStatses = null;
1657         mRcsClientProvisioningStats1Proto = null;
1658         mRcsClientProvisioningStats2Proto = null;
1659         mRcsClientProvisioningStatses = null;
1660         mRcsAcsProvisioningStats1Proto = null;
1661         mRcsAcsProvisioningStats2Proto = null;
1662         mRcsAcsProvisioningStatses = null;
1663         mImsRegistrationServiceIm = null;
1664         mImsRegistrationServiceFt = null;
1665         mImsRegistrationServiceDescStats = null;
1666         mImsDedicatedBearerListenerEvent1 = null;
1667         mImsDedicatedBearerListenerEvent2 = null;
1668         mImsDedicatedBearerListenerEvents = null;
1669         mImsDedicatedBearerEvent1 = null;
1670         mImsDedicatedBearerEvent2 = null;
1671         mImsDedicatedBearerEvents = null;
1672         mUceEventStats1 = null;
1673         mUceEventStats2 = null;
1674         mUceEventStatses = null;
1675         mPresenceNotifyEvent1 = null;
1676         mPresenceNotifyEvent2 = null;
1677         mPresenceNotifyEvents = null;
1678         mSipTransportFeatureTagStats1 = null;
1679         mSipTransportFeatureTagStats2 = null;
1680         mSipTransportFeatureTagStatsArray = null;
1681         mSipDelegateStats1 = null;
1682         mSipDelegateStats2 = null;
1683         mSipDelegateStats3 = null;
1684         mSipDelegateStatsArray = null;
1685         mGbaEvent1 = null;
1686         mGbaEvent2 = null;
1687         mGbaEvent = null;
1688         mSipMessageResponse1 = null;
1689         mSipMessageResponse2 = null;
1690         mSipMessageResponse = null;
1691         mSipTransportSession1 = null;
1692         mSipTransportSession2 = null;
1693         mSipTransportSession = null;
1694         mIncomingSms = null;
1695         mIncomingSms1 = null;
1696         mIncomingSms2 = null;
1697         mOutgoingSms = null;
1698         mOutgoingSms1 = null;
1699         mOutgoingSms2 = null;
1700         mOutgoingShortCodeSms1 = null;
1701         mOutgoingShortCodeSms2 = null;
1702         mOutgoingShortCodeSms = null;
1703         mSatelliteController1 = null;
1704         mSatelliteController2 = null;
1705         mSatelliteControllers = null;
1706         mSatelliteSession1 = null;
1707         mSatelliteSession2 = null;
1708         mSatelliteSessions = null;
1709         mSatelliteIncomingDatagram1 = null;
1710         mSatelliteIncomingDatagram2 = null;
1711         mSatelliteIncomingDatagrams = null;
1712         mSatelliteOutgoingDatagram1 = null;
1713         mSatelliteOutgoingDatagram2 = null;
1714         mSatelliteOutgoingDatagrams = null;
1715         mSatelliteProvision1 = null;
1716         mSatelliteProvision2 = null;
1717         mSatelliteProvisions = null;
1718         mSatelliteSosMessageRecommender1 = null;
1719         mSatelliteSosMessageRecommender2 = null;
1720         mSatelliteSosMessageRecommenders = null;
1721         mDataNetworkValidationLte1 = null;
1722         mDataNetworkValidationLte2 = null;
1723         mDataNetworkValidationIwlan1 = null;
1724         mDataNetworkValidationIwlan2 = null;
1725         mCarrierRoamingSatelliteSession1 = null;
1726         mCarrierRoamingSatelliteSession2 = null;
1727         mCarrierRoamingSatelliteSessions = null;
1728         mCarrierRoamingSatelliteControllerStats1 = null;
1729         mCarrierRoamingSatelliteControllerStats2 = null;
1730         mCarrierRoamingSatelliteControllerStats = null;
1731         mSatelliteEntitlement1 = null;
1732         mSatelliteEntitlement2 = null;
1733         mSatelliteEntitlements = null;
1734         mSatelliteConfigUpdater1 = null;
1735         mSatelliteConfigUpdater2 = null;
1736         mSatelliteConfigUpdaters = null;
1737         mSatelliteAccessController1 = null;
1738         mSatelliteAccessController2 = null;
1739         mSatelliteAccessControllers = null;
1740         super.tearDown();
1741     }
1742 
1743     @Test
1744     @SmallTest
loadAtoms_fileNotExist()1745     public void loadAtoms_fileNotExist() throws Exception {
1746         mTestFile.delete();
1747 
1748         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1749         mPersistAtomsStorage.incTimeMillis(100L);
1750 
1751         // no exception should be thrown, storage should be empty, pull time should be start time
1752         assertAllPullTimestampEquals(START_TIME_MILLIS);
1753         assertStorageIsEmptyForAllAtoms();
1754     }
1755 
1756     @Test
1757     @SmallTest
loadAtoms_unreadable()1758     public void loadAtoms_unreadable() throws Exception {
1759         createEmptyTestFile();
1760         mTestFile.setReadable(false);
1761 
1762         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1763         mPersistAtomsStorage.incTimeMillis(100L);
1764 
1765         // no exception should be thrown, storage should be empty, pull time should be start time
1766         assertAllPullTimestampEquals(START_TIME_MILLIS);
1767         assertStorageIsEmptyForAllAtoms();
1768     }
1769 
1770     @Test
1771     @SmallTest
loadAtoms_emptyProto()1772     public void loadAtoms_emptyProto() throws Exception {
1773         createEmptyTestFile();
1774 
1775         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1776         mPersistAtomsStorage.incTimeMillis(100L);
1777 
1778         // no exception should be thrown, storage should be empty, pull time should be start time
1779         assertAllPullTimestampEquals(START_TIME_MILLIS);
1780         assertStorageIsEmptyForAllAtoms();
1781     }
1782 
1783     @Test
1784     @SmallTest
loadAtoms_malformedFile()1785     public void loadAtoms_malformedFile() throws Exception {
1786         FileOutputStream stream = new FileOutputStream(mTestFile);
1787         stream.write("This is not a proto file.".getBytes(StandardCharsets.UTF_8));
1788         stream.close();
1789 
1790         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1791         mPersistAtomsStorage.incTimeMillis(100L);
1792 
1793         // no exception should be thrown, storage should be empty, pull time should be start time
1794         assertAllPullTimestampEquals(START_TIME_MILLIS);
1795         assertStorageIsEmptyForAllAtoms();
1796     }
1797 
1798     @Test
1799     @SmallTest
loadAtoms_pullTimeMissing()1800     public void loadAtoms_pullTimeMissing() throws Exception {
1801         // create test file with lastPullTimeMillis = 0L, i.e. default/unknown
1802         createTestFile(0L);
1803 
1804         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1805         mPersistAtomsStorage.incTimeMillis(100L);
1806 
1807         // no exception should be thrown, storage should be match, pull time should be start time
1808         assertAllPullTimestampEquals(START_TIME_MILLIS);
1809         assertProtoArrayEquals(mVoiceCallRatUsages, mPersistAtomsStorage.getVoiceCallRatUsages(0L));
1810         assertProtoArrayEquals(mVoiceCallSessions, mPersistAtomsStorage.getVoiceCallSessions(0L));
1811         assertProtoArrayEqualsIgnoringOrder(
1812                 mServiceStates, mPersistAtomsStorage.getCellularServiceStates(0L));
1813         assertProtoArrayEqualsIgnoringOrder(
1814                 mServiceSwitches, mPersistAtomsStorage.getCellularDataServiceSwitches(0L));
1815     }
1816 
1817     @Test
1818     @SmallTest
loadAtoms_validContents()1819     public void loadAtoms_validContents() throws Exception {
1820         createTestFile(/* lastPullTimeMillis= */ 100L);
1821 
1822         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1823 
1824         // no exception should be thrown, storage and pull time should match
1825         assertAllPullTimestampEquals(100L);
1826         assertProtoArrayEquals(mVoiceCallRatUsages, mPersistAtomsStorage.getVoiceCallRatUsages(0L));
1827         assertProtoArrayEquals(mVoiceCallSessions, mPersistAtomsStorage.getVoiceCallSessions(0L));
1828         assertProtoArrayEqualsIgnoringOrder(
1829                 mServiceStates, mPersistAtomsStorage.getCellularServiceStates(0L));
1830         assertProtoArrayEqualsIgnoringOrder(
1831                 mServiceSwitches, mPersistAtomsStorage.getCellularDataServiceSwitches(0L));
1832     }
1833 
1834     @Test
1835     @SmallTest
addVoiceCallSession_emptyProto()1836     public void addVoiceCallSession_emptyProto() throws Exception {
1837         createEmptyTestFile();
1838 
1839         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1840         mPersistAtomsStorage.addVoiceCallSession(mCall1Proto);
1841         mPersistAtomsStorage.incTimeMillis(100L);
1842 
1843         // call should be added successfully, there should be no RAT usage, changes should be saved
1844         verifyCurrentStateSavedToFileOnce();
1845         assertProtoArrayIsEmpty(mPersistAtomsStorage.getVoiceCallRatUsages(0L));
1846         VoiceCallSession[] voiceCallSession = mPersistAtomsStorage.getVoiceCallSessions(0L);
1847         assertProtoArrayEquals(new VoiceCallSession[] {mCall1Proto}, voiceCallSession);
1848     }
1849 
1850     @Test
1851     @SmallTest
addVoiceCallSession_withExistingCalls()1852     public void addVoiceCallSession_withExistingCalls() throws Exception {
1853         createTestFile(START_TIME_MILLIS);
1854 
1855         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1856         mPersistAtomsStorage.addVoiceCallSession(mCall1Proto);
1857         mPersistAtomsStorage.incTimeMillis(100L);
1858 
1859         // call should be added successfully, RAT usages should not change, changes should be saved
1860         assertProtoArrayEquals(mVoiceCallRatUsages, mPersistAtomsStorage.getVoiceCallRatUsages(0L));
1861         VoiceCallSession[] expectedVoiceCallSessions =
1862                 new VoiceCallSession[] {
1863                     mCall1Proto, mCall1Proto, mCall2Proto, mCall3Proto, mCall4Proto
1864                 };
1865         // call list is randomized at this point
1866         verifyCurrentStateSavedToFileOnce();
1867         assertProtoArrayEqualsIgnoringOrder(
1868                 expectedVoiceCallSessions, mPersistAtomsStorage.getVoiceCallSessions(0L));
1869     }
1870 
1871     @Test
1872     @SmallTest
addVoiceCallSession_tooManyCalls()1873     public void addVoiceCallSession_tooManyCalls() throws Exception {
1874         createEmptyTestFile();
1875 
1876         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1877         addRepeatedCalls(mPersistAtomsStorage, mCall1Proto, 50);
1878         mPersistAtomsStorage.addVoiceCallSession(mCall2Proto);
1879         mPersistAtomsStorage.incTimeMillis(100L);
1880 
1881         // one previous call should be evicted, the new call should be added
1882         verifyCurrentStateSavedToFileOnce();
1883         VoiceCallSession[] calls = mPersistAtomsStorage.getVoiceCallSessions(0L);
1884         assertHasCall(calls, mCall1Proto, /* expectedCount= */ 49);
1885         assertHasCall(calls, mCall2Proto, /* expectedCount= */ 1);
1886     }
1887 
1888     @Test
1889     @SmallTest
addVoiceCallSession_tooManyCalls_withEmergencyCalls()1890     public void addVoiceCallSession_tooManyCalls_withEmergencyCalls() throws Exception {
1891         createEmptyTestFile();
1892         // We initially have storage full of emergency calls except one.
1893         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1894         addRepeatedCalls(mPersistAtomsStorage, mCall4Proto, 49);
1895         mPersistAtomsStorage.addVoiceCallSession(mCall1Proto);
1896 
1897         mPersistAtomsStorage.addVoiceCallSession(mCall4Proto);
1898         mPersistAtomsStorage.incTimeMillis(100L);
1899 
1900         // after adding one more emergency call, the previous non-emergency call should be evicted
1901         verifyCurrentStateSavedToFileOnce();
1902         VoiceCallSession[] calls = mPersistAtomsStorage.getVoiceCallSessions(0L);
1903         assertHasCall(calls, mCall4Proto, /* expectedCount= */ 50);
1904     }
1905 
1906     @Test
1907     @SmallTest
addVoiceCallRatUsage_emptyProto()1908     public void addVoiceCallRatUsage_emptyProto() throws Exception {
1909         createEmptyTestFile();
1910         VoiceCallRatTracker ratTracker = VoiceCallRatTracker.fromProto(mVoiceCallRatUsages);
1911 
1912         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1913         mPersistAtomsStorage.addVoiceCallRatUsage(ratTracker);
1914         mPersistAtomsStorage.incTimeMillis(100L);
1915 
1916         // RAT should be added successfully, calls should not change, changes should be saved
1917         verifyCurrentStateSavedToFileOnce();
1918         assertProtoArrayEqualsIgnoringOrder(
1919                 mVoiceCallRatUsages, mPersistAtomsStorage.getVoiceCallRatUsages(0L));
1920         assertProtoArrayIsEmpty(mPersistAtomsStorage.getVoiceCallSessions(0L));
1921     }
1922 
1923     @Test
1924     @SmallTest
addVoiceCallRatUsage_withExistingUsages()1925     public void addVoiceCallRatUsage_withExistingUsages() throws Exception {
1926         createTestFile(START_TIME_MILLIS);
1927         VoiceCallRatTracker ratTracker = VoiceCallRatTracker.fromProto(mVoiceCallRatUsages);
1928 
1929         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1930         mPersistAtomsStorage.addVoiceCallRatUsage(ratTracker);
1931         mPersistAtomsStorage.incTimeMillis(100L);
1932 
1933         // RAT should be added successfully, calls should not change, changes should be saved
1934         // call count and duration should become doubled since mVoiceCallRatUsages applied through
1935         // both file and addVoiceCallRatUsage()
1936         verifyCurrentStateSavedToFileOnce();
1937         VoiceCallRatUsage[] expectedVoiceCallRatUsage =
1938                 multiplyVoiceCallRatUsage(mVoiceCallRatUsages, 2);
1939         assertProtoArrayEqualsIgnoringOrder(
1940                 expectedVoiceCallRatUsage, mPersistAtomsStorage.getVoiceCallRatUsages(0L));
1941         assertProtoArrayEquals(mVoiceCallSessions, mPersistAtomsStorage.getVoiceCallSessions(0L));
1942     }
1943 
1944     @Test
1945     @SmallTest
addVoiceCallRatUsage_empty()1946     public void addVoiceCallRatUsage_empty() throws Exception {
1947         createEmptyTestFile();
1948         VoiceCallRatTracker ratTracker = VoiceCallRatTracker.fromProto(new VoiceCallRatUsage[0]);
1949 
1950         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1951         mPersistAtomsStorage.addVoiceCallRatUsage(ratTracker);
1952         mPersistAtomsStorage.incTimeMillis(100L);
1953 
1954         // RAT should be added successfully, calls should not change
1955         // in this case saving is unnecessarily
1956         assertProtoArrayIsEmpty(mPersistAtomsStorage.getVoiceCallRatUsages(0L));
1957         assertProtoArrayIsEmpty(mPersistAtomsStorage.getVoiceCallSessions(0L));
1958     }
1959 
1960     @Test
1961     @SmallTest
getVoiceCallRatUsages_tooFrequent()1962     public void getVoiceCallRatUsages_tooFrequent() throws Exception {
1963         createTestFile(START_TIME_MILLIS);
1964 
1965         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1966         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
1967         VoiceCallRatUsage[] voiceCallRatUsage = mPersistAtomsStorage.getVoiceCallRatUsages(100L);
1968 
1969         // Should be denied
1970         assertNull(voiceCallRatUsage);
1971     }
1972 
1973     @Test
1974     @SmallTest
getVoiceCallRatUsages_withSavedAtoms()1975     public void getVoiceCallRatUsages_withSavedAtoms() throws Exception {
1976         createTestFile(START_TIME_MILLIS);
1977 
1978         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
1979         mPersistAtomsStorage.incTimeMillis(100L);
1980         VoiceCallRatUsage[] voiceCallRatUsage1 = mPersistAtomsStorage.getVoiceCallRatUsages(50L);
1981         mPersistAtomsStorage.incTimeMillis(100L);
1982         VoiceCallRatUsage[] voiceCallRatUsage2 = mPersistAtomsStorage.getVoiceCallRatUsages(50L);
1983         long voiceCallSessionPullTimestampMillis =
1984                 mPersistAtomsStorage.getAtomsProto().voiceCallSessionPullTimestampMillis;
1985         VoiceCallSession[] voiceCallSession = mPersistAtomsStorage.getVoiceCallSessions(50L);
1986 
1987         // First set of results should equal to file contents, second should be empty, corresponding
1988         // pull timestamp should be updated and saved, other fields should be unaffected
1989         assertProtoArrayEquals(mVoiceCallRatUsages, voiceCallRatUsage1);
1990         assertProtoArrayIsEmpty(voiceCallRatUsage2);
1991         assertEquals(
1992                 START_TIME_MILLIS + 200L,
1993                 mPersistAtomsStorage.getAtomsProto().voiceCallRatUsagePullTimestampMillis);
1994         assertProtoArrayEquals(mVoiceCallSessions, voiceCallSession);
1995         assertEquals(START_TIME_MILLIS, voiceCallSessionPullTimestampMillis);
1996         InOrder inOrder = inOrder(mTestFileOutputStream);
1997         assertEquals(
1998                 START_TIME_MILLIS + 100L,
1999                 getAtomsWritten(inOrder).voiceCallRatUsagePullTimestampMillis);
2000         assertEquals(
2001                 START_TIME_MILLIS + 200L,
2002                 getAtomsWritten(inOrder).voiceCallRatUsagePullTimestampMillis);
2003         assertEquals(
2004                 START_TIME_MILLIS + 200L,
2005                 getAtomsWritten(inOrder).voiceCallSessionPullTimestampMillis);
2006         inOrder.verifyNoMoreInteractions();
2007     }
2008 
2009     @Test
2010     @SmallTest
getVoiceCallSessions_tooFrequent()2011     public void getVoiceCallSessions_tooFrequent() throws Exception {
2012         createTestFile(START_TIME_MILLIS);
2013 
2014         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2015         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
2016         VoiceCallSession[] voiceCallSession = mPersistAtomsStorage.getVoiceCallSessions(100L);
2017 
2018         // Should be denied
2019         assertNull(voiceCallSession);
2020     }
2021 
2022     @Test
2023     @SmallTest
getVoiceCallSessions_withSavedAtoms()2024     public void getVoiceCallSessions_withSavedAtoms() throws Exception {
2025         createTestFile(START_TIME_MILLIS);
2026 
2027         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2028         mPersistAtomsStorage.incTimeMillis(100L);
2029         VoiceCallSession[] voiceCallSession1 = mPersistAtomsStorage.getVoiceCallSessions(50L);
2030         mPersistAtomsStorage.incTimeMillis(100L);
2031         VoiceCallSession[] voiceCallSession2 = mPersistAtomsStorage.getVoiceCallSessions(50L);
2032         long voiceCallRatUsagePullTimestampMillis =
2033                 mPersistAtomsStorage.getAtomsProto().voiceCallRatUsagePullTimestampMillis;
2034         VoiceCallRatUsage[] voiceCallRatUsage = mPersistAtomsStorage.getVoiceCallRatUsages(50L);
2035 
2036         // First set of results should equal to file contents, second should be empty, corresponding
2037         // pull timestamp should be updated and saved, other fields should be unaffected
2038         assertProtoArrayEquals(mVoiceCallSessions, voiceCallSession1);
2039         assertProtoArrayIsEmpty(voiceCallSession2);
2040         assertEquals(
2041                 START_TIME_MILLIS + 200L,
2042                 mPersistAtomsStorage.getAtomsProto().voiceCallSessionPullTimestampMillis);
2043         assertProtoArrayEquals(mVoiceCallRatUsages, voiceCallRatUsage);
2044         assertEquals(START_TIME_MILLIS, voiceCallRatUsagePullTimestampMillis);
2045         InOrder inOrder = inOrder(mTestFileOutputStream);
2046         assertEquals(
2047                 START_TIME_MILLIS + 100L,
2048                 getAtomsWritten(inOrder).voiceCallSessionPullTimestampMillis);
2049         assertEquals(
2050                 START_TIME_MILLIS + 200L,
2051                 getAtomsWritten(inOrder).voiceCallSessionPullTimestampMillis);
2052         assertEquals(
2053                 START_TIME_MILLIS + 200L,
2054                 getAtomsWritten(inOrder).voiceCallRatUsagePullTimestampMillis);
2055         inOrder.verifyNoMoreInteractions();
2056     }
2057 
2058     @Test
2059     @SmallTest
addCellularServiceStateAndCellularDataServiceSwitch_emptyProto()2060     public void addCellularServiceStateAndCellularDataServiceSwitch_emptyProto() throws Exception {
2061         createEmptyTestFile();
2062 
2063         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2064         mPersistAtomsStorage.addCellularServiceStateAndCellularDataServiceSwitch(
2065                 mServiceState1Proto, mServiceSwitch1Proto);
2066         mPersistAtomsStorage.incTimeMillis(100L);
2067 
2068         // Service state and service switch should be added successfully
2069         verifyCurrentStateSavedToFileOnce();
2070         CellularServiceState[] serviceStates = mPersistAtomsStorage.getCellularServiceStates(0L);
2071         CellularDataServiceSwitch[] serviceSwitches =
2072                 mPersistAtomsStorage.getCellularDataServiceSwitches(0L);
2073         assertProtoArrayEquals(new CellularServiceState[] {mServiceState1Proto}, serviceStates);
2074         assertProtoArrayEquals(
2075                 new CellularDataServiceSwitch[] {mServiceSwitch1Proto}, serviceSwitches);
2076     }
2077 
2078     @Test
2079     @SmallTest
addCellularServiceStateAndCellularDataServiceSwitch_withExistingEntries()2080     public void addCellularServiceStateAndCellularDataServiceSwitch_withExistingEntries()
2081             throws Exception {
2082         createEmptyTestFile();
2083         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2084         mPersistAtomsStorage.addCellularServiceStateAndCellularDataServiceSwitch(
2085                 mServiceState1Proto, mServiceSwitch1Proto);
2086 
2087         mPersistAtomsStorage.addCellularServiceStateAndCellularDataServiceSwitch(
2088                 mServiceState2Proto, mServiceSwitch2Proto);
2089         mPersistAtomsStorage.incTimeMillis(100L);
2090 
2091         // Service state and service switch should be added successfully
2092         verifyCurrentStateSavedToFileOnce();
2093         CellularServiceState[] serviceStates = mPersistAtomsStorage.getCellularServiceStates(0L);
2094         CellularDataServiceSwitch[] serviceSwitches =
2095                 mPersistAtomsStorage.getCellularDataServiceSwitches(0L);
2096         assertProtoArrayEqualsIgnoringOrder(
2097                 new CellularServiceState[] {mServiceState1Proto, mServiceState2Proto},
2098                 serviceStates);
2099         assertProtoArrayEqualsIgnoringOrder(
2100                 new CellularDataServiceSwitch[] {mServiceSwitch1Proto, mServiceSwitch2Proto},
2101                 serviceSwitches);
2102     }
2103 
2104     @Test
2105     @SmallTest
addCellularServiceStateAndCellularDataServiceSwitch_updateExistingEntries()2106     public void addCellularServiceStateAndCellularDataServiceSwitch_updateExistingEntries()
2107             throws Exception {
2108         createTestFile(START_TIME_MILLIS);
2109         CellularServiceState newServiceState1Proto = copyOf(mServiceState1Proto);
2110         CellularDataServiceSwitch newServiceSwitch1Proto = copyOf(mServiceSwitch1Proto);
2111         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2112 
2113         mPersistAtomsStorage.addCellularServiceStateAndCellularDataServiceSwitch(
2114                 copyOf(mServiceState1Proto), copyOf(mServiceSwitch1Proto));
2115         mPersistAtomsStorage.incTimeMillis(100L);
2116 
2117         // mServiceState1Proto's duration and mServiceSwitch1Proto's switch should be doubled
2118         verifyCurrentStateSavedToFileOnce();
2119         CellularServiceState[] serviceStates = mPersistAtomsStorage.getCellularServiceStates(0L);
2120         newServiceState1Proto.totalTimeMillis *= 2;
2121         assertProtoArrayEqualsIgnoringOrder(
2122                 new CellularServiceState[] {
2123                     newServiceState1Proto,
2124                     mServiceState2Proto,
2125                     mServiceState3Proto,
2126                     mServiceState4Proto,
2127                     mServiceState5Proto
2128                 },
2129                 serviceStates);
2130         CellularDataServiceSwitch[] serviceSwitches =
2131                 mPersistAtomsStorage.getCellularDataServiceSwitches(0L);
2132         newServiceSwitch1Proto.switchCount *= 2;
2133         assertProtoArrayEqualsIgnoringOrder(
2134                 new CellularDataServiceSwitch[] {newServiceSwitch1Proto, mServiceSwitch2Proto},
2135                 serviceSwitches);
2136     }
2137 
2138     @Test
2139     @SmallTest
addCellularServiceStateAndCellularDataServiceSwitch_tooManyServiceStates()2140     public void addCellularServiceStateAndCellularDataServiceSwitch_tooManyServiceStates()
2141             throws Exception {
2142         createEmptyTestFile();
2143         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2144         Queue<CellularServiceState> expectedServiceStates = new LinkedList<>();
2145         Queue<CellularDataServiceSwitch> expectedServiceSwitches = new LinkedList<>();
2146 
2147         // Add 51 service states, with the first being least recent
2148         for (int i = 0; i < 51; i++) {
2149             CellularServiceState state = new CellularServiceState();
2150             state.voiceRat = i / 10;
2151             state.dataRat = i % 10;
2152             expectedServiceStates.add(state);
2153             CellularDataServiceSwitch serviceSwitch = new CellularDataServiceSwitch();
2154             serviceSwitch.ratFrom = i / 10;
2155             serviceSwitch.ratTo = i % 10;
2156             expectedServiceSwitches.add(serviceSwitch);
2157             mPersistAtomsStorage.addCellularServiceStateAndCellularDataServiceSwitch(
2158                     copyOf(state), copyOf(serviceSwitch));
2159             mPersistAtomsStorage.incTimeMillis(100L);
2160         }
2161 
2162         // The least recent (the first) service state should be evicted
2163         verifyCurrentStateSavedToFileOnce();
2164         CellularServiceState[] serviceStates = mPersistAtomsStorage.getCellularServiceStates(0L);
2165         expectedServiceStates.remove();
2166         assertProtoArrayEqualsIgnoringOrder(
2167                 expectedServiceStates.toArray(new CellularServiceState[0]), serviceStates);
2168         CellularDataServiceSwitch[] serviceSwitches =
2169                 mPersistAtomsStorage.getCellularDataServiceSwitches(0L);
2170         expectedServiceSwitches.remove();
2171         assertProtoArrayEqualsIgnoringOrder(
2172                 expectedServiceSwitches.toArray(new CellularDataServiceSwitch[0]), serviceSwitches);
2173     }
2174 
2175     @Test
2176     @SmallTest
getCellularDataServiceSwitches_tooFrequent()2177     public void getCellularDataServiceSwitches_tooFrequent() throws Exception {
2178         createTestFile(START_TIME_MILLIS);
2179 
2180         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2181         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
2182         CellularDataServiceSwitch[] serviceSwitches =
2183                 mPersistAtomsStorage.getCellularDataServiceSwitches(100L);
2184 
2185         // Should be denied
2186         assertNull(serviceSwitches);
2187     }
2188 
2189     @Test
2190     @SmallTest
getCellularDataServiceSwitches_withSavedAtoms()2191     public void getCellularDataServiceSwitches_withSavedAtoms() throws Exception {
2192         createTestFile(START_TIME_MILLIS);
2193 
2194         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2195         mPersistAtomsStorage.incTimeMillis(100L);
2196         CellularDataServiceSwitch[] serviceSwitches1 =
2197                 mPersistAtomsStorage.getCellularDataServiceSwitches(50L);
2198         mPersistAtomsStorage.incTimeMillis(100L);
2199         CellularDataServiceSwitch[] serviceSwitches2 =
2200                 mPersistAtomsStorage.getCellularDataServiceSwitches(50L);
2201 
2202         // First set of results should equal to file contents, second should be empty, corresponding
2203         // pull timestamp should be updated and saved
2204         assertProtoArrayEqualsIgnoringOrder(
2205                 new CellularDataServiceSwitch[] {mServiceSwitch1Proto, mServiceSwitch2Proto},
2206                 serviceSwitches1);
2207         assertProtoArrayEquals(new CellularDataServiceSwitch[0], serviceSwitches2);
2208         assertEquals(
2209                 START_TIME_MILLIS + 200L,
2210                 mPersistAtomsStorage.getAtomsProto().cellularDataServiceSwitchPullTimestampMillis);
2211         InOrder inOrder = inOrder(mTestFileOutputStream);
2212         assertEquals(
2213                 START_TIME_MILLIS + 100L,
2214                 getAtomsWritten(inOrder).cellularDataServiceSwitchPullTimestampMillis);
2215         assertEquals(
2216                 START_TIME_MILLIS + 200L,
2217                 getAtomsWritten(inOrder).cellularDataServiceSwitchPullTimestampMillis);
2218         inOrder.verifyNoMoreInteractions();
2219     }
2220 
2221     @Test
2222     @SmallTest
getCellularServiceStates_tooFrequent()2223     public void getCellularServiceStates_tooFrequent() throws Exception {
2224         createTestFile(START_TIME_MILLIS);
2225 
2226         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2227         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
2228         CellularServiceState[] serviceStates = mPersistAtomsStorage.getCellularServiceStates(100L);
2229 
2230         // Should be denied
2231         assertNull(serviceStates);
2232     }
2233 
2234     @Test
2235     @SmallTest
getCellularServiceStates_withSavedAtoms()2236     public void getCellularServiceStates_withSavedAtoms() throws Exception {
2237         createTestFile(START_TIME_MILLIS);
2238 
2239         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2240         mPersistAtomsStorage.incTimeMillis(100L);
2241         CellularServiceState[] serviceStates1 = mPersistAtomsStorage.getCellularServiceStates(50L);
2242         mPersistAtomsStorage.incTimeMillis(100L);
2243         CellularServiceState[] serviceStates2 = mPersistAtomsStorage.getCellularServiceStates(50L);
2244 
2245         // First set of results should equal to file contents, second should be empty, corresponding
2246         // pull timestamp should be updated and saved
2247         assertProtoArrayEqualsIgnoringOrder(
2248                 new CellularServiceState[] {
2249                     mServiceState1Proto,
2250                     mServiceState2Proto,
2251                     mServiceState3Proto,
2252                     mServiceState4Proto,
2253                     mServiceState5Proto
2254                 },
2255                 serviceStates1);
2256         assertProtoArrayEquals(new CellularServiceState[0], serviceStates2);
2257         assertEquals(
2258                 START_TIME_MILLIS + 200L,
2259                 mPersistAtomsStorage.getAtomsProto().cellularServiceStatePullTimestampMillis);
2260         InOrder inOrder = inOrder(mTestFileOutputStream);
2261         assertEquals(
2262                 START_TIME_MILLIS + 100L,
2263                 getAtomsWritten(inOrder).cellularServiceStatePullTimestampMillis);
2264         assertEquals(
2265                 START_TIME_MILLIS + 200L,
2266                 getAtomsWritten(inOrder).cellularServiceStatePullTimestampMillis);
2267         inOrder.verifyNoMoreInteractions();
2268     }
2269 
2270     @Test
2271     @SmallTest
addImsRegistrationStats_emptyProto()2272     public void addImsRegistrationStats_emptyProto() throws Exception {
2273         createEmptyTestFile();
2274 
2275         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2276         mPersistAtomsStorage.addImsRegistrationStats(copyOf(mImsRegistrationStatsLte0));
2277         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS);
2278 
2279         // mImsRegistrationStatsLte0 should be added successfully
2280         verifyCurrentStateSavedToFileOnce();
2281         ImsRegistrationStats[] regStats = mPersistAtomsStorage.getImsRegistrationStats(0L);
2282         assertProtoArrayEquals(new ImsRegistrationStats[] {mImsRegistrationStatsLte0}, regStats);
2283     }
2284 
2285     @Test
2286     @SmallTest
addImsRegistrationStats_withExistingEntries()2287     public void addImsRegistrationStats_withExistingEntries() throws Exception {
2288         createEmptyTestFile();
2289         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2290         mPersistAtomsStorage.addImsRegistrationStats(copyOf(mImsRegistrationStatsLte0));
2291 
2292         mPersistAtomsStorage.addImsRegistrationStats(copyOf(mImsRegistrationStatsWifi0));
2293         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS);
2294 
2295         // mImsRegistrationStatsLte0 and mImsRegistrationStatsWifi0 should be added successfully
2296         verifyCurrentStateSavedToFileOnce();
2297         ImsRegistrationStats[] regStats = mPersistAtomsStorage.getImsRegistrationStats(0L);
2298         assertProtoArrayEqualsIgnoringOrder(
2299                 new ImsRegistrationStats[] {mImsRegistrationStatsLte0, mImsRegistrationStatsWifi0},
2300                 regStats);
2301     }
2302 
2303     @Test
2304     @SmallTest
addImsRegistrationStats_withExistingRegisteringEntries()2305     public void addImsRegistrationStats_withExistingRegisteringEntries() throws Exception {
2306         createEmptyTestFile();
2307         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2308         mPersistAtomsStorage.addImsRegistrationStats(copyOf(mImsRegStatsRegisteringLte0));
2309         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS);
2310 
2311         // mImsRegStatsRegisteringLte0's info should be added successfully
2312         verifyCurrentStateSavedToFileOnce();
2313         ImsRegistrationStats[] regStats = mPersistAtomsStorage.getImsRegistrationStats(0L);
2314         assertProtoArrayEqualsIgnoringOrder(
2315                 new ImsRegistrationStats[] {mImsRegStatsRegisteringLte0},
2316                 regStats);
2317     }
2318 
2319     @Test
2320     @SmallTest
addImsRegistrationStats_updateExistingEntries()2321     public void addImsRegistrationStats_updateExistingEntries() throws Exception {
2322         createTestFile(START_TIME_MILLIS);
2323         ImsRegistrationStats newImsRegistrationStatsLte0 = copyOf(mImsRegStatsUnregisteredLte0);
2324         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2325 
2326         mPersistAtomsStorage.addImsRegistrationStats(copyOf(mImsRegStatsUnregisteredLte0));
2327         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS);
2328 
2329         // mImsRegStatsUnregisteredLte0's durations should be doubled
2330         verifyCurrentStateSavedToFileOnce();
2331         ImsRegistrationStats[] regStats = mPersistAtomsStorage.getImsRegistrationStats(0L);
2332         newImsRegistrationStatsLte0.unregisteredMillis *= 2;
2333         newImsRegistrationStatsLte0.voiceCapableMillis *= 2;
2334         newImsRegistrationStatsLte0.voiceAvailableMillis *= 2;
2335         newImsRegistrationStatsLte0.smsCapableMillis *= 2;
2336         newImsRegistrationStatsLte0.smsAvailableMillis *= 2;
2337         newImsRegistrationStatsLte0.videoCapableMillis *= 2;
2338         newImsRegistrationStatsLte0.videoAvailableMillis *= 2;
2339         newImsRegistrationStatsLte0.utCapableMillis *= 2;
2340         newImsRegistrationStatsLte0.utAvailableMillis *= 2;
2341         assertProtoArrayEqualsIgnoringOrder(
2342                 new ImsRegistrationStats[] {
2343                     newImsRegistrationStatsLte0,
2344                     mImsRegStatsRegisteringLte0,
2345                     mImsRegistrationStatsLte0,
2346                     mImsRegistrationStatsWifi0,
2347                     mImsRegistrationStatsLte1
2348                 },
2349                 regStats);
2350     }
2351 
2352     @Test
2353     @SmallTest
addImsRegistrationStats_tooManyRegistrationStats()2354     public void addImsRegistrationStats_tooManyRegistrationStats() throws Exception {
2355         createEmptyTestFile();
2356         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2357         Queue<ImsRegistrationStats> expectedRegistrationStats = new LinkedList<>();
2358 
2359         // Add 11 registration stats
2360         for (int i = 0; i < 11; i++) {
2361             ImsRegistrationStats stats = copyOf(mImsRegistrationStatsLte0);
2362             stats.rat = i;
2363             expectedRegistrationStats.add(stats);
2364             mPersistAtomsStorage.addImsRegistrationStats(stats);
2365             mPersistAtomsStorage.incTimeMillis(100L);
2366         }
2367 
2368         // The least recent (the first) registration stats should be evicted
2369         verifyCurrentStateSavedToFileOnce();
2370         ImsRegistrationStats[] stats = mPersistAtomsStorage.getImsRegistrationStats(0L);
2371         expectedRegistrationStats.remove();
2372         assertProtoArrayEqualsIgnoringOrder(
2373                 expectedRegistrationStats.toArray(new ImsRegistrationStats[0]), stats);
2374     }
2375 
2376     @Test
2377     @SmallTest
addImsRegistrationStats_withExistingDurationEntries()2378     public void addImsRegistrationStats_withExistingDurationEntries() throws Exception {
2379         createEmptyTestFile();
2380         ImsRegistrationStats newImsRegStatsLte0 = copyOf(mImsRegistrationStatsLte0);
2381         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2382         mPersistAtomsStorage.addImsRegistrationStats(copyOf(mImsRegStatsUnregisteredLte0));
2383         mPersistAtomsStorage.addImsRegistrationStats(copyOf(mImsRegStatsRegisteringLte0));
2384         mPersistAtomsStorage.addImsRegistrationStats(copyOf(mImsRegistrationStatsLte0));
2385         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS);
2386 
2387         // UnregisteredMillis, registeringMillis and registeredTimes should be added successfully
2388         // capable and available durations should be tripled
2389         verifyCurrentStateSavedToFileOnce();
2390         ImsRegistrationStats[] regStats = mPersistAtomsStorage.getImsRegistrationStats(0L);
2391         newImsRegStatsLte0.unregisteredMillis += newImsRegStatsLte0.registeredMillis;
2392         newImsRegStatsLte0.registeringMillis += newImsRegStatsLte0.registeredMillis;
2393         newImsRegStatsLte0.registeredTimes += 1;
2394         newImsRegStatsLte0.voiceCapableMillis *= 3;
2395         newImsRegStatsLte0.voiceAvailableMillis *= 3;
2396         newImsRegStatsLte0.smsCapableMillis *= 3;
2397         newImsRegStatsLte0.smsAvailableMillis *= 3;
2398         newImsRegStatsLte0.videoCapableMillis *= 3;
2399         newImsRegStatsLte0.videoAvailableMillis *= 3;
2400         newImsRegStatsLte0.utCapableMillis *= 3;
2401         newImsRegStatsLte0.utAvailableMillis *= 3;
2402         assertProtoArrayEqualsIgnoringOrder(
2403                 new ImsRegistrationStats[] {
2404                     newImsRegStatsLte0
2405                 },
2406                 regStats);
2407     }
2408 
2409     @Test
2410     @SmallTest
addImsRegistrationTermination_emptyProto()2411     public void addImsRegistrationTermination_emptyProto() throws Exception {
2412         createEmptyTestFile();
2413 
2414         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2415         mPersistAtomsStorage.addImsRegistrationTermination(mImsRegistrationTerminationLte);
2416         mPersistAtomsStorage.incTimeMillis(100L);
2417 
2418         // Service state and service switch should be added successfully
2419         verifyCurrentStateSavedToFileOnce();
2420         ImsRegistrationTermination[] terminations =
2421                 mPersistAtomsStorage.getImsRegistrationTerminations(0L);
2422         assertProtoArrayEquals(
2423                 new ImsRegistrationTermination[] {mImsRegistrationTerminationLte}, terminations);
2424     }
2425 
2426     @Test
2427     @SmallTest
addImsRegistrationTermination_withExistingEntries()2428     public void addImsRegistrationTermination_withExistingEntries() throws Exception {
2429         createEmptyTestFile();
2430         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2431         mPersistAtomsStorage.addImsRegistrationTermination(mImsRegistrationTerminationLte);
2432 
2433         mPersistAtomsStorage.addImsRegistrationTermination(mImsRegistrationTerminationWifi);
2434         mPersistAtomsStorage.incTimeMillis(100L);
2435 
2436         // Service state and service switch should be added successfully
2437         verifyCurrentStateSavedToFileOnce();
2438         ImsRegistrationTermination[] terminations =
2439                 mPersistAtomsStorage.getImsRegistrationTerminations(0L);
2440         assertProtoArrayEqualsIgnoringOrder(
2441                 new ImsRegistrationTermination[] {
2442                     mImsRegistrationTerminationLte, mImsRegistrationTerminationWifi
2443                 },
2444                 terminations);
2445     }
2446 
2447     @Test
2448     @SmallTest
addImsRegistrationTermination_updateExistingEntries()2449     public void addImsRegistrationTermination_updateExistingEntries() throws Exception {
2450         createTestFile(START_TIME_MILLIS);
2451         ImsRegistrationTermination newTermination = copyOf(mImsRegistrationTerminationWifi);
2452         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2453 
2454         mPersistAtomsStorage.addImsRegistrationTermination(copyOf(mImsRegistrationTerminationWifi));
2455         mPersistAtomsStorage.incTimeMillis(100L);
2456 
2457         // mImsRegistrationTerminationWifi's count should be doubled
2458         verifyCurrentStateSavedToFileOnce();
2459         ImsRegistrationTermination[] terminations =
2460                 mPersistAtomsStorage.getImsRegistrationTerminations(0L);
2461         newTermination.count *= 2;
2462         assertProtoArrayEqualsIgnoringOrder(
2463                 new ImsRegistrationTermination[] {mImsRegistrationTerminationLte, newTermination},
2464                 terminations);
2465     }
2466 
2467     @Test
2468     @SmallTest
addImsRegistrationTermination_tooManyTerminations()2469     public void addImsRegistrationTermination_tooManyTerminations() throws Exception {
2470         createEmptyTestFile();
2471         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2472         Queue<ImsRegistrationTermination> expectedTerminations = new LinkedList<>();
2473 
2474         // Add 11 registration terminations
2475         for (int i = 0; i < 11; i++) {
2476             ImsRegistrationTermination termination = copyOf(mImsRegistrationTerminationLte);
2477             termination.reasonCode = i;
2478             expectedTerminations.add(termination);
2479             mPersistAtomsStorage.addImsRegistrationTermination(termination);
2480             mPersistAtomsStorage.incTimeMillis(100L);
2481         }
2482 
2483         // The least recent (the first) registration termination should be evicted
2484         verifyCurrentStateSavedToFileOnce();
2485         ImsRegistrationTermination[] terminations =
2486                 mPersistAtomsStorage.getImsRegistrationTerminations(0L);
2487         expectedTerminations.remove();
2488         assertProtoArrayEqualsIgnoringOrder(
2489                 expectedTerminations.toArray(new ImsRegistrationTermination[0]), terminations);
2490     }
2491 
2492     @Test
2493     @SmallTest
getImsRegistrationStats_tooFrequent()2494     public void getImsRegistrationStats_tooFrequent() throws Exception {
2495         createTestFile(START_TIME_MILLIS);
2496 
2497         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2498         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
2499         ImsRegistrationStats[] stats = mPersistAtomsStorage.getImsRegistrationStats(100L);
2500 
2501         // Should be denied
2502         assertNull(stats);
2503     }
2504 
2505     @Test
2506     @SmallTest
getImsRegistrationStats_withSavedAtoms()2507     public void getImsRegistrationStats_withSavedAtoms() throws Exception {
2508         createTestFile(START_TIME_MILLIS);
2509 
2510         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2511         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS);
2512         ImsRegistrationStats[] stats1 = mPersistAtomsStorage.getImsRegistrationStats(50L);
2513         mPersistAtomsStorage.incTimeMillis(100L);
2514         ImsRegistrationStats[] stats2 = mPersistAtomsStorage.getImsRegistrationStats(50L);
2515 
2516         // First set of results should equal to file contents, second should be empty, corresponding
2517         // pull timestamp should be updated and saved
2518         assertProtoArrayEqualsIgnoringOrder(
2519                 new ImsRegistrationStats[] {
2520                     mImsRegStatsUnregisteredLte0,
2521                     mImsRegStatsRegisteringLte0,
2522                     mImsRegistrationStatsLte0,
2523                     mImsRegistrationStatsWifi0,
2524                     mImsRegistrationStatsLte1
2525                 },
2526                 stats1);
2527         assertProtoArrayEquals(new ImsRegistrationStats[0], stats2);
2528         assertEquals(
2529                 START_TIME_MILLIS + DAY_IN_MILLIS + 100L,
2530                 mPersistAtomsStorage.getAtomsProto().imsRegistrationStatsPullTimestampMillis);
2531         InOrder inOrder = inOrder(mTestFileOutputStream);
2532         assertEquals(
2533                 START_TIME_MILLIS + DAY_IN_MILLIS,
2534                 getAtomsWritten(inOrder).imsRegistrationStatsPullTimestampMillis);
2535         assertEquals(
2536                 START_TIME_MILLIS + DAY_IN_MILLIS + 100L,
2537                 getAtomsWritten(inOrder).imsRegistrationStatsPullTimestampMillis);
2538         inOrder.verifyNoMoreInteractions();
2539     }
2540 
2541     @Test
2542     @SmallTest
getImsRegistrationTerminations_tooFrequent()2543     public void getImsRegistrationTerminations_tooFrequent() throws Exception {
2544         createTestFile(START_TIME_MILLIS);
2545 
2546         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2547         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
2548         ImsRegistrationTermination[] terminations =
2549                 mPersistAtomsStorage.getImsRegistrationTerminations(100L);
2550 
2551         // Should be denied
2552         assertNull(terminations);
2553     }
2554 
2555     @Test
2556     @SmallTest
getImsRegistrationTerminations_withSavedAtoms()2557     public void getImsRegistrationTerminations_withSavedAtoms() throws Exception {
2558         createTestFile(START_TIME_MILLIS);
2559 
2560         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2561         mPersistAtomsStorage.incTimeMillis(100L);
2562         ImsRegistrationTermination[] terminations1 =
2563                 mPersistAtomsStorage.getImsRegistrationTerminations(50L);
2564         mPersistAtomsStorage.incTimeMillis(100L);
2565         ImsRegistrationTermination[] terminations2 =
2566                 mPersistAtomsStorage.getImsRegistrationTerminations(50L);
2567 
2568         // First set of results should equal to file contents, second should be empty, corresponding
2569         // pull timestamp should be updated and saved
2570         assertProtoArrayEqualsIgnoringOrder(
2571                 new ImsRegistrationTermination[] {
2572                     mImsRegistrationTerminationLte, mImsRegistrationTerminationWifi
2573                 },
2574                 terminations1);
2575         assertProtoArrayEquals(new ImsRegistrationTermination[0], terminations2);
2576         assertEquals(
2577                 START_TIME_MILLIS + 200L,
2578                 mPersistAtomsStorage.getAtomsProto().imsRegistrationTerminationPullTimestampMillis);
2579         InOrder inOrder = inOrder(mTestFileOutputStream);
2580         assertEquals(
2581                 START_TIME_MILLIS + 100L,
2582                 getAtomsWritten(inOrder).imsRegistrationTerminationPullTimestampMillis);
2583         assertEquals(
2584                 START_TIME_MILLIS + 200L,
2585                 getAtomsWritten(inOrder).imsRegistrationTerminationPullTimestampMillis);
2586         inOrder.verifyNoMoreInteractions();
2587     }
2588 
2589     @Test
2590     @SmallTest
addDataCallSession_newEntry()2591     public void addDataCallSession_newEntry()
2592             throws Exception {
2593         createEmptyTestFile();
2594         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2595 
2596         mPersistAtomsStorage.addDataCallSession(mDataCallSession0);
2597         mPersistAtomsStorage.addDataCallSession(mDataCallSession1);
2598         mPersistAtomsStorage.incTimeMillis(100L);
2599 
2600         // there should be 2 data calls
2601         verifyCurrentStateSavedToFileOnce();
2602         DataCallSession[] dataCalls = mPersistAtomsStorage.getDataCallSessions(0L);
2603         assertProtoArrayEqualsIgnoringOrder(
2604                 new DataCallSession[]{mDataCallSession0, mDataCallSession1},
2605                 dataCalls);
2606         for (DataCallSession dataCallSession : dataCalls) {
2607             if (dataCallSession.dimension == mDataCallSession0.dimension) {
2608                 assertArrayEquals(new int[]{1, 2, 3}, dataCallSession.handoverFailureCauses);
2609                 assertArrayEquals(new int[]{6, 5, 5}, dataCallSession.handoverFailureRat);
2610             }
2611         }
2612     }
2613 
2614     @Test
2615     @SmallTest
addImsRegistrationFeatureTagStats_emptyProto()2616     public void addImsRegistrationFeatureTagStats_emptyProto() throws Exception {
2617         createEmptyTestFile();
2618 
2619         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2620         mPersistAtomsStorage
2621                 .addImsRegistrationFeatureTagStats(mImsRegistrationFeatureTagStats1Proto);
2622         mPersistAtomsStorage.incTimeMillis(100L);
2623 
2624         verifyCurrentStateSavedToFileOnce();
2625 
2626         ImsRegistrationFeatureTagStats[] expected =
2627                 new ImsRegistrationFeatureTagStats[] {
2628                         mImsRegistrationFeatureTagStats1Proto
2629                 };
2630         assertProtoArrayEquals(
2631                 expected, mPersistAtomsStorage.getImsRegistrationFeatureTagStats(0L));
2632     }
2633 
2634     @Test
2635     @SmallTest
addDataCallSession_existingEntry()2636     public void addDataCallSession_existingEntry()
2637             throws Exception {
2638         createEmptyTestFile();
2639         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2640         DataCallSession newDataCallSession0 = copyOf(mDataCallSession0);
2641         newDataCallSession0.ongoing = false;
2642         newDataCallSession0.ratAtEnd = TelephonyManager.NETWORK_TYPE_LTE;
2643         newDataCallSession0.durationMinutes = 10;
2644         newDataCallSession0.ratSwitchCount = 5;
2645         newDataCallSession0.handoverFailureCauses = new int[]{4};
2646         newDataCallSession0.handoverFailureRat = new int[]{4};
2647         DataCallSession totalDataCallSession0 = copyOf(newDataCallSession0);
2648         totalDataCallSession0.durationMinutes =
2649                 mDataCallSession0.durationMinutes + newDataCallSession0.durationMinutes;
2650         totalDataCallSession0.ratSwitchCount =
2651                 mDataCallSession0.ratSwitchCount + newDataCallSession0.ratSwitchCount;
2652         totalDataCallSession0.handoverFailureCauses = new int[]{1, 2, 3, 4};
2653         totalDataCallSession0.handoverFailureRat = new int[]{6, 5, 5, 4};
2654 
2655         mPersistAtomsStorage.addDataCallSession(mDataCallSession0);
2656         mPersistAtomsStorage.addDataCallSession(newDataCallSession0);
2657         mPersistAtomsStorage.incTimeMillis(100L);
2658 
2659         // there should be 1 data call
2660         verifyCurrentStateSavedToFileOnce();
2661         DataCallSession[] dataCalls = mPersistAtomsStorage.getDataCallSessions(0L);
2662         assertProtoArrayEqualsIgnoringOrder(
2663                 new DataCallSession[]{totalDataCallSession0}, dataCalls);
2664     }
2665 
2666     @Test
2667     @SmallTest
addImsRegistrationFeatureTagStats_withExistingEntries()2668     public void addImsRegistrationFeatureTagStats_withExistingEntries() throws Exception {
2669         createEmptyTestFile();
2670 
2671         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2672         mPersistAtomsStorage
2673                 .addImsRegistrationFeatureTagStats(mImsRegistrationFeatureTagStats1Proto);
2674         mPersistAtomsStorage.incTimeMillis(100L);
2675         mPersistAtomsStorage
2676                 .addImsRegistrationFeatureTagStats(mImsRegistrationFeatureTagStats2Proto);
2677         mPersistAtomsStorage.incTimeMillis(100L);
2678 
2679         verifyCurrentStateSavedToFileOnce();
2680 
2681         ImsRegistrationFeatureTagStats[] expected =
2682                 new ImsRegistrationFeatureTagStats[] {
2683                         mImsRegistrationFeatureTagStats1Proto,
2684                         mImsRegistrationFeatureTagStats2Proto
2685                 };
2686         // 2 atoms stored on initially and when try to add 2 same atoms, should be increased.
2687         assertProtoArrayEqualsIgnoringOrder(
2688                 expected, mPersistAtomsStorage.getImsRegistrationFeatureTagStats(0L));
2689     }
2690 
2691     @Test
2692     @SmallTest
addImsRegistrationFeatureTagStats_tooManyEntries()2693     public void addImsRegistrationFeatureTagStats_tooManyEntries() throws Exception {
2694         createEmptyTestFile();
2695 
2696         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2697 
2698         int maxCount = 10;
2699         for (int i = 0; i < maxCount; i++) {
2700             mPersistAtomsStorage
2701                     .addImsRegistrationFeatureTagStats(
2702                             copyOf(mImsRegistrationFeatureTagStats1Proto));
2703             mPersistAtomsStorage.incTimeMillis(100L);
2704         }
2705 
2706         mPersistAtomsStorage
2707                 .addImsRegistrationFeatureTagStats(copyOf(mImsRegistrationFeatureTagStats2Proto));
2708 
2709         verifyCurrentStateSavedToFileOnce();
2710 
2711         ImsRegistrationFeatureTagStats[] result =
2712                 mPersistAtomsStorage.getImsRegistrationFeatureTagStats(0L);
2713 
2714         // tried store 26 statses, but only 2 statses stored
2715         // total time 3600L * maxCount
2716         assertHasStatsCountTime(result, mImsRegistrationFeatureTagStats1Proto, 1,
2717                 maxCount * 3600L);
2718         // total time 3600L * 1
2719         assertHasStatsCountTime(result, mImsRegistrationFeatureTagStats2Proto, 1,
2720                 1 * 3600L);
2721     }
2722 
2723     @Test
2724     @SmallTest
getImsRegistrationFeatureTagStats_tooFrequent()2725     public void getImsRegistrationFeatureTagStats_tooFrequent() throws Exception {
2726         createTestFile(START_TIME_MILLIS);
2727 
2728         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2729         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
2730         ImsRegistrationFeatureTagStats[] result =
2731                 mPersistAtomsStorage.getImsRegistrationFeatureTagStats(100L);
2732 
2733         // Should be denied
2734         assertNull(result);
2735     }
2736 
2737     @Test
2738     @SmallTest
getImsRegistrationFeatureTagStats_withSavedAtoms()2739     public void getImsRegistrationFeatureTagStats_withSavedAtoms() throws Exception {
2740         createTestFile(START_TIME_MILLIS);
2741 
2742         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2743         mPersistAtomsStorage.incTimeMillis(100L);
2744         ImsRegistrationFeatureTagStats[] statses1 =
2745                 mPersistAtomsStorage.getImsRegistrationFeatureTagStats(50L);
2746         mPersistAtomsStorage.incTimeMillis(100L);
2747         ImsRegistrationFeatureTagStats[] statses2 =
2748                 mPersistAtomsStorage.getImsRegistrationFeatureTagStats(50L);
2749 
2750         // First results of get should have two atoms, second should be empty
2751         // pull timestamp should be updated and saved
2752         assertProtoArrayEqualsIgnoringOrder(mImsRegistrationFeatureTagStatses, statses1);
2753         assertProtoArrayEquals(new ImsRegistrationFeatureTagStats[0], statses2);
2754         assertEquals(
2755                 START_TIME_MILLIS + 200L,
2756                 mPersistAtomsStorage.getAtomsProto()
2757                         .imsRegistrationFeatureTagStatsPullTimestampMillis);
2758         InOrder inOrder = inOrder(mTestFileOutputStream);
2759         assertEquals(
2760                 START_TIME_MILLIS + 100L,
2761                 getAtomsWritten(inOrder).imsRegistrationFeatureTagStatsPullTimestampMillis);
2762         assertEquals(
2763                 START_TIME_MILLIS + 200L,
2764                 getAtomsWritten(inOrder).imsRegistrationFeatureTagStatsPullTimestampMillis);
2765         inOrder.verifyNoMoreInteractions();
2766     }
2767 
2768     @Test
2769     @SmallTest
addRcsClientProvisioningStats_emptyProto()2770     public void addRcsClientProvisioningStats_emptyProto() throws Exception {
2771         createEmptyTestFile();
2772 
2773         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2774         mPersistAtomsStorage
2775                 .addRcsClientProvisioningStats(mRcsClientProvisioningStats1Proto);
2776         mPersistAtomsStorage.incTimeMillis(100L);
2777         mPersistAtomsStorage
2778                 .addRcsClientProvisioningStats(mRcsClientProvisioningStats2Proto);
2779         mPersistAtomsStorage.incTimeMillis(100L);
2780 
2781         verifyCurrentStateSavedToFileOnce();
2782 
2783         RcsClientProvisioningStats[] expected =
2784                 new RcsClientProvisioningStats[] {
2785                         mRcsClientProvisioningStats1Proto,
2786                         mRcsClientProvisioningStats2Proto
2787                 };
2788 
2789         assertProtoArrayEqualsIgnoringOrder(
2790                 expected, mPersistAtomsStorage.getRcsClientProvisioningStats(0L));
2791     }
2792 
2793     @Test
2794     @SmallTest
addRcsClientProvisioningStats_tooManyEntries()2795     public void addRcsClientProvisioningStats_tooManyEntries() throws Exception {
2796         createEmptyTestFile();
2797 
2798         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2799 
2800         // Store 11 same atoms, but only 1 atoms stored with count 11
2801         for (int i = 0; i < 11; i++) {
2802             mPersistAtomsStorage
2803                     .addRcsClientProvisioningStats(mRcsClientProvisioningStats1Proto);
2804             mPersistAtomsStorage.incTimeMillis(100L);
2805         }
2806         // Store 1 different atom and count 1
2807         mPersistAtomsStorage
2808                 .addRcsClientProvisioningStats(mRcsClientProvisioningStats2Proto);
2809 
2810         verifyCurrentStateSavedToFileOnce();
2811 
2812         RcsClientProvisioningStats[] result =
2813                 mPersistAtomsStorage.getRcsClientProvisioningStats(0L);
2814 
2815         // First atom has count 11, the other has 1
2816         assertHasStatsAndCount(result, mRcsClientProvisioningStats1Proto, 11);
2817         assertHasStatsAndCount(result, mRcsClientProvisioningStats2Proto, 1);
2818     }
2819 
2820     @Test
2821     @SmallTest
getRcsClientProvisioningStats_tooFrequent()2822     public void getRcsClientProvisioningStats_tooFrequent() throws Exception {
2823         createTestFile(START_TIME_MILLIS);
2824 
2825         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2826         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
2827         RcsClientProvisioningStats[] result =
2828                 mPersistAtomsStorage.getRcsClientProvisioningStats(100L);
2829 
2830         // Should be denied
2831         assertNull(result);
2832     }
2833 
2834     @Test
2835     @SmallTest
getRcsClientProvisioningStats_withSavedAtoms()2836     public void getRcsClientProvisioningStats_withSavedAtoms() throws Exception {
2837         createTestFile(START_TIME_MILLIS);
2838 
2839         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2840         mPersistAtomsStorage.incTimeMillis(100L);
2841         RcsClientProvisioningStats[] statses1 =
2842                 mPersistAtomsStorage.getRcsClientProvisioningStats(50L);
2843         mPersistAtomsStorage.incTimeMillis(100L);
2844         RcsClientProvisioningStats[] statses2 =
2845                 mPersistAtomsStorage.getRcsClientProvisioningStats(50L);
2846 
2847         // First results of get should have two atoms, second should be empty
2848         // pull timestamp should be updated and saved
2849         assertProtoArrayEqualsIgnoringOrder(mRcsClientProvisioningStatses, statses1);
2850         assertProtoArrayEquals(new RcsClientProvisioningStats[0], statses2);
2851         assertEquals(
2852                 START_TIME_MILLIS + 200L,
2853                 mPersistAtomsStorage.getAtomsProto()
2854                         .rcsClientProvisioningStatsPullTimestampMillis);
2855         InOrder inOrder = inOrder(mTestFileOutputStream);
2856         assertEquals(
2857                 START_TIME_MILLIS + 100L,
2858                 getAtomsWritten(inOrder).rcsClientProvisioningStatsPullTimestampMillis);
2859         assertEquals(
2860                 START_TIME_MILLIS + 200L,
2861                 getAtomsWritten(inOrder).rcsClientProvisioningStatsPullTimestampMillis);
2862         inOrder.verifyNoMoreInteractions();
2863     }
2864 
2865     @Test
2866     @SmallTest
addRcsAcsProvisioningStats_emptyProto()2867     public void addRcsAcsProvisioningStats_emptyProto() throws Exception {
2868         createEmptyTestFile();
2869 
2870         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2871         mPersistAtomsStorage
2872                 .addRcsAcsProvisioningStats(mRcsAcsProvisioningStats1Proto);
2873         mPersistAtomsStorage.incTimeMillis(100L);
2874         mPersistAtomsStorage
2875                 .addRcsAcsProvisioningStats(mRcsAcsProvisioningStats2Proto);
2876         mPersistAtomsStorage.incTimeMillis(100L);
2877 
2878         verifyCurrentStateSavedToFileOnce();
2879 
2880         RcsAcsProvisioningStats[] expected =
2881                 new RcsAcsProvisioningStats[] {
2882                         mRcsAcsProvisioningStats1Proto,
2883                         mRcsAcsProvisioningStats2Proto
2884                 };
2885 
2886         assertProtoArrayEqualsIgnoringOrder(
2887                 expected, mPersistAtomsStorage.getRcsAcsProvisioningStats(0L));
2888     }
2889 
2890     @Test
2891     @SmallTest
addRcsAcsProvisioningStats_updateExistingEntries()2892     public void addRcsAcsProvisioningStats_updateExistingEntries() throws Exception {
2893         final int maxCount = 5;
2894         final long duration = START_TIME_MILLIS;
2895         createEmptyTestFile();
2896 
2897         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2898 
2899         // Store 5 same atoms (1Proto), but only 1 atoms stored with count 5, total time 2000L * 5
2900         // Store 5 same atoms (2Proto), but only 1 atoms stored with count 5, total time 2000L * 5
2901 
2902         for (int i = 0; i < maxCount; i++) {
2903             mPersistAtomsStorage
2904                     .addRcsAcsProvisioningStats(copyOf(mRcsAcsProvisioningStats1Proto));
2905             mPersistAtomsStorage.incTimeMillis(100L);
2906             mPersistAtomsStorage
2907                     .addRcsAcsProvisioningStats(copyOf(mRcsAcsProvisioningStats2Proto));
2908             mPersistAtomsStorage.incTimeMillis(100L);
2909         }
2910         // add one more atoms (2Proto), count 6, total time 2000L * 6
2911         mPersistAtomsStorage
2912                 .addRcsAcsProvisioningStats(copyOf(mRcsAcsProvisioningStats2Proto));
2913         mPersistAtomsStorage.incTimeMillis(100L);
2914 
2915         verifyCurrentStateSavedToFileOnce();
2916 
2917         RcsAcsProvisioningStats[] result =
2918                 mPersistAtomsStorage.getRcsAcsProvisioningStats(0L);
2919 
2920         // atom (1Proto) : count = 5, time = 2000L * 5
2921         assertHasStatsAndCountDuration(
2922                 result, mRcsAcsProvisioningStats1Proto, 5, duration * maxCount);
2923         // atom (2Proto) : count = 6, time = 2000L * 6
2924         assertHasStatsAndCountDuration(
2925                 result, mRcsAcsProvisioningStats2Proto, 6, duration * (maxCount + 1));
2926     }
2927 
2928     @Test
2929     @SmallTest
getRcsAcsProvisioningStats_tooFrequent()2930     public void getRcsAcsProvisioningStats_tooFrequent() throws Exception {
2931         createTestFile(START_TIME_MILLIS);
2932 
2933         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2934         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
2935         RcsAcsProvisioningStats[] result =
2936                 mPersistAtomsStorage.getRcsAcsProvisioningStats(100L);
2937 
2938         // Should be denied
2939         assertNull(result);
2940     }
2941 
2942     @Test
2943     @SmallTest
getRcsAcstProvisioningStats_withSavedAtoms()2944     public void getRcsAcstProvisioningStats_withSavedAtoms() throws Exception {
2945         createTestFile(START_TIME_MILLIS);
2946 
2947         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2948         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS);
2949         RcsAcsProvisioningStats[] statses1 =
2950                 mPersistAtomsStorage.getRcsAcsProvisioningStats(DAY_IN_MILLIS - HOUR_IN_MILLIS);
2951         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS);
2952         RcsAcsProvisioningStats[] statses2 =
2953                 mPersistAtomsStorage.getRcsAcsProvisioningStats(DAY_IN_MILLIS - HOUR_IN_MILLIS);
2954 
2955         // First results of get should have two atoms, second should be empty
2956         // pull timestamp should be updated and saved
2957         assertProtoArrayEqualsIgnoringOrder(mRcsAcsProvisioningStatses, statses1);
2958         assertProtoArrayEquals(new RcsAcsProvisioningStats[0], statses2);
2959         assertEquals(
2960                 START_TIME_MILLIS + 2 * DAY_IN_MILLIS,
2961                 mPersistAtomsStorage.getAtomsProto()
2962                         .rcsAcsProvisioningStatsPullTimestampMillis);
2963         InOrder inOrder = inOrder(mTestFileOutputStream);
2964         assertEquals(
2965                 START_TIME_MILLIS + DAY_IN_MILLIS,
2966                 getAtomsWritten(inOrder).rcsAcsProvisioningStatsPullTimestampMillis);
2967         assertEquals(
2968                 START_TIME_MILLIS + 2 * DAY_IN_MILLIS,
2969                 getAtomsWritten(inOrder).rcsAcsProvisioningStatsPullTimestampMillis);
2970         inOrder.verifyNoMoreInteractions();
2971     }
2972 
2973     @Test
2974     @SmallTest
addImsRegistrationServiceDescStats_emptyProto()2975     public void addImsRegistrationServiceDescStats_emptyProto() throws Exception {
2976         createEmptyTestFile();
2977         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2978         mPersistAtomsStorage.addImsRegistrationServiceDescStats(mImsRegistrationServiceIm);
2979         mPersistAtomsStorage.incTimeMillis(100L);
2980 
2981         // Service state and service switch should be added successfully
2982         verifyCurrentStateSavedToFileOnce();
2983         ImsRegistrationServiceDescStats[] outputs =
2984             mPersistAtomsStorage.getImsRegistrationServiceDescStats(0L);
2985         assertProtoArrayEquals(
2986                 new ImsRegistrationServiceDescStats[] {mImsRegistrationServiceIm}, outputs);
2987     }
2988 
2989     @Test
2990     @SmallTest
addImsRegistrationServiceDescStats_withExistingEntries()2991     public void addImsRegistrationServiceDescStats_withExistingEntries() throws Exception {
2992         createEmptyTestFile();
2993         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
2994         mPersistAtomsStorage.addImsRegistrationServiceDescStats(mImsRegistrationServiceIm);
2995 
2996         mPersistAtomsStorage.addImsRegistrationServiceDescStats(mImsRegistrationServiceFt);
2997         mPersistAtomsStorage.incTimeMillis(100L);
2998 
2999         // Service state and service switch should be added successfully
3000         verifyCurrentStateSavedToFileOnce();
3001         ImsRegistrationServiceDescStats[] output =
3002             mPersistAtomsStorage.getImsRegistrationServiceDescStats(0L);
3003         assertProtoArrayEqualsIgnoringOrder(
3004                 new ImsRegistrationServiceDescStats[] {
3005                     mImsRegistrationServiceIm,
3006                     mImsRegistrationServiceFt
3007                 },
3008                 output);
3009     }
3010 
3011     @Test
3012     @SmallTest
addImsRegistrationServiceDescStats_tooManyServiceDesc()3013     public void addImsRegistrationServiceDescStats_tooManyServiceDesc() throws Exception {
3014         createEmptyTestFile();
3015         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3016 
3017         Queue<ImsRegistrationServiceDescStats> expectedOutput = new LinkedList<>();
3018         // Add 101 registration terminations
3019         for (int i = 0; i < 26 + 1; i++) {
3020             ImsRegistrationServiceDescStats stats = copyOf(mImsRegistrationServiceIm);
3021             stats.registrationTech = i;
3022             expectedOutput.add(stats);
3023             mPersistAtomsStorage.addImsRegistrationServiceDescStats(stats);
3024             mPersistAtomsStorage.incTimeMillis(100L);
3025         }
3026         mPersistAtomsStorage.incTimeMillis(100L);
3027 
3028         // The least recent (the first) registration termination should be evicted
3029         verifyCurrentStateSavedToFileOnce();
3030         ImsRegistrationServiceDescStats[] output =
3031             mPersistAtomsStorage.getImsRegistrationServiceDescStats(0L);
3032         expectedOutput.remove();
3033         assertEquals(expectedOutput.size() - 1, output.length);
3034     }
3035 
3036     @Test
3037     @SmallTest
getImsRegistrationServiceDescStats_tooFrequent()3038     public void getImsRegistrationServiceDescStats_tooFrequent() throws Exception {
3039         createTestFile(START_TIME_MILLIS);
3040 
3041         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3042         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
3043         ImsRegistrationServiceDescStats[] output =
3044             mPersistAtomsStorage.getImsRegistrationServiceDescStats(100L);
3045 
3046         // Should be denied
3047         assertNull(output);
3048     }
3049 
3050     @Test
3051     @SmallTest
getImsRegistrationServiceDescStats_withSavedAtoms()3052     public void getImsRegistrationServiceDescStats_withSavedAtoms() throws Exception {
3053         createTestFile(START_TIME_MILLIS);
3054 
3055         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3056         mPersistAtomsStorage.incTimeMillis(100L);
3057         ImsRegistrationServiceDescStats[] output1 =
3058             mPersistAtomsStorage.getImsRegistrationServiceDescStats(50L);
3059         mPersistAtomsStorage.incTimeMillis(100L);
3060         ImsRegistrationServiceDescStats[] output2 =
3061             mPersistAtomsStorage.getImsRegistrationServiceDescStats(50L);
3062 
3063         // First set of results should equal to file contents, second should be empty, corresponding
3064         // pull timestamp should be updated and saved
3065         assertProtoArrayEqualsIgnoringOrder(
3066                 new ImsRegistrationServiceDescStats[] {
3067                     mImsRegistrationServiceIm,
3068                     mImsRegistrationServiceFt
3069                 },
3070                 output1);
3071         assertProtoArrayEquals(new ImsRegistrationServiceDescStats[0], output2);
3072         assertEquals(
3073                 START_TIME_MILLIS + 200L,
3074                 mPersistAtomsStorage.getAtomsProto()
3075                     .imsRegistrationServiceDescStatsPullTimestampMillis);
3076         InOrder inOrder = inOrder(mTestFileOutputStream);
3077         assertEquals(
3078                 START_TIME_MILLIS + 100L,
3079                 getAtomsWritten(inOrder).imsRegistrationServiceDescStatsPullTimestampMillis);
3080         assertEquals(
3081                 START_TIME_MILLIS + 200L,
3082                 getAtomsWritten(inOrder).imsRegistrationServiceDescStatsPullTimestampMillis);
3083         inOrder.verifyNoMoreInteractions();
3084     }
3085 
3086     @Test
3087     @SmallTest
getImsRegistrationStats_24hNormalization()3088     public void getImsRegistrationStats_24hNormalization() throws Exception {
3089         createEmptyTestFile();
3090         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3091         mPersistAtomsStorage.addImsRegistrationStats(copyOf(mImsRegistrationStatsWifi0));
3092         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS / 2);
3093 
3094         ImsRegistrationStats[] serviceStates = mPersistAtomsStorage.getImsRegistrationStats(0L);
3095         mImsRegistrationStatsWifi0.registeredMillis *= 2;
3096         mImsRegistrationStatsWifi0.voiceCapableMillis *= 2;
3097         mImsRegistrationStatsWifi0.voiceAvailableMillis *= 2;
3098         mImsRegistrationStatsWifi0.smsCapableMillis *= 2;
3099         mImsRegistrationStatsWifi0.smsAvailableMillis *= 2;
3100         mImsRegistrationStatsWifi0.videoCapableMillis *= 2;
3101         mImsRegistrationStatsWifi0.videoAvailableMillis *= 2;
3102         mImsRegistrationStatsWifi0.utCapableMillis *= 2;
3103         mImsRegistrationStatsWifi0.utAvailableMillis *= 2;
3104         assertProtoArrayEqualsIgnoringOrder(
3105                 new ImsRegistrationStats[] {
3106                     mImsRegistrationStatsWifi0
3107                 },
3108                 serviceStates);
3109     }
3110 
3111     @Test
getRcsAcsProvisioningStats_24h_normalization()3112     public void getRcsAcsProvisioningStats_24h_normalization() throws Exception {
3113         // in case pulling interval is greater than a day
3114         final long stateTimer = HOUR_IN_MILLIS;
3115         final long weightFactor = 2;
3116         createTestFile(START_TIME_MILLIS);
3117         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3118         RcsAcsProvisioningStats mSubjectStats = copyOf(mRcsAcsProvisioningStats1Proto);
3119 
3120         mSubjectStats.stateTimerMillis = stateTimer;
3121         mPersistAtomsStorage.addRcsAcsProvisioningStats(mSubjectStats);
3122         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS * weightFactor);
3123 
3124         RcsAcsProvisioningStats[] savedStats =
3125                 mPersistAtomsStorage.getRcsAcsProvisioningStats(0L);
3126 
3127         assertEquals(
3128                 (START_TIME_MILLIS + stateTimer) / weightFactor, savedStats[0].stateTimerMillis);
3129 
3130         // in case pulling interval is smaller than a day
3131         long incTimeMillis = DAY_IN_MILLIS * 23 / 24 + 1;
3132         mSubjectStats = copyOf(mRcsAcsProvisioningStats1Proto);
3133         mSubjectStats.stateTimerMillis = stateTimer;
3134         mPersistAtomsStorage.addRcsAcsProvisioningStats(mSubjectStats);
3135         mPersistAtomsStorage.incTimeMillis(incTimeMillis);
3136         savedStats =
3137                 mPersistAtomsStorage.getRcsAcsProvisioningStats(0L);
3138 
3139 
3140         assertEquals(stateTimer, savedStats[0].stateTimerMillis);
3141     }
3142 
3143     @Test
getSipDelegateStats_24h_normalization()3144     public void getSipDelegateStats_24h_normalization() throws Exception {
3145         final long stateTimer = HOUR_IN_MILLIS;
3146         final long weightFactor = 2;
3147         createTestFile(START_TIME_MILLIS);
3148         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3149         SipDelegateStats mSubjectStats = copyOf(mSipDelegateStats1);
3150         mSubjectStats.uptimeMillis = stateTimer;
3151         mPersistAtomsStorage.addSipDelegateStats(mSubjectStats);
3152         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS * weightFactor);
3153         SipDelegateStats[] savedStats =
3154                 mPersistAtomsStorage.getSipDelegateStats(0L);
3155         for (SipDelegateStats stat : savedStats) {
3156             if (stat.destroyReason
3157                     == SipDelegateManager.SIP_DELEGATE_DESTROY_REASON_REQUESTED_BY_APP) {
3158                 assertEquals(stateTimer / weightFactor, stat.uptimeMillis);
3159             }
3160         }
3161 
3162         long incTimeMillis = DAY_IN_MILLIS * 23 / 24 + 1;
3163         mSubjectStats = copyOf(mSipDelegateStats1);
3164         mSubjectStats.uptimeMillis = stateTimer;
3165         mPersistAtomsStorage.addSipDelegateStats(mSubjectStats);
3166         mPersistAtomsStorage.incTimeMillis(incTimeMillis);
3167         savedStats =
3168                 mPersistAtomsStorage.getSipDelegateStats(0L);
3169         for (SipDelegateStats stat : savedStats) {
3170             if (stat.destroyReason
3171                     == SipDelegateManager.SIP_DELEGATE_DESTROY_REASON_REQUESTED_BY_APP) {
3172                 assertEquals(stateTimer, stat.uptimeMillis);
3173             }
3174         }
3175     }
3176 
3177     @Test
getSipTransportFeatureTagStats_24h_normalization()3178     public void getSipTransportFeatureTagStats_24h_normalization() throws Exception {
3179         final long stateTimer = HOUR_IN_MILLIS;
3180         final long weightFactor = 2;
3181         createTestFile(START_TIME_MILLIS);
3182         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3183         SipTransportFeatureTagStats mSubjectStats = copyOf(mSipTransportFeatureTagStats1);
3184         mSubjectStats.associatedMillis = stateTimer;
3185         mPersistAtomsStorage.addSipTransportFeatureTagStats(mSubjectStats);
3186         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS * weightFactor);
3187         SipTransportFeatureTagStats[] savedStats =
3188                 mPersistAtomsStorage.getSipTransportFeatureTagStats(0L);
3189         assertEquals((stateTimer) / weightFactor, savedStats[0].associatedMillis);
3190 
3191         long incTimeMillis = DAY_IN_MILLIS * 23 / 24 + 1;
3192         mSubjectStats = copyOf(mSipTransportFeatureTagStats1);
3193         mSubjectStats.associatedMillis = stateTimer;
3194         mPersistAtomsStorage.addSipTransportFeatureTagStats(mSubjectStats);
3195         mPersistAtomsStorage.incTimeMillis(incTimeMillis);
3196         savedStats =
3197                 mPersistAtomsStorage.getSipTransportFeatureTagStats(0L);
3198         assertEquals(stateTimer, savedStats[0].associatedMillis);
3199     }
3200 
3201     @Test
getImsRegistrationServiceDescStats_24h_normalization()3202     public void getImsRegistrationServiceDescStats_24h_normalization() throws Exception {
3203         final long stateTimer = HOUR_IN_MILLIS;
3204         final long weightFactor = 2;
3205         createTestFile(START_TIME_MILLIS);
3206         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3207         ImsRegistrationServiceDescStats mSubjectStats = copyOf(mImsRegistrationServiceIm);
3208         mSubjectStats.publishedMillis = stateTimer;
3209         mPersistAtomsStorage.addImsRegistrationServiceDescStats(mSubjectStats);
3210         mPersistAtomsStorage.incTimeMillis(DAY_IN_MILLIS * weightFactor);
3211         ImsRegistrationServiceDescStats[] savedStats =
3212                 mPersistAtomsStorage.getImsRegistrationServiceDescStats(0L);
3213         assertEquals(
3214                 (START_TIME_MILLIS + stateTimer) / weightFactor, savedStats[0].publishedMillis);
3215 
3216         long incTimeMillis = DAY_IN_MILLIS * 23 / 24 + 1;
3217         mSubjectStats = copyOf(mImsRegistrationServiceIm);
3218         mSubjectStats.publishedMillis = stateTimer;
3219         mPersistAtomsStorage.addImsRegistrationServiceDescStats(mSubjectStats);
3220         mPersistAtomsStorage.incTimeMillis(incTimeMillis);
3221         savedStats =
3222                 mPersistAtomsStorage.getImsRegistrationServiceDescStats(0L);
3223         assertEquals(stateTimer, savedStats[0].publishedMillis);
3224     }
3225 
3226     @Test
3227     @SmallTest
addImsDedicatedBearerListenerEvent_emptyProto()3228     public void addImsDedicatedBearerListenerEvent_emptyProto() throws Exception {
3229         createEmptyTestFile();
3230         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3231         mPersistAtomsStorage.addImsDedicatedBearerListenerEvent(mImsDedicatedBearerListenerEvent1);
3232         mPersistAtomsStorage.incTimeMillis(100L);
3233 
3234         // Service state and service switch should be added successfully
3235         verifyCurrentStateSavedToFileOnce();
3236         ImsDedicatedBearerListenerEvent[] outputs =
3237                 mPersistAtomsStorage.getImsDedicatedBearerListenerEvent(0L);
3238         assertProtoArrayEquals(
3239                 new ImsDedicatedBearerListenerEvent[] {mImsDedicatedBearerListenerEvent1}, outputs);
3240     }
3241 
3242     @Test
3243     @SmallTest
addImsDedicatedBearerListenerEvent_withExistingEntries()3244     public void addImsDedicatedBearerListenerEvent_withExistingEntries() throws Exception {
3245         createEmptyTestFile();
3246         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3247         mPersistAtomsStorage.addImsDedicatedBearerListenerEvent(mImsDedicatedBearerListenerEvent1);
3248 
3249         mPersistAtomsStorage.addImsDedicatedBearerListenerEvent(mImsDedicatedBearerListenerEvent2);
3250         mPersistAtomsStorage.incTimeMillis(100L);
3251 
3252         // Service state and service switch should be added successfully
3253         verifyCurrentStateSavedToFileOnce();
3254         ImsDedicatedBearerListenerEvent[] output =
3255                 mPersistAtomsStorage.getImsDedicatedBearerListenerEvent(0L);
3256         assertProtoArrayEqualsIgnoringOrder(
3257                 new ImsDedicatedBearerListenerEvent[] {
3258                     mImsDedicatedBearerListenerEvent1, mImsDedicatedBearerListenerEvent2}, output);
3259     }
3260 
3261     @Test
3262     @SmallTest
addImsDedicatedBearerListenerEvent_withSameProto()3263     public void addImsDedicatedBearerListenerEvent_withSameProto() throws Exception {
3264         createEmptyTestFile();
3265         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3266 
3267         mPersistAtomsStorage.addImsDedicatedBearerListenerEvent(mImsDedicatedBearerListenerEvent1);
3268         mPersistAtomsStorage.addImsDedicatedBearerListenerEvent(mImsDedicatedBearerListenerEvent1);
3269         mPersistAtomsStorage.incTimeMillis(100L);
3270 
3271         // The least recent (the first) registration termination should be evicted
3272         verifyCurrentStateSavedToFileOnce();
3273         ImsDedicatedBearerListenerEvent[] output =
3274                 mPersistAtomsStorage.getImsDedicatedBearerListenerEvent(0L);
3275         assertEquals(mImsDedicatedBearerListenerEvent1.carrierId, output[0].carrierId);
3276         assertEquals(mImsDedicatedBearerListenerEvent1.slotId, output[0].slotId);
3277         assertEquals(mImsDedicatedBearerListenerEvent1.ratAtEnd, output[0].ratAtEnd);
3278         assertEquals(mImsDedicatedBearerListenerEvent1.qci, output[0].qci);
3279         assertEquals(mImsDedicatedBearerListenerEvent1.dedicatedBearerEstablished,
3280                 output[0].dedicatedBearerEstablished);
3281         assertEquals(mImsDedicatedBearerListenerEvent1.eventCount,
3282                 output[0].eventCount);
3283     }
3284 
3285     @Test
3286     @SmallTest
addImsDedicatedBearerEvent_emptyProto()3287     public void addImsDedicatedBearerEvent_emptyProto() throws Exception {
3288         createEmptyTestFile();
3289         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3290         mPersistAtomsStorage.addImsDedicatedBearerEvent(mImsDedicatedBearerEvent1);
3291         mPersistAtomsStorage.incTimeMillis(100L);
3292 
3293         // Service state and service switch should be added successfully
3294         verifyCurrentStateSavedToFileOnce();
3295         ImsDedicatedBearerEvent[] outputs =
3296             mPersistAtomsStorage.getImsDedicatedBearerEvent(0L);
3297         assertProtoArrayEquals(
3298                 new ImsDedicatedBearerEvent[] {mImsDedicatedBearerEvent1}, outputs);
3299     }
3300 
3301     @Test
3302     @SmallTest
addImsDedicatedBearerEvent_withExistingEntries()3303     public void addImsDedicatedBearerEvent_withExistingEntries() throws Exception {
3304         createEmptyTestFile();
3305         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3306         mPersistAtomsStorage.addImsDedicatedBearerEvent(mImsDedicatedBearerEvent1);
3307 
3308         mPersistAtomsStorage.addImsDedicatedBearerEvent(mImsDedicatedBearerEvent2);
3309         mPersistAtomsStorage.incTimeMillis(100L);
3310 
3311         // Service state and service switch should be added successfully
3312         verifyCurrentStateSavedToFileOnce();
3313         ImsDedicatedBearerEvent[] output =
3314             mPersistAtomsStorage.getImsDedicatedBearerEvent(0L);
3315         assertProtoArrayEqualsIgnoringOrder(
3316                     new ImsDedicatedBearerEvent[] {
3317                         mImsDedicatedBearerEvent1, mImsDedicatedBearerEvent2}, output);
3318     }
3319 
3320     @Test
3321     @SmallTest
addImsDedicatedBearerEvent_withSameProto()3322     public void addImsDedicatedBearerEvent_withSameProto() throws Exception {
3323         createEmptyTestFile();
3324         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3325 
3326         mPersistAtomsStorage.addImsDedicatedBearerEvent(mImsDedicatedBearerEvent1);
3327         mPersistAtomsStorage.addImsDedicatedBearerEvent(mImsDedicatedBearerEvent1);
3328         mPersistAtomsStorage.incTimeMillis(100L);
3329 
3330         // The least recent (the first) registration termination should be evicted
3331         verifyCurrentStateSavedToFileOnce();
3332         ImsDedicatedBearerEvent[] output =
3333             mPersistAtomsStorage.getImsDedicatedBearerEvent(0L);
3334         assertEquals(mImsDedicatedBearerEvent1.carrierId, output[0].carrierId);
3335         assertEquals(mImsDedicatedBearerEvent1.slotId, output[0].slotId);
3336         assertEquals(mImsDedicatedBearerEvent1.ratAtEnd, output[0].ratAtEnd);
3337         assertEquals(mImsDedicatedBearerEvent1.qci, output[0].qci);
3338         assertEquals(mImsDedicatedBearerEvent1.localConnectionInfoReceived,
3339                 output[0].localConnectionInfoReceived);
3340         assertEquals(mImsDedicatedBearerEvent1.remoteConnectionInfoReceived,
3341                 output[0].remoteConnectionInfoReceived);
3342         assertEquals(mImsDedicatedBearerEvent1.hasListeners,
3343                 output[0].hasListeners);
3344     }
3345 
3346     @Test
3347     @SmallTest
addImsDedicatedBearerEvent_tooManyEntries()3348     public void addImsDedicatedBearerEvent_tooManyEntries() throws Exception {
3349         createEmptyTestFile();
3350 
3351         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3352 
3353         // Add 11 stats, but max is 10
3354         for (int i = 0; i < 11; i++) {
3355             mPersistAtomsStorage.addImsDedicatedBearerEvent(mImsDedicatedBearerEvent1);
3356             mPersistAtomsStorage.incTimeMillis(100L);
3357         }
3358         mPersistAtomsStorage.addImsDedicatedBearerEvent(mImsDedicatedBearerEvent2);
3359 
3360         verifyCurrentStateSavedToFileOnce();
3361         ImsDedicatedBearerEvent[] stats =
3362             mPersistAtomsStorage.getImsDedicatedBearerEvent(0L);
3363         assertHasStatsAndCount(stats, mImsDedicatedBearerEvent1, 11);
3364         assertHasStatsAndCount(stats, mImsDedicatedBearerEvent2, 1);
3365     }
3366 
3367     @Test
3368     @SmallTest
addImsDedicatedBearerEvent_updateExistingEntries()3369     public void addImsDedicatedBearerEvent_updateExistingEntries() throws Exception {
3370         createTestFile(START_TIME_MILLIS);
3371         ImsDedicatedBearerEvent newStats = copyOf(mImsDedicatedBearerEvent1);
3372         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3373 
3374         mPersistAtomsStorage.addImsDedicatedBearerEvent(copyOf(mImsDedicatedBearerEvent1));
3375         mPersistAtomsStorage.incTimeMillis(100L);
3376 
3377         // mImsDedicatedBearerEvent1's count should be doubled
3378         verifyCurrentStateSavedToFileOnce();
3379         ImsDedicatedBearerEvent[] stats =
3380             mPersistAtomsStorage.getImsDedicatedBearerEvent(0L);
3381         newStats.count *= 2;
3382         assertProtoArrayEqualsIgnoringOrder(new ImsDedicatedBearerEvent[] {
3383                 mImsDedicatedBearerEvent2, newStats}, stats);
3384     }
3385 
3386     @Test
3387     @SmallTest
addUceEventStats_emptyProto()3388     public void addUceEventStats_emptyProto() throws Exception {
3389         createEmptyTestFile();
3390         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3391         mPersistAtomsStorage.addUceEventStats(mUceEventStats1);
3392         mPersistAtomsStorage.incTimeMillis(100L);
3393 
3394         // Service state and service switch should be added successfully
3395         verifyCurrentStateSavedToFileOnce();
3396         UceEventStats[] outputs = mPersistAtomsStorage.getUceEventStats(0L);
3397         assertProtoArrayEquals(
3398                 new UceEventStats[] {mUceEventStats1}, outputs);
3399     }
3400 
3401     @Test
3402     @SmallTest
addUceEventStats_withExistingEntries()3403     public void addUceEventStats_withExistingEntries() throws Exception {
3404         createEmptyTestFile();
3405         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3406         mPersistAtomsStorage.addUceEventStats(mUceEventStats1);
3407 
3408         mPersistAtomsStorage.addUceEventStats(mUceEventStats2);
3409         mPersistAtomsStorage.incTimeMillis(100L);
3410 
3411         // Service state and service switch should be added successfully
3412         verifyCurrentStateSavedToFileOnce();
3413         UceEventStats[] output = mPersistAtomsStorage.getUceEventStats(0L);
3414         assertProtoArrayEqualsIgnoringOrder(
3415                 new UceEventStats[] {mUceEventStats1, mUceEventStats2}, output);
3416     }
3417 
3418     @Test
3419     @SmallTest
addUceEventStats_withSameProto()3420     public void addUceEventStats_withSameProto() throws Exception {
3421         createEmptyTestFile();
3422         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3423 
3424         mPersistAtomsStorage.addUceEventStats(mUceEventStats1);
3425         mPersistAtomsStorage.addUceEventStats(mUceEventStats1);
3426         mPersistAtomsStorage.incTimeMillis(100L);
3427 
3428         // The least recent (the first) registration termination should be evicted
3429         verifyCurrentStateSavedToFileOnce();
3430         UceEventStats[] output = mPersistAtomsStorage.getUceEventStats(0L);
3431         assertEquals(mUceEventStats1.carrierId, output[0].carrierId);
3432         assertEquals(mUceEventStats1.slotId, output[0].slotId);
3433         assertEquals(mUceEventStats1.type, output[0].type);
3434         assertEquals(mUceEventStats1.successful, output[0].successful);
3435         assertEquals(mUceEventStats1.commandCode, output[0].commandCode);
3436         assertEquals(mUceEventStats1.networkResponse, output[0].networkResponse);
3437         assertEquals(2, output[0].count);
3438     }
3439 
3440     @Test
3441     @SmallTest
addPresenceNotifyEvent_withSameProto()3442     public void addPresenceNotifyEvent_withSameProto() throws Exception {
3443         createEmptyTestFile();
3444         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3445 
3446         PresenceNotifyEvent event1 = new PresenceNotifyEvent();
3447         event1.carrierId = CARRIER1_ID;
3448         event1.slotId = SLOT_ID1;
3449         event1.reason = 1;
3450         event1.contentBodyReceived = true;
3451         event1.rcsCapsCount = 1;
3452         event1.mmtelCapsCount = 1;
3453         event1.noCapsCount = 0;
3454         event1.count = 1;
3455 
3456         PresenceNotifyEvent event2 = copyOf(event1);
3457         event2.rcsCapsCount = 0;
3458 
3459         mPersistAtomsStorage.addPresenceNotifyEvent(event1);
3460         mPersistAtomsStorage.addPresenceNotifyEvent(event2);
3461 
3462         mPersistAtomsStorage.incTimeMillis(100L);
3463 
3464         // The least recent (the first) registration termination should be evicted
3465         verifyCurrentStateSavedToFileOnce();
3466         PresenceNotifyEvent[] output = mPersistAtomsStorage.getPresenceNotifyEvent(0L);
3467 
3468         assertEquals(event1.carrierId, output[0].carrierId);
3469         assertEquals(event1.slotId, output[0].slotId);
3470         assertEquals(event1.contentBodyReceived, output[0].contentBodyReceived);
3471         assertEquals(1, output[0].rcsCapsCount);
3472         assertEquals(2, output[0].mmtelCapsCount);
3473         assertEquals(2, output[0].count);
3474 
3475     }
3476     @Test
3477     @SmallTest
addPresenceNotifyEvent_withExistingEntries()3478     public void addPresenceNotifyEvent_withExistingEntries() throws Exception {
3479         createEmptyTestFile();
3480         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3481         mPersistAtomsStorage.addPresenceNotifyEvent(mPresenceNotifyEvent1);
3482         mPersistAtomsStorage.addPresenceNotifyEvent(mPresenceNotifyEvent2);
3483         mPersistAtomsStorage.incTimeMillis(100L);
3484 
3485         // Service state and service switch should be added successfully
3486         verifyCurrentStateSavedToFileOnce();
3487         PresenceNotifyEvent[] output = mPersistAtomsStorage.getPresenceNotifyEvent(0L);
3488         assertProtoArrayEqualsIgnoringOrder(
3489                 new PresenceNotifyEvent[] {mPresenceNotifyEvent1, mPresenceNotifyEvent2}, output);
3490     }
3491 
3492     @Test
3493     @SmallTest
getPresenceNotifyEvent_tooFrequent()3494     public void getPresenceNotifyEvent_tooFrequent() throws Exception {
3495         createTestFile(START_TIME_MILLIS);
3496 
3497         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3498         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
3499         PresenceNotifyEvent[] output = mPersistAtomsStorage.getPresenceNotifyEvent(100L);
3500 
3501         // Should be denied
3502         assertNull(output);
3503     }
3504 
3505     @Test
3506     @SmallTest
getPresenceNotifyEvent_withSavedAtoms()3507     public void getPresenceNotifyEvent_withSavedAtoms() throws Exception {
3508         createTestFile(START_TIME_MILLIS);
3509 
3510         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3511         mPersistAtomsStorage.incTimeMillis(100L);
3512         PresenceNotifyEvent[] output1 = mPersistAtomsStorage.getPresenceNotifyEvent(50L);
3513         mPersistAtomsStorage.incTimeMillis(100L);
3514         PresenceNotifyEvent[] output2 = mPersistAtomsStorage.getPresenceNotifyEvent(50L);
3515 
3516         // First set of results should equal to file contents, second should be empty, corresponding
3517         // pull timestamp should be updated and saved
3518         assertProtoArrayEqualsIgnoringOrder(
3519                 new PresenceNotifyEvent[] {mPresenceNotifyEvent1, mPresenceNotifyEvent2}, output1);
3520         assertProtoArrayEquals(new PresenceNotifyEvent[0], output2);
3521         assertEquals(
3522                 START_TIME_MILLIS + 200L,
3523                 mPersistAtomsStorage.getAtomsProto().presenceNotifyEventPullTimestampMillis);
3524         InOrder inOrder = inOrder(mTestFileOutputStream);
3525         assertEquals(
3526                 START_TIME_MILLIS + 100L,
3527                 getAtomsWritten(inOrder).presenceNotifyEventPullTimestampMillis);
3528         assertEquals(
3529                 START_TIME_MILLIS + 200L,
3530                 getAtomsWritten(inOrder).presenceNotifyEventPullTimestampMillis);
3531         inOrder.verifyNoMoreInteractions();
3532     }
3533 
3534     @Test
3535     @SmallTest
addSipTransportFeatureTag_emptyProto()3536     public void addSipTransportFeatureTag_emptyProto() throws Exception {
3537         // verify add atom into new file
3538         createEmptyTestFile();
3539         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3540         mPersistAtomsStorage.addSipTransportFeatureTagStats(mSipTransportFeatureTagStats1);
3541         mPersistAtomsStorage.incTimeMillis(100L);
3542         verifyCurrentStateSavedToFileOnce();
3543 
3544         SipTransportFeatureTagStats[] outputs =
3545                 mPersistAtomsStorage.getSipTransportFeatureTagStats(0L);
3546         assertProtoArrayEquals(
3547                 new SipTransportFeatureTagStats[] {mSipTransportFeatureTagStats1}, outputs);
3548     }
3549 
3550     @Test
3551     @SmallTest
addSipTransportFeatureTagStats_withExistingEntries()3552     public void addSipTransportFeatureTagStats_withExistingEntries() throws Exception {
3553         // verify add atom on existing atom already stored
3554         createEmptyTestFile();
3555 
3556         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3557         //Add two different SipTransportFeatureTagStats.
3558         mPersistAtomsStorage.addSipTransportFeatureTagStats(mSipTransportFeatureTagStats1);
3559         mPersistAtomsStorage.addSipTransportFeatureTagStats(mSipTransportFeatureTagStats2);
3560         mPersistAtomsStorage.incTimeMillis(100L);
3561 
3562         // SipTransportFeatureTagStats should be added successfully
3563         verifyCurrentStateSavedToFileOnce();
3564         SipTransportFeatureTagStats[] outputs =
3565                 mPersistAtomsStorage.getSipTransportFeatureTagStats(0L);
3566 
3567         assertProtoArrayEqualsIgnoringOrder(new SipTransportFeatureTagStats[]
3568                 {mSipTransportFeatureTagStats1, mSipTransportFeatureTagStats2}, outputs);
3569     }
3570 
3571     @Test
3572     @SmallTest
addSipTransportFeatureTagStats_tooManyEntries()3573     public void addSipTransportFeatureTagStats_tooManyEntries() throws Exception {
3574         // verify add atom excess MAX count (100)
3575         createEmptyTestFile();
3576 
3577         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3578 
3579         // Try to add 26 stats where MAX is 25
3580         int max = 26;
3581         SipTransportFeatureTagStats[] overMaxSipTransportFeatureTagStats =
3582                 new SipTransportFeatureTagStats[max];
3583 
3584         for (int i = 0; i < max; i++) {
3585             overMaxSipTransportFeatureTagStats[i] = copyOf(mSipTransportFeatureTagStats1);
3586             overMaxSipTransportFeatureTagStats[i].sipTransportDeniedReason = i;
3587             mPersistAtomsStorage
3588                     .addSipTransportFeatureTagStats(overMaxSipTransportFeatureTagStats[i]);
3589             mPersistAtomsStorage.incTimeMillis(100L);
3590         }
3591 
3592         mPersistAtomsStorage
3593                 .addSipTransportFeatureTagStats(mSipTransportFeatureTagStats2);
3594         verifyCurrentStateSavedToFileOnce();
3595 
3596         SipTransportFeatureTagStats[] outputs =
3597                 mPersistAtomsStorage.getSipTransportFeatureTagStats(0L);
3598 
3599         // The last added SipTransportFeatureTagStat remains
3600         // and two old stats should be removed
3601         assertHasStats(outputs, overMaxSipTransportFeatureTagStats, max - 2);
3602         assertHasStats(outputs, mSipTransportFeatureTagStats2, 1);
3603     }
3604 
3605     @Test
3606     @SmallTest
addSipTransportFeatureTagStats_updateExistingEntries()3607     public void addSipTransportFeatureTagStats_updateExistingEntries() throws Exception {
3608         // verify count
3609         createTestFile(START_TIME_MILLIS);
3610 
3611         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3612         mPersistAtomsStorage.addSipTransportFeatureTagStats(copyOf(mSipTransportFeatureTagStats1));
3613         mPersistAtomsStorage.incTimeMillis(100L);
3614         verifyCurrentStateSavedToFileOnce();
3615 
3616         // SipTransportFeatureTag's durations should be doubled
3617         SipTransportFeatureTagStats newSipTransportFeatureTagStats1 =
3618                 copyOf(mSipTransportFeatureTagStats1);
3619         newSipTransportFeatureTagStats1.associatedMillis *= 2;
3620 
3621         SipTransportFeatureTagStats[] outputs =
3622                 mPersistAtomsStorage.getSipTransportFeatureTagStats(0L);
3623 
3624         assertProtoArrayEqualsIgnoringOrder(
3625                 new SipTransportFeatureTagStats[] {
3626                         newSipTransportFeatureTagStats1,
3627                         mSipTransportFeatureTagStats2
3628                 }, outputs);
3629     }
3630 
3631     @Test
3632     @SmallTest
getSipTransportFeatureTagStats_tooFrequent()3633     public void getSipTransportFeatureTagStats_tooFrequent() throws Exception {
3634         // verify get frequently
3635         createTestFile(START_TIME_MILLIS);
3636 
3637         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3638         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
3639 
3640         SipTransportFeatureTagStats[] outputs =
3641                 mPersistAtomsStorage.getSipTransportFeatureTagStats(100L);
3642 
3643         // Should be denied
3644         assertNull(outputs);
3645     }
3646 
3647     @Test
3648     @SmallTest
getSipTransportFeatureTagStats_withSavedAtoms()3649     public void getSipTransportFeatureTagStats_withSavedAtoms() throws Exception {
3650         // verify last get time after get atoms
3651         createTestFile(START_TIME_MILLIS);
3652 
3653         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3654         mPersistAtomsStorage.incTimeMillis(100L);
3655 
3656         SipTransportFeatureTagStats[] output1 =
3657                 mPersistAtomsStorage.getSipTransportFeatureTagStats(50L);
3658         mPersistAtomsStorage.incTimeMillis(100L);
3659         SipTransportFeatureTagStats[] output2 =
3660                 mPersistAtomsStorage.getSipTransportFeatureTagStats(50L);
3661 
3662         // First set of results should equal to file contents, second should be empty, corresponding
3663         // pull timestamp should be updated and saved
3664         assertProtoArrayEqualsIgnoringOrder(
3665                 new SipTransportFeatureTagStats[] {
3666                         mSipTransportFeatureTagStats1,
3667                         mSipTransportFeatureTagStats2
3668                 }, output1);
3669         assertProtoArrayEquals(new SipTransportFeatureTagStats[0], output2);
3670         assertEquals(START_TIME_MILLIS + 200L,
3671                 mPersistAtomsStorage.getAtomsProto()
3672                         .sipTransportFeatureTagStatsPullTimestampMillis);
3673 
3674         InOrder inOrder = inOrder(mTestFileOutputStream);
3675         assertEquals(START_TIME_MILLIS + 100L,
3676                 getAtomsWritten(inOrder).sipTransportFeatureTagStatsPullTimestampMillis);
3677         assertEquals(START_TIME_MILLIS + 200L,
3678                 getAtomsWritten(inOrder).sipTransportFeatureTagStatsPullTimestampMillis);
3679         inOrder.verifyNoMoreInteractions();
3680     }
3681 
3682     @Test
3683     @SmallTest
addSipDelegateStats_emptyProto()3684     public void addSipDelegateStats_emptyProto() throws Exception {
3685         // verify add atom into new file
3686         createEmptyTestFile();
3687 
3688         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3689         mPersistAtomsStorage.addSipDelegateStats(mSipDelegateStats1);
3690         mPersistAtomsStorage.incTimeMillis(100L);
3691         verifyCurrentStateSavedToFileOnce();
3692 
3693         SipDelegateStats[] outputs = mPersistAtomsStorage.getSipDelegateStats(0L);
3694         assertProtoArrayEquals(new SipDelegateStats[] {mSipDelegateStats1}, outputs);
3695     }
3696 
3697     @Test
3698     @SmallTest
addSipDelegateStats_withExistingEntries()3699     public void addSipDelegateStats_withExistingEntries() throws Exception {
3700         // verify add atom on existing atom already stored
3701         createEmptyTestFile();
3702 
3703         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3704         mPersistAtomsStorage.addSipDelegateStats(copyOf(mSipDelegateStats1));
3705         mPersistAtomsStorage.addSipDelegateStats(copyOf(mSipDelegateStats2));
3706         mPersistAtomsStorage.addSipDelegateStats(copyOf(mSipDelegateStats3));
3707         mPersistAtomsStorage.incTimeMillis(100L);
3708         // Three SipDelegateStats should be added successfully
3709         verifyCurrentStateSavedToFileOnce();
3710 
3711         SipDelegateStats[] outputs =
3712                 mPersistAtomsStorage.getSipDelegateStats(0L);
3713 
3714         assertProtoArrayEqualsIgnoringOrder(
3715                 new SipDelegateStats[] {mSipDelegateStats1, mSipDelegateStats2, mSipDelegateStats3},
3716                 outputs);
3717     }
3718 
3719     @Test
3720     @SmallTest
addSipDelegateStats_tooManyEntries()3721     public void addSipDelegateStats_tooManyEntries() throws Exception {
3722         // verify add atom excess MAX count
3723         createEmptyTestFile();
3724 
3725         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3726 
3727         // Try to add 11 stats where MAX is 10
3728         int max = 11;
3729         SipDelegateStats[] overMaxSipDelegateStats = new SipDelegateStats[max];
3730         for (int i = 0; i < max; i++) {
3731             overMaxSipDelegateStats[i] = copyOf(mSipDelegateStats1);
3732             mPersistAtomsStorage
3733                     .addSipDelegateStats(overMaxSipDelegateStats[i]);
3734             mPersistAtomsStorage.incTimeMillis(100L);
3735         }
3736         mPersistAtomsStorage.addSipDelegateStats(mSipDelegateStats3);
3737         verifyCurrentStateSavedToFileOnce();
3738 
3739         SipDelegateStats[] outputs =
3740                 mPersistAtomsStorage.getSipDelegateStats(0L);
3741 
3742         // The last added SipDelegate remains
3743         // and two old stats should be removed
3744         assertHasStats(outputs, overMaxSipDelegateStats, max - 2);
3745         assertHasStats(outputs, mSipDelegateStats3, 1);
3746     }
3747 
3748     @Test
3749     @SmallTest
addSipDelegateStats_updateExistingEntries()3750     public void addSipDelegateStats_updateExistingEntries() throws Exception {
3751         // verify count
3752         createTestFile(START_TIME_MILLIS);
3753 
3754         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3755         SipDelegateStats newSipDelegateStats3 = copyOf(mSipDelegateStats3);
3756         newSipDelegateStats3.destroyReason =
3757                 SipDelegateManager.SIP_DELEGATE_DESTROY_REASON_SERVICE_DEAD;
3758         mPersistAtomsStorage.addSipDelegateStats(newSipDelegateStats3);
3759         mPersistAtomsStorage.incTimeMillis(100L);
3760 
3761         SipDelegateStats newSipDelegateStats1 = copyOf(mSipDelegateStats1);
3762         newSipDelegateStats1.destroyReason =
3763                 SipDelegateManager.SIP_DELEGATE_DESTROY_REASON_REQUESTED_BY_APP;
3764         mPersistAtomsStorage.addSipDelegateStats(newSipDelegateStats1);
3765         mPersistAtomsStorage.incTimeMillis(100L);
3766         verifyCurrentStateSavedToFileOnce();
3767 
3768         SipDelegateStats[] outputs = mPersistAtomsStorage.getSipDelegateStats(0L);
3769 
3770         assertProtoArrayEqualsIgnoringOrder(
3771                 new SipDelegateStats[] {mSipDelegateStats2, mSipDelegateStats3,
3772                         newSipDelegateStats3, newSipDelegateStats1}, outputs);
3773     }
3774 
3775     @Test
3776     @SmallTest
getSipDelegateStats_tooFrequent()3777     public void getSipDelegateStats_tooFrequent() throws Exception {
3778         // verify get frequently
3779         createTestFile(START_TIME_MILLIS);
3780 
3781         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3782         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
3783 
3784         SipDelegateStats[] outputs = mPersistAtomsStorage.getSipDelegateStats(100L);
3785         // Should be denied
3786         assertNull(outputs);
3787     }
3788 
3789     @Test
3790     @SmallTest
getSipDelegateStats_withSavedAtoms()3791     public void getSipDelegateStats_withSavedAtoms() throws Exception {
3792         // verify last get time after get atoms
3793         createTestFile(START_TIME_MILLIS);
3794 
3795         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3796         mPersistAtomsStorage.incTimeMillis(100L);
3797 
3798         SipDelegateStats[] output1 = mPersistAtomsStorage.getSipDelegateStats(50L);
3799         mPersistAtomsStorage.incTimeMillis(100L);
3800         SipDelegateStats[] output2 = mPersistAtomsStorage.getSipDelegateStats(50L);
3801 
3802         // First set of results should equal to file contents, second should be empty, corresponding
3803         // pull timestamp should be updated and saved
3804         assertProtoArrayEqualsIgnoringOrder(
3805                 new SipDelegateStats[] {
3806                         mSipDelegateStats2,
3807                         mSipDelegateStats3}, output1);
3808         assertProtoArrayEquals(new SipDelegateStats[0], output2);
3809         assertEquals(
3810                 START_TIME_MILLIS + 200L,
3811                 mPersistAtomsStorage.getAtomsProto().sipDelegateStatsPullTimestampMillis);
3812         InOrder inOrder = inOrder(mTestFileOutputStream);
3813         assertEquals(
3814                 START_TIME_MILLIS + 100L,
3815                 getAtomsWritten(inOrder).sipDelegateStatsPullTimestampMillis);
3816         assertEquals(
3817                 START_TIME_MILLIS + 200L,
3818                 getAtomsWritten(inOrder).sipDelegateStatsPullTimestampMillis);
3819         inOrder.verifyNoMoreInteractions();
3820     }
3821 
3822     @Test
3823     @SmallTest
addGbaEvent_emptyProto()3824     public void addGbaEvent_emptyProto() throws Exception {
3825         createEmptyTestFile();
3826         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3827         mPersistAtomsStorage.addGbaEvent(mGbaEvent1);
3828         mPersistAtomsStorage.incTimeMillis(100L);
3829 
3830         // gba event should be added successfully
3831         verifyCurrentStateSavedToFileOnce();
3832         GbaEvent[] stats = mPersistAtomsStorage.getGbaEvent(0L);
3833         assertProtoArrayEquals(new GbaEvent[] {mGbaEvent1}, stats);
3834     }
3835 
3836     @Test
3837     @SmallTest
addGbaEvent_withExistingEntries()3838     public void addGbaEvent_withExistingEntries() throws Exception {
3839         createEmptyTestFile();
3840         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3841         mPersistAtomsStorage.addGbaEvent(mGbaEvent1);
3842         mPersistAtomsStorage.incTimeMillis(100L);
3843         mPersistAtomsStorage.addGbaEvent(mGbaEvent2);
3844         mPersistAtomsStorage.incTimeMillis(100L);
3845 
3846         // gba event1, gba event2 should be added successfully
3847         verifyCurrentStateSavedToFileOnce();
3848         GbaEvent[] stats = mPersistAtomsStorage.getGbaEvent(0L);
3849         assertProtoArrayEqualsIgnoringOrder(
3850                 new GbaEvent[] {mGbaEvent1, mGbaEvent2}, stats);
3851     }
3852 
3853     @Test
3854     @SmallTest
addGbaEvent_tooManyEntries()3855     public void addGbaEvent_tooManyEntries() throws Exception {
3856         createEmptyTestFile();
3857 
3858         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3859 
3860         // Add 11 stats, but max is 10
3861         for (int i = 0; i < 11; i++) {
3862             mPersistAtomsStorage.addGbaEvent(mGbaEvent1);
3863             mPersistAtomsStorage.incTimeMillis(100L);
3864         }
3865         mPersistAtomsStorage.addGbaEvent(mGbaEvent2);
3866 
3867         verifyCurrentStateSavedToFileOnce();
3868         GbaEvent[] stats = mPersistAtomsStorage.getGbaEvent(0L);
3869         assertHasStatsAndCount(stats, mGbaEvent1, 11);
3870         assertHasStatsAndCount(stats, mGbaEvent2, 1);
3871     }
3872 
3873     @Test
3874     @SmallTest
addGbaEvent_updateExistingEntries()3875     public void addGbaEvent_updateExistingEntries() throws Exception {
3876         createTestFile(START_TIME_MILLIS);
3877         GbaEvent newStats = copyOf(mGbaEvent1);
3878         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3879 
3880         mPersistAtomsStorage.addGbaEvent(copyOf(mGbaEvent1));
3881         mPersistAtomsStorage.incTimeMillis(100L);
3882 
3883         // mGbaEvent1's count should be doubled
3884         verifyCurrentStateSavedToFileOnce();
3885         GbaEvent[] stats =
3886                 mPersistAtomsStorage.getGbaEvent(0L);
3887         newStats.count *= 2;
3888         assertProtoArrayEqualsIgnoringOrder(new GbaEvent[] {mGbaEvent2, newStats}, stats);
3889     }
3890 
3891     @Test
3892     @SmallTest
addSipMessageResponse_emptyProto()3893     public void addSipMessageResponse_emptyProto() throws Exception {
3894         createEmptyTestFile();
3895         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3896         mPersistAtomsStorage.addSipMessageResponse(mSipMessageResponse1);
3897         mPersistAtomsStorage.incTimeMillis(100L);
3898 
3899         verifyCurrentStateSavedToFileOnce();
3900         SipMessageResponse[] expected = mPersistAtomsStorage.getSipMessageResponse(0L);
3901         assertProtoArrayEquals(new SipMessageResponse[] {mSipMessageResponse1}, expected);
3902     }
3903 
3904     @Test
3905     @SmallTest
addSipMessageResponse_withExistingEntries()3906     public void addSipMessageResponse_withExistingEntries() throws Exception {
3907         createEmptyTestFile();
3908         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3909         mPersistAtomsStorage.addSipMessageResponse(mSipMessageResponse1);
3910         mPersistAtomsStorage.incTimeMillis(100L);
3911         mPersistAtomsStorage.addSipMessageResponse(mSipMessageResponse2);
3912         mPersistAtomsStorage.incTimeMillis(100L);
3913 
3914         verifyCurrentStateSavedToFileOnce();
3915         SipMessageResponse[] expected =
3916                 new SipMessageResponse[] {mSipMessageResponse1, mSipMessageResponse2};
3917 
3918         assertProtoArrayEqualsIgnoringOrder(
3919                 expected, mPersistAtomsStorage.getSipMessageResponse(0L));
3920     }
3921 
3922     @Test
3923     @SmallTest
addSipMessageResponse_tooManyEntries()3924     public void addSipMessageResponse_tooManyEntries() throws Exception {
3925         createEmptyTestFile();
3926         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3927 
3928         // Store 11 same atoms, but only 1 atoms stored with count 11
3929         for (int i = 0; i < 11; i++) {
3930             mPersistAtomsStorage.addSipMessageResponse(mSipMessageResponse1);
3931             mPersistAtomsStorage.incTimeMillis(100L);
3932         }
3933         // Store 1 different atom and count 1
3934         mPersistAtomsStorage.addSipMessageResponse(mSipMessageResponse2);
3935 
3936         verifyCurrentStateSavedToFileOnce();
3937         SipMessageResponse[] result = mPersistAtomsStorage.getSipMessageResponse(0L);
3938 
3939         // First atom has count 11, the other has 1
3940         assertHasStats(result, mSipMessageResponse1, 11);
3941         assertHasStats(result, mSipMessageResponse2, 1);
3942     }
3943 
3944     @Test
3945     @SmallTest
addSipMessageResponse_updateExistingEntries()3946     public void addSipMessageResponse_updateExistingEntries() throws Exception {
3947         createTestFile(START_TIME_MILLIS);
3948         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3949         mPersistAtomsStorage.addSipMessageResponse(copyOf(mSipMessageResponse2));
3950         mPersistAtomsStorage.incTimeMillis(100L);
3951         verifyCurrentStateSavedToFileOnce();
3952 
3953         SipMessageResponse[] outputs = mPersistAtomsStorage.getSipMessageResponse(0L);
3954         SipMessageResponse newSipMessageResponse = copyOf(mSipMessageResponse2);
3955         newSipMessageResponse.count *= 2;
3956         assertProtoArrayEqualsIgnoringOrder(
3957                 new SipMessageResponse[] {mSipMessageResponse1, newSipMessageResponse}, outputs);
3958     }
3959 
3960     @Test
3961     @SmallTest
addCompleteSipTransportSession_emptyProto()3962     public void addCompleteSipTransportSession_emptyProto() throws Exception {
3963         createEmptyTestFile();
3964         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3965         mPersistAtomsStorage.addCompleteSipTransportSession(mSipTransportSession1);
3966         mPersistAtomsStorage.incTimeMillis(100L);
3967 
3968         verifyCurrentStateSavedToFileOnce();
3969         SipTransportSession[] expected = mPersistAtomsStorage.getSipTransportSession(0L);
3970         assertProtoArrayEquals(new SipTransportSession[] {mSipTransportSession1}, expected);
3971     }
3972 
3973     @Test
3974     @SmallTest
addCompleteSipTransportSession_withExistingEntries()3975     public void addCompleteSipTransportSession_withExistingEntries() throws Exception {
3976         createEmptyTestFile();
3977         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3978         mPersistAtomsStorage.addCompleteSipTransportSession(mSipTransportSession1);
3979         mPersistAtomsStorage.incTimeMillis(100L);
3980         mPersistAtomsStorage.addCompleteSipTransportSession(mSipTransportSession2);
3981         mPersistAtomsStorage.incTimeMillis(100L);
3982 
3983         verifyCurrentStateSavedToFileOnce();
3984         SipTransportSession[] expected =
3985                 new SipTransportSession[] {mSipTransportSession1, mSipTransportSession2};
3986 
3987         assertProtoArrayEqualsIgnoringOrder(
3988                 expected, mPersistAtomsStorage.getSipTransportSession(0L));
3989     }
3990 
3991     @Test
3992     @SmallTest
addCompleteSipTransportSession_tooManyEntries()3993     public void addCompleteSipTransportSession_tooManyEntries() throws Exception {
3994         createEmptyTestFile();
3995         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
3996 
3997         // Store 11 same atoms, but only 1 atoms stored with count 11
3998         for (int i = 0; i < 11; i++) {
3999             mPersistAtomsStorage.addCompleteSipTransportSession(mSipTransportSession1);
4000             mPersistAtomsStorage.incTimeMillis(100L);
4001         }
4002         // Store 1 different atom and count 1
4003         mPersistAtomsStorage.addCompleteSipTransportSession(mSipTransportSession2);
4004 
4005         verifyCurrentStateSavedToFileOnce();
4006         SipTransportSession[] result = mPersistAtomsStorage.getSipTransportSession(0L);
4007 
4008         // First atom has count 11, the other has 1
4009         assertHasStats(result, mSipTransportSession1, 11);
4010         assertHasStats(result, mSipTransportSession2, 1);
4011     }
4012 
4013     @Test
4014     @SmallTest
addCompleteSipTransportSession_updateExistingEntries()4015     public void addCompleteSipTransportSession_updateExistingEntries() throws Exception {
4016         createTestFile(START_TIME_MILLIS);
4017         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4018         mPersistAtomsStorage.addCompleteSipTransportSession(copyOf(mSipTransportSession2));
4019         mPersistAtomsStorage.incTimeMillis(100L);
4020         verifyCurrentStateSavedToFileOnce();
4021 
4022         SipTransportSession[] outputs = mPersistAtomsStorage.getSipTransportSession(0L);
4023         SipTransportSession newSipTransportSession = copyOf(mSipTransportSession2);
4024         newSipTransportSession.sessionCount *= 2;
4025         newSipTransportSession.endedGracefullyCount *= 2;
4026         assertProtoArrayEqualsIgnoringOrder(
4027                 new SipTransportSession[] {mSipTransportSession1,
4028                         newSipTransportSession}, outputs);
4029     }
4030 
4031     @Test
4032     @SmallTest
getUnmeteredNetworks_noExistingEntry()4033     public void getUnmeteredNetworks_noExistingEntry() throws Exception {
4034         createTestFile(START_TIME_MILLIS);
4035         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4036         InOrder inOrder = inOrder(mTestFileOutputStream);
4037 
4038         assertEquals(0L, mPersistAtomsStorage.getUnmeteredNetworks(1, 0));
4039 
4040         inOrder.verifyNoMoreInteractions();
4041     }
4042 
4043     @Test
4044     @SmallTest
getUnmeteredNetworks()4045     public void getUnmeteredNetworks() throws Exception {
4046         createTestFile(START_TIME_MILLIS);
4047         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4048         InOrder inOrder = inOrder(mTestFileOutputStream);
4049 
4050         mPersistAtomsStorage.addUnmeteredNetworks(0, 0, NETWORK_TYPE_BITMASK_GPRS);
4051         inOrder.verify(mTestFileOutputStream, times(1))
4052                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4053         inOrder.verify(mTestFileOutputStream, times(1)).close();
4054         inOrder.verifyNoMoreInteractions();
4055 
4056         assertEquals(NETWORK_TYPE_BITMASK_GPRS, mPersistAtomsStorage.getUnmeteredNetworks(0, 0));
4057         inOrder.verify(mTestFileOutputStream, times(1))
4058                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4059         inOrder.verify(mTestFileOutputStream, times(1)).close();
4060         inOrder.verifyNoMoreInteractions();
4061 
4062         assertEquals(0L, mPersistAtomsStorage.getUnmeteredNetworks(0, 0));
4063         inOrder.verifyNoMoreInteractions();
4064     }
4065 
4066     @Test
4067     @SmallTest
getUnmeteredNetworks_carrierIdMismatch()4068     public void getUnmeteredNetworks_carrierIdMismatch() throws Exception {
4069         createTestFile(START_TIME_MILLIS);
4070         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4071         InOrder inOrder = inOrder(mTestFileOutputStream);
4072 
4073         mPersistAtomsStorage.addUnmeteredNetworks(0, 0, NETWORK_TYPE_BITMASK_GPRS);
4074         inOrder.verify(mTestFileOutputStream, times(1))
4075                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4076         inOrder.verify(mTestFileOutputStream, times(1)).close();
4077         inOrder.verifyNoMoreInteractions();
4078 
4079         assertEquals(0L, mPersistAtomsStorage.getUnmeteredNetworks(0, 1));
4080         inOrder.verify(mTestFileOutputStream, times(1))
4081                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4082         inOrder.verify(mTestFileOutputStream, times(1)).close();
4083         inOrder.verifyNoMoreInteractions();
4084 
4085         assertEquals(0L, mPersistAtomsStorage.getUnmeteredNetworks(0, 0));
4086         inOrder.verifyNoMoreInteractions();
4087     }
4088 
4089     @Test
4090     @SmallTest
addUnmeteredNetworks()4091     public void addUnmeteredNetworks() throws Exception {
4092         createTestFile(START_TIME_MILLIS);
4093         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4094         InOrder inOrder = inOrder(mTestFileOutputStream);
4095 
4096         mPersistAtomsStorage.addUnmeteredNetworks(0, 0, NETWORK_TYPE_BITMASK_GPRS);
4097         inOrder.verify(mTestFileOutputStream, times(1))
4098                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4099         inOrder.verify(mTestFileOutputStream, times(1)).close();
4100         inOrder.verifyNoMoreInteractions();
4101 
4102         mPersistAtomsStorage.addUnmeteredNetworks(0, 0, NETWORK_TYPE_BITMASK_GSM);
4103         inOrder.verify(mTestFileOutputStream, times(1))
4104                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4105         inOrder.verify(mTestFileOutputStream, times(1)).close();
4106         inOrder.verifyNoMoreInteractions();
4107 
4108         assertEquals(
4109                 NETWORK_TYPE_BITMASK_GPRS | NETWORK_TYPE_BITMASK_GSM,
4110                 mPersistAtomsStorage.getUnmeteredNetworks(0, 0));
4111         inOrder.verify(mTestFileOutputStream, times(1))
4112                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4113         inOrder.verify(mTestFileOutputStream, times(1)).close();
4114         inOrder.verifyNoMoreInteractions();
4115 
4116         assertEquals(0, mPersistAtomsStorage.getUnmeteredNetworks(0, 0));
4117         inOrder.verifyNoMoreInteractions();
4118 
4119         mPersistAtomsStorage.addUnmeteredNetworks(1, 2, NETWORK_TYPE_BITMASK_GPRS);
4120         inOrder.verify(mTestFileOutputStream, times(1))
4121                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4122         inOrder.verify(mTestFileOutputStream, times(1)).close();
4123         inOrder.verifyNoMoreInteractions();
4124 
4125         mPersistAtomsStorage.addUnmeteredNetworks(1, 2, NETWORK_TYPE_BITMASK_GSM);
4126         inOrder.verify(mTestFileOutputStream, times(1))
4127                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4128         inOrder.verify(mTestFileOutputStream, times(1)).close();
4129         inOrder.verifyNoMoreInteractions();
4130 
4131         assertEquals(
4132                 NETWORK_TYPE_BITMASK_GPRS | NETWORK_TYPE_BITMASK_GSM,
4133                 mPersistAtomsStorage.getUnmeteredNetworks(1, 2));
4134         inOrder.verify(mTestFileOutputStream, times(1))
4135                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4136         inOrder.verify(mTestFileOutputStream, times(1)).close();
4137         inOrder.verifyNoMoreInteractions();
4138 
4139         assertEquals(0, mPersistAtomsStorage.getUnmeteredNetworks(1, 2));
4140         inOrder.verifyNoMoreInteractions();
4141     }
4142 
4143     @Test
4144     @SmallTest
addUnmeteredNetworks_carrierIdMismatch()4145     public void addUnmeteredNetworks_carrierIdMismatch() throws Exception {
4146         createTestFile(START_TIME_MILLIS);
4147         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4148         InOrder inOrder = inOrder(mTestFileOutputStream);
4149 
4150         mPersistAtomsStorage.addUnmeteredNetworks(0, 0, NETWORK_TYPE_BITMASK_GPRS);
4151         inOrder.verify(mTestFileOutputStream, times(1))
4152                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4153         inOrder.verify(mTestFileOutputStream, times(1)).close();
4154         inOrder.verifyNoMoreInteractions();
4155 
4156         mPersistAtomsStorage.addUnmeteredNetworks(0, 1, NETWORK_TYPE_BITMASK_GSM);
4157         inOrder.verify(mTestFileOutputStream, times(1))
4158                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4159         inOrder.verify(mTestFileOutputStream, times(1)).close();
4160         inOrder.verifyNoMoreInteractions();
4161 
4162         assertEquals(NETWORK_TYPE_BITMASK_GSM, mPersistAtomsStorage.getUnmeteredNetworks(0, 1));
4163         inOrder.verify(mTestFileOutputStream, times(1))
4164                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4165         inOrder.verify(mTestFileOutputStream, times(1)).close();
4166         inOrder.verifyNoMoreInteractions();
4167 
4168         assertEquals(0L, mPersistAtomsStorage.getUnmeteredNetworks(0, 1));
4169         inOrder.verifyNoMoreInteractions();
4170     }
4171 
4172     @Test
4173     @SmallTest
addUnmeteredNetworks_sameBitmask()4174     public void addUnmeteredNetworks_sameBitmask() throws Exception {
4175         createTestFile(START_TIME_MILLIS);
4176         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4177         InOrder inOrder = inOrder(mTestFileOutputStream);
4178 
4179         mPersistAtomsStorage.addUnmeteredNetworks(0, 0, NETWORK_TYPE_BITMASK_GPRS);
4180         inOrder.verify(mTestFileOutputStream, times(1))
4181                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4182         inOrder.verify(mTestFileOutputStream, times(1)).close();
4183         inOrder.verifyNoMoreInteractions();
4184 
4185         mPersistAtomsStorage.addUnmeteredNetworks(0, 0, NETWORK_TYPE_BITMASK_GPRS);
4186         inOrder.verifyNoMoreInteractions();
4187 
4188         assertEquals(NETWORK_TYPE_BITMASK_GPRS, mPersistAtomsStorage.getUnmeteredNetworks(0, 0));
4189         inOrder.verify(mTestFileOutputStream, times(1))
4190                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
4191         inOrder.verify(mTestFileOutputStream, times(1)).close();
4192         inOrder.verifyNoMoreInteractions();
4193     }
4194 
4195     @Test
addIncomingSms_emptyProto()4196     public void addIncomingSms_emptyProto() throws Exception {
4197         createEmptyTestFile();
4198 
4199         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4200         mPersistAtomsStorage.addIncomingSms(mIncomingSms1);
4201         mPersistAtomsStorage.incTimeMillis(100L);
4202 
4203         // IncomingSms should be added successfully, changes should be saved.
4204         verifyCurrentStateSavedToFileOnce();
4205         IncomingSms[] expectedList = new IncomingSms[] {mIncomingSms1};
4206         assertProtoArrayEquals(expectedList, mPersistAtomsStorage.getIncomingSms(0L));
4207     }
4208 
4209     @Test
addIncomingSms_withExistingEntries()4210     public void addIncomingSms_withExistingEntries() throws Exception {
4211         createEmptyTestFile();
4212 
4213         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4214         mPersistAtomsStorage.addIncomingSms(mIncomingSms1);
4215         mPersistAtomsStorage.addIncomingSms(mIncomingSms2);
4216         mPersistAtomsStorage.incTimeMillis(100L);
4217 
4218         // IncomingSms should be added successfully.
4219         verifyCurrentStateSavedToFileOnce();
4220         IncomingSms[] expectedList = new IncomingSms[] {mIncomingSms1, mIncomingSms2};
4221         assertProtoArrayEqualsIgnoringOrder(expectedList, mPersistAtomsStorage.getIncomingSms(0L));
4222     }
4223 
4224     @Test
getIncomingSms_tooFrequent()4225     public void getIncomingSms_tooFrequent() throws Exception {
4226         createTestFile(START_TIME_MILLIS);
4227 
4228         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4229         // Pull interval less than minimum.
4230         mPersistAtomsStorage.incTimeMillis(50L);
4231         IncomingSms[] outgoingShortCodeSmsList = mPersistAtomsStorage.getIncomingSms(100L);
4232         // Should be denied.
4233         assertNull(outgoingShortCodeSmsList);
4234     }
4235 
4236     @Test
addOutgoingSms_emptyProto()4237     public void addOutgoingSms_emptyProto() throws Exception {
4238         createEmptyTestFile();
4239 
4240         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4241         mPersistAtomsStorage.addOutgoingSms(mOutgoingSms1);
4242         mPersistAtomsStorage.incTimeMillis(100L);
4243 
4244         // OutgoingSms should be added successfully, changes should be saved.
4245         verifyCurrentStateSavedToFileOnce();
4246         OutgoingSms[] expectedList = new OutgoingSms[] {mOutgoingSms1};
4247         assertProtoArrayEquals(expectedList, mPersistAtomsStorage.getOutgoingSms(0L));
4248     }
4249 
4250     @Test
addOutgoingSms_withExistingEntries()4251     public void addOutgoingSms_withExistingEntries() throws Exception {
4252         createEmptyTestFile();
4253 
4254         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4255         mPersistAtomsStorage.addOutgoingSms(mOutgoingSms1);
4256         mPersistAtomsStorage.addOutgoingSms(mOutgoingSms2);
4257         mPersistAtomsStorage.incTimeMillis(100L);
4258 
4259         // OutgoingSms should be added successfully.
4260         verifyCurrentStateSavedToFileOnce();
4261         OutgoingSms[] expectedList = new OutgoingSms[] {mOutgoingSms1, mOutgoingSms2};
4262         assertProtoArrayEqualsIgnoringOrder(expectedList, mPersistAtomsStorage.getOutgoingSms(0L));
4263     }
4264 
4265     @Test
getOutgoingSms_tooFrequent()4266     public void getOutgoingSms_tooFrequent() throws Exception {
4267         createTestFile(START_TIME_MILLIS);
4268 
4269         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4270         // Pull interval less than minimum.
4271         mPersistAtomsStorage.incTimeMillis(50L);
4272         OutgoingSms[] outgoingShortCodeSmsList = mPersistAtomsStorage.getOutgoingSms(100L);
4273         // Should be denied.
4274         assertNull(outgoingShortCodeSmsList);
4275     }
4276 
4277     @Test
addOutgoingShortCodeSms_emptyProto()4278     public void addOutgoingShortCodeSms_emptyProto() throws Exception {
4279         createEmptyTestFile();
4280 
4281         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4282         mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms1);
4283         mPersistAtomsStorage.incTimeMillis(100L);
4284 
4285         // OutgoingShortCodeSms should be added successfully, changes should be saved.
4286         verifyCurrentStateSavedToFileOnce();
4287         OutgoingShortCodeSms[] expectedList = new OutgoingShortCodeSms[] {mOutgoingShortCodeSms1};
4288         assertProtoArrayEquals(expectedList,
4289                 mPersistAtomsStorage.getOutgoingShortCodeSms(0L));
4290     }
4291 
4292     @Test
addOutgoingShortCodeSms_withExistingEntries()4293     public void addOutgoingShortCodeSms_withExistingEntries() throws Exception {
4294         createEmptyTestFile();
4295 
4296         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4297         mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms1);
4298         mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms2);
4299         mPersistAtomsStorage.incTimeMillis(100L);
4300 
4301         // OutgoingShortCodeSms should be added successfully.
4302         verifyCurrentStateSavedToFileOnce();
4303         OutgoingShortCodeSms[] expectedList = new OutgoingShortCodeSms[] {mOutgoingShortCodeSms1,
4304                 mOutgoingShortCodeSms2};
4305         assertProtoArrayEqualsIgnoringOrder(expectedList,
4306                 mPersistAtomsStorage.getOutgoingShortCodeSms(0L));
4307     }
4308 
4309     @Test
addOutgoingShortCodeSms_updateExistingEntries()4310     public void addOutgoingShortCodeSms_updateExistingEntries() throws Exception {
4311         createTestFile(START_TIME_MILLIS);
4312 
4313         // Add copy of mOutgoingShortCodeSms1.
4314         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4315         mPersistAtomsStorage.addOutgoingShortCodeSms(copyOf(mOutgoingShortCodeSms1));
4316         mPersistAtomsStorage.incTimeMillis(100L);
4317 
4318         // mOutgoingShortCodeSms1's short code sms count should be increased by 1.
4319         verifyCurrentStateSavedToFileOnce();
4320         OutgoingShortCodeSms newOutgoingShortCodeSms1 = copyOf(mOutgoingShortCodeSms1);
4321         newOutgoingShortCodeSms1.shortCodeSmsCount = 2;
4322         OutgoingShortCodeSms[] expectedList = new OutgoingShortCodeSms[] {newOutgoingShortCodeSms1,
4323                 mOutgoingShortCodeSms2};
4324         assertProtoArrayEqualsIgnoringOrder(expectedList,
4325                 mPersistAtomsStorage.getOutgoingShortCodeSms(0L));
4326     }
4327 
4328     @Test
addOutgoingShortCodeSms_tooManyEntries()4329     public void addOutgoingShortCodeSms_tooManyEntries() throws Exception {
4330         createEmptyTestFile();
4331 
4332         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4333 
4334         // Store mOutgoingShortCodeSms1 11 times.
4335         for (int i = 0; i < 11; i++) {
4336             mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms1);
4337             mPersistAtomsStorage.incTimeMillis(100L);
4338         }
4339         // Store mOutgoingShortCodeSms2 1 time.
4340         mPersistAtomsStorage.addOutgoingShortCodeSms(mOutgoingShortCodeSms2);
4341 
4342         verifyCurrentStateSavedToFileOnce();
4343         OutgoingShortCodeSms[] result = mPersistAtomsStorage
4344                 .getOutgoingShortCodeSms(0L);
4345         assertHasStatsAndCount(result, mOutgoingShortCodeSms1, 11);
4346         assertHasStatsAndCount(result, mOutgoingShortCodeSms2, 1);
4347     }
4348 
4349     @Test
getOutgoingShortCodeSms_tooFrequent()4350     public void getOutgoingShortCodeSms_tooFrequent() throws Exception {
4351         createTestFile(START_TIME_MILLIS);
4352 
4353         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4354         // Pull interval less than minimum.
4355         mPersistAtomsStorage.incTimeMillis(50L);
4356         OutgoingShortCodeSms[] outgoingShortCodeSmsList = mPersistAtomsStorage
4357                 .getOutgoingShortCodeSms(100L);
4358         // Should be denied.
4359         assertNull(outgoingShortCodeSmsList);
4360     }
4361 
4362     @Test
getOutgoingShortCodeSms_withSavedAtoms()4363     public void getOutgoingShortCodeSms_withSavedAtoms() throws Exception {
4364         createTestFile(START_TIME_MILLIS);
4365 
4366         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4367         mPersistAtomsStorage.incTimeMillis(100L);
4368         OutgoingShortCodeSms[] outgoingShortCodeSmsList1 = mPersistAtomsStorage
4369                 .getOutgoingShortCodeSms(50L);
4370         mPersistAtomsStorage.incTimeMillis(100L);
4371         OutgoingShortCodeSms[] outgoingShortCodeSmsList2 = mPersistAtomsStorage
4372                 .getOutgoingShortCodeSms(50L);
4373 
4374         // First set of results should be equal to file contents.
4375         OutgoingShortCodeSms[] expectedOutgoingShortCodeSmsList =
4376                 new OutgoingShortCodeSms[] {mOutgoingShortCodeSms1, mOutgoingShortCodeSms2};
4377         assertProtoArrayEqualsIgnoringOrder(expectedOutgoingShortCodeSmsList,
4378                 outgoingShortCodeSmsList1);
4379         // Second set of results should be empty.
4380         assertProtoArrayEquals(new OutgoingShortCodeSms[0], outgoingShortCodeSmsList2);
4381         // Corresponding pull timestamp should be updated and saved.
4382         assertEquals(START_TIME_MILLIS + 200L, mPersistAtomsStorage
4383                 .getAtomsProto().outgoingShortCodeSmsPullTimestampMillis);
4384         InOrder inOrder = inOrder(mTestFileOutputStream);
4385         assertEquals(START_TIME_MILLIS + 100L,
4386                 getAtomsWritten(inOrder).outgoingShortCodeSmsPullTimestampMillis);
4387         assertEquals(START_TIME_MILLIS + 200L,
4388                 getAtomsWritten(inOrder).outgoingShortCodeSmsPullTimestampMillis);
4389         inOrder.verifyNoMoreInteractions();
4390     }
4391 
4392     @Test
addSatelliteControllerStats_emptyProto()4393     public void addSatelliteControllerStats_emptyProto() throws Exception {
4394         createEmptyTestFile();
4395         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4396         mPersistAtomsStorage.addSatelliteControllerStats(mSatelliteController1);
4397         mPersistAtomsStorage.incTimeMillis(100L);
4398 
4399         // Service state and service switch should be added successfully
4400         verifyCurrentStateSavedToFileOnce();
4401         SatelliteController[] output =
4402                 mPersistAtomsStorage.getSatelliteControllerStats(0L);
4403         assertProtoArrayEquals(
4404                 new SatelliteController[] {mSatelliteController1}, output);
4405     }
4406 
4407     @Test
addSatelliteControllerStats_withExistingEntries()4408     public void addSatelliteControllerStats_withExistingEntries() throws Exception {
4409         createEmptyTestFile();
4410         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4411         mPersistAtomsStorage.addSatelliteControllerStats(copyOf(mSatelliteController1));
4412         mPersistAtomsStorage.addSatelliteControllerStats(copyOf(mSatelliteController1));
4413         mPersistAtomsStorage.incTimeMillis(100L);
4414 
4415         SatelliteController expected = new SatelliteController();
4416         expected.countOfSatelliteServiceEnablementsSuccess =
4417                 mSatelliteController1.countOfSatelliteServiceEnablementsSuccess * 2;
4418         expected.countOfSatelliteServiceEnablementsFail =
4419                 mSatelliteController1.countOfSatelliteServiceEnablementsFail * 2;
4420         expected.countOfOutgoingDatagramSuccess =
4421                 mSatelliteController1.countOfOutgoingDatagramSuccess * 2;
4422         expected.countOfOutgoingDatagramFail =
4423                 mSatelliteController1.countOfOutgoingDatagramFail * 2;
4424         expected.countOfIncomingDatagramSuccess =
4425                 mSatelliteController1.countOfIncomingDatagramSuccess * 2;
4426         expected.countOfIncomingDatagramFail =
4427                 mSatelliteController1.countOfIncomingDatagramFail * 2;
4428         expected.countOfDatagramTypeSosSmsSuccess =
4429                 mSatelliteController1.countOfDatagramTypeSosSmsSuccess * 2;
4430         expected.countOfDatagramTypeSosSmsFail =
4431                 mSatelliteController1.countOfDatagramTypeSosSmsFail * 2;
4432         expected.countOfDatagramTypeLocationSharingSuccess =
4433                 mSatelliteController1.countOfDatagramTypeLocationSharingSuccess * 2;
4434         expected.countOfDatagramTypeLocationSharingFail =
4435                 mSatelliteController1.countOfDatagramTypeLocationSharingFail * 2;
4436         expected.countOfProvisionSuccess =
4437                 mSatelliteController1.countOfProvisionSuccess * 2;
4438         expected.countOfProvisionFail =
4439                 mSatelliteController1.countOfProvisionFail * 2;
4440         expected.countOfDeprovisionSuccess =
4441                 mSatelliteController1.countOfDeprovisionSuccess * 2;
4442         expected.countOfDeprovisionFail =
4443                 mSatelliteController1.countOfDeprovisionFail * 2;
4444         expected.totalServiceUptimeSec =
4445                 mSatelliteController1.totalServiceUptimeSec * 2;
4446         expected.totalBatteryConsumptionPercent =
4447                 mSatelliteController1.totalBatteryConsumptionPercent * 2;
4448         expected.totalBatteryChargedTimeSec =
4449                 mSatelliteController1.totalBatteryChargedTimeSec * 2;
4450         expected.countOfDemoModeSatelliteServiceEnablementsSuccess =
4451                 mSatelliteController1.countOfDemoModeSatelliteServiceEnablementsSuccess * 2;
4452         expected.countOfDemoModeSatelliteServiceEnablementsFail =
4453                 mSatelliteController1.countOfDemoModeSatelliteServiceEnablementsFail * 2;
4454         expected.countOfDemoModeOutgoingDatagramSuccess =
4455                 mSatelliteController1.countOfDemoModeOutgoingDatagramSuccess * 2;
4456         expected.countOfDemoModeOutgoingDatagramFail =
4457                 mSatelliteController1.countOfDemoModeOutgoingDatagramFail * 2;
4458         expected.countOfDemoModeIncomingDatagramSuccess =
4459                 mSatelliteController1.countOfDemoModeIncomingDatagramSuccess * 2;
4460         expected.countOfDemoModeIncomingDatagramFail =
4461                 mSatelliteController1.countOfDemoModeIncomingDatagramFail * 2;
4462         expected.countOfDatagramTypeKeepAliveSuccess =
4463                 mSatelliteController1.countOfDatagramTypeKeepAliveSuccess * 2;
4464         expected.countOfDatagramTypeKeepAliveFail =
4465                 mSatelliteController1.countOfDatagramTypeKeepAliveFail * 2;
4466         expected.isProvisioned = mSatelliteController1.isProvisioned;
4467         expected.carrierId = mSatelliteController1.carrierId;
4468         expected.countOfSatelliteAllowedStateChangedEvents =
4469                 mSatelliteController1.countOfSatelliteAllowedStateChangedEvents * 2;
4470         expected.countOfSuccessfulLocationQueries =
4471                 mSatelliteController1.countOfSuccessfulLocationQueries * 2;
4472         expected.countOfFailedLocationQueries =
4473                 mSatelliteController1.countOfFailedLocationQueries * 2;
4474         expected.countOfP2PSmsAvailableNotificationShown =
4475                 mSatelliteController1.countOfP2PSmsAvailableNotificationShown * 2;
4476         expected.countOfP2PSmsAvailableNotificationRemoved =
4477                 mSatelliteController1.countOfP2PSmsAvailableNotificationRemoved * 2;
4478         expected.isNtnOnlyCarrier = mSatelliteController1.isNtnOnlyCarrier;
4479         expected.versionOfSatelliteAccessConfig =
4480                 mSatelliteController1.versionOfSatelliteAccessConfig;
4481         expected.countOfIncomingDatagramTypeSosSmsSuccess =
4482                 mSatelliteController1.countOfIncomingDatagramTypeSosSmsSuccess * 2;
4483         expected.countOfIncomingDatagramTypeSosSmsFail =
4484                 mSatelliteController1.countOfIncomingDatagramTypeSosSmsFail * 2;
4485         expected.countOfOutgoingDatagramTypeSmsSuccess =
4486                 mSatelliteController1.countOfOutgoingDatagramTypeSmsSuccess * 2;
4487         expected.countOfOutgoingDatagramTypeSmsFail =
4488                 mSatelliteController1.countOfOutgoingDatagramTypeSmsFail * 2;
4489         expected.countOfIncomingDatagramTypeSmsSuccess =
4490                 mSatelliteController1.countOfIncomingDatagramTypeSmsSuccess * 2;
4491         expected.countOfIncomingDatagramTypeSmsFail =
4492                 mSatelliteController1.countOfIncomingDatagramTypeSmsFail * 2;
4493 
4494         // Service state and service switch should be added successfully
4495         verifyCurrentStateSavedToFileOnce();
4496         SatelliteController[] output =
4497                 mPersistAtomsStorage.getSatelliteControllerStats(0L);
4498         assertHasStats(output, expected, 1);
4499     }
4500 
4501     @Test
addSatelliteControllerStats_addNewCarrierId()4502     public void addSatelliteControllerStats_addNewCarrierId() throws Exception {
4503         createEmptyTestFile();
4504         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4505         mPersistAtomsStorage.addSatelliteControllerStats(mSatelliteController1);
4506         mPersistAtomsStorage.addSatelliteControllerStats(mSatelliteController2);
4507         mPersistAtomsStorage.incTimeMillis(100L);
4508 
4509         SatelliteController expected1 = mSatelliteController1;
4510         SatelliteController expected2 = mSatelliteController2;
4511 
4512         // Service state and service switch should be added successfully
4513         verifyCurrentStateSavedToFileOnce();
4514         SatelliteController[] output =
4515                 mPersistAtomsStorage.getSatelliteControllerStats(0L);
4516 
4517         assertHasStats(output, expected1, 1);
4518         assertHasStats(output, expected2, 1);
4519     }
4520 
4521     @Test
addSatelliteControllerStats_addNewProvisionId()4522     public void addSatelliteControllerStats_addNewProvisionId() throws Exception {
4523         createEmptyTestFile();
4524         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4525         mPersistAtomsStorage.addSatelliteControllerStats(mSatelliteController1);
4526         mPersistAtomsStorage.addSatelliteControllerStats(mSatelliteController2);
4527         mPersistAtomsStorage.incTimeMillis(100L);
4528 
4529         SatelliteController expected1 = mSatelliteController1;
4530         SatelliteController expected2 = mSatelliteController2;
4531 
4532         // Service state and service switch should be added successfully
4533         verifyCurrentStateSavedToFileOnce();
4534         SatelliteController[] output =
4535                 mPersistAtomsStorage.getSatelliteControllerStats(0L);
4536 
4537         assertHasStats(output, expected1, 1);
4538         assertHasStats(output, expected2, 1);
4539     }
4540 
4541     @Test
addSatelliteControllerStats_addSameDimension()4542     public void addSatelliteControllerStats_addSameDimension() throws Exception {
4543         createEmptyTestFile();
4544         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4545 
4546         SatelliteController satelliteController1 = copyOf(mSatelliteController1);
4547         SatelliteController satelliteController2 = copyOf(mSatelliteController2);
4548 
4549         // set same provisioned and carrier id so that they are in same dimension.
4550         satelliteController1.isProvisioned = true;
4551         satelliteController2.isProvisioned = true;
4552         satelliteController1.carrierId = 1589;
4553         satelliteController2.carrierId = 1589;
4554         satelliteController1.isNtnOnlyCarrier = false;
4555         satelliteController2.isNtnOnlyCarrier = false;
4556 
4557         mPersistAtomsStorage.addSatelliteControllerStats(copyOf(satelliteController1));
4558         mPersistAtomsStorage.addSatelliteControllerStats(copyOf(satelliteController2));
4559         mPersistAtomsStorage.incTimeMillis(100L);
4560 
4561         SatelliteController expected = new SatelliteController();
4562         expected.countOfSatelliteServiceEnablementsSuccess =
4563                 satelliteController1.countOfSatelliteServiceEnablementsSuccess
4564                         + satelliteController2.countOfSatelliteServiceEnablementsSuccess;
4565         expected.countOfSatelliteServiceEnablementsFail =
4566                 satelliteController1.countOfSatelliteServiceEnablementsFail
4567                         + satelliteController2.countOfSatelliteServiceEnablementsFail;
4568         expected.countOfOutgoingDatagramSuccess =
4569                 satelliteController1.countOfOutgoingDatagramSuccess
4570                         + satelliteController2.countOfOutgoingDatagramSuccess;
4571         expected.countOfOutgoingDatagramFail = satelliteController1.countOfOutgoingDatagramFail
4572                 + satelliteController2.countOfOutgoingDatagramFail;
4573         expected.countOfIncomingDatagramSuccess =
4574                 satelliteController1.countOfIncomingDatagramSuccess
4575                         + satelliteController2.countOfIncomingDatagramSuccess;
4576         expected.countOfIncomingDatagramFail = satelliteController1.countOfIncomingDatagramFail
4577                 + satelliteController2.countOfIncomingDatagramFail;
4578         expected.countOfDatagramTypeSosSmsSuccess =
4579                 satelliteController1.countOfDatagramTypeSosSmsSuccess
4580                         + satelliteController2.countOfDatagramTypeSosSmsSuccess;
4581         expected.countOfDatagramTypeSosSmsFail = satelliteController1.countOfDatagramTypeSosSmsFail
4582                 + satelliteController2.countOfDatagramTypeSosSmsFail;
4583         expected.countOfDatagramTypeLocationSharingSuccess =
4584                 satelliteController1.countOfDatagramTypeLocationSharingSuccess
4585                         + satelliteController2.countOfDatagramTypeLocationSharingSuccess;
4586         expected.countOfDatagramTypeLocationSharingFail =
4587                 satelliteController1.countOfDatagramTypeLocationSharingFail
4588                         + satelliteController2.countOfDatagramTypeLocationSharingFail;
4589         expected.countOfProvisionSuccess = satelliteController1.countOfProvisionSuccess
4590                 + satelliteController2.countOfProvisionSuccess;
4591         expected.countOfProvisionFail = satelliteController1.countOfProvisionFail
4592                 + satelliteController2.countOfProvisionFail;
4593         expected.countOfDeprovisionSuccess = satelliteController1.countOfDeprovisionSuccess
4594                 + satelliteController2.countOfDeprovisionSuccess;
4595         expected.countOfDeprovisionFail = satelliteController1.countOfDeprovisionFail
4596                 + satelliteController2.countOfDeprovisionFail;
4597         expected.totalServiceUptimeSec = satelliteController1.totalServiceUptimeSec
4598                 + satelliteController2.totalServiceUptimeSec;
4599         expected.totalBatteryConsumptionPercent =
4600                 satelliteController1.totalBatteryConsumptionPercent
4601                         + satelliteController2.totalBatteryConsumptionPercent;
4602         expected.totalBatteryChargedTimeSec = satelliteController1.totalBatteryChargedTimeSec
4603                 + satelliteController2.totalBatteryChargedTimeSec;
4604         expected.countOfDemoModeSatelliteServiceEnablementsSuccess =
4605                 satelliteController1.countOfDemoModeSatelliteServiceEnablementsSuccess
4606                         + satelliteController2.countOfDemoModeSatelliteServiceEnablementsSuccess;
4607         expected.countOfDemoModeSatelliteServiceEnablementsFail =
4608                 satelliteController1.countOfDemoModeSatelliteServiceEnablementsFail
4609                         + satelliteController2.countOfDemoModeSatelliteServiceEnablementsFail;
4610         expected.countOfDemoModeOutgoingDatagramSuccess =
4611                 satelliteController1.countOfDemoModeOutgoingDatagramSuccess
4612                         + satelliteController2.countOfDemoModeOutgoingDatagramSuccess;
4613         expected.countOfDemoModeOutgoingDatagramFail =
4614                 satelliteController1.countOfDemoModeOutgoingDatagramFail
4615                         + satelliteController2.countOfDemoModeOutgoingDatagramFail;
4616         expected.countOfDemoModeIncomingDatagramSuccess =
4617                 satelliteController1.countOfDemoModeIncomingDatagramSuccess
4618                         + satelliteController2.countOfDemoModeIncomingDatagramSuccess;
4619         expected.countOfDemoModeIncomingDatagramFail =
4620                 satelliteController1.countOfDemoModeIncomingDatagramFail
4621                         + satelliteController2.countOfDemoModeIncomingDatagramFail;
4622         expected.countOfDatagramTypeKeepAliveSuccess =
4623                 satelliteController1.countOfDatagramTypeKeepAliveSuccess
4624                         + satelliteController2.countOfDatagramTypeKeepAliveSuccess;
4625         expected.countOfDatagramTypeKeepAliveFail =
4626                 satelliteController1.countOfDatagramTypeKeepAliveFail
4627                         + satelliteController2.countOfDatagramTypeKeepAliveFail;
4628         expected.isProvisioned = true;
4629         expected.carrierId = 1589;
4630         expected.countOfSatelliteAllowedStateChangedEvents =
4631                 satelliteController1.countOfSatelliteAllowedStateChangedEvents
4632                         + satelliteController2.countOfSatelliteAllowedStateChangedEvents;
4633         expected.countOfSuccessfulLocationQueries =
4634                 satelliteController1.countOfSuccessfulLocationQueries
4635                         + satelliteController2.countOfSuccessfulLocationQueries;
4636         expected.countOfFailedLocationQueries = satelliteController1.countOfFailedLocationQueries
4637                 + satelliteController2.countOfFailedLocationQueries;
4638         expected.countOfP2PSmsAvailableNotificationShown =
4639                 satelliteController1.countOfP2PSmsAvailableNotificationShown
4640                         + satelliteController2.countOfP2PSmsAvailableNotificationShown;
4641         expected.countOfP2PSmsAvailableNotificationRemoved =
4642                 satelliteController1.countOfP2PSmsAvailableNotificationRemoved
4643                         + satelliteController2.countOfP2PSmsAvailableNotificationRemoved;
4644         expected.isNtnOnlyCarrier = false;
4645         expected.versionOfSatelliteAccessConfig =
4646                 mSatelliteController2.versionOfSatelliteAccessConfig;
4647         expected.countOfIncomingDatagramTypeSosSmsSuccess =
4648                 mSatelliteController1.countOfIncomingDatagramTypeSosSmsSuccess
4649                         + mSatelliteController2.countOfIncomingDatagramTypeSosSmsSuccess;
4650         expected.countOfIncomingDatagramTypeSosSmsFail =
4651                 mSatelliteController1.countOfIncomingDatagramTypeSosSmsFail
4652                         + mSatelliteController2.countOfIncomingDatagramTypeSosSmsFail;
4653         expected.countOfOutgoingDatagramTypeSmsSuccess =
4654                 mSatelliteController1.countOfOutgoingDatagramTypeSmsSuccess
4655                         + mSatelliteController2.countOfOutgoingDatagramTypeSmsSuccess;
4656         expected.countOfOutgoingDatagramTypeSmsFail =
4657                 mSatelliteController1.countOfOutgoingDatagramTypeSmsFail
4658                         + mSatelliteController2.countOfOutgoingDatagramTypeSmsFail;
4659         expected.countOfIncomingDatagramTypeSmsSuccess =
4660                 mSatelliteController1.countOfIncomingDatagramTypeSmsSuccess
4661                         + mSatelliteController2.countOfIncomingDatagramTypeSmsSuccess;
4662         expected.countOfIncomingDatagramTypeSmsFail =
4663                 mSatelliteController1.countOfIncomingDatagramTypeSmsFail
4664                         + mSatelliteController2.countOfIncomingDatagramTypeSmsFail;
4665 
4666         // Service state and service switch should be added successfully
4667         verifyCurrentStateSavedToFileOnce();
4668         SatelliteController[] output =
4669                 mPersistAtomsStorage.getSatelliteControllerStats(0L);
4670 
4671         assertHasStats(output, expected, 1);
4672     }
4673 
4674     @Test
getSatelliteControllerStats_tooFrequent()4675     public void getSatelliteControllerStats_tooFrequent() throws Exception {
4676         createTestFile(START_TIME_MILLIS);
4677 
4678         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4679         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
4680         SatelliteController[] output =
4681                 mPersistAtomsStorage.getSatelliteControllerStats(100L);
4682 
4683         // Should be denied
4684         assertNull(output);
4685     }
4686 
4687     @Test
addSatelliteSessionStats_emptyProto()4688     public void addSatelliteSessionStats_emptyProto() throws Exception {
4689         createEmptyTestFile();
4690         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4691         mPersistAtomsStorage.addSatelliteSessionStats(
4692                 mSatelliteSession1);
4693         mPersistAtomsStorage.incTimeMillis(100L);
4694 
4695         // Service state and service switch should be added successfully
4696         verifyCurrentStateSavedToFileOnce();
4697         SatelliteSession[] output =
4698                 mPersistAtomsStorage.getSatelliteSessionStats(0L);
4699         assertProtoArrayEquals(
4700                 new SatelliteSession[] {mSatelliteSession1}, output);
4701     }
4702 
4703     @Test
addSatelliteSessionStats_withExistingEntries()4704     public void addSatelliteSessionStats_withExistingEntries() throws Exception {
4705         createEmptyTestFile();
4706         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4707         mPersistAtomsStorage.addSatelliteSessionStats(
4708                 mSatelliteSession1);
4709         mPersistAtomsStorage.addSatelliteSessionStats(
4710                 mSatelliteSession2);
4711         mPersistAtomsStorage.incTimeMillis(100L);
4712 
4713         // Service state and service switch should be added successfully
4714         verifyCurrentStateSavedToFileOnce();
4715         SatelliteSession[] output =
4716                 mPersistAtomsStorage.getSatelliteSessionStats(0L);
4717         assertProtoArrayEqualsIgnoringOrder(
4718                 new SatelliteSession[] {
4719                         mSatelliteSession1, mSatelliteSession2},
4720                 output);
4721     }
4722 
4723     @Test
addSatelliteSessionStats_tooManyEntries()4724     public void addSatelliteSessionStats_tooManyEntries() throws Exception {
4725         createEmptyTestFile();
4726 
4727         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4728 
4729         // Store atoms up to maximum number + 1
4730         int maxCount = 15 + 1;
4731         for (int i = 0; i < maxCount; i++) {
4732             mPersistAtomsStorage
4733                     .addSatelliteSessionStats(
4734                             copyOf(mSatelliteSession1));
4735             mPersistAtomsStorage.incTimeMillis(100L);
4736         }
4737 
4738         // Store 1 different atom
4739         mPersistAtomsStorage
4740                 .addSatelliteSessionStats(mSatelliteSession2);
4741 
4742         verifyCurrentStateSavedToFileOnce();
4743 
4744         SatelliteSession[] result =
4745                 mPersistAtomsStorage.getSatelliteSessionStats(0L);
4746 
4747         // First atom has count 16, the other has 1
4748         assertHasStatsAndCount(result, mSatelliteSession1, 16);
4749         assertHasStatsAndCount(result, mSatelliteSession2, 1);
4750 
4751     }
4752 
4753     @Test
getSatelliteSessionStats_tooFrequent()4754     public void getSatelliteSessionStats_tooFrequent() throws Exception {
4755         createTestFile(START_TIME_MILLIS);
4756 
4757         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4758         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
4759         SatelliteSession[] output =
4760                 mPersistAtomsStorage.getSatelliteSessionStats(100L);
4761 
4762         // Should be denied
4763         assertNull(output);
4764     }
4765 
4766 
4767 
4768     @Test
addSatelliteIncomingDatagramStats_emptyProto()4769     public void addSatelliteIncomingDatagramStats_emptyProto() throws Exception {
4770         createEmptyTestFile();
4771         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4772         mPersistAtomsStorage.addSatelliteIncomingDatagramStats(mSatelliteIncomingDatagram1);
4773         mPersistAtomsStorage.incTimeMillis(100L);
4774 
4775         // Service state and service switch should be added successfully
4776         verifyCurrentStateSavedToFileOnce();
4777         SatelliteIncomingDatagram[] output =
4778                 mPersistAtomsStorage.getSatelliteIncomingDatagramStats(0L);
4779         assertProtoArrayEquals(
4780                 new SatelliteIncomingDatagram[] {mSatelliteIncomingDatagram1}, output);
4781     }
4782 
4783     @Test
addSatelliteIncomingDatagramStats_withExistingEntries()4784     public void addSatelliteIncomingDatagramStats_withExistingEntries() throws Exception {
4785         createEmptyTestFile();
4786         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4787         mPersistAtomsStorage.addSatelliteIncomingDatagramStats(mSatelliteIncomingDatagram1);
4788         mPersistAtomsStorage.addSatelliteIncomingDatagramStats(mSatelliteIncomingDatagram2);
4789         mPersistAtomsStorage.incTimeMillis(100L);
4790 
4791         // Service state and service switch should be added successfully
4792         verifyCurrentStateSavedToFileOnce();
4793         SatelliteIncomingDatagram[] output =
4794                 mPersistAtomsStorage.getSatelliteIncomingDatagramStats(0L);
4795         assertProtoArrayEqualsIgnoringOrder(
4796                 new SatelliteIncomingDatagram[] {
4797                         mSatelliteIncomingDatagram1, mSatelliteIncomingDatagram2}, output);
4798     }
4799 
4800     @Test
addSatelliteIncomingDatagramStats_tooManyEntries()4801     public void addSatelliteIncomingDatagramStats_tooManyEntries() throws Exception {
4802         createEmptyTestFile();
4803 
4804         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4805 
4806         // Store atoms up to maximum number + 1
4807         int maxCount = 15 + 1;
4808         for (int i = 0; i < maxCount; i++) {
4809             mPersistAtomsStorage
4810                     .addSatelliteIncomingDatagramStats(copyOf(mSatelliteIncomingDatagram1));
4811             mPersistAtomsStorage.incTimeMillis(100L);
4812         }
4813 
4814         // Store 1 different atom
4815         mPersistAtomsStorage
4816                 .addSatelliteIncomingDatagramStats(mSatelliteIncomingDatagram2);
4817 
4818         verifyCurrentStateSavedToFileOnce();
4819 
4820         SatelliteIncomingDatagram[] result =
4821                 mPersistAtomsStorage.getSatelliteIncomingDatagramStats(0L);
4822 
4823         // First atom has count 14, the other has 1
4824         assertHasStatsAndCount(result, mSatelliteIncomingDatagram1, 14);
4825         assertHasStatsAndCount(result, mSatelliteIncomingDatagram2, 1);
4826 
4827     }
4828 
4829     @Test
getSatelliteIncomingDatagramStats_tooFrequent()4830     public void getSatelliteIncomingDatagramStats_tooFrequent() throws Exception {
4831         createTestFile(START_TIME_MILLIS);
4832 
4833         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4834         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
4835         SatelliteIncomingDatagram[] output =
4836                 mPersistAtomsStorage.getSatelliteIncomingDatagramStats(100L);
4837 
4838         // Should be denied
4839         assertNull(output);
4840     }
4841 
4842     @Test
addSatelliteOutgoingDatagramStats_emptyProto()4843     public void addSatelliteOutgoingDatagramStats_emptyProto() throws Exception {
4844         createEmptyTestFile();
4845         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4846         mPersistAtomsStorage.addSatelliteOutgoingDatagramStats(mSatelliteOutgoingDatagram1);
4847         mPersistAtomsStorage.incTimeMillis(100L);
4848 
4849         // Service state and service switch should be added successfully
4850         verifyCurrentStateSavedToFileOnce();
4851         SatelliteOutgoingDatagram[] output =
4852                 mPersistAtomsStorage.getSatelliteOutgoingDatagramStats(0L);
4853         assertProtoArrayEquals(
4854                 new SatelliteOutgoingDatagram[] {mSatelliteOutgoingDatagram1}, output);
4855     }
4856 
4857     @Test
addSatelliteOutgoingDatagramStats_withExistingEntries()4858     public void addSatelliteOutgoingDatagramStats_withExistingEntries() throws Exception {
4859         createEmptyTestFile();
4860         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4861         mPersistAtomsStorage.addSatelliteOutgoingDatagramStats(mSatelliteOutgoingDatagram1);
4862         mPersistAtomsStorage.addSatelliteOutgoingDatagramStats(mSatelliteOutgoingDatagram2);
4863         mPersistAtomsStorage.incTimeMillis(100L);
4864 
4865         // Service state and service switch should be added successfully
4866         verifyCurrentStateSavedToFileOnce();
4867         SatelliteOutgoingDatagram[] output =
4868                 mPersistAtomsStorage.getSatelliteOutgoingDatagramStats(0L);
4869         assertProtoArrayEqualsIgnoringOrder(
4870                 new SatelliteOutgoingDatagram[] {
4871                         mSatelliteOutgoingDatagram1, mSatelliteOutgoingDatagram2}, output);
4872     }
4873 
4874     @Test
addSatelliteOutgoingDatagramStats_tooManyEntries()4875     public void addSatelliteOutgoingDatagramStats_tooManyEntries() throws Exception {
4876         createEmptyTestFile();
4877 
4878         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4879 
4880         // Store atoms up to maximum number + 1
4881         int maxCount = 15 + 1;
4882         for (int i = 0; i < maxCount; i++) {
4883             mPersistAtomsStorage
4884                     .addSatelliteOutgoingDatagramStats(copyOf(mSatelliteOutgoingDatagram1));
4885             mPersistAtomsStorage.incTimeMillis(100L);
4886         }
4887 
4888         // Store 1 different atom
4889         mPersistAtomsStorage
4890                 .addSatelliteOutgoingDatagramStats(mSatelliteOutgoingDatagram2);
4891 
4892         verifyCurrentStateSavedToFileOnce();
4893 
4894         SatelliteOutgoingDatagram[] result =
4895                 mPersistAtomsStorage.getSatelliteOutgoingDatagramStats(0L);
4896 
4897         // First atom has count 14, the other has 1
4898         assertHasStatsAndCount(result, mSatelliteOutgoingDatagram1, 14);
4899         assertHasStatsAndCount(result, mSatelliteOutgoingDatagram2, 1);
4900 
4901     }
4902 
4903     @Test
getSatelliteOutgoingDatagramStats_tooFrequent()4904     public void getSatelliteOutgoingDatagramStats_tooFrequent() throws Exception {
4905         createTestFile(START_TIME_MILLIS);
4906 
4907         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4908         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
4909         SatelliteOutgoingDatagram[] output =
4910                 mPersistAtomsStorage.getSatelliteOutgoingDatagramStats(100L);
4911 
4912         // Should be denied
4913         assertNull(output);
4914     }
4915 
4916     @Test
addSatelliteProvisionStats_emptyProto()4917     public void addSatelliteProvisionStats_emptyProto() throws Exception {
4918         createEmptyTestFile();
4919         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4920         mPersistAtomsStorage.addSatelliteProvisionStats(mSatelliteProvision1);
4921         mPersistAtomsStorage.incTimeMillis(100L);
4922 
4923         // Service state and service switch should be added successfully
4924         verifyCurrentStateSavedToFileOnce();
4925         SatelliteProvision[] output =
4926                 mPersistAtomsStorage.getSatelliteProvisionStats(0L);
4927         assertProtoArrayEquals(
4928                 new SatelliteProvision[] {mSatelliteProvision1}, output);
4929     }
4930 
4931     @Test
addSatelliteProvisionStats_withExistingEntries()4932     public void addSatelliteProvisionStats_withExistingEntries() throws Exception {
4933         createEmptyTestFile();
4934         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4935         mPersistAtomsStorage.addSatelliteProvisionStats(mSatelliteProvision1);
4936         mPersistAtomsStorage.addSatelliteProvisionStats(mSatelliteProvision2);
4937         mPersistAtomsStorage.incTimeMillis(100L);
4938 
4939         // Service state and service switch should be added successfully
4940         verifyCurrentStateSavedToFileOnce();
4941         SatelliteProvision[] output =
4942                 mPersistAtomsStorage.getSatelliteProvisionStats(0L);
4943         assertProtoArrayEqualsIgnoringOrder(
4944                 new SatelliteProvision[] {
4945                         mSatelliteProvision1, mSatelliteProvision2}, output);
4946     }
4947 
4948     @Test
addSatelliteProvisionStats_tooManyEntries()4949     public void addSatelliteProvisionStats_tooManyEntries() throws Exception {
4950         createEmptyTestFile();
4951 
4952         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4953 
4954         // Store atoms up to maximum number + 1
4955         int maxCount = 15 + 1;
4956         for (int i = 0; i < maxCount; i++) {
4957             mPersistAtomsStorage
4958                     .addSatelliteProvisionStats(copyOf(mSatelliteProvision1));
4959             mPersistAtomsStorage.incTimeMillis(100L);
4960         }
4961 
4962         // Store 1 different atom
4963         mPersistAtomsStorage
4964                 .addSatelliteProvisionStats(mSatelliteProvision2);
4965 
4966         verifyCurrentStateSavedToFileOnce();
4967 
4968         SatelliteProvision[] result =
4969                 mPersistAtomsStorage.getSatelliteProvisionStats(0L);
4970 
4971         // First atom has count 14, the other has 1
4972         assertHasStatsAndCount(result, mSatelliteProvision1, 14);
4973         assertHasStatsAndCount(result, mSatelliteProvision2, 1);
4974 
4975     }
4976 
4977     @Test
getSatelliteProvisionStats_tooFrequent()4978     public void getSatelliteProvisionStats_tooFrequent() throws Exception {
4979         createTestFile(START_TIME_MILLIS);
4980 
4981         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4982         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
4983         SatelliteProvision[] output =
4984                 mPersistAtomsStorage.getSatelliteProvisionStats(100L);
4985 
4986         // Should be denied
4987         assertNull(output);
4988     }
4989 
4990     @Test
addSatelliteSosMessageRecommenderStats_emptyProto()4991     public void addSatelliteSosMessageRecommenderStats_emptyProto() throws Exception {
4992         createEmptyTestFile();
4993         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
4994         mPersistAtomsStorage.addSatelliteSosMessageRecommenderStats(
4995                 mSatelliteSosMessageRecommender1);
4996         mPersistAtomsStorage.incTimeMillis(100L);
4997 
4998         // Service state and service switch should be added successfully
4999         verifyCurrentStateSavedToFileOnce();
5000         SatelliteSosMessageRecommender[] output =
5001                 mPersistAtomsStorage.getSatelliteSosMessageRecommenderStats(0L);
5002         assertProtoArrayEquals(
5003                 new SatelliteSosMessageRecommender[] {mSatelliteSosMessageRecommender1}, output);
5004     }
5005 
5006     @Test
addSatelliteSosMessageRecommenderStats_withExistingEntries()5007     public void addSatelliteSosMessageRecommenderStats_withExistingEntries() throws Exception {
5008         createEmptyTestFile();
5009         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5010         mPersistAtomsStorage.addSatelliteSosMessageRecommenderStats(
5011                 mSatelliteSosMessageRecommender1);
5012         mPersistAtomsStorage.addSatelliteSosMessageRecommenderStats(
5013                 mSatelliteSosMessageRecommender2);
5014         mPersistAtomsStorage.incTimeMillis(100L);
5015 
5016         // Service state and service switch should be added successfully
5017         verifyCurrentStateSavedToFileOnce();
5018         SatelliteSosMessageRecommender[] output =
5019                 mPersistAtomsStorage.getSatelliteSosMessageRecommenderStats(0L);
5020         assertProtoArrayEqualsIgnoringOrder(
5021                 new SatelliteSosMessageRecommender[] {
5022                         mSatelliteSosMessageRecommender1, mSatelliteSosMessageRecommender2},
5023                 output);
5024     }
5025 
5026     @Test
addSatelliteSosMessageRecommenderStats_tooManyEntries()5027     public void addSatelliteSosMessageRecommenderStats_tooManyEntries() throws Exception {
5028         createEmptyTestFile();
5029 
5030         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5031 
5032         // Store atoms up to maximum number + 1
5033         int maxCount = 15 + 1;
5034         for (int i = 0; i < maxCount; i++) {
5035             mPersistAtomsStorage
5036                     .addSatelliteSosMessageRecommenderStats(
5037                             copyOf(mSatelliteSosMessageRecommender1));
5038             mPersistAtomsStorage.incTimeMillis(100L);
5039         }
5040 
5041         // Store 1 different atom
5042         mPersistAtomsStorage
5043                 .addSatelliteSosMessageRecommenderStats(mSatelliteSosMessageRecommender2);
5044 
5045         verifyCurrentStateSavedToFileOnce();
5046 
5047         SatelliteSosMessageRecommender[] result =
5048                 mPersistAtomsStorage.getSatelliteSosMessageRecommenderStats(0L);
5049 
5050         // First atom has count 16, the other has 1
5051         assertHasStatsAndCount(result, mSatelliteSosMessageRecommender1, 16);
5052         assertHasStatsAndCount(result, mSatelliteSosMessageRecommender2, 1);
5053 
5054     }
5055 
5056     @Test
getSatelliteSosMessageRecommenderStats_tooFrequent()5057     public void getSatelliteSosMessageRecommenderStats_tooFrequent() throws Exception {
5058         createTestFile(START_TIME_MILLIS);
5059 
5060         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5061         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
5062         SatelliteSosMessageRecommender[] output =
5063                 mPersistAtomsStorage.getSatelliteSosMessageRecommenderStats(100L);
5064 
5065         // Should be denied
5066         assertNull(output);
5067     }
5068 
5069     @Test
addCarrierRoamingSatelliteSessionStats_emptyProto()5070     public void addCarrierRoamingSatelliteSessionStats_emptyProto() throws Exception {
5071         createEmptyTestFile();
5072         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5073         mPersistAtomsStorage.addCarrierRoamingSatelliteSessionStats(
5074                 mCarrierRoamingSatelliteSession1);
5075         mPersistAtomsStorage.incTimeMillis(100L);
5076 
5077         verifyCurrentStateSavedToFileOnce();
5078         CarrierRoamingSatelliteSession[] output =
5079                 mPersistAtomsStorage.getCarrierRoamingSatelliteSessionStats(0L);
5080         assertProtoArrayEquals(new CarrierRoamingSatelliteSession[] {
5081                 mCarrierRoamingSatelliteSession1}, output);
5082     }
5083 
5084     @Test
addCarrierRoamingSatelliteSessionStats_withExistingEntries()5085     public void addCarrierRoamingSatelliteSessionStats_withExistingEntries() throws Exception {
5086         createEmptyTestFile();
5087         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5088         mPersistAtomsStorage.addCarrierRoamingSatelliteSessionStats(
5089                 mCarrierRoamingSatelliteSession1);
5090         mPersistAtomsStorage.addCarrierRoamingSatelliteSessionStats(
5091                 mCarrierRoamingSatelliteSession2);
5092         mPersistAtomsStorage.incTimeMillis(100L);
5093 
5094         verifyCurrentStateSavedToFileOnce();
5095         CarrierRoamingSatelliteSession[] output =
5096                 mPersistAtomsStorage.getCarrierRoamingSatelliteSessionStats(0L);
5097         assertProtoArrayEqualsIgnoringOrder(
5098                 new CarrierRoamingSatelliteSession[] {mCarrierRoamingSatelliteSession2}, output);
5099     }
5100 
5101     @Test
addCarrierRoamingSatelliteSessionStats_tooManyEntries()5102     public void addCarrierRoamingSatelliteSessionStats_tooManyEntries() throws Exception {
5103         createEmptyTestFile();
5104 
5105         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5106 
5107         // Store atoms up to maximum number + 1
5108         int maxCount = 1 + 1;
5109         for (int i = 0; i < maxCount; i++) {
5110             mPersistAtomsStorage.addCarrierRoamingSatelliteSessionStats(
5111                     copyOf(mCarrierRoamingSatelliteSession1));
5112             mPersistAtomsStorage.incTimeMillis(100L);
5113         }
5114 
5115         // Store 1 different atom
5116         mPersistAtomsStorage.addCarrierRoamingSatelliteSessionStats(
5117                 mCarrierRoamingSatelliteSession2);
5118 
5119         verifyCurrentStateSavedToFileOnce();
5120 
5121         CarrierRoamingSatelliteSession[] result =
5122                 mPersistAtomsStorage.getCarrierRoamingSatelliteSessionStats(0L);
5123 
5124         // First atom has count 0, the other has 1
5125         assertHasStatsAndCount(result, mCarrierRoamingSatelliteSession1, 0);
5126         assertHasStatsAndCount(result, mCarrierRoamingSatelliteSession2, 1);
5127     }
5128 
5129     @Test
getCarrierRoamingSatelliteSessionStats_tooFrequent()5130     public void getCarrierRoamingSatelliteSessionStats_tooFrequent() throws Exception {
5131         createTestFile(START_TIME_MILLIS);
5132 
5133         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5134         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
5135         CarrierRoamingSatelliteSession[] output =
5136                 mPersistAtomsStorage.getCarrierRoamingSatelliteSessionStats(100L);
5137 
5138         // Should be denied
5139         assertNull(output);
5140     }
5141 
5142     @Test
getCarrierRoamingSatelliteSessionStats_withSavedAtoms()5143     public void getCarrierRoamingSatelliteSessionStats_withSavedAtoms() throws Exception {
5144         createTestFile(START_TIME_MILLIS);
5145 
5146         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5147         mPersistAtomsStorage.incTimeMillis(100L);
5148         CarrierRoamingSatelliteSession[] carrierRoamingSatelliteSessionStatsList1 =
5149                 mPersistAtomsStorage.getCarrierRoamingSatelliteSessionStats(50L);
5150         mPersistAtomsStorage.incTimeMillis(100L);
5151         CarrierRoamingSatelliteSession[] carrierRoamingSatelliteSessionStatsList2 =
5152                 mPersistAtomsStorage.getCarrierRoamingSatelliteSessionStats(50L);
5153 
5154         // First set of results should be equal to file contents.
5155         CarrierRoamingSatelliteSession[] expectedList = new CarrierRoamingSatelliteSession[] {
5156                 mCarrierRoamingSatelliteSession1, mCarrierRoamingSatelliteSession2};
5157         assertProtoArrayEqualsIgnoringOrder(expectedList, carrierRoamingSatelliteSessionStatsList1);
5158         // Second set of results should be empty.
5159         assertProtoArrayEquals(new CarrierRoamingSatelliteSession[0],
5160                 carrierRoamingSatelliteSessionStatsList2);
5161         // Corresponding pull timestamp should be updated and saved.
5162         assertEquals(START_TIME_MILLIS + 200L, mPersistAtomsStorage
5163                 .getAtomsProto().carrierRoamingSatelliteSessionPullTimestampMillis);
5164         InOrder inOrder = inOrder(mTestFileOutputStream);
5165         assertEquals(START_TIME_MILLIS + 100L,
5166                 getAtomsWritten(inOrder).carrierRoamingSatelliteSessionPullTimestampMillis);
5167         assertEquals(START_TIME_MILLIS + 200L,
5168                 getAtomsWritten(inOrder).carrierRoamingSatelliteSessionPullTimestampMillis);
5169         inOrder.verifyNoMoreInteractions();
5170     }
5171 
5172     @Test
addCarrierRoamingSatelliteControllerStats_emptyProto()5173     public void addCarrierRoamingSatelliteControllerStats_emptyProto() throws Exception {
5174         createEmptyTestFile();
5175         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5176         mPersistAtomsStorage.addCarrierRoamingSatelliteControllerStats(
5177                 mCarrierRoamingSatelliteControllerStats1);
5178         mPersistAtomsStorage.incTimeMillis(100L);
5179 
5180         verifyCurrentStateSavedToFileOnce();
5181         CarrierRoamingSatelliteControllerStats[] output =
5182                 mPersistAtomsStorage.getCarrierRoamingSatelliteControllerStats(0L);
5183         assertProtoArrayEquals(new CarrierRoamingSatelliteControllerStats[] {
5184                 mCarrierRoamingSatelliteControllerStats1}, output);
5185     }
5186 
5187     @Test
addCarrierRoamingSatelliteControllerStats_withExistingCarrierId()5188     public void addCarrierRoamingSatelliteControllerStats_withExistingCarrierId() throws Exception {
5189         createEmptyTestFile();
5190         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5191         mPersistAtomsStorage.addCarrierRoamingSatelliteControllerStats(
5192                 copyOf(mCarrierRoamingSatelliteControllerStats1));
5193         mPersistAtomsStorage.addCarrierRoamingSatelliteControllerStats(
5194                 copyOf(mCarrierRoamingSatelliteControllerStats1));
5195         mPersistAtomsStorage.incTimeMillis(100L);
5196 
5197         CarrierRoamingSatelliteControllerStats expected =
5198                 new CarrierRoamingSatelliteControllerStats();
5199         expected.configDataSource = mCarrierRoamingSatelliteControllerStats1.configDataSource;
5200         expected.countOfEntitlementStatusQueryRequest =
5201                 mCarrierRoamingSatelliteControllerStats1.countOfEntitlementStatusQueryRequest * 2;
5202         expected.countOfSatelliteConfigUpdateRequest =
5203                 mCarrierRoamingSatelliteControllerStats1.countOfSatelliteConfigUpdateRequest * 2;
5204         expected.countOfSatelliteNotificationDisplayed =
5205                 mCarrierRoamingSatelliteControllerStats1.countOfSatelliteNotificationDisplayed * 2;
5206         expected.satelliteSessionGapMinSec =
5207                 mCarrierRoamingSatelliteControllerStats1.satelliteSessionGapMinSec;
5208         expected.satelliteSessionGapAvgSec =
5209                 mCarrierRoamingSatelliteControllerStats1.satelliteSessionGapAvgSec;
5210         expected.satelliteSessionGapMaxSec =
5211                 mCarrierRoamingSatelliteControllerStats1.satelliteSessionGapMaxSec;
5212         expected.carrierId = mCarrierRoamingSatelliteControllerStats1.carrierId;
5213         expected.isDeviceEntitled = mCarrierRoamingSatelliteControllerStats1.isDeviceEntitled;
5214         expected.isMultiSim = mCarrierRoamingSatelliteControllerStats1.isMultiSim;
5215         expected.countOfSatelliteSessions =
5216                 mCarrierRoamingSatelliteControllerStats1.countOfSatelliteSessions * 2;
5217         verifyCurrentStateSavedToFileOnce();
5218         CarrierRoamingSatelliteControllerStats[] output =
5219                 mPersistAtomsStorage.getCarrierRoamingSatelliteControllerStats(0L);
5220         assertHasStats(output, expected, 1);
5221     }
5222 
5223     @Test
addCarrierRoamingSatelliteControllerStats_addNewCarrierId()5224     public void addCarrierRoamingSatelliteControllerStats_addNewCarrierId() throws Exception {
5225         createEmptyTestFile();
5226         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5227         mPersistAtomsStorage.addCarrierRoamingSatelliteControllerStats(
5228                 copyOf(mCarrierRoamingSatelliteControllerStats1));
5229         mPersistAtomsStorage.addCarrierRoamingSatelliteControllerStats(
5230                 copyOf(mCarrierRoamingSatelliteControllerStats2));
5231         mPersistAtomsStorage.incTimeMillis(100L);
5232 
5233         CarrierRoamingSatelliteControllerStats expected1 = mCarrierRoamingSatelliteControllerStats1;
5234         CarrierRoamingSatelliteControllerStats expected2 = mCarrierRoamingSatelliteControllerStats2;
5235 
5236         CarrierRoamingSatelliteControllerStats[] output =
5237                 mPersistAtomsStorage.getCarrierRoamingSatelliteControllerStats(0L);
5238 
5239         assertHasStats(output, expected1, 1);
5240         assertHasStats(output, expected2, 1);
5241     }
5242 
5243     @Test
getCarrierRoamingSatelliteControllerStats_tooFrequent()5244     public void getCarrierRoamingSatelliteControllerStats_tooFrequent() throws Exception {
5245         createTestFile(START_TIME_MILLIS);
5246 
5247         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5248         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
5249         CarrierRoamingSatelliteControllerStats[] output =
5250                 mPersistAtomsStorage.getCarrierRoamingSatelliteControllerStats(100L);
5251 
5252         // Should be denied
5253         assertNull(output);
5254     }
5255 
5256 
5257     @Test
addSatelliteEntitlementStats_emptyProto()5258     public void addSatelliteEntitlementStats_emptyProto() throws Exception {
5259         createEmptyTestFile();
5260         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5261         mPersistAtomsStorage.addSatelliteEntitlementStats(mSatelliteEntitlement1);
5262         mPersistAtomsStorage.incTimeMillis(100L);
5263 
5264         verifyCurrentStateSavedToFileOnce();
5265         SatelliteEntitlement[] output =
5266                 mPersistAtomsStorage.getSatelliteEntitlementStats(0L);
5267         assertProtoArrayEquals(new SatelliteEntitlement[] {mSatelliteEntitlement1}, output);
5268     }
5269 
5270     @Test
addSatelliteEntitlementStats_withExistingEntries()5271     public void addSatelliteEntitlementStats_withExistingEntries() throws Exception {
5272         createEmptyTestFile();
5273         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5274         mPersistAtomsStorage.addSatelliteEntitlementStats(mSatelliteEntitlement1);
5275         mPersistAtomsStorage.addSatelliteEntitlementStats(mSatelliteEntitlement2);
5276         mPersistAtomsStorage.incTimeMillis(100L);
5277 
5278         verifyCurrentStateSavedToFileOnce();
5279         SatelliteEntitlement[] output =
5280                 mPersistAtomsStorage.getSatelliteEntitlementStats(0L);
5281         assertProtoArrayEqualsIgnoringOrder(
5282                 new SatelliteEntitlement[] {
5283                         mSatelliteEntitlement1, mSatelliteEntitlement2}, output);
5284     }
5285 
5286     @Test
addSatelliteEntitlementStats_updateExistingEntries()5287     public void addSatelliteEntitlementStats_updateExistingEntries() throws Exception {
5288         createTestFile(START_TIME_MILLIS);
5289 
5290         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5291         mPersistAtomsStorage.addSatelliteEntitlementStats(copyOf(mSatelliteEntitlement1));
5292         mPersistAtomsStorage.incTimeMillis(100L);
5293 
5294         // Count should be increased by 1.
5295         verifyCurrentStateSavedToFileOnce();
5296         SatelliteEntitlement newSatelliteEntitlement1 = copyOf(mSatelliteEntitlement1);
5297         newSatelliteEntitlement1.count = 2;
5298         SatelliteEntitlement[] expectedList = new SatelliteEntitlement[] {newSatelliteEntitlement1,
5299                 mSatelliteEntitlement2};
5300         assertProtoArrayEqualsIgnoringOrder(expectedList,
5301                 mPersistAtomsStorage.getSatelliteEntitlementStats(0L));
5302     }
5303 
5304     @Test
addSatelliteEntitlementStats_tooManyEntries()5305     public void addSatelliteEntitlementStats_tooManyEntries() throws Exception {
5306         createEmptyTestFile();
5307 
5308         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5309 
5310         // Store atoms up to maximum number + 1
5311         int maxCount = 15 + 1;
5312         for (int i = 0; i < maxCount; i++) {
5313             mPersistAtomsStorage.addSatelliteEntitlementStats(mSatelliteEntitlement1);
5314             mPersistAtomsStorage.incTimeMillis(100L);
5315         }
5316 
5317         // Store 1 different atom
5318         mPersistAtomsStorage.addSatelliteEntitlementStats(mSatelliteEntitlement2);
5319 
5320         verifyCurrentStateSavedToFileOnce();
5321 
5322         SatelliteEntitlement[] result =
5323                 mPersistAtomsStorage.getSatelliteEntitlementStats(0L);
5324 
5325         // First atom has count 14, the other has 1
5326         assertHasStatsAndCount(result, mSatelliteEntitlement1, 16);
5327         assertHasStatsAndCount(result, mSatelliteEntitlement2, 1);
5328     }
5329 
5330     @Test
getSatelliteEntitlementStats_tooFrequent()5331     public void getSatelliteEntitlementStats_tooFrequent() throws Exception {
5332         createTestFile(START_TIME_MILLIS);
5333 
5334         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5335         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
5336         SatelliteEntitlement[] output =
5337                 mPersistAtomsStorage.getSatelliteEntitlementStats(100L);
5338 
5339         // Should be denied
5340         assertNull(output);
5341     }
5342 
5343     @Test
getSatelliteEntitlementStats_withSavedAtoms()5344     public void getSatelliteEntitlementStats_withSavedAtoms() throws Exception {
5345         createTestFile(START_TIME_MILLIS);
5346 
5347         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5348         mPersistAtomsStorage.incTimeMillis(100L);
5349         SatelliteEntitlement[] satelliteEntitlementStatsList1 =
5350                 mPersistAtomsStorage.getSatelliteEntitlementStats(50L);
5351         mPersistAtomsStorage.incTimeMillis(100L);
5352         SatelliteEntitlement[] satelliteEntitlementStatsList2 =
5353                 mPersistAtomsStorage.getSatelliteEntitlementStats(50L);
5354 
5355         // First set of results should be equal to file contents.
5356         SatelliteEntitlement[] expectedList = new SatelliteEntitlement[] {
5357                 mSatelliteEntitlement1, mSatelliteEntitlement2};
5358         assertProtoArrayEqualsIgnoringOrder(expectedList, satelliteEntitlementStatsList1);
5359         // Second set of results should be empty.
5360         assertProtoArrayEquals(new SatelliteEntitlement[0], satelliteEntitlementStatsList2);
5361         // Corresponding pull timestamp should be updated and saved.
5362         assertEquals(START_TIME_MILLIS + 200L, mPersistAtomsStorage
5363                 .getAtomsProto().satelliteEntitlementPullTimestampMillis);
5364         InOrder inOrder = inOrder(mTestFileOutputStream);
5365         assertEquals(START_TIME_MILLIS + 100L,
5366                 getAtomsWritten(inOrder).satelliteEntitlementPullTimestampMillis);
5367         assertEquals(START_TIME_MILLIS + 200L,
5368                 getAtomsWritten(inOrder).satelliteEntitlementPullTimestampMillis);
5369         inOrder.verifyNoMoreInteractions();
5370     }
5371 
5372     @Test
addSatelliteConfigUpdaterStats_emptyProto()5373     public void addSatelliteConfigUpdaterStats_emptyProto() throws Exception {
5374         createEmptyTestFile();
5375         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5376         mPersistAtomsStorage.addSatelliteConfigUpdaterStats(mSatelliteConfigUpdater1);
5377         mPersistAtomsStorage.incTimeMillis(100L);
5378 
5379         verifyCurrentStateSavedToFileOnce();
5380         SatelliteConfigUpdater[] output =
5381                 mPersistAtomsStorage.getSatelliteConfigUpdaterStats(0L);
5382         assertProtoArrayEquals(new SatelliteConfigUpdater[] {mSatelliteConfigUpdater1}, output);
5383     }
5384 
5385     @Test
addSatelliteConfigUpdaterStats_withExistingEntries()5386     public void addSatelliteConfigUpdaterStats_withExistingEntries() throws Exception {
5387         createEmptyTestFile();
5388         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5389         mPersistAtomsStorage.addSatelliteConfigUpdaterStats(mSatelliteConfigUpdater1);
5390         mPersistAtomsStorage.addSatelliteConfigUpdaterStats(mSatelliteConfigUpdater2);
5391         mPersistAtomsStorage.incTimeMillis(100L);
5392 
5393         verifyCurrentStateSavedToFileOnce();
5394         SatelliteConfigUpdater[] output =
5395                 mPersistAtomsStorage.getSatelliteConfigUpdaterStats(0L);
5396         assertProtoArrayEqualsIgnoringOrder(new SatelliteConfigUpdater[] {
5397                 mSatelliteConfigUpdater1, mSatelliteConfigUpdater2}, output);
5398     }
5399 
5400     @Test
addSatelliteConfigUpdaterStats_updateExistingEntries()5401     public void addSatelliteConfigUpdaterStats_updateExistingEntries() throws Exception {
5402         createTestFile(START_TIME_MILLIS);
5403 
5404         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5405         mPersistAtomsStorage.addSatelliteConfigUpdaterStats(copyOf(mSatelliteConfigUpdater1));
5406         mPersistAtomsStorage.incTimeMillis(100L);
5407 
5408         // Count should be increased by 1.
5409         verifyCurrentStateSavedToFileOnce();
5410         SatelliteConfigUpdater newSatelliteConfigUpdater1 = copyOf(mSatelliteConfigUpdater1);
5411         newSatelliteConfigUpdater1.count = 2;
5412         SatelliteConfigUpdater[] expectedList = new SatelliteConfigUpdater[] {
5413                 newSatelliteConfigUpdater1, mSatelliteConfigUpdater2};
5414         assertProtoArrayEqualsIgnoringOrder(expectedList,
5415                 mPersistAtomsStorage.getSatelliteConfigUpdaterStats(0L));
5416     }
5417 
5418     @Test
addSatelliteConfigUpdaterStats_tooManyEntries()5419     public void addSatelliteConfigUpdaterStats_tooManyEntries() throws Exception {
5420         createEmptyTestFile();
5421 
5422         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5423 
5424         // Store atoms up to maximum number + 1
5425         int maxCount = 15 + 1;
5426         for (int i = 0; i < maxCount; i++) {
5427             mPersistAtomsStorage.addSatelliteConfigUpdaterStats(mSatelliteConfigUpdater1);
5428             mPersistAtomsStorage.incTimeMillis(100L);
5429         }
5430 
5431         // Store 1 different atom
5432         mPersistAtomsStorage.addSatelliteConfigUpdaterStats(mSatelliteConfigUpdater2);
5433 
5434         verifyCurrentStateSavedToFileOnce();
5435 
5436         SatelliteConfigUpdater[] result =
5437                 mPersistAtomsStorage.getSatelliteConfigUpdaterStats(0L);
5438 
5439         // First atom has count 14, the other has 1
5440         assertHasStatsAndCount(result, mSatelliteConfigUpdater1, 16);
5441         assertHasStatsAndCount(result, mSatelliteConfigUpdater2, 1);
5442     }
5443 
5444     @Test
getSatelliteConfigUpdaterStats_tooFrequent()5445     public void getSatelliteConfigUpdaterStats_tooFrequent() throws Exception {
5446         createTestFile(START_TIME_MILLIS);
5447 
5448         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5449         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
5450         SatelliteConfigUpdater[] output =
5451                 mPersistAtomsStorage.getSatelliteConfigUpdaterStats(100L);
5452 
5453         // Should be denied
5454         assertNull(output);
5455     }
5456 
5457     @Test
getSatelliteConfigUpdaterStats_withSavedAtoms()5458     public void getSatelliteConfigUpdaterStats_withSavedAtoms() throws Exception {
5459         createTestFile(START_TIME_MILLIS);
5460 
5461         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5462         mPersistAtomsStorage.incTimeMillis(100L);
5463         SatelliteConfigUpdater[] satelliteConfigUpdaterStatsList1 =
5464                 mPersistAtomsStorage.getSatelliteConfigUpdaterStats(50L);
5465         mPersistAtomsStorage.incTimeMillis(100L);
5466         SatelliteConfigUpdater[] satelliteConfigUpdaterStatsList2 =
5467                 mPersistAtomsStorage.getSatelliteConfigUpdaterStats(50L);
5468 
5469         // First set of results should be equal to file contents.
5470         SatelliteConfigUpdater[] expectedList = new SatelliteConfigUpdater[] {
5471                 mSatelliteConfigUpdater1, mSatelliteConfigUpdater2};
5472         assertProtoArrayEqualsIgnoringOrder(expectedList, satelliteConfigUpdaterStatsList1);
5473         // Second set of results should be empty.
5474         assertProtoArrayEquals(new SatelliteConfigUpdater[0], satelliteConfigUpdaterStatsList2);
5475         // Corresponding pull timestamp should be updated and saved.
5476         assertEquals(START_TIME_MILLIS + 200L, mPersistAtomsStorage
5477                 .getAtomsProto().satelliteConfigUpdaterPullTimestampMillis);
5478         InOrder inOrder = inOrder(mTestFileOutputStream);
5479         assertEquals(START_TIME_MILLIS + 100L,
5480                 getAtomsWritten(inOrder).satelliteConfigUpdaterPullTimestampMillis);
5481         assertEquals(START_TIME_MILLIS + 200L,
5482                 getAtomsWritten(inOrder).satelliteConfigUpdaterPullTimestampMillis);
5483         inOrder.verifyNoMoreInteractions();
5484     }
5485 
5486     @Test
addSatelliteAccessControllerStats_emptyProto()5487     public void addSatelliteAccessControllerStats_emptyProto() throws Exception {
5488         createEmptyTestFile();
5489         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5490         mPersistAtomsStorage.addSatelliteAccessControllerStats(
5491                 mSatelliteAccessController1);
5492         mPersistAtomsStorage.incTimeMillis(100L);
5493 
5494         // Service state and service switch should be added successfully
5495         verifyCurrentStateSavedToFileOnce();
5496         SatelliteAccessController[] output =
5497                 mPersistAtomsStorage.getSatelliteAccessControllerStats(0L);
5498         assertProtoArrayEquals(
5499                 new SatelliteAccessController[] {mSatelliteAccessController1}, output);
5500     }
5501 
5502     @Test
addSatelliteAccessControllerStats_tooManyEntries()5503     public void addSatelliteAccessControllerStats_tooManyEntries() throws Exception {
5504         createEmptyTestFile();
5505         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5506 
5507         // Store atoms up to maximum number + 1
5508         int maxCount = 15 + 1;
5509         for (int i = 0; i < maxCount; i++) {
5510             mPersistAtomsStorage
5511                     .addSatelliteAccessControllerStats(//mSatelliteAccessController1);
5512                             copyOf(mSatelliteAccessController1));
5513             mPersistAtomsStorage.incTimeMillis(100L);
5514         }
5515 
5516         // Store 1 different atom
5517         mPersistAtomsStorage
5518                 .addSatelliteAccessControllerStats(mSatelliteAccessController2);
5519         mPersistAtomsStorage.incTimeMillis(100L);
5520 
5521         verifyCurrentStateSavedToFileOnce();
5522 
5523         SatelliteAccessController[] result =
5524                 mPersistAtomsStorage.getSatelliteAccessControllerStats(0L);
5525         // First atom should have count 14, the other should have 1
5526         assertHasStatsAndCount(result, mSatelliteAccessController1, 14);
5527         assertHasStatsAndCount(result, mSatelliteAccessController2, 1);
5528     }
5529 
5530     @Test
getSatelliteAccessControllerStats_tooFrequent()5531     public void getSatelliteAccessControllerStats_tooFrequent() throws Exception {
5532         createTestFile(START_TIME_MILLIS);
5533 
5534         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5535         mPersistAtomsStorage.incTimeMillis(50L); // pull interval less than minimum
5536         SatelliteAccessController[] output =
5537                 mPersistAtomsStorage.getSatelliteAccessControllerStats(100L);
5538 
5539         // Should be denied
5540         assertNull(output);
5541     }
5542 
5543     @Test
5544     @SmallTest
addDataNetworkValidation_newEntry()5545     public void addDataNetworkValidation_newEntry() throws Exception {
5546         createEmptyTestFile();
5547         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5548 
5549         mPersistAtomsStorage.addDataNetworkValidation(mDataNetworkValidationLte1);
5550         mPersistAtomsStorage.addDataNetworkValidation(mDataNetworkValidationIwlan1);
5551         mPersistAtomsStorage.incTimeMillis(100L);
5552 
5553         // There should be 2 DataNetworkValidation
5554         verifyCurrentStateSavedToFileOnce();
5555         DataNetworkValidation[] output = mPersistAtomsStorage.getDataNetworkValidation(0L);
5556         assertProtoArrayEqualsIgnoringOrder(
5557                 new DataNetworkValidation[] {
5558                         mDataNetworkValidationLte1, mDataNetworkValidationIwlan1},
5559                 output);
5560     }
5561 
5562     @Test
addDataNetworkValidation_existingEntry()5563     public void addDataNetworkValidation_existingEntry() throws Exception {
5564         createEmptyTestFile();
5565         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5566 
5567         int expectedNetworkValidationCount =
5568                 mDataNetworkValidationLte1.networkValidationCount
5569                         + mDataNetworkValidationLte2.networkValidationCount;
5570 
5571         mPersistAtomsStorage.addDataNetworkValidation(mDataNetworkValidationLte1);
5572         mPersistAtomsStorage.addDataNetworkValidation(mDataNetworkValidationLte2);
5573         mPersistAtomsStorage.incTimeMillis(100L);
5574 
5575         DataNetworkValidation expected = copyOf(mDataNetworkValidationLte1);
5576         expected.networkValidationCount = expectedNetworkValidationCount;
5577 
5578         // There should be 1 DataNetworkValidation
5579         verifyCurrentStateSavedToFileOnce();
5580         DataNetworkValidation[] output =
5581                 mPersistAtomsStorage.getDataNetworkValidation(0L);
5582         assertProtoArrayEqualsIgnoringOrder(
5583                 new DataNetworkValidation[] {expected},
5584                 output);
5585     }
5586 
5587     @Test
addDataNetworkValidation_tooManyEntries()5588     public void addDataNetworkValidation_tooManyEntries() throws Exception {
5589 
5590         // Set up
5591         doReturn(false).when(mPackageManager).hasSystemFeature(anyString());
5592 
5593         createEmptyTestFile();
5594         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5595 
5596         // Currently, the maximum number that can be stored for DataNetworkValidation in Atom
5597         // storage is 15. Try saving excess.
5598         int maxNumDataNetworkValidation = 20;
5599         mPersistAtomsStorage.addDataNetworkValidation(mDataNetworkValidationLte1);
5600         for (int i = 0; i < maxNumDataNetworkValidation; i++) {
5601             DataNetworkValidation copied = copyOf(mDataNetworkValidationLte1);
5602             copied.apnTypeBitmask = mDataNetworkValidationLte1.apnTypeBitmask + i;
5603             mPersistAtomsStorage.addDataNetworkValidation(copied);
5604         }
5605         mPersistAtomsStorage.incTimeMillis(100L);
5606 
5607         // There should be less than or equal to maxNumDataNetworkValidation
5608         verifyCurrentStateSavedToFileOnce();
5609         DataNetworkValidation[] output =
5610                 mPersistAtomsStorage.getDataNetworkValidation(0L);
5611         assertTrue(output.length <= maxNumDataNetworkValidation);
5612     }
5613 
5614     @Test
5615     @SmallTest
clearAtoms()5616     public void clearAtoms() throws Exception {
5617         createTestFile(START_TIME_MILLIS);
5618         mPersistAtomsStorage = new TestablePersistAtomsStorage(mContext);
5619         mPersistAtomsStorage.addCompleteSipTransportSession(copyOf(mSipTransportSession1));
5620         mPersistAtomsStorage.incTimeMillis(100L);
5621         verifyCurrentStateSavedToFileOnce();
5622 
5623         mPersistAtomsStorage.addUceEventStats(mUceEventStats1);
5624         mPersistAtomsStorage.incTimeMillis(100L);
5625         verifyCurrentStateSavedToFileOnce();
5626 
5627         mPersistAtomsStorage.clearAtoms();
5628         verifyCurrentStateSavedToFileOnce();
5629         UceEventStats[] uceEventStats = mPersistAtomsStorage.getUceEventStats(0L);
5630         assertEquals(null, uceEventStats);
5631         SipTransportSession[] sipTransportSession = mPersistAtomsStorage.getSipTransportSession(0L);
5632         assertEquals(null, sipTransportSession);
5633     }
5634 
5635     /* Utilities */
5636 
createEmptyTestFile()5637     private void createEmptyTestFile() throws Exception {
5638         PersistAtoms atoms = new PersistAtoms();
5639         FileOutputStream stream = new FileOutputStream(mTestFile);
5640         stream.write(PersistAtoms.toByteArray(atoms));
5641         stream.close();
5642     }
5643 
createTestFile(long lastPullTimeMillis)5644     private void createTestFile(long lastPullTimeMillis) throws Exception {
5645         PersistAtoms atoms = new PersistAtoms();
5646         atoms.buildFingerprint = Build.FINGERPRINT;
5647         atoms.voiceCallRatUsagePullTimestampMillis = lastPullTimeMillis;
5648         atoms.voiceCallRatUsage = mVoiceCallRatUsages;
5649         atoms.voiceCallSessionPullTimestampMillis = lastPullTimeMillis;
5650         atoms.voiceCallSession = mVoiceCallSessions;
5651         atoms.cellularServiceStatePullTimestampMillis = lastPullTimeMillis;
5652         atoms.cellularServiceState = mServiceStates;
5653         atoms.cellularDataServiceSwitchPullTimestampMillis = lastPullTimeMillis;
5654         atoms.cellularDataServiceSwitch = mServiceSwitches;
5655         atoms.imsRegistrationStatsPullTimestampMillis = lastPullTimeMillis;
5656         atoms.imsRegistrationStats = mImsRegistrationStats;
5657         atoms.imsRegistrationTerminationPullTimestampMillis = lastPullTimeMillis;
5658         atoms.imsRegistrationTermination = mImsRegistrationTerminations;
5659         atoms.imsRegistrationFeatureTagStatsPullTimestampMillis = lastPullTimeMillis;
5660         atoms.imsRegistrationFeatureTagStats = mImsRegistrationFeatureTagStatses;
5661         atoms.rcsClientProvisioningStatsPullTimestampMillis = lastPullTimeMillis;
5662         atoms.rcsClientProvisioningStats = mRcsClientProvisioningStatses;
5663         atoms.rcsAcsProvisioningStatsPullTimestampMillis = lastPullTimeMillis;
5664         atoms.rcsAcsProvisioningStats = mRcsAcsProvisioningStatses;
5665         atoms.imsRegistrationServiceDescStatsPullTimestampMillis = lastPullTimeMillis;
5666         atoms.imsRegistrationServiceDescStats = mImsRegistrationServiceDescStats;
5667         atoms.imsDedicatedBearerListenerEventPullTimestampMillis = lastPullTimeMillis;
5668         atoms.imsDedicatedBearerListenerEvent = mImsDedicatedBearerListenerEvents;
5669         atoms.imsDedicatedBearerEventPullTimestampMillis = lastPullTimeMillis;
5670         atoms.imsDedicatedBearerEvent = mImsDedicatedBearerEvents;
5671         atoms.uceEventStatsPullTimestampMillis = lastPullTimeMillis;
5672         atoms.uceEventStats = mUceEventStatses;
5673         atoms.presenceNotifyEventPullTimestampMillis = lastPullTimeMillis;
5674         atoms.presenceNotifyEvent = mPresenceNotifyEvents;
5675         atoms.sipTransportFeatureTagStatsPullTimestampMillis = lastPullTimeMillis;
5676         atoms.sipTransportFeatureTagStats = mSipTransportFeatureTagStatsArray;
5677         atoms.sipDelegateStatsPullTimestampMillis = lastPullTimeMillis;
5678         atoms.sipDelegateStats = mSipDelegateStatsArray;
5679         atoms.gbaEventPullTimestampMillis = lastPullTimeMillis;
5680         atoms.gbaEvent = mGbaEvent;
5681         atoms.sipMessageResponsePullTimestampMillis = lastPullTimeMillis;
5682         atoms.sipMessageResponse = mSipMessageResponse;
5683         atoms.sipTransportSessionPullTimestampMillis = lastPullTimeMillis;
5684         atoms.sipTransportSession = mSipTransportSession;
5685         atoms.outgoingShortCodeSms = mOutgoingShortCodeSms;
5686         atoms.outgoingShortCodeSmsPullTimestampMillis = lastPullTimeMillis;
5687         atoms.satelliteController = mSatelliteControllers;
5688         atoms.satelliteControllerPullTimestampMillis = lastPullTimeMillis;
5689         atoms.satelliteSession = mSatelliteSessions;
5690         atoms.satelliteSessionPullTimestampMillis = lastPullTimeMillis;
5691         atoms.satelliteIncomingDatagram = mSatelliteIncomingDatagrams;
5692         atoms.satelliteIncomingDatagramPullTimestampMillis = lastPullTimeMillis;
5693         atoms.satelliteOutgoingDatagram = mSatelliteOutgoingDatagrams;
5694         atoms.satelliteOutgoingDatagramPullTimestampMillis = lastPullTimeMillis;
5695         atoms.satelliteProvision = mSatelliteProvisions;
5696         atoms.satelliteProvisionPullTimestampMillis = lastPullTimeMillis;
5697         atoms.satelliteSosMessageRecommender = mSatelliteSosMessageRecommenders;
5698         atoms.satelliteSosMessageRecommenderPullTimestampMillis = lastPullTimeMillis;
5699         atoms.dataNetworkValidation = mDataNetworkValidations;
5700         atoms.dataNetworkValidationPullTimestampMillis = lastPullTimeMillis;
5701         atoms.carrierRoamingSatelliteSession = mCarrierRoamingSatelliteSessions;
5702         atoms.carrierRoamingSatelliteSessionPullTimestampMillis = lastPullTimeMillis;
5703         atoms.carrierRoamingSatelliteControllerStats = mCarrierRoamingSatelliteControllerStats;
5704         atoms.carrierRoamingSatelliteControllerStatsPullTimestampMillis = lastPullTimeMillis;
5705         atoms.satelliteEntitlement = mSatelliteEntitlements;
5706         atoms.satelliteEntitlementPullTimestampMillis = lastPullTimeMillis;
5707         atoms.satelliteConfigUpdater = mSatelliteConfigUpdaters;
5708         atoms.satelliteConfigUpdaterPullTimestampMillis = lastPullTimeMillis;
5709         atoms.satelliteAccessControllerPullTimestampMillis = lastPullTimeMillis;
5710         FileOutputStream stream = new FileOutputStream(mTestFile);
5711         stream.write(PersistAtoms.toByteArray(atoms));
5712         stream.close();
5713     }
5714 
getAtomsWritten(@ullable InOrder inOrder)5715     private PersistAtoms getAtomsWritten(@Nullable InOrder inOrder) throws Exception {
5716         if (inOrder == null) {
5717             inOrder = inOrder(mTestFileOutputStream);
5718         }
5719         ArgumentCaptor bytesCaptor = ArgumentCaptor.forClass(Object.class);
5720         inOrder.verify(mTestFileOutputStream, times(1)).write((byte[]) bytesCaptor.capture());
5721         PersistAtoms savedAtoms = PersistAtoms.parseFrom((byte[]) bytesCaptor.getValue());
5722         inOrder.verify(mTestFileOutputStream, times(1)).close();
5723         return savedAtoms;
5724     }
5725 
addRepeatedCalls( PersistAtomsStorage storage, VoiceCallSession call, int count)5726     private static void addRepeatedCalls(
5727             PersistAtomsStorage storage, VoiceCallSession call, int count) {
5728         for (int i = 0; i < count; i++) {
5729             storage.addVoiceCallSession(call);
5730         }
5731     }
5732 
multiplyVoiceCallRatUsage( VoiceCallRatUsage[] usages, int times)5733     private static VoiceCallRatUsage[] multiplyVoiceCallRatUsage(
5734             VoiceCallRatUsage[] usages, int times) {
5735         VoiceCallRatUsage[] multipliedUsages = new VoiceCallRatUsage[usages.length];
5736         for (int i = 0; i < usages.length; i++) {
5737             multipliedUsages[i] = new VoiceCallRatUsage();
5738             multipliedUsages[i].carrierId = usages[i].carrierId;
5739             multipliedUsages[i].rat = usages[i].rat;
5740             multipliedUsages[i].callCount = usages[i].callCount * 2;
5741             multipliedUsages[i].totalDurationMillis = usages[i].totalDurationMillis * 2;
5742         }
5743         return multipliedUsages;
5744     }
5745 
copyOf(CellularServiceState source)5746     private static CellularServiceState copyOf(CellularServiceState source) throws Exception {
5747         return CellularServiceState.parseFrom(MessageNano.toByteArray(source));
5748     }
5749 
copyOf(CellularDataServiceSwitch source)5750     private static CellularDataServiceSwitch copyOf(CellularDataServiceSwitch source)
5751             throws Exception {
5752         return CellularDataServiceSwitch.parseFrom(MessageNano.toByteArray(source));
5753     }
5754 
copyOf(ImsRegistrationStats source)5755     private static ImsRegistrationStats copyOf(ImsRegistrationStats source) throws Exception {
5756         return ImsRegistrationStats.parseFrom(MessageNano.toByteArray(source));
5757     }
5758 
copyOf(ImsRegistrationTermination source)5759     private static ImsRegistrationTermination copyOf(ImsRegistrationTermination source)
5760             throws Exception {
5761         return ImsRegistrationTermination.parseFrom(MessageNano.toByteArray(source));
5762     }
5763 
copyOf(DataCallSession source)5764     private static DataCallSession copyOf(DataCallSession source)
5765             throws Exception {
5766         return DataCallSession.parseFrom(MessageNano.toByteArray(source));
5767     }
5768 
copyOf(ImsRegistrationFeatureTagStats source)5769     private static ImsRegistrationFeatureTagStats copyOf(ImsRegistrationFeatureTagStats source)
5770             throws Exception {
5771         return ImsRegistrationFeatureTagStats.parseFrom(MessageNano.toByteArray(source));
5772     }
5773 
copyOf(RcsAcsProvisioningStats source)5774     private static RcsAcsProvisioningStats copyOf(RcsAcsProvisioningStats source)
5775             throws Exception {
5776         return RcsAcsProvisioningStats.parseFrom(MessageNano.toByteArray(source));
5777     }
5778 
copyOf(ImsRegistrationServiceDescStats source)5779     private static ImsRegistrationServiceDescStats copyOf(ImsRegistrationServiceDescStats source)
5780             throws Exception {
5781         return ImsRegistrationServiceDescStats.parseFrom(MessageNano.toByteArray(source));
5782     }
5783 
copyOf(ImsDedicatedBearerListenerEvent source)5784     private static ImsDedicatedBearerListenerEvent copyOf(ImsDedicatedBearerListenerEvent source)
5785             throws Exception {
5786         return ImsDedicatedBearerListenerEvent.parseFrom(MessageNano.toByteArray(source));
5787     }
5788 
copyOf(ImsDedicatedBearerEvent source)5789     private static ImsDedicatedBearerEvent copyOf(ImsDedicatedBearerEvent source)
5790             throws Exception {
5791         return ImsDedicatedBearerEvent.parseFrom(MessageNano.toByteArray(source));
5792     }
5793 
copyOf(UceEventStats source)5794     private static UceEventStats copyOf(UceEventStats source)
5795             throws Exception {
5796         return UceEventStats.parseFrom(MessageNano.toByteArray(source));
5797     }
5798 
copyOf(PresenceNotifyEvent source)5799     private static PresenceNotifyEvent copyOf(PresenceNotifyEvent source)
5800             throws Exception {
5801         return PresenceNotifyEvent.parseFrom(MessageNano.toByteArray(source));
5802     }
5803 
copyOf(SipDelegateStats source)5804     private static SipDelegateStats copyOf(SipDelegateStats source)
5805             throws Exception {
5806         return SipDelegateStats.parseFrom(MessageNano.toByteArray(source));
5807     }
copyOf(SipTransportFeatureTagStats source)5808     private static SipTransportFeatureTagStats copyOf(SipTransportFeatureTagStats source)
5809             throws Exception {
5810         return SipTransportFeatureTagStats.parseFrom(MessageNano.toByteArray(source));
5811     }
5812 
copyOf(GbaEvent source)5813     private static GbaEvent copyOf(GbaEvent source)
5814             throws Exception {
5815         return GbaEvent.parseFrom(MessageNano.toByteArray(source));
5816     }
5817 
copyOf(SipMessageResponse source)5818     private static SipMessageResponse copyOf(SipMessageResponse source)
5819             throws Exception {
5820         return SipMessageResponse.parseFrom(MessageNano.toByteArray(source));
5821     }
5822 
copyOf(SipTransportSession source)5823     private static SipTransportSession copyOf(SipTransportSession source)
5824             throws Exception {
5825         return SipTransportSession.parseFrom(MessageNano.toByteArray(source));
5826     }
5827 
copyOf(IncomingSms source)5828     private static IncomingSms copyOf(IncomingSms source)
5829             throws Exception {
5830         return IncomingSms.parseFrom(MessageNano.toByteArray(source));
5831     }
5832 
copyOf(OutgoingSms source)5833     private static OutgoingSms copyOf(OutgoingSms source)
5834             throws Exception {
5835         return OutgoingSms.parseFrom(MessageNano.toByteArray(source));
5836     }
5837 
copyOf(OutgoingShortCodeSms source)5838     private static OutgoingShortCodeSms copyOf(OutgoingShortCodeSms source)
5839             throws Exception {
5840         return OutgoingShortCodeSms.parseFrom(MessageNano.toByteArray(source));
5841     }
5842 
copyOf(SatelliteController source)5843     private static SatelliteController copyOf(SatelliteController source)
5844             throws Exception {
5845         return SatelliteController.parseFrom(MessageNano.toByteArray(source));
5846     }
5847 
copyOf(SatelliteSession source)5848     private static SatelliteSession copyOf(SatelliteSession source)
5849             throws Exception {
5850         return SatelliteSession.parseFrom(MessageNano.toByteArray(source));
5851     }
5852 
copyOf(SatelliteIncomingDatagram source)5853     private static SatelliteIncomingDatagram copyOf(SatelliteIncomingDatagram source)
5854             throws Exception {
5855         return SatelliteIncomingDatagram.parseFrom(MessageNano.toByteArray(source));
5856     }
5857 
copyOf(SatelliteOutgoingDatagram source)5858     private static SatelliteOutgoingDatagram copyOf(SatelliteOutgoingDatagram source)
5859             throws Exception {
5860         return SatelliteOutgoingDatagram.parseFrom(MessageNano.toByteArray(source));
5861     }
5862 
copyOf(SatelliteProvision source)5863     private static SatelliteProvision copyOf(SatelliteProvision source)
5864             throws Exception {
5865         return SatelliteProvision.parseFrom(MessageNano.toByteArray(source));
5866     }
5867 
copyOf(SatelliteSosMessageRecommender source)5868     private static SatelliteSosMessageRecommender copyOf(SatelliteSosMessageRecommender source)
5869             throws Exception {
5870         return SatelliteSosMessageRecommender.parseFrom(MessageNano.toByteArray(source));
5871     }
5872 
copyOf(DataNetworkValidation source)5873     private static DataNetworkValidation copyOf(DataNetworkValidation source)
5874             throws Exception {
5875         return DataNetworkValidation.parseFrom(MessageNano.toByteArray(source));
5876     }
5877 
copyOf(CarrierRoamingSatelliteSession source)5878     private static CarrierRoamingSatelliteSession copyOf(CarrierRoamingSatelliteSession source)
5879             throws Exception {
5880         return CarrierRoamingSatelliteSession.parseFrom(MessageNano.toByteArray(source));
5881     }
5882 
copyOf( CarrierRoamingSatelliteControllerStats source)5883     private static CarrierRoamingSatelliteControllerStats copyOf(
5884             CarrierRoamingSatelliteControllerStats source) throws Exception {
5885         return CarrierRoamingSatelliteControllerStats.parseFrom(MessageNano.toByteArray(source));
5886     }
5887 
copyOf(SatelliteEntitlement source)5888     private static SatelliteEntitlement copyOf(SatelliteEntitlement source) throws Exception {
5889         return SatelliteEntitlement.parseFrom(MessageNano.toByteArray(source));
5890     }
5891 
copyOf(SatelliteConfigUpdater source)5892     private static SatelliteConfigUpdater copyOf(SatelliteConfigUpdater source) throws Exception {
5893         return SatelliteConfigUpdater.parseFrom(MessageNano.toByteArray(source));
5894     }
5895 
copyOf(SatelliteAccessController source)5896     private static SatelliteAccessController copyOf(SatelliteAccessController source)
5897             throws Exception {
5898         return SatelliteAccessController.parseFrom(MessageNano.toByteArray(source));
5899     }
5900 
assertAllPullTimestampEquals(long timestamp)5901     private void assertAllPullTimestampEquals(long timestamp) {
5902         assertEquals(
5903                 timestamp,
5904                 mPersistAtomsStorage.getAtomsProto().voiceCallRatUsagePullTimestampMillis);
5905         assertEquals(
5906                 timestamp,
5907                 mPersistAtomsStorage.getAtomsProto().voiceCallSessionPullTimestampMillis);
5908         assertEquals(
5909                 timestamp,
5910                 mPersistAtomsStorage.getAtomsProto().cellularServiceStatePullTimestampMillis);
5911         assertEquals(
5912                 timestamp,
5913                 mPersistAtomsStorage.getAtomsProto().cellularDataServiceSwitchPullTimestampMillis);
5914     }
5915 
assertStorageIsEmptyForAllAtoms()5916     private void assertStorageIsEmptyForAllAtoms() {
5917         assertProtoArrayIsEmpty(mPersistAtomsStorage.getVoiceCallRatUsages(0L));
5918         assertProtoArrayIsEmpty(mPersistAtomsStorage.getVoiceCallSessions(0L));
5919         assertProtoArrayIsEmpty(mPersistAtomsStorage.getCellularServiceStates(0L));
5920         assertProtoArrayIsEmpty(mPersistAtomsStorage.getCellularDataServiceSwitches(0L));
5921     }
5922 
assertProtoArrayIsEmpty(T[] array)5923     private static <T extends MessageNano> void assertProtoArrayIsEmpty(T[] array) {
5924         assertNotNull(array);
5925         assertEquals(0, array.length);
5926     }
5927 
assertProtoArrayEquals(MessageNano[] expected, MessageNano[] actual)5928     private static void assertProtoArrayEquals(MessageNano[] expected, MessageNano[] actual) {
5929         assertNotNull(expected);
5930         assertNotNull(actual);
5931         String message =
5932                 "Expected:\n" + Arrays.toString(expected) + "\nGot:\n" + Arrays.toString(actual);
5933         assertEquals(message, expected.length, actual.length);
5934         for (int i = 0; i < expected.length; i++) {
5935             assertTrue(message, MessageNano.messageNanoEquals(expected[i], actual[i]));
5936         }
5937     }
5938 
assertProtoArrayEqualsIgnoringOrder( MessageNano[] expected, MessageNano[] actual)5939     private static void assertProtoArrayEqualsIgnoringOrder(
5940             MessageNano[] expected, MessageNano[] actual) {
5941         assertNotNull(expected);
5942         assertNotNull(actual);
5943         expected = expected.clone();
5944         actual = actual.clone();
5945         Arrays.sort(expected, sProtoComparator);
5946         Arrays.sort(actual, sProtoComparator);
5947         assertProtoArrayEquals(expected, actual);
5948     }
5949 
assertHasCall( VoiceCallSession[] calls, @Nullable VoiceCallSession expectedCall, int expectedCount)5950     private static void assertHasCall(
5951             VoiceCallSession[] calls, @Nullable VoiceCallSession expectedCall, int expectedCount) {
5952         assertNotNull(calls);
5953         int actualCount = 0;
5954         for (VoiceCallSession call : calls) {
5955             if (call != null && expectedCall != null) {
5956                 if (MessageNano.messageNanoEquals(call, expectedCall)) {
5957                     actualCount++;
5958                 }
5959             }
5960         }
5961         assertEquals(expectedCount, actualCount);
5962     }
5963 
verifyCurrentStateSavedToFileOnce()5964     private void verifyCurrentStateSavedToFileOnce() throws Exception {
5965         InOrder inOrder = inOrder(mTestFileOutputStream);
5966         inOrder.verify(mTestFileOutputStream, times(1))
5967                 .write(eq(PersistAtoms.toByteArray(mPersistAtomsStorage.getAtomsProto())));
5968         inOrder.verify(mTestFileOutputStream, times(1)).close();
5969         inOrder.verifyNoMoreInteractions();
5970     }
5971 
assertHasStatsCountTime( ImsRegistrationFeatureTagStats[] statses, @Nullable ImsRegistrationFeatureTagStats expectedStats, int expectedCount, long expectedTime)5972     private static void assertHasStatsCountTime(
5973             ImsRegistrationFeatureTagStats[] statses,
5974             @Nullable ImsRegistrationFeatureTagStats expectedStats,
5975             int expectedCount, long expectedTime) {
5976         assertNotNull(statses);
5977         int actualCount = 0;
5978         long actualTime = 0;
5979         for (ImsRegistrationFeatureTagStats stats : statses) {
5980             if (stats != null && expectedStats != null) {
5981                 if (stats.carrierId == expectedStats.carrierId
5982                         && stats.slotId == expectedStats.slotId
5983                         && stats.featureTagName == expectedStats.featureTagName
5984                         && stats.registrationTech == expectedStats.registrationTech) {
5985                     actualCount++;
5986                     actualTime += stats.registeredMillis;
5987                 }
5988             }
5989         }
5990         assertEquals(expectedCount, actualCount);
5991         assertEquals(expectedTime, actualTime);
5992     }
5993 
assertHasStatsAndCount( RcsClientProvisioningStats[] statses, @Nullable RcsClientProvisioningStats expectedStats, int expectedCount)5994     private static void assertHasStatsAndCount(
5995             RcsClientProvisioningStats[] statses,
5996             @Nullable RcsClientProvisioningStats expectedStats, int expectedCount) {
5997         assertNotNull(statses);
5998         int actualCount = -1;
5999         for (RcsClientProvisioningStats stats : statses) {
6000             if (stats.carrierId == expectedStats.carrierId
6001                     && stats.slotId == expectedStats.slotId
6002                     && stats.event == expectedStats.event) {
6003                 actualCount = stats.count;
6004             }
6005         }
6006         assertEquals(expectedCount, actualCount);
6007     }
6008 
assertHasStats( ImsDedicatedBearerListenerEvent[] statses, @Nullable ImsDedicatedBearerListenerEvent expectedStats, int expectedCount)6009     private static void assertHasStats(
6010             ImsDedicatedBearerListenerEvent[] statses,
6011             @Nullable ImsDedicatedBearerListenerEvent expectedStats, int expectedCount) {
6012         assertNotNull(statses);
6013         int actualCount = 0;
6014         for (ImsDedicatedBearerListenerEvent stats : statses) {
6015             if (stats != null && expectedStats != null) {
6016                 if (MessageNano.messageNanoEquals(stats, expectedStats)) {
6017                     actualCount++;
6018                 }
6019             }
6020         }
6021         assertEquals(expectedCount, actualCount);
6022     }
6023 
assertHasStatsAndCount( ImsDedicatedBearerEvent[] statses, @Nullable ImsDedicatedBearerEvent expectedStats, int expectedCount)6024     private static void assertHasStatsAndCount(
6025             ImsDedicatedBearerEvent[] statses,
6026             @Nullable ImsDedicatedBearerEvent expectedStats, int expectedCount) {
6027         assertNotNull(statses);
6028         int actualCount = -1;
6029         for (ImsDedicatedBearerEvent stats : statses) {
6030             if (stats.carrierId == expectedStats.carrierId
6031                     && stats.slotId == expectedStats.slotId
6032                     && stats.ratAtEnd == expectedStats.ratAtEnd
6033                     && stats.qci == expectedStats.qci
6034                     && stats.bearerState == expectedStats.bearerState
6035                     && stats.localConnectionInfoReceived
6036                             == expectedStats.localConnectionInfoReceived
6037                     && stats.remoteConnectionInfoReceived
6038                             == expectedStats.remoteConnectionInfoReceived
6039                     && stats.hasListeners == expectedStats.hasListeners) {
6040                 actualCount = stats.count;
6041             }
6042         }
6043         assertEquals(expectedCount, actualCount);
6044     }
6045 
assertHasStats(SatelliteController[] tested, @Nullable SatelliteController expectedStats, int expectedCount)6046     private static void assertHasStats(SatelliteController[] tested,
6047             @Nullable SatelliteController expectedStats, int expectedCount) {
6048         assertNotNull(tested);
6049         int actualCount = 0;
6050         for (SatelliteController stats : tested) {
6051             if (expectedStats.carrierId == stats.carrierId
6052                     && expectedStats.isProvisioned == stats.isProvisioned
6053                     && expectedStats.isNtnOnlyCarrier == stats.isNtnOnlyCarrier) {
6054                 assertEquals(expectedStats.countOfSatelliteServiceEnablementsSuccess,
6055                         stats.countOfSatelliteServiceEnablementsSuccess);
6056                 assertEquals(expectedStats.countOfSatelliteServiceEnablementsFail,
6057                         stats.countOfSatelliteServiceEnablementsFail);
6058                 assertEquals(expectedStats.countOfOutgoingDatagramSuccess,
6059                         stats.countOfOutgoingDatagramSuccess);
6060                 assertEquals(expectedStats.countOfOutgoingDatagramFail,
6061                         stats.countOfOutgoingDatagramFail);
6062                 assertEquals(expectedStats.countOfIncomingDatagramSuccess,
6063                         stats.countOfIncomingDatagramSuccess);
6064                 assertEquals(expectedStats.countOfIncomingDatagramFail,
6065                         stats.countOfIncomingDatagramFail);
6066                 assertEquals(expectedStats.countOfDatagramTypeSosSmsSuccess,
6067                         stats.countOfDatagramTypeSosSmsSuccess);
6068                 assertEquals(expectedStats.countOfDatagramTypeSosSmsFail,
6069                         stats.countOfDatagramTypeSosSmsFail);
6070                 assertEquals(expectedStats.countOfDatagramTypeLocationSharingSuccess,
6071                         stats.countOfDatagramTypeLocationSharingSuccess);
6072                 assertEquals(expectedStats.countOfDatagramTypeLocationSharingFail,
6073                         stats.countOfDatagramTypeLocationSharingFail);
6074                 assertEquals(expectedStats.totalServiceUptimeSec, stats.totalServiceUptimeSec);
6075                 assertEquals(expectedStats.totalBatteryConsumptionPercent,
6076                         stats.totalBatteryConsumptionPercent);
6077                 assertEquals(expectedStats.totalBatteryChargedTimeSec,
6078                         stats.totalBatteryChargedTimeSec);
6079                 assertEquals(expectedStats.countOfDemoModeSatelliteServiceEnablementsSuccess,
6080                         stats.countOfDemoModeSatelliteServiceEnablementsSuccess);
6081                 assertEquals(expectedStats.countOfDemoModeSatelliteServiceEnablementsFail,
6082                         stats.countOfDemoModeSatelliteServiceEnablementsFail);
6083                 assertEquals(expectedStats.countOfDemoModeOutgoingDatagramSuccess,
6084                         stats.countOfDemoModeOutgoingDatagramSuccess);
6085                 assertEquals(expectedStats.countOfDemoModeOutgoingDatagramFail,
6086                         stats.countOfDemoModeOutgoingDatagramFail);
6087                 assertEquals(expectedStats.countOfDemoModeIncomingDatagramSuccess,
6088                         stats.countOfDemoModeIncomingDatagramSuccess);
6089                 assertEquals(expectedStats.countOfDemoModeIncomingDatagramFail,
6090                         stats.countOfDemoModeIncomingDatagramFail);
6091                 assertEquals(expectedStats.countOfDatagramTypeKeepAliveSuccess,
6092                         stats.countOfDatagramTypeKeepAliveSuccess);
6093                 assertEquals(expectedStats.countOfDatagramTypeKeepAliveFail,
6094                         stats.countOfDatagramTypeKeepAliveFail);
6095                 assertEquals(expectedStats.countOfAllowedSatelliteAccess,
6096                         stats.countOfAllowedSatelliteAccess);
6097                 assertEquals(expectedStats.countOfDisallowedSatelliteAccess,
6098                         stats.countOfDisallowedSatelliteAccess);
6099                 assertEquals(expectedStats.countOfSatelliteAccessCheckFail,
6100                         stats.countOfSatelliteAccessCheckFail);
6101                 assertEquals(expectedStats.isProvisioned, stats.isProvisioned);
6102                 assertEquals(expectedStats.carrierId, stats.carrierId);
6103                 assertEquals(expectedStats.countOfSatelliteAllowedStateChangedEvents,
6104                         stats.countOfSatelliteAllowedStateChangedEvents);
6105                 assertEquals(expectedStats.countOfSuccessfulLocationQueries,
6106                         stats.countOfSuccessfulLocationQueries);
6107                 assertEquals(expectedStats.countOfFailedLocationQueries,
6108                         stats.countOfFailedLocationQueries);
6109                 assertEquals(expectedStats.countOfP2PSmsAvailableNotificationShown,
6110                         stats.countOfP2PSmsAvailableNotificationShown);
6111                 assertEquals(expectedStats.countOfP2PSmsAvailableNotificationRemoved,
6112                         stats.countOfP2PSmsAvailableNotificationRemoved);
6113                 assertEquals(expectedStats.versionOfSatelliteAccessConfig,
6114                         stats.versionOfSatelliteAccessConfig);
6115                 assertEquals(expectedStats.countOfIncomingDatagramTypeSosSmsSuccess,
6116                         stats.countOfIncomingDatagramTypeSosSmsSuccess);
6117                 assertEquals(expectedStats.countOfIncomingDatagramTypeSosSmsFail,
6118                         stats.countOfIncomingDatagramTypeSosSmsFail);
6119                 assertEquals(expectedStats.countOfOutgoingDatagramTypeSmsSuccess,
6120                         stats.countOfOutgoingDatagramTypeSmsSuccess);
6121                 assertEquals(expectedStats.countOfOutgoingDatagramTypeSmsFail,
6122                         stats.countOfOutgoingDatagramTypeSmsFail);
6123                 assertEquals(expectedStats.countOfIncomingDatagramTypeSmsSuccess,
6124                         stats.countOfIncomingDatagramTypeSmsSuccess);
6125                 assertEquals(expectedStats.countOfIncomingDatagramTypeSmsFail,
6126                         stats.countOfIncomingDatagramTypeSmsFail);
6127                 actualCount++;
6128             }
6129         }
6130         assertEquals(expectedCount, actualCount);
6131     }
6132 
assertHasStatsAndCount( SatelliteSession[] tested, @Nullable SatelliteSession expectedStats, int expectedCount)6133     private static void assertHasStatsAndCount(
6134             SatelliteSession[] tested,
6135             @Nullable SatelliteSession expectedStats, int expectedCount) {
6136         assertNotNull(tested);
6137         int actualCount = 0;
6138         for (SatelliteSession stats : tested) {
6139             if (stats.satelliteServiceInitializationResult
6140                             == expectedStats.satelliteServiceInitializationResult
6141                     && stats.satelliteTechnology == expectedStats.satelliteTechnology
6142                     && stats.satelliteServiceTerminationResult
6143                             == expectedStats.satelliteServiceTerminationResult
6144                     && stats.initializationProcessingTimeMillis
6145                             == expectedStats.initializationProcessingTimeMillis
6146                     && stats.terminationProcessingTimeMillis
6147                             == expectedStats.terminationProcessingTimeMillis
6148                     && stats.sessionDurationSeconds == expectedStats.sessionDurationSeconds
6149                     && stats.countOfOutgoingDatagramSuccess
6150                             == expectedStats.countOfOutgoingDatagramSuccess
6151                     && stats.countOfOutgoingDatagramFailed
6152                             == expectedStats.countOfOutgoingDatagramFailed
6153                     && stats.countOfIncomingDatagramSuccess
6154                             == expectedStats.countOfIncomingDatagramSuccess
6155                     && stats.countOfIncomingDatagramFailed
6156                             == expectedStats.countOfIncomingDatagramFailed
6157                     && stats.isDemoMode == expectedStats.isDemoMode
6158                     && stats.carrierId == expectedStats.carrierId
6159                     && stats.countOfSatelliteNotificationDisplayed
6160                             == expectedStats.countOfSatelliteNotificationDisplayed
6161                     && stats.countOfAutoExitDueToScreenOff
6162                             == expectedStats.countOfAutoExitDueToScreenOff
6163                     && stats.countOfAutoExitDueToTnNetwork
6164                             == expectedStats.countOfAutoExitDueToTnNetwork
6165                     && stats.isEmergency == expectedStats.isEmergency
6166                     && stats.maxInactivityDurationSec == expectedStats.maxInactivityDurationSec) {
6167                 actualCount = stats.count;
6168             }
6169         }
6170         assertEquals(expectedCount, actualCount);
6171     }
6172 
assertHasStatsAndCount( SatelliteIncomingDatagram[] tested, @Nullable SatelliteIncomingDatagram expectedStats, int expectedCount)6173     private static void assertHasStatsAndCount(
6174             SatelliteIncomingDatagram[] tested,
6175             @Nullable SatelliteIncomingDatagram expectedStats, int expectedCount) {
6176         assertNotNull(tested);
6177         int actualCount = 0;
6178         for (SatelliteIncomingDatagram stats : tested) {
6179             if (stats.resultCode == expectedStats.resultCode
6180                     && stats.datagramSizeBytes == expectedStats.datagramSizeBytes
6181                     && stats.datagramTransferTimeMillis
6182                         == expectedStats.datagramTransferTimeMillis
6183                     && stats.isDemoMode == expectedStats.isDemoMode
6184                     && stats.carrierId == expectedStats.carrierId) {
6185                 actualCount++;
6186             }
6187         }
6188         assertEquals(expectedCount, actualCount);
6189     }
6190 
assertHasStatsAndCount( SatelliteOutgoingDatagram[] tested, @Nullable SatelliteOutgoingDatagram expectedStats, int expectedCount)6191     private static void assertHasStatsAndCount(
6192             SatelliteOutgoingDatagram[] tested,
6193             @Nullable SatelliteOutgoingDatagram expectedStats, int expectedCount) {
6194         assertNotNull(tested);
6195         int actualCount = 0;
6196         for (SatelliteOutgoingDatagram stats : tested) {
6197             if (stats.datagramType == expectedStats.datagramType
6198                     && stats.resultCode == expectedStats.resultCode
6199                     && stats.datagramSizeBytes == expectedStats.datagramSizeBytes
6200                     && stats.datagramTransferTimeMillis
6201                         == expectedStats.datagramTransferTimeMillis
6202                     && stats.isDemoMode == expectedStats.isDemoMode
6203                     && stats.carrierId == expectedStats.carrierId) {
6204                 actualCount++;
6205             }
6206         }
6207         assertEquals(expectedCount, actualCount);
6208     }
6209 
assertHasStatsAndCount( SatelliteProvision[] tested, @Nullable SatelliteProvision expectedStats, int expectedCount)6210     private static void assertHasStatsAndCount(
6211             SatelliteProvision[] tested,
6212             @Nullable SatelliteProvision expectedStats, int expectedCount) {
6213         assertNotNull(tested);
6214         int actualCount = 0;
6215         for (SatelliteProvision stats : tested) {
6216             if (stats.resultCode == expectedStats.resultCode
6217                     && stats.provisioningTimeSec == expectedStats.provisioningTimeSec
6218                     && stats.isProvisionRequest == expectedStats.isProvisionRequest
6219                     && stats.isCanceled == expectedStats.isCanceled
6220                     && stats.carrierId == expectedStats.carrierId) {
6221                 actualCount++;
6222             }
6223         }
6224         assertEquals(expectedCount, actualCount);
6225     }
6226 
assertHasStatsAndCount( SatelliteSosMessageRecommender[] tested, @Nullable SatelliteSosMessageRecommender expectedStats, int expectedCount)6227     private static void assertHasStatsAndCount(
6228             SatelliteSosMessageRecommender[] tested,
6229             @Nullable SatelliteSosMessageRecommender expectedStats, int expectedCount) {
6230         assertNotNull(tested);
6231         int actualCount = 0;
6232         for (SatelliteSosMessageRecommender stats : tested) {
6233             if (stats.isDisplaySosMessageSent
6234                     == expectedStats.isDisplaySosMessageSent
6235                     && stats.countOfTimerStarted == expectedStats.countOfTimerStarted
6236                     && stats.isImsRegistered == expectedStats.isImsRegistered
6237                     && stats.cellularServiceState == expectedStats.cellularServiceState
6238                     && stats.isMultiSim == expectedStats.isMultiSim
6239                     && stats.recommendingHandoverType == expectedStats.recommendingHandoverType
6240                     && stats.isSatelliteAllowedInCurrentLocation
6241                     == expectedStats.isSatelliteAllowedInCurrentLocation
6242                     && stats.isWifiConnected == expectedStats.isWifiConnected
6243                     && stats.carrierId == expectedStats.carrierId) {
6244                 actualCount = stats.count;
6245             }
6246         }
6247         assertEquals(expectedCount, actualCount);
6248     }
6249 
assertHasStatsAndCount( SatelliteAccessController[] tested, @Nullable SatelliteAccessController expectedStats, int expectedCount)6250     private static void assertHasStatsAndCount(
6251             SatelliteAccessController[] tested,
6252             @Nullable SatelliteAccessController expectedStats, int expectedCount) {
6253         assertNotNull(tested);
6254         int actualCount = 0;
6255         for (SatelliteAccessController stats : tested) {
6256             if (stats.accessControlType
6257                     == expectedStats.accessControlType
6258                     && stats.locationQueryTimeMillis == expectedStats.locationQueryTimeMillis
6259                     && stats.onDeviceLookupTimeMillis == expectedStats.onDeviceLookupTimeMillis
6260                     && stats.totalCheckingTimeMillis == expectedStats.totalCheckingTimeMillis
6261                     && stats.isAllowed == expectedStats.isAllowed
6262                     && stats.isEmergency == expectedStats.isEmergency
6263                     && stats.resultCode == expectedStats.resultCode
6264                     && Arrays.equals(stats.countryCodes, expectedStats.countryCodes)
6265                     && stats.configDataSource == expectedStats.configDataSource
6266                     && stats.carrierId == expectedStats.carrierId) {
6267                 actualCount++;
6268             }
6269         }
6270         assertEquals(expectedCount, actualCount);
6271     }
6272 
assertHasStatsAndCountDuration( RcsAcsProvisioningStats[] statses, @Nullable RcsAcsProvisioningStats expectedStats, int count, long duration)6273     private static void assertHasStatsAndCountDuration(
6274             RcsAcsProvisioningStats[] statses,
6275             @Nullable RcsAcsProvisioningStats expectedStats, int count, long duration) {
6276         assertNotNull(statses);
6277         int actualCount = -1;
6278         long actualDuration = -1;
6279         for (RcsAcsProvisioningStats stats : statses) {
6280             if (stats.carrierId == expectedStats.carrierId
6281                     && stats.slotId == expectedStats.slotId
6282                     && stats.responseCode == expectedStats.responseCode
6283                     && stats.responseType == expectedStats.responseType
6284                     && stats.isSingleRegistrationEnabled
6285                             == expectedStats.isSingleRegistrationEnabled) {
6286                 actualCount = stats.count;
6287                 actualDuration = stats.stateTimerMillis;
6288             }
6289         }
6290         assertEquals(count, actualCount);
6291         assertEquals(duration, actualDuration);
6292     }
6293 
assertHasStats(SipDelegateStats[] results, Object expectedStats, int expectedCount)6294     private static void assertHasStats(SipDelegateStats[] results,
6295             Object expectedStats, int expectedCount) {
6296         assertNotNull(results);
6297         assertNotNull(expectedStats);
6298 
6299         int realCount = 0;
6300         if (expectedStats instanceof SipDelegateStats[]) {
6301             SipDelegateStats[] expectedResults = (SipDelegateStats[]) expectedStats;
6302             for (SipDelegateStats stat: results) {
6303                 for (SipDelegateStats estat : expectedResults) {
6304                     if (stat != null && estat != null) {
6305                         if (MessageNano.messageNanoEquals(stat, estat)) {
6306                             realCount++;
6307                             break;
6308                         }
6309                     }
6310                 }
6311             }
6312         } else {
6313             SipDelegateStats expectedResult = (SipDelegateStats) expectedStats;
6314             for (SipDelegateStats stat : results) {
6315                 if (stat != null && expectedStats != null) {
6316                     if (MessageNano.messageNanoEquals(stat, expectedResult)) {
6317                         realCount++;
6318                     }
6319                 }
6320             }
6321         }
6322         assertEquals(expectedCount, realCount);
6323     }
6324 
assertHasStats(SipTransportFeatureTagStats[] results, Object expectedStats, int expectedCount)6325     private static void assertHasStats(SipTransportFeatureTagStats[] results,
6326             Object expectedStats, int expectedCount) {
6327         assertNotNull(results);
6328         assertNotNull(expectedStats);
6329 
6330         int realCount = 0;
6331         if (expectedStats instanceof SipTransportFeatureTagStats[]) {
6332             SipTransportFeatureTagStats[] expectedResults =
6333                     (SipTransportFeatureTagStats[]) expectedStats;
6334             for (SipTransportFeatureTagStats stat: results) {
6335                 for (SipTransportFeatureTagStats estat : expectedResults) {
6336                     if (stat != null && estat != null) {
6337                         if (MessageNano.messageNanoEquals(stat, estat)) {
6338                             realCount++;
6339                             break;
6340                         }
6341                     }
6342                 }
6343             }
6344         } else {
6345             SipTransportFeatureTagStats expectedResult =
6346                     (SipTransportFeatureTagStats) expectedStats;
6347             for (SipTransportFeatureTagStats stat : results) {
6348                 if (stat != null && expectedStats != null) {
6349                     if (MessageNano.messageNanoEquals(stat, expectedResult)) {
6350                         realCount++;
6351                     }
6352                 }
6353             }
6354         }
6355         assertEquals(expectedCount, realCount);
6356     }
6357 
assertHasStatsAndCount( GbaEvent[] statses, @Nullable GbaEvent expectedStats, int expectedCount)6358     private static void assertHasStatsAndCount(
6359             GbaEvent[] statses,
6360             @Nullable GbaEvent expectedStats, int expectedCount) {
6361         assertNotNull(statses);
6362         int actualCount = -1;
6363         for (GbaEvent stats : statses) {
6364             if (stats.carrierId == expectedStats.carrierId
6365                     && stats.slotId == expectedStats.slotId
6366                     && stats.successful == expectedStats.successful
6367                     && stats.failedReason == expectedStats.failedReason) {
6368                 actualCount = stats.count;
6369             }
6370         }
6371         assertEquals(expectedCount, actualCount);
6372     }
6373 
assertHasStats( SipMessageResponse[] statses, @Nullable SipMessageResponse expectedStats, int expectedCount)6374     private static void assertHasStats(
6375             SipMessageResponse[] statses,
6376             @Nullable SipMessageResponse expectedStats, int expectedCount) {
6377         assertNotNull(statses);
6378         int actualCount = -1;
6379         for (SipMessageResponse stats : statses) {
6380             if (stats.carrierId == expectedStats.carrierId
6381                     && stats.slotId == expectedStats.slotId
6382                     && stats.sipMessageMethod == expectedStats.sipMessageMethod
6383                     && stats.sipMessageResponse == expectedStats.sipMessageResponse
6384                     && stats.sipMessageDirection == expectedStats.sipMessageDirection) {
6385                 actualCount = stats.count;
6386             }
6387         }
6388         assertEquals(expectedCount, actualCount);
6389     }
6390 
assertHasStats( SipTransportSession[] statses, @Nullable SipTransportSession expectedStats, int expectedCount)6391     private static void assertHasStats(
6392             SipTransportSession[] statses,
6393             @Nullable SipTransportSession expectedStats, int expectedCount) {
6394         assertNotNull(statses);
6395         int actualCount = -1;
6396         for (SipTransportSession stats : statses) {
6397             if (stats.carrierId == expectedStats.carrierId
6398                     && stats.slotId == expectedStats.slotId
6399                     && stats.sessionMethod == expectedStats.sessionMethod
6400                     && stats.sipMessageDirection == expectedStats.sipMessageDirection
6401                     && stats.sipResponse == expectedStats.sipResponse) {
6402                 actualCount = stats.sessionCount;
6403             }
6404         }
6405         assertEquals(expectedCount, actualCount);
6406     }
6407 
assertHasStatsAndCount( IncomingSms[] incomingSmsList, @Nullable IncomingSms expectedIncomingSms, int expectedCount)6408     private static void assertHasStatsAndCount(
6409             IncomingSms[] incomingSmsList,
6410             @Nullable IncomingSms expectedIncomingSms, int expectedCount) {
6411         assertNotNull(incomingSmsList);
6412         int actualCount = -1;
6413         for (IncomingSms incomingSms : incomingSmsList) {
6414             if (incomingSms.smsFormat == expectedIncomingSms.smsFormat
6415                     && incomingSms.smsTech == expectedIncomingSms.smsTech
6416                     && incomingSms.rat == expectedIncomingSms.rat
6417                     && incomingSms.smsType == expectedIncomingSms.smsType
6418                     && incomingSms.totalParts == expectedIncomingSms.totalParts
6419                     && incomingSms.receivedParts == expectedIncomingSms.receivedParts
6420                     && incomingSms.blocked == expectedIncomingSms.blocked
6421                     && incomingSms.error == expectedIncomingSms.error
6422                     && incomingSms.isRoaming == expectedIncomingSms.isRoaming
6423                     && incomingSms.simSlotIndex == expectedIncomingSms.simSlotIndex
6424                     && incomingSms.isMultiSim == expectedIncomingSms.isMultiSim
6425                     && incomingSms.isEsim == expectedIncomingSms.isEsim
6426                     && incomingSms.carrierId == expectedIncomingSms.carrierId
6427                     && incomingSms.messageId == expectedIncomingSms.messageId
6428                     && incomingSms.isManagedProfile == expectedIncomingSms.isManagedProfile
6429                     && incomingSms.isNtn == expectedIncomingSms.isNtn
6430                     && incomingSms.isEmergency == expectedIncomingSms.isEmergency) {
6431                 actualCount = incomingSms.count;
6432             }
6433         }
6434         assertEquals(expectedCount, actualCount);
6435     }
6436 
assertHasStatsAndCount( OutgoingSms[] outgoingSmsList, @Nullable OutgoingSms expectedOutgoingSms, int expectedCount)6437     private static void assertHasStatsAndCount(
6438             OutgoingSms[] outgoingSmsList,
6439             @Nullable OutgoingSms expectedOutgoingSms, int expectedCount) {
6440         assertNotNull(outgoingSmsList);
6441         int actualCount = -1;
6442         for (OutgoingSms outgoingSms : outgoingSmsList) {
6443             if (outgoingSms.smsFormat == expectedOutgoingSms.smsFormat
6444                     && outgoingSms.smsTech == expectedOutgoingSms.smsTech
6445                     && outgoingSms.rat == expectedOutgoingSms.rat
6446                     && outgoingSms.sendResult == expectedOutgoingSms.sendResult
6447                     && outgoingSms.errorCode == expectedOutgoingSms.errorCode
6448                     && outgoingSms.isRoaming == expectedOutgoingSms.isRoaming
6449                     && outgoingSms.isFromDefaultApp == expectedOutgoingSms.isFromDefaultApp
6450                     && outgoingSms.simSlotIndex == expectedOutgoingSms.simSlotIndex
6451                     && outgoingSms.isMultiSim == expectedOutgoingSms.isMultiSim
6452                     && outgoingSms.carrierId == expectedOutgoingSms.carrierId
6453                     && outgoingSms.messageId == expectedOutgoingSms.messageId
6454                     && outgoingSms.retryId == expectedOutgoingSms.retryId
6455                     && outgoingSms.intervalMillis == expectedOutgoingSms.intervalMillis
6456                     && outgoingSms.sendErrorCode == expectedOutgoingSms.sendErrorCode
6457                     && outgoingSms.networkErrorCode == expectedOutgoingSms.networkErrorCode
6458                     && outgoingSms.isManagedProfile == expectedOutgoingSms.isManagedProfile
6459                     && outgoingSms.isEmergency == expectedOutgoingSms.isEmergency
6460                     && outgoingSms.isNtn == expectedOutgoingSms.isNtn) {
6461                 actualCount = outgoingSms.count;
6462             }
6463         }
6464         assertEquals(expectedCount, actualCount);
6465     }
6466 
assertHasStatsAndCount( OutgoingShortCodeSms[] outgoingShortCodeSmsList, @Nullable OutgoingShortCodeSms expectedOutgoingShortCodeSms, int expectedCount)6467     private static void assertHasStatsAndCount(
6468             OutgoingShortCodeSms[] outgoingShortCodeSmsList,
6469             @Nullable OutgoingShortCodeSms expectedOutgoingShortCodeSms, int expectedCount) {
6470         assertNotNull(outgoingShortCodeSmsList);
6471         int actualCount = -1;
6472         for (OutgoingShortCodeSms outgoingShortCodeSms : outgoingShortCodeSmsList) {
6473             if (outgoingShortCodeSms.category == expectedOutgoingShortCodeSms.category
6474                     && outgoingShortCodeSms.xmlVersion == expectedOutgoingShortCodeSms.xmlVersion) {
6475                 actualCount = outgoingShortCodeSms.shortCodeSmsCount;
6476             }
6477         }
6478         assertEquals(expectedCount, actualCount);
6479     }
6480 
assertHasStatsAndCount(CarrierRoamingSatelliteSession[] tested, @Nullable CarrierRoamingSatelliteSession expectedStats, int expectedCount)6481     private static void assertHasStatsAndCount(CarrierRoamingSatelliteSession[] tested,
6482             @Nullable CarrierRoamingSatelliteSession expectedStats, int expectedCount) {
6483         assertNotNull(tested);
6484         int actualCount = 0;
6485         for (CarrierRoamingSatelliteSession stats : tested) {
6486             if (stats.carrierId == expectedStats.carrierId
6487                     && stats.isNtnRoamingInHomeCountry == expectedStats.isNtnRoamingInHomeCountry
6488                     && stats.totalSatelliteModeTimeSec == expectedStats.totalSatelliteModeTimeSec
6489                     && stats.numberOfSatelliteConnections
6490                     == expectedStats.numberOfSatelliteConnections
6491                     && stats.avgDurationOfSatelliteConnectionSec
6492                     == expectedStats.avgDurationOfSatelliteConnectionSec
6493                     && stats.satelliteConnectionGapMinSec
6494                     == expectedStats.satelliteConnectionGapMinSec
6495                     && stats.satelliteConnectionGapAvgSec
6496                     == expectedStats.satelliteConnectionGapAvgSec
6497                     && stats.satelliteConnectionGapMaxSec
6498                     == expectedStats.satelliteConnectionGapMaxSec
6499                     && stats.rsrpAvg == expectedStats.rsrpAvg
6500                     && stats.rsrpMedian == expectedStats.rsrpMedian
6501                     && stats.rssnrAvg == expectedStats.rssnrAvg
6502                     && stats.rssnrMedian == expectedStats.rssnrMedian
6503                     && stats.countOfIncomingSms == expectedStats.countOfIncomingSms
6504                     && stats.countOfOutgoingSms == expectedStats.countOfOutgoingSms
6505                     && stats.countOfIncomingMms == expectedStats.countOfIncomingMms
6506                     && stats.countOfOutgoingMms == expectedStats.countOfOutgoingMms
6507                     && stats.isMultiSim == expectedStats.isMultiSim
6508                     && stats.isNbIotNtn == expectedStats.isNbIotNtn) {
6509                 actualCount++;
6510             }
6511         }
6512         assertEquals(expectedCount, actualCount);
6513     }
6514 
assertHasStats(CarrierRoamingSatelliteControllerStats[] tested, @Nullable CarrierRoamingSatelliteControllerStats expectedStats, int expectedCount)6515     private static void assertHasStats(CarrierRoamingSatelliteControllerStats[] tested,
6516             @Nullable CarrierRoamingSatelliteControllerStats expectedStats, int expectedCount) {
6517         assertNotNull(tested);
6518         int count = 0;
6519         for (CarrierRoamingSatelliteControllerStats stats : tested) {
6520             if (expectedStats.carrierId == stats.carrierId) {
6521                 assertEquals(expectedStats.configDataSource, stats.configDataSource);
6522                 assertEquals(expectedStats.countOfEntitlementStatusQueryRequest,
6523                         stats.countOfEntitlementStatusQueryRequest);
6524                 assertEquals(expectedStats.countOfSatelliteConfigUpdateRequest,
6525                         stats.countOfSatelliteConfigUpdateRequest);
6526                 assertEquals(expectedStats.countOfSatelliteNotificationDisplayed,
6527                         stats.countOfSatelliteNotificationDisplayed);
6528                 assertEquals(expectedStats.satelliteSessionGapMinSec,
6529                         stats.satelliteSessionGapMinSec);
6530                 assertEquals(expectedStats.satelliteSessionGapAvgSec,
6531                         stats.satelliteSessionGapAvgSec);
6532                 assertEquals(expectedStats.satelliteSessionGapMaxSec,
6533                         stats.satelliteSessionGapMaxSec);
6534                 assertEquals(expectedStats.isDeviceEntitled, stats.isDeviceEntitled);
6535                 assertEquals(expectedStats.isMultiSim, stats.isMultiSim);
6536                 assertEquals(expectedStats.countOfSatelliteSessions,
6537                         stats.countOfSatelliteSessions);
6538                 assertEquals(expectedStats.isNbIotNtn, stats.isNbIotNtn);
6539                 count++;
6540             }
6541         }
6542         assertEquals(expectedCount, count);
6543     }
6544 
assertHasStatsAndCount( SatelliteEntitlement[] tested, @Nullable SatelliteEntitlement expectedStats, int expectedCount)6545     private static void assertHasStatsAndCount(
6546             SatelliteEntitlement[] tested,
6547             @Nullable SatelliteEntitlement expectedStats, int expectedCount) {
6548         assertNotNull(tested);
6549         int actualCount = 0;
6550         for (SatelliteEntitlement stats : tested) {
6551             if (stats.carrierId == expectedStats.carrierId
6552                     && stats.result == expectedStats.result
6553                     && stats.entitlementStatus == expectedStats.entitlementStatus
6554                     && stats.isRetry == expectedStats.isRetry) {
6555                 actualCount = stats.count;
6556             }
6557         }
6558         assertEquals(expectedCount, actualCount);
6559     }
6560 
assertHasStatsAndCount( SatelliteConfigUpdater[] tested, @Nullable SatelliteConfigUpdater expectedStats, int expectedCount)6561     private static void assertHasStatsAndCount(
6562             SatelliteConfigUpdater[] tested,
6563             @Nullable SatelliteConfigUpdater expectedStats, int expectedCount) {
6564         assertNotNull(tested);
6565         int actualCount = 0;
6566         for (SatelliteConfigUpdater stats : tested) {
6567             if (stats.configVersion == expectedStats.configVersion
6568                     && stats.oemConfigResult == expectedStats.oemConfigResult
6569                     && stats.carrierConfigResult == expectedStats.carrierConfigResult) {
6570                 actualCount = stats.count;
6571             }
6572         }
6573         assertEquals(expectedCount, actualCount);
6574     }
6575 }
6576