@simba-dev/react-native-media-player 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +317 -560
  2. package/android/src/main/cpp/main.cpp +117 -17
  3. package/android/src/main/java/com/simba/player/PipManager.kt +13 -0
  4. package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +177 -6
  5. package/android/src/main/java/com/simba/player/mpv/PipAutoEnterPolicy.kt +54 -0
  6. package/package.json +11 -2
  7. package/android/src/test/java/com/simba/player/PipActionReceiverTest.kt +0 -123
  8. package/android/src/test/java/com/simba/player/PipManagerTest.kt +0 -281
  9. package/android/src/test/java/com/simba/player/TestApplication.kt +0 -26
  10. package/android/src/test/java/com/simba/player/mpv/MpvBridgeModuleNullInstanceTest.kt +0 -117
  11. package/android/src/test/java/com/simba/player/mpv/MpvBridgeModuleTest.kt +0 -212
  12. package/android/src/test/java/com/simba/player/mpv/MpvRenderViewRegistryTest.kt +0 -120
  13. package/android/src/test/java/com/simba/player/mpv/MpvRenderViewTest.kt +0 -249
  14. package/scripts/override_react_methods.js +0 -184
  15. package/scripts/override_react_methods.py +0 -150
  16. package/src/bridge/__tests__/MpvPlayerModule.test.ts +0 -163
  17. package/src/bridge/__tests__/debugMode.test.ts +0 -138
  18. package/src/bridge/__tests__/errorContract.test.ts +0 -175
  19. package/src/bridge/__tests__/loadAndPlay.startPosition.test.tsx +0 -142
  20. package/src/components/__tests__/PlayerProvider.performance.test.tsx +0 -242
  21. package/src/components/__tests__/PlayerProvider.progressContext.test.tsx +0 -194
  22. package/src/components/__tests__/PlayerProvider.test.tsx +0 -558
  23. package/src/components/__tests__/PlayerRoot.test.tsx +0 -71
  24. package/src/components/__tests__/SimbaVideo.test.tsx +0 -164
  25. package/src/hooks/__tests__/resolveStreamType.test.ts +0 -159
  26. package/src/hooks/__tests__/useLaunchPlayback.test.tsx +0 -278
  27. package/src/hooks/__tests__/useResumeThumbnail.test.ts +0 -154
  28. package/src/stores/__tests__/playerQueueSelectionStore.test.ts +0 -73
  29. package/src/stores/__tests__/playerQueueStore.test.ts +0 -171
  30. package/src/types/__tests__/config.test.ts +0 -188
  31. package/src/types/__tests__/player.pipAnswer.test.tsx +0 -87
  32. package/src/types/__tests__/player.pipArity.test.tsx +0 -74
  33. package/src/types/__tests__/player.test.tsx +0 -1365
  34. package/src/types/__tests__/playerLoading.test.ts +0 -170
