Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 5f95287

Browse files
committed
Simplify testing constructor for VideoPlayer.java.
Use `rotationCorrection` instead of `rotation`.
1 parent fd20737 commit 5f95287

File tree

6 files changed

+47
-64
lines changed

6 files changed

+47
-64
lines changed

packages/video_player/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayer.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,11 @@ final class VideoPlayer {
6767
String formatHint,
6868
Map<String, String> httpHeaders,
6969
VideoPlayerOptions options) {
70-
this(
71-
context,
72-
eventChannel,
73-
textureEntry,
74-
dataSource,
75-
formatHint,
76-
httpHeaders,
77-
options,
78-
null);
79-
}
80-
81-
VideoPlayer(
82-
Context context,
83-
EventChannel eventChannel,
84-
TextureRegistry.SurfaceTextureEntry textureEntry,
85-
String dataSource,
86-
String formatHint,
87-
Map<String, String> httpHeaders,
88-
VideoPlayerOptions options,
89-
SimpleExoPlayer exoPlayer) {
9070
this.eventChannel = eventChannel;
9171
this.textureEntry = textureEntry;
9272
this.options = options;
9373

94-
if (exoPlayer != null) { // Skip remaining setup in tests if using a mock exoPlayer.
95-
this.exoPlayer = exoPlayer;
96-
return;
97-
}
98-
this.exoPlayer = new SimpleExoPlayer.Builder(context).build();
74+
exoPlayer = new SimpleExoPlayer.Builder(context).build();
9975

10076
Uri uri = Uri.parse(dataSource);
10177

@@ -115,12 +91,24 @@ final class VideoPlayer {
11591
}
11692

11793
MediaSource mediaSource = buildMediaSource(uri, dataSourceFactory, formatHint, context);
118-
this.exoPlayer.setMediaSource(mediaSource);
119-
this.exoPlayer.prepare();
94+
exoPlayer.setMediaSource(mediaSource);
95+
exoPlayer.prepare();
12096

12197
setupVideoPlayer(eventChannel, textureEntry);
12298
}
12399

100+
@VisibleForTesting
101+
VideoPlayer(
102+
EventChannel eventChannel,
103+
TextureRegistry.SurfaceTextureEntry textureEntry,
104+
VideoPlayerOptions options,
105+
SimpleExoPlayer exoPlayer) {
106+
this.eventChannel = eventChannel;
107+
this.textureEntry = textureEntry;
108+
this.options = options;
109+
this.exoPlayer = exoPlayer;
110+
}
111+
124112
private static boolean isHTTP(Uri uri) {
125113
if (uri == null || uri.getScheme() == null) {
126114
return false;
@@ -306,11 +294,15 @@ void sendInitialized() {
306294
width = exoPlayer.getVideoFormat().height;
307295
height = exoPlayer.getVideoFormat().width;
308296
}
309-
if (rotationDegrees == 180) {
310-
event.put("rotation", Math.PI);
311-
}
312297
event.put("width", width);
313298
event.put("height", height);
299+
300+
// Rotating the video with ExoPlayer does not seem to be possible with a Surface,
301+
// so inform the Flutter code that the widget needs to be rotated to prevent
302+
// upside-down playback.
303+
if (rotationDegrees == 180) {
304+
event.put("rotationCorrection", Math.PI);
305+
}
314306
}
315307
eventSink.success(event);
316308
}

packages/video_player/video_player/android/src/test/java/io/flutter/plugins/videoplayer/VideoPlayerTest.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
import static org.mockito.Mockito.verify;
1111
import static org.mockito.Mockito.when;
1212

13-
import android.content.Context;
1413
import com.google.android.exoplayer2.Format;
1514
import com.google.android.exoplayer2.SimpleExoPlayer;
1615
import io.flutter.plugin.common.EventChannel;
1716
import io.flutter.view.TextureRegistry;
18-
import java.util.HashMap;
1917
import java.util.Map;
2018
import org.junit.Test;
2119
import org.mockito.ArgumentCaptor;
@@ -28,17 +26,13 @@ public void initPluginDoesNotThrow() {
2826
}
2927

