From 210529efc434a8c396456997504ed2e43f3bdb23 Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Mon, 5 Aug 2024 10:23:02 +0800 Subject: [PATCH] Reduce flaky of BackgroundInstalledAppsPageProviderTest Bug: 355413226 Flag: EXEMPT test only Test: atest BackgroundInstalledAppsPageProviderTest Change-Id: Iede656b7cf34bac82718f0aff12ee28cbaa9d8f8 --- ...BackgroundInstalledAppsPageProviderTest.kt | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/tests/spa_unit/src/com/android/settings/spa/app/backgroundinstall/BackgroundInstalledAppsPageProviderTest.kt b/tests/spa_unit/src/com/android/settings/spa/app/backgroundinstall/BackgroundInstalledAppsPageProviderTest.kt index 6a6b91b2922..5ced84b0b5b 100644 --- a/tests/spa_unit/src/com/android/settings/spa/app/backgroundinstall/BackgroundInstalledAppsPageProviderTest.kt +++ b/tests/spa_unit/src/com/android/settings/spa/app/backgroundinstall/BackgroundInstalledAppsPageProviderTest.kt @@ -37,45 +37,32 @@ import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest -import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith -import org.mockito.Mock -import org.mockito.junit.MockitoJUnit -import org.mockito.junit.MockitoRule import org.mockito.kotlin.any import org.mockito.kotlin.argumentCaptor +import org.mockito.kotlin.doReturn import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import org.mockito.kotlin.spy import org.mockito.kotlin.whenever @RunWith(AndroidJUnit4::class) class BackgroundInstalledAppsPageProviderTest { - @get:Rule - val composeTestRule = createComposeRule() + @get:Rule val composeTestRule = createComposeRule() - @get:Rule - val mockito: MockitoRule = MockitoJUnit.rule() + private val mockPackageManager = mock() - private val context: Context = ApplicationProvider.getApplicationContext() + private val context: Context = + spy(ApplicationProvider.getApplicationContext()) { + on { packageManager } doReturn mockPackageManager + } - @Mock - private lateinit var mockContext: Context - - @Mock - private lateinit var mockPackageManager: PackageManager - - @Mock - private lateinit var mockBackgroundInstallControlService: IBackgroundInstallControlService - - private var packageInfoFlagsCaptor = argumentCaptor() + private val mockBackgroundInstallControlService = mock() private val fakeNavControllerWrapper = FakeNavControllerWrapper() - @Before - fun setup() { - whenever(mockContext.packageManager).thenReturn(mockPackageManager) - } @Test fun allAppListPageProvider_name() { assertThat(BackgroundInstalledAppsPageProvider.name) @@ -108,7 +95,7 @@ class BackgroundInstalledAppsPageProviderTest { setInjectEntry(false) - composeTestRule.onNodeWithText("0 apps").assertIsDisplayed() + composeTestRule.waitUntilExists(hasText("0 apps")) } @Test @@ -200,7 +187,8 @@ class BackgroundInstalledAppsPageProviderTest { @Test fun backgroundInstalledAppsWithGroupingListModel_transform() = runTest { - val listModel = BackgroundInstalledAppsWithGroupingListModel(mockContext) + val packageInfoFlagsCaptor = argumentCaptor() + val listModel = BackgroundInstalledAppsWithGroupingListModel(context) whenever(mockPackageManager.getPackageInfoAsUser( eq(TEST_PACKAGE_NAME), packageInfoFlagsCaptor.capture(), @@ -218,7 +206,7 @@ class BackgroundInstalledAppsPageProviderTest { @Test fun backgroundInstalledAppsWithGroupingListModel_filter() = runTest { - val listModel = BackgroundInstalledAppsWithGroupingListModel(mockContext) + val listModel = BackgroundInstalledAppsWithGroupingListModel(context) listModel.setBackgroundInstallControlService(mockBackgroundInstallControlService) whenever(mockBackgroundInstallControlService.getBackgroundInstalledPackages( PackageManager.MATCH_ALL.toLong(),