|
11 | 11 | import static org.mockito.Mockito.reset; |
12 | 12 | import static org.mockito.Mockito.spy; |
13 | 13 | import static org.mockito.Mockito.verify; |
| 14 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
14 | 15 | import static org.mockito.Mockito.when; |
15 | 16 |
|
16 | | -import android.graphics.SurfaceTexture; |
17 | 17 | import android.util.Size; |
18 | 18 | import android.view.Surface; |
19 | 19 | import androidx.camera.core.Preview; |
@@ -83,62 +83,93 @@ public void create_createsPreviewWithCorrectConfiguration() { |
83 | 83 | } |
84 | 84 |
|
85 | 85 | @Test |
86 | | - public void setSurfaceProviderTest_createsSurfaceProviderAndReturnsTextureEntryId() { |
| 86 | + public void setSurfaceProvider_createsSurfaceProviderAndReturnsTextureEntryId() { |
87 | 87 | final PreviewHostApiImpl previewHostApi = |
88 | 88 | spy(new PreviewHostApiImpl(mockBinaryMessenger, testInstanceManager, mockTextureRegistry)); |
89 | | - final TextureRegistry.SurfaceTextureEntry mockSurfaceTextureEntry = |
90 | | - mock(TextureRegistry.SurfaceTextureEntry.class); |
91 | | - final SurfaceTexture mockSurfaceTexture = mock(SurfaceTexture.class); |
| 89 | + final TextureRegistry.SurfaceProducer mockSurfaceProducer = |
| 90 | + mock(TextureRegistry.SurfaceProducer.class); |
92 | 91 | final Long previewIdentifier = 5L; |
93 | | - final Long surfaceTextureEntryId = 120L; |
| 92 | + final Long surfaceProducerEntryId = 120L; |
94 | 93 |
|
95 | 94 | previewHostApi.cameraXProxy = mockCameraXProxy; |
96 | 95 | testInstanceManager.addDartCreatedInstance(mockPreview, previewIdentifier); |
97 | 96 |
|
98 | | - when(mockTextureRegistry.createSurfaceTexture()).thenReturn(mockSurfaceTextureEntry); |
99 | | - when(mockSurfaceTextureEntry.surfaceTexture()).thenReturn(mockSurfaceTexture); |
100 | | - when(mockSurfaceTextureEntry.id()).thenReturn(surfaceTextureEntryId); |
| 97 | + when(mockTextureRegistry.createSurfaceProducer()).thenReturn(mockSurfaceProducer); |
| 98 | + when(mockSurfaceProducer.id()).thenReturn(surfaceProducerEntryId); |
101 | 99 |
|
102 | 100 | final ArgumentCaptor<Preview.SurfaceProvider> surfaceProviderCaptor = |
103 | 101 | ArgumentCaptor.forClass(Preview.SurfaceProvider.class); |
104 | | - final ArgumentCaptor<Surface> surfaceCaptor = ArgumentCaptor.forClass(Surface.class); |
105 | 102 |
|
106 | 103 | // Test that surface provider was set and the surface texture ID was returned. |
107 | | - assertEquals(previewHostApi.setSurfaceProvider(previewIdentifier), surfaceTextureEntryId); |
| 104 | + assertEquals(previewHostApi.setSurfaceProvider(previewIdentifier), surfaceProducerEntryId); |
108 | 105 | verify(mockPreview).setSurfaceProvider(surfaceProviderCaptor.capture()); |
109 | | - verify(previewHostApi).createSurfaceProvider(mockSurfaceTexture); |
| 106 | + verify(previewHostApi).createSurfaceProvider(mockSurfaceProducer); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void createSurfaceProducer_setsExpectedSurfaceProducerCallback() { |
| 111 | + final PreviewHostApiImpl previewHostApi = |
| 112 | + new PreviewHostApiImpl(mockBinaryMessenger, testInstanceManager, mockTextureRegistry); |
| 113 | + final TextureRegistry.SurfaceProducer mockSurfaceProducer = |
| 114 | + mock(TextureRegistry.SurfaceProducer.class); |
| 115 | + final SurfaceRequest mockSurfaceRequest = mock(SurfaceRequest.class); |
| 116 | + final ArgumentCaptor<TextureRegistry.SurfaceProducer.Callback> callbackCaptor = |
| 117 | + ArgumentCaptor.forClass(TextureRegistry.SurfaceProducer.Callback.class); |
| 118 | + |
| 119 | + when(mockSurfaceRequest.getResolution()).thenReturn(new Size(5, 6)); |
| 120 | + when(mockSurfaceProducer.getSurface()).thenReturn(mock(Surface.class)); |
| 121 | + |
| 122 | + Preview.SurfaceProvider previewSurfaceProvider = |
| 123 | + previewHostApi.createSurfaceProvider(mockSurfaceProducer); |
| 124 | + previewSurfaceProvider.onSurfaceRequested(mockSurfaceRequest); |
| 125 | + |
| 126 | + verify(mockSurfaceProducer).setCallback(callbackCaptor.capture()); |
| 127 | + |
| 128 | + TextureRegistry.SurfaceProducer.Callback callback = callbackCaptor.getValue(); |
| 129 | + |
| 130 | + // Verify callback's onSurfaceDestroyed invalidates SurfaceRequest. |
| 131 | + callback.onSurfaceDestroyed(); |
| 132 | + verify(mockSurfaceRequest).invalidate(); |
| 133 | + |
| 134 | + reset(mockSurfaceRequest); |
| 135 | + |
| 136 | + // Verify callback's onSurfaceCreated does not interact with the SurfaceRequest. |
| 137 | + callback.onSurfaceCreated(); |
| 138 | + verifyNoMoreInteractions(mockSurfaceRequest); |
110 | 139 | } |
111 | 140 |
|
112 | 141 | @Test |
113 | 142 | public void createSurfaceProvider_createsExpectedPreviewSurfaceProvider() { |
114 | 143 | final PreviewHostApiImpl previewHostApi = |
115 | 144 | new PreviewHostApiImpl(mockBinaryMessenger, testInstanceManager, mockTextureRegistry); |
116 | | - final SurfaceTexture mockSurfaceTexture = mock(SurfaceTexture.class); |
| 145 | + final TextureRegistry.SurfaceProducer mockSurfaceProducer = |
| 146 | + mock(TextureRegistry.SurfaceProducer.class); |
117 | 147 | final Surface mockSurface = mock(Surface.class); |
118 | 148 | final SurfaceRequest mockSurfaceRequest = mock(SurfaceRequest.class); |
119 | 149 | final SurfaceRequest.Result mockSurfaceRequestResult = mock(SurfaceRequest.Result.class); |
120 | 150 | final SystemServicesFlutterApiImpl mockSystemServicesFlutterApi = |
121 | 151 | mock(SystemServicesFlutterApiImpl.class); |
122 | 152 | final int resolutionWidth = 200; |
123 | 153 | final int resolutionHeight = 500; |
| 154 | + final Long surfaceProducerEntryId = 120L; |
124 | 155 |
|
125 | 156 | previewHostApi.cameraXProxy = mockCameraXProxy; |
126 | | - when(mockCameraXProxy.createSurface(mockSurfaceTexture)).thenReturn(mockSurface); |
127 | 157 | when(mockSurfaceRequest.getResolution()) |
128 | 158 | .thenReturn(new Size(resolutionWidth, resolutionHeight)); |
129 | 159 | when(mockCameraXProxy.createSystemServicesFlutterApiImpl(mockBinaryMessenger)) |
130 | 160 | .thenReturn(mockSystemServicesFlutterApi); |
| 161 | + when(mockSurfaceProducer.getSurface()).thenReturn(mockSurface); |
131 | 162 |
|
132 | 163 | final ArgumentCaptor<Surface> surfaceCaptor = ArgumentCaptor.forClass(Surface.class); |
133 | 164 | @SuppressWarnings("unchecked") |
134 | 165 | final ArgumentCaptor<Consumer<SurfaceRequest.Result>> consumerCaptor = |
135 | 166 | ArgumentCaptor.forClass(Consumer.class); |
136 | 167 |
|
137 | 168 | Preview.SurfaceProvider previewSurfaceProvider = |
138 | | - previewHostApi.createSurfaceProvider(mockSurfaceTexture); |
| 169 | + previewHostApi.createSurfaceProvider(mockSurfaceProducer); |
139 | 170 | previewSurfaceProvider.onSurfaceRequested(mockSurfaceRequest); |
140 | 171 |
|
141 | | - verify(mockSurfaceTexture).setDefaultBufferSize(resolutionWidth, resolutionHeight); |
| 172 | + verify(mockSurfaceProducer).setSize(resolutionWidth, resolutionHeight); |
142 | 173 | verify(mockSurfaceRequest) |
143 | 174 | .provideSurface(surfaceCaptor.capture(), any(Executor.class), consumerCaptor.capture()); |
144 | 175 |
|
@@ -189,13 +220,13 @@ public void createSurfaceProvider_createsExpectedPreviewSurfaceProvider() { |
189 | 220 | public void releaseFlutterSurfaceTexture_makesCallToReleaseFlutterSurfaceTexture() { |
190 | 221 | final PreviewHostApiImpl previewHostApi = |
191 | 222 | new PreviewHostApiImpl(mockBinaryMessenger, testInstanceManager, mockTextureRegistry); |
192 | | - final TextureRegistry.SurfaceTextureEntry mockSurfaceTextureEntry = |
193 | | - mock(TextureRegistry.SurfaceTextureEntry.class); |
| 223 | + final TextureRegistry.SurfaceProducer mockSurfaceProducer = |
| 224 | + mock(TextureRegistry.SurfaceProducer.class); |
194 | 225 |
|
195 | | - previewHostApi.flutterSurfaceTexture = mockSurfaceTextureEntry; |
| 226 | + previewHostApi.flutterSurfaceProducer = mockSurfaceProducer; |
196 | 227 |
|
197 | 228 | previewHostApi.releaseFlutterSurfaceTexture(); |
198 | | - verify(mockSurfaceTextureEntry).release(); |
| 229 | + verify(mockSurfaceProducer).release(); |
199 | 230 | } |
200 | 231 |
|
201 | 232 | @Test |
|
0 commit comments