|
| 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 | +import 'dart:html' as html; |
| 7 | + |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:integration_test/integration_test.dart'; |
| 10 | +import 'package:video_player_platform_interface/video_player_platform_interface.dart'; |
| 11 | +import 'package:video_player_web/src/video_player.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 15 | + |
| 16 | + group('VideoPlayer', () { |
| 17 | + late html.VideoElement video; |
| 18 | + |
| 19 | + setUp(() { |
| 20 | + // Never set "src" on the video, so this test doesn't hit the network! |
| 21 | + video = html.VideoElement() |
| 22 | + ..controls = true |
| 23 | + ..setAttribute('playsinline', 'false'); |
| 24 | + }); |
| 25 | + |
| 26 | + testWidgets('fixes critical video element config', (WidgetTester _) async { |
| 27 | + VideoPlayer(videoElement: video).initialize(); |
| 28 | + |
| 29 | + expect(video.controls, isFalse, |
| 30 | + reason: 'Video is controlled through code'); |
| 31 | + expect(video.getAttribute('autoplay'), 'false', |
| 32 | + reason: 'Cannot autoplay on the web'); |
| 33 | + expect(video.getAttribute('playsinline'), 'true', |
| 34 | + reason: 'Needed by safari iOS'); |
| 35 | + }); |
| 36 | + |
| 37 | + testWidgets('setVolume', (WidgetTester tester) async { |
| 38 | + final VideoPlayer player = VideoPlayer(videoElement: video)..initialize(); |
| 39 | + |
| 40 | + player.setVolume(0); |
| 41 | + |
| 42 | + expect(video.volume, isZero, reason: 'Volume should be zero'); |
| 43 | + expect(video.muted, isTrue, reason: 'muted attribute should be true'); |
| 44 | + |
| 45 | + expect(() { |
| 46 | + player.setVolume(-0.0001); |
| 47 | + }, throwsAssertionError, reason: 'Volume cannot be < 0'); |
| 48 | + |
| 49 | + expect(() { |
| 50 | + player.setVolume(1.0001); |
| 51 | + }, throwsAssertionError, reason: 'Volume cannot be > 1'); |
| 52 | + }); |
| 53 | + |
| 54 | + testWidgets('setPlaybackSpeed', (WidgetTester tester) async { |
| 55 | + final VideoPlayer player = VideoPlayer(videoElement: video)..initialize(); |
| 56 | + |
| 57 | + expect(() { |
| 58 | + player.setPlaybackSpeed(-1); |
| 59 | + }, throwsAssertionError, reason: 'Playback speed cannot be < 0'); |
| 60 | + |
| 61 | + expect(() { |
| 62 | + player.setPlaybackSpeed(0); |
| 63 | + }, throwsAssertionError, reason: 'Playback speed cannot be == 0'); |
| 64 | + }); |
| 65 | + |
| 66 | + testWidgets('seekTo', (WidgetTester tester) async { |
| 67 | + final VideoPlayer player = VideoPlayer(videoElement: video)..initialize(); |
| 68 | + |
| 69 | + expect(() { |
| 70 | + player.seekTo(const Duration(seconds: -1)); |
| 71 | + }, throwsAssertionError, reason: 'Cannot seek into negative numbers'); |
| 72 | + }); |
| 73 | + |
| 74 | + // The events tested in this group do *not* represent the actual sequence |
| 75 | + // of events from a real "video" element. They're crafted to test the |
| 76 | + // behavior of the VideoPlayer in different states with different events. |
| 77 | + group('events', () { |
| 78 | + late StreamController<VideoEvent> streamController; |
| 79 | + late VideoPlayer player; |
| 80 | + late Stream<VideoEvent> timedStream; |
| 81 | + |
| 82 | + final Set<VideoEventType> bufferingEvents = <VideoEventType>{ |
| 83 | + VideoEventType.bufferingStart, |
| 84 | + VideoEventType.bufferingEnd, |
| 85 | + }; |
| 86 | + |
| 87 | + setUp(() { |
| 88 | + streamController = StreamController<VideoEvent>(); |
| 89 | + player = |
| 90 | + VideoPlayer(videoElement: video, eventController: streamController) |
| 91 | + ..initialize(); |
| 92 | + |
| 93 | + // This stream will automatically close after 100 ms without seeing any events |
| 94 | + timedStream = streamController.stream.timeout( |
| 95 | + const Duration(milliseconds: 100), |
| 96 | + onTimeout: (EventSink<VideoEvent> sink) { |
| 97 | + sink.close(); |
| 98 | + }, |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + testWidgets('buffering dispatches only when it changes', |
| 103 | + (WidgetTester tester) async { |
| 104 | + // Take all the "buffering" events that we see during the next few seconds |
| 105 | + final Future<List<bool>> stream = timedStream |
| 106 | + .where( |
| 107 | + (VideoEvent event) => bufferingEvents.contains(event.eventType)) |
| 108 | + .map((VideoEvent event) => |
| 109 | + event.eventType == VideoEventType.bufferingStart) |
| 110 | + .toList(); |
| 111 | + |
| 112 | + // Simulate some events coming from the player... |
| 113 | + player.setBuffering(true); |
| 114 | + player.setBuffering(true); |
| 115 | + player.setBuffering(true); |
| 116 | + player.setBuffering(false); |
| 117 | + player.setBuffering(false); |
| 118 | + player.setBuffering(true); |
| 119 | + player.setBuffering(false); |
| 120 | + player.setBuffering(true); |
| 121 | + player.setBuffering(false); |
| 122 | + |
| 123 | + final List<bool> events = await stream; |
| 124 | + |
| 125 | + expect(events, hasLength(6)); |
| 126 | + expect(events, <bool>[true, false, true, false, true, false]); |
| 127 | + }); |
| 128 | + |
| 129 | + testWidgets('canplay event does not change buffering state', |
| 130 | + (WidgetTester tester) async { |
| 131 | + // Take all the "buffering" events that we see during the next few seconds |
| 132 | + final Future<List<bool>> stream = timedStream |
| 133 | + .where( |
| 134 | + (VideoEvent event) => bufferingEvents.contains(event.eventType)) |
| 135 | + .map((VideoEvent event) => |
| 136 | + event.eventType == VideoEventType.bufferingStart) |
| 137 | + .toList(); |
| 138 | + |
| 139 | + player.setBuffering(true); |
| 140 | + |
| 141 | + // Simulate "canplay" event... |
| 142 | + video.dispatchEvent(html.Event('canplay')); |
| 143 | + |
| 144 | + final List<bool> events = await stream; |
| 145 | + |
| 146 | + expect(events, hasLength(1)); |
| 147 | + expect(events, <bool>[true]); |
| 148 | + }); |
| 149 | + |
| 150 | + testWidgets('canplaythrough event does change buffering state', |
| 151 | + (WidgetTester tester) async { |
| 152 | + // Take all the "buffering" events that we see during the next few seconds |
| 153 | + final Future<List<bool>> stream = timedStream |
| 154 | + .where( |
| 155 | + (VideoEvent event) => bufferingEvents.contains(event.eventType)) |
| 156 | + .map((VideoEvent event) => |
| 157 | + event.eventType == VideoEventType.bufferingStart) |
| 158 | + .toList(); |
| 159 | + |
| 160 | + player.setBuffering(true); |
| 161 | + |
| 162 | + // Simulate "canplaythrough" event... |
| 163 | + video.dispatchEvent(html.Event('canplaythrough')); |
| 164 | + |
| 165 | + final List<bool> events = await stream; |
| 166 | + |
| 167 | + expect(events, hasLength(2)); |
| 168 | + expect(events, <bool>[true, false]); |
| 169 | + }); |
| 170 | + |
| 171 | + testWidgets('initialized dispatches only once', |
| 172 | + (WidgetTester tester) async { |
| 173 | + // Dispatch some bogus "canplay" events from the video object |
| 174 | + video.dispatchEvent(html.Event('canplay')); |
| 175 | + video.dispatchEvent(html.Event('canplay')); |
| 176 | + video.dispatchEvent(html.Event('canplay')); |
| 177 | + |
| 178 | + // Take all the "initialized" events that we see during the next few seconds |
| 179 | + final Future<List<VideoEvent>> stream = timedStream |
| 180 | + .where((VideoEvent event) => |
| 181 | + event.eventType == VideoEventType.initialized) |
| 182 | + .toList(); |
| 183 | + |
| 184 | + video.dispatchEvent(html.Event('canplay')); |
| 185 | + video.dispatchEvent(html.Event('canplay')); |
| 186 | + video.dispatchEvent(html.Event('canplay')); |
| 187 | + |
| 188 | + final List<VideoEvent> events = await stream; |
| 189 | + |
| 190 | + expect(events, hasLength(1)); |
| 191 | + expect(events[0].eventType, VideoEventType.initialized); |
| 192 | + }); |
| 193 | + }); |
| 194 | + }); |
| 195 | +} |
0 commit comments