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 RIL functionality.
45  */
46 @RunWith(JUnit4.class)
47 public class RilE2eTests {
48     private static final String TAG = "RilE2eTests";
49     private static final int MAX_POLL_DISABLED_WIFI_COUNT = 10;
50     private Context mContext;
51     private WifiManager mWifiManager;
52     private ConnectivityManager mConnManager;
53     private TelephonyManager mTeleManager;
54 
55     @Before
setUp()56     public void setUp() throws Exception {
57         mContext = InstrumentationRegistry.getInstrumentation().getContext();
58         mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
59         mConnManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
60         mTeleManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
61         // There must not be an active wifi connection while running the test or else
62         // getActiveNetworkInfo() will return that instead of the telephony network.
63         // Turning wifi off should do the trick.
64         disableWifi();
65     }
66 
67 
disableWifi()68     private void disableWifi() throws Exception {
69         Log.i(TAG, "Disabling WIFI...");
70 
71         mWifiManager.setWifiEnabled(false);
72         int count = MAX_POLL_DISABLED_WIFI_COUNT;
73         while (mWifiManager.isWifiEnabled() && count-- > 0) {
74             Log.i(TAG, "Waiting for WIFI to be disabled...");
75             try {
76                 Thread.sleep(1000);
77             } catch (InterruptedException e) {}
78         }
79         if (count < 0) {
80             Log.e(TAG, "Reached max number of polls while waiting to disable wifi");
81             throw new Exception("Timed out waiting for wifi to be disabled");
82         }
83     }
84 
85 
86     /**
87      * Verify that RIL stack is able to get up and connect to network in
88      * 60 seconds.
89      */
90     @Test(timeout = 10 * 1000)
testRilConnects()91     public void testRilConnects() throws Exception {
92         while (true) {
93             NetworkInfo net = mConnManager.getActiveNetworkInfo();
94             if (net != null && net.getType() == ConnectivityManager.TYPE_MOBILE) break;
95 
96             Log.i(TAG, "Waiting for MOBILE to become primary network for DATA.");
97 
98             try {
99                 Thread.sleep(1000);
100             } catch (InterruptedException e) {}
101         }
102 
103         // Bind process to MOBILE network. This should allow us to verify network is functional.
104         Network net = mConnManager.getActiveNetwork();
105         Assert.assertNotNull(net);
106         Assert.assertTrue(mConnManager.bindProcessToNetwork(net));
107 
108         // Open connection to google.com servers.
109         try (Socket s = new Socket("google.com", 80)) {
110             Assert.assertTrue(s.isConnected());
111         }
112     }
113 
114 
115     /**
116      * Verify that AVD is connected to our virtual network operator and is
117      * phone-, sms- and data capable.
118      */
119     @Test
testBasicPhoneAttributes()120     public void testBasicPhoneAttributes() throws Exception {
121         Assert.assertEquals("Android Virtual Operator", mTeleManager.getNetworkOperatorName());
122         Assert.assertFalse(mTeleManager.isNetworkRoaming());
123         Assert.assertTrue(mTeleManager.isSmsCapable());
124         Assert.assertSame(TelephonyManager.NETWORK_TYPE_LTE, mTeleManager.getVoiceNetworkType());
125         Assert.assertSame(TelephonyManager.SIM_STATE_READY, mTeleManager.getSimState());
126         Assert.assertSame(TelephonyManager.PHONE_TYPE_GSM, mTeleManager.getPhoneType());
127         Assert.assertSame(mTeleManager.getPhoneCount(), 1);
128         // See SIM FS response for 178 28480 (Cuttlefish RIL).
129         Assert.assertEquals("+15551234567", mTeleManager.getLine1Number());
130         // See SIM FS response for 178 28615 (Cuttlefish RIL).
131         Assert.assertEquals("+15557654321", mTeleManager.getVoiceMailNumber());
132         Assert.assertSame(TelephonyManager.DATA_CONNECTED, mTeleManager.getDataState());
133     }
134 
135     // See b/74256305
136     @Ignore
137     @Test
testSignalLevels()138     public void testSignalLevels() throws Exception {
139         CellInfoGsm cellinfogsm = (CellInfoGsm)mTeleManager.getAllCellInfo().get(0);
140         CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
141         int bars = cellSignalStrengthGsm.getLevel();
142         Assert.assertThat("Signal Bars", bars, greaterThan(1));
143     }
144 }
145