|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package androidx.media3.ui.compose.state |
| 18 | + |
| 19 | +import androidx.compose.runtime.LaunchedEffect |
| 20 | +import androidx.compose.ui.geometry.Size |
| 21 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 22 | +import androidx.media3.common.Player |
| 23 | +import androidx.media3.common.VideoSize |
| 24 | +import androidx.media3.ui.compose.utils.TestPlayer |
| 25 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 26 | +import com.google.common.truth.Truth.assertThat |
| 27 | +import org.junit.Rule |
| 28 | +import org.junit.Test |
| 29 | +import org.junit.runner.RunWith |
| 30 | + |
| 31 | +/** Unit test for [PresentationState]. */ |
| 32 | +@RunWith(AndroidJUnit4::class) |
| 33 | +class PresentationStateTest { |
| 34 | + |
| 35 | + @get:Rule val composeTestRule = createComposeRule() |
| 36 | + |
| 37 | + @Test |
| 38 | + fun playerInitialized_presentationStateInitialized() { |
| 39 | + val player = TestPlayer() |
| 40 | + player.playbackState = Player.STATE_IDLE |
| 41 | + |
| 42 | + lateinit var state: PresentationState |
| 43 | + composeTestRule.setContent { state = rememberPresentationState(player) } |
| 44 | + |
| 45 | + assertThat(state.coverSurface).isTrue() |
| 46 | + assertThat(state.keepContentOnReset).isFalse() |
| 47 | + assertThat(state.videoSizeDp).isEqualTo(null) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun playerChangesVideoSizeBeforeEventListenerRegisters_observeGetsTheLatestValues_uiInSync() { |
| 52 | + val player = TestPlayer() |
| 53 | + player.playbackState = Player.STATE_IDLE |
| 54 | + |
| 55 | + lateinit var state: PresentationState |
| 56 | + composeTestRule.setContent { |
| 57 | + // Schedule LaunchedEffect to update player state before PresentationState is created. |
| 58 | + // This update could end up being executed *before* PresentationState schedules the start |
| 59 | + // of event listening and we don't want to lose it. |
| 60 | + LaunchedEffect(player) { player.videoSize = VideoSize(480, 360) } |
| 61 | + state = rememberPresentationState(player) |
| 62 | + } |
| 63 | + |
| 64 | + assertThat(state.videoSizeDp).isEqualTo(Size(480f, 360f)) |
| 65 | + assertThat(state.coverSurface).isTrue() |
| 66 | + assertThat(state.keepContentOnReset).isFalse() |
| 67 | + } |
| 68 | +} |
0 commit comments