|
| 1 | +/* |
| 2 | + * Nextcloud Talk - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Alain Lauzon |
| 5 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + */ |
| 7 | +package com.nextcloud.talk.activities |
| 8 | + |
| 9 | +import android.app.Application |
| 10 | +import com.nextcloud.talk.call.LocalStateBroadcaster |
| 11 | +import com.nextcloud.talk.signaling.SignalingMessageReceiver |
| 12 | +import kotlinx.coroutines.flow.StateFlow |
| 13 | +import org.junit.Assert.assertSame |
| 14 | +import org.junit.Before |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import org.mockito.kotlin.argumentCaptor |
| 18 | +import org.mockito.kotlin.mock |
| 19 | +import org.mockito.kotlin.verify |
| 20 | +import org.robolectric.Robolectric |
| 21 | +import org.robolectric.RobolectricTestRunner |
| 22 | +import org.robolectric.annotation.Config |
| 23 | +import java.lang.reflect.Field |
| 24 | + |
| 25 | +/** |
| 26 | + * Robolectric test verifying that [CallActivity.addCallParticipant] passes the **live StateFlow** |
| 27 | + * from [ParticipantHandler.uiState] to [LocalStateBroadcaster.handleCallParticipantAdded]. |
| 28 | + * |
| 29 | + * Before the fix, the method passed only the snapshot value [StateFlow.value], meaning |
| 30 | + * [LocalStateBroadcasterNoMcu] could not observe future ICE state changes and videoOn/audioOn |
| 31 | + * were never sent once the data channel was ready. |
| 32 | + * |
| 33 | + * This test: |
| 34 | + * - creates [CallActivity] via Robolectric WITHOUT calling [Activity.onCreate] (avoiding native |
| 35 | + * WebRTC and Dagger initialisation) |
| 36 | + * - injects the required fields via reflection |
| 37 | + * - calls [addCallParticipant] via reflection |
| 38 | + * - verifies that the argument passed to [LocalStateBroadcaster.handleCallParticipantAdded] is |
| 39 | + * the exact same [StateFlow] object as [CallViewModel.getParticipant]!!.uiState |
| 40 | + * |
| 41 | + * If the buggy snapshot pattern is used (`uiState.value` instead of `uiState`), the verification |
| 42 | + * fails because the [StateFlow] overload is never called. |
| 43 | + */ |
| 44 | +@RunWith(RobolectricTestRunner::class) |
| 45 | +@Config( |
| 46 | + // Use the plain Application to avoid NextcloudTalkApplication.onCreate() which initialises |
| 47 | + // Dagger, WebRTC, and WorkManager — none of which are needed for this focused test. |
| 48 | + application = Application::class, |
| 49 | + sdk = [33] |
| 50 | +) |
| 51 | +class CallActivityAddParticipantTest { |
| 52 | + |
| 53 | + private val mockLocalStateBroadcaster: LocalStateBroadcaster = mock() |
| 54 | + private val mockSignalingMessageReceiver: SignalingMessageReceiver = mock() |
| 55 | + |
| 56 | + private lateinit var callViewModel: CallViewModel |
| 57 | + private lateinit var activity: CallActivity |
| 58 | + |
| 59 | + @Before |
| 60 | + fun setUp() { |
| 61 | + callViewModel = CallViewModel() |
| 62 | + |
| 63 | + // Build the Activity without triggering onCreate — this avoids Dagger injection, |
| 64 | + // EglBase.create() (native), and all network calls. |
| 65 | + activity = Robolectric.buildActivity(CallActivity::class.java).get() |
| 66 | + |
| 67 | + // Inject the minimum set of fields required by addCallParticipant. |
| 68 | + setField("callViewModel", callViewModel) |
| 69 | + setField("localStateBroadcaster", mockLocalStateBroadcaster) |
| 70 | + setField("signalingMessageReceiver", mockSignalingMessageReceiver) |
| 71 | + setField("baseUrl", "https://test.example.com") |
| 72 | + setField("roomToken", "testRoom") |
| 73 | + // hasExternalSignalingServer = true skips the OfferAnswerNickProvider branch |
| 74 | + setField("hasExternalSignalingServer", true) |
| 75 | + } |
| 76 | + |
| 77 | + // ----------------------------------------------------------------------------------------- |
| 78 | + // Tests |
| 79 | + // ----------------------------------------------------------------------------------------- |
| 80 | + |
| 81 | + /** |
| 82 | + * The live [StateFlow] from [ParticipantHandler.uiState] must be passed to |
| 83 | + * [LocalStateBroadcaster.handleCallParticipantAdded], not just the current snapshot. |
| 84 | + * |
| 85 | + * Fails with the buggy code because the snapshot call goes to the |
| 86 | + * [handleCallParticipantAdded(ParticipantUiState)] overload, not the StateFlow one. |
| 87 | + */ |
| 88 | + @Test |
| 89 | + fun `addCallParticipant passes the live StateFlow to handleCallParticipantAdded`() { |
| 90 | + val sessionId = "robolectric-session-1" |
| 91 | + |
| 92 | + invokeAddCallParticipant(sessionId) |
| 93 | + |
| 94 | + val captor = argumentCaptor<StateFlow<ParticipantUiState>>() |
| 95 | + verify(mockLocalStateBroadcaster).handleCallParticipantAdded(captor.capture()) |
| 96 | + |
| 97 | + val capturedFlow = captor.firstValue |
| 98 | + |
| 99 | + // The captured argument must be the SAME StateFlow object that ParticipantHandler |
| 100 | + // exposes — not a copy, not a one-shot MutableStateFlow wrapping the snapshot. |
| 101 | + assertSame( |
| 102 | + "Expected the live ParticipantHandler.uiState StateFlow, got a different object", |
| 103 | + callViewModel.getParticipant(sessionId)!!.uiState, |
| 104 | + capturedFlow |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + // ----------------------------------------------------------------------------------------- |
| 109 | + // Reflection helpers |
| 110 | + // ----------------------------------------------------------------------------------------- |
| 111 | + |
| 112 | + private fun invokeAddCallParticipant(sessionId: String) { |
| 113 | + val method = CallActivity::class.java.getDeclaredMethod("addCallParticipant", String::class.java) |
| 114 | + method.isAccessible = true |
| 115 | + method.invoke(activity, sessionId) |
| 116 | + } |
| 117 | + |
| 118 | + private fun setField(fieldName: String, value: Any?) { |
| 119 | + val field = findField(CallActivity::class.java, fieldName) |
| 120 | + ?: error("Field '$fieldName' not found in CallActivity hierarchy") |
| 121 | + field.isAccessible = true |
| 122 | + field.set(activity, value) |
| 123 | + } |
| 124 | + |
| 125 | + private fun findField(clazz: Class<*>, fieldName: String): Field? { |
| 126 | + var current: Class<*>? = clazz |
| 127 | + while (current != null) { |
| 128 | + try { |
| 129 | + return current.getDeclaredField(fieldName) |
| 130 | + } catch (e: NoSuchFieldException) { |
| 131 | + current = current.superclass |
| 132 | + } |
| 133 | + } |
| 134 | + return null |
| 135 | + } |
| 136 | +} |
0 commit comments