org.robolectric.util.Scheduler.post()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(122)

本文整理了Java中org.robolectric.util.Scheduler.post()方法的一些代码示例,展示了Scheduler.post()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Scheduler.post()方法的具体详情如下:
包路径:org.robolectric.util.Scheduler
类名称:Scheduler
方法名:post

Scheduler.post介绍

[英]Add a runnable to the queue.
[中]将runnable添加到队列中。

代码示例

代码示例来源:origin: robolectric/robolectric

private <T> Future<T> schedule(final FutureTask<T> futureTask) {
 Runnable runnable = new Runnable() {
  @Override
  public void run() {
   futureTask.run();
   runnables.remove(this);
  }
 };
 runnables.add(runnable);
 scheduler.post(runnable);
 return futureTask;
}

代码示例来源:origin: robolectric/robolectric

@Test
public void postAtFrontOfQueue_addsJobAtFrontOfQueue() throws Exception {
 scheduler.post(new AddToTranscript("one"));
 scheduler.post(new AddToTranscript("two"));
 scheduler.postAtFrontOfQueue(new AddToTranscript("three"));
 scheduler.runOneTask();
 assertThat(transcript).containsExactly("three");
 transcript.clear();
 scheduler.runOneTask();
 assertThat(transcript).containsExactly("one");
 transcript.clear();
 scheduler.runOneTask();
 assertThat(transcript).containsExactly("two");
}

代码示例来源:origin: robolectric/robolectric

@Override protected void done() {
  try {
   final D result = get();
   Robolectric.getForegroundThreadScheduler().post(new Runnable() {
    @Override public void run() {
     realLoader.deliverResult(result);
    }
   });
  } catch (InterruptedException e) {
   // Ignore
  } catch (ExecutionException e) {
   throw new RuntimeException(e.getCause());
  }
 }
};

代码示例来源:origin: robolectric/robolectric

@Override
 public void run() {
  order.add(1);
  scheduler.post(new Runnable() {
   @Override
   public void run() {
    order.add(4);
   }
  });
  order.add(2);
 }
}, 0);

代码示例来源:origin: robolectric/robolectric

@Override
 public void run() {
  order.add(1);
  scheduler.post(new Runnable() {
   @Override
   public void run() {
    order.add(3);
   }
  });
  order.add(2);
 }
}, 0);

代码示例来源:origin: robolectric/robolectric

@Override
 protected void done() {
  try {
   final D result = get();
   ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
    @Override
    public void run() {
     realObject.deliverResult(result);
    }
   });
  } catch (InterruptedException e) {
   // Ignore
  } catch (ExecutionException e) {
   throw new RuntimeException(e.getCause());
  }
 }
};

代码示例来源:origin: robolectric/robolectric

@Implementation
protected void onForceLoad() {
 FutureTask<D> future = new FutureTask<D>(worker) {
  @Override protected void done() {
   try {
    final D result = get();
    Robolectric.getForegroundThreadScheduler().post(new Runnable() {
     @Override public void run() {
      realLoader.deliverResult(result);
     }
    });
   } catch (InterruptedException e) {
    // Ignore
   } catch (ExecutionException e) {
    throw new RuntimeException(e.getCause());
   }
  }
 };
 Robolectric.getBackgroundThreadScheduler().post(future);
}

代码示例来源:origin: robolectric/robolectric

@Implementation
protected boolean post(Runnable action) {
 ShadowApplication.getInstance().getForegroundThreadScheduler().post(action);
 return true;
}

代码示例来源:origin: robolectric/robolectric

@Implementation
protected void runOnUiThread(Runnable action) {
 ShadowApplication.getInstance().getForegroundThreadScheduler().post(action);
}

代码示例来源:origin: robolectric/robolectric

/**
 * Enqueue a call to {@link AsyncTask#onProgressUpdate(Object[])} on UI looper (or run it
 * immediately if the looper it is not paused).
 *
 * @param values The progress values to update the UI with.
 * @see AsyncTask#publishProgress(Object[])
 */
@Implementation
protected void publishProgress(final Progress... values) {
 ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
  @Override
  public void run() {
   getBridge().onProgressUpdate(values);
  }
 });
}

代码示例来源:origin: robolectric/robolectric

