From 4c7ba24f57bdc7d53e8f28d87c36372bb9ce8fa9 Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Tue, 1 Jul 2025 18:15:51 +0800 Subject: [PATCH] Refactor: Introduce TestableCallback for EthernetStateListener The monolithic EthernetStateListener has been refactored and split into two specialized test classes: - TestInterfaceStateListener: For interface state callbacks. - TestEthernetStateListener: For ethernet state callbacks. Both new classes now extend TestableCallback, providing a standardized and more robust way to manage and verify listener interactions in unit tests. This improves testability and clarity of test code. This is a no-op, test-only change. Flag: EXEMPT refactor Test: atest CtsNetTestCases:android.net.cts.EthernetManagerTest Bug: 410731080 Change-Id: I667e3607954dcb073b2732b16bedfbc71a70bb39 --- .../testutils/EthernetStateListener.kt | 147 ----------------- .../testutils/TestEthernetStateListener.kt | 119 ++++++++++++++ .../android/net/cts/EthernetManagerTest.kt | 151 ++++++++++-------- 3 files changed, 203 insertions(+), 214 deletions(-) delete mode 100644 staticlibs/testutils/devicetests/com/android/testutils/EthernetStateListener.kt create mode 100644 staticlibs/testutils/devicetests/com/android/testutils/TestEthernetStateListener.kt diff --git a/staticlibs/testutils/devicetests/com/android/testutils/EthernetStateListener.kt b/staticlibs/testutils/devicetests/com/android/testutils/EthernetStateListener.kt deleted file mode 100644 index 7fc2056850..0000000000 --- a/staticlibs/testutils/devicetests/com/android/testutils/EthernetStateListener.kt +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright (C) 2025 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.EthernetManager.ETHERNET_STATE_DISABLED -import android.net.EthernetManager.ETHERNET_STATE_ENABLED -import android.net.EthernetManager.InterfaceStateListener -import android.net.EthernetManager.ROLE_CLIENT -import android.net.EthernetManager.ROLE_NONE -import android.net.EthernetManager.ROLE_SERVER -import android.net.EthernetManager.STATE_ABSENT -import android.net.EthernetManager.STATE_LINK_DOWN -import android.net.EthernetManager.STATE_LINK_UP -import android.net.IpConfiguration -import com.android.net.module.util.ArrayTrackRecord -import com.android.net.module.util.TrackRecord -import java.util.function.IntConsumer -import kotlin.test.assertEquals -import kotlin.test.assertNotNull -import kotlin.test.assertNull - -private const val TIMEOUT_MS = 10_000L -private const val NO_CALLBACK_TIMEOUT_MS = 500L - -private val DEFAULT_IP_CONFIGURATION = IpConfiguration( - IpConfiguration.IpAssignment.DHCP, - IpConfiguration.ProxySettings.NONE, - null, - null -) - -/** - * A listener for both ethernet interface state and ethernet feature state. - * - * This class can be used to track both onInterfaceStateChanged and onEthernetStateChanged - * callbacks from EthernetManager. - */ -open class EthernetStateListener private constructor( - private val history: ArrayTrackRecord -) : InterfaceStateListener, IntConsumer, TrackRecord by history { - constructor() : this(ArrayTrackRecord()) - - val events = history.newReadHead() - - sealed class Event { - data class InterfaceStateChanged( - val iface: String, - val state: Int, - val role: Int, - val configuration: IpConfiguration? - ) : Event() { - override fun toString(): String { - val stateString = when (state) { - STATE_ABSENT -> "STATE_ABSENT" - STATE_LINK_UP -> "STATE_LINK_UP" - STATE_LINK_DOWN -> "STATE_LINK_DOWN" - else -> state.toString() - } - val roleString = when (role) { - ROLE_NONE -> "ROLE_NONE" - ROLE_CLIENT -> "ROLE_CLIENT" - ROLE_SERVER -> "ROLE_SERVER" - else -> role.toString() - } - return ("InterfaceStateChanged(iface=$iface, state=$stateString, " + - "role=$roleString, ipConfig=$configuration)") - } - } - - data class EthernetStateChanged(val state: Int) : Event() { - override fun toString(): String { - val stateString = when (state) { - ETHERNET_STATE_ENABLED -> "ETHERNET_STATE_ENABLED" - ETHERNET_STATE_DISABLED -> "ETHERNET_STATE_DISABLED" - else -> state.toString() - } - return "EthernetStateChanged(state=$stateString)" - } - } - } - - override fun onInterfaceStateChanged( - iface: String, - state: Int, - role: Int, - cfg: IpConfiguration? - ) { - add(Event.InterfaceStateChanged(iface, state, role, cfg)) - } - - override fun accept(state: Int) { - add(Event.EthernetStateChanged(state)) - } - - fun expectCallback(expected: T): T { - val event = events.poll(TIMEOUT_MS) - assertEquals(expected, event) - return event as T - } - - fun expectCallback(ifaceName: String, state: Int, role: Int) { - expectCallback(createChangeEvent(ifaceName, state, role)) - } - - fun expectCallback(state: Int) { - expectCallback(Event.EthernetStateChanged(state)) - } - - private fun createChangeEvent(iface: String, state: Int, role: Int) = - Event.InterfaceStateChanged( - iface, - state, - role, - if (state != STATE_ABSENT) DEFAULT_IP_CONFIGURATION else null - ) - - fun eventuallyExpect(expected: Event) { - val cb = events.poll(TIMEOUT_MS) { it == expected } - assertNotNull(cb, "Never received expected $expected. Received: ${events.backtrace()}") - } - - fun eventuallyExpect(ifaceName: String, state: Int, role: Int) { - eventuallyExpect(createChangeEvent(ifaceName, state, role)) - } - - fun eventuallyExpect(state: Int) { - eventuallyExpect(Event.EthernetStateChanged(state)) - } - - fun assertNoCallback() { - val cb = events.poll(NO_CALLBACK_TIMEOUT_MS) - assertNull(cb, "Expected no callback but got $cb") - } -} diff --git a/staticlibs/testutils/devicetests/com/android/testutils/TestEthernetStateListener.kt b/staticlibs/testutils/devicetests/com/android/testutils/TestEthernetStateListener.kt new file mode 100644 index 0000000000..eac352ba31 --- /dev/null +++ b/staticlibs/testutils/devicetests/com/android/testutils/TestEthernetStateListener.kt @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2025 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.EthernetManager.ETHERNET_STATE_DISABLED +import android.net.EthernetManager.ETHERNET_STATE_ENABLED +import android.net.EthernetManager.InterfaceStateListener +import android.net.EthernetManager.ROLE_CLIENT +import android.net.EthernetManager.ROLE_NONE +import android.net.EthernetManager.ROLE_SERVER +import android.net.EthernetManager.STATE_ABSENT +import android.net.EthernetManager.STATE_LINK_DOWN +import android.net.EthernetManager.STATE_LINK_UP +import android.net.IpConfiguration +import com.android.net.module.util.TestableCallback +import com.android.testutils.TestEthernetStateListener.EthernetStateChanged +import com.android.testutils.TestInterfaceStateListener.InterfaceStateChanged +import java.util.function.IntConsumer + +private val DEFAULT_IP_CONFIGURATION = IpConfiguration( + IpConfiguration.IpAssignment.DHCP, + IpConfiguration.ProxySettings.NONE, + null, + null +) + +/** + * A test listener for interface state. + */ +class TestInterfaceStateListener : TestableCallback(), + InterfaceStateListener { + data class InterfaceStateChanged( + val iface: String, + val state: Int, + val role: Int, + val configuration: IpConfiguration? + ) { + override fun toString(): String { + val stateString = when (state) { + STATE_ABSENT -> "STATE_ABSENT" + STATE_LINK_UP -> "STATE_LINK_UP" + STATE_LINK_DOWN -> "STATE_LINK_DOWN" + else -> state.toString() + } + val roleString = when (role) { + ROLE_NONE -> "ROLE_NONE" + ROLE_CLIENT -> "ROLE_CLIENT" + ROLE_SERVER -> "ROLE_SERVER" + else -> role.toString() + } + return ("InterfaceStateChanged(iface=$iface, state=$stateString, " + + "role=$roleString, ipConfig=$configuration)") + } + } + + override fun onInterfaceStateChanged( + iface: String, + state: Int, + role: Int, + configuration: IpConfiguration?, + ) { + history.add(InterfaceStateChanged(iface, state, role, configuration)) + } + + fun expectInterfaceStateChanged( + iface: String, + state: Int, + role: Int, + timeoutMs: Long = defaultTimeoutMs + ): InterfaceStateChanged = expect(timeoutMs = timeoutMs) { + val expectedConfig = if (state != STATE_ABSENT) DEFAULT_IP_CONFIGURATION else null + it.iface == iface && it.state == state && it.role == role && + it.configuration == expectedConfig + } + + fun eventuallyExpectInterfaceStateChanged( + iface: String, + state: Int, + role: Int, + timeoutMs: Long = defaultTimeoutMs + ): InterfaceStateChanged = eventuallyExpect(timeoutMs = timeoutMs) { + val expectedConfig = if (state != STATE_ABSENT) DEFAULT_IP_CONFIGURATION else null + it.iface == iface && it.state == state && it.role == role && + it.configuration == expectedConfig + } +} + +/** + * A test listener for ethernet state. + */ +class TestEthernetStateListener : TestableCallback(), IntConsumer { + data class EthernetStateChanged(val state: Int) { + override fun toString(): String { + val stateString = when (state) { + ETHERNET_STATE_ENABLED -> "ETHERNET_STATE_ENABLED" + ETHERNET_STATE_DISABLED -> "ETHERNET_STATE_DISABLED" + else -> state.toString() + } + return "EthernetStateChanged(state=$stateString)" + } + } + + override fun accept(state: Int) { + history.add(EthernetStateChanged(state)) + } +} diff --git a/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt b/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt index a9c70dc7df..77f94f8cef 100644 --- a/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt +++ b/tests/cts/net/src/android/net/cts/EthernetManagerTest.kt @@ -64,9 +64,11 @@ import com.android.testutils.ConnectivityModuleTest import com.android.testutils.DevSdkIgnoreRule import com.android.testutils.DevSdkIgnoreRunner import com.android.testutils.DeviceInfoUtils.isKernelVersionAtLeast -import com.android.testutils.EthernetStateListener import com.android.testutils.NdResponder import com.android.testutils.PollPacketReader +import com.android.testutils.TestEthernetStateListener +import com.android.testutils.TestEthernetStateListener.EthernetStateChanged +import com.android.testutils.TestInterfaceStateListener import com.android.testutils.TestableNetworkCallback import com.android.testutils.TestableNetworkCallback.Event.Available import com.android.testutils.TestableNetworkCallback.Event.CapabilitiesChanged @@ -146,9 +148,10 @@ class EthernetManagerTest { private val cm by lazy { context.getSystemService(ConnectivityManager::class.java)!! } private val handler by lazy { Handler(Looper.getMainLooper()) } - private val ifaceListener = EthernetStateListener() + private val ifaceListener = TestInterfaceStateListener() private val createdIfaces = ArrayList() - private val addedListeners = ArrayList() + private val addedInterfaceStateListeners = ArrayList() + private val addedEthernetStateListeners = ArrayList() private val registeredCallbacks = ArrayList() private var tetheredInterfaceRequest: TetheredInterfaceRequest? = null @@ -285,16 +288,16 @@ class EthernetManagerTest { for (iface in createdIfaces) { iface.destroy() - ifaceListener.eventuallyExpect(iface.name, STATE_ABSENT, ROLE_NONE) + ifaceListener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_ABSENT, ROLE_NONE) } // After test interfaces are removed, disable tracking. setIncludeTestInterfaces(false) - for (listener in addedListeners) { - // Even if a given listener was not registered as both an interface and ethernet state - // listener, calling remove is safe. + for (listener in addedInterfaceStateListeners) { em.removeInterfaceStateListener(listener) + } + for (listener in addedEthernetStateListeners) { em.removeEthernetStateListener(listener) } registeredCallbacks.forEach { cm.unregisterNetworkCallback(it) } @@ -327,14 +330,14 @@ class EthernetManagerTest { SystemProperties.getInt("service.adb.tcp.port", -1) > -1) } - private fun addInterfaceStateListener(listener: EthernetStateListener) { + private fun addInterfaceStateListener(listener: TestInterfaceStateListener) { em.addInterfaceStateListener(handler::post, listener) - addedListeners.add(listener) + addedInterfaceStateListeners.add(listener) } - private fun addEthernetStateListener(listener: EthernetStateListener) { + private fun addEthernetStateListener(listener: TestEthernetStateListener) { em.addEthernetStateListener(handler::post, listener) - addedListeners.add(listener) + addedEthernetStateListeners.add(listener) } // WARNING: setting hasCarrier to false requires kernel support. Call @@ -347,9 +350,13 @@ class EthernetManagerTest { ).also { createdIfaces.add(it) } // when an interface comes up, we should always see a down cb before an up cb. - ifaceListener.eventuallyExpect(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) + ifaceListener.eventuallyExpectInterfaceStateChanged( + iface.name, + STATE_LINK_DOWN, + ROLE_CLIENT + ) if (hasCarrier && ethernetEnabled) { - ifaceListener.expectCallback(iface.name, STATE_LINK_UP, ROLE_CLIENT) + ifaceListener.expectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) } return iface } @@ -363,7 +370,7 @@ class EthernetManagerTest { private fun removeInterface(iface: EthernetTestInterface) { iface.destroy() createdIfaces.remove(iface) - ifaceListener.eventuallyExpect(iface.name, STATE_ABSENT, ROLE_NONE) + ifaceListener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_ABSENT, ROLE_NONE) } private fun requestNetwork(request: NetworkRequest): TestableNetworkCallback { @@ -444,9 +451,11 @@ class EthernetManagerTest { runAsShell(NETWORK_SETTINGS) { em.setEthernetEnabled(enabled) } ethernetEnabled = enabled - val listener = EthernetStateListener() + val listener = TestEthernetStateListener() addEthernetStateListener(listener) - listener.eventuallyExpect(if (enabled) ETHERNET_STATE_ENABLED else ETHERNET_STATE_DISABLED) + listener.eventuallyExpect { + it.state == if (enabled) ETHERNET_STATE_ENABLED else ETHERNET_STATE_DISABLED + } } // NetworkRequest.Builder does not create a copy of the passed NetworkRequest, so in order to @@ -493,35 +502,43 @@ class EthernetManagerTest { // If an interface exists when the callback is registered, it is reported on registration. val iface = createInterface() - val listener1 = EthernetStateListener() + val listener1 = TestInterfaceStateListener() addInterfaceStateListener(listener1) - listener1.expectCallback(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener1.expectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) // If an interface appears, existing callbacks see it. val iface2 = createInterface() - listener1.expectCallback(iface2.name, STATE_LINK_DOWN, ROLE_CLIENT) - listener1.expectCallback(iface2.name, STATE_LINK_UP, ROLE_CLIENT) + listener1.expectInterfaceStateChanged(iface2.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener1.expectInterfaceStateChanged(iface2.name, STATE_LINK_UP, ROLE_CLIENT) // Register a new listener, it should see state of all existing interfaces immediately. - val listener2 = EthernetStateListener() + val listener2 = TestInterfaceStateListener() addInterfaceStateListener(listener2) // Note that interfaces are in lexicographical order with restricted interfaces appearing // last. This means that testtap10 is ordered ahead of testtap9. val sortedIfaces = listOf(iface, iface2).sortedWith(compareBy { it.name }) - listener2.expectCallback(sortedIfaces.elementAt(0).name, STATE_LINK_UP, ROLE_CLIENT) - listener2.expectCallback(sortedIfaces.elementAt(1).name, STATE_LINK_UP, ROLE_CLIENT) + listener2.expectInterfaceStateChanged( + sortedIfaces.elementAt(0).name, + STATE_LINK_UP, + ROLE_CLIENT + ) + listener2.expectInterfaceStateChanged( + sortedIfaces.elementAt(1).name, + STATE_LINK_UP, + ROLE_CLIENT + ) // Removing interfaces first sends link down, then STATE_ABSENT/ROLE_NONE. removeInterface(iface) for (listener in listOf(listener1, listener2)) { - listener.expectCallback(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) - listener.expectCallback(iface.name, STATE_ABSENT, ROLE_NONE) + listener.expectInterfaceStateChanged(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface.name, STATE_ABSENT, ROLE_NONE) } removeInterface(iface2) for (listener in listOf(listener1, listener2)) { - listener.expectCallback(iface2.name, STATE_LINK_DOWN, ROLE_CLIENT) - listener.expectCallback(iface2.name, STATE_ABSENT, ROLE_NONE) + listener.expectInterfaceStateChanged(iface2.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface2.name, STATE_ABSENT, ROLE_NONE) listener.assertNoCallback() } } @@ -560,20 +577,20 @@ class EthernetManagerTest { val iface = createInterface() requestTetheredInterface().expectOnAvailable() - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) // TODO(b/295146844): do not report IpConfiguration for server mode interfaces. - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_SERVER) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_SERVER) releaseTetheredInterface() - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) requestTetheredInterface().expectOnAvailable() // This should be changed to expectCallback, once b/236895792 is fixed. - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_SERVER) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_SERVER) releaseTetheredInterface() - listener.expectCallback(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) } @Test @@ -585,7 +602,7 @@ class EthernetManagerTest { requestTetheredInterface().expectOnAvailable() removeInterface(iface) - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) listener.assertNoCallback() } @@ -606,7 +623,7 @@ class EthernetManagerTest { setIncludeTestInterfaces(false) setEthernetEnabled(true) - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) listener.assertNoCallback() @@ -614,9 +631,9 @@ class EthernetManagerTest { setIncludeTestInterfaces(true) setEthernetEnabled(true) - listener.expectCallback(iface1.name, STATE_LINK_UP, ROLE_CLIENT) - listener.expectCallback(iface2.name, STATE_LINK_UP, ROLE_CLIENT) - listener.expectCallback(iface3.name, STATE_LINK_UP, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface1.name, STATE_LINK_UP, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface2.name, STATE_LINK_UP, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface3.name, STATE_LINK_UP, ROLE_CLIENT) listener.assertNoCallback() } @@ -783,20 +800,20 @@ class EthernetManagerTest { fun testRemoveInterface_whileInServerMode() { assumeNoInterfaceForTetheringAvailable() - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) val iface = createInterface() val ifaceName = requestTetheredInterface().expectOnAvailable() assertEquals(iface.name, ifaceName) - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_SERVER) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_SERVER) removeInterface(iface) // Note: removeInterface already verifies that a STATE_ABSENT, ROLE_NONE callback is // received, but it can't hurt to explicitly check for it. - listener.expectCallback(iface.name, STATE_ABSENT, ROLE_NONE) + listener.expectInterfaceStateChanged(iface.name, STATE_ABSENT, ROLE_NONE) releaseTetheredInterface() listener.assertNoCallback() } @@ -838,43 +855,43 @@ class EthernetManagerTest { @Test fun testEnableDisableInterface_callbacks() { val iface = createInterface() - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) // Uses eventuallyExpect to account for interfaces that could already exist on device - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) disableInterface(iface).expectResult(iface.name) - listener.eventuallyExpect(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) enableInterface(iface).expectResult(iface.name) - listener.expectCallback(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) disableInterface(iface).expectResult(iface.name) - listener.expectCallback(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) } @Test fun testEnableDisableInterface_disableEnableEthernet() { val iface = createInterface() - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) // When ethernet is disabled, interface should be down and enable/disableInterface() // should not bring the interfaces up. setEthernetEnabled(false) - listener.eventuallyExpect(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) enableInterface(iface).expectError() disableInterface(iface).expectError() listener.assertNoCallback() // When ethernet is enabled, enable/disableInterface() should succeed. setEthernetEnabled(true) - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) disableInterface(iface).expectResult(iface.name) - listener.eventuallyExpect(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) enableInterface(iface).expectResult(iface.name) - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) } @Test @@ -992,52 +1009,52 @@ class EthernetManagerTest { @Test fun testUpdateConfiguration_callbacksForMissingInterface() { - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) val bogusIfname = "testtap000" updateConfiguration(bogusIfname, STATIC_IP_CONFIGURATION).expectResult(bogusIfname) - listener.eventuallyExpect(bogusIfname, STATE_ABSENT, ROLE_NONE) + listener.eventuallyExpectInterfaceStateChanged(bogusIfname, STATE_ABSENT, ROLE_NONE) } @Test fun testAddInterface_disableEnableEthernet() { - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) // When ethernet is disabled, newly added interfaces should not be brought up. setEthernetEnabled(false) val iface = createInterface(/* hasCarrier */ true) - listener.eventuallyExpect(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) // When ethernet is re-enabled after interface is added, it will be brought up. setEthernetEnabled(true) - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) } @Test fun testRemoveInterface_disableEnableEthernet() { // Set up 2 interfaces for testing val iface1 = createInterface() - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) - listener.eventuallyExpect(iface1.name, STATE_LINK_UP, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface1.name, STATE_LINK_UP, ROLE_CLIENT) val iface2 = createInterface() - listener.eventuallyExpect(iface2.name, STATE_LINK_UP, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface2.name, STATE_LINK_UP, ROLE_CLIENT) // Removing interfaces when ethernet is enabled will first send link down, then // STATE_ABSENT/ROLE_NONE. removeInterface(iface1) - listener.expectCallback(iface1.name, STATE_LINK_DOWN, ROLE_CLIENT) - listener.expectCallback(iface1.name, STATE_ABSENT, ROLE_NONE) + listener.expectInterfaceStateChanged(iface1.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface1.name, STATE_ABSENT, ROLE_NONE) // Removing interfaces after ethernet is disabled will first send link down when ethernet is // disabled, then STATE_ABSENT/ROLE_NONE when interface is removed. setEthernetEnabled(false) - listener.expectCallback(iface2.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.expectInterfaceStateChanged(iface2.name, STATE_LINK_DOWN, ROLE_CLIENT) removeInterface(iface2) - listener.expectCallback(iface2.name, STATE_ABSENT, ROLE_NONE) + listener.expectInterfaceStateChanged(iface2.name, STATE_ABSENT, ROLE_NONE) } @Test @@ -1045,12 +1062,12 @@ class EthernetManagerTest { // do not run this test if an interface that can be used for tethering already exists. assumeNoInterfaceForTetheringAvailable() - val listener = EthernetStateListener() + val listener = TestInterfaceStateListener() addInterfaceStateListener(listener) val iface = createInterface() requestTetheredInterface().expectOnAvailable() - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_SERVER) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_SERVER) // (b/234743836): Currently the state of server mode interfaces always returns true due to // that interface state for server mode interfaces is not tracked properly. @@ -1060,11 +1077,11 @@ class EthernetManagerTest { // When ethernet is disabled, change interface mode will not bring the interface up. releaseTetheredInterface() - listener.eventuallyExpect(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_DOWN, ROLE_CLIENT) // When ethernet is re-enabled, interface will be brought up. setEthernetEnabled(true) - listener.eventuallyExpect(iface.name, STATE_LINK_UP, ROLE_CLIENT) + listener.eventuallyExpectInterfaceStateChanged(iface.name, STATE_LINK_UP, ROLE_CLIENT) } @Test