aboutsummaryrefslogtreecommitdiff
path: root/tests/telephonytests/src/com/android/internal/telephony/data/DataConfigManagerTest.java
blob: f3128089aa04230bedb1d506948cb64007f4366f (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
/*
 * Copyright (C) 2022 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.data;

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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;

import android.os.Looper;
import android.os.PersistableBundle;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;

import com.android.internal.telephony.TelephonyTest;

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

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
public class DataConfigManagerTest extends TelephonyTest {
    private DataConfigManager mDataConfigManagerUT;
    private PersistableBundle mBundle;

    @Before
    public void setUp() throws Exception {
        logd("DataConfigManagerTest +Setup!");
        super.setUp(getClass().getSimpleName());
        mBundle = mContextFixture.getCarrierConfigBundle();
        when(mCarrierConfigManager.getConfigForSubId(anyInt(), any())).thenReturn(mBundle);
        mDataConfigManagerUT = new DataConfigManager(mPhone, Looper.myLooper());
        logd("DataConfigManagerTest -Setup!");
    }

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

    @Test
    public void testParseSlidingWindowCounterThreshold() {
        long defaultTimeWindow = 0;
        int defaultOccurrence = 2;
        DataConfigManager.EventFrequency defaultValue = new DataConfigManager.EventFrequency(0, 2);

        DataConfigManager.EventFrequency normal =
                mDataConfigManagerUT.parseSlidingWindowCounterThreshold(Long.MAX_VALUE + ","
                        + Integer.MAX_VALUE, defaultTimeWindow, defaultOccurrence);
        DataConfigManager.EventFrequency expected =
                new DataConfigManager.EventFrequency(Long.MAX_VALUE, Integer.MAX_VALUE);
        assertThat(normal.timeWindow).isEqualTo(expected.timeWindow);
        assertThat(normal.eventNumOccurrence).isEqualTo(expected.eventNumOccurrence);

        //allow " time , occurrences ," as we can infer even though format is not strictly valid
        DataConfigManager.EventFrequency invalidFormat = mDataConfigManagerUT
                .parseSlidingWindowCounterThreshold(
                        Long.MAX_VALUE + "," + Integer.MAX_VALUE + " ,",
                        defaultTimeWindow, defaultOccurrence);
        assertThat(invalidFormat.timeWindow).isEqualTo(Long.MAX_VALUE);
        assertThat(invalidFormat.eventNumOccurrence).isEqualTo(Integer.MAX_VALUE);

        DataConfigManager.EventFrequency invalidRange = mDataConfigManagerUT
                .parseSlidingWindowCounterThreshold(
                        Long.MAX_VALUE + "," + Long.MAX_VALUE, defaultTimeWindow,
                        defaultOccurrence);
        assertThat(invalidRange.timeWindow).isEqualTo(defaultValue.timeWindow);
        assertThat(invalidRange.eventNumOccurrence).isEqualTo(defaultValue.eventNumOccurrence);

        DataConfigManager.EventFrequency invalidFormat2 = mDataConfigManagerUT
                .parseSlidingWindowCounterThreshold("", defaultTimeWindow, defaultOccurrence);
        assertThat(invalidFormat2.timeWindow).isEqualTo(defaultValue.timeWindow);
        assertThat(invalidFormat2.eventNumOccurrence).isEqualTo(defaultValue.eventNumOccurrence);

        DataConfigManager.EventFrequency invalidFormat3 = mDataConfigManagerUT
                .parseSlidingWindowCounterThreshold(null, defaultTimeWindow, defaultOccurrence);
        assertThat(invalidFormat3.timeWindow).isEqualTo(defaultValue.timeWindow);
        assertThat(invalidFormat3.eventNumOccurrence).isEqualTo(defaultValue.eventNumOccurrence);
    }
}