1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.internal.telephony;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.os.Parcel;
24 import android.telephony.SignalThresholdInfo;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import junit.framework.TestCase;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 
37 @RunWith(JUnit4.class)
38 public class SignalThresholdInfoTest extends TestCase {
39     private static final int HYSTERESIS_DB = 2;
40     private static final int HYSTERESIS_MS = 30;
41     private static final int[] SSRSRP_THRESHOLDS = new int[] {-30, 10, 45, 130};
42 
43     private final int[] mRssiThresholds = new int[] {-109, -103, -97, -89};
44     private final int[] mRscpThresholds = new int[] {-115, -105, -95, -85};
45     private final int[] mRsrpThresholds = new int[] {-128, -118, -108, -98};
46     private final int[] mRsrqThresholds = new int[] {-19, -17, -14, -12};
47     private final int[] mRssnrThresholds = new int[] {-30, 10, 45, 130};
48     private final int[][] mThresholds = new int[5][];
49 
50     @Test
51     @SmallTest
testSignalThresholdInfo()52     public void testSignalThresholdInfo() throws Exception {
53         SignalThresholdInfo signalThresholdInfo = new SignalThresholdInfo(
54                 SignalThresholdInfo.SIGNAL_SSRSRP,
55                 HYSTERESIS_MS,
56                 HYSTERESIS_DB,
57                 SSRSRP_THRESHOLDS,
58                 false);
59 
60         assertEquals(SignalThresholdInfo.SIGNAL_SSRSRP,
61                 signalThresholdInfo.getSignalMeasurement());
62         assertEquals(HYSTERESIS_MS, signalThresholdInfo.getHysteresisMs());
63         assertEquals(HYSTERESIS_DB, signalThresholdInfo.getHysteresisDb());
64         assertEquals(Arrays.toString(SSRSRP_THRESHOLDS), Arrays.toString(
65                 signalThresholdInfo.getThresholds()));
66         assertFalse(signalThresholdInfo.isEnabled());
67     }
68 
69     @Test
70     @SmallTest
testDefaultThresholdsConstruction()71     public void testDefaultThresholdsConstruction() {
72         setThresholds();
73         ArrayList<SignalThresholdInfo> stList = setSignalThresholdInfoConstructor();
74 
75         int count = 0;
76         for (SignalThresholdInfo st : stList) {
77             assertThat(st.getThresholds()).isEqualTo(mThresholds[count]);
78             count++;
79         }
80     }
81 
82     @Test
83     @SmallTest
testDefaultThresholdsParcel()84     public void testDefaultThresholdsParcel() {
85         ArrayList<SignalThresholdInfo> stList = setSignalThresholdInfoConstructor();
86 
87         for (SignalThresholdInfo st : stList) {
88             Parcel p = Parcel.obtain();
89             st.writeToParcel(p, 0);
90             p.setDataPosition(0);
91 
92             SignalThresholdInfo newSt = SignalThresholdInfo.CREATOR.createFromParcel(p);
93             assertThat(newSt).isEqualTo(st);
94         }
95     }
96 
97     @Test
98     @SmallTest
testGetSignalThresholdInfo()99     public void testGetSignalThresholdInfo() {
100         ArrayList<SignalThresholdInfo> stList = new ArrayList<>();
101         stList.add(new SignalThresholdInfo(0, 0, 0, null, false));
102         stList.add(new SignalThresholdInfo(SignalThresholdInfo.SIGNAL_RSSI, HYSTERESIS_MS,
103                 HYSTERESIS_DB, mRssiThresholds, false));
104 
105         assertThat(stList.get(0).getThresholds()).isEqualTo(null);
106         assertThat(stList.get(1).getSignalMeasurement()).isEqualTo(SignalThresholdInfo.SIGNAL_RSSI);
107         assertThat(stList.get(1).getThresholds()).isEqualTo(mRssiThresholds);
108     }
109 
110     @Test
111     @SmallTest
testEqualsSignalThresholdInfo()112     public void testEqualsSignalThresholdInfo() {
113         final int[] dummyThresholds = new int[] {-100, -1, 1, 100};
114         SignalThresholdInfo st1 = new SignalThresholdInfo(1, HYSTERESIS_MS, HYSTERESIS_DB,
115                 mRssiThresholds, false);
116         SignalThresholdInfo st2 = new SignalThresholdInfo(2, HYSTERESIS_MS, HYSTERESIS_DB,
117                 mRssiThresholds, false);
118         SignalThresholdInfo st3 = new SignalThresholdInfo(1, HYSTERESIS_MS, HYSTERESIS_DB,
119                 dummyThresholds, false);
120         SignalThresholdInfo st4 = new SignalThresholdInfo(1, HYSTERESIS_MS, HYSTERESIS_DB,
121                 mRssiThresholds, false);
122 
123         //Return true if all SignalThresholdInfo values match.
124         assertTrue(st1.equals(st1));
125         assertFalse(st1.equals(st2));
126         assertFalse(st1.equals(st3));
127         assertTrue(st1.equals(st4));
128         //Return false if the object of argument is other than SignalThresholdInfo.
129         assertFalse(st1.equals(new String("test")));
130     }
131 
setThresholds()132     private void setThresholds() {
133         mThresholds[0] = mRssiThresholds;
134         mThresholds[1] = mRscpThresholds;
135         mThresholds[2] = mRsrpThresholds;
136         mThresholds[3] = mRsrqThresholds;
137         mThresholds[4] = mRssnrThresholds;
138     }
139 
setSignalThresholdInfoConstructor()140     private ArrayList<SignalThresholdInfo> setSignalThresholdInfoConstructor() {
141         ArrayList<SignalThresholdInfo> stList = new ArrayList<>();
142         stList.add(new SignalThresholdInfo(SignalThresholdInfo.SIGNAL_RSSI, HYSTERESIS_MS,
143                 HYSTERESIS_DB, mRssiThresholds, false));
144         stList.add(new SignalThresholdInfo(SignalThresholdInfo.SIGNAL_RSCP, HYSTERESIS_MS,
145                 HYSTERESIS_DB, mRscpThresholds, false));
146         stList.add(new SignalThresholdInfo(SignalThresholdInfo.SIGNAL_RSRP, HYSTERESIS_MS,
147                 HYSTERESIS_DB, mRsrpThresholds, false));
148         stList.add(new SignalThresholdInfo(SignalThresholdInfo.SIGNAL_RSRQ, HYSTERESIS_MS,
149                 HYSTERESIS_DB, mRsrqThresholds, false));
150         stList.add(new SignalThresholdInfo(SignalThresholdInfo.SIGNAL_RSSNR, HYSTERESIS_MS,
151                 HYSTERESIS_DB, mRssnrThresholds, false));
152 
153         return stList;
154     }
155 }
156