Move pollingCheck to testutils

Test: TH
Change-Id: I7d9362dcda095f8037467baf3602911fc3a3c072
This commit is contained in:
Patrick Rohr 2025-02-25 12:29:50 -08:00
commit b4809fdd08
2 changed files with 31 additions and 11 deletions

View file

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

View file

@ -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")