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 android.content.Context; 19 import android.net.ConnectivityManager; 20 import android.net.Network; 21 import android.net.NetworkInfo; 22 import android.net.wifi.SupplicantState; 23 import android.net.wifi.WifiConfiguration; 24 import android.net.wifi.WifiInfo; 25 import android.net.wifi.WifiManager; 26 import android.support.test.InstrumentationRegistry; 27 import android.telephony.CellInfoGsm; 28 import android.telephony.CellSignalStrengthGsm; 29 import android.telephony.TelephonyManager; 30 import android.util.Log; 31 32 import static org.hamcrest.Matchers.greaterThan; 33 import org.junit.Assert; 34 import org.junit.Before; 35 import org.junit.Ignore; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.junit.runners.JUnit4; 39 40 import java.net.Socket; 41 import java.util.List; 42 43 /** 44 * Tests used to validate E2E WIFI functionality. 45 */ 46 @RunWith(JUnit4.class) 47 public class RilE2eTests { 48 private static final String TAG = "RilE2eTests"; 49 private Context mContext; 50 private WifiManager mWifiManager; 51 private ConnectivityManager mConnManager; 52 private TelephonyManager mTeleManager; 53 54 @Before setUp()55 public void setUp() throws Exception { 56 mContext = InstrumentationRegistry.getInstrumentation().getContext(); 57 mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE); 58 mConnManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 59 mTeleManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE); 60 disableAllWifiNetworks(); 61 } 62 63 enableWifi()64 private void enableWifi() { 65 Log.i(TAG, "Enabling WIFI..."); 66 mWifiManager.setWifiEnabled(true); 67 while (!(mWifiManager.isWifiEnabled() && mWifiManager.pingSupplicant())) { 68 Log.i(TAG, "Waiting for WIFI (Enabled: " + mWifiManager.isWifiEnabled() + 69 ", Ready: " + mWifiManager.pingSupplicant() + ")"); 70 try { 71 Thread.sleep(1000); 72 } catch (InterruptedException e) {} 73 } 74 } 75 76 disableWifi()77 private void disableWifi() { 78 Log.i(TAG, "Disabling WIFI..."); 79 80 mWifiManager.setWifiEnabled(false); 81 while (mWifiManager.isWifiEnabled()) { 82 Log.i(TAG, "Waiting for WIFI to be disabled..."); 83 try { 84 Thread.sleep(1000); 85 } catch (InterruptedException e) {} 86 } 87 } 88 89 disableAllWifiNetworks()90 private void disableAllWifiNetworks() { 91 enableWifi(); 92 93 List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); 94 Assert.assertNotNull(configs); 95 for (WifiConfiguration config : configs) { 96 Log.i(TAG, "Removing network " + config.networkId + ": " + config.SSID); 97 Assert.assertTrue(mWifiManager.disableNetwork(config.networkId)); 98 Assert.assertTrue(mWifiManager.removeNetwork(config.networkId)); 99 } 100 101 disableWifi(); 102 } 103 104 105 /** 106 * Verify that WIFI stack is able to get up and connect to network in 107 * 60 seconds. 108 */ 109 @Test(timeout = 60 * 1000) testRilConnects()110 public void testRilConnects() throws Exception { 111 while (true) { 112 NetworkInfo net = mConnManager.getActiveNetworkInfo(); 113 if (net != null && net.getType() == ConnectivityManager.TYPE_MOBILE) break; 114 115 Log.i(TAG, "Waiting for MOBILE to become primary network for DATA."); 116 117 try { 118 Thread.sleep(1000); 119 } catch (InterruptedException e) {} 120 } 121 122 // Bind process to MOBILE network. This should allow us to verify network is functional. 123 Network net = mConnManager.getActiveNetwork(); 124 Assert.assertNotNull(net); 125 Assert.assertTrue(mConnManager.bindProcessToNetwork(net)); 126 127 // Open connection to google.com servers. 128 try (Socket s = new Socket("google.com", 80)) { 129 Assert.assertTrue(s.isConnected()); 130 } 131 } 132 133 134 /** 135 * Verify that AVD is connected to our virtual network operator and is 136 * phone-, sms- and data capable. 137 */ 138 @Test testBasicPhoneAttributes()139 public void testBasicPhoneAttributes() throws Exception { 140 Assert.assertEquals("Android Virtual Operator", mTeleManager.getNetworkOperatorName()); 141 Assert.assertFalse(mTeleManager.isNetworkRoaming()); 142 Assert.assertTrue(mTeleManager.isSmsCapable()); 143 Assert.assertSame(TelephonyManager.NETWORK_TYPE_LTE, mTeleManager.getVoiceNetworkType()); 144 Assert.assertSame(TelephonyManager.SIM_STATE_READY, mTeleManager.getSimState()); 145 Assert.assertSame(TelephonyManager.PHONE_TYPE_GSM, mTeleManager.getPhoneType()); 146 Assert.assertSame(mTeleManager.getPhoneCount(), 1); 147 // See SIM FS response for 178 28480 (Cuttlefish RIL). 148 Assert.assertEquals("+15551234567", mTeleManager.getLine1Number()); 149 // See SIM FS response for 178 28615 (Cuttlefish RIL). 150 Assert.assertEquals("+15557654321", mTeleManager.getVoiceMailNumber()); 151 Assert.assertSame(TelephonyManager.DATA_CONNECTED, mTeleManager.getDataState()); 152 } 153 154 // See b/74256305 155 @Ignore 156 @Test testSignalLevels()157 public void testSignalLevels() throws Exception { 158 CellInfoGsm cellinfogsm = (CellInfoGsm)mTeleManager.getAllCellInfo().get(0); 159 CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength(); 160 int bars = cellSignalStrengthGsm.getLevel(); 161 Assert.assertThat("Signal Bars", bars, greaterThan(1)); 162 } 163 } 164