|
4 | 4 |
|
5 | 5 | package io.flutter.plugins.videoplayer;
|
6 | 6 |
|
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import com.google.android.exoplayer2.ExoPlayer; |
| 13 | +import com.google.android.exoplayer2.Format; |
| 14 | +import io.flutter.plugin.common.EventChannel; |
| 15 | +import io.flutter.view.TextureRegistry; |
| 16 | +import java.util.HashMap; |
| 17 | +import org.junit.Before; |
7 | 18 | import org.junit.Test;
|
| 19 | +import org.junit.runner.RunWith; |
| 20 | +import org.mockito.ArgumentCaptor; |
| 21 | +import org.mockito.Captor; |
| 22 | +import org.mockito.MockitoAnnotations; |
| 23 | +import org.robolectric.RobolectricTestRunner; |
8 | 24 |
|
| 25 | +@RunWith(RobolectricTestRunner.class) |
9 | 26 | public class VideoPlayerTest {
|
10 |
| - // This is only a placeholder test and doesn't actually initialize the plugin. |
| 27 | + private ExoPlayer fakeExoPlayer; |
| 28 | + private EventChannel fakeEventChannel; |
| 29 | + private TextureRegistry.SurfaceTextureEntry fakeSurfaceTextureEntry; |
| 30 | + private VideoPlayerOptions fakeVideoPlayerOptions; |
| 31 | + private QueuingEventSink fakeEventSink; |
| 32 | + |
| 33 | + @Captor private ArgumentCaptor<HashMap<String, Object>> eventCaptor; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void before() { |
| 37 | + MockitoAnnotations.openMocks(this); |
| 38 | + |
| 39 | + fakeExoPlayer = mock(ExoPlayer.class); |
| 40 | + fakeEventChannel = mock(EventChannel.class); |
| 41 | + fakeSurfaceTextureEntry = mock(TextureRegistry.SurfaceTextureEntry.class); |
| 42 | + fakeVideoPlayerOptions = mock(VideoPlayerOptions.class); |
| 43 | + fakeEventSink = mock(QueuingEventSink.class); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void sendInitializedSendsExpectedEvent_90RotationDegrees() { |
| 48 | + VideoPlayer videoPlayer = |
| 49 | + new VideoPlayer( |
| 50 | + fakeExoPlayer, |
| 51 | + fakeEventChannel, |
| 52 | + fakeSurfaceTextureEntry, |
| 53 | + fakeVideoPlayerOptions, |
| 54 | + fakeEventSink); |
| 55 | + Format testFormat = |
| 56 | + new Format.Builder().setWidth(100).setHeight(200).setRotationDegrees(90).build(); |
| 57 | + |
| 58 | + when(fakeExoPlayer.getVideoFormat()).thenReturn(testFormat); |
| 59 | + when(fakeExoPlayer.getDuration()).thenReturn(10L); |
| 60 | + |
| 61 | + videoPlayer.isInitialized = true; |
| 62 | + videoPlayer.sendInitialized(); |
| 63 | + |
| 64 | + verify(fakeEventSink).success(eventCaptor.capture()); |
| 65 | + HashMap<String, Object> event = eventCaptor.getValue(); |
| 66 | + |
| 67 | + assertEquals(event.get("event"), "initialized"); |
| 68 | + assertEquals(event.get("duration"), 10L); |
| 69 | + assertEquals(event.get("width"), 200); |
| 70 | + assertEquals(event.get("height"), 100); |
| 71 | + assertEquals(event.get("rotationCorrection"), null); |
| 72 | + } |
| 73 | + |
11 | 74 | @Test
|
12 |
| - public void initPluginDoesNotThrow() { |
13 |
| - final VideoPlayerPlugin plugin = new VideoPlayerPlugin(); |
| 75 | + public void sendInitializedSendsExpectedEvent_270RotationDegrees() { |
| 76 | + VideoPlayer videoPlayer = |
| 77 | + new VideoPlayer( |
| 78 | + fakeExoPlayer, |
| 79 | + fakeEventChannel, |
| 80 | + fakeSurfaceTextureEntry, |
| 81 | + fakeVideoPlayerOptions, |
| 82 | + fakeEventSink); |
| 83 | + Format testFormat = |
| 84 | + new Format.Builder().setWidth(100).setHeight(200).setRotationDegrees(270).build(); |
| 85 | + |
| 86 | + when(fakeExoPlayer.getVideoFormat()).thenReturn(testFormat); |
| 87 | + when(fakeExoPlayer.getDuration()).thenReturn(10L); |
| 88 | + |
| 89 | + videoPlayer.isInitialized = true; |
| 90 | + videoPlayer.sendInitialized(); |
| 91 | + |
| 92 | + verify(fakeEventSink).success(eventCaptor.capture()); |
| 93 | + HashMap<String, Object> event = eventCaptor.getValue(); |
| 94 | + |
| 95 | + assertEquals(event.get("event"), "initialized"); |
| 96 | + assertEquals(event.get("duration"), 10L); |
| 97 | + assertEquals(event.get("width"), 200); |
| 98 | + assertEquals(event.get("height"), 100); |
| 99 | + assertEquals(event.get("rotationCorrection"), null); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void sendInitializedSendsExpectedEvent_0RotationDegrees() { |
| 104 | + VideoPlayer videoPlayer = |
| 105 | + new VideoPlayer( |
| 106 | + fakeExoPlayer, |
| 107 | + fakeEventChannel, |
| 108 | + fakeSurfaceTextureEntry, |
| 109 | + fakeVideoPlayerOptions, |
| 110 | + fakeEventSink); |
| 111 | + Format testFormat = |
| 112 | + new Format.Builder().setWidth(100).setHeight(200).setRotationDegrees(0).build(); |
| 113 | + |
| 114 | + when(fakeExoPlayer.getVideoFormat()).thenReturn(testFormat); |
| 115 | + when(fakeExoPlayer.getDuration()).thenReturn(10L); |
| 116 | + |
| 117 | + videoPlayer.isInitialized = true; |
| 118 | + videoPlayer.sendInitialized(); |
| 119 | + |
| 120 | + verify(fakeEventSink).success(eventCaptor.capture()); |
| 121 | + HashMap<String, Object> event = eventCaptor.getValue(); |
| 122 | + |
| 123 | + assertEquals(event.get("event"), "initialized"); |
| 124 | + assertEquals(event.get("duration"), 10L); |
| 125 | + assertEquals(event.get("width"), 100); |
| 126 | + assertEquals(event.get("height"), 200); |
| 127 | + assertEquals(event.get("rotationCorrection"), null); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void sendInitializedSendsExpectedEvent_180RotationDegrees() { |
| 132 | + VideoPlayer videoPlayer = |
| 133 | + new VideoPlayer( |
| 134 | + fakeExoPlayer, |
| 135 | + fakeEventChannel, |
| 136 | + fakeSurfaceTextureEntry, |
| 137 | + fakeVideoPlayerOptions, |
| 138 | + fakeEventSink); |
| 139 | + Format testFormat = |
| 140 | + new Format.Builder().setWidth(100).setHeight(200).setRotationDegrees(180).build(); |
| 141 | + |
| 142 | + when(fakeExoPlayer.getVideoFormat()).thenReturn(testFormat); |
| 143 | + when(fakeExoPlayer.getDuration()).thenReturn(10L); |
| 144 | + |
| 145 | + videoPlayer.isInitialized = true; |
| 146 | + videoPlayer.sendInitialized(); |
| 147 | + |
| 148 | + verify(fakeEventSink).success(eventCaptor.capture()); |
| 149 | + HashMap<String, Object> event = eventCaptor.getValue(); |
| 150 | + |
| 151 | + assertEquals(event.get("event"), "initialized"); |
| 152 | + assertEquals(event.get("duration"), 10L); |
| 153 | + assertEquals(event.get("width"), 100); |
| 154 | + assertEquals(event.get("height"), 200); |
| 155 | + assertEquals(event.get("rotationCorrection"), 180); |
14 | 156 | }
|
15 | 157 | }
|
0 commit comments