public void startOnParallel(DownloadListener listener) { start(listener, false); }
public DownloadContext build() { DownloadTask[] tasks = new DownloadTask[boundTaskList.size()]; return new DownloadContext(boundTaskList.toArray(tasks), listener, set); } }
context = spy(new DownloadContext(tasks, queueListener, queueSet, handler)); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) { return null; }).when(context).executeOnSerialExecutor(any(Runnable.class)); doNothing().when(tasks[0]).execute(any(DownloadListener.class)); doNothing().when(tasks[1]).execute(any(DownloadListener.class)); when(context.isStarted()).thenReturn(true, false); context.start(listener, true); verify(context).executeOnSerialExecutor(any(Runnable.class)); verify(tasks[0]).execute(any(DownloadListener.class)); verify(tasks[1], never()).execute(any(DownloadListener.class));
@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 startOnSerial() { doNothing().when(context).start(eq(listener), anyBoolean()); context.startOnSerial(listener); verify(context).start(eq(listener), eq(true)); }
@Test public void startOnParallel() { doNothing().when(context).start(eq(listener), anyBoolean()); context.startOnParallel(listener); verify(context).start(eq(listener), eq(false)); }
@Override public void run() { for (DownloadTask task : scheduleTaskList) { if (!isStarted()) { callbackQueueEndOnSerialLoop(task.isAutoCallbackToUIThread()); break; } task.execute(targetListener); } } });
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()); }
Collections.addAll(scheduleTaskList, tasks); Collections.sort(scheduleTaskList); executeOnSerialExecutor(new Runnable() { @Override public void run() { for (DownloadTask task : scheduleTaskList) {
@Test public void replaceTask() { final DownloadTask oldTask = tasks[0]; final DownloadTask newTask = mock(DownloadTask.class); context.alter().replaceTask(oldTask, newTask); assertThat(tasks[0]).isEqualTo(newTask); }
this.taskList = Arrays.asList(this.context.getTasks());
context = spy(new DownloadContext(tasks, queueListener, queueSet, handler)); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) { return null; }).when(context).executeOnSerialExecutor(any(Runnable.class)); doNothing().when(tasks[0]).execute(any(DownloadListener.class)); doNothing().when(tasks[1]).execute(any(DownloadListener.class)); when(context.isStarted()).thenReturn(false); context.start(listener, true); verify(context).executeOnSerialExecutor(any(Runnable.class)); verify(tasks[0], never()).execute(any(DownloadListener.class)); verify(tasks[1], never()).execute(any(DownloadListener.class));
.setText(holder.priorityTv.getContext().getString(R.string.priority, priority)); holder.prioritySb.setProgress(priority); if (this.context.isStarted()) { holder.prioritySb.setEnabled(false); } else {
@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); }
public void start(boolean isSerial) { this.context.start(listener, isSerial); }
@Before public void setup() { initMocks(this); tasks = new DownloadTask[3]; tasks[0] = mock(DownloadTask.class); tasks[1] = mock(DownloadTask.class); tasks[2] = mock(DownloadTask.class); queueSet = new DownloadContext.QueueSet(); context = spy(new DownloadContext(tasks, queueListener, queueSet)); builder = spy(new DownloadContext.Builder(queueSet)); }
public void startOnSerial(DownloadListener listener) { start(listener, true); }