3028
@Test
31-
public void videoPlayerSendInitializedSetsRotationForRotationDegrees180() {
29+
public void videoPlayerSendInitializedSetsRotationCorrectionForRotationDegrees180() {
3230
Format format = new Format.Builder().setRotationDegrees(180).build();
3331
SimpleExoPlayer mockExoPlayer = mock(SimpleExoPlayer.class);
3432
when(mockExoPlayer.getVideoFormat()).thenReturn(format);
3533
final VideoPlayer player = new VideoPlayer(
36-
mock(Context.class),
3734
mock(EventChannel.class),
3835
mock(TextureRegistry.SurfaceTextureEntry.class),
39-
"",
40-
"",
41-
new HashMap<String, String>(),
4236
mock(VideoPlayerOptions.class),
4337
mockExoPlayer
4438
);
@@ -52,21 +46,17 @@ public void videoPlayerSendInitializedSetsRotationForRotationDegrees180() {
5246
verify(mockEventSink).success(eventCaptor.capture());
5347
@SuppressWarnings("unchecked") Map<String, Object> capturedEventMap =
5448
(Map<String, Object>) eventCaptor.getValue();
55-
assertEquals(Math.PI, capturedEventMap.get("rotation"));
49+
assertEquals(Math.PI, capturedEventMap.get("rotationCorrection"));
5650
}
5751

5852
@Test
59-
public void videoPlayerSendInitializedDoesNotSetRotationForRotationDegreesNot180() {
53+
public void videoPlayerSendInitializedDoesNotSetRotationCorrectionForRotationDegreesNot180() {
6054
Format format = new Format.Builder().setRotationDegrees(90).build();
6155
SimpleExoPlayer mockExoPlayer = mock(SimpleExoPlayer.class);
6256
when(mockExoPlayer.getVideoFormat()).thenReturn(format);
6357
final VideoPlayer player = new VideoPlayer(
64-
mock(Context.class),
6558
mock(EventChannel.class),
6659
mock(TextureRegistry.SurfaceTextureEntry.class),
67-
"",
68-
"",
69-
new HashMap<String, String>(),
7060
mock(VideoPlayerOptions.class),
7161
mockExoPlayer
7262
);
@@ -80,6 +70,6 @@ public void videoPlayerSendInitializedDoesNotSetRotationForRotationDegreesNot180
8070
verify(mockEventSink).success(eventCaptor.capture());
8171
@SuppressWarnings("unchecked") Map<String, Object> capturedEventMap =
8272
(Map<String, Object>) eventCaptor.getValue();
83-
assertFalse(capturedEventMap.containsKey("rotation"));
73+
assertFalse(capturedEventMap.containsKey("rotationCorrection"));
8474
}
8575
}

packages/video_player/video_player/lib/video_player.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class VideoPlayerValue {
3939
this.isBuffering = false,
4040
this.volume = 1.0,
4141
this.playbackSpeed = 1.0,
42-
this.rotation = 0.0,
42+
this.rotationCorrection = 0.0,
4343
this.errorDescription,
4444
});
4545

@@ -94,8 +94,8 @@ class VideoPlayerValue {
9494
/// The [size] of the currently loaded video.
9595
final Size size;
9696

97-
/// The [rotation] of the video.
98-
final double rotation;
97+
/// Radians to rotate the video so it is displayed correctly.
98+
final double rotationCorrection;
9999

100100
/// Indicates whether or not the video has been loaded and is ready to play.
101101
final bool isInitialized;
@@ -135,7 +135,7 @@ class VideoPlayerValue {
135135
bool? isBuffering,
136136
double? volume,
137137
double? playbackSpeed,
138-
double? rotation,
138+
double? rotationCorrection,
139139
String? errorDescription,
140140
}) {
141141
return VideoPlayerValue(
@@ -150,7 +150,7 @@ class VideoPlayerValue {
150150
isBuffering: isBuffering ?? this.isBuffering,
151151
volume: volume ?? this.volume,
152152
playbackSpeed: playbackSpeed ?? this.playbackSpeed,
153-
rotation: rotation ?? this.rotation,
153+
rotationCorrection: rotationCorrection ?? this.rotationCorrection,
154154
errorDescription: errorDescription ?? this.errorDescription,
155155
);
156156
}
@@ -326,7 +326,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
326326
value = value.copyWith(
327327
duration: event.duration,
328328
size: event.size,
329-
rotation: event.rotation,
329+
rotationCorrection: event.rotationCorrection,
330330
isInitialized: event.duration != null,
331331
);
332332
initializingCompleter.complete(null);
@@ -669,7 +669,7 @@ class _VideoPlayerState extends State<VideoPlayer> {
669669
return _textureId == VideoPlayerController.kUninitializedTextureId
670670
? Container()
671671
: Transform.rotate(
672-
angle: widget.controller.value.rotation,
672+
angle: widget.controller.value.rotationCorrection,
673673
child: _videoPlayerPlatform.buildView(_textureId),
674674
);
675675
}

packages/video_player/video_player/test/video_player_test.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,17 @@ void main() {
134134
findsOneWidget);
135135
});
136136

