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] Add VerticalPager #254

Merged
merged 8 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions pager/api/pager.api
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public abstract interface annotation class com/google/accompanist/pager/Experime

public final class com/google/accompanist/pager/Pager {
public static final fun HorizontalPager (Lcom/google/accompanist/pager/PagerState;Landroidx/compose/ui/Modifier;ZILandroidx/compose/animation/core/DecayAnimationSpec;Landroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
public static final fun VerticalPager (Lcom/google/accompanist/pager/PagerState;Landroidx/compose/ui/Modifier;ZILandroidx/compose/animation/core/DecayAnimationSpec;Landroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
}

public abstract interface class com/google/accompanist/pager/PagerScope : androidx/compose/foundation/layout/BoxScope {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.accompanist.pager

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.assertHeightIsAtLeast
import androidx.compose.ui.test.assertLeftPositionInRootIsEqualTo
import androidx.compose.ui.test.assertTopPositionInRootIsEqualTo
import androidx.compose.ui.test.assertWidthIsEqualTo
import androidx.compose.ui.test.getUnclippedBoundsInRoot
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.height
import androidx.compose.ui.unit.width
import androidx.test.filters.LargeTest
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@OptIn(ExperimentalPagerApi::class) // Pager is currently experimental
@LargeTest
@RunWith(Parameterized::class)
class HorizontalPagerTest(
private val itemWidthFraction: Float,
offscreenLimit: Int,
layoutDirection: LayoutDirection,
) : PagerTest(
offscreenLimit = offscreenLimit,
layoutDirection = layoutDirection,
) {
companion object {
@JvmStatic
@Parameterized.Parameters
fun data(): Collection<Array<Any>> = listOf(
// itemWidthFraction, offscreenLimit, layoutDirection

// Test typical full-width items
arrayOf(1f, 2, LayoutDirection.Ltr),
arrayOf(1f, 2, LayoutDirection.Rtl),

// Test an increased offscreenLimit
arrayOf(1f, 4, LayoutDirection.Ltr),
arrayOf(1f, 4, LayoutDirection.Rtl),

// Test items with 80% widths
arrayOf(0.8f, 2, LayoutDirection.Ltr),
arrayOf(0.8f, 2, LayoutDirection.Rtl),
)
}

override fun SemanticsNodeInteraction.swipeAcrossCenter(
velocity: Float,
distancePercentage: Float
): SemanticsNodeInteraction = swipeAcrossCenterWithVelocity(
distancePercentageX = when (layoutDirection) {
LayoutDirection.Rtl -> -distancePercentage
else -> distancePercentage
},
velocity = velocity,
)

override fun SemanticsNodeInteraction.assertLaidOutItemPosition(
page: Int,
currentPage: Int,
): SemanticsNodeInteraction {
val rootBounds = composeTestRule.onRoot().getUnclippedBoundsInRoot()
val expectedItemSize = rootBounds.width * itemWidthFraction

// The expected coordinates. This uses the implicit fact that Pager
// centers items horizontally, and that we're using items with an aspect ratio of 1:1
val expectedTop = (rootBounds.height - expectedItemSize) / 2
val expectedFirstItemLeft = (rootBounds.width - expectedItemSize) / 2

return assertWidthIsEqualTo(expectedItemSize)
.assertHeightIsAtLeast(expectedItemSize)
.assertTopPositionInRootIsEqualTo(expectedTop)
.run {
if (layoutDirection == LayoutDirection.Ltr) {
assertLeftPositionInRootIsEqualTo(
expectedFirstItemLeft + (expectedItemSize * (page - currentPage))
)
} else { // layoutDirection = RTL
assertLeftPositionInRootIsEqualTo(
expectedFirstItemLeft - (expectedItemSize * (page - currentPage))
)
}
}
}

override fun setPagerContent(
layoutDirection: LayoutDirection,
pageCount: Int,
offscreenLimit: Int,
): PagerState {
val pagerState = PagerState(pageCount = pageCount)
composeTestRule.setContent(layoutDirection) {
HorizontalPager(
state = pagerState,
offscreenLimit = offscreenLimit,
modifier = Modifier.fillMaxSize()
) { page ->
Box(
modifier = Modifier
.fillMaxWidth(itemWidthFraction)
.aspectRatio(1f)
.background(randomColor())
.testTag(page.toString())
) {
BasicText(
text = page.toString(),
modifier = Modifier.align(Alignment.Center)
)
}
}
}
return pagerState
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.junit.runners.JUnit4

@OptIn(ExperimentalPagerApi::class) // Pager is currently experimental
@RunWith(JUnit4::class)
class PagerUnitTest {
class PagerStateUnitTest {
@get:Rule
val composeTestRule = createComposeRule()

Expand Down
Loading