|
| 1 | +package org.schabi.newpipe.util |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.util.SparseArray |
| 5 | +import android.view.View |
| 6 | +import android.view.View.GONE |
| 7 | +import android.view.View.INVISIBLE |
| 8 | +import android.view.View.VISIBLE |
| 9 | +import android.widget.Spinner |
| 10 | +import androidx.test.core.app.ApplicationProvider |
| 11 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 12 | +import androidx.test.filters.MediumTest |
| 13 | +import androidx.test.internal.runner.junit4.statement.UiThreadStatement |
| 14 | +import org.junit.Assert |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import org.junit.runner.RunWith |
| 18 | +import org.schabi.newpipe.R |
| 19 | +import org.schabi.newpipe.extractor.MediaFormat |
| 20 | +import org.schabi.newpipe.extractor.stream.AudioStream |
| 21 | +import org.schabi.newpipe.extractor.stream.Stream |
| 22 | +import org.schabi.newpipe.extractor.stream.SubtitlesStream |
| 23 | +import org.schabi.newpipe.extractor.stream.VideoStream |
| 24 | + |
| 25 | +@MediumTest |
| 26 | +@RunWith(AndroidJUnit4::class) |
| 27 | +class StreamItemAdapterTest { |
| 28 | + private lateinit var context: Context |
| 29 | + private lateinit var spinner: Spinner |
| 30 | + |
| 31 | + @Before |
| 32 | + fun setUp() { |
| 33 | + context = ApplicationProvider.getApplicationContext() |
| 34 | + UiThreadStatement.runOnUiThread { |
| 35 | + spinner = Spinner(context) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun videoStreams_noSecondaryStream() { |
| 41 | + val adapter = StreamItemAdapter<VideoStream, AudioStream>( |
| 42 | + context, |
| 43 | + getVideoStreams(true, true, true, true), |
| 44 | + null |
| 45 | + ) |
| 46 | + |
| 47 | + spinner.adapter = adapter |
| 48 | + assertIconVisibility(spinner, 0, VISIBLE, VISIBLE) |
| 49 | + assertIconVisibility(spinner, 1, VISIBLE, VISIBLE) |
| 50 | + assertIconVisibility(spinner, 2, VISIBLE, VISIBLE) |
| 51 | + assertIconVisibility(spinner, 3, VISIBLE, VISIBLE) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun videoStreams_hasSecondaryStream() { |
| 56 | + val adapter = StreamItemAdapter( |
| 57 | + context, |
| 58 | + getVideoStreams(false, true, false, true), |
| 59 | + getAudioStreams(false, true, false, true) |
| 60 | + ) |
| 61 | + |
| 62 | + spinner.adapter = adapter |
| 63 | + assertIconVisibility(spinner, 0, GONE, GONE) |
| 64 | + assertIconVisibility(spinner, 1, GONE, GONE) |
| 65 | + assertIconVisibility(spinner, 2, GONE, GONE) |
| 66 | + assertIconVisibility(spinner, 3, GONE, GONE) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun videoStreams_Mixed() { |
| 71 | + val adapter = StreamItemAdapter( |
| 72 | + context, |
| 73 | + getVideoStreams(true, true, true, true, true, false, true, true), |
| 74 | + getAudioStreams(false, true, false, false, false, true, true, true) |
| 75 | + ) |
| 76 | + |
| 77 | + spinner.adapter = adapter |
| 78 | + assertIconVisibility(spinner, 0, VISIBLE, VISIBLE) |
| 79 | + assertIconVisibility(spinner, 1, GONE, INVISIBLE) |
| 80 | + assertIconVisibility(spinner, 2, VISIBLE, VISIBLE) |
| 81 | + assertIconVisibility(spinner, 3, VISIBLE, VISIBLE) |
| 82 | + assertIconVisibility(spinner, 4, VISIBLE, VISIBLE) |
| 83 | + assertIconVisibility(spinner, 5, GONE, INVISIBLE) |
| 84 | + assertIconVisibility(spinner, 6, GONE, INVISIBLE) |
| 85 | + assertIconVisibility(spinner, 7, GONE, INVISIBLE) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun subtitleStreams_noIcon() { |
| 90 | + val adapter = StreamItemAdapter<SubtitlesStream, Stream>( |
| 91 | + context, |
| 92 | + StreamItemAdapter.StreamSizeWrapper( |
| 93 | + (0 until 5).map { |
| 94 | + SubtitlesStream(MediaFormat.SRT, "pt-BR", "https://example.com", false) |
| 95 | + }, |
| 96 | + context |
| 97 | + ), |
| 98 | + null |
| 99 | + ) |
| 100 | + spinner.adapter = adapter |
| 101 | + for (i in 0 until spinner.count) { |
| 102 | + assertIconVisibility(spinner, i, GONE, GONE) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun audioStreams_noIcon() { |
| 108 | + val adapter = StreamItemAdapter<AudioStream, Stream>( |
| 109 | + context, |
| 110 | + StreamItemAdapter.StreamSizeWrapper( |
| 111 | + (0 until 5).map { AudioStream("https://example.com/$it", MediaFormat.OPUS, 192) }, |
| 112 | + context |
| 113 | + ), |
| 114 | + null |
| 115 | + ) |
| 116 | + spinner.adapter = adapter |
| 117 | + for (i in 0 until spinner.count) { |
| 118 | + assertIconVisibility(spinner, i, GONE, GONE) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return a list of video streams, in which their video only property mirrors the provided |
| 124 | + * [videoOnly] vararg. |
| 125 | + */ |
| 126 | + private fun getVideoStreams(vararg videoOnly: Boolean) = |
| 127 | + StreamItemAdapter.StreamSizeWrapper( |
| 128 | + videoOnly.map { |
| 129 | + VideoStream("https://example.com", MediaFormat.MPEG_4, "720p", it) |
| 130 | + }, |
| 131 | + context |
| 132 | + ) |
| 133 | + |
| 134 | + /** |
| 135 | + * @return a list of audio streams, containing valid and null elements mirroring the provided |
| 136 | + * [shouldBeValid] vararg. |
| 137 | + */ |
| 138 | + private fun getAudioStreams(vararg shouldBeValid: Boolean) = |
| 139 | + getSecondaryStreamsFromList( |
| 140 | + shouldBeValid.map { |
| 141 | + if (it) AudioStream("https://example.com", MediaFormat.OPUS, 192) |
| 142 | + else null |
| 143 | + } |
| 144 | + ) |
| 145 | + |
| 146 | + /** |
| 147 | + * Checks whether the item at [position] in the [spinner] has the correct icon visibility when |
| 148 | + * it is shown in normal mode (selected) and in dropdown mode (user is choosing one of a list). |
| 149 | + */ |
| 150 | + private fun assertIconVisibility( |
| 151 | + spinner: Spinner, |
| 152 | + position: Int, |
| 153 | + normalVisibility: Int, |
| 154 | + dropDownVisibility: Int |
| 155 | + ) { |
| 156 | + spinner.setSelection(position) |
| 157 | + spinner.adapter.getView(position, null, spinner).run { |
| 158 | + Assert.assertEquals( |
| 159 | + "normal visibility (pos=[$position]) is not correct", |
| 160 | + findViewById<View>(R.id.wo_sound_icon).visibility, |
| 161 | + normalVisibility, |
| 162 | + ) |
| 163 | + } |
| 164 | + spinner.adapter.getDropDownView(position, null, spinner).run { |
| 165 | + Assert.assertEquals( |
| 166 | + "drop down visibility (pos=[$position]) is not correct", |
| 167 | + findViewById<View>(R.id.wo_sound_icon).visibility, |
| 168 | + dropDownVisibility |
| 169 | + ) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Helper function that builds a secondary stream list. |
| 175 | + */ |
| 176 | + private fun <T : Stream> getSecondaryStreamsFromList(streams: List<T?>) = |
| 177 | + SparseArray<SecondaryStreamHelper<T>?>(streams.size).apply { |
| 178 | + streams.forEachIndexed { index, stream -> |
| 179 | + val secondaryStreamHelper: SecondaryStreamHelper<T>? = stream?.let { |
| 180 | + SecondaryStreamHelper( |
| 181 | + StreamItemAdapter.StreamSizeWrapper(streams, context), |
| 182 | + it |
| 183 | + ) |
| 184 | + } |
| 185 | + put(index, secondaryStreamHelper) |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments