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

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

本文整理了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

  1. private <T> Future<T> schedule(final FutureTask<T> futureTask) {
  2. Runnable runnable = new Runnable() {
  3. @Override
  4. public void run() {
  5. futureTask.run();
  6. runnables.remove(this);
  7. }
  8. };
  9. runnables.add(runnable);
  10. scheduler.post(runnable);
  11. return futureTask;
  12. }

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

  1. @Test
  2. public void postAtFrontOfQueue_addsJobAtFrontOfQueue() throws Exception {
  3. scheduler.post(new AddToTranscript("one"));
  4. scheduler.post(new AddToTranscript("two"));
  5. scheduler.postAtFrontOfQueue(new AddToTranscript("three"));
  6. scheduler.runOneTask();
  7. assertThat(transcript).containsExactly("three");
  8. transcript.clear();
  9. scheduler.runOneTask();
  10. assertThat(transcript).containsExactly("one");
  11. transcript.clear();
  12. scheduler.runOneTask();
  13. assertThat(transcript).containsExactly("two");
  14. }

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

  1. @Override protected void done() {
  2. try {
  3. final D result = get();
  4. Robolectric.getForegroundThreadScheduler().post(new Runnable() {
  5. @Override public void run() {
  6. realLoader.deliverResult(result);
  7. }
  8. });
  9. } catch (InterruptedException e) {
  10. // Ignore
  11. } catch (ExecutionException e) {
  12. throw new RuntimeException(e.getCause());
  13. }
  14. }
  15. };

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

  1. @Override
  2. public void run() {
  3. order.add(1);
  4. scheduler.post(new Runnable() {
  5. @Override
  6. public void run() {
  7. order.add(4);
  8. }
  9. });
  10. order.add(2);
  11. }
  12. }, 0);

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

  1. @Override
  2. public void run() {
  3. order.add(1);
  4. scheduler.post(new Runnable() {
  5. @Override
  6. public void run() {
  7. order.add(3);
  8. }
  9. });
  10. order.add(2);
  11. }
  12. }, 0);

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

  1. @Override
  2. protected void done() {
  3. try {
  4. final D result = get();
  5. ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
  6. @Override
  7. public void run() {
  8. realObject.deliverResult(result);
  9. }
  10. });
  11. } catch (InterruptedException e) {
  12. // Ignore
  13. } catch (ExecutionException e) {
  14. throw new RuntimeException(e.getCause());
  15. }
  16. }
  17. };

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

  1. @Implementation
  2. protected void onForceLoad() {
  3. FutureTask<D> future = new FutureTask<D>(worker) {
  4. @Override protected void done() {
  5. try {
  6. final D result = get();
  7. Robolectric.getForegroundThreadScheduler().post(new Runnable() {
  8. @Override public void run() {
  9. realLoader.deliverResult(result);
  10. }
  11. });
  12. } catch (InterruptedException e) {
  13. // Ignore
  14. } catch (ExecutionException e) {
  15. throw new RuntimeException(e.getCause());
  16. }
  17. }
  18. };
  19. Robolectric.getBackgroundThreadScheduler().post(future);
  20. }

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

  1. @Implementation
  2. protected boolean post(Runnable action) {
  3. ShadowApplication.getInstance().getForegroundThreadScheduler().post(action);
  4. return true;
  5. }

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

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

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

  1. /**
  2. * Enqueue a call to {@link AsyncTask#onProgressUpdate(Object[])} on UI looper (or run it
  3. * immediately if the looper it is not paused).
  4. *
  5. * @param values The progress values to update the UI with.
  6. * @see AsyncTask#publishProgress(Object[])
  7. */
  8. @Implementation
  9. protected void publishProgress(final Progress... values) {
  10. ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
  11. @Override
  12. public void run() {
  13. getBridge().onProgressUpdate(values);
  14. }
  15. });
  16. }

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

  1. @Implementation
  2. protected boolean sendBroadcast(Intent intent) {
  3. boolean sent = false;
  4. sentBroadcastIntents.add(intent);
  5. List<Wrapper> copy = new ArrayList<>();
  6. copy.addAll(registeredReceivers);
  7. for (Wrapper wrapper : copy) {
  8. if (wrapper.intentFilter.matchAction(intent.getAction())) {
  9. final int match = wrapper.intentFilter.matchData(intent.getType(), intent.getScheme(), intent.getData());
  10. if (match != IntentFilter.NO_MATCH_DATA && match != IntentFilter.NO_MATCH_TYPE) {
  11. sent = true;
  12. final BroadcastReceiver receiver = wrapper.broadcastReceiver;
  13. final Intent broadcastIntent = intent;
  14. Robolectric.getForegroundThreadScheduler().post(new Runnable() {
  15. @Override
  16. public void run() {
  17. receiver.onReceive(RuntimeEnvironment.application, broadcastIntent);
  18. }
  19. });
  20. }
  21. }
  22. }
  23. return sent;
  24. }

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

  1. ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
  2. @Override
  3. public void run() {
  4. ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
  5. @Override
  6. public void run() {

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

  1. @Implementation
  2. protected void onForceLoad() {
  3. FutureTask<D> future = new FutureTask<D>(worker) {
  4. @Override
  5. protected void done() {
  6. try {
  7. final D result = get();
  8. ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() {
  9. @Override
  10. public void run() {
  11. realObject.deliverResult(result);
  12. }
  13. });
  14. } catch (InterruptedException e) {
  15. // Ignore
  16. } catch (ExecutionException e) {
  17. throw new RuntimeException(e.getCause());
  18. }
  19. }
  20. };
  21. ShadowApplication.getInstance().getBackgroundThreadScheduler().post(future);
  22. }

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

  1. @Test
  2. public void post_whenTheRunnableThrows_executesSubsequentRunnables() throws Exception {
  3. final List<Integer> runnablesThatWereRun = new ArrayList<>();
  4. scheduler.post(new Runnable() {
  5. @Override
  6. public void run() {
  7. runnablesThatWereRun.add(1);
  8. throw new RuntimeException("foo");
  9. }
  10. });
  11. try {
  12. scheduler.unPause();
  13. } catch (RuntimeException ignored) { }
  14. scheduler.post(new Runnable() {
  15. @Override
  16. public void run() {
  17. runnablesThatWereRun.add(2);
  18. }
  19. });
  20. assertThat(runnablesThatWereRun).containsExactly(1, 2);
  21. }

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

  1. @Implementation
  2. protected AsyncTask<Params, Progress, Result> execute(final Params... params) {
  3. status = AsyncTask.Status.RUNNING;
  4. getBridge().onPreExecute();
  5. worker.params = params;
  6. ShadowApplication.getInstance().getBackgroundThreadScheduler().post(new Runnable() {
  7. @Override
  8. public void run() {
  9. future.run();
  10. }
  11. });
  12. return realAsyncTask;
  13. }

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

  1. @Test
  2. public void reset_shouldUnPause() throws Exception {
  3. scheduler.pause();
  4. TestRunnable runnable = new TestRunnable();
  5. scheduler.post(runnable);
  6. assertThat(runnable.wasRun).isFalse();
  7. scheduler.reset();
  8. scheduler.post(runnable);
  9. assertThat(runnable.wasRun).isTrue();
  10. }

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

  1. @Test
  2. public void remove_ShouldRemoveAllInstancesOfRunnableFromQueue() throws Exception {
  3. scheduler.post(new TestRunnable());
  4. TestRunnable runnable = new TestRunnable();
  5. scheduler.post(runnable);
  6. scheduler.post(runnable);
  7. assertThat(scheduler.size()).isEqualTo(3);
  8. scheduler.remove(runnable);
  9. assertThat(scheduler.size()).isEqualTo(1);
  10. scheduler.advanceToLastPostedRunnable();
  11. assertThat(runnable.wasRun).isFalse();
  12. }

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

  1. @Test
  2. public void reset_shouldClearPendingRunnables() throws Exception {
  3. scheduler.pause();
  4. TestRunnable runnable1 = new TestRunnable();
  5. scheduler.post(runnable1);
  6. assertThat(runnable1.wasRun).isFalse();
  7. scheduler.reset();
  8. TestRunnable runnable2 = new TestRunnable();
  9. scheduler.post(runnable2);
  10. assertThat(runnable1.wasRun).isFalse();
  11. assertThat(runnable2.wasRun).isTrue();
  12. }

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

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

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

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

相关文章