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.NetworkInfo; 24 import android.net.wifi.WifiManager; 25 import android.os.Build; 26 import android.telephony.CellInfoGsm; 27 import android.telephony.CellSignalStrengthGsm; 28 import android.telephony.TelephonyManager; 29 import android.util.Log; 30 31 import androidx.test.InstrumentationRegistry; 32 33 import com.android.compatibility.common.util.PropertyUtil; 34 35 import org.junit.Assert; 36 import org.junit.Assume; 37 import org.junit.Before; 38 import org.junit.Ignore; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.junit.runners.JUnit4; 42 43 import java.net.Socket; 44 45 /** 46 * Tests used to validate E2E RIL functionality. 47 */ 48 @RunWith(JUnit4.class) 49 public class RilE2eTests { 50 private static final String TAG = "RilE2eTests"; 51 private static final int MAX_POLL_DISABLED_WIFI_COUNT = 10; 52 private Context mContext; 53 private WifiManager mWifiManager; 54 private ConnectivityManager mConnManager; 55 private TelephonyManager mTeleManager; 56 57 @Before setUp()58 public void setUp() throws Exception { 59 // Ideally this should be done in the @BeforeClass hook, but that would 60 // make tradefed unhappy with a bunch "test did not run due to 61 // instrumentation issue. See run level error for reason." errors. 62 Assume.assumeFalse( 63 "Skip testing deprecated radio HAL from Q or earlier vendor", 64 PropertyUtil.getFirstApiLevel() <= Build.VERSION_CODES.Q); 65 66 mContext = InstrumentationRegistry.getInstrumentation().getContext(); 67 mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE); 68 mConnManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 69 mTeleManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE); 70 // There must not be an active wifi connection while running the test or else 71 // getActiveNetworkInfo() will return that instead of the telephony network. 72 // Turning wifi off should do the trick. 73 disableWifi(); 74 } 75 76 disableWifi()77 private void disableWifi() throws Exception { 78 Log.i(TAG, "Disabling WIFI..."); 79 80 mWifiManager.setWifiEnabled(false); 81 int count = MAX_POLL_DISABLED_WIFI_COUNT; 82 while (mWifiManager.isWifiEnabled() && count-- > 0) { 83 Log.i(TAG, "Waiting for WIFI to be disabled..."); 84 try { 85 Thread.sleep(1000); 86 } catch (InterruptedException e) {} 87 } 88 if (count < 0) { 89 Log.e(TAG, "Reached max number of polls while waiting to disable wifi"); 90 throw new Exception("Timed out waiting for wifi to be disabled"); 91 } 92 } 93 94 95 /** 96 * Verify that RIL stack is able to get up and connect to network in 97 * 60 seconds. 98 */ 99 @Test(timeout = 10 * 1000) testRilConnects()100 public void testRilConnects() throws Exception { 101 while (true) { 102 NetworkInfo net = mConnManager.getActiveNetworkInfo(); 103 if (net != null && net.getType() == ConnectivityManager.TYPE_MOBILE) break; 104 105 Log.i(TAG, "Waiting for MOBILE to become primary network for DATA."); 106 107 try { 108 Thread.sleep(1000); 109 } catch (InterruptedException e) {} 110 } 111 112 // Bind process to MOBILE network. This should allow us to verify network is functional. 113 Network net = mConnManager.getActiveNetwork(); 114 Assert.assertNotNull(net); 115 Assert.assertTrue(mConnManager.bindProcessToNetwork(net)); 116 117 // Open connection to google.com servers. 118 try (Socket s = new Socket("google.com", 80)) { 119 Assert.assertTrue(s.isConnected()); 120 } 121 } 122 123 124 /** 125 * Verify that AVD is connected to our virtual network operator and is 126 * phone-, sms- and data capable. 127 */ 128 @Test testBasicPhoneAttributes()129 public void testBasicPhoneAttributes() throws Exception { 130 Assert.assertEquals("Android Virtual Operator", mTeleManager.getNetworkOperatorName()); 131 Assert.assertFalse(mTeleManager.isNetworkRoaming()); 132 Assert.assertTrue(mTeleManager.isSmsCapable()); 133 Assert.assertSame(TelephonyManager.NETWORK_TYPE_LTE, mTeleManager.getVoiceNetworkType()); 134 Assert.assertSame(TelephonyManager.SIM_STATE_READY, mTeleManager.getSimState()); 135 Assert.assertSame(TelephonyManager.PHONE_TYPE_GSM, mTeleManager.getPhoneType()); 136 Assert.assertSame(mTeleManager.getPhoneCount(), 1); 137 // See SIM FS response for 178 28480 (Cuttlefish RIL). 138 Assert.assertEquals("+15551234567", mTeleManager.getLine1Number()); 139 // See SIM FS response for 178 28615 (Cuttlefish RIL). 140 Assert.assertEquals("+15557654321", mTeleManager.getVoiceMailNumber()); 141 Assert.assertSame(TelephonyManager.DATA_CONNECTED, mTeleManager.getDataState()); 142 } 143 144 // See b/74256305 145 @Ignore 146 @Test testSignalLevels()147 public void testSignalLevels() throws Exception { 148 CellInfoGsm cellinfogsm = (CellInfoGsm)mTeleManager.getAllCellInfo().get(0); 149 CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength(); 150 int bars = cellSignalStrengthGsm.getLevel(); 151 Assert.assertThat("Signal Bars", bars, greaterThan(1)); 152 } 153 } 154