Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: library tests to play multiple sources (#1333)
* test: disable simultaneously tests for Android
- Loading branch information
Showing
12 changed files
with
234 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/audioplayers/example/integration_test/lib_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:audioplayers/audioplayers.dart'; | ||
import 'package:audioplayers_example/tabs/sources.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:integration_test/integration_test.dart'; | ||
|
||
import 'platform_features.dart'; | ||
import 'source_test_data.dart'; | ||
|
||
void main() { | ||
final features = PlatformFeatures.instance(); | ||
|
||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
group('play multiple sources', () { | ||
final audioTestDataList = [ | ||
if (features.hasUrlSource) | ||
LibSourceTestData( | ||
source: UrlSource(wavUrl1), | ||
duration: const Duration(milliseconds: 451), | ||
), | ||
if (features.hasUrlSource) | ||
LibSourceTestData( | ||
source: UrlSource(wavUrl2), | ||
duration: const Duration(seconds: 1, milliseconds: 068), | ||
), | ||
if (features.hasUrlSource) | ||
LibSourceTestData( | ||
source: UrlSource(mp3Url1), | ||
duration: const Duration(minutes: 3, seconds: 30, milliseconds: 77), | ||
), | ||
if (features.hasUrlSource) | ||
LibSourceTestData( | ||
source: UrlSource(mp3Url2), | ||
duration: const Duration(minutes: 1, seconds: 34, milliseconds: 119), | ||
), | ||
if (features.hasUrlSource && features.hasPlaylistSourceType) | ||
LibSourceTestData( | ||
source: UrlSource(m3u8StreamUrl), | ||
duration: Duration.zero, | ||
isLiveStream: true, | ||
), | ||
if (features.hasUrlSource) | ||
LibSourceTestData( | ||
source: UrlSource(mpgaStreamUrl), | ||
duration: Duration.zero, | ||
isLiveStream: true, | ||
), | ||
if (features.hasAssetSource) | ||
LibSourceTestData( | ||
source: AssetSource(asset1), | ||
duration: const Duration(seconds: 1, milliseconds: 068), | ||
), | ||
if (features.hasAssetSource) | ||
LibSourceTestData( | ||
source: AssetSource(asset2), | ||
duration: const Duration(minutes: 1, seconds: 34, milliseconds: 119), | ||
), | ||
]; | ||
|
||
testWidgets( | ||
'play multiple sources simultaneously', | ||
(WidgetTester tester) async { | ||
final players = | ||
List.generate(audioTestDataList.length, (_) => AudioPlayer()); | ||
|
||
// Start all players simultaneously | ||
final iterator = List<int>.generate(audioTestDataList.length, (i) => i); | ||
await Future.wait<void>( | ||
iterator.map((i) => players[i].play(audioTestDataList[i].source)), | ||
); | ||
await tester.pumpAndSettle(); | ||
// Sources take some time to get initialized | ||
await tester.pump(const Duration(seconds: 8)); | ||
for (var i = 0; i < audioTestDataList.length; i++) { | ||
final td = audioTestDataList[i]; | ||
if (td.isLiveStream || td.duration > const Duration(seconds: 10)) { | ||
await tester.pump(); | ||
final position = await players[i].getCurrentPosition(); | ||
printOnFailure('Test position: $td'); | ||
expect(position, greaterThan(Duration.zero)); | ||
} | ||
await players[i].stop(); | ||
} | ||
}, | ||
// FIXME: Causes media error on Android (see #1333, #1353) | ||
// Unexpected platform error: MediaPlayer error with | ||
// what:MEDIA_ERROR_UNKNOWN {what:1} extra:MEDIA_ERROR_SYSTEM | ||
skip: !kIsWeb && Platform.isAndroid, | ||
); | ||
|
||
testWidgets('play multiple sources consecutively', | ||
(WidgetTester tester) async { | ||
final player = AudioPlayer(); | ||
|
||
for (var i = 0; i < audioTestDataList.length; i++) { | ||
final td = audioTestDataList[i]; | ||
await player.play(td.source); | ||
await tester.pumpAndSettle(); | ||
// Sources take some time to get initialized | ||
await tester.pump(const Duration(seconds: 8)); | ||
if (td.isLiveStream || td.duration > const Duration(seconds: 10)) { | ||
await tester.pump(); | ||
final position = await player.getCurrentPosition(); | ||
printOnFailure('Test position: $td'); | ||
expect(position, greaterThan(Duration.zero)); | ||
} | ||
await player.stop(); | ||
} | ||
}); | ||
}); | ||
} |
46 changes: 42 additions & 4 deletions
46
packages/audioplayers/example/integration_test/source_test_data.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,61 @@ | ||
/// Data of a test source. | ||
class SourceTestData { | ||
String sourceKey; | ||
import 'package:audioplayers/audioplayers.dart'; | ||
|
||
/// Data of a ui test source. | ||
abstract class SourceTestData { | ||
Duration duration; | ||
|
||
bool isLiveStream; | ||
|
||
SourceTestData({ | ||
required this.sourceKey, | ||
required this.duration, | ||
this.isLiveStream = false, | ||
}); | ||
|
||
@override | ||
String toString() { | ||
return 'SourceTestData(' | ||
'duration: $duration, ' | ||
'isLiveStream: $isLiveStream' | ||
')'; | ||
} | ||
} | ||
|
||
/// Data of a ui test source. | ||
class AppSourceTestData extends SourceTestData { | ||
String sourceKey; | ||
|
||
AppSourceTestData({ | ||
required this.sourceKey, | ||
required super.duration, | ||
super.isLiveStream, | ||
}); | ||
|
||
@override | ||
String toString() { | ||
return 'UiSourceTestData(' | ||
'sourceKey: $sourceKey, ' | ||
'duration: $duration, ' | ||
'isLiveStream: $isLiveStream' | ||
')'; | ||
} | ||
} | ||
|
||
/// Data of a library test source. | ||
class LibSourceTestData extends SourceTestData { | ||
Source source; | ||
|
||
LibSourceTestData({ | ||
required this.source, | ||
required super.duration, | ||
super.isLiveStream, | ||
}); | ||
|
||
@override | ||
String toString() { | ||
return 'RawSourceTestData(' | ||
'source: $source, ' | ||
'duration: $duration, ' | ||
'isLiveStream: $isLiveStream' | ||
')'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.