/** * Run a task on threads supplied by injected {@link ExecutorService} without a callback. By * default it runs tasks on separate threads by {@link ExecutorService} injected from AndroidMvc * framework. A simple {@link ExecutorService} that runs tasks on the same thread in test cases * to make the test easier. * * <p><b> * User the protected property {@link UiThreadRunner} to post action back to main UI thread * in the method block of {@link Task#execute(Task.Monitor)}. * </b></p> * <pre> * uiThreadRunner.post(new Runnable() { * @Override * public void run() { * view.update(); * } * }); * </pre> * * @param task The task * @return The monitor to track the state of the execution of the task. It also can cancel the * task. * */ protected <RESULT> Task.Monitor<RESULT> runTask(final Task<RESULT> task) { return runTask(executorService, task, null); }
/** * Run a task on threads supplied by injected {@link ExecutorService}. By default it runs tasks * on separate threads by {@link ExecutorService} injected from AndroidMvc framework. A simple * {@link ExecutorService} that runs tasks on the same thread in test cases to make the test * easier. * * <p>The methods of callback will be guaranteed to be run Android's UI thread</p> * * <p><b> * User the protected property {@link UiThreadRunner} to post action back to main UI thread * in the method block of {@link Task#execute(Task.Monitor)}. * </b></p> * <pre> * uiThreadRunner.post(new Runnable() { * @Override * public void run() { * view.update(); * } * }); * </pre> * * @param task The task * @param callback The callback * @return The monitor to track the state of the execution of the task. It also can cancel the * task. */ protected <RESULT> Task.Monitor<RESULT> runTask(final Task<RESULT> task, final Task.Callback<RESULT> callback) { return runTask(executorService, task, callback); }
@Test public void should_post_wrapped_other_exception_when_run_async_task_without_callback() { Controller controller = new Controller() { @Override public Class modelType() { return null; } }; graph.inject(controller); boolean exceptionCaught = false; final Exception exp = new Exception(""); try { controller.runTask(new Task() { @Override public Object execute(Monitor monitor) throws Exception { throw exp; } }); } catch (Exception e) { exceptionCaught = true; Assert.assertTrue(e.getCause() == exp); } Assert.assertTrue(exceptionCaught); } }
@Test public void should_post_wrapped_MvcGraphException_when_run_async_task() { Controller controller = new Controller() { @Override public Class modelType() { return null; } }; graph.inject(controller); boolean exceptionCaught = false; final MvcGraphException exp = new MvcGraphException(""); try { controller.runTask(new Task() { @Override public Object execute(Monitor monitor) throws Exception { throw exp; } }); } catch (Exception e) { exceptionCaught = true; Assert.assertTrue(e.getCause() == exp); } Assert.assertTrue(exceptionCaught); }