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 android.net;
18 
19 import static com.android.testutils.ParcelUtils.assertParcelSane;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24 
25 import android.net.wifi.WifiNetworkSpecifier;
26 import android.os.Build;
27 import android.telephony.SubscriptionManager;
28 
29 import androidx.test.filters.SmallTest;
30 
31 import com.android.testutils.DevSdkIgnoreRule;
32 
33 import org.junit.Test;
34 
35 /**
36  * Unit test for {@link android.net.TelephonyNetworkSpecifier}.
37  */
38 @SmallTest
39 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
40 public class TelephonyNetworkSpecifierTest {
41     private static final int TEST_SUBID = 5;
42     private static final String TEST_SSID = "Test123";
43 
44     /**
45      * Validate that IllegalArgumentException will be thrown if build TelephonyNetworkSpecifier
46      * without calling {@link TelephonyNetworkSpecifier.Builder#setSubscriptionId(int)}.
47      */
48     @Test
testBuilderBuildWithDefault()49     public void testBuilderBuildWithDefault() {
50         try {
51             new TelephonyNetworkSpecifier.Builder().build();
52         } catch (IllegalArgumentException iae) {
53             // expected, test pass
54         }
55     }
56 
57     /**
58      * Validate that no exception will be thrown even if pass invalid subscription id to
59      * {@link TelephonyNetworkSpecifier.Builder#setSubscriptionId(int)}.
60      */
61     @Test
testBuilderBuildWithInvalidSubId()62     public void testBuilderBuildWithInvalidSubId() {
63         TelephonyNetworkSpecifier specifier = new TelephonyNetworkSpecifier.Builder()
64                 .setSubscriptionId(SubscriptionManager.INVALID_SUBSCRIPTION_ID)
65                 .build();
66         assertEquals(specifier.getSubscriptionId(), SubscriptionManager.INVALID_SUBSCRIPTION_ID);
67     }
68 
69     /**
70      * Validate the correctness of TelephonyNetworkSpecifier when provide valid subId.
71      */
72     @Test
testBuilderBuildWithValidSubId()73     public void testBuilderBuildWithValidSubId() {
74         final TelephonyNetworkSpecifier specifier = new TelephonyNetworkSpecifier.Builder()
75                 .setSubscriptionId(TEST_SUBID)
76                 .build();
77         assertEquals(TEST_SUBID, specifier.getSubscriptionId());
78     }
79 
80     /**
81      * Validate that parcel marshalling/unmarshalling works.
82      */
83     @Test
testParcel()84     public void testParcel() {
85         TelephonyNetworkSpecifier specifier = new TelephonyNetworkSpecifier.Builder()
86                 .setSubscriptionId(TEST_SUBID)
87                 .build();
88         assertParcelSane(specifier, 1 /* fieldCount */);
89     }
90 
91     /**
92      * Validate the behavior of method canBeSatisfiedBy().
93      */
94     @Test
testCanBeSatisfiedBy()95     public void testCanBeSatisfiedBy() {
96         final TelephonyNetworkSpecifier tns1 = new TelephonyNetworkSpecifier.Builder()
97                 .setSubscriptionId(TEST_SUBID)
98                 .build();
99         final TelephonyNetworkSpecifier tns2 = new TelephonyNetworkSpecifier.Builder()
100                 .setSubscriptionId(TEST_SUBID)
101                 .build();
102         final WifiNetworkSpecifier wns = new WifiNetworkSpecifier.Builder()
103                 .setSsid(TEST_SSID)
104                 .build();
105         final MatchAllNetworkSpecifier mans = new MatchAllNetworkSpecifier();
106 
107         // Test equality
108         assertEquals(tns1, tns2);
109         assertTrue(tns1.canBeSatisfiedBy(tns1));
110         assertTrue(tns1.canBeSatisfiedBy(tns2));
111 
112         // Test other edge cases.
113         assertFalse(tns1.canBeSatisfiedBy(null));
114         assertFalse(tns1.canBeSatisfiedBy(wns));
115         assertTrue(tns1.canBeSatisfiedBy(mans));
116     }
117 }
118