@@ -21,6 +21,9 @@ void main() {
2121 final isAndroid = ! kIsWeb && Platform .isAndroid;
2222 final isLinux = ! kIsWeb && Platform .isLinux;
2323
24+ // FIXME(gustl22): Cannot reuse event channel with same id on Linux (flutter/flutter#126209)
25+ var linuxPlayerCount = 0 ;
26+
2427 final wavUrl1TestData = LibSourceTestData (
2528 source: UrlSource (wavUrl1),
2629 duration: const Duration (milliseconds: 451 ),
@@ -245,8 +248,7 @@ void main() {
245248 testWidgets ('Emit platform log' , (tester) async {
246249 final logCompleter = Completer <String >();
247250
248- // FIXME(gustl22): Cannot reuse event channel with same id on Linux (flutter/flutter#126209)
249- final playerId = isLinux ? 'somePlayerId0' : 'somePlayerId' ;
251+ final playerId = 'somePlayerId${isLinux ? linuxPlayerCount ++ : '' }' ;
250252 final player = AudioPlayer (playerId: playerId);
251253 final onLogSub = player.onLog.listen (
252254 logCompleter.complete,
@@ -331,8 +333,7 @@ void main() {
331333 testWidgets ('#create and #dispose' , (tester) async {
332334 final platform = AudioplayersPlatformInterface .instance;
333335
334- // FIXME(gustl22): Cannot reuse event channel with same id on Linux (flutter/flutter#126209)
335- final playerId = isLinux ? 'somePlayerId1' : 'somePlayerId' ;
336+ final playerId = 'somePlayerId${isLinux ? linuxPlayerCount ++ : '' }' ;
336337 await platform.create (playerId);
337338 await tester.pumpAndSettle ();
338339 await platform.dispose (playerId);
@@ -353,8 +354,7 @@ void main() {
353354 testWidgets ('#setSource #getPosition and #getDuration' , (tester) async {
354355 final platform = AudioplayersPlatformInterface .instance;
355356
356- // FIXME(gustl22): Cannot reuse event channel with same id on Linux (flutter/flutter#126209)
357- final playerId = isLinux ? 'somePlayerId2' : 'somePlayerId' ;
357+ final playerId = 'somePlayerId${isLinux ? linuxPlayerCount ++ : '' }' ;
358358 await platform.create (playerId);
359359
360360 final preparedCompleter = Completer <void >();
@@ -392,10 +392,65 @@ void main() {
392392 await platform.dispose (playerId);
393393 }
394394 });
395+
396+ testWidgets ('Set same source twice (#1520)' , (tester) async {
397+ final platform = AudioplayersPlatformInterface .instance;
398+
399+ final playerId = 'somePlayerId${isLinux ? linuxPlayerCount ++ : '' }' ;
400+ await platform.create (playerId);
401+
402+ final eventStream = platform.getEventStream (playerId);
403+ for (var i = 0 ; i < 2 ; i++ ) {
404+ final preparedCompleter = Completer <void >();
405+ final onPreparedSub = eventStream
406+ .where ((event) => event.eventType == AudioEventType .prepared)
407+ .map ((event) => event.isPrepared! )
408+ .listen (
409+ (isPrepared) {
410+ if (isPrepared) {
411+ preparedCompleter.complete ();
412+ }
413+ },
414+ onError: preparedCompleter.completeError,
415+ );
416+ if (isLinux) {
417+ // FIXME(gustl22): Linux needs additional pump (#1507)
418+ await tester.pump ();
419+ }
420+ await platform.setSourceUrl (
421+ playerId,
422+ (wavUrl1TestData.source as UrlSource ).url,
423+ );
424+ await preparedCompleter.future.timeout (const Duration (seconds: 30 ));
425+ await onPreparedSub.cancel ();
426+ }
427+ if (! isLinux) {
428+ // FIXME(gustl22): Linux not disposing properly (#1507)
429+ await platform.dispose (playerId);
430+ }
431+ });
395432 });
396433
397434 group ('Platform event channel' , () {
398- // TODO(gustl22): remove once https://github.com/flutter/flutter/issues/126209 is fixed
435+ testWidgets ('Listen and cancel twice' , (tester) async {
436+ final platform = AudioplayersPlatformInterface .instance;
437+
438+ final playerId = 'somePlayerId${isLinux ? linuxPlayerCount ++ : '' }' ;
439+ await platform.create (playerId);
440+
441+ final eventStream = platform.getEventStream (playerId);
442+ for (var i = 0 ; i < 2 ; i++ ) {
443+ final eventSub = eventStream.listen (null );
444+ await eventSub.cancel ();
445+ }
446+ if (! isLinux) {
447+ // FIXME(gustl22): Linux not disposing properly (#1507)
448+ await platform.dispose (playerId);
449+ }
450+ });
451+
452+ // TODO(gustl22): remove once https://github.com/flutter/flutter/issues/126209
453+ // is fixed, as tests should cover the problem in flutter engine.
399454 testWidgets (
400455 'Reuse same platform event channel id' ,
401456 (tester) async {
@@ -425,8 +480,7 @@ void main() {
425480 final errorCompleter = Completer <Object >();
426481 final platform = AudioplayersPlatformInterface .instance;
427482
428- // FIXME(gustl22): Cannot reuse event channel with same id on Linux (flutter/flutter#126209)
429- final playerId = isLinux ? 'somePlayerId3' : 'somePlayerId' ;
483+ final playerId = 'somePlayerId${isLinux ? linuxPlayerCount ++ : '' }' ;
430484 await platform.create (playerId);
431485
432486 final eventStreamSub = platform
0 commit comments