Rewrite NetworkInfo CTS tests to Kotlin
Bug: 152356365
Test: atest CtsNetTestCasesLatestSdk:android.net.cts.NetworkInfoTest
on both Q and R devices
Change-Id: I44ffdc4b4a9ba8fcc1fda895b9d7f8f551fd6bb3
Merged-In: I44ffdc4b4a9ba8fcc1fda895b9d7f8f551fd6bb3
(cherry picked from aosp/1256248)
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2009 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net.cts;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.NetworkInfo.DetailedState;
|
||||
import android.net.NetworkInfo.State;
|
||||
import android.test.AndroidTestCase;
|
||||
|
||||
public class NetworkInfoTest extends AndroidTestCase {
|
||||
|
||||
public static final int TYPE_MOBILE = ConnectivityManager.TYPE_MOBILE;
|
||||
public static final int TYPE_WIFI = ConnectivityManager.TYPE_WIFI;
|
||||
public static final String MOBILE_TYPE_NAME = "mobile";
|
||||
public static final String WIFI_TYPE_NAME = "WIFI";
|
||||
|
||||
public void testAccessNetworkInfoProperties() {
|
||||
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(
|
||||
Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo[] ni = cm.getAllNetworkInfo();
|
||||
assertTrue(ni.length >= 1);
|
||||
|
||||
for (NetworkInfo netInfo: ni) {
|
||||
switch (netInfo.getType()) {
|
||||
case TYPE_MOBILE:
|
||||
assertNetworkInfo(netInfo, MOBILE_TYPE_NAME);
|
||||
break;
|
||||
case TYPE_WIFI:
|
||||
assertNetworkInfo(netInfo, WIFI_TYPE_NAME);
|
||||
break;
|
||||
// TODO: Add BLUETOOTH_TETHER testing
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void assertNetworkInfo(NetworkInfo netInfo, String expectedTypeName) {
|
||||
assertEquals(expectedTypeName.compareToIgnoreCase(netInfo.getTypeName()), 0);
|
||||
if(netInfo.isConnectedOrConnecting()) {
|
||||
assertTrue(netInfo.isAvailable());
|
||||
if (State.CONNECTED == netInfo.getState()) {
|
||||
assertTrue(netInfo.isConnected());
|
||||
}
|
||||
assertTrue(State.CONNECTING == netInfo.getState()
|
||||
|| State.CONNECTED == netInfo.getState());
|
||||
assertTrue(DetailedState.SCANNING == netInfo.getDetailedState()
|
||||
|| DetailedState.CONNECTING == netInfo.getDetailedState()
|
||||
|| DetailedState.AUTHENTICATING == netInfo.getDetailedState()
|
||||
|| DetailedState.CONNECTED == netInfo.getDetailedState());
|
||||
}
|
||||
assertNotNull(netInfo.toString());
|
||||
}
|
||||
}
|
||||
73
tests/cts/net/src/android/net/cts/NetworkInfoTest.kt
Normal file
73
tests/cts/net/src/android/net/cts/NetworkInfoTest.kt
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net.cts
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkInfo
|
||||
import android.net.NetworkInfo.DetailedState
|
||||
import android.net.NetworkInfo.State
|
||||
import androidx.test.filters.SmallTest
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.runner.AndroidJUnit4
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.runner.RunWith
|
||||
import org.junit.Test
|
||||
|
||||
const val TYPE_MOBILE = ConnectivityManager.TYPE_MOBILE
|
||||
const val TYPE_WIFI = ConnectivityManager.TYPE_WIFI
|
||||
const val MOBILE_TYPE_NAME = "mobile"
|
||||
const val WIFI_TYPE_NAME = "WIFI"
|
||||
|
||||
@SmallTest
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class NetworkInfoTest {
|
||||
@Test
|
||||
fun testAccessNetworkInfoProperties() {
|
||||
val cm = InstrumentationRegistry.getInstrumentation().context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val ni = cm.getAllNetworkInfo()
|
||||
assertTrue(ni.isNotEmpty())
|
||||
|
||||
for (netInfo in ni) {
|
||||
when (netInfo.getType()) {
|
||||
TYPE_MOBILE -> assertNetworkInfo(netInfo, MOBILE_TYPE_NAME)
|
||||
TYPE_WIFI -> assertNetworkInfo(netInfo, WIFI_TYPE_NAME)
|
||||
// TODO: Add BLUETOOTH_TETHER testing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun assertNetworkInfo(netInfo: NetworkInfo, expectedTypeName: String) {
|
||||
assertTrue(expectedTypeName.equals(netInfo.getTypeName(), ignoreCase = true))
|
||||
assertNotNull(netInfo.toString())
|
||||
|
||||
if (!netInfo.isConnectedOrConnecting()) return
|
||||
|
||||
assertTrue(netInfo.isAvailable())
|
||||
if (State.CONNECTED == netInfo.getState()) {
|
||||
assertTrue(netInfo.isConnected())
|
||||
}
|
||||
assertTrue(State.CONNECTING == netInfo.getState() ||
|
||||
State.CONNECTED == netInfo.getState())
|
||||
assertTrue(DetailedState.SCANNING == netInfo.getDetailedState() ||
|
||||
DetailedState.CONNECTING == netInfo.getDetailedState() ||
|
||||
DetailedState.AUTHENTICATING == netInfo.getDetailedState() ||
|
||||
DetailedState.CONNECTED == netInfo.getDetailedState())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user