Add a keep connected for test reason
This has been sorely needed for a while. Instead of filing requests for each of your networks in a CSTest, which is fiddly at best and sometimes almost impossible (because you can't single out the network), you can now add this flag and be done with it. Test: CSKeepConnectedTest Change-Id: Ie168fe1f3a17de035fdf05e3d6580d3262a3448e
This commit is contained in:
@@ -44,7 +44,8 @@ public final class NetworkScore implements Parcelable {
|
|||||||
@Retention(RetentionPolicy.SOURCE)
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
@IntDef(value = {
|
@IntDef(value = {
|
||||||
KEEP_CONNECTED_NONE,
|
KEEP_CONNECTED_NONE,
|
||||||
KEEP_CONNECTED_FOR_HANDOVER
|
KEEP_CONNECTED_FOR_HANDOVER,
|
||||||
|
KEEP_CONNECTED_FOR_TEST
|
||||||
})
|
})
|
||||||
public @interface KeepConnectedReason { }
|
public @interface KeepConnectedReason { }
|
||||||
|
|
||||||
@@ -57,6 +58,12 @@ public final class NetworkScore implements Parcelable {
|
|||||||
* is being considered for handover.
|
* is being considered for handover.
|
||||||
*/
|
*/
|
||||||
public static final int KEEP_CONNECTED_FOR_HANDOVER = 1;
|
public static final int KEEP_CONNECTED_FOR_HANDOVER = 1;
|
||||||
|
/**
|
||||||
|
* Keep this network connected even if there is no outstanding request for it, because it
|
||||||
|
* is used in a test and it's not necessarily easy to file the right request for it.
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
public static final int KEEP_CONNECTED_FOR_TEST = 2;
|
||||||
|
|
||||||
// Agent-managed policies
|
// Agent-managed policies
|
||||||
// This network should lose to a wifi that has ever been validated
|
// This network should lose to a wifi that has ever been validated
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2023 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 com.android.server
|
||||||
|
|
||||||
|
import android.net.NetworkRequest
|
||||||
|
import android.net.NetworkScore
|
||||||
|
import android.net.NetworkScore.KEEP_CONNECTED_FOR_TEST
|
||||||
|
import android.os.Build
|
||||||
|
import androidx.test.filters.SmallTest
|
||||||
|
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
|
||||||
|
import com.android.testutils.DevSdkIgnoreRunner
|
||||||
|
import com.android.testutils.RecorderCallback.CallbackEntry.Lost
|
||||||
|
import com.android.testutils.TestableNetworkCallback
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
|
||||||
|
@RunWith(DevSdkIgnoreRunner::class)
|
||||||
|
@SmallTest
|
||||||
|
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
|
||||||
|
class CSKeepConnectedTest : CSTest() {
|
||||||
|
@Test
|
||||||
|
fun testKeepConnectedForTest() {
|
||||||
|
val keepAgent = Agent(score = FromS(NetworkScore.Builder()
|
||||||
|
.setKeepConnectedReason(KEEP_CONNECTED_FOR_TEST)
|
||||||
|
.build()))
|
||||||
|
val dontKeepAgent = Agent()
|
||||||
|
doTestKeepConnected(keepAgent, dontKeepAgent)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun doTestKeepConnected(keepAgent: CSAgentWrapper, dontKeepAgent: CSAgentWrapper) {
|
||||||
|
val cb = TestableNetworkCallback()
|
||||||
|
cm.registerNetworkCallback(NetworkRequest.Builder().clearCapabilities().build(), cb)
|
||||||
|
|
||||||
|
keepAgent.connect()
|
||||||
|
dontKeepAgent.connect()
|
||||||
|
|
||||||
|
cb.expectAvailableCallbacks(keepAgent.network, validated = false)
|
||||||
|
cb.expectAvailableCallbacks(dontKeepAgent.network, validated = false)
|
||||||
|
|
||||||
|
// After the nascent timer, the agent without keep connected gets lost.
|
||||||
|
cb.expect<Lost>(dontKeepAgent.network)
|
||||||
|
cb.assertNoCallback()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -129,9 +129,10 @@ class CSAgentWrapper(
|
|||||||
|
|
||||||
fun connect() {
|
fun connect() {
|
||||||
val mgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
val mgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
val request = NetworkRequest.Builder().clearCapabilities()
|
val request = NetworkRequest.Builder().apply {
|
||||||
.addTransportType(nc.transportTypes[0])
|
clearCapabilities()
|
||||||
.build()
|
if (nc.transportTypes.isNotEmpty()) addTransportType(nc.transportTypes[0])
|
||||||
|
}.build()
|
||||||
val cb = TestableNetworkCallback()
|
val cb = TestableNetworkCallback()
|
||||||
mgr.registerNetworkCallback(request, cb)
|
mgr.registerNetworkCallback(request, cb)
|
||||||
agent.markConnected()
|
agent.markConnected()
|
||||||
|
|||||||
Reference in New Issue
Block a user