-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Pager Indicators] Update Indicators to also be usable with Foundatio…
…n Pager (#1485) * Update pager indicators to support foundation PagerState * Change FoundationTabIndicatorTest name to TabIndicatorWithFoundationPagerTest. * Clean up formatting * Update API docs * Apply code review feedback * Apply code review feedback - second round
- Loading branch information
1 parent
1a7346f
commit c7306a3
Showing
4 changed files
with
444 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
...rc/androidTest/kotlin/com/google/accompanist/pager/TabIndicatorWithFoundationPagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* | ||
* Copyright 2022 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.ExperimentalFoundationApi | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.ScrollableTabRow | ||
import androidx.compose.material.Tab | ||
import androidx.compose.material.TabRowDefaults | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.test.getBoundsInRoot | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.height | ||
import androidx.compose.ui.unit.lerp | ||
import androidx.compose.ui.unit.times | ||
import androidx.compose.ui.unit.width | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import androidx.compose.foundation.pager.HorizontalPager as FoundationHorizontalPager | ||
import androidx.compose.foundation.pager.PagerState as FoundationPagerState | ||
import androidx.compose.foundation.pager.rememberPagerState as rememberFoundationPagerState | ||
|
||
@OptIn(ExperimentalFoundationApi::class) | ||
@RunWith(AndroidJUnit4::class) | ||
class TabIndicatorWithFoundationPagerTest { | ||
@get:Rule | ||
val rule = createComposeRule() | ||
|
||
private val IndicatorTag = "indicator" | ||
private val TabRowTag = "TabRow" | ||
|
||
@Test | ||
fun emptyPager() { | ||
rule.setContent { | ||
val pagerState = rememberFoundationPagerState() | ||
TabRow(pagerState, 0) | ||
} | ||
} | ||
|
||
@Test | ||
fun scrollOffsetIsPositive() { | ||
lateinit var pagerState: FoundationPagerState | ||
val pageCount = 4 | ||
rule.setContent { | ||
pagerState = rememberFoundationPagerState() | ||
Column { | ||
TabRow(pagerState, pageCount) | ||
FoundationHorizontalPager(pageCount = pageCount, state = pagerState) { | ||
Box(Modifier.fillMaxSize()) | ||
} | ||
} | ||
} | ||
|
||
rule.runOnIdle { | ||
runBlocking { pagerState.scrollToPage(1, 0.25f) } | ||
} | ||
|
||
val tab1Bounds = rule.onNodeWithTag("1").getBoundsInRoot() | ||
val tab2Bounds = rule.onNodeWithTag("2").getBoundsInRoot() | ||
val indicatorBounds = rule.onNodeWithTag(IndicatorTag).getBoundsInRoot() | ||
|
||
with(rule.density) { | ||
assertThat(indicatorBounds.left.roundToPx()) | ||
.isEqualTo(lerp(tab1Bounds.left, tab2Bounds.left, 0.25f).roundToPx()) | ||
assertThat(indicatorBounds.width.roundToPx()) | ||
.isEqualTo(lerp(tab1Bounds.width, tab2Bounds.width, 0.25f).roundToPx()) | ||
} | ||
} | ||
|
||
@Test | ||
fun scrollOffsetIsNegative() { | ||
lateinit var pagerState: FoundationPagerState | ||
val pageCount = 4 | ||
rule.setContent { | ||
pagerState = rememberFoundationPagerState() | ||
Column { | ||
TabRow(pagerState, pageCount) | ||
FoundationHorizontalPager(pageCount = pageCount, state = pagerState) { | ||
Box(Modifier.fillMaxSize()) | ||
} | ||
} | ||
} | ||
|
||
rule.runOnIdle { | ||
runBlocking { pagerState.scrollToPage(1, -0.25f) } | ||
} | ||
|
||
val tab1Bounds = rule.onNodeWithTag("1").getBoundsInRoot() | ||
val tab0Bounds = rule.onNodeWithTag("0").getBoundsInRoot() | ||
val indicatorBounds = rule.onNodeWithTag(IndicatorTag).getBoundsInRoot() | ||
|
||
with(rule.density) { | ||
assertThat(indicatorBounds.left.roundToPx()) | ||
.isEqualTo(lerp(tab1Bounds.left, tab0Bounds.left, 0.25f).roundToPx()) | ||
assertThat(indicatorBounds.width.roundToPx()) | ||
.isEqualTo(lerp(tab1Bounds.width, tab0Bounds.width, 0.25f).roundToPx()) | ||
} | ||
} | ||
|
||
@Test | ||
fun indicatorIsAtBottom() { | ||
lateinit var pagerState: FoundationPagerState | ||
val pageCount = 4 | ||
rule.setContent { | ||
pagerState = rememberFoundationPagerState() | ||
Column { | ||
TabRow(pagerState, pageCount) | ||
FoundationHorizontalPager(pageCount = pageCount, state = pagerState) { | ||
Box(Modifier.fillMaxSize()) | ||
} | ||
} | ||
} | ||
|
||
rule.runOnIdle { | ||
runBlocking { pagerState.scrollToPage(1, 0.25f) } | ||
} | ||
|
||
val tabRowBounds = rule.onNodeWithTag(TabRowTag).getBoundsInRoot() | ||
|
||
val indicatorBounds = rule.onNodeWithTag(IndicatorTag).getBoundsInRoot() | ||
|
||
with(rule.density) { | ||
assertThat(indicatorBounds.height.roundToPx()).isEqualTo(2.dp.roundToPx()) | ||
assertThat(indicatorBounds.bottom).isEqualTo(tabRowBounds.bottom) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalPagerApi::class) | ||
@Composable | ||
private fun TabRow(pagerState: FoundationPagerState, pageCount: Int) { | ||
ScrollableTabRow( | ||
selectedTabIndex = pagerState.currentPage, | ||
indicator = { tabPositions -> | ||
TabRowDefaults.Indicator( | ||
Modifier | ||
.pagerTabIndicatorOffset(pagerState, tabPositions) | ||
.testTag(IndicatorTag), | ||
height = 2.dp | ||
) | ||
}, | ||
modifier = Modifier.testTag(TabRowTag) | ||
) { | ||
// Add tabs for all of our pages | ||
(0 until pageCount).forEach { index -> | ||
Tab( | ||
text = { Text("Tab $index", Modifier.padding(horizontal = index * 5.dp)) }, | ||
selected = pagerState.currentPage == index, | ||
modifier = Modifier.testTag("$index"), | ||
onClick = {} | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.