|
12 | 12 | import static org.mockito.Mockito.lenient; |
13 | 13 | import static org.mockito.Mockito.mock; |
14 | 14 | import static org.mockito.Mockito.mockStatic; |
| 15 | +import static org.mockito.Mockito.never; |
15 | 16 | import static org.mockito.Mockito.verify; |
16 | 17 | import static org.mockito.Mockito.when; |
17 | 18 |
|
@@ -288,6 +289,60 @@ public void openFilesCompletesWithError_whenSecurityExceptionInGetPathFromCopyOf |
288 | 289 | // Regression test for https://github.com/flutter/flutter/issues/159568: the |
289 | 290 | // single-file `openFile` path must likewise surface a copy failure to Dart |
290 | 291 | // instead of crashing. |
| 292 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 293 | + @Test |
| 294 | + public void openFileCompletesWithError_whenProviderReturnsNullStream() |
| 295 | + throws FileNotFoundException { |
| 296 | + final ContentResolver mockContentResolver = mock(ContentResolver.class); |
| 297 | + final Uri mockUri = mock(Uri.class); |
| 298 | + |
| 299 | + final Cursor mockCursor = mock(Cursor.class); |
| 300 | + when(mockCursor.moveToFirst()).thenReturn(true); |
| 301 | + when(mockCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)).thenReturn(0); |
| 302 | + when(mockCursor.getString(0)).thenReturn("filename"); |
| 303 | + when(mockCursor.getColumnIndex(OpenableColumns.SIZE)).thenReturn(1); |
| 304 | + when(mockCursor.isNull(1)).thenReturn(false); |
| 305 | + when(mockCursor.getInt(1)).thenReturn(30); |
| 306 | + when(mockContentResolver.query(mockUri, null, null, null, null, null)).thenReturn(mockCursor); |
| 307 | + // A provider that cannot serve the file answers the open with null; previously this reached |
| 308 | + // `DataInputStream#readFully` and threw a NullPointerException on the main thread. |
| 309 | + when(mockContentResolver.openInputStream(mockUri)).thenReturn(null); |
| 310 | + |
| 311 | + when(mockObjectFactory.newIntent(Intent.ACTION_OPEN_DOCUMENT)).thenReturn(mockIntent); |
| 312 | + when(mockActivity.getContentResolver()).thenReturn(mockContentResolver); |
| 313 | + when(mockActivityBinding.getActivity()).thenReturn(mockActivity); |
| 314 | + final FileSelectorApiImpl fileSelectorApi = |
| 315 | + new FileSelectorApiImpl( |
| 316 | + mockActivityBinding, mockObjectFactory, (version) -> Build.VERSION.SDK_INT >= version); |
| 317 | + |
| 318 | + final boolean[] callbackCalled = new boolean[1]; |
| 319 | + final Throwable[] failure = new Throwable[1]; |
| 320 | + fileSelectorApi.openFile( |
| 321 | + null, |
| 322 | + new FileTypes(Collections.emptyList(), Collections.emptyList()), |
| 323 | + ResultCompat.asCompatCallback( |
| 324 | + (reply) -> { |
| 325 | + callbackCalled[0] = true; |
| 326 | + failure[0] = reply.exceptionOrNull(); |
| 327 | + return null; |
| 328 | + })); |
| 329 | + |
| 330 | + verify(mockActivity).startActivityForResult(mockIntent, 221); |
| 331 | + |
| 332 | + final ArgumentCaptor<PluginRegistry.ActivityResultListener> listenerArgumentCaptor = |
| 333 | + ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class); |
| 334 | + verify(mockActivityBinding).addActivityResultListener(listenerArgumentCaptor.capture()); |
| 335 | + |
| 336 | + final Intent resultMockIntent = mock(Intent.class); |
| 337 | + when(resultMockIntent.getData()).thenReturn(mockUri); |
| 338 | + listenerArgumentCaptor.getValue().onActivityResult(221, Activity.RESULT_OK, resultMockIntent); |
| 339 | + |
| 340 | + assertTrue(callbackCalled[0]); |
| 341 | + assertNotNull(failure[0]); |
| 342 | + assertTrue(failure[0].getMessage().contains("Failed to read file")); |
| 343 | + verify(mockObjectFactory, never()).newDataInputStream(any()); |
| 344 | + } |
| 345 | + |
291 | 346 | @SuppressWarnings({"rawtypes", "unchecked"}) |
292 | 347 | @Test |
293 | 348 | public void openFileCompletesWithError_whenSecurityExceptionInGetPathFromCopyOfFileFromUri() |
|
0 commit comments