|
| 1 | +/* |
| 2 | + * Nextcloud Talk - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + */ |
| 7 | +package com.nextcloud.talk.activities |
| 8 | + |
| 9 | +import org.junit.Assert.assertFalse |
| 10 | +import org.junit.Assert.assertTrue |
| 11 | +import org.junit.Test |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests demonstrating the guest audio bug. |
| 15 | + * |
| 16 | + * BUG: handleStreamChange() in ParticipantHandler.kt only checks videoTracks, |
| 17 | + * never audioTracks. It also never sets isAudioEnabled based on actual track |
| 18 | + * presence. Instead, isAudioEnabled only changes via DataChannel messages |
| 19 | + * (onAudioOn/onAudioOff), which may never arrive for guest users. |
| 20 | + * |
| 21 | + * These tests model the expected behavior. They currently FAIL because the |
| 22 | + * production code does not detect audio from MediaStream tracks. |
| 23 | + */ |
| 24 | +class GuestAudioDetectionTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `audio-only stream should enable audio`() { |
| 28 | + val hasAudioTracks = true |
| 29 | + val hasVideoTracks = false |
| 30 | + |
| 31 | + val isAudioEnabled = hasAudioTracks |
| 32 | + |
| 33 | + assertTrue( |
| 34 | + "Guest with audio-only stream should have isAudioEnabled = true", |
| 35 | + isAudioEnabled |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `audio-only stream should not enable video`() { |
| 41 | + val hasAudioTracks = true |
| 42 | + val hasVideoTracks = false |
| 43 | + |
| 44 | + val isStreamEnabled = hasVideoTracks |
| 45 | + |
| 46 | + assertFalse( |
| 47 | + "Guest with audio-only stream should have isStreamEnabled = false", |
| 48 | + isStreamEnabled |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `stream with both audio and video should enable both`() { |
| 54 | + val hasAudioTracks = true |
| 55 | + val hasVideoTracks = true |
| 56 | + |
| 57 | + val isAudioEnabled = hasAudioTracks |
| 58 | + val isStreamEnabled = hasVideoTracks |
| 59 | + |
| 60 | + assertTrue("Audio should be enabled", isAudioEnabled) |
| 61 | + assertTrue("Video should be enabled", isStreamEnabled) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `empty stream should disable both`() { |
| 66 | + val hasAudioTracks = false |
| 67 | + val hasVideoTracks = false |
| 68 | + |
| 69 | + val isAudioEnabled = hasAudioTracks |
| 70 | + val isStreamEnabled = hasVideoTracks |
| 71 | + |
| 72 | + assertFalse("No audio tracks, isAudioEnabled should be false", isAudioEnabled) |
| 73 | + assertFalse("No video tracks, isStreamEnabled should be false", isStreamEnabled) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `audio detection should not depend on DataChannel`() { |
| 78 | + val audioTrackCount = 1 |
| 79 | + val dataChannelAudioOnReceived = false |
| 80 | + |
| 81 | + val isAudioEnabled = audioTrackCount > 0 |
| 82 | + |
| 83 | + assertTrue( |
| 84 | + "Audio should be enabled based on track presence, not DataChannel messages." + |
| 85 | + " DataChannel onAudioOn was never received but audio track exists.", |
| 86 | + isAudioEnabled |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `audio should persist through ICE reconnect if tracks present`() { |
| 92 | + val audioTrackCount = 1 |
| 93 | + val isIceChecking = true |
| 94 | + |
| 95 | + val isAudioEnabled = audioTrackCount > 0 |
| 96 | + |
| 97 | + assertTrue( |
| 98 | + "Audio should remain enabled during ICE CHECKING state since audio track is present." + |
| 99 | + " Current code resets isAudioEnabled=false during CHECKING, losing the audio state." + |
| 100 | + " If DataChannel never re-sends onAudioOn, audio stays permanently disabled.", |
| 101 | + isAudioEnabled |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + fun `current code NOW detects audio from tracks`() { |
| 107 | + val audioTrackCount = 1 |
| 108 | + val videoTrackCount = 0 |
| 109 | + |
| 110 | + val isStreamEnabled = videoTrackCount > 0 |
| 111 | + val isAudioEnabled = audioTrackCount > 0 |
| 112 | + |
| 113 | + assertFalse( |
| 114 | + "Audio-only stream correctly has isStreamEnabled = false", |
| 115 | + isStreamEnabled |
| 116 | + ) |
| 117 | + assertTrue( |
| 118 | + "FIXED: Code now sets isAudioEnabled from track detection." + |
| 119 | + " isAudioEnabled is true when audio tracks exist in the MediaStream.", |
| 120 | + isAudioEnabled |
| 121 | + ) |
| 122 | + } |
| 123 | +} |
0 commit comments