1 /*
2  * Copyright (C) 2017 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.cuttlefish.ril.tests;
17 
18 import static org.hamcrest.Matchers.greaterThan;
19 
20 import android.content.Context;
21 import android.net.ConnectivityManager;
22 import android.net.Network;
23 import android.net.NetworkCapabilities;
24 import android.net.wifi.WifiManager;
25 import android.os.Build;
26 import android.telephony.CellInfo;
27 import android.telephony.CellInfoLte;
28 import android.telephony.CellSignalStrengthLte;
29 import android.telephony.TelephonyManager;
30 import android.util.Log;
31 
32 import androidx.annotation.NonNull;
33 import androidx.test.core.app.ApplicationProvider;
34 
35 import com.android.compatibility.common.util.PropertyUtil;
36 
37 import org.hamcrest.MatcherAssert;
38 import org.junit.Assert;
39 import org.junit.Assume;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.JUnit4;
44 
45 import java.net.InetSocketAddress;
46 import java.net.Socket;
47 import java.net.SocketTimeoutException;
48 import java.util.ArrayList;
49 import java.util.List;
50 import java.util.concurrent.CountDownLatch;
51 
52 /**
53  * Tests used to validate E2E RIL functionality.
54  */
55 @RunWith(JUnit4.class)
56 public class RilE2eTests {
57     private static final String TAG = "RilE2eTests";
58     private static final int MAX_POLL_DISABLED_WIFI_COUNT = 10;
59     private Context mContext;
60     private WifiManager mWifiManager;
61     private ConnectivityManager mConnManager;
62     private TelephonyManager mTeleManager;
63 
64     @Before
setUp()65     public void setUp() throws Exception {
66         // Ideally this should be done in the @BeforeClass hook, but that would
67         // make tradefed unhappy with a bunch "test did not run due to
68         // instrumentation issue. See run level error for reason." errors.
69         Assume.assumeFalse(
70                 "Skip testing deprecated radio HAL from Q or earlier vendor",
71                 PropertyUtil.getFirstApiLevel() <= Build.VERSION_CODES.Q);
72 
73         mContext = ApplicationProvider.getApplicationContext();
74         mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
75         mConnManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
76         mTeleManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
77         // There must not be an active wifi connection while running the test or else
78         // getActiveNetworkInfo() will return that instead of the telephony network.
79         // Turning wifi off should do the trick.
80         disableWifi();
81     }
82 
83     @SuppressWarnings("deprecation") // setWifiEnabled not deprecated for system uid
disableWifi()84     private void disableWifi() throws Exception {
85         Log.i(TAG, "Disabling WIFI...");
86 
87         mWifiManager.setWifiEnabled(false);
88         int count = MAX_POLL_DISABLED_WIFI_COUNT;
89         while (mWifiManager.isWifiEnabled() && count-- > 0) {
90             Log.i(TAG, "Waiting for WIFI to be disabled...");
91             Thread.sleep(1000);
92         }
93         if (count < 0) {
94             Log.e(TAG, "Reached max number of polls while waiting to disable wifi");
95             throw new Exception("Timed out waiting for wifi to be disabled");
96         }
97     }
98 
99 
100     /**
101      * Verify that AVD is connected to our virtual network operator and is
102      * phone-, sms- and data capable.
103      */
104     @Test
testBasicPhoneAttributes()105     public void testBasicPhoneAttributes() throws Exception {
106         Assert.assertEquals("Android Virtual Operator", mTeleManager.getNetworkOperatorName());
107         Assert.assertFalse(mTeleManager.isNetworkRoaming());
108         Assert.assertTrue(mTeleManager.isSmsCapable());
109         Assert.assertSame(TelephonyManager.NETWORK_TYPE_LTE, mTeleManager.getVoiceNetworkType());
110         Assert.assertSame(TelephonyManager.SIM_STATE_READY, mTeleManager.getSimState());
111         Assert.assertSame(TelephonyManager.PHONE_TYPE_GSM, mTeleManager.getPhoneType());
112         Assert.assertSame(mTeleManager.getActiveModemCount(), 1);
113         // See SIM FS response for 178 28480 (Cuttlefish RIL).
114         Assert.assertEquals("+15551234567", mTeleManager.getLine1Number());
115         // See SIM FS response for 178 28615 (Cuttlefish RIL).
116         Assert.assertEquals("+15557654321", mTeleManager.getVoiceMailNumber());
117     }
118 
119     @Test
testSignalLevels()120     public void testSignalLevels() throws Exception {
121         List<CellInfo> cellInfos = new ArrayList<>();
122         CountDownLatch cdl = new CountDownLatch(1);
123         mTeleManager.requestCellInfoUpdate(mContext.getMainExecutor(),
124                 new TelephonyManager.CellInfoCallback() {
125                     @Override
126                     public void onCellInfo(@NonNull List<CellInfo> cellInfo) {
127                         if (cellInfo != null) {
128                             cellInfos.addAll(cellInfo);
129                         }
130                         cdl.countDown();
131                     }
132                 });
133         cdl.await();
134         MatcherAssert.assertThat("Size of list of cell info", cellInfos.size(), greaterThan(0));
135         CellInfoLte cellInfo = (CellInfoLte) cellInfos.get(0);
136         CellSignalStrengthLte signalStrength = cellInfo.getCellSignalStrength();
137         int bars = signalStrength.getLevel();
138         MatcherAssert.assertThat("Signal Bars", bars, greaterThan(1));
139     }
140 }
141