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 
18 package android.telephony.cts;
19 
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertThrows;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.os.Parcel;
25 import android.telephony.CellBroadcastIdRange;
26 import android.telephony.SmsCbMessage;
27 
28 import androidx.test.ext.junit.runners.AndroidJUnit4;
29 
30 import com.android.compatibility.common.util.ApiTest;
31 
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 @RunWith(AndroidJUnit4.class)
36 public final class CellBroadcastIdRangeTest {
37     private static final int[][] CHANNEL_VALUES = {
38         {0, 999}, {1000, 1003}, {1004, 0x0FFF}, {0x1000, 0x10FF}, {0x1100, 0x112F},
39         {0x1130, 0x1900}, {0x1901, 0x9FFF}, {0xA000, 0xFFFE}, {0xFFFF, 0xFFFF}};
40     private static final int[] TYPE_VALUES = {
41         SmsCbMessage.MESSAGE_FORMAT_3GPP, SmsCbMessage.MESSAGE_FORMAT_3GPP2};
42     private static final boolean[] ENABLED_VALUES = {true, false};
43 
44     @Test
45     @ApiTest(apis = {
46             "android.telephony.CellBroadcastIdRange#CellBroadcastIdRange",
47             "android.telephony.CellBroadcastIdRange#getStartId",
48             "android.telephony.CellBroadcastIdRange#getEndId",
49             "android.telephony.CellBroadcastIdRange#getType",
50             "android.telephony.CellBroadcastIdRange#isEnabled"})
testCreateCellBroadcastIdRange()51     public void testCreateCellBroadcastIdRange() {
52         //IllegalArgumentException is expected if id is negative
53         assertThrows(IllegalArgumentException.class, () -> {
54             new CellBroadcastIdRange(-1, 1, SmsCbMessage.MESSAGE_FORMAT_3GPP, true);
55         });
56 
57         //IllegalArgumentException is expected if startId > endId
58         assertThrows(IllegalArgumentException.class, () -> {
59             new CellBroadcastIdRange(0x1000, 1, SmsCbMessage.MESSAGE_FORMAT_3GPP, true);
60         });
61 
62         for (int i = 0; i < CHANNEL_VALUES.length; i++) {
63             for (int j = 0; j < TYPE_VALUES.length; j++) {
64                 for (int k = 0; k < ENABLED_VALUES.length; k++) {
65                     CellBroadcastIdRange range = new CellBroadcastIdRange(CHANNEL_VALUES[i][0],
66                             CHANNEL_VALUES[i][1], TYPE_VALUES[j], ENABLED_VALUES[k]);
67                     assertEquals(range.getStartId(), CHANNEL_VALUES[i][0]);
68                     assertEquals(range.getEndId(), CHANNEL_VALUES[i][1]);
69                     assertEquals(range.getType(), TYPE_VALUES[j]);
70                     assertEquals(range.isEnabled(), ENABLED_VALUES[k]);
71                 }
72             }
73         }
74     }
75 
76     @Test
77     @ApiTest(apis = {
78             "android.telephony.CellBroadcastIdRange#CellBroadcastIdRange",
79             "android.telephony.CellBroadcastIdRange#equals"})
testEquals()80     public void testEquals() {
81         CellBroadcastIdRange range1 = new CellBroadcastIdRange(CHANNEL_VALUES[0][0],
82                 CHANNEL_VALUES[0][1], TYPE_VALUES[0], ENABLED_VALUES[0]);
83         CellBroadcastIdRange range2 = new CellBroadcastIdRange(CHANNEL_VALUES[0][0],
84                 CHANNEL_VALUES[0][1], TYPE_VALUES[0], ENABLED_VALUES[0]);
85 
86         assertTrue(!range1.equals(null));
87         assertTrue(range1.equals(range2));
88 
89         range2 = new CellBroadcastIdRange(CHANNEL_VALUES[1][0], CHANNEL_VALUES[1][1],
90                 TYPE_VALUES[0], ENABLED_VALUES[0]);
91 
92         assertTrue(!range1.equals(range2));
93 
94         range2 = new CellBroadcastIdRange(CHANNEL_VALUES[0][0], CHANNEL_VALUES[0][1],
95                 TYPE_VALUES[1], ENABLED_VALUES[0]);
96 
97         assertTrue(!range1.equals(range2));
98 
99         range2 = new CellBroadcastIdRange(CHANNEL_VALUES[0][0], CHANNEL_VALUES[0][1],
100                 TYPE_VALUES[0], ENABLED_VALUES[1]);
101 
102         assertTrue(!range1.equals(range2));
103     }
104 
105     @Test
106     @ApiTest(apis = {
107             "android.telephony.CellBroadcastIdRange#CellBroadcastIdRange",
108             "android.telephony.CellBroadcastIdRange#describeContents"})
testDescribeContents()109     public void testDescribeContents() {
110         for (int i = 0; i < CHANNEL_VALUES.length; i++) {
111             for (int j = 0; j < TYPE_VALUES.length; j++) {
112                 for (int k = 0; k < ENABLED_VALUES.length; k++) {
113                     CellBroadcastIdRange range = new CellBroadcastIdRange(CHANNEL_VALUES[i][0],
114                             CHANNEL_VALUES[i][1], TYPE_VALUES[j], ENABLED_VALUES[k]);
115                     assertEquals(0, range.describeContents());
116                 }
117             }
118         }
119     }
120 
121     @Test
122     @ApiTest(apis = {
123             "android.telephony.CellBroadcastIdRange#CellBroadcastIdRange",
124             "android.telephony.CellBroadcastIdRange#writeToParcel",
125             "android.telephony.CellBroadcastIdRange#CREATOR"})
testParcelUnparcel()126     public void testParcelUnparcel() {
127         for (int i = 0; i < CHANNEL_VALUES.length; i++) {
128             for (int j = 0; j < TYPE_VALUES.length; j++) {
129                 for (int k = 0; k < ENABLED_VALUES.length; k++) {
130                     CellBroadcastIdRange range = new CellBroadcastIdRange(CHANNEL_VALUES[i][0],
131                             CHANNEL_VALUES[i][1], TYPE_VALUES[j], ENABLED_VALUES[k]);
132 
133                     Parcel parcel = Parcel.obtain();
134                     range.writeToParcel(parcel, 0);
135                     parcel.setDataPosition(0);
136                     CellBroadcastIdRange range2 =
137                             CellBroadcastIdRange.CREATOR.createFromParcel(parcel);
138                     parcel.recycle();
139 
140                     assertEquals(range, range2);
141                 }
142             }
143         }
144     }
145 }
146