|
| 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.ui.test.junit4.createComposeRule |
| 20 | +import androidx.core.net.toUri |
| 21 | +import androidx.media3.common.MediaItem |
| 22 | +import androidx.media3.common.Player |
| 23 | +import androidx.media3.common.SimpleBasePlayer.MediaItemData |
| 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 [MetadataState]. */ |
| 32 | +@RunWith(AndroidJUnit4::class) |
| 33 | +class MetadataStateTest { |
| 34 | + |
| 35 | + @get:Rule |
| 36 | + val composeTestRule = createComposeRule() |
| 37 | + |
| 38 | + @Test |
| 39 | + fun uri_emptyPlaylist_returnsNull() { |
| 40 | + val player = TestPlayer( |
| 41 | + playbackState = Player.STATE_IDLE, |
| 42 | + playlist = emptyList(), |
| 43 | + ) |
| 44 | + |
| 45 | + lateinit var state: MetadataState |
| 46 | + composeTestRule.setContent { state = rememberMetadataState(player) } |
| 47 | + |
| 48 | + assertThat(state.uri).isNull() |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun uri_singleItemWithoutUri_returnsNull() { |
| 53 | + val player = TestPlayer( |
| 54 | + playlist = listOf( |
| 55 | + MediaItemData.Builder("uid_1").build(), |
| 56 | + ), |
| 57 | + ) |
| 58 | + |
| 59 | + lateinit var state: MetadataState |
| 60 | + composeTestRule.setContent { state = rememberMetadataState(player) } |
| 61 | + |
| 62 | + assertThat(state.uri).isNull() |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun uri_singleItemWithUri_returnsTheUri() { |
| 67 | + val uri = "https://storage.googleapis.com/wvmedia/clear/h264/tears/tears.mpd".toUri() |
| 68 | + val player = TestPlayer( |
| 69 | + playlist = listOf( |
| 70 | + MediaItemData.Builder("uid_1").setMediaItem(MediaItem.fromUri(uri)).build(), |
| 71 | + ), |
| 72 | + ) |
| 73 | + |
| 74 | + lateinit var state: MetadataState |
| 75 | + composeTestRule.setContent { state = rememberMetadataState(player) } |
| 76 | + |
| 77 | + assertThat(state.uri).isEqualTo(uri) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun uri_transitionBetweenItems_returnsUpdatedUri() { |
| 82 | + val uri1 = "https://storage.googleapis.com/wvmedia/clear/h264/tears/tears.mpd".toUri() |
| 83 | + val uri2 = |
| 84 | + "https://storage.googleapis.com/exoplayer-test-media-1/mp4/dizzy-with-tx3g.mp4".toUri() |
| 85 | + val player = TestPlayer( |
| 86 | + playlist = listOf( |
| 87 | + MediaItemData.Builder("uid_1").setMediaItem(MediaItem.fromUri(uri1)).build(), |
| 88 | + MediaItemData.Builder("uid_2").build(), |
| 89 | + MediaItemData.Builder("uid_3").setMediaItem(MediaItem.fromUri(uri2)).build(), |
| 90 | + ), |
| 91 | + ) |
| 92 | + |
| 93 | + lateinit var state: MetadataState |
| 94 | + composeTestRule.setContent { state = rememberMetadataState(player) } |
| 95 | + |
| 96 | + assertThat(state.uri).isEqualTo(uri1) |
| 97 | + |
| 98 | + player.seekToNext() |
| 99 | + composeTestRule.waitForIdle() |
| 100 | + |
| 101 | + assertThat(state.uri).isNull() |
| 102 | + |
| 103 | + player.seekToNext() |
| 104 | + composeTestRule.waitForIdle() |
| 105 | + |
| 106 | + assertThat(state.uri).isEqualTo(uri2) |
| 107 | + } |
| 108 | +} |
0 commit comments