aboutsummaryrefslogtreecommitdiff
path: root/tests/telephonytests/src/com/android/internal/telephony/metrics/MetricsCollectorTest.java
blob: 66bf48202ebe99e48d10bf0fe86d6eb6770ed82c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.telephony.metrics;

import static com.android.internal.telephony.TelephonyStatsLog.CELLULAR_DATA_SERVICE_SWITCH;
import static com.android.internal.telephony.TelephonyStatsLog.CELLULAR_SERVICE_STATE;
import static com.android.internal.telephony.TelephonyStatsLog.OUTGOING_SHORT_CODE_SMS;
import static com.android.internal.telephony.TelephonyStatsLog.SIM_SLOT_STATE;
import static com.android.internal.telephony.TelephonyStatsLog.SUPPORTED_RADIO_ACCESS_FAMILY;
import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_RAT_USAGE;
import static com.android.internal.telephony.TelephonyStatsLog.VOICE_CALL_SESSION;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import android.app.StatsManager;
import android.telephony.TelephonyManager;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.StatsEvent;

import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import com.android.internal.telephony.TelephonyTest;
import com.android.internal.telephony.nano.PersistAtomsProto.CellularDataServiceSwitch;
import com.android.internal.telephony.nano.PersistAtomsProto.CellularServiceState;
import com.android.internal.telephony.nano.PersistAtomsProto.OutgoingShortCodeSms;
import com.android.internal.telephony.nano.PersistAtomsProto.VoiceCallRatUsage;
import com.android.internal.telephony.nano.PersistAtomsProto.VoiceCallSession;
import com.android.internal.telephony.uicc.IccCardStatus.CardState;
import com.android.internal.telephony.uicc.UiccCard;
import com.android.internal.telephony.uicc.UiccController;
import com.android.internal.telephony.uicc.UiccPort;
import com.android.internal.telephony.uicc.UiccProfile;
import com.android.internal.telephony.uicc.UiccSlot;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class MetricsCollectorTest extends TelephonyTest {
    private static final StatsManager.PullAtomMetadata POLICY_PULL_DAILY =
            new StatsManager.PullAtomMetadata.Builder()
                    .setCoolDownMillis(24L * 3600L * 1000L)
                    .build();
    private static final long MIN_COOLDOWN_MILLIS = 23L * 3600L * 1000L;
    private static final long MIN_CALLS_PER_BUCKET = 5L;

    // NOTE: these fields are currently 32-bit internally and padded to 64-bit by TelephonyManager
    private static final int SUPPORTED_RAF_1 =
            (int) TelephonyManager.NETWORK_TYPE_BITMASK_GSM
                    | (int) TelephonyManager.NETWORK_TYPE_BITMASK_LTE
                    | (int) TelephonyManager.NETWORK_TYPE_BITMASK_NR;
    private static final int SUPPORTED_RAF_2 =
            (int) TelephonyManager.NETWORK_TYPE_BITMASK_GSM
                    | (int) TelephonyManager.NETWORK_TYPE_BITMASK_1xRTT
                    | (int) TelephonyManager.NETWORK_TYPE_BITMASK_UMTS;
    private static final int SUPPORTED_RAF_BOTH = SUPPORTED_RAF_1 | SUPPORTED_RAF_2;

    // TODO: if we want to check puller registration by mocking StatsManager, we will have to enable
    // inline mocking since the StatsManager class is final

    // b/153195691: we cannot verify the contents of StatsEvent as its getters are marked with @hide

    // Mocked classes
    private Phone mSecondPhone;
    private UiccSlot mPhysicalSlot;
    private UiccSlot mEsimSlot;
    private UiccCard mActiveCard;
    private UiccPort mActivePort;
    private ServiceStateStats mServiceStateStats;

    private MetricsCollector mMetricsCollector;

    @Before
    public void setUp() throws Exception {
        super.setUp(getClass().getSimpleName());
        mSecondPhone = mock(Phone.class);
        mPhysicalSlot = mock(UiccSlot.class);
        mEsimSlot = mock(UiccSlot.class);
        mActiveCard = mock(UiccCard.class);
        mActivePort = mock(UiccPort.class);
        mServiceStateStats = mock(ServiceStateStats.class);
        mMetricsCollector =
                new MetricsCollector(mContext, mPersistAtomsStorage, mDeviceStateHelper);
        doReturn(mSST).when(mSecondPhone).getServiceStateTracker();
        doReturn(mServiceStateStats).when(mSST).getServiceStateStats();
    }

    @After
    public void tearDown() throws Exception {
        mMetricsCollector = null;
        super.tearDown();
    }

    @Test
    @SmallTest
    public void onPullAtom_simSlotState_bothSimPresent() {
        // these have been tested extensively in SimSlotStateTest, here we verify atom generation
        UiccProfile activeProfile = mock(UiccProfile.class);
        doReturn(4).when(activeProfile).getNumApplications();
        doReturn(activeProfile).when(mActivePort).getUiccProfile();
        doReturn(true).when(mPhysicalSlot).isActive();
        doReturn(CardState.CARDSTATE_PRESENT).when(mPhysicalSlot).getCardState();
        doReturn(false).when(mPhysicalSlot).isEuicc();
        doReturn(true).when(mEsimSlot).isActive();
        doReturn(CardState.CARDSTATE_PRESENT).when(mEsimSlot).getCardState();
        doReturn(true).when(mEsimSlot).isEuicc();
        doReturn(mActiveCard).when(mEsimSlot).getUiccCard();
        doReturn(new UiccPort[] {mActivePort}).when(mActiveCard).getUiccPortList();
        doReturn(new UiccSlot[] {mPhysicalSlot, mEsimSlot}).when(mUiccController).getUiccSlots();
        doReturn(mPhysicalSlot).when(mUiccController).getUiccSlot(eq(0));
        doReturn(mEsimSlot).when(mUiccController).getUiccSlot(eq(1));
        StatsEvent expectedAtom =
                StatsEvent.newBuilder()
                        .setAtomId(SIM_SLOT_STATE)
                        .writeInt(2)
                        .writeInt(2)
                        .writeInt(1)
                        .build();
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(SIM_SLOT_STATE, actualAtoms);

        assertThat(actualAtoms).hasSize(1);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    @SmallTest
    public void onPullAtom_simSlotState_beforeUiccControllerReady() throws Exception {
        // there is a slight chance that MetricsCollector gets pulled after registration while
        // PhoneFactory havne't made UiccController yet, RuntimeException will be thrown
        replaceInstance(UiccController.class, "mInstance", mUiccController, null);
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(SIM_SLOT_STATE, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SKIP);
    }

    @Test
    @SmallTest
    public void onPullAtom_supportedRadioAccessFamily_singlePhone() {
        doReturn(SUPPORTED_RAF_1).when(mPhone).getRadioAccessFamily();
        StatsEvent expectedAtom =
                StatsEvent.newBuilder()
                        .setAtomId(SUPPORTED_RADIO_ACCESS_FAMILY)
                        .writeLong(SUPPORTED_RAF_1)
                        .build();
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(SUPPORTED_RADIO_ACCESS_FAMILY, actualAtoms);

        assertThat(actualAtoms).hasSize(1);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    @SmallTest
    public void onPullAtom_supportedRadioAccessFamily_dualPhones() {
        doReturn(SUPPORTED_RAF_1).when(mPhone).getRadioAccessFamily();
        doReturn(SUPPORTED_RAF_2).when(mSecondPhone).getRadioAccessFamily();
        mPhones = new Phone[] {mPhone, mSecondPhone};
        StatsEvent expectedAtom =
                StatsEvent.newBuilder()
                        .setAtomId(SUPPORTED_RADIO_ACCESS_FAMILY)
                        .writeLong(SUPPORTED_RAF_BOTH)
                        .build();
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(SUPPORTED_RADIO_ACCESS_FAMILY, actualAtoms);

        assertThat(actualAtoms).hasSize(1);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    @SmallTest
    public void onPullAtom_supportedRadioAccessFamily_dualPhonesWithUnknownRaf() {
        doReturn(SUPPORTED_RAF_1).when(mPhone).getRadioAccessFamily();
        doReturn((int) TelephonyManager.NETWORK_TYPE_BITMASK_UNKNOWN)
                .when(mSecondPhone)
                .getRadioAccessFamily();
        mPhones = new Phone[] {mPhone, mSecondPhone};
        StatsEvent expectedAtom =
                StatsEvent.newBuilder()
                        .setAtomId(SUPPORTED_RADIO_ACCESS_FAMILY)
                        .writeLong(SUPPORTED_RAF_1)
                        .build();
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(SUPPORTED_RADIO_ACCESS_FAMILY, actualAtoms);

        assertThat(actualAtoms).hasSize(1);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    @SmallTest
    public void onPullAtom_supportedRadioAccessFamily_beforePhoneReady() throws Exception {
        replaceInstance(PhoneFactory.class, "sMadeDefaults", true, false);
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(SUPPORTED_RADIO_ACCESS_FAMILY, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SKIP);
    }

    @Test
    @SmallTest
    public void onPullAtom_voiceCallRatUsage_empty() throws Exception {
        doReturn(new VoiceCallRatUsage[0])
                .when(mPersistAtomsStorage)
                .getVoiceCallRatUsages(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(VOICE_CALL_RAT_USAGE, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
    }

    @Test
    @SmallTest
    public void onPullAtom_voiceCallRatUsage_tooFrequent() throws Exception {
        doReturn(null).when(mPersistAtomsStorage).getVoiceCallRatUsages(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(VOICE_CALL_RAT_USAGE, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SKIP);
        verify(mPersistAtomsStorage, times(1)).getVoiceCallRatUsages(eq(MIN_COOLDOWN_MILLIS));
        verifyNoMoreInteractions(mPersistAtomsStorage);
    }

    @Test
    @SmallTest
    public void onPullAtom_voiceCallRatUsage_bucketWithTooFewCalls() throws Exception {
        VoiceCallRatUsage usage1 = new VoiceCallRatUsage();
        usage1.callCount = MIN_CALLS_PER_BUCKET;
        VoiceCallRatUsage usage2 = new VoiceCallRatUsage();
        usage2.callCount = MIN_CALLS_PER_BUCKET - 1L;
        doReturn(new VoiceCallRatUsage[] {usage1, usage1, usage1, usage2})
                .when(mPersistAtomsStorage)
                .getVoiceCallRatUsages(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(VOICE_CALL_RAT_USAGE, actualAtoms);

        assertThat(actualAtoms).hasSize(3); // usage 2 should be dropped
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    @SmallTest
    public void onPullAtom_voiceCallSession_empty() throws Exception {
        doReturn(new VoiceCallSession[0])
                .when(mPersistAtomsStorage)
                .getVoiceCallSessions(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(VOICE_CALL_SESSION, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
    }

    @Test
    @SmallTest
    public void onPullAtom_voiceCallSession_tooFrequent() throws Exception {
        doReturn(null).when(mPersistAtomsStorage).getVoiceCallSessions(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(VOICE_CALL_SESSION, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SKIP);
        verify(mPersistAtomsStorage, times(1)).getVoiceCallSessions(eq(MIN_COOLDOWN_MILLIS));
        verifyNoMoreInteractions(mPersistAtomsStorage);
    }

    @Test
    @SmallTest
    public void onPullAtom_voiceCallSession_multipleCalls() throws Exception {
        VoiceCallSession call = new VoiceCallSession();
        doReturn(new VoiceCallSession[] {call, call, call, call})
                .when(mPersistAtomsStorage)
                .getVoiceCallSessions(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(VOICE_CALL_SESSION, actualAtoms);

        assertThat(actualAtoms).hasSize(4);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    @SmallTest
    public void onPullAtom_cellularDataServiceSwitch_empty() throws Exception {
        doReturn(new CellularDataServiceSwitch[0])
                .when(mPersistAtomsStorage)
                .getCellularDataServiceSwitches(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(CELLULAR_DATA_SERVICE_SWITCH, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    @SmallTest
    public void onPullAtom_cellularDataServiceSwitch_tooFrequent() throws Exception {
        doReturn(null).when(mPersistAtomsStorage).getCellularDataServiceSwitches(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(CELLULAR_DATA_SERVICE_SWITCH, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SKIP);
        verify(mPersistAtomsStorage, times(1))
                .getCellularDataServiceSwitches(eq(MIN_COOLDOWN_MILLIS));
        verifyNoMoreInteractions(mPersistAtomsStorage);
    }

    @Test
    @SmallTest
    public void onPullAtom_cellularDataServiceSwitch_multipleSwitches() throws Exception {
        CellularDataServiceSwitch serviceSwitch = new CellularDataServiceSwitch();
        doReturn(new CellularDataServiceSwitch[] {serviceSwitch, serviceSwitch, serviceSwitch})
                .when(mPersistAtomsStorage)
                .getCellularDataServiceSwitches(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(CELLULAR_DATA_SERVICE_SWITCH, actualAtoms);

        assertThat(actualAtoms).hasSize(3);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    @SmallTest
    public void onPullAtom_cellularServiceState_empty() throws Exception {
        doReturn(new CellularServiceState[0])
                .when(mPersistAtomsStorage)
                .getCellularServiceStates(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(CELLULAR_SERVICE_STATE, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    @SmallTest
    public void onPullAtom_cellularServiceState_tooFrequent() throws Exception {
        doReturn(null).when(mPersistAtomsStorage).getCellularServiceStates(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(CELLULAR_SERVICE_STATE, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SKIP);
        verify(mPersistAtomsStorage, times(1)).getCellularServiceStates(eq(MIN_COOLDOWN_MILLIS));
        verifyNoMoreInteractions(mPersistAtomsStorage);
    }

    @Test
    @SmallTest
    public void onPullAtom_cellularServiceState_multipleStates() throws Exception {
        CellularServiceState state = new CellularServiceState();
        doReturn(new CellularServiceState[] {state, state, state})
                .when(mPersistAtomsStorage)
                .getCellularServiceStates(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(CELLULAR_SERVICE_STATE, actualAtoms);

        assertThat(actualAtoms).hasSize(3);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
        // TODO(b/153196254): verify atom contents
    }

    @Test
    public void onPullAtom_outgoingShortCodeSms_empty() {
        doReturn(new OutgoingShortCodeSms[0]).when(mPersistAtomsStorage)
                .getOutgoingShortCodeSms(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(OUTGOING_SHORT_CODE_SMS, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
    }

    @Test
    public void onPullAtom_outgoingShortCodeSms_tooFrequent() {
        doReturn(null).when(mPersistAtomsStorage).getOutgoingShortCodeSms(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(OUTGOING_SHORT_CODE_SMS, actualAtoms);

        assertThat(actualAtoms).hasSize(0);
        assertThat(result).isEqualTo(StatsManager.PULL_SKIP);
        verify(mPersistAtomsStorage, times(1))
                .getOutgoingShortCodeSms(eq(MIN_COOLDOWN_MILLIS));
        verifyNoMoreInteractions(mPersistAtomsStorage);
    }

    @Test
    public void onPullAtom_outgoingShortCodeSms_multipleSms() {
        OutgoingShortCodeSms outgoingShortCodeSms = new OutgoingShortCodeSms();
        doReturn(new OutgoingShortCodeSms[] {outgoingShortCodeSms, outgoingShortCodeSms,
                outgoingShortCodeSms, outgoingShortCodeSms})
                .when(mPersistAtomsStorage)
                .getOutgoingShortCodeSms(anyLong());
        List<StatsEvent> actualAtoms = new ArrayList<>();

        int result = mMetricsCollector.onPullAtom(OUTGOING_SHORT_CODE_SMS, actualAtoms);

        assertThat(actualAtoms).hasSize(4);
        assertThat(result).isEqualTo(StatsManager.PULL_SUCCESS);
    }
}