From 9a27925368fcd2450a62740e57ecd5f906d2b0b8 Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Wed, 24 Mar 2021 18:49:19 +0800 Subject: [PATCH] Update tests for onNetworkCreated and onNetworkDestroyed Update test to: 1. verify onNetworkCreated callback called when network is created. 2. Verify onNetworkDestroyed callback called when agent updates its connection state to disconnect and native network is destroyed. Bug: 178725261 Test: atest android.net.cts.NetworkAgentTest Change-Id: Icd30440e0f553d0f97b860d0e69b4f3d4f0a2a1d --- .../src/android/net/cts/NetworkAgentTest.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt b/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt index ef529f8289..a95897d655 100644 --- a/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt +++ b/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt @@ -55,6 +55,8 @@ import android.net.VpnTransportInfo import android.net.cts.NetworkAgentTest.TestableNetworkAgent.CallbackEntry.OnAddKeepalivePacketFilter import android.net.cts.NetworkAgentTest.TestableNetworkAgent.CallbackEntry.OnAutomaticReconnectDisabled import android.net.cts.NetworkAgentTest.TestableNetworkAgent.CallbackEntry.OnBandwidthUpdateRequested +import android.net.cts.NetworkAgentTest.TestableNetworkAgent.CallbackEntry.OnNetworkCreated +import android.net.cts.NetworkAgentTest.TestableNetworkAgent.CallbackEntry.OnNetworkDestroyed import android.net.cts.NetworkAgentTest.TestableNetworkAgent.CallbackEntry.OnNetworkUnwanted import android.net.cts.NetworkAgentTest.TestableNetworkAgent.CallbackEntry.OnRemoveKeepalivePacketFilter import android.net.cts.NetworkAgentTest.TestableNetworkAgent.CallbackEntry.OnSaveAcceptUnvalidated @@ -216,6 +218,8 @@ class NetworkAgentTest { object OnAutomaticReconnectDisabled : CallbackEntry() data class OnValidationStatus(val status: Int, val uri: Uri?) : CallbackEntry() data class OnSignalStrengthThresholdsUpdated(val thresholds: IntArray) : CallbackEntry() + object OnNetworkCreated : CallbackEntry() + object OnNetworkDestroyed : CallbackEntry() } override fun onBandwidthUpdateRequested() { @@ -269,6 +273,14 @@ class NetworkAgentTest { history.add(OnValidationStatus(status, uri)) } + override fun onNetworkCreated() { + history.add(OnNetworkCreated) + } + + override fun onNetworkDestroyed() { + history.add(OnNetworkDestroyed) + } + // Expects the initial validation event that always occurs immediately after registering // a NetworkAgent whose network does not require validation (which test networks do // not, since they lack the INTERNET capability). It always contains the default argument @@ -347,8 +359,10 @@ class NetworkAgentTest { val callback = TestableNetworkCallback(timeoutMs = DEFAULT_TIMEOUT_MS) requestNetwork(request, callback) val agent = createNetworkAgent(context, name) + agent.setTeardownDelayMs(0) agent.register() agent.markConnected() + agent.expectCallback() return agent to callback } @@ -368,6 +382,7 @@ class NetworkAgentTest { assertFailsWith("Must not be able to register an agent twice") { agent.register() } + agent.expectCallback() } @Test @@ -627,7 +642,8 @@ class NetworkAgentTest { val mockContext = mock(Context::class.java) val mockCm = mock(ConnectivityManager::class.java) doReturn(mockCm).`when`(mockContext).getSystemService(Context.CONNECTIVITY_SERVICE) - createConnectedNetworkAgent(mockContext) + val agent = createNetworkAgent(mockContext) + agent.register() verify(mockCm).registerNetworkAgent(any(), argThat { it.detailedState == NetworkInfo.DetailedState.CONNECTING }, any(LinkProperties::class.java),