From cdf45529adfa539854910b603894fc1fdda987e7 Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Wed, 14 Jul 2021 13:19:58 +0800 Subject: [PATCH] Add assertNoCallbackThat assertNoCallback() fails when any callbacks are received. In some cases, the test is expected to not to receive certain callback, like available or lost. Depending on the network environment, network may trigger linkproperties change. It will fail the test, but these callback does not go against the intention of the test. It should not fail the test. Thus, add this method to allow tests to verify no certain callback received. Bug: 192239030 Test: atest TestableNetworkCallbackTest Change-Id: I7db720becb23636f85a9ac21192f8185eceb22fe --- .../com/android/testutils/TestableNetworkCallback.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkCallback.kt b/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkCallback.kt index 1b3d0f69e1..7d851f114a 100644 --- a/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkCallback.kt +++ b/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkCallback.kt @@ -116,6 +116,7 @@ open class RecorderCallback private constructor( val mark get() = history.mark override fun onAvailable(network: Network) { + Log.d(TAG, "onAvailable $network") history.add(Available(network)) } @@ -199,6 +200,15 @@ open class TestableNetworkCallback private constructor( if (null != cb) fail("Expected no callback but got $cb") } + fun assertNoCallbackThat( + timeoutMs: Long = defaultTimeoutMs, + valid: (CallbackEntry) -> Boolean + ) { + val cb = history.poll(timeoutMs) { valid(it) }.let { + if (null != it) fail("Expected no callback but got $it") + } + } + // Expects a callback of the specified type on the specified network within the timeout. // If no callback arrives, or a different callback arrives, fail. Returns the callback. inline fun expectCallback(