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:
Chalard Jean
2023-10-06 19:16:54 +09:00
parent 026ca940d9
commit c517fb1247
3 changed files with 70 additions and 4 deletions

View File

@@ -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()
}
}

View File

@@ -129,9 +129,10 @@ class CSAgentWrapper(
fun connect() {
val mgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val request = NetworkRequest.Builder().clearCapabilities()
.addTransportType(nc.transportTypes[0])
.build()
val request = NetworkRequest.Builder().apply {
clearCapabilities()
if (nc.transportTypes.isNotEmpty()) addTransportType(nc.transportTypes[0])
}.build()
val cb = TestableNetworkCallback()
mgr.registerNetworkCallback(request, cb)
agent.markConnected()