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

Add PositionIndicator test #36

Merged
merged 2 commits into from
Mar 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion audio-ui/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package com.google.android.horologist.audioui {

public final class VolumePositionIndicatorKt {
method @androidx.compose.runtime.Composable public static void VolumePositionIndicator(kotlin.jvm.functions.Function0<com.google.android.horologist.audio.VolumeState> volumeState, optional boolean autoHide);
method @androidx.compose.runtime.Composable public static void VolumePositionIndicator(optional androidx.compose.ui.Modifier modifier, kotlin.jvm.functions.Function0<com.google.android.horologist.audio.VolumeState> volumeState, optional boolean autoHide);
}

public final class VolumeScreenKt {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.android.horologist.audioui

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.printToLog
import com.google.android.horologist.audio.VolumeState
import org.junit.Rule
import org.junit.Test

class VolumePositionIndicatorTest {
@get:Rule
val composeTestRule = createComposeRule().apply {
mainClock.autoAdvance = false
}

private var volumeState by mutableStateOf(
VolumeState(
current = 50,
min = 0,
max = 100,
isMute = false
)
)

@Test
fun testNoAutoHide() {
composeTestRule.setContent {
VolumePositionIndicator(
modifier = Modifier.testTag(TEST_TAG),
volumeState = { volumeState },
autoHide = false
)
}

val positionIndicator = composeTestRule.onNodeWithTag(TEST_TAG)

positionIndicator.assertIsDisplayed()
}

@Test
fun testAutoHide() {
composeTestRule.setContent {
VolumePositionIndicator(
modifier = Modifier.testTag(TEST_TAG),
volumeState = { volumeState },
autoHide = true
)
}

val positionIndicator = composeTestRule.onNodeWithTag(TEST_TAG)

positionIndicator.assertDoesNotExist()

volumeState = volumeState.copy(current = 51)

composeTestRule.mainClock.advanceTimeByFrame()
composeTestRule.mainClock.advanceTimeBy(500L)

composeTestRule.onRoot(useUnmergedTree = true).printToLog("testAutoHide")

positionIndicator.assertIsDisplayed()
}
}

const val TEST_TAG = "test-item"
3 changes: 3 additions & 0 deletions audio-ui/src/androidTest/resources/robolectric.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Pin SDK to 30 since Robolectric does not currently support API 31:
# https://github.com/robolectric/robolectric/issues/6635
sdk=30
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import kotlinx.coroutines.delay
*/
@Composable
public fun VolumePositionIndicator(
modifier: Modifier = Modifier,
volumeState: () -> VolumeState,
autoHide: Boolean = true
) {
Expand Down Expand Up @@ -71,9 +72,9 @@ public fun VolumePositionIndicator(
exit = fadeOut()
) {
PositionIndicator(
modifier = modifier,
value = {
val vs = volumeState()
vs.current.toFloat()
volumeState().current.toFloat()
},
range = volumeState().min.toFloat().rangeTo(
volumeState().max.toFloat()
Expand Down