1 /*
2  * Copyright (C) 2018 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;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.os.Parcel;
23 import android.os.PersistableBundle;
24 import android.telephony.CarrierConfigManager;
25 import android.telephony.CellInfo;
26 import android.telephony.CellSignalStrength;
27 import android.telephony.CellSignalStrengthCdma;
28 import android.telephony.CellSignalStrengthGsm;
29 import android.telephony.CellSignalStrengthLte;
30 import android.telephony.CellSignalStrengthNr;
31 import android.telephony.CellSignalStrengthTdscdma;
32 import android.telephony.CellSignalStrengthWcdma;
33 import android.telephony.SignalStrength;
34 
35 import androidx.test.filters.SmallTest;
36 
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.junit.runners.JUnit4;
40 
41 import java.util.List;
42 
43 /** Unit tests for {@link IpSecConfig}. */
44 @SmallTest
45 @RunWith(JUnit4.class)
46 public class SignalStrengthTest {
47     private static final int[] DEFAULT_LTE_RSRP_THRESHOLDS = {
48             -128,  // SIGNAL_STRENGTH_POOR
49             -118,  // SIGNAL_STRENGTH_MODERATE
50             -108,  // SIGNAL_STRENGTH_GOOD
51             -98 }; // SIGNAL_STRENGTH_GREAT
52 
53     private static final int[] DEFAULT_LTE_RSRQ_THRESHOLDS = {
54             -19,   // SIGNAL_STRENGTH_POOR
55             -17,   // SIGNAL_STRENGTH_MODERATE
56             -14,   // SIGNAL_STRENGTH_GOOD
57             -12 }; // SIGNAL_STRENGTH_GREAT
58 
59     private static final int[] DEFAULT_LTE_RSSNR_THRESHOLDS = {
60             -3,   // SIGNAL_STRENGTH_POOR
61             1,    // SIGNAL_STRENGTH_MODERATE
62             5,    // SIGNAL_STRENGTH_GOOD
63             13 }; // SIGNAL_STRENGTH_GREAT
64 
65     private static final int[] DEFAULT_5G_NR_SSRSRP_THRESHOLDS = {
66             -125,  // SIGNAL_STRENGTH_POOR
67             -115,  // SIGNAL_STRENGTH_MODERATE
68             -105,  // SIGNAL_STRENGTH_GOOD
69             -95 }; // SIGNAL_STRENGTH_GREAT
70 
71     @Test
testDefaults()72     public void testDefaults() throws Exception {
73         SignalStrength s = new SignalStrength();
74         assertEquals(CellInfo.UNAVAILABLE, s.getCdmaDbm());
75         assertEquals(CellInfo.UNAVAILABLE, s.getCdmaEcio());
76         assertEquals(CellInfo.UNAVAILABLE, s.getEvdoDbm());
77         assertEquals(CellInfo.UNAVAILABLE, s.getEvdoEcio());
78         assertEquals(CellInfo.UNAVAILABLE, s.getEvdoSnr());
79         assertEquals(CellInfo.UNAVAILABLE, s.getGsmBitErrorRate());
80         // getGsmSignalStrength is an oddball because internally it actually returns an AsuLevel
81         // rather than a dBm value. For backwards compatibility reasons, this is left as the
82         // RSSI ASU value [0-31], 99.
83         assertEquals(99, s.getGsmSignalStrength());
84         assertEquals(true, s.isGsm());
85     }
86 
87     @Test
testParcelUnparcel()88     public void testParcelUnparcel() throws Exception {
89         assertParcelingIsLossless(new SignalStrength());
90 
91         SignalStrength s = new SignalStrength(
92                 new CellSignalStrengthCdma(-93, -132, -89, -125, 5),
93                 new CellSignalStrengthGsm(-79, 2, 5),
94                 new CellSignalStrengthWcdma(-94, 4, -102, -5),
95                 new CellSignalStrengthTdscdma(-95, 2, -103),
96                 new CellSignalStrengthLte(-85, -91, -6, -10, 12, 1),
97                 new CellSignalStrengthNr(-91, -6, 3, -80, -7, 4));
98         assertParcelingIsLossless(s);
99 
100         PersistableBundle bundle = new PersistableBundle();
101         bundle.putIntArray(
102                 CarrierConfigManager.KEY_LTE_RSRQ_THRESHOLDS_INT_ARRAY,
103                 DEFAULT_LTE_RSRQ_THRESHOLDS);
104         bundle.putIntArray(
105                 CarrierConfigManager.KEY_LTE_RSRP_THRESHOLDS_INT_ARRAY,
106                 DEFAULT_LTE_RSRP_THRESHOLDS);
107         bundle.putIntArray(
108                 CarrierConfigManager.KEY_LTE_RSSNR_THRESHOLDS_INT_ARRAY,
109                 DEFAULT_LTE_RSSNR_THRESHOLDS);
110         bundle.putIntArray(
111                 CarrierConfigManager.KEY_5G_NR_SSRSRP_THRESHOLDS_INT_ARRAY,
112                 DEFAULT_5G_NR_SSRSRP_THRESHOLDS);
113 
114         s.updateLevel(bundle, null);
115         assertParcelingIsLossless(s);
116     }
117 
assertParcelingIsLossless(SignalStrength ssi)118     private void assertParcelingIsLossless(SignalStrength ssi) throws Exception {
119         Parcel p = Parcel.obtain();
120         ssi.writeToParcel(p, 0);
121         p.setDataPosition(0);
122         SignalStrength sso = SignalStrength.CREATOR.createFromParcel(p);
123         assertTrue(sso.equals(ssi));
124     }
125 
126     @Test
testGetCellSignalStrengths()127     public void testGetCellSignalStrengths() throws Exception {
128         CellSignalStrengthLte lte = new CellSignalStrengthLte(-85, -91, -6, -10, 12, 1);
129         CellSignalStrengthGsm gsm = new CellSignalStrengthGsm(-79, 2, 5);
130         CellSignalStrengthCdma cdma = new CellSignalStrengthCdma(-93, -132, -89, -125, 5);
131         CellSignalStrengthWcdma wcdma = new CellSignalStrengthWcdma(-94, 4, -102, -5);
132         CellSignalStrengthTdscdma tdscdma = new CellSignalStrengthTdscdma(-95, 2, -103);
133 
134         // Test that a single object is properly stored and returned by getCellSignalStrengths()
135         SignalStrength s = new SignalStrength(
136                 new CellSignalStrengthCdma(),
137                 gsm,
138                 new CellSignalStrengthWcdma(),
139                 new CellSignalStrengthTdscdma(),
140                 new CellSignalStrengthLte(),
141                 new CellSignalStrengthNr());
142 
143         List<CellSignalStrength> css = s.getCellSignalStrengths();
144         assertEquals(1, css.size());
145         assertTrue(gsm.equals(css.get(0)));
146 
147         // Test that a multiple objects are properly stored and returned by getCellSignalStrengths()
148         s = new SignalStrength(
149                 cdma,
150                 new CellSignalStrengthGsm(),
151                 new CellSignalStrengthWcdma(),
152                 new CellSignalStrengthTdscdma(),
153                 lte,
154                 new CellSignalStrengthNr());
155 
156         css = s.getCellSignalStrengths();
157         assertEquals(2, css.size());
158         assertTrue(css.contains(cdma));
159         assertTrue(css.contains(lte));
160     }
161 
createSignalStrengthLteReportRsrq(int lteRsrp, int lteRsrq)162     private static SignalStrength createSignalStrengthLteReportRsrq(int lteRsrp, int lteRsrq) {
163 
164         CellSignalStrengthLte lte = new CellSignalStrengthLte(
165                 -89,                   // rssi
166                 lteRsrp,               // rsrp
167                 lteRsrq,               // rsrq
168                 -25,                   // rssnr
169                 CellInfo.UNAVAILABLE,  // cqi
170                 CellInfo.UNAVAILABLE); // timingAdvance
171 
172         SignalStrength signalStrength = new SignalStrength(
173                 new CellSignalStrengthCdma(),
174                 new CellSignalStrengthGsm(),
175                 new CellSignalStrengthWcdma(),
176                 new CellSignalStrengthTdscdma(),
177                 lte,
178                 new CellSignalStrengthNr());
179 
180         PersistableBundle bundle = new PersistableBundle();
181         bundle.putInt(
182                 CarrierConfigManager.KEY_PARAMETERS_USED_FOR_LTE_SIGNAL_BAR_INT,
183                 CellSignalStrengthLte.USE_RSRP | CellSignalStrengthLte.USE_RSRQ);
184         bundle.putIntArray(
185                 CarrierConfigManager.KEY_LTE_RSRQ_THRESHOLDS_INT_ARRAY,
186                 DEFAULT_LTE_RSRQ_THRESHOLDS);
187         bundle.putIntArray(
188                 CarrierConfigManager.KEY_LTE_RSRP_THRESHOLDS_INT_ARRAY,
189                 DEFAULT_LTE_RSRP_THRESHOLDS);
190         signalStrength.updateLevel(bundle, null);
191         return signalStrength;
192     }
193 
createSignalStrengthLteReportRssnr(int lteRsrp, int lteRssnr)194     private static SignalStrength createSignalStrengthLteReportRssnr(int lteRsrp, int lteRssnr) {
195 
196         CellSignalStrengthLte lte = new CellSignalStrengthLte(
197                 -89,                   // rssi
198                 lteRsrp,               // rsrp
199                 15,                    // rsrq
200                 lteRssnr,              // rssnr
201                 CellInfo.UNAVAILABLE,  // cqi
202                 CellInfo.UNAVAILABLE); // timingAdvance
203 
204         SignalStrength signalStrength = new SignalStrength(
205                 new CellSignalStrengthCdma(),
206                 new CellSignalStrengthGsm(),
207                 new CellSignalStrengthWcdma(),
208                 new CellSignalStrengthTdscdma(),
209                 lte,
210                 new CellSignalStrengthNr());
211 
212         PersistableBundle bundle = new PersistableBundle();
213         bundle.putInt(
214                 CarrierConfigManager.KEY_PARAMETERS_USED_FOR_LTE_SIGNAL_BAR_INT,
215                 CellSignalStrengthLte.USE_RSRP | CellSignalStrengthLte.USE_RSSNR);
216         bundle.putIntArray(
217                 CarrierConfigManager.KEY_LTE_RSRP_THRESHOLDS_INT_ARRAY,
218                 DEFAULT_LTE_RSRP_THRESHOLDS);
219         bundle.putIntArray(
220                 CarrierConfigManager.KEY_LTE_RSSNR_THRESHOLDS_INT_ARRAY,
221                 DEFAULT_LTE_RSSNR_THRESHOLDS);
222         signalStrength.updateLevel(bundle, null);
223         return signalStrength;
224     }
225 
226     @Test
testValidateInput()227     public void testValidateInput() throws Exception {
228 
229         SignalStrength ss;
230 
231         // Input value of RSRQ: 4[dB]
232         ss = createSignalStrengthLteReportRsrq(-60, 4);
233         assertEquals(SignalStrength.INVALID, ss.getLteRsrq());
234 
235         // Input value of RSRQ: 3[dB]
236         ss = createSignalStrengthLteReportRsrq(-60, 3);
237         assertEquals(3, ss.getLteRsrq());
238 
239         // Input value of RSRQ: -34[dB]
240         ss = createSignalStrengthLteReportRsrq(-60, -34);
241         assertEquals(-34, ss.getLteRsrq());
242 
243         // Input value of RSRQ: -35[dB]
244         ss = createSignalStrengthLteReportRsrq(-60, -35);
245         assertEquals(SignalStrength.INVALID, ss.getLteRsrq());
246 
247         // Input value of RSSNR: 31[dB]
248         ss = createSignalStrengthLteReportRssnr(-60, 31);
249         assertEquals(SignalStrength.INVALID, ss.getLteRssnr());
250 
251         // Input value of RSSNR: 30[dB]
252         ss = createSignalStrengthLteReportRssnr(-60, 30);
253         assertEquals(30, ss.getLteRssnr());
254 
255         // Input value of RSSNR: -20[dB]
256         ss = createSignalStrengthLteReportRssnr(60, -20);
257         assertEquals(-20, ss.getLteRssnr());
258 
259         // Input value of RSSNR: -21[dB]
260         ss = createSignalStrengthLteReportRssnr(60, -21);
261         assertEquals(SignalStrength.INVALID, ss.getLteRssnr());
262     }
263 
264     @Test
testRsrqThresholds_rsrp_great()265     public void testRsrqThresholds_rsrp_great() throws Exception {
266         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
267                 createSignalStrengthLteReportRsrq(-98, -34).getLteLevel());
268         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
269                 createSignalStrengthLteReportRsrq(-98, -19).getLteLevel());
270         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
271                 createSignalStrengthLteReportRsrq(-98, -17).getLteLevel());
272         assertEquals(SignalStrength.SIGNAL_STRENGTH_GOOD,
273                 createSignalStrengthLteReportRsrq(-98, -14).getLteLevel());
274         assertEquals(SignalStrength.SIGNAL_STRENGTH_GREAT,
275                 createSignalStrengthLteReportRsrq(-98, -12).getLteLevel());
276     }
277 
278     @Test
testRsrqThresholds_rsrp_good()279     public void testRsrqThresholds_rsrp_good() throws Exception {
280         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
281                 createSignalStrengthLteReportRsrq(-108, -34).getLteLevel());
282         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
283                 createSignalStrengthLteReportRsrq(-108, -19).getLteLevel());
284         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
285                 createSignalStrengthLteReportRsrq(-108, -17).getLteLevel());
286         assertEquals(SignalStrength.SIGNAL_STRENGTH_GOOD,
287                 createSignalStrengthLteReportRsrq(-108, -14).getLteLevel());
288         assertEquals(SignalStrength.SIGNAL_STRENGTH_GOOD,
289                 createSignalStrengthLteReportRsrq(-108, -12).getLteLevel());
290     }
291 
292     @Test
testRsrqThresholds_rsrp_moderate()293     public void testRsrqThresholds_rsrp_moderate() throws Exception {
294         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
295                 createSignalStrengthLteReportRsrq(-118, -34).getLteLevel());
296         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
297                 createSignalStrengthLteReportRsrq(-118, -19).getLteLevel());
298         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
299                 createSignalStrengthLteReportRsrq(-118, -17).getLteLevel());
300         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
301                 createSignalStrengthLteReportRsrq(-118, -14).getLteLevel());
302         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
303                 createSignalStrengthLteReportRsrq(-118, -12).getLteLevel());
304     }
305 
306     @Test
testRsrqThresholds_rsrp_poor()307     public void testRsrqThresholds_rsrp_poor() throws Exception {
308         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
309                 createSignalStrengthLteReportRsrq(-128, -34).getLteLevel());
310         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
311                 createSignalStrengthLteReportRsrq(-128, -19).getLteLevel());
312         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
313                 createSignalStrengthLteReportRsrq(-128, -17).getLteLevel());
314         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
315                 createSignalStrengthLteReportRsrq(-128, -14).getLteLevel());
316         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
317                 createSignalStrengthLteReportRsrq(-128, -12).getLteLevel());
318     }
319 
320     @Test
testRsrqThresholds_rsrp_unknown()321     public void testRsrqThresholds_rsrp_unknown() throws Exception {
322         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
323                 createSignalStrengthLteReportRsrq(-138, -34).getLteLevel());
324         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
325                 createSignalStrengthLteReportRsrq(-138, -19).getLteLevel());
326         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
327                 createSignalStrengthLteReportRsrq(-138, -17).getLteLevel());
328         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
329                 createSignalStrengthLteReportRsrq(-138, -14).getLteLevel());
330         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
331                 createSignalStrengthLteReportRsrq(-138, -12).getLteLevel());
332     }
333 
334     @Test
testRssnrThresholds_rsrp_great()335     public void testRssnrThresholds_rsrp_great() throws Exception {
336         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
337                 createSignalStrengthLteReportRssnr(-98, -20).getLteLevel());
338         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
339                 createSignalStrengthLteReportRssnr(-98, -3).getLteLevel());
340         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
341                 createSignalStrengthLteReportRssnr(-98, 1).getLteLevel());
342         assertEquals(SignalStrength.SIGNAL_STRENGTH_GOOD,
343                 createSignalStrengthLteReportRssnr(-98, 5).getLteLevel());
344         assertEquals(SignalStrength.SIGNAL_STRENGTH_GREAT,
345                 createSignalStrengthLteReportRssnr(-98, 13).getLteLevel());
346     }
347 
348     @Test
testRssnrThresholds_rsrp_good()349     public void testRssnrThresholds_rsrp_good() throws Exception {
350         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
351                 createSignalStrengthLteReportRssnr(-108, -20).getLteLevel());
352         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
353                 createSignalStrengthLteReportRssnr(-108, -3).getLteLevel());
354         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
355                 createSignalStrengthLteReportRssnr(-108, 1).getLteLevel());
356         assertEquals(SignalStrength.SIGNAL_STRENGTH_GOOD,
357                 createSignalStrengthLteReportRssnr(-108, 5).getLteLevel());
358         assertEquals(SignalStrength.SIGNAL_STRENGTH_GOOD,
359                 createSignalStrengthLteReportRssnr(-108, 13).getLteLevel());
360     }
361 
362     @Test
testRssnrThresholds_rsrp_moderate()363     public void testRssnrThresholds_rsrp_moderate() throws Exception {
364         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
365                 createSignalStrengthLteReportRssnr(-118, -20).getLteLevel());
366         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
367                 createSignalStrengthLteReportRssnr(-118, -3).getLteLevel());
368         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
369                 createSignalStrengthLteReportRssnr(-118, 1).getLteLevel());
370         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
371                 createSignalStrengthLteReportRssnr(-118, 5).getLteLevel());
372         assertEquals(SignalStrength.SIGNAL_STRENGTH_MODERATE,
373                 createSignalStrengthLteReportRssnr(-118, 13).getLteLevel());
374     }
375 
376     @Test
testRssnrThresholds_rsrp_poor()377     public void testRssnrThresholds_rsrp_poor() throws Exception {
378         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
379                 createSignalStrengthLteReportRssnr(-128, -20).getLteLevel());
380         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
381                 createSignalStrengthLteReportRssnr(-128, -3).getLteLevel());
382         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
383                 createSignalStrengthLteReportRssnr(-128, 1).getLteLevel());
384         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
385                 createSignalStrengthLteReportRssnr(-128, 5).getLteLevel());
386         assertEquals(SignalStrength.SIGNAL_STRENGTH_POOR,
387                 createSignalStrengthLteReportRssnr(-128, 13).getLteLevel());
388     }
389 
390     @Test
testRssnrThresholds_rsrp_unknown()391     public void testRssnrThresholds_rsrp_unknown() throws Exception {
392         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
393                 createSignalStrengthLteReportRssnr(-138, -20).getLteLevel());
394         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
395                 createSignalStrengthLteReportRssnr(-138, -3).getLteLevel());
396         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
397                 createSignalStrengthLteReportRssnr(-138, 1).getLteLevel());
398         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
399                 createSignalStrengthLteReportRssnr(-138, 5).getLteLevel());
400         assertEquals(SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN,
401                 createSignalStrengthLteReportRssnr(-138, 13).getLteLevel());
402     }
403 }
404 
405