ParseFile implements Parcelable { public Task<InputStream> getDataStreamInBackground(final ProgressCallback progressCallback) { final TaskCompletionSource<Void> cts = new TaskCompletionSource<>(); currentTasks.add(cts); return taskQueue.enqueue(new Continuation<Void, Task<InputStream>>() { @Override public Task<InputStream> then(Task<Void> toAwait) { return fetchInBackground(progressCallback, toAwait, cts.getTask()).onSuccess(new Continuation<File, InputStream>() { @Override public InputStream then(Task<File> task) throws Exception { return new FileInputStream(task.getResult()); } }); } }).continueWithTask(new Continuation<InputStream, Task<InputStream>>() { @Override public Task<InputStream> then(Task<InputStream> task) { cts.trySetResult(null); currentTasks.remove(cts); return task; } }); } ParseFile(File file);  ParseFile(File file, String contentType);  ParseFile(String name, byte[] data, String contentType);  ParseFile(byte[] data);  ParseFile(String name, byte[] data);  ParseFile(byte[] data, String contentType); protected  ParseFile(Parcel source);   ParseFile(Parcel source, ParseParcelDecoder decoder);   ParseFile(State state); @SuppressWarnings("unused")  ParseFile(JSONObject json, ParseDecoder decoder); String getName(); boolean isDirty(); boolean isDataAvailable(); String getUrl(); void save(); Task<Void> saveInBackground(final ProgressCallback uploadProgressCallback); Task<Void> saveInBackground(); void saveInBackground(final SaveCallback saveCallback,
                                 ProgressCallback progressCallback); void saveInBackground(SaveCallback callback); byte[] getData(); Task<byte[]> getDataInBackground(final ProgressCallback progressCallback); Task<byte[]> getDataInBackground(); void getDataInBackground(GetDataCallback dataCallback,
                                    final ProgressCallback progressCallback); void getDataInBackground(GetDataCallback dataCallback); File getFile(); Task<File> getFileInBackground(final ProgressCallback progressCallback); Task<File> getFileInBackground(); void getFileInBackground(GetFileCallback fileCallback,
                                    final ProgressCallback progressCallback); void getFileInBackground(GetFileCallback fileCallback); InputStream getDataStream(); Task<InputStream> getDataStreamInBackground(final ProgressCallback progressCallback); Task<InputStream> getDataStreamInBackground(); void getDataStreamInBackground(GetDataStreamCallback dataStreamCallback,
                                          final ProgressCallback progressCallback); void getDataStreamInBackground(GetDataStreamCallback dataStreamCallback); void cancel(); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); final static Creator<ParseFile> CREATOR; }
@Test public void testGetDataStreamAsyncSuccess() throws Exception { String content = "content"; File file = temporaryFolder.newFile("test"); ParseFileUtils.writeStringToFile(file, content, "UTF-8"); ParseFileController controller = mock(ParseFileController.class); when(controller.fetchAsync( any(ParseFile.State.class), any(String.class), any(ProgressCallback.class), Matchers.<Task<Void>>any())).thenReturn(Task.forResult(file)); ParseCorePlugins.getInstance().registerFileController(controller); String url = "url"; ParseFile.State state = new ParseFile.State.Builder() .url(url) .build(); ParseFile parseFile = new ParseFile(state); InputStream dataStream = ParseTaskUtils.wait(parseFile.getDataStreamInBackground()); ArgumentCaptor<ParseFile.State> stateCaptor = ArgumentCaptor.forClass(ParseFile.State.class); verify(controller, times(1)).fetchAsync( stateCaptor.capture(), anyString(), any(ProgressCallback.class), Matchers.<Task<Void>>any() ); assertEquals(url, stateCaptor.getValue().url()); assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStream)); InputStream dataStreamAgain = ParseTaskUtils.wait(parseFile.getDataStreamInBackground()); ArgumentCaptor<ParseFile.State> stateCaptorAgain = ArgumentCaptor.forClass(ParseFile.State.class); verify(controller, times(2)).fetchAsync( stateCaptorAgain.capture(), anyString(), any(ProgressCallback.class), Matchers.<Task<Void>>any() ); assertEquals(url, stateCaptorAgain.getValue().url()); assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStreamAgain)); }