1 /* 2 * Copyright (C) 2021 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 com.android.testutils.connectivitypreparer 18 19 import android.content.pm.PackageManager.FEATURE_TELEPHONY 20 import android.content.pm.PackageManager.FEATURE_WIFI 21 import android.telephony.TelephonyManager 22 import androidx.test.ext.junit.runners.AndroidJUnit4 23 import androidx.test.platform.app.InstrumentationRegistry 24 import com.android.testutils.ConnectUtil 25 import kotlin.test.assertTrue 26 import kotlin.test.fail 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 30 @RunWith(AndroidJUnit4::class) 31 class ConnectivityCheckTest { <lambda>null32 private val context by lazy { InstrumentationRegistry.getInstrumentation().context } <lambda>null33 private val pm by lazy { context.packageManager } <lambda>null34 private val connectUtil by lazy { ConnectUtil(context) } 35 36 @Test testCheckWifiSetupnull37 fun testCheckWifiSetup() { 38 if (!pm.hasSystemFeature(FEATURE_WIFI)) return 39 connectUtil.ensureWifiValidated() 40 } 41 42 @Test testCheckTelephonySetupnull43 fun testCheckTelephonySetup() { 44 if (!pm.hasSystemFeature(FEATURE_TELEPHONY)) return 45 val tm = context.getSystemService(TelephonyManager::class.java) 46 ?: fail("Could not get telephony service") 47 48 val commonError = "Check the test bench. To run the tests anyway for quick & dirty local " + 49 "testing, you can use atest X -- " + 50 "--test-arg com.android.testutils.ConnectivityTestTargetPreparer" + 51 ":ignore-mobile-data-check:true" 52 // Do not use assertEquals: it outputs "expected X, was Y", which looks like a test failure 53 if (tm.simState == TelephonyManager.SIM_STATE_ABSENT) { 54 fail("The device has no SIM card inserted. $commonError") 55 } else if (tm.simState != TelephonyManager.SIM_STATE_READY) { 56 fail("The device is not setup with a usable SIM card. Sim state was ${tm.simState}. " + 57 commonError) 58 } 59 assertTrue(tm.isDataConnectivityPossible, 60 "The device has a SIM card, but it does not supports data connectivity. " + 61 "Check the data plan, and verify that mobile data is working. " + commonError) 62 connectUtil.ensureCellularValidated() 63 } 64 } 65