1 /*
2  * Copyright (C) 2022 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.cellbroadcastreceiver.unit;
18 
19 import static com.android.cellbroadcastreceiver.CellBroadcastReceiverMetrics.CBR_CONFIG_UPDATED;
20 import static com.android.cellbroadcastreceiver.CellBroadcastReceiverMetrics.FeatureMetrics.ALERT_IN_CALL;
21 import static com.android.cellbroadcastreceiver.CellBroadcastReceiverMetrics.FeatureMetrics.OVERRIDE_DND;
22 import static com.android.cellbroadcastreceiver.CellBroadcastReceiverMetrics.FeatureMetrics.ROAMING_SUPPORT;
23 import static com.android.cellbroadcastreceiver.CellBroadcastReceiverMetrics.FeatureMetrics.STORE_SMS;
24 import static com.android.cellbroadcastreceiver.CellBroadcastReceiverMetrics.FeatureMetrics.TEST_MODE;
25 import static com.android.cellbroadcastreceiver.CellBroadcastReceiverMetrics.FeatureMetrics.TEST_MODE_ON_USER_BUILD;
26 import static com.android.cellbroadcastreceiver.CellBroadcastReceiverMetrics.FeatureMetrics.TTS_MODE;
27 
28 import static org.junit.Assert.assertArrayEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotSame;
31 import static org.junit.Assert.assertSame;
32 import static org.junit.Assert.assertTrue;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.ArgumentMatchers.anyBoolean;
35 import static org.mockito.ArgumentMatchers.anyInt;
36 import static org.mockito.ArgumentMatchers.anyString;
37 import static org.mockito.Mockito.doNothing;
38 import static org.mockito.Mockito.doReturn;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.times;
41 import static org.mockito.Mockito.verify;
42 
43 import android.content.SharedPreferences;
44 import android.telephony.SubscriptionManager;
45 import android.util.Pair;
46 
47 import com.android.cellbroadcastreceiver.CellBroadcastChannelManager;
48 import com.android.cellbroadcastreceiver.CellBroadcastReceiverMetrics;
49 import com.android.cellbroadcastreceiver.Cellbroadcastmetric;
50 
51 import org.junit.After;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.mockito.Mock;
55 
56 import java.io.IOException;
57 import java.util.HashSet;
58 
59 public class CellBroadcastReceiverMetricsTest extends CellBroadcastTest {
60 
61     private static final String[] CHANNEL_CONFIG = {
62             "12:type=etws_earthquake, emergency=true, display=false, always_on=true",
63             "456:type=etws_tsunami, emergency=true, override_dnd=true, scope=domestic",
64             "0xAC00-0xAFED:type=other, emergency=false, override_dnd=true, scope=carrier",
65             "54-60:emergency=true, testing_mode=true, dialog_with_notification=true",
66             "100-200",
67             "0xA804:type=test, emergency=true, exclude_from_sms_inbox=true, "
68                     + "vibration=0|350|250|350",
69             "0x111E:debug_build=true"};
70 
71     private static final String OPERATOR = "123456";
72     private static final int SUB_ID = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
73 
74     private CellBroadcastChannelManager mChannelManager;
75 
76     @Mock
77     private SharedPreferences.Editor mEditor;
78 
79     @Before
setUp()80     public void setUp() throws Exception {
81         super.setUp(getClass().getSimpleName());
82 
83         doReturn(null).when(mTelephonyManager).getServiceState();
84         doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt());
85         doReturn(mContext).when(mContext).createConfigurationContext(any());
86 
87         CellBroadcastChannelManager.clearAllCellBroadcastChannelRanges();
88 
89         putResources(com.android.cellbroadcastreceiver.R.array.additional_cbs_channels_strings,
90                 CHANNEL_CONFIG);
91         mChannelManager = new CellBroadcastChannelManager(mContext, SUB_ID, null, false);
92 
93         doReturn(false).when(mSharedPreferences).getBoolean(anyString(), anyBoolean());
94         doReturn(String.valueOf(0)).when(mSharedPreferences).getString(anyString(), anyString());
95 
96         doReturn(mEditor).when(mSharedPreferences).edit();
97         doReturn(mEditor).when(mEditor).putBoolean(anyString(), anyBoolean());
98         doNothing().when(mEditor).apply();
99     }
100 
101     @After
tearDown()102     public void tearDown() throws Exception {
103         super.tearDown();
104     }
105 
106     @Test
testGetInstance()107     public void testGetInstance() {
108         CellBroadcastReceiverMetrics testFeatureMetrics1 =
109                 CellBroadcastReceiverMetrics.getInstance();
110         CellBroadcastReceiverMetrics testFeatureMetrics2 =
111                 CellBroadcastReceiverMetrics.getInstance();
112 
113         assertSame(testFeatureMetrics1, testFeatureMetrics2);
114     }
115 
116     @Test
testGetFeatureMetrics()117     public void testGetFeatureMetrics() {
118         CellBroadcastReceiverMetrics.FeatureMetrics testFeatureMetrics =
119                 CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext);
120         CellBroadcastReceiverMetrics.FeatureMetrics testFeatureMetricsClone =
121                 CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext);
122 
123         assertSame(testFeatureMetrics, testFeatureMetricsClone);
124 
125         doReturn(false).when(mSharedPreferences)
126                 .getBoolean(ALERT_IN_CALL, false);
127         doReturn(String.valueOf(0)).when(mSharedPreferences)
128                 .getString(OVERRIDE_DND, String.valueOf(0));
129         doReturn(false).when(mSharedPreferences)
130                 .getBoolean(ROAMING_SUPPORT, false);
131         doReturn(false).when(mSharedPreferences)
132                 .getBoolean(STORE_SMS, false);
133         doReturn(false).when(mSharedPreferences)
134                 .getBoolean(TEST_MODE, false);
135         doReturn(true).when(mSharedPreferences)
136                 .getBoolean(TTS_MODE, true);
137         doReturn(true).when(mSharedPreferences)
138                 .getBoolean(TEST_MODE_ON_USER_BUILD, true);
139 
140         CellBroadcastReceiverMetrics.getInstance().setFeatureMetrics(null);
141         CellBroadcastReceiverMetrics.getInstance().setFeatureMetricsSharedPreferences(null);
142 
143         testFeatureMetrics = CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(
144                 mContext);
145 
146 
147         assertFalse(testFeatureMetrics.isAlertDuringCall());
148         HashSet<Pair<Integer, Integer>> compareSet = new HashSet<>();
149         compareSet.add(new Pair(0, 0));
150         assertTrue(testFeatureMetrics.getDnDChannelSet().equals(compareSet));
151         assertFalse(testFeatureMetrics.isRoamingSupport());
152         assertFalse(testFeatureMetrics.isStoreSms());
153         assertFalse(testFeatureMetrics.isTestMode());
154         assertTrue(testFeatureMetrics.isEnableAlertSpeech());
155         assertTrue(testFeatureMetrics.isTestModeOnUserBuild());
156 
157         CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext)
158                 .onChangedAlertDuringCall(true);
159         CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext)
160                 .onChangedOverrideDnD(mChannelManager, false);
161         CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext)
162                 .onChangedRoamingSupport(true);
163         CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext)
164                 .onChangedStoreSms(true);
165         CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext)
166                 .onChangedTestMode(true);
167         CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext)
168                 .onChangedEnableAlertSpeech(true);
169         CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext)
170                 .onChangedTestModeOnUserBuild(false);
171 
172         testFeatureMetrics = CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(
173                 mContext);
174 
175         assertTrue(testFeatureMetrics.isAlertDuringCall());
176         compareSet.clear();
177         compareSet.add(new Pair(456, 456));
178         compareSet.add(new Pair(44032, 45037));
179         assertTrue(testFeatureMetrics.getDnDChannelSet().equals(compareSet));
180         assertTrue(testFeatureMetrics.isRoamingSupport());
181         assertTrue(testFeatureMetrics.isStoreSms());
182         assertTrue(testFeatureMetrics.isTestMode());
183         assertTrue(testFeatureMetrics.isEnableAlertSpeech());
184         assertFalse(testFeatureMetrics.isTestModeOnUserBuild());
185 
186         CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext)
187                 .onChangedOverrideDnD(mChannelManager, true);
188         testFeatureMetrics = CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(
189                 mContext);
190         compareSet.clear();
191         compareSet.add(new Pair(Integer.MAX_VALUE, Integer.MAX_VALUE));
192         assertTrue(testFeatureMetrics.getDnDChannelSet().equals(compareSet));
193     }
194 
195     @Test
testEquals()196     public void testEquals() {
197         CellBroadcastReceiverMetrics.getInstance().setFeatureMetrics(null);
198         CellBroadcastReceiverMetrics.FeatureMetrics testFeatureMetrics =
199                 CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext);
200         CellBroadcastReceiverMetrics.FeatureMetrics testSharedPreferenceFeatureMetrics =
201                 CellBroadcastReceiverMetrics.getInstance().getFeatureMetricsSharedPreferences();
202 
203         assertTrue(testFeatureMetrics.equals(testSharedPreferenceFeatureMetrics));
204         assertNotSame(testFeatureMetrics, testSharedPreferenceFeatureMetrics);
205 
206         testFeatureMetrics.onChangedAlertDuringCall(true);
207         testFeatureMetrics.onChangedOverrideDnD(mChannelManager, false);
208 
209         assertFalse(testFeatureMetrics.equals(testSharedPreferenceFeatureMetrics));
210 
211         testSharedPreferenceFeatureMetrics.onChangedAlertDuringCall(true);
212         testSharedPreferenceFeatureMetrics.onChangedOverrideDnD(mChannelManager, false);
213 
214         assertTrue(testFeatureMetrics.equals(testSharedPreferenceFeatureMetrics));
215 
216         testFeatureMetrics.onChangedOverrideDnD(mChannelManager, true);
217 
218         assertFalse(testFeatureMetrics.equals(testSharedPreferenceFeatureMetrics));
219     }
220 
221     @Test
testClone()222     public void testClone() throws CloneNotSupportedException {
223         CellBroadcastReceiverMetrics.getInstance().setFeatureMetrics(null);
224         CellBroadcastReceiverMetrics.FeatureMetrics testFeatureMetrics =
225                 CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext);
226         CellBroadcastReceiverMetrics.FeatureMetrics testSharedPreferenceFeatureMetrics =
227                 CellBroadcastReceiverMetrics.getInstance().getFeatureMetricsSharedPreferences();
228 
229         assertTrue(testFeatureMetrics.equals(testSharedPreferenceFeatureMetrics));
230 
231         testFeatureMetrics.onChangedAlertDuringCall(true);
232         testFeatureMetrics.onChangedOverrideDnD(mChannelManager, false);
233 
234         assertFalse(testFeatureMetrics.equals(testSharedPreferenceFeatureMetrics));
235 
236         testSharedPreferenceFeatureMetrics =
237                 (CellBroadcastReceiverMetrics.FeatureMetrics) testFeatureMetrics.clone();
238 
239         assertTrue(testFeatureMetrics.equals(testSharedPreferenceFeatureMetrics));
240         assertNotSame(testFeatureMetrics, testSharedPreferenceFeatureMetrics);
241     }
242 
243     @Test
testConvertToProtoBuffer()244     public void testConvertToProtoBuffer() throws IOException {
245         HashSet<Pair<Integer, Integer>> testChannelSet1 = new HashSet<>();
246         testChannelSet1.add(new Pair(4370, 4375));
247         testChannelSet1.add(new Pair(9000, 9000));
248         testChannelSet1.add(new Pair(1112, 1133));
249         testChannelSet1.add(new Pair(4370, 4376));
250         testChannelSet1.add(new Pair(2345, 2900));
251         testChannelSet1.add(new Pair(1112, 1199));
252         testChannelSet1.add(new Pair(1112, 1111));
253 
254         HashSet<Pair<Integer, Integer>> testChannelSet_sorted = new HashSet<>();
255         testChannelSet_sorted.add(new Pair(1112, 1133));
256         testChannelSet_sorted.add(new Pair(4370, 4376));
257         testChannelSet_sorted.add(new Pair(2345, 2900));
258         testChannelSet_sorted.add(new Pair(4370, 4375));
259         testChannelSet_sorted.add(new Pair(1112, 1199));
260         testChannelSet_sorted.add(new Pair(9000, 9000));
261         testChannelSet_sorted.add(new Pair(1112, 1111));
262 
263         byte[] testArrayByte = CellBroadcastReceiverMetrics.getInstance()
264                 .convertToProtoBuffer(testChannelSet1);
265 
266         Cellbroadcastmetric.CellBroadcastChannelRangesProto channelRangesProto =
267                 Cellbroadcastmetric.CellBroadcastChannelRangesProto
268                         .parser().parseFrom(testArrayByte);
269 
270         HashSet<Pair<Integer, Integer>> testChannelSet2 = new HashSet<>();
271 
272         for (Cellbroadcastmetric.CellBroadcastChannelRangeProto range :
273                 channelRangesProto.getChannelRangesList()) {
274             testChannelSet2.add(new Pair(range.getStart(), range.getEnd()));
275         }
276 
277         assertTrue(testChannelSet1.equals(testChannelSet2));
278 
279         byte[] testArrayByte_sorted = CellBroadcastReceiverMetrics.getInstance()
280                 .convertToProtoBuffer(testChannelSet_sorted);
281 
282         assertArrayEquals(testArrayByte, testArrayByte_sorted);
283 
284         HashSet<Pair<Integer, Integer>> testChannelSetEmpty1 = new HashSet<>();
285 
286         byte[] testArrayByteEmpty = CellBroadcastReceiverMetrics.getInstance()
287                 .convertToProtoBuffer(testChannelSetEmpty1);
288 
289         Cellbroadcastmetric.CellBroadcastChannelRangesProto channelRangesProtoEmpty =
290                 Cellbroadcastmetric.CellBroadcastChannelRangesProto.parser().parseFrom(
291                         testArrayByteEmpty);
292 
293         HashSet<Pair<Integer, Integer>> testChannelSetEmpty2 = new HashSet<>();
294 
295         for (Cellbroadcastmetric.CellBroadcastChannelRangeProto range :
296                 channelRangesProtoEmpty.getChannelRangesList()) {
297             testChannelSetEmpty2.add(new Pair(range.getStart(), range.getEnd()));
298         }
299 
300         assertTrue(testChannelSetEmpty1.equals(testChannelSetEmpty2));
301     }
302 
303     @Test
testGetDataFromProtoArrayByte()304     public void testGetDataFromProtoArrayByte() throws IOException {
305         HashSet<Pair<Integer, Integer>> testChannelSet1 = new HashSet<>();
306         testChannelSet1.add(new Pair(4370, 4375));
307         testChannelSet1.add(new Pair(9000, 9000));
308         testChannelSet1.add(new Pair(1112, 1133));
309 
310         byte[] testArrayByte = CellBroadcastReceiverMetrics.getInstance()
311                 .convertToProtoBuffer(testChannelSet1);
312 
313         HashSet<Pair<Integer, Integer>> testChannelSet2 =
314                 CellBroadcastReceiverMetrics.getInstance().getDataFromProtoArrayByte(testArrayByte);
315         assertTrue(testChannelSet1.equals(testChannelSet2));
316     }
317 
318     @Test
testGetChannelSetFromString()319     public void testGetChannelSetFromString() {
320         String inString1 = "4370,4380,4700-4750,5600,50,9600-9700,10000-11001";
321         HashSet<Pair<Integer, Integer>> outChSet1;
322         HashSet<Pair<Integer, Integer>> compareChSet1 = new HashSet<>();
323         compareChSet1.add(new Pair(4370, 4370));
324         compareChSet1.add(new Pair(4380, 4380));
325         compareChSet1.add(new Pair(4700, 4750));
326         compareChSet1.add(new Pair(5600, 5600));
327         compareChSet1.add(new Pair(50, 50));
328         compareChSet1.add(new Pair(9600, 9700));
329         compareChSet1.add(new Pair(10000, 11001));
330 
331         outChSet1 = CellBroadcastReceiverMetrics.getInstance().getChannelSetFromString(inString1);
332 
333         assertTrue(outChSet1.equals(compareChSet1));
334 
335         String inString2 = "4370-4380, 4370-4380, 50, 50";
336         HashSet<Pair<Integer, Integer>> outChSet2;
337         HashSet<Pair<Integer, Integer>> compareChSet2 = new HashSet<>();
338 
339         compareChSet2.add(new Pair(4370, 4380));
340         compareChSet2.add(new Pair(4370, 4380));
341         compareChSet2.add(new Pair(50, 50));
342         compareChSet2.add(new Pair(50, 50));
343 
344         outChSet2 = CellBroadcastReceiverMetrics.getInstance().getChannelSetFromString(inString2);
345 
346         assertTrue(outChSet2.equals(compareChSet2));
347     }
348 
349     @Test
testGetStringFromChannelSet()350     public void testGetStringFromChannelSet() {
351         HashSet<Pair<Integer, Integer>> intChSet = new HashSet<>();
352         intChSet.add(new Pair(4370, 4370));
353         intChSet.add(new Pair(4380, 4380));
354         intChSet.add(new Pair(4700, 4750));
355         intChSet.add(new Pair(5600, 5600));
356         intChSet.add(new Pair(50, 50));
357         intChSet.add(new Pair(9600, 9700));
358         intChSet.add(new Pair(10000, 11001));
359 
360         String outStr = CellBroadcastReceiverMetrics.getInstance()
361                 .getStringFromChannelSet(intChSet);
362         HashSet<Pair<Integer, Integer>> compareChSet =
363                 CellBroadcastReceiverMetrics.getInstance().getChannelSetFromString(outStr);
364 
365         assertTrue(compareChSet.equals(intChSet));
366     }
367 
368     @Test
testLogFeatureChangedAsNeeded()369     public void testLogFeatureChangedAsNeeded() throws CloneNotSupportedException {
370         CellBroadcastReceiverMetrics.getInstance().setFeatureMetrics(null);
371         CellBroadcastReceiverMetrics.FeatureMetrics testFeatureMetrics =
372                 CellBroadcastReceiverMetrics.getInstance().getFeatureMetrics(mContext);
373         CellBroadcastReceiverMetrics.FeatureMetrics testSharedPreferenceFeatureMetrics =
374                 CellBroadcastReceiverMetrics.getInstance().getFeatureMetricsSharedPreferences();
375 
376         assertTrue(testFeatureMetrics.equals(testSharedPreferenceFeatureMetrics));
377 
378         testFeatureMetrics.onChangedAlertDuringCall(true);
379         testFeatureMetrics.onChangedOverrideDnD(mChannelManager, false);
380 
381         assertFalse(testFeatureMetrics.equals(testSharedPreferenceFeatureMetrics));
382 
383         CellBroadcastReceiverMetrics.getInstance().logFeatureChangedAsNeeded(mContext);
384 
385         CellBroadcastReceiverMetrics.FeatureMetrics mockedFeatureMetrics =
386                 mock(CellBroadcastReceiverMetrics.FeatureMetrics.class);
387 
388         CellBroadcastReceiverMetrics.getInstance()
389                 .setFeatureMetricsSharedPreferences(mockedFeatureMetrics);
390 
391         assertFalse(mockedFeatureMetrics.equals(testFeatureMetrics));
392 
393         CellBroadcastReceiverMetrics.getInstance().logFeatureChangedAsNeeded(mContext);
394 
395         CellBroadcastReceiverMetrics.getInstance().setFeatureMetrics(mockedFeatureMetrics);
396 
397         assertTrue(testFeatureMetrics.equals(CellBroadcastReceiverMetrics.getInstance()
398                 .getFeatureMetricsSharedPreferences()));
399 
400         CellBroadcastReceiverMetrics.getInstance().logFeatureChangedAsNeeded(mContext);
401 
402         verify(mockedFeatureMetrics, times(1)).logFeatureChanged();
403         verify(mockedFeatureMetrics, times(1)).updateSharedPreferences();
404         verify(mockedFeatureMetrics, times(1)).clone();
405     }
406 
407     @Test
testOnConfigUpdated()408     public void testOnConfigUpdated() {
409         doReturn(String.valueOf(0)).when(mSharedPreferences).getString(anyString(), anyString());
410 
411         CellBroadcastReceiverMetrics.getInstance().onConfigUpdated(mContext, OPERATOR, null);
412 
413         verify(mSharedPreferences, times(0)).getString(CBR_CONFIG_UPDATED, String.valueOf(0));
414 
415         HashSet<Pair<Integer, Integer>> curChRange = new HashSet<>();
416         curChRange.add(new Pair(4370, 4370));
417         curChRange.add(new Pair(4380, 4380));
418 
419         CellBroadcastReceiverMetrics.getInstance().onConfigUpdated(mContext, OPERATOR, curChRange);
420 
421         verify(mEditor, times(1)).apply();
422         HashSet<Pair<Integer, Integer>> mCachedChSet =
423                 CellBroadcastReceiverMetrics.getInstance().getCachedChannelSet();
424         assertTrue(curChRange.equals(mCachedChSet));
425         assertNotSame(curChRange, mCachedChSet);
426     }
427 }
428 
429