|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:stream_video/src/call/state/call_state_notifier.dart'; |
| 3 | +import 'package:stream_video/stream_video.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('CallStateNotifier participantMirrorVideo tests', () { |
| 7 | + late CallStateNotifier stateNotifier; |
| 8 | + late CallState initialState; |
| 9 | + |
| 10 | + setUp(() { |
| 11 | + final targetParticipant = CallParticipantState( |
| 12 | + userId: 'target-user', |
| 13 | + sessionId: 'target-session', |
| 14 | + name: 'Target Participant', |
| 15 | + roles: const [], |
| 16 | + custom: const {}, |
| 17 | + trackIdPrefix: 'target-track', |
| 18 | + publishedTracks: { |
| 19 | + SfuTrackType.video: TrackState.remote( |
| 20 | + subscribed: true, |
| 21 | + received: true, |
| 22 | + ), |
| 23 | + }, |
| 24 | + ); |
| 25 | + |
| 26 | + final otherParticipant = CallParticipantState( |
| 27 | + userId: 'other-user', |
| 28 | + sessionId: 'other-session', |
| 29 | + name: 'Other Participant', |
| 30 | + image: '', |
| 31 | + roles: const [], |
| 32 | + custom: const {}, |
| 33 | + trackIdPrefix: 'other-track', |
| 34 | + publishedTracks: { |
| 35 | + SfuTrackType.video: TrackState.remote( |
| 36 | + subscribed: true, |
| 37 | + received: true, |
| 38 | + ), |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + initialState = CallState( |
| 43 | + callCid: StreamCallCid.from( |
| 44 | + type: StreamCallType.defaultType(), |
| 45 | + id: 'test-call', |
| 46 | + ), |
| 47 | + currentUserId: 'current-user', |
| 48 | + preferences: DefaultCallPreferences(), |
| 49 | + ).copyWith( |
| 50 | + callParticipants: [targetParticipant, otherParticipant], |
| 51 | + ); |
| 52 | + |
| 53 | + stateNotifier = CallStateNotifier(initialState); |
| 54 | + }); |
| 55 | + |
| 56 | + tearDown(() { |
| 57 | + stateNotifier.dispose(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('participantMirrorVideo updates target participant video track', () { |
| 61 | + final stateBefore = stateNotifier.state; |
| 62 | + expect(stateBefore.callParticipants.length, 2); |
| 63 | + |
| 64 | + final targetParticipantBefore = stateBefore.callParticipants.firstWhere( |
| 65 | + (p) => p.sessionId == 'target-session', |
| 66 | + ); |
| 67 | + final targetVideoTrackBefore = targetParticipantBefore |
| 68 | + .publishedTracks[SfuTrackType.video]! as RemoteTrackState; |
| 69 | + expect(targetVideoTrackBefore.mirrorVideo, false); |
| 70 | + |
| 71 | + final otherParticipantBefore = stateBefore.callParticipants.firstWhere( |
| 72 | + (p) => p.sessionId == 'other-session', |
| 73 | + ); |
| 74 | + final otherVideoTrackBefore = otherParticipantBefore |
| 75 | + .publishedTracks[SfuTrackType.video]! as RemoteTrackState; |
| 76 | + expect(otherVideoTrackBefore.mirrorVideo, false); |
| 77 | + |
| 78 | + stateNotifier.participantMirrorVideo( |
| 79 | + sessionId: 'target-session', |
| 80 | + userId: 'target-user', |
| 81 | + mirrorVideo: true, |
| 82 | + ); |
| 83 | + |
| 84 | + final stateAfter = stateNotifier.state; |
| 85 | + expect(stateAfter.callParticipants.length, 2); |
| 86 | + |
| 87 | + final targetParticipantAfter = stateAfter.callParticipants.firstWhere( |
| 88 | + (p) => p.sessionId == 'target-session', |
| 89 | + ); |
| 90 | + final targetVideoTrackAfter = targetParticipantAfter |
| 91 | + .publishedTracks[SfuTrackType.video]! as RemoteTrackState; |
| 92 | + expect(targetVideoTrackAfter.mirrorVideo, true); |
| 93 | + |
| 94 | + // Other participant should remain unchanged |
| 95 | + final otherParticipantAfter = stateAfter.callParticipants.firstWhere( |
| 96 | + (p) => p.sessionId == 'other-session', |
| 97 | + ); |
| 98 | + final otherVideoTrackAfter = otherParticipantAfter |
| 99 | + .publishedTracks[SfuTrackType.video]! as RemoteTrackState; |
| 100 | + expect(otherVideoTrackAfter.mirrorVideo, false); |
| 101 | + |
| 102 | + // Verify other properties remain unchanged |
| 103 | + expect(targetVideoTrackAfter.muted, targetVideoTrackBefore.muted); |
| 104 | + expect( |
| 105 | + targetVideoTrackAfter.subscribed, |
| 106 | + targetVideoTrackBefore.subscribed, |
| 107 | + ); |
| 108 | + expect(targetVideoTrackAfter.received, targetVideoTrackBefore.received); |
| 109 | + }); |
| 110 | + |
| 111 | + test('participantMirrorVideo ignores participants without video tracks', |
| 112 | + () { |
| 113 | + // Arrange - Create state with participant that has no video track |
| 114 | + final participantWithoutVideo = CallParticipantState( |
| 115 | + userId: 'no-video-user', |
| 116 | + sessionId: 'no-video-session', |
| 117 | + name: 'Audio Only Participant', |
| 118 | + image: '', |
| 119 | + roles: const [], |
| 120 | + custom: const {}, |
| 121 | + trackIdPrefix: 'no-video-track', |
| 122 | + publishedTracks: { |
| 123 | + SfuTrackType.audio: TrackState.remote(), // Only audio track |
| 124 | + }, |
| 125 | + ); |
| 126 | + |
| 127 | + final stateWithAudioOnly = initialState.copyWith( |
| 128 | + callParticipants: [ |
| 129 | + ...initialState.callParticipants, |
| 130 | + participantWithoutVideo |
| 131 | + ], |
| 132 | + ); |
| 133 | + stateNotifier.state = stateWithAudioOnly; |
| 134 | + |
| 135 | + stateNotifier.participantMirrorVideo( |
| 136 | + sessionId: 'no-video-session', |
| 137 | + userId: 'no-video-user', |
| 138 | + mirrorVideo: true, |
| 139 | + ); |
| 140 | + |
| 141 | + final stateAfter = stateNotifier.state; |
| 142 | + final participantAfter = stateAfter.callParticipants.firstWhere( |
| 143 | + (p) => p.sessionId == 'no-video-session', |
| 144 | + ); |
| 145 | + |
| 146 | + expect( |
| 147 | + participantAfter.publishedTracks.containsKey(SfuTrackType.video), |
| 148 | + false, |
| 149 | + ); |
| 150 | + expect( |
| 151 | + participantAfter.publishedTracks[SfuTrackType.audio], |
| 152 | + isA<RemoteTrackState>(), |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + test('participantMirrorVideo only affects RemoteTrackState', () { |
| 157 | + // Arrange - Create participant with LocalTrackState |
| 158 | + final localParticipant = CallParticipantState( |
| 159 | + userId: 'local-user', |
| 160 | + sessionId: 'local-session', |
| 161 | + name: 'Local Participant', |
| 162 | + roles: const [], |
| 163 | + custom: const {}, |
| 164 | + isLocal: true, |
| 165 | + trackIdPrefix: 'local-track', |
| 166 | + publishedTracks: { |
| 167 | + SfuTrackType.video: TrackState.local(), // Local track |
| 168 | + }, |
| 169 | + ); |
| 170 | + |
| 171 | + final stateWithLocal = initialState.copyWith( |
| 172 | + callParticipants: [...initialState.callParticipants, localParticipant], |
| 173 | + ); |
| 174 | + stateNotifier.state = stateWithLocal; |
| 175 | + |
| 176 | + final localTrackBefore = stateWithLocal.callParticipants |
| 177 | + .firstWhere((p) => p.isLocal) |
| 178 | + .publishedTracks[SfuTrackType.video]! as LocalTrackState; |
| 179 | + |
| 180 | + stateNotifier.participantMirrorVideo( |
| 181 | + sessionId: 'local-session', |
| 182 | + userId: 'local-user', |
| 183 | + mirrorVideo: true, |
| 184 | + ); |
| 185 | + |
| 186 | + // Assert - Local track should remain unchanged (no mirrorVideo property) |
| 187 | + final stateAfter = stateNotifier.state; |
| 188 | + final localTrackAfter = stateAfter.callParticipants |
| 189 | + .firstWhere((p) => p.sessionId == 'local-session') |
| 190 | + .publishedTracks[SfuTrackType.video]! as LocalTrackState; |
| 191 | + |
| 192 | + expect(localTrackAfter.muted, localTrackBefore.muted); |
| 193 | + expect(localTrackAfter.sourceDevice, localTrackBefore.sourceDevice); |
| 194 | + expect(localTrackAfter.cameraPosition, localTrackBefore.cameraPosition); |
| 195 | + }); |
| 196 | + }); |
| 197 | +} |
0 commit comments