Skip to content

Commit 4e53528

Browse files
[video_player] Activate leak testing (flutter#8379)
The package manipulates disposable objects. This PR activates leak testing to make sure disposable objects are correctly disposed. It also fixes the memory leak warnings from the tests See the documentation: https://github.com/dart-lang/leak_tracker/blob/main/doc%2Fleak_tracking%2FDETECT.md
1 parent b346343 commit 4e53528

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

packages/video_player/video_player/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies:
3333
dev_dependencies:
3434
flutter_test:
3535
sdk: flutter
36+
leak_tracker_flutter_testing: any
3637

3738
topics:
3839
- video
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8+
9+
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
10+
LeakTesting.enable();
11+
LeakTracking.warnForUnsupportedPlatforms = false;
12+
await testMain();
13+
}

packages/video_player/video_player/test/video_player_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ void main() {
131131

132132
testWidgets('update texture', (WidgetTester tester) async {
133133
final FakeController controller = FakeController();
134+
addTearDown(controller.dispose);
134135
await tester.pumpWidget(VideoPlayer(controller));
135136
expect(find.byType(Texture), findsNothing);
136137

@@ -146,6 +147,7 @@ void main() {
146147

147148
testWidgets('update controller', (WidgetTester tester) async {
148149
final FakeController controller1 = FakeController();
150+
addTearDown(controller1.dispose);
149151
controller1.textureId = 101;
150152
await tester.pumpWidget(VideoPlayer(controller1));
151153
expect(
@@ -155,6 +157,7 @@ void main() {
155157
findsOneWidget);
156158

157159
final FakeController controller2 = FakeController();
160+
addTearDown(controller2.dispose);
158161
controller2.textureId = 102;
159162
await tester.pumpWidget(VideoPlayer(controller2));
160163
expect(
@@ -169,6 +172,7 @@ void main() {
169172
final FakeController controller = FakeController.value(
170173
const VideoPlayerValue(
171174
duration: Duration.zero, rotationCorrection: 180));
175+
addTearDown(controller.dispose);
172176
controller.textureId = 1;
173177
await tester.pumpWidget(VideoPlayer(controller));
174178
final Transform actualRotationCorrection =
@@ -189,6 +193,7 @@ void main() {
189193
(WidgetTester tester) async {
190194
final FakeController controller =
191195
FakeController.value(const VideoPlayerValue(duration: Duration.zero));
196+
addTearDown(controller.dispose);
192197
controller.textureId = 1;
193198
await tester.pumpWidget(VideoPlayer(controller));
194199
expect(find.byType(Transform), findsNothing);
@@ -315,6 +320,7 @@ void main() {
315320
VideoPlayerController.networkUrl(
316321
Uri.parse('https://127.0.0.1'),
317322
);
323+
addTearDown(controller.dispose);
318324
await controller.initialize();
319325
await controller.play();
320326
verifyPlayStateRespondsToLifecycle(controller,
@@ -334,6 +340,7 @@ void main() {
334340
test('network url', () async {
335341
final VideoPlayerController controller =
336342
VideoPlayerController.networkUrl(Uri.parse('https://127.0.0.1'));
343+
addTearDown(controller.dispose);
337344
await controller.initialize();
338345

339346
expect(
@@ -356,6 +363,7 @@ void main() {
356363
Uri.parse('https://127.0.0.1'),
357364
formatHint: VideoFormat.dash,
358365
);
366+
addTearDown(controller.dispose);
359367
await controller.initialize();
360368

361369
expect(
@@ -378,6 +386,7 @@ void main() {
378386
Uri.parse('https://127.0.0.1'),
379387
httpHeaders: <String, String>{'Authorization': 'Bearer token'},
380388
);
389+
addTearDown(controller.dispose);
381390
await controller.initialize();
382391

383392
expect(
@@ -401,6 +410,7 @@ void main() {
401410

402411
final VideoPlayerController controller =
403412
VideoPlayerController.networkUrl(invalidUrl);
413+
addTearDown(controller.dispose);
404414

405415
late Object error;
406416
fakeVideoPlayerPlatform.forceInitError = true;
@@ -464,6 +474,7 @@ void main() {
464474
() async {
465475
final VideoPlayerController controller =
466476
VideoPlayerController.networkUrl(_localhostUri);
477+
addTearDown(controller.dispose);
467478

468479
fakeVideoPlayerPlatform.forceInitError = true;
469480
await controller.initialize().catchError((dynamic e) {});
@@ -485,6 +496,7 @@ void main() {
485496
test('dispose', () async {
486497
final VideoPlayerController controller =
487498
VideoPlayerController.networkUrl(_localhostUri);
499+
addTearDown(controller.dispose);
488500

489501
expect(
490502
controller.textureId, VideoPlayerController.kUninitializedTextureId);
@@ -500,6 +512,7 @@ void main() {
500512
test('calling dispose() on disposed controller does not throw', () async {
501513
final VideoPlayerController controller =
502514
VideoPlayerController.networkUrl(_localhostUri);
515+
addTearDown(controller.dispose);
503516

504517
await controller.initialize();
505518
await controller.dispose();
@@ -510,6 +523,7 @@ void main() {
510523
test('play', () async {
511524
final VideoPlayerController controller =
512525
VideoPlayerController.networkUrl(Uri.parse('https://127.0.0.1'));
526+
addTearDown(controller.dispose);
513527

514528
await controller.initialize();
515529
expect(controller.value.isPlaying, isFalse);
@@ -529,6 +543,7 @@ void main() {
529543
test('play before initialized does not call platform', () async {
530544
final VideoPlayerController controller =
531545
VideoPlayerController.networkUrl(_localhostUri);
546+
addTearDown(controller.dispose);
532547

533548
expect(controller.value.isInitialized, isFalse);
534549

@@ -540,6 +555,7 @@ void main() {
540555
test('play restarts from beginning if video is at end', () async {
541556
final VideoPlayerController controller =
542557
VideoPlayerController.networkUrl(_localhostUri);
558+
addTearDown(controller.dispose);
543559

544560
await controller.initialize();
545561
const Duration nonzeroDuration = Duration(milliseconds: 100);
@@ -557,6 +573,7 @@ void main() {
557573
test('setLooping', () async {
558574
final VideoPlayerController controller =
559575
VideoPlayerController.networkUrl(_localhostUri);
576+
addTearDown(controller.dispose);
560577

561578
await controller.initialize();
562579
expect(controller.value.isLooping, isFalse);
@@ -568,6 +585,7 @@ void main() {
568585
test('pause', () async {
569586
final VideoPlayerController controller =
570587
VideoPlayerController.networkUrl(_localhostUri);
588+
addTearDown(controller.dispose);
571589

572590
await controller.initialize();
573591
await controller.play();
@@ -583,6 +601,7 @@ void main() {
583601
test('works', () async {
584602
final VideoPlayerController controller =
585603
VideoPlayerController.networkUrl(_localhostUri);
604+
addTearDown(controller.dispose);
586605

587606
await controller.initialize();
588607
expect(await controller.position, Duration.zero);
@@ -595,6 +614,7 @@ void main() {
595614
test('before initialized does not call platform', () async {
596615
final VideoPlayerController controller =
597616
VideoPlayerController.networkUrl(_localhostUri);
617+
addTearDown(controller.dispose);
598618

599619
expect(controller.value.isInitialized, isFalse);
600620

@@ -606,6 +626,7 @@ void main() {
606626
test('clamps values that are too high or low', () async {
607627
final VideoPlayerController controller =
608628
VideoPlayerController.networkUrl(_localhostUri);
629+
addTearDown(controller.dispose);
609630

610631
await controller.initialize();
611632
expect(await controller.position, Duration.zero);
@@ -622,6 +643,7 @@ void main() {
622643
test('works', () async {
623644
final VideoPlayerController controller =
624645
VideoPlayerController.networkUrl(_localhostUri);
646+
addTearDown(controller.dispose);
625647

626648
await controller.initialize();
627649
expect(controller.value.volume, 1.0);
@@ -635,6 +657,7 @@ void main() {
635657
test('clamps values that are too high or low', () async {
636658
final VideoPlayerController controller =
637659
VideoPlayerController.networkUrl(_localhostUri);
660+
addTearDown(controller.dispose);
638661

639662
await controller.initialize();
640663
expect(controller.value.volume, 1.0);
@@ -651,6 +674,7 @@ void main() {
651674
test('works', () async {
652675
final VideoPlayerController controller =
653676
VideoPlayerController.networkUrl(_localhostUri);
677+
addTearDown(controller.dispose);
654678

655679
await controller.initialize();
656680
expect(controller.value.playbackSpeed, 1.0);
@@ -664,6 +688,7 @@ void main() {
664688
test('rejects negative values', () async {
665689
final VideoPlayerController controller =
666690
VideoPlayerController.networkUrl(_localhostUri);
691+
addTearDown(controller.dispose);
667692

668693
await controller.initialize();
669694
expect(controller.value.playbackSpeed, 1.0);
@@ -698,6 +723,7 @@ void main() {
698723
expect(controller.value.isPlaying, isTrue);
699724

700725
await controller.pause();
726+
await tester.runAsync(controller.dispose);
701727
});
702728

703729
testWidgets('does not restart when dragging to end',
@@ -723,6 +749,7 @@ void main() {
723749

724750
expect(controller.value.position, controller.value.duration);
725751
expect(controller.value.isPlaying, isFalse);
752+
await tester.runAsync(controller.dispose);
726753
});
727754
});
728755

@@ -733,6 +760,7 @@ void main() {
733760
_localhostUri,
734761
closedCaptionFile: _loadClosedCaption(),
735762
);
763+
addTearDown(controller.dispose);
736764

737765
await controller.initialize();
738766
expect(controller.value.position, Duration.zero);
@@ -766,6 +794,7 @@ void main() {
766794
_localhostUri,
767795
closedCaptionFile: _loadClosedCaption(),
768796
);
797+
addTearDown(controller.dispose);
769798

770799
await controller.initialize();
771800
controller.setCaptionOffset(const Duration(milliseconds: 100));
@@ -803,6 +832,7 @@ void main() {
803832
_localhostUri,
804833
closedCaptionFile: _loadClosedCaption(),
805834
);
835+
addTearDown(controller.dispose);
806836

807837
await controller.initialize();
808838
controller.setCaptionOffset(const Duration(milliseconds: -100));
@@ -842,6 +872,7 @@ void main() {
842872
VideoPlayerController.networkUrl(
843873
_localhostUri,
844874
);
875+
addTearDown(controller.dispose);
845876

846877
await controller.initialize();
847878
expect(controller.closedCaptionFile, null);
@@ -859,6 +890,7 @@ void main() {
859890
_localhostUri,
860891
closedCaptionFile: _loadClosedCaption(),
861892
);
893+
addTearDown(controller.dispose);
862894

863895
await controller.initialize();
864896
expect(
@@ -893,6 +925,7 @@ void main() {
893925

894926
expect(controller.value.isPlaying, isFalse);
895927
expect(controller.value.position, nonzeroDuration);
928+
await tester.runAsync(controller.dispose);
896929
});
897930

898931
testWidgets('playback status', (WidgetTester tester) async {
@@ -917,6 +950,7 @@ void main() {
917950
));
918951
await tester.pumpAndSettle();
919952
expect(controller.value.isPlaying, isFalse);
953+
await tester.runAsync(controller.dispose);
920954
});
921955

922956
testWidgets('buffering status', (WidgetTester tester) async {
@@ -953,6 +987,7 @@ void main() {
953987
.add(VideoEvent(eventType: VideoEventType.bufferingEnd));
954988
await tester.pumpAndSettle();
955989
expect(controller.value.isBuffering, isFalse);
990+
await tester.runAsync(controller.dispose);
956991
});
957992
});
958993
});
@@ -1155,6 +1190,7 @@ void main() {
11551190
_localhostUri,
11561191
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
11571192
);
1193+
addTearDown(controller.dispose);
11581194

11591195
await controller.initialize();
11601196
expect(controller.videoPlayerOptions!.mixWithOthers, true);
@@ -1167,6 +1203,7 @@ void main() {
11671203
allowBackgroundPlayback: true,
11681204
),
11691205
);
1206+
addTearDown(controller.dispose);
11701207

11711208
await controller.initialize();
11721209
await controller.play();
@@ -1181,6 +1218,7 @@ void main() {
11811218
_localhostUri,
11821219
videoPlayerOptions: VideoPlayerOptions(),
11831220
);
1221+
addTearDown(controller.dispose);
11841222

11851223
await controller.initialize();
11861224
await controller.play();
@@ -1211,6 +1249,7 @@ void main() {
12111249
_localhostUri,
12121250
videoPlayerOptions: VideoPlayerOptions(),
12131251
);
1252+
addTearDown(controller.dispose);
12141253

12151254
await controller.initialize();
12161255

@@ -1238,6 +1277,7 @@ void main() {
12381277
_localhostUri,
12391278
videoPlayerOptions: VideoPlayerOptions(),
12401279
);
1280+
addTearDown(controller.dispose);
12411281

12421282
await controller.initialize();
12431283

@@ -1275,6 +1315,7 @@ void main() {
12751315
_localhostUri,
12761316
videoPlayerOptions: VideoPlayerOptions(),
12771317
);
1318+
addTearDown(controller.dispose);
12781319

12791320
await controller.initialize();
12801321

0 commit comments

Comments
 (0)