|
| 1 | +// Copyright 2019, the Chromium project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// TODO(amirh): Remove this once flutter_driver supports null safety. |
| 6 | +// https://github.com/flutter/flutter/issues/71379 |
| 7 | +// @dart = 2.9 |
| 8 | +import 'dart:async'; |
| 9 | + |
| 10 | +import 'package:flutter/material.dart'; |
| 11 | +import 'package:integration_test/integration_test.dart'; |
| 12 | +import 'package:flutter_test/flutter_test.dart'; |
| 13 | +import 'package:video_player/video_player.dart'; |
| 14 | + |
| 15 | +const Duration _playDuration = Duration(seconds: 1); |
| 16 | + |
| 17 | +void main() { |
| 18 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 19 | + VideoPlayerController _controller; |
| 20 | + tearDown(() async => _controller.dispose()); |
| 21 | + |
| 22 | + group('network videos', () { |
| 23 | + setUp(() { |
| 24 | + _controller = VideoPlayerController.network( |
| 25 | + 'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4', |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + testWidgets('can be initialized', (WidgetTester tester) async { |
| 30 | + await _controller.initialize(); |
| 31 | + |
| 32 | + expect(_controller.value.isInitialized, true); |
| 33 | + expect(_controller.value.position, const Duration(seconds: 0)); |
| 34 | + expect(_controller.value.isPlaying, false); |
| 35 | + expect(_controller.value.duration, |
| 36 | + const Duration(seconds: 4, milliseconds: 036)); |
| 37 | + }); |
| 38 | + |
| 39 | + testWidgets('can be played', (WidgetTester tester) async { |
| 40 | + await _controller.initialize(); |
| 41 | + await _controller.play(); |
| 42 | + await tester.pumpAndSettle(_playDuration); |
| 43 | + |
| 44 | + expect(_controller.value.isPlaying, true); |
| 45 | + expect(_controller.value.position, |
| 46 | + (Duration position) => position > const Duration(seconds: 0)); |
| 47 | + }); |
| 48 | + |
| 49 | + testWidgets('can seek', (WidgetTester tester) async { |
| 50 | + await _controller.initialize(); |
| 51 | + |
| 52 | + await _controller.seekTo(const Duration(seconds: 3)); |
| 53 | + |
| 54 | + expect(_controller.value.position, const Duration(seconds: 3)); |
| 55 | + }); |
| 56 | + |
| 57 | + testWidgets( |
| 58 | + 'can be paused', |
| 59 | + (WidgetTester tester) async { |
| 60 | + await _controller.initialize(); |
| 61 | + // Play for a second, then pause, and then wait a second. |
| 62 | + await _controller.play(); |
| 63 | + await tester.pumpAndSettle(_playDuration); |
| 64 | + await _controller.pause(); |
| 65 | + final Duration pausedPosition = _controller.value.position; |
| 66 | + await tester.pumpAndSettle(_playDuration); |
| 67 | + |
| 68 | + // Verify that we stopped playing after the pause. |
| 69 | + expect(_controller.value.isPlaying, false); |
| 70 | + expect(_controller.value.position, pausedPosition); |
| 71 | + }, |
| 72 | + ); |
| 73 | + |
| 74 | + testWidgets('reports buffering status', (WidgetTester tester) async { |
| 75 | + await _controller.initialize(); |
| 76 | + final Completer<void> started = Completer(); |
| 77 | + final Completer<void> ended = Completer(); |
| 78 | + bool startedBuffering = false; |
| 79 | + bool endedBuffering = false; |
| 80 | + _controller.addListener( |
| 81 | + () { |
| 82 | + if (_controller.value.isBuffering && !startedBuffering) { |
| 83 | + startedBuffering = true; |
| 84 | + started.complete(); |
| 85 | + } |
| 86 | + if (startedBuffering && |
| 87 | + !_controller.value.isBuffering && |
| 88 | + !endedBuffering) { |
| 89 | + endedBuffering = true; |
| 90 | + ended.complete(); |
| 91 | + } |
| 92 | + }, |
| 93 | + ); |
| 94 | + |
| 95 | + await _controller.play(); |
| 96 | + await _controller.seekTo(const Duration(seconds: 5)); |
| 97 | + await tester.pumpAndSettle(_playDuration); |
| 98 | + await _controller.pause(); |
| 99 | + |
| 100 | + expect(_controller.value.isPlaying, false); |
| 101 | + expect(_controller.value.position, |
| 102 | + (Duration position) => position > const Duration(seconds: 0)); |
| 103 | + |
| 104 | + await started; |
| 105 | + expect(startedBuffering, true); |
| 106 | + |
| 107 | + await ended; |
| 108 | + expect(endedBuffering, true); |
| 109 | + }); |
| 110 | + |
| 111 | + testWidgets('can show video player', (WidgetTester tester) async { |
| 112 | + Future<bool> started() async { |
| 113 | + await _controller.initialize(); |
| 114 | + await _controller.play(); |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + await tester.pumpWidget(Material( |
| 119 | + elevation: 0, |
| 120 | + child: Directionality( |
| 121 | + textDirection: TextDirection.ltr, |
| 122 | + child: Center( |
| 123 | + child: FutureBuilder<bool>( |
| 124 | + future: started(), |
| 125 | + builder: (BuildContext context, AsyncSnapshot<bool> snapshot) { |
| 126 | + if (snapshot.data == true) { |
| 127 | + return AspectRatio( |
| 128 | + aspectRatio: _controller.value.aspectRatio, |
| 129 | + child: VideoPlayer(_controller), |
| 130 | + ); |
| 131 | + } else { |
| 132 | + return const Text('waiting for video to load'); |
| 133 | + } |
| 134 | + }, |
| 135 | + ), |
| 136 | + ), |
| 137 | + ), |
| 138 | + )); |
| 139 | + |
| 140 | + await tester.pumpAndSettle(_playDuration); |
| 141 | + expect(_controller.value.isPlaying, true); |
| 142 | + }); |
| 143 | + }); |
| 144 | +} |
0 commit comments