From ad49d71fabe62c3593a891621f0f911f90fba419 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Thu, 17 Jun 2021 08:47:42 +0000 Subject: Allow expect onRequestStatsUpdated with any token Altough token field is not used for now. But by design, the token will be the version number of networkIdentity mappings that maintain by the service, which cannot be predicted by the test. Thus, allow expecting any token is necessary. Ignore-AOSP-First: Counter part CL is not available in aosp. Test: m gts && atest \ GtsNetworkStackHostTestCases:NetworkStatsHostTest#testNetworkStatsManager Bug: 191327585 Change-Id: Ibf10040a5c10b7b1c7d381c01a071fb157409004 --- .../com/android/testutils/TestableNetworkStatsProvider.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'common') diff --git a/common/testutils/devicetests/com/android/testutils/TestableNetworkStatsProvider.kt b/common/testutils/devicetests/com/android/testutils/TestableNetworkStatsProvider.kt index d5c3a2a0..d034a7d5 100644 --- a/common/testutils/devicetests/com/android/testutils/TestableNetworkStatsProvider.kt +++ b/common/testutils/devicetests/com/android/testutils/TestableNetworkStatsProvider.kt @@ -23,6 +23,7 @@ import kotlin.test.assertTrue import kotlin.test.fail private const val DEFAULT_TIMEOUT_MS = 200L +const val TOKEN_ANY = -1 open class TestableNetworkStatsProvider( val defaultTimeoutMs: Long = DEFAULT_TIMEOUT_MS @@ -49,8 +50,13 @@ open class TestableNetworkStatsProvider( history.add(CallbackType.OnSetAlert(quotaBytes)) } - fun expectOnRequestStatsUpdate(token: Int, timeout: Long = defaultTimeoutMs) { - assertEquals(CallbackType.OnRequestStatsUpdate(token), history.poll(timeout)) + fun expectOnRequestStatsUpdate(token: Int, timeout: Long = defaultTimeoutMs): Int { + val event = history.poll(timeout) + assertTrue(event is CallbackType.OnRequestStatsUpdate) + if (token != TOKEN_ANY) { + assertEquals(token, event.token) + } + return token } fun expectOnSetLimit(iface: String?, quotaBytes: Long, timeout: Long = defaultTimeoutMs) { -- cgit v1.2.3 From ddb932871640408d22626769e9e198ef11631984 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Mon, 21 Jun 2021 13:54:13 +0000 Subject: Add TestableNetworkOfferCallback Ignore-AOSP-First: Needs cherry-picks Test: android.net.NetworkProviderTest on R/S device Bug: 189074532 Original-Change: https://android-review.googlesource.com/1731491 Merged-In: Iac7ec1bb4c34b4354f7b43fec86ded68c1a1d0de Change-Id: Iac7ec1bb4c34b4354f7b43fec86ded68c1a1d0de --- .../testutils/TestableNetworkOfferCallback.kt | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 common/testutils/devicetests/com/android/testutils/TestableNetworkOfferCallback.kt (limited to 'common') diff --git a/common/testutils/devicetests/com/android/testutils/TestableNetworkOfferCallback.kt b/common/testutils/devicetests/com/android/testutils/TestableNetworkOfferCallback.kt new file mode 100644 index 00000000..21bd60c9 --- /dev/null +++ b/common/testutils/devicetests/com/android/testutils/TestableNetworkOfferCallback.kt @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2021 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.testutils + +import android.net.NetworkCapabilities +import android.net.NetworkProvider +import android.net.NetworkRequest +import android.util.Log +import com.android.net.module.util.ArrayTrackRecord +import kotlin.test.fail + +class TestableNetworkOfferCallback(val timeoutMs: Long, private val noCallbackTimeoutMs: Long) + : NetworkProvider.NetworkOfferCallback { + private val TAG = this::class.simpleName + val history = ArrayTrackRecord().newReadHead() + + sealed class CallbackEntry { + data class OnNetworkNeeded(val request: NetworkRequest) : CallbackEntry() + data class OnNetworkUnneeded(val request: NetworkRequest) : CallbackEntry() + } + + /** + * Called by the system when a network for this offer is needed to satisfy some + * networking request. + */ + override fun onNetworkNeeded(request: NetworkRequest) { + Log.d(TAG, "onNetworkNeeded $request") + history.add(CallbackEntry.OnNetworkNeeded(request)) + } + + /** + * Called by the system when this offer is no longer valuable for this request. + */ + override fun onNetworkUnneeded(request: NetworkRequest) { + Log.d(TAG, "onNetworkUnneeded $request") + history.add(CallbackEntry.OnNetworkUnneeded(request)) + } + + inline fun expectCallbackThat( + crossinline predicate: (T) -> Boolean + ) { + val event = history.poll(timeoutMs) + ?: fail("Did not receive callback after ${timeoutMs}ms") + if (event !is T || !predicate(event)) fail("Received unexpected callback $event") + } + + fun expectOnNetworkNeeded(capabilities: NetworkCapabilities) = + expectCallbackThat { + it.request.canBeSatisfiedBy(capabilities) + } + + fun expectOnNetworkUnneeded(capabilities: NetworkCapabilities) = + expectCallbackThat { + it.request.canBeSatisfiedBy(capabilities) + } + + fun assertNoCallback() { + val cb = history.poll(noCallbackTimeoutMs) + if (null != cb) fail("Expected no callback but got $cb") + } +} \ No newline at end of file -- cgit v1.2.3