|
| 1 | +// Copyright 2026 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'package:flutter/services.dart'; |
| 16 | + |
| 17 | +import 'package:flutter_test/flutter_test.dart'; |
| 18 | + |
| 19 | +import 'package:livekit_client/livekit_client.dart'; |
| 20 | +import 'package:livekit_client/src/proto/livekit_models.pb.dart' as lk_models; |
| 21 | +import 'package:livekit_client/src/proto/livekit_rtc.pb.dart' as lk_rtc; |
| 22 | +import 'package:livekit_client/src/support/native.dart'; |
| 23 | +import '../mock/e2e_container.dart'; |
| 24 | +import '../mock/e2ee_fake_manager.dart'; |
| 25 | + |
| 26 | +void main() { |
| 27 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 28 | + final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; |
| 29 | + const webrtcChannel = MethodChannel('FlutterWebRTC.Method'); |
| 30 | + |
| 31 | + late E2EContainer container; |
| 32 | + |
| 33 | + setUp(() { |
| 34 | + messenger.setMockMethodCallHandler(webrtcChannel, (call) async { |
| 35 | + if (call.method == 'getUserMedia') { |
| 36 | + return <String, dynamic>{ |
| 37 | + 'streamId': 'stream-1', |
| 38 | + 'audioTracks': [ |
| 39 | + <String, dynamic>{'id': 'mic-1', 'label': 'mic', 'kind': 'audio', 'enabled': true}, |
| 40 | + ], |
| 41 | + 'videoTracks': <dynamic>[], |
| 42 | + }; |
| 43 | + } |
| 44 | + return null; |
| 45 | + }); |
| 46 | + messenger.setMockMethodCallHandler(Native.channel, (call) async => true); |
| 47 | + container = E2EContainer(); |
| 48 | + }); |
| 49 | + |
| 50 | + tearDown(() async { |
| 51 | + await container.dispose(); |
| 52 | + messenger.setMockMethodCallHandler(webrtcChannel, null); |
| 53 | + messenger.setMockMethodCallHandler(Native.channel, null); |
| 54 | + }); |
| 55 | + |
| 56 | + /// Publishes a microphone track and returns the AddTrackRequest the SDK sent. |
| 57 | + Future<lk_rtc.AddTrackRequest> publishAndCaptureAddTrack({AudioPublishOptions? publishOptions}) async { |
| 58 | + await container.connectRoom(); |
| 59 | + final track = await LocalAudioTrack.create(); |
| 60 | + final publish = container.room.localParticipant!.publishAudioTrack(track, publishOptions: publishOptions); |
| 61 | + |
| 62 | + lk_rtc.AddTrackRequest? request; |
| 63 | + for (var i = 0; i < 100 && request == null; i++) { |
| 64 | + await Future<void>.delayed(const Duration(milliseconds: 5)); |
| 65 | + request = container.wsConnector.sentData |
| 66 | + .map(lk_rtc.SignalRequest.fromBuffer) |
| 67 | + .where((r) => r.hasAddTrack()) |
| 68 | + .map((r) => r.addTrack) |
| 69 | + .firstOrNull; |
| 70 | + } |
| 71 | + expect(request, isNotNull, reason: 'the SDK never sent an AddTrackRequest'); |
| 72 | + |
| 73 | + container.wsConnector.onData( |
| 74 | + lk_rtc.SignalResponse( |
| 75 | + trackPublished: lk_rtc.TrackPublishedResponse( |
| 76 | + cid: request!.cid, |
| 77 | + track: lk_models.TrackInfo(sid: 'TR_audio', type: lk_models.TrackType.AUDIO), |
| 78 | + ), |
| 79 | + ).writeToBuffer(), |
| 80 | + ); |
| 81 | + await publish; |
| 82 | + return request; |
| 83 | + } |
| 84 | + |
| 85 | + test('RED is requested by default', () async { |
| 86 | + final request = await publishAndCaptureAddTrack(); |
| 87 | + expect(request.disableRed, isFalse); |
| 88 | + }); |
| 89 | + |
| 90 | + test('red: false disables RED', () async { |
| 91 | + final request = await publishAndCaptureAddTrack(publishOptions: const AudioPublishOptions(red: false)); |
| 92 | + expect(request.disableRed, isTrue); |
| 93 | + }); |
| 94 | + |
| 95 | + test('E2EE disables RED even when red is true', () async { |
| 96 | + container.engine.setE2eeManager(TestE2EEManager()); |
| 97 | + final request = await publishAndCaptureAddTrack(publishOptions: const AudioPublishOptions(red: true)); |
| 98 | + expect(request.disableRed, isTrue); |
| 99 | + }); |
| 100 | +} |
0 commit comments