@NonNull static DownloadTask createFinder(@NonNull String url, @NonNull String parentPath, @Nullable String filename) { return new DownloadTask.Builder(url, parentPath, filename) .build(); }
@Test public void setHeaderMapFields() { 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 Map<String, List<String>> headerMap = new HashMap<>(); builder.setHeaderMapFields(headerMap); assertThat(builder.build().getHeaderMapFields()).isEqualTo(headerMap); }
@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 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 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); }
@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(); }
private void initTask() { task = new DownloadTask .Builder(DemoUtil.URL, DemoUtil.getParentFile(this)) .setFilename("notification-file.apk") // if there is the same task has been completed download, just delete it and // re-download automatically. .setPassIfAlreadyCompleted(false) .setMinIntervalMillisCallbackProcess(80) // because for the notification we don't need make sure invoke on the ui thread, so // just let callback no need callback to the ui thread. .setAutoCallbackToUIThread(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()); }
@Test public void validInfoOnCompleted_storeValid() { final DownloadStore store = mock(DownloadStore.class); final DownloadTask task = new DownloadTask.Builder("https://jacksgong.com", "path", "name") .build(); when(store.getAfterCompleted(task.getId())).thenReturn(info); strategy.validInfoOnCompleted(task, store); assertThat(task.getInfo()).isEqualTo(info); }
@Test public void enqueue() { final DownloadTask[] tasks = new DownloadTask[2]; tasks[0] = new DownloadTask.Builder("url1", "path", "filename1").build(); tasks[1] = new DownloadTask.Builder("url2", "path", "filename1").build(); final DownloadListener listener = mock(DownloadListener.class); DownloadTask.enqueue(tasks, listener); assertThat(tasks[0].getListener()).isEqualTo(listener); assertThat(tasks[1].getListener()).isEqualTo(listener); verify(OkDownload.with().downloadDispatcher()).enqueue(eq(tasks)); }
private DownloadTask createTask(String url) { final String filename = "each-block-progress-test"; final File parentFile = DemoUtil.getParentFile(this); return new DownloadTask.Builder(url, parentFile) .setFilename(filename) // the minimal interval millisecond for callback progress .setMinIntervalMillisCallbackProcess(64) // ignore the same task has already completed in the past. .setPassIfAlreadyCompleted(false) .build(); }
@Test public void fetchProgress_setMinInterval() { final DownloadTask task = spy(new DownloadTask .Builder("https://jacksgong.com", "parentPath", "filename") .setMinIntervalMillisCallbackProcess(1) .build()); final DownloadListener listener = mock(DownloadListener.class); when(task.getListener()).thenReturn(listener); dispatcher.dispatch().fetchProgress(task, 1, 2); assertThat(DownloadTask.TaskHideWrapper.getLastCallbackProcessTs(task)).isNotZero(); } }
@Test public void getInfo() throws IOException { mockOkDownload(); final BreakpointInfo info = mock(BreakpointInfo.class); final BreakpointStore store = OkDownload.with().breakpointStore(); when(store.get(1)).thenReturn(info); when(store.findOrCreateId(any(DownloadTask.class))).thenReturn(1); final DownloadTask task = new DownloadTask .Builder("https://jacksgong.com", new File(parentPath)) .build(); assertThat(task.getInfo()).isEqualTo(info); }
@Test public void redirectLocation() { final String redirectLocation = "http://redirect"; DownloadTask task = new DownloadTask.Builder("https://jacksgong.com", new File(parentPath, filename)).build(); task.setRedirectLocation(redirectLocation); assertThat(task.getRedirectLocation()).isEqualTo(redirectLocation); }
private void handleUri(Uri uri) { DownloadTask task = new DownloadTask.Builder(DemoUtil.URL, uri) .setMinIntervalMillisCallbackProcess(300) .build(); this.task = task; filenameTv.setText(task.getFilename()); task.enqueue(new SampleListener()); }
@Test public void replaceListener() { DownloadTask task = new DownloadTask.Builder("https://jacksgong.com", new File(parentPath, filename)).build(); final DownloadListener listener = mock(DownloadListener.class); task.enqueue(listener); assertThat(task.getListener()).isEqualTo(listener); final DownloadListener another = mock(DownloadListener.class); task.replaceListener(another); assertThat(task.getListener()).isEqualTo(another); }
@Test public void taskToString() { DownloadTask task = new DownloadTask.Builder("https://jacksgong.com", new File(parentPath, filename)).build(); assertThat(task.toString()) .endsWith("@" + task.getId() + "@https://jacksgong.com@" + new File(parentPath, filename).getAbsolutePath()); }