@Implementation
protected boolean sendBroadcast(Intent intent) {
 boolean sent = false;
 sentBroadcastIntents.add(intent);
 List<Wrapper> copy = new ArrayList<>();
 copy.addAll(registeredReceivers);
 for (Wrapper wrapper : copy) {
  if (wrapper.intentFilter.matchAction(intent.getAction())) {
   final int match = wrapper.intentFilter.matchData(intent.getType(), intent.getScheme(), intent.getData());
   if (match != IntentFilter.NO_MATCH_DATA && match != IntentFilter.NO_MATCH_TYPE) {
    sent = true;
    final BroadcastReceiver receiver = wrapper.broadcastReceiver;
    final Intent broadcastIntent = intent;
    Robolectric.getForegroundThreadScheduler().post(new Runnable() {
     @Override
     public void run() {
      receiver.onReceive(RuntimeEnvironment.application, broadcastIntent);
     }
    });
   }
  }
 }
 return sent;
}

代码示例来源:origin: robolectric/robolectric

ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
  @Override
  public void run() {
ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
 @Override
 public void run() {

代码示例来源:origin: robolectric/robolectric

@Implementation
protected void onForceLoad() {
 FutureTask<D> future = new FutureTask<D>(worker) {
  @Override
  protected void done() {
   try {
    final D result = get();
    ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
     @Override
     public void run() {
      realObject.deliverResult(result);
     }
    });
   } catch (InterruptedException e) {
    // Ignore
   } catch (ExecutionException e) {
    throw new RuntimeException(e.getCause());
   }
  }
 };
 ShadowApplication.getInstance().getBackgroundThreadScheduler().post(future);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void post_whenTheRunnableThrows_executesSubsequentRunnables() throws Exception {
 final List<Integer> runnablesThatWereRun = new ArrayList<>();
 scheduler.post(new Runnable() {
  @Override
  public void run() {
   runnablesThatWereRun.add(1);
   throw new RuntimeException("foo");
  }
 });
 try {
  scheduler.unPause();
 } catch (RuntimeException ignored) { }
 scheduler.post(new Runnable() {
  @Override
  public void run() {
   runnablesThatWereRun.add(2);
  }
 });
 assertThat(runnablesThatWereRun).containsExactly(1, 2);
}

代码示例来源:origin: robolectric/robolectric

@Implementation
protected AsyncTask<Params, Progress, Result> execute(final Params... params) {
 status = AsyncTask.Status.RUNNING;
 getBridge().onPreExecute();
 worker.params = params;
 ShadowApplication.getInstance().getBackgroundThreadScheduler().post(new Runnable() {
  @Override
  public void run() {
   future.run();
  }
 });
 return realAsyncTask;
}

代码示例来源:origin: robolectric/robolectric

@Test
public void reset_shouldUnPause() throws Exception {
 scheduler.pause();
 TestRunnable runnable = new TestRunnable();
 scheduler.post(runnable);
 assertThat(runnable.wasRun).isFalse();
 scheduler.reset();
 scheduler.post(runnable);
 assertThat(runnable.wasRun).isTrue();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void remove_ShouldRemoveAllInstancesOfRunnableFromQueue() throws Exception {
 scheduler.post(new TestRunnable());
 TestRunnable runnable = new TestRunnable();
 scheduler.post(runnable);
 scheduler.post(runnable);
 assertThat(scheduler.size()).isEqualTo(3);
 scheduler.remove(runnable);
 assertThat(scheduler.size()).isEqualTo(1);
 scheduler.advanceToLastPostedRunnable();
 assertThat(runnable.wasRun).isFalse();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void reset_shouldClearPendingRunnables() throws Exception {
 scheduler.pause();
 TestRunnable runnable1 = new TestRunnable();
 scheduler.post(runnable1);
 assertThat(runnable1.wasRun).isFalse();
 scheduler.reset();
 TestRunnable runnable2 = new TestRunnable();
 scheduler.post(runnable2);
 assertThat(runnable1.wasRun).isFalse();
 assertThat(runnable2.wasRun).isTrue();
}

代码示例来源:origin: robolectric/robolectric

private void runOnUiThread(Runnable action) {
  // This is meant to emulate the behavior of Activity.runOnUiThread();
  shadowOf(handler.getLooper()).getScheduler().post(action);
 }
}

代码示例来源:origin: robolectric/robolectric

private void runOnUiThread(Runnable action) {
  // This is meant to emulate the behavior of Activity.runOnUiThread();
  shadowOf(handler.getLooper()).getScheduler().post(action);
 }
}

相关文章