Skip to content

Commit

Permalink
feat: ensure setOptions is de-initialized if called twice
Browse files Browse the repository at this point in the history
  • Loading branch information
defuncart committed Jul 25, 2023
1 parent 6bd779f commit a2b2329
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,56 @@ void main() {
expect(video.getAttribute('disableRemotePlayback'), 'true');
});
});

group('when called first time', () {
testWidgets('expect correct options', (WidgetTester tester) async {
await player.setOptions(
const VideoPlayerWebOptions(
controls: VideoPlayerWebOptionsControls.enabled(
allowDownload: false,
allowFullscreen: false,
allowPlaybackRate: false,
allowPictureInPicture: false,
),
allowContextMenu: false,
allowRemotePlayback: false,
),
);

expect(video.controls, isTrue);
expect(video.controlsList, isNotNull);
expect(video.controlsList?.length, 3);
expect(video.controlsList?.contains('nodownload'), isTrue);
expect(video.controlsList?.contains('nofullscreen'), isTrue);
expect(video.controlsList?.contains('noplaybackrate'), isTrue);
expect(video.getAttribute('disablePictureInPicture'), 'true');
expect(video.getAttribute('disableRemotePlayback'), 'true');
});

group('when called once more', () {
testWidgets('expect correct options', (WidgetTester tester) async {
await player.setOptions(
const VideoPlayerWebOptions(
// ignore: avoid_redundant_argument_values
controls: VideoPlayerWebOptionsControls.disabled(),
// ignore: avoid_redundant_argument_values
allowContextMenu: true,
// ignore: avoid_redundant_argument_values
allowRemotePlayback: true,
),
);

expect(video.controls, isFalse);
expect(video.controlsList, isNotNull);
expect(video.controlsList?.length, 0);
expect(video.controlsList?.contains('nodownload'), isFalse);
expect(video.controlsList?.contains('nofullscreen'), isFalse);
expect(video.controlsList?.contains('noplaybackrate'), isFalse);
expect(video.getAttribute('disablePictureInPicture'), isNull);
expect(video.getAttribute('disableRemotePlayback'), isNull);
});
});
});
});
});
}
26 changes: 24 additions & 2 deletions packages/video_player/video_player_web/lib/src/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ class VideoPlayer {

/// Sets options
Future<void> setOptions(VideoPlayerWebOptions options) async {
// incase called multiple times, reset options
_resetOptions();

if (options.controls.enabled) {
_videoElement.controls = true;
final String controlsList = options.controls.controlsList;
Expand All @@ -227,11 +230,30 @@ class VideoPlayer {
}
}

void _resetOptions() {
_videoElement.controls = false;
if (_videoElement.hasAttribute('controlsList')) {
_videoElement.setAttribute('controlsList', '');
}
if (_videoElement.hasAttribute('disablePictureInPicture')) {
_videoElement.setAttribute('disablePictureInPicture', false);
}
if (_onContextMenu != null) {
_videoElement.removeEventListener('contextmenu', _onContextMenu);
_onContextMenu = null;
}
if (_videoElement.hasAttribute('disableRemotePlayback')) {
_videoElement.setAttribute('disableRemotePlayback', false);
}
}

/// Disposes of the current [html.VideoElement].
void dispose() {
_videoElement.removeAttribute('src');
_videoElement.removeEventListener('contextmenu', _onContextMenu);
_onContextMenu = null;
if (_onContextMenu != null) {
_videoElement.removeEventListener('contextmenu', _onContextMenu);
_onContextMenu = null;
}
_videoElement.load();
}

Expand Down

0 comments on commit a2b2329

Please sign in to comment.