1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settingslib.spa.testutils 18 19 import kotlinx.coroutines.flow.Flow 20 import kotlinx.coroutines.flow.first 21 import kotlinx.coroutines.flow.toList 22 import kotlinx.coroutines.withTimeoutOrNull 23 firstWithTimeoutOrNullnull24suspend fun <T> Flow<T>.firstWithTimeoutOrNull(timeMillis: Long = 500): T? = 25 withTimeoutOrNull(timeMillis) { 26 first() 27 } 28 toListWithTimeoutnull29suspend fun <T> Flow<T>.toListWithTimeout(timeMillis: Long = 500): List<T> { 30 val list = mutableListOf<T>() 31 return withTimeoutOrNull(timeMillis) { 32 toList(list) 33 } ?: list 34 } 35