Skip to content

Commit a89ddbe

Browse files
Simplify mocking THEOplayer version
1 parent 8c3901b commit a89ddbe

5 files changed

Lines changed: 13 additions & 34 deletions

File tree

ui/src/main/java/com/theoplayer/android/ui/Helper.kt

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,3 @@ fun rememberTrackLabel(
7171
): String = remember(key1 = track.id, key2 = track.uid) {
7272
constructLabel(track) ?: resources.getString(R.string.theoplayer_ui_track_unknown)
7373
}
74-
75-
/**
76-
* Memoize the most recent call.
77-
*/
78-
internal inline fun <P, R> memoizeLast(crossinline transform: (P) -> R): (P) -> R {
79-
return object : (P) -> R {
80-
private var lastCall: Pair<P, R>? = null
81-
82-
override fun invoke(input: P): R {
83-
val lastCall = this.lastCall
84-
return if (lastCall != null && lastCall.first == input) {
85-
lastCall.second
86-
} else {
87-
transform(input).also { output ->
88-
this.lastCall = input to output
89-
}
90-
}
91-
}
92-
}
93-
}

ui/src/main/java/com/theoplayer/android/ui/util/Compat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.theoplayer.android.api.player.track.texttrack.TextTrack
99
internal val TextTrack.captionChannelCompat: Int?
1010
get() {
1111
// TextTrack.getCaptionChannel was added in THEOplayer 10.13.0.
12-
return if (theoplayerVersion >= version1013) {
12+
return if (THEOplayerGlobalExt.version >= version1013) {
1313
TheoPlayer1013Impl.getTextTrackCaptionChannel(this)
1414
} else null
1515
}

ui/src/main/java/com/theoplayer/android/ui/util/TrackExts.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal fun constructLabel(
5252
): String? {
5353
val label: String? = if (
5454
(track is TextTrack) &&
55-
theoplayerVersion.major < 11 &&
55+
THEOplayerGlobalExt.version.major < 11 &&
5656
(isLabelCeaFormatted(track.label) || (track.label != null && track.language == track.label))
5757
) {
5858
// If we are below 11th major release

ui/src/main/java/com/theoplayer/android/ui/util/VersionUtil.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.theoplayer.android.ui.util
22

33
import com.theoplayer.android.api.THEOplayerGlobal
4-
import com.theoplayer.android.ui.memoizeLast
54

65
private const val VERSION_DELIMITER = '.'
76

@@ -55,10 +54,9 @@ internal data class Version(
5554
}
5655
}
5756

58-
private val getCachedTheoplayerVersion = memoizeLast(Version::parse)
59-
60-
/**
61-
* Returns the major version of THEOplayer.
62-
*/
63-
internal val theoplayerVersion: Version
64-
get() = getCachedTheoplayerVersion(THEOplayerGlobal.getVersion())
57+
internal object THEOplayerGlobalExt {
58+
/**
59+
* Returns the version of THEOplayer, as a [Version].
60+
*/
61+
val version: Version by lazy { Version.parse(THEOplayerGlobal.getVersion()) }
62+
}

ui/src/test/java/com/theoplayer/android/ui/util/TrackExtsTest.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.theoplayer.android.ui.util
22

3-
import com.theoplayer.android.api.THEOplayerGlobal
43
import com.theoplayer.android.api.player.track.Track
54
import com.theoplayer.android.api.player.track.texttrack.TextTrack
65
import com.theoplayer.android.api.player.track.texttrack.TextTrackType
6+
import io.mockk.clearMocks
77
import io.mockk.clearStaticMockk
88
import io.mockk.every
99
import io.mockk.mockk
10+
import io.mockk.mockkObject
1011
import io.mockk.mockkStatic
1112
import org.junit.After
1213
import org.junit.Assert
@@ -100,8 +101,8 @@ class TrackExtsTest {
100101

101102
@Before
102103
fun setUp() {
103-
mockkStatic(THEOplayerGlobal::class)
104-
every { THEOplayerGlobal.getVersion() } returns args.playerVersion
104+
mockkObject(THEOplayerGlobalExt)
105+
every { THEOplayerGlobalExt.version } returns Version.parse(args.playerVersion)
105106

106107
every { track.type } returns TextTrackType.CEA608
107108
every { track.label } returns args.label
@@ -114,7 +115,7 @@ class TrackExtsTest {
114115

115116
@After
116117
fun tearDown() {
117-
clearStaticMockk(THEOplayerGlobal::class)
118+
clearMocks(THEOplayerGlobalExt)
118119
clearStaticMockk(Track::localizedLanguageName)
119120
}
120121

0 commit comments

Comments
 (0)