From db644bfe8f87965231daf1fe50d739f881c10a57 Mon Sep 17 00:00:00 2001 From: Chris Banes Date: Tue, 27 Apr 2021 10:01:08 +0100 Subject: [PATCH] [Pager] Re-enable animateScrollToPage() test --- .../accompanist/pager/HorizontalPagerTest.kt | 3 +++ .../com/google/accompanist/pager/PagerTest.kt | 19 ++++++++++++++++--- .../accompanist/pager/VerticalPagerTest.kt | 3 +++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pager/src/androidTest/java/com/google/accompanist/pager/HorizontalPagerTest.kt b/pager/src/androidTest/java/com/google/accompanist/pager/HorizontalPagerTest.kt index a229fd224..fcf5fc4ac 100644 --- a/pager/src/androidTest/java/com/google/accompanist/pager/HorizontalPagerTest.kt +++ b/pager/src/androidTest/java/com/google/accompanist/pager/HorizontalPagerTest.kt @@ -22,6 +22,7 @@ import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.text.BasicText +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag @@ -159,6 +160,8 @@ class HorizontalPagerTest( ): PagerState { val pagerState = PagerState(pageCount = pageCount) composeTestRule.setContent(layoutDirection) { + applierScope = rememberCoroutineScope() + HorizontalPager( state = pagerState, itemSpacing = itemSpacingDp.dp, diff --git a/pager/src/androidTest/java/com/google/accompanist/pager/PagerTest.kt b/pager/src/androidTest/java/com/google/accompanist/pager/PagerTest.kt index 25180cc43..a2658b294 100644 --- a/pager/src/androidTest/java/com/google/accompanist/pager/PagerTest.kt +++ b/pager/src/androidTest/java/com/google/accompanist/pager/PagerTest.kt @@ -25,9 +25,11 @@ import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onParent import androidx.compose.ui.test.performScrollTo import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.produceIn +import kotlinx.coroutines.withContext import org.junit.Ignore import org.junit.Rule import org.junit.Test @@ -49,6 +51,12 @@ abstract class PagerTest { @get:Rule val composeTestRule = createComposeRule() + /** + * This is a workaround for https://issuetracker.google.com/issues/179492185. + * Ideally we would have a way to get the applier scope from the rule + */ + protected lateinit var applierScope: CoroutineScope + @Test fun layout() { setPagerContent(pageCount = 10) @@ -196,15 +204,20 @@ abstract class PagerTest { @OptIn(ExperimentalCoroutinesApi::class) @Test - @Ignore("https://issuetracker.google.com/185228750") fun animateScrollToPage() = suspendTest { val pagerState = setPagerContent(pageCount = 10) - pagerState.animateScrollToPage(3) + withContext(applierScope.coroutineContext) { + pagerState.animateScrollToPage(3) + } + composeTestRule.awaitIdle() assertThat(pagerState.currentPage).isEqualTo(3) assertPagerLayout(3) - pagerState.animateScrollToPage(0) + withContext(applierScope.coroutineContext) { + pagerState.animateScrollToPage(0) + } + composeTestRule.awaitIdle() assertThat(pagerState.currentPage).isEqualTo(0) assertPagerLayout(0) } diff --git a/pager/src/androidTest/java/com/google/accompanist/pager/VerticalPagerTest.kt b/pager/src/androidTest/java/com/google/accompanist/pager/VerticalPagerTest.kt index 8576139af..383d30c32 100644 --- a/pager/src/androidTest/java/com/google/accompanist/pager/VerticalPagerTest.kt +++ b/pager/src/androidTest/java/com/google/accompanist/pager/VerticalPagerTest.kt @@ -22,6 +22,7 @@ import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.text.BasicText +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag @@ -122,6 +123,8 @@ class VerticalPagerTest( val pagerState = PagerState(pageCount = pageCount) // Stick to LTR for vertical tests composeTestRule.setContent(LayoutDirection.Ltr) { + applierScope = rememberCoroutineScope() + VerticalPager( state = pagerState, offscreenLimit = offscreenLimit,