1 /* 2 * Copyright (C) 2024 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.lifecycle 18 19 import androidx.compose.material3.Text 20 import androidx.compose.ui.test.hasText 21 import androidx.compose.ui.test.junit4.createComposeRule 22 import androidx.test.ext.junit.runners.AndroidJUnit4 23 import com.android.settingslib.spa.testutils.waitUntilExists 24 import kotlinx.coroutines.flow.MutableStateFlow 25 import kotlinx.coroutines.flow.flowOf 26 import org.junit.Rule 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 30 @RunWith(AndroidJUnit4::class) 31 class FlowExtTest { 32 @get:Rule 33 val composeTestRule = createComposeRule() 34 35 @Test collectAsCallbackWithLifecyclenull36 fun collectAsCallbackWithLifecycle() { 37 val flow = flowOf(TEXT) 38 39 composeTestRule.setContent { 40 val callback = flow.collectAsCallbackWithLifecycle() 41 Text(callback() ?: "") 42 } 43 44 composeTestRule.waitUntilExists(hasText(TEXT)) 45 } 46 47 @Test collectAsCallbackWithLifecycle_changednull48 fun collectAsCallbackWithLifecycle_changed() { 49 val flow = MutableStateFlow(TEXT) 50 51 composeTestRule.setContent { 52 val callback = flow.collectAsCallbackWithLifecycle() 53 Text(callback() ?: "") 54 } 55 flow.value = NEW_TEXT 56 57 composeTestRule.waitUntilExists(hasText(NEW_TEXT)) 58 } 59 60 private companion object { 61 const val TEXT = "Text" 62 const val NEW_TEXT = "New Text" 63 } 64 } 65