Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pager] Re-enable animateScrollToPage() test #373

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -159,6 +160,8 @@ class HorizontalPagerTest(
): PagerState {
val pagerState = PagerState(pageCount = pageCount)
composeTestRule.setContent(layoutDirection) {
applierScope = rememberCoroutineScope()

HorizontalPager(
state = pagerState,
itemSpacing = itemSpacingDp.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down