@@ -1,212 +0,0 @@
1
- package com.simba.player.mpv
2
-
3
- import android.os.Build
4
- import androidx.test.ext.junit.runners.AndroidJUnit4
5
- import com.facebook.react.bridge.ReactApplicationContext
6
- import com.facebook.react.modules.core.DeviceEventManagerModule
7
- import com.simba.player.TestApplication
8
- import org.junit.After
9
- import org.junit.Assert.assertEquals
10
- import org.junit.Assert.assertNull
11
- import org.junit.Before
12
- import org.junit.Ignore
13
- import org.junit.Test
14
- import org.junit.runner.RunWith
15
- import org.mockito.kotlin.any
16
- import org.mockito.kotlin.eq
17
- import org.mockito.kotlin.mock
18
- import org.mockito.kotlin.verify
19
- import org.mockito.kotlin.whenever
20
- import org.robolectric.annotation.Config
21
- import org.robolectric.shadows.ShadowLog
22
-
23
- /**
24
- * Unit tests for the companion `onPictureInPictureModeChanged` method on
25
- * [MpvBridgeModule]. This is the single most error-prone path in the PiP
26
- * lifecycle because it's called from MainActivity before the module
27
- * instance may have been created (race condition during cold-start PiP).
28
- *
29
- * The method must:
30
- * 1. Return silently when `instance` is null (the V12 spec calls this
31
- * out explicitly in §Phase 33.4 — cold-start race).
32
- * 2. Emit `onPipModeChanged` with `{ isInPip: true|false }` when the
33
- * instance is set.
34
- * 3. Never propagate exceptions to the caller (Activity onPictureInPictureModeChanged
35
- * would otherwise crash).
36
- *
37
- * Test strategy:
38
- * - We use reflection to read/write the private `instance` companion
39
- * field. This is the only way to drive the path because the
40
- * constructor requires a real `ReactApplicationContext` (final class
41
- * in RN; not mockable with vanilla Mockito without mockito-inline).
42
- * - For the emit-happy-path test we substitute `instance` with a
43
- * `mock<ReactApplicationContext>()` and stub `getJSModule` to return
44
- * a mocked emitter; we then verify `emit("onPipModeChanged", ...)` was
45
- * called with `isInPip = true/false` matching the input.
46
- *
47
- * Why Robolectric + AndroidJUnit4:
48
- * - `ShadowLog.stream = System.out` is a Robolectric-provided helper
49
- * that lets us inspect logs in tests. The companion method logs
50
- * before and after emit, which makes assertions about its path
51
- * easier.
52
- * - `TestApplication` is wired via `@Config(application=...)` so we
53
- * can grab a Context from `ApplicationProvider`.
54
- *
55
- * **Sandboxed CI runner limitation:** Robolectric downloads the
56
- * `android-all-instrumented` runtime jar from Maven Central at first
57
- * run. Sandboxed environments that block writes to `~/.m2/repository/`
58
- * (notably the TRAE sandbox) cannot populate the cache. The whole
59
- * class is `@Ignore`'d in that case; CI runners with full disk access
60
- * can run the suite.
61
- *
62
- * The pure-Kotlin null-instance tests live in
63
- * [MpvBridgeModuleNullInstanceTest] (no Android dependency, no
64
- * Robolectric) — those run on any environment.
65
- */
66
- @Ignore("Robolectric requires downloading android-all-instrumented jars at first run; sandboxed CI runners block writes to ~/.m2/repository/. Run on a non-sandboxed workstation or full CI runner to enable.")
67
- @RunWith(AndroidJUnit4::class)
68
- @Config(sdk = [Build.VERSION_CODES.TIRAMISU], application = TestApplication::class)
69
- class MpvBridgeModuleTest {
70
-
71
- /**
72
- * Reflection helper for the private `instance: ReactApplicationContext?`
73
- * companion field. The Kotlin compiler hoists such fields into the
74
- * outer class as `private static volatile` — verified by inspecting
75
- * the AAR's bytecode: `instance` lives on `MpvBridgeModule`, not on
76
- * its Companion object. Pass `null` for the reflection instance
77
- * parameter since the field is static.
78
- */
79
- private val instanceField = run {
80
- MpvBridgeModule::class.java
81
- .getDeclaredField("instance")
82
- .apply { isAccessible = true }
83
- }
84
-
85
- @Before
86
- fun setUp() {
87
- // Stream Robolectric shadow logs to stdout so the companion's
88
- // log calls (Log.i / Log.w under TAG "MpvBridgeModule") show up
89
- // in test output if a test fails.
90
- ShadowLog.stream = System.out
91
- // Reset the companion's `instance` to null between tests so
92
- // we have a clean slate (each test sets it explicitly).
93
- instanceField.set(null, null)
94
- }
95
-
96
- @After
97
- fun tearDown() {
98
- // Be a good citizen — clear the companion field so leftover state
99
- // doesn't leak into other test classes.
100
- instanceField.set(null, null)
101
- }
102
-
103
- // ── Spec §Phase 33.4: null instance is a no-op ───────────────────────
104
-
105
- @Test
106
- fun onPictureInPictureModeChanged_nullInstance_returnsWithoutCrash() {
107
- // Cold-start race: MainActivity.onPictureInPictureModeChanged
108
- // fires before MpvBridgeModule's `init { instance = ... }` has
109
- // run. The companion must NOT propagate the resulting null
110
- // deref to the Activity's override — it must log + return.
111
- assertNull("instance must start as null", instanceField.get(null))
112
- // The call would throw NPE if `instance` was used directly. The
113
- // contract says it must return cleanly.
114
- MpvBridgeModule.onPictureInPictureModeChanged(isInPictureInPictureMode = true)
115
- // Verify instance is still null (the call didn't accidentally
116
- // mutate it).
117
- assertNull(
118
- "instance must remain null after a null-instance call",
119
- instanceField.get(null),
120
- )
121
- }
122
-
123
- @Test
124
- fun onPictureInPictureModeChanged_nullInstance_withFalseArg_alsoReturns() {
125
- // Both true and false paths must handle null instance — the
126
- // method body doesn't branch on the boolean before the null
127
- // check, so this is a quick smoke test that the same null-
128
- // guard applies for both cases.
129
- MpvBridgeModule.onPictureInPictureModeChanged(isInPictureInPictureMode = false)
130
- assertNull(instanceField.get(null))
131
- }
132
-
133
- // ── Happy-path emit ──────────────────────────────────────────────────
134
-
135
- @Test
136
- fun onPictureInPictureModeChanged_withInstance_emitsOnPipModeChanged() {
137
- // Set `instance` to a mocked ReactApplicationContext, then
138
- // verify the emit() call lands with the expected event name +
139
- // payload. The mock setup uses mockito-kotlin (mockk-style).
140
- val mockReactContext = mock<ReactApplicationContext>()
141
- val mockEmitter = mock<DeviceEventManagerModule.RCTDeviceEventEmitter>()
142
- whenever(
143
- mockReactContext.getJSModule(
144
- DeviceEventManagerModule.RCTDeviceEventEmitter::class.java,
145
- ),
146
- ).thenReturn(mockEmitter)
147
-
148
- instanceField.set(null, mockReactContext)
149
-
150
- MpvBridgeModule.onPictureInPictureModeChanged(isInPictureInPictureMode = true)
151
-
152
- // Verify the JS event was emitted with the expected name. The
153
- // payload is an Arguments.createMap() output; we check the name
154
- // (string-typed) and confirm emit was called once.
155
- verify(mockEmitter).emit(eq("onPipModeChanged"), any())
156
- }
157
-
158
- @Test
159
- fun onPictureInPictureModeChanged_withInstance_andFalseArg_emitsOnPipModeChanged() {
160
- // Same path but for isInPip=false — verify both branches reach emit.
161
- val mockReactContext = mock<ReactApplicationContext>()
162
- val mockEmitter = mock<DeviceEventManagerModule.RCTDeviceEventEmitter>()
163
- whenever(
164
- mockReactContext.getJSModule(
165
- DeviceEventManagerModule.RCTDeviceEventEmitter::class.java,
166
- ),
167
- ).thenReturn(mockEmitter)
168
-
169
- instanceField.set(null, mockReactContext)
170
-
171
- MpvBridgeModule.onPictureInPictureModeChanged(isInPictureInPictureMode = false)
172
-
173
- verify(mockEmitter).emit(eq("onPipModeChanged"), any())
174
- }
175
-
176
- @Test
177
- fun onPictureInPictureModeChanged_withInstance_andEmitterThrows_doesNotPropagate() {
178
- // Defensive guard: if the JS bridge is gone (e.g. JS bundle
179
- // unloaded mid-PiP), the emit() call can throw. The companion
180
- // wraps the emit in try/catch and must NOT propagate the
181
- // exception to Activity.onPictureInPictureModeChanged.
182
- val mockReactContext = mock<ReactApplicationContext>()
183
- val mockEmitter = mock<DeviceEventManagerModule.RCTDeviceEventEmitter>()
184
- whenever(
185
- mockReactContext.getJSModule(
186
- DeviceEventManagerModule.RCTDeviceEventEmitter::class.java,
187
- ),
188
- ).thenReturn(mockEmitter)
189
- // Make emit() throw an arbitrary RuntimeException to simulate
190
- // a torn-down bridge.
191
- whenever(mockEmitter.emit(any<String>(), any())).thenThrow(
192
- RuntimeException("bridge torn down"),
193
- )
194
-
195
- instanceField.set(null, mockReactContext)
196
-
197
- // If the companion propagated the exception, this test would
198
- // fail with the RuntimeException above.
199
- MpvBridgeModule.onPictureInPictureModeChanged(isInPictureInPictureMode = true)
200
- }
201
-
202
- // ── Constants / sanity ───────────────────────────────────────────────
203
-
204
- @Test
205
- fun name_isStable_acrossReleases() {
206
- // The module name is part of the JS <-> Native bridge contract.
207
- // JS calls `NativeModules.MpvPlayerModule`; if the Kotlin name
208
- // changes, the bridge silently breaks. Lock the value.
209
- assertEquals("MpvPlayerModule", MpvBridgeModule.NAME)
210
- }
211
-
212
- }
@@ -1,120 +0,0 @@
1
- package com.simba.player.mpv
2
-
3
- import org.junit.After
4
- import org.junit.Assert.assertFalse
5
- import org.junit.Assert.assertNull
6
- import org.junit.Assert.assertSame
7
- import org.junit.Assert.assertTrue
8
- import org.junit.Before
9
- import org.junit.Test
10
- import org.mockito.kotlin.mock
11
-
12
- /**
13
- * Pure-JUnit unit tests for [MpvRenderViewRegistry] — the registry that
14
- * answers "is there a live picture view?".
15
- *
16
- * These tests do NOT require the Android runtime (no Robolectric). The
17
- * registry only ever stores [MpvRenderView] instances weakly and compares
18
- * them by identity, so a Mockito mock stands in for a real view: the mock
19
- * is never asked to behave like a View, it only has to be an object the
20
- * registry can hold, compare and withdraw.
21
- *
22
- * ## Why this registry exists at all
23
- *
24
- * `PlayerSurface` was the old registry, written by `PlayerSurface.mount()`.
25
- * The V22 architecture never mounts a surface — the picture is a child of
26
- * the React tree — so `PlayerSurface.current()` answered `null` forever,
27
- * and `isSurfaceMounted()` answered `false` forever. A consumer that had
28
- * a `<SimbaVideo />` mounted was told there was nowhere to put a video.
29
- *
30
- * The registry is written by [MpvRenderViewManager], the thing that
31
- * actually creates the views, so it is true by construction.
32
- *
33
- * ## Why a separate file
34
- *
35
- * Keeps the suite split into "always-runs" (this file, plain JUnit) and
36
- * "needs-Robolectric" (the rest). CI runners without Android sandbox
37
- * restrictions can run the full suite; a sandboxed environment can run
38
- * just this file for the part that matters most.
39
- */
40
- class MpvRenderViewRegistryTest {
41
-
42
- /** Every mock this test registered, so @After can withdraw all of them. */
43
- private val registered = mutableListOf<MpvRenderView>()
44
-
45
- @Before
46
- fun setUp() {
47
- // The registry is a process-wide singleton, so a test that leaves a
48
- // registration behind would make the NEXT test lie. Start clean.
49
- registered.forEach { MpvRenderViewRegistry.unregister(it) }
50
- registered.clear()
51
- }
52
-
53
- @After
54
- fun tearDown() {
55
- registered.forEach { MpvRenderViewRegistry.unregister(it) }
56
- registered.clear()
57
- }
58
-
59
- private fun newPicture(): MpvRenderView {
60
- val view = mock<MpvRenderView>()
61
- registered.add(view)
62
- return view
63
- }
64
-
65
- @Test
66
- fun `with no picture registered there is no live view`() {
67
- assertFalse(MpvRenderViewRegistry.hasLiveView())
68
- assertNull(MpvRenderViewRegistry.current())
69
- }
70
-
71
- @Test
72
- fun `a registered picture is the live view`() {
73
- val view = newPicture()
74
- MpvRenderViewRegistry.register(view)
75
-
76
- assertTrue(MpvRenderViewRegistry.hasLiveView())
77
- assertSame(view, MpvRenderViewRegistry.current())
78
- }
79
-
80
- @Test
81
- fun `withdrawing the picture takes the live view away`() {
82
- val view = newPicture()
83
- MpvRenderViewRegistry.register(view)
84
- assertTrue(MpvRenderViewRegistry.hasLiveView())
85
-
86
- MpvRenderViewRegistry.unregister(view)
87
-
88
- assertFalse(MpvRenderViewRegistry.hasLiveView())
89
- assertNull(MpvRenderViewRegistry.current())
90
- }
91
-
92
- @Test
93
- fun `withdrawing a view that is not registered leaves the live one alone`() {
94
- // Identity-safe: a stale teardown must not unregister its
95
- // replacement. This is the same rule `PlayerSurface.detach` has.
96
- val live = newPicture()
97
- val stranger = newPicture()
98
- MpvRenderViewRegistry.register(live)
99
-
100
- MpvRenderViewRegistry.unregister(stranger)
101
-
102
- assertTrue(MpvRenderViewRegistry.hasLiveView())
103
- assertSame(live, MpvRenderViewRegistry.current())
104
- }
105
-
106
- @Test
107
- fun `the newest registered picture does not displace the live one`() {
108
- // D9: one mounted player per lane, so two picture views should
109
- // not coexist. If they ever do, the registry still answers with a
110
- // real view rather than with nothing.
111
- val first = newPicture()
112
- val second = newPicture()
113
- MpvRenderViewRegistry.register(first)
114
- MpvRenderViewRegistry.register(second)
115
-
116
- assertTrue(MpvRenderViewRegistry.hasLiveView())
117
- val current = MpvRenderViewRegistry.current()
118
- assertTrue(current === first || current === second)
119
- }
120
- }
@@ -1,249 +0,0 @@
1
- package com.simba.player.mpv
2
-
3
- import android.os.Build
4
- import android.view.Surface
5
- import android.view.ViewGroup
6
- import androidx.test.core.app.ApplicationProvider
7
- import androidx.test.ext.junit.runners.AndroidJUnit4
8
- import com.simba.player.TestApplication
9
- import org.junit.Assert.assertEquals
10
- import org.junit.Assert.assertNotNull
11
- import org.junit.Assert.assertNull
12
- import org.junit.Before
13
- import org.junit.Ignore
14
- import org.junit.Test
15
- import org.junit.runner.RunWith
16
- import org.mockito.kotlin.mock
17
- import org.robolectric.annotation.Config
18
-
19
- /**
20
- * Unit tests for [MpvRenderView]'s private surface attach/detach
21
- * machinery, covering V12 spec §Phase 33.5 (null surface guard) and
22
- * §Phase 33.6 (detach idempotency).
23
- *
24
- * Both methods are `private` and interact with [MPVLib] (a JNI wrapper
25
- * that requires `System.loadLibrary("simbaplayer_mpv")` — not available
26
- * in JVM unit tests). We use reflection to:
27
- * 1. invoke the private methods directly with crafted inputs
28
- * 2. read/write the private `nativePtr` and `attachedSurface` fields
29
- * to drive specific branches
30
- *
31
- * What we test:
32
- * - attachSurfaceLocked(null) is a no-op (Phase 33.5) — added as a
33
- * defensive guard in Phase 33.
34
- * - attachSurfaceLocked with `nativePtr == 0L` is a no-op (covers the
35
- * "mpv not initialized yet" cold-start case).
36
- * - attachSurfaceLocked is idempotent for the same Surface instance
37
- * (the `attachedSurface === surface` check).
38
- * - detachSurfaceLocked is idempotent (Phase 33.6) — multiple calls
39
- * with `attachedSurface == null` don't crash.
40
- * - detachSurfaceLocked with `nativePtr == 0L` is a no-op.
41
- * - cleanup() (the public entry-point for "mpv destroyed") is safe
42
- * to call multiple times.
43
- * - surfaceCreated / surfaceDestroyed callbacks (the public API path
44
- * that drives attach/detach internally) work without crashing.
45
- *
46
- * Why Robolectric + AndroidJUnit4:
47
- * - `View.isAttachedToWindow()` works under Robolectric (returns false
48
- * because no Activity host has attached the view). This means the
49
- * attachSurfaceLocked's `!isAttachedToWindow` early-return is
50
- * always taken in unit tests — which is exactly what we want to
51
- * verify for the audio-mode / GONE case.
52
- * - `Surface` and `SurfaceHolder` can be mocked via Mockito.
53
- *
54
- * **Sandboxed CI runner limitation:** Robolectric downloads the
55
- * `android-all-instrumented` runtime jar from Maven Central at first
56
- * run. Sandboxed environments that block writes to `~/.m2/repository/`
57
- * (notably the TRAE sandbox) cannot populate the cache. The whole
58
- * class is `@Ignore`'d; CI runners with full disk access can run the
59
- * suite.
60
- */
61
- @Ignore("Robolectric requires downloading android-all-instrumented jars at first run; sandboxed CI runners block writes to ~/.m2/repository/. Run on a non-sandboxed workstation or full CI runner to enable.")
62
- @RunWith(AndroidJUnit4::class)
63
- @Config(sdk = [Build.VERSION_CODES.TIRAMISU], application = TestApplication::class)
64
- class MpvRenderViewTest {
65
-
66
- private lateinit var view: MpvRenderView
67
-
68
- private val nativePtrField by lazy {
69
- MpvRenderView::class.java
70
- .getDeclaredField("nativePtr")
71
- .apply { isAccessible = true }
72
- }
73
-
74
- private val attachedSurfaceField by lazy {
75
- MpvRenderView::class.java
76
- .getDeclaredField("attachedSurface")
77
- .apply { isAccessible = true }
78
- }
79
-
80
- private val attachSurfaceLockedMethod by lazy {
81
- MpvRenderView::class.java
82
- .getDeclaredMethod("attachSurfaceLocked", Surface::class.java)
83
- .apply { isAccessible = true }
84
- }
85
-
86
- private val detachSurfaceLockedMethod by lazy {
87
- MpvRenderView::class.java
88
- .getDeclaredMethod("detachSurfaceLocked")
89
- .apply { isAccessible = true }
90
- }
91
-
92
- @Before
93
- fun setUp() {
94
- view = MpvRenderView(ApplicationProvider.getApplicationContext())
95
- }
96
-
97
- // ── Spec §Phase 33.5: null surface is a no-op ────────────────────────
98
-
99
- @Test
100
- fun attachSurfaceLocked_nullSurface_isNoOp() {
101
- // Phase 33.5: the method must not NPE when called with a null
102
- // Surface. The new guard `if (surface == null) return` catches
103
- // this BEFORE the `surface.isValid` deref.
104
- // Set nativePtr to a non-zero value so the null guard is the
105
- // active check (rather than the earlier `nativePtr == 0L` guard).
106
- nativePtrField.setLong(view, 1L)
107
- // The actual reflection invocation must complete without
108
- // throwing. If the null guard is missing, this NPEs on
109
- // `surface.isValid`.
110
- attachSurfaceLockedMethod.invoke(view, /* surface = */ null)
111
- // Sanity: nativePtr is unchanged (the guard returned before any
112
- // mpv interaction).
113
- assertEquals(1L, nativePtrField.getLong(view))
114
- }
115
-
116
- @Test
117
- fun attachSurfaceLocked_nativePtrZero_isNoOp() {
118
- // Cold-start path: mpv not yet initialized (nativePtr == 0L).
119
- // The first guard `if (nativePtr == 0L) return` catches this
120
- // before any surface check.
121
- nativePtrField.setLong(view, 0L)
122
- // A non-null surface is required to confirm the guard is
123
- // checking nativePtr, not surface. (A mock Surface would NPE on
124
- // isValid, but we won't reach that line.)
125
- val mockSurface = mock<Surface>()
126
- attachSurfaceLockedMethod.invoke(view, mockSurface)
127
- assertEquals(0L, nativePtrField.getLong(view))
128
- }
129
-
130
- @Test
131
- fun attachSurfaceLocked_nullSurfaceAndNativePtrZero_isNoOp() {
132
- // Combined path: both guards trigger, but the earlier one wins.
133
- nativePtrField.setLong(view, 0L)
134
- attachSurfaceLockedMethod.invoke(view, null)
135
- assertEquals(0L, nativePtrField.getLong(view))
136
- }
137
-
138
- // ── Spec §Phase 33.6: detachSurfaceLocked idempotency ────────────────
139
-
140
- @Test
141
- fun detachSurfaceLocked_nativePtrZero_isNoOp() {
142
- // The method's first guard is `if (nativePtr == 0L) return;`.
143
- // Calling it on a fresh view (no mpv yet) must be a no-op.
144
- nativePtrField.setLong(view, 0L)
145
- detachSurfaceLockedMethod.invoke(view)
146
- detachSurfaceLockedMethod.invoke(view)
147
- detachSurfaceLockedMethod.invoke(view)
148
- // No exception thrown + nativePtr still 0.
149
- assertEquals(0L, nativePtrField.getLong(view))
150
- assertNull(attachedSurfaceField.get(view))
151
- }
152
-
153
- @Test
154
- fun detachSurfaceLocked_attachedSurfaceAlreadyNull_isNoOp() {
155
- // Second guard: `if (attachedSurface == null) return;`. With
156
- // nativePtr non-zero but no attached surface, calling detach
157
- // must be a no-op (and not accidentally call MPVLib).
158
- nativePtrField.setLong(view, 1L)
159
- attachedSurfaceField.set(view, null)
160
- detachSurfaceLockedMethod.invoke(view)
161
- detachSurfaceLockedMethod.invoke(view)
162
- // Native ptr unchanged (detach returned early).
163
- assertEquals(1L, nativePtrField.getLong(view))
164
- assertNull(attachedSurfaceField.get(view))
165
- }
166
-
167
- @Test
168
- fun cleanup_isIdempotent_andSafeToCallMultipleTimes() {
169
- // The public cleanup() entry-point delegates to
170
- // detachSurfaceLocked() and zeros nativePtr. It should be safe
171
- // to call multiple times (e.g. if onDestroy fires twice due to
172
- // some Activity restart edge case).
173
- view.cleanup()
174
- view.cleanup()
175
- view.cleanup()
176
- // After cleanup: nativePtr is 0 and attachedSurface is null.
177
- assertEquals(0L, nativePtrField.getLong(view))
178
- assertNull(attachedSurfaceField.get(view))
179
- }
180
-
181
- // ── SurfaceHolder.Callback (public API) ──────────────────────────────
182
-
183
- @Test
184
- fun surfaceDestroyed_withAttachedSurface_resetsState() {
185
- // Verify the public surfaceDestroyed path calls
186
- // detachSurfaceLocked which guards on attachedSurface == null.
187
- // We start with nativePtr=0, so detach should be a no-op.
188
- nativePtrField.setLong(view, 0L)
189
- view.surfaceDestroyed(view.holder)
190
- // If we got here without exception, the callback path is safe.
191
- assertEquals(0L, nativePtrField.getLong(view))
192
- }
193
-
194
- @Test
195
- fun surfaceCreated_doesNotCrash_whenMpvNotInitialized() {
196
- // Verify the public surfaceCreated path calls
197
- // attachSurfaceLocked(holder.surface). With nativePtr=0, the
198
- // early-return prevents any JNI access. holder.surface may be
199
- // null under Robolectric (no real Surface backing), which
200
- // tests our null guard transitively.
201
- nativePtrField.setLong(view, 0L)
202
- view.surfaceCreated(view.holder)
203
- assertEquals(0L, nativePtrField.getLong(view))
204
- }
205
-
206
- // ── Lifecycle: cleanup resets state correctly ────────────────────────
207
-
208
- @Test
209
- fun cleanup_afterAttachAttempt_resetsNativePtr() {
210
- // Simulate an attach attempt that did happen (attachedSurface
211
- // non-null) and then cleanup. Cleanup zeros nativePtr.
212
- nativePtrField.setLong(view, 1L)
213
- val mockSurface = mock<Surface>()
214
- attachedSurfaceField.set(view, mockSurface)
215
- view.cleanup()
216
- assertEquals(0L, nativePtrField.getLong(view))
217
- // detachSurfaceLocked sets attachedSurface = null (the line
218
- // runs BEFORE we zero nativePtr in cleanup()).
219
- assertNull(attachedSurfaceField.get(view))
220
- }
221
-
222
- // ── Layout params sanity ──────────────────────────────────────────────
223
-
224
- @Test
225
- fun init_setsMatchParentLayoutParams() {
226
- // The view's layoutParams are set in init to MATCH_PARENT x
227
- // MATCH_PARENT. This is part of the SurfaceView contract
228
- // required for PiP — verify it's not regressed.
229
- assertNotNull(view.layoutParams)
230
- assertEquals(
231
- ViewGroup.LayoutParams.MATCH_PARENT,
232
- view.layoutParams.width,
233
- )
234
- assertEquals(
235
- ViewGroup.LayoutParams.MATCH_PARENT,
236
- view.layoutParams.height,
237
- )
238
- }
239
-
240
- @Test
241
- fun init_setsNonInteractiveFlags() {
242
- // SurfaceView itself shouldn't capture touches — the controls
243
- // overlay handles them. isFocusable=false + isClickable=false
244
- // prevent the SurfaceView from stealing focus when overlays
245
- // are added on top.
246
- assertEquals(false, view.isFocusable)
247
- assertEquals(false, view.isClickable)
248
- }
249
- }