137-
testWidgets('rotation value is used', (WidgetTester tester) async {
138-
final double expectedRotation = 3.14;
139-
final FakeController controller = FakeController.value(
140-
VideoPlayerValue(duration: Duration.zero, rotation: expectedRotation));
137+
testWidgets('rotationCorrection value is used', (WidgetTester tester) async {
138+
final double expectedRotationCorrection = 3.14;
139+
final FakeController controller = FakeController.value(VideoPlayerValue(
140+
duration: Duration.zero,
141+
rotationCorrection: expectedRotationCorrection));
141142
controller.textureId = 1;
142143
await tester.pumpWidget(VideoPlayer(controller));
143-
Transform actualRotation =
144+
Transform actualRotationCorrection =
144145
find.byType(Transform).evaluate().single.widget as Transform;
145-
expect(
146-
actualRotation.transform, equals(Matrix4.rotationZ(expectedRotation)));
146+
expect(actualRotationCorrection.transform,
147+
equals(Matrix4.rotationZ(expectedRotationCorrection)));
147148
});
148149

149150
group('ClosedCaption widget', () {

packages/video_player/video_player_platform_interface/lib/method_channel_video_player.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class MethodChannelVideoPlayer extends VideoPlayerPlatform {
108108
duration: Duration(milliseconds: map['duration']),
109109
size: Size(map['width']?.toDouble() ?? 0.0,
110110
map['height']?.toDouble() ?? 0.0),
111-
rotation: map['rotation']?.toDouble() ?? 0.0,
111+
rotationCorrection: map['rotationCorrection']?.toDouble() ?? 0.0,
112112
);
113113
case 'completed':
114114
return VideoEvent(

packages/video_player/video_player_platform_interface/lib/video_player_platform_interface.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ class VideoEvent {
219219
///
220220
/// The [eventType] argument is required.
221221
///
222-
/// Depending on the [eventType], the [duration], [size], [rotation], and
223-
/// [buffered] arguments can be null.
222+
/// Depending on the [eventType], the [duration], [size],
223+
/// [rotationCorrection], and [buffered] arguments can be null.
224224
VideoEvent({
225225
required this.eventType,
226226
this.duration,
227227
this.size,
228-
this.rotation,
228+
this.rotationCorrection,
229229
this.buffered,
230230
});
231231

@@ -242,10 +242,10 @@ class VideoEvent {
242242
/// Only used if [eventType] is [VideoEventType.initialized].
243243
final Size? size;
244244

245-
/// Rotation of the video.
245+
/// Radians to rotate the video so it is displayed correctly.
246246
///
247247
/// Only used if [eventType] is [VideoEventType.initialized].
248-
final double? rotation;
248+
final double? rotationCorrection;
249249

250250
/// Buffered parts of the video.
251251
///

0 commit comments

Comments
 (0)