From b4809fdd0862fc8f0f200419f6484f505fe2b738 Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Tue, 25 Feb 2025 12:29:50 -0800 Subject: [PATCH] Move pollingCheck to testutils Test: TH Change-Id: I7d9362dcda095f8037467baf3602911fc3a3c072 --- .../com/android/testutils/PollingUtils.kt | 30 +++++++++++++++++++ .../src/android/net/cts/ApfIntegrationTest.kt | 12 +------- 2 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 staticlibs/testutils/devicetests/com/android/testutils/PollingUtils.kt diff --git a/staticlibs/testutils/devicetests/com/android/testutils/PollingUtils.kt b/staticlibs/testutils/devicetests/com/android/testutils/PollingUtils.kt new file mode 100644 index 0000000000..0a0290a55d --- /dev/null +++ b/staticlibs/testutils/devicetests/com/android/testutils/PollingUtils.kt @@ -0,0 +1,30 @@ +/* + * 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 + +private const val POLLING_INTERVAL_MS: Int = 100 + +/** Calls condition() until it returns true or timeout occurs. */ +fun pollingCheck(condition: () -> Boolean, timeout_ms: Int): Boolean { + var polling_time = 0 + do { + Thread.sleep(POLLING_INTERVAL_MS.toLong()) + polling_time += POLLING_INTERVAL_MS + if (condition()) return true + } while (polling_time < timeout_ms) + return false +} diff --git a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt index c6a1b096e0..4d2e740a48 100644 --- a/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt +++ b/tests/cts/net/src/android/net/cts/ApfIntegrationTest.kt @@ -86,6 +86,7 @@ import com.android.testutils.RecorderCallback.CallbackEntry.Available import com.android.testutils.RecorderCallback.CallbackEntry.LinkPropertiesChanged import com.android.testutils.SkipPresubmit import com.android.testutils.TestableNetworkCallback +import com.android.testutils.pollingCheck import com.android.testutils.waitForIdle import com.google.common.truth.Expect import com.google.common.truth.Truth.assertThat @@ -111,7 +112,6 @@ import org.junit.runner.RunWith private const val TAG = "ApfIntegrationTest" private const val TIMEOUT_MS = 2000L -private const val POLLING_INTERVAL_MS: Int = 100 private const val RCV_BUFFER_SIZE = 1480 private const val PING_HEADER_LENGTH = 8 @@ -129,16 +129,6 @@ class ApfIntegrationTest { private val powerManager = context.getSystemService(PowerManager::class.java)!! private val wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG) - fun pollingCheck(condition: () -> Boolean, timeout_ms: Int): Boolean { - var polling_time = 0 - do { - Thread.sleep(POLLING_INTERVAL_MS.toLong()) - polling_time += POLLING_INTERVAL_MS - if (condition()) return true - } while (polling_time < timeout_ms) - return false - } - fun turnScreenOff() { if (!wakeLock.isHeld()) wakeLock.acquire() runShellCommandOrThrow("input keyevent KEYCODE_SLEEP")