tasks[0] = spy(new DownloadTask.Builder("url1", "path", "filename1").build()); tasks[1] = spy(new DownloadTask.Builder("url2", "path", "filename1").build()); DownloadTask task = tasks[0]; when(task.isAutoCallbackToUIThread()).thenReturn(true);
DownloadTask build() { if (path == null) { path = FileDownloadUtils.getDefaultSaveFilePath(url); } @NonNull DownloadTask.Builder builder; if (pathAsDirectory) { builder = new DownloadTask.Builder(url, path, null); } else { builder = new DownloadTask.Builder(url, new File(path)); } builder.setMinIntervalMillisCallbackProcess(minIntervalMillisCallbackProgress); builder.setPassIfAlreadyCompleted(!forceReDownload); builder.setWifiRequired(isWifiRequired); for (Map.Entry<String, String> entry : headerMap.entrySet()) { builder.addHeader(entry.getKey(), entry.getValue()); } builder.setAutoCallbackToUIThread(autoCallbackToUIThread); DownloadTask task = builder.build(); if (tag != null) task.setTag(tag); if (keyOfTag != null) task.addTag(keyOfTag, tagWithKey); return task; } }
public Builder toBuilder(String anotherUrl, Uri anotherUri) { final Builder builder = new Builder(anotherUrl, anotherUri) .setPriority(priority) .setReadBufferSize(readBufferSize) .setFlushBufferSize(flushBufferSize) .setSyncBufferSize(syncBufferSize) .setSyncBufferIntervalMillis(syncBufferIntervalMills) .setAutoCallbackToUIThread(autoCallbackToUIThread) .setMinIntervalMillisCallbackProcess(minIntervalMillisCallbackProcess) .setHeaderMapFields(headerMapFields) .setPassIfAlreadyCompleted(passIfAlreadyCompleted); // check whether the filename is special set from method. if (Util.isUriFileScheme(anotherUri) // only if another uri is file-scheme && !new File(anotherUri.getPath()).isFile() // another uri is not file already && Util.isUriFileScheme(uri) // only if uri is file-scheme // only if filename is provided and not provided through uri && filenameHolder.get() != null && !new File(uri.getPath()).getName().equals(filenameHolder.get()) ) { builder.setFilename(filenameHolder.get()); } return builder; }
DownloadTask task = new DownloadTask .Builder("https://jacksgong.com", parentPath, filename) .build(); assertThat(task.getFilename()).isEqualTo(filename); assertThat(task.getFile().getAbsolutePath()) task = new DownloadTask .Builder("https://jacksgong.com", parentPath, filename) .setFilenameFromResponse(true) .build(); assertThat(task.getFilename()).isNull(); assertThat(task.getFile()).isNull(); task = new DownloadTask .Builder("https://jacksgong.com", new File(parentPath)) .setFilename(filename) .build(); assertThat(task.getFilename()).isEqualTo(filename); assertFile(task.getFile()).isEqualTo(new File(parentPath, filename)); task = new DownloadTask .Builder("https://jacksgong.com", new File(parentPath)) .build(); assertThat(task.getFilename()).isNull(); assertThat(task.getFile()).isNull(); task = new DownloadTask .Builder("https://jacksgong.com", new File("/not-exist")) .build();
DownloadTask task = new DownloadTask .Builder("url", uri) .setFilename(filename) .build(); when(anotherUri.getPath()).thenReturn(parentPath + filename); when(anotherUri.getScheme()).thenReturn(ContentResolver.SCHEME_FILE); DownloadTask anotherTask = new DownloadTask .Builder("url", anotherUri) .build(); assertThat(task.equals(anotherTask)).isTrue(); anotherTask = new DownloadTask .Builder("url", uri) .build(); anotherTask = new DownloadTask .Builder("url", uri) .setFilename("another-filename") .build(); DownloadTask noFilenameTask = new DownloadTask .Builder("url", uri) .build(); anotherTask = new DownloadTask .Builder("url", uri) .setFilename("another-filename") .build();
queueSet.setHeaderMapFields(headerMap); builder.bind(taskBuilder); verify(taskBuilder).setHeaderMapFields(headerMap); assertThat(queueSet.getReadBufferSize()).isEqualTo(readBufferSize); builder.bind(taskBuilder); verify(taskBuilder).setReadBufferSize(eq(readBufferSize)); assertThat(queueSet.getFlushBufferSize()).isEqualTo(flushBufferSize); builder.bind(taskBuilder); verify(taskBuilder).setFlushBufferSize(eq(flushBufferSize)); assertThat(queueSet.getSyncBufferSize()).isEqualTo(syncBufferSize); builder.bind(taskBuilder); verify(taskBuilder).setSyncBufferSize(eq(syncBufferSize)); assertThat(queueSet.getSyncBufferIntervalMillis()).isEqualTo(syncBufferIntervalMillis); builder.bind(taskBuilder); verify(taskBuilder).setSyncBufferIntervalMillis(eq(syncBufferIntervalMillis)); assertThat(queueSet.isAutoCallbackToUIThread()).isEqualTo(autoCallbackToUIThread); builder.bind(taskBuilder); verify(taskBuilder).setAutoCallbackToUIThread(eq(autoCallbackToUIThread)); builder.bind(taskBuilder); verify(taskBuilder) .setMinIntervalMillisCallbackProcess(eq(minIntervalMillisCallbackProgress)); assertThat(queueSet.getTag()).isEqualTo(tag);
DownloadTask task = new DownloadTask.Builder(url, uri) .setReadBufferSize(1) .setFlushBufferSize(2) .setSyncBufferSize(3) .setSyncBufferIntervalMillis(4) .setMinIntervalMillisCallbackProcess(5) .setAutoCallbackToUIThread(true) .setWifiRequired(true) .build(); assertThat(task.getReadBufferSize()).isEqualTo(1); assertThat(task.getFlushBufferSize()).isEqualTo(2); DownloadTask oldTask = new DownloadTask.Builder(url, uri).build(); DownloadTask newTask = new DownloadTask.Builder(url, uri).build(); oldTask.setTag("tag"); oldTask.addTag(0, "tag0");
tasks[0] = spy(new DownloadTask.Builder("url1", "path", "filename1").build()); tasks[1] = spy(new DownloadTask.Builder("url2", "path", "filename1").build()); DownloadTask task = tasks[0]; when(task.isAutoCallbackToUIThread()).thenReturn(true);
@Test public void start_withoutQueueListener() throws IOException { mockOkDownload(); // without queue listener final DownloadTask[] tasks = new DownloadTask[2]; tasks[0] = spy(new DownloadTask.Builder("url1", "path", "filename1").build()); tasks[1] = spy(new DownloadTask.Builder("url2", "path", "filename1").build()); context = spy(new DownloadContext(tasks, null, queueSet)); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) { final Runnable runnable = invocation.getArgument(0); runnable.run(); return null; } }).when(context).executeOnSerialExecutor(any(Runnable.class)); doNothing().when(tasks[0]).execute(any(DownloadListener.class)); doNothing().when(tasks[1]).execute(any(DownloadListener.class)); assertThat(context.isStarted()).isFalse(); context.start(listener, true); verify(context).executeOnSerialExecutor(any(Runnable.class)); verify(tasks[0]).execute(any(DownloadListener.class)); verify(tasks[1]).execute(any(DownloadListener.class)); assertThat(context.isStarted()).isTrue(); context.start(listener, false); final DownloadDispatcher dispatcher = OkDownload.with().downloadDispatcher(); verify(dispatcher).enqueue(tasks); assertThat(tasks[0].getListener()).isEqualTo(listener); assertThat(tasks[1].getListener()).isEqualTo(listener); }
@Test public void toBuilder() { // filename is provided specially => set filename final Uri uri = mock(Uri.class); when(uri.getPath()).thenReturn(parentPath); when(uri.getScheme()).thenReturn(ContentResolver.SCHEME_FILE); DownloadTask task = new DownloadTask .Builder("url", uri) .setFilename("filename1") .build(); DownloadTask buildTask = task.toBuilder().build(); assertThat(buildTask.getUrl()).isEqualTo("url"); assertThat(buildTask.getUri()).isEqualTo(uri); assertThat(buildTask.getFilename()).isEqualTo("filename1"); // another uri is file, use new filename final Uri anotherUri = mock(Uri.class); when(anotherUri.getScheme()).thenReturn(ContentResolver.SCHEME_FILE); when(anotherUri.getPath()).thenReturn(parentPath + filename); buildTask = task.toBuilder("anotherUrl", anotherUri).build(); assertThat(buildTask.getUrl()).isEqualTo("anotherUrl"); assertThat(buildTask.getUri()).isEqualTo(anotherUri); assertThat(buildTask.getFilename()).isEqualTo(filename); // same uri provided filename => same file when(uri.getPath()).thenReturn(parentPath + filename); task = new DownloadTask .Builder("url", uri) .build(); buildTask = task.toBuilder("anotherUrl", uri).build(); assertFile(buildTask.getFile()).isEqualTo(task.getFile()); }
@Test public void operation() { final DownloadDispatcher dispatcher = OkDownload.with().downloadDispatcher(); final String url = "url"; final Uri uri = mock(Uri.class); when(uri.getScheme()).thenReturn(ContentResolver.SCHEME_FILE); when(uri.getPath()).thenReturn("~/path"); DownloadTask task = new DownloadTask.Builder(url, uri).build(); // enqueue final DownloadListener listener = mock(DownloadListener.class); task.enqueue(listener); assertThat(task.getListener()).isEqualTo(listener); verify(dispatcher).enqueue(eq(task)); // cancel task.cancel(); verify(dispatcher).cancel(eq(task)); // execute task.execute(listener); assertThat(task.getListener()).isEqualTo(listener); verify(dispatcher).execute(eq(task)); }
@Test public void validInfoOnCompleted_InfoNotOnStore_FileScheme() throws IOException { mockOkDownload(); final DownloadTask task = spy( new DownloadTask.Builder("https://jacksgong.com", "path", "name") .build()); // null file doReturn(null).when(task).getFile(); strategy.validInfoOnCompleted(task, mock(DownloadStore.class)); BreakpointInfo info = task.getInfo(); assertThat(info.getId()).isEqualTo(task.getId()); assertThat(info.getTotalLength()).isEqualTo(0); assertThat(info.getTotalOffset()).isEqualTo(0); // valid file final File file = mock(File.class); doReturn(file).when(task).getFile(); doReturn(1L).when(file).length(); strategy.validInfoOnCompleted(task, mock(DownloadStore.class)); info = task.getInfo(); assertThat(info.getId()).isEqualTo(task.getId()); assertThat(info.getTotalLength()).isEqualTo(1L); assertThat(info.getTotalOffset()).isEqualTo(1L); }
@Test public void validInfoOnCompleted_InfoNotOnStore_UriScheme() { final Uri contentUri = mock(Uri.class); when(contentUri.getScheme()).thenReturn(ContentResolver.SCHEME_CONTENT); when(contentUri.getPath()).thenReturn("content://1"); final ContentResolver resolver = mock(ContentResolver.class); final OkDownload okDownload = OkDownload.with(); final Context context = mock(Context.class); when(okDownload.context()).thenReturn(context); when(context.getContentResolver()).thenReturn(resolver); final Cursor cursor = mock(Cursor.class); when(resolver.query(contentUri, null, null, null, null)).thenReturn(cursor); when(cursor.getLong(anyInt())).thenReturn(1L); final DownloadTask task = new DownloadTask.Builder("https://jacksgong.com", contentUri) .build(); strategy.validInfoOnCompleted(task, mock(DownloadStore.class)); final BreakpointInfo info = task.getInfo(); assertThat(info.getId()).isEqualTo(task.getId()); assertThat(info.getTotalLength()).isEqualTo(1L); assertThat(info.getTotalOffset()).isEqualTo(1L); }
@Test public void taskBuilder_constructWithFile() { final String url = "https://jacksgong.com"; final File noExistFile = new File(parentPath, "no-exist"); DownloadTask task = new DownloadTask.Builder(url, noExistFile).build(); assertThat(task.getFilename()).isEqualTo(noExistFile.getName()); assertThat(task.getFile().getAbsolutePath()).isEqualTo(noExistFile.getAbsolutePath()); final File existFile = new File(parentPath, filename); task = new DownloadTask.Builder(url, existFile).build(); assertThat(task.getFilename()).isEqualTo(existFile.getName()); assertThat(task.getFile().getAbsolutePath()).isEqualTo(existFile.getAbsolutePath()); final File existParentFile = new File(parentPath); task = new DownloadTask.Builder(url, existParentFile).build(); assertThat(task.getFilename()).isNull(); assertThat(task.getFile()).isNull(); assertFile(task.getParentFile()).isEqualTo(existParentFile); final File onlyFile = new File("/path"); task = new DownloadTask.Builder(url, onlyFile).build(); assertThat(task.getFilename()).isEqualTo("path"); assertFile(task.getFile()).isEqualTo(onlyFile); assertFile(task.getParentFile()).isEqualTo(new File("/")); }
@Test public void addHeader() throws Exception { final String url = "mock url"; final Uri mockFileUri = mock(Uri.class); when(mockFileUri.getScheme()).thenReturn(ContentResolver.SCHEME_FILE); when(mockFileUri.getPath()).thenReturn("mock path"); DownloadTask.Builder builder = new DownloadTask.Builder(url, mockFileUri); final String mockKey1 = "mock key1"; final String mockKey2 = "mock key2"; final String mockValue1 = "mock value1"; final String mockValue2 = "mock value2"; builder.addHeader(mockKey1, mockValue1); builder.addHeader(mockKey1, mockValue2); builder.addHeader(mockKey2, mockValue2); final Map<String, List<String>> headerMap = builder.build().getHeaderMapFields(); assertThat(headerMap).isNotNull(); assertThat(headerMap).containsKey(mockKey1).containsKey(mockKey2); final List<String> key1Values = headerMap.get(mockKey1); assertThat(key1Values).containsOnly(mockValue1, mockValue2); final List<String> key2Values = headerMap.get(mockKey2); assertThat(key2Values).containsOnly(mockValue2); }
public DownloadTask bind(@NonNull DownloadTask.Builder taskBuilder) { if (set.headerMapFields != null) taskBuilder.setHeaderMapFields(set.headerMapFields); if (set.readBufferSize != null) taskBuilder.setReadBufferSize(set.readBufferSize); if (set.flushBufferSize != null) taskBuilder.setFlushBufferSize(set.flushBufferSize); if (set.syncBufferSize != null) taskBuilder.setSyncBufferSize(set.syncBufferSize); if (set.wifiRequired != null) taskBuilder.setWifiRequired(set.wifiRequired); if (set.syncBufferIntervalMillis != null) { taskBuilder.setSyncBufferIntervalMillis(set.syncBufferIntervalMillis); } if (set.autoCallbackToUIThread != null) { taskBuilder.setAutoCallbackToUIThread(set.autoCallbackToUIThread); } if (set.minIntervalMillisCallbackProcess != null) { taskBuilder .setMinIntervalMillisCallbackProcess(set.minIntervalMillisCallbackProcess); } if (set.passIfAlreadyCompleted != null) { taskBuilder.setPassIfAlreadyCompleted(set.passIfAlreadyCompleted); } final DownloadTask task = taskBuilder.build(); if (set.tag != null) task.setTag(set.tag); boundTaskList.add(task); return task; }
@Test public void start_withQueueListener() throws IOException { mockOkDownload(); // with queue listener final DownloadTask[] tasks = new DownloadTask[2]; tasks[0] = new DownloadTask.Builder("url1", "path", "filename1").build(); tasks[1] = new DownloadTask.Builder("url2", "path", "filename1").build(); context = spy(new DownloadContext(tasks, queueListener, queueSet)); doNothing().when(context).executeOnSerialExecutor(any(Runnable.class)); context.start(listener, false); final DownloadDispatcher dispatcher = OkDownload.with().downloadDispatcher(); verify(dispatcher).enqueue(tasks); assertThat(tasks[0].getListener()).isEqualTo(tasks[1].getListener()); final DownloadListener taskListener = tasks[0].getListener(); assertThat(taskListener).isExactlyInstanceOf(DownloadListenerBunch.class); assertThat(((DownloadListenerBunch) taskListener).contain(listener)).isTrue(); // provided listener must callback before queue-attached-listener. assertThat(((DownloadListenerBunch) taskListener).indexOf(listener)).isZero(); }
@Test public void mockTaskForCompare() { DownloadTask task = new DownloadTask .Builder("https://jacksgong.com", parentPath, filename) .build(); IdentifiedTask identifiedTask = task.mock(0); assertThat(identifiedTask.compareIgnoreId(task)).isTrue(); task = new DownloadTask .Builder("https://www.jacksgong.com", new File(parentPath)) .build(); identifiedTask = task.mock(0); assertThat(identifiedTask.compareIgnoreId(task)).isTrue(); task = new DownloadTask .Builder("https://jacksgong.com", "non-exist-parent", "non-exist") .build(); identifiedTask = task.mock(0); assertThat(identifiedTask.compareIgnoreId(task)).isTrue(); }
private void initTask() { final String filename = "single-test"; final String url = "https://cdn.llscdn.com/yy/files/xs8qmxn8-lls-LLS-5.8-800-20171207-111607.apk"; final File parentFile = DemoUtil.getParentFile(this); task = new DownloadTask.Builder(url, parentFile) .setFilename(filename) // the minimal interval millisecond for callback progress .setMinIntervalMillisCallbackProcess(16) // ignore the same task has already completed in the past. .setPassIfAlreadyCompleted(false) .build(); }
public void setPriority(DownloadTask task, int priority) { final DownloadTask newTask = task.toBuilder().setPriority(priority).build(); this.context = context.toBuilder() .bindSetTask(newTask) .build(); newTask.setTags(task); TagUtil.savePriority(newTask, priority); this.taskList = Arrays.asList(this.context.getTasks()); }