@Test public void executeOnExecutor_usesPassedExecutor() throws Exception { AsyncTask<String, String, String> asyncTask = new MyAsyncTask(); assertThat(asyncTask.getStatus()).isEqualTo(AsyncTask.Status.PENDING); asyncTask.executeOnExecutor(new ImmediateExecutor(), "a", "b"); assertThat(asyncTask.getStatus()).isEqualTo(AsyncTask.Status.FINISHED); assertThat(transcript).containsExactly("onPreExecute", "doInBackground a, b"); transcript.clear(); assertEquals("Result should get stored in the AsyncTask", "c", asyncTask.get()); ShadowLooper.runUiThreadTasks(); assertThat(transcript).containsExactly("onPostExecute c"); }
@Test public void testNormalFlow() throws Exception { AsyncTask<String, String, String> asyncTask = new MyAsyncTask(); asyncTask.execute("a", "b"); assertThat(transcript).containsExactly("onPreExecute"); transcript.clear(); ShadowApplication.runBackgroundTasks(); assertThat(transcript).containsExactly("doInBackground a, b"); transcript.clear(); assertEquals("Result should get stored in the AsyncTask", "c", asyncTask.get(100, TimeUnit.MILLISECONDS)); ShadowLooper.runUiThreadTasks(); assertThat(transcript).containsExactly("onPostExecute c"); }
@Test public void executeReturnsAsyncTask() throws Exception { Robolectric.getBackgroundThreadScheduler().unPause(); AsyncTask<String, String, String> asyncTask = new MyAsyncTask(); assertThat(asyncTask.execute("a", "b").get()).isEqualTo("c"); }
@Test public void progressUpdatesAreQueuedUntilBackgroundThreadFinishes() throws Exception { AsyncTask<String, String, String> asyncTask = new MyAsyncTask() { @Override protected String doInBackground(String... strings) { publishProgress("33%"); publishProgress("66%"); publishProgress("99%"); return "done"; } }; asyncTask.execute("a", "b"); assertThat(transcript).containsExactly("onPreExecute"); transcript.clear(); ShadowApplication.runBackgroundTasks(); assertThat(transcript).isEmpty(); assertEquals("Result should get stored in the AsyncTask", "done", asyncTask.get(100, TimeUnit.MILLISECONDS)); ShadowLooper.runUiThreadTasks(); assertThat(transcript).containsExactly( "onProgressUpdate 33%", "onProgressUpdate 66%", "onProgressUpdate 99%", "onPostExecute done" ); }
@Test public void testCancelBeforePostExecute() throws Exception { AsyncTask<String, String, String> asyncTask = new MyAsyncTask(); asyncTask.execute("a", "b"); assertThat(transcript).containsExactly("onPreExecute"); transcript.clear(); ShadowApplication.runBackgroundTasks(); assertThat(transcript).containsExactly("doInBackground a, b"); transcript.clear(); assertEquals("Result should get stored in the AsyncTask", "c", asyncTask.get(100, TimeUnit.MILLISECONDS)); assertFalse(asyncTask.cancel(true)); assertFalse(asyncTask.isCancelled()); ShadowLooper.runUiThreadTasks(); assertThat(transcript).containsExactly("onPostExecute c"); }
SharedPreferences get() { try { return asyncTask.get(); } catch (InterruptedException | ExecutionException ignored) { return null; } }
/** * Runs a script as root (multiple commands separated by "\n") * * @param ctx mandatory context * @param script the script to be executed * @param res the script output response (stdout + stderr) * @return the script exit code * @throws IOException on any error executing the script, or writing it to disk */ public static int runScriptAsRoot(Context ctx, List<String> script, StringBuilder res) throws IOException { int returnCode = -1; if ((Looper.myLooper() != null) && (Looper.myLooper() == Looper.getMainLooper())) { Log.e(TAG, "runScriptAsRoot should not be called from the main thread\nCall Trace:\n"); for (StackTraceElement e : new Throwable().getStackTrace()) { Log.e(TAG, e.toString()); } } try { returnCode = new RunCommand().execute(script, res, ctx).get(); } catch (RejectedExecutionException r) { Log.e(TAG, "runScript failed: " + r.getLocalizedMessage()); } catch (InterruptedException e) { Log.e(TAG, "Caught InterruptedException"); } catch (ExecutionException e) { Log.e(TAG, "runScript failed: " + e.getLocalizedMessage()); } catch (Exception e) { Log.e(TAG, "runScript failed: " + e.getLocalizedMessage()); } return returnCode; }
if (imageIconUrl != null) { try { largeIconBitmap = new DownloadImageTask().execute(imageIconUrl).get(); } catch (final InterruptedException e) { log.error("Interrupted when downloading image : " + e.getMessage(), e); if (imageSmallIconUrl != null) { try { smallIconBitmap = new DownloadImageTask().execute(imageSmallIconUrl).get(); } catch (final InterruptedException e) { log.error("Interrupted when downloading small icon : " + e.getMessage(), e);
notificationImage = new DownloadImageTask().execute(imageUrl).get(); } catch (final InterruptedException e) { log.error("Interrupted when downloading image : " + e.getMessage(), e);
/** Immediately returns the phone formatter or null if it has not initialized yet. */ private PhoneNumberFormattingTextWatcher getPhoneFormatter() { try { return mPhoneFormatterTask.get(0, TimeUnit.MILLISECONDS); } catch (CancellationException | ExecutionException | InterruptedException | TimeoutException e) { return null; } }
public void getKey(View v) { JSONParser JSONParse = new JSONParser(); //JSONParse.execute(); try { Snackbar.make(v, JSONParse.execute().get(), Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }
/** @return The directory that stores the TabState files. */ @Override public File getStateDirectory() { try { return sBaseStateDirectoryFetchTask.get(); } catch (InterruptedException e) { } catch (ExecutionException e) { } // If the AsyncTask failed for some reason, we have no choice but to fall back to // main-thread disk access. return ContextUtils.getApplicationContext().getDir(STATE_DIRECTORY, Context.MODE_PRIVATE); }
@NonNull public List<ServerProfile> getRecordingProfiles() { List<ServerProfile> serverProfiles = new ArrayList<>(); try { serverProfiles.addAll(new ServerProfileTask(db, RECORDINGS).execute().get()); } catch (InterruptedException e) { Timber.d("Loading recording server profile task got interrupted", e); } catch (ExecutionException e) { Timber.d("Loading recording server profile task aborted", e); } return serverProfiles; }
@NonNull public List<ServerProfile> getHttpPlaybackProfiles() { List<ServerProfile> serverProfiles = new ArrayList<>(); try { serverProfiles.addAll(new ServerProfileTask(db, HTTP_PLAYBACK).execute().get()); } catch (InterruptedException e) { Timber.d("Loading http playback server profile task got interrupted", e); } catch (ExecutionException e) { Timber.d("Loading http playback server profile task aborted", e); } return serverProfiles; }
@Override @Nullable public ChannelTag getItemById(Object id) { try { return new ChannelTagByIdTask(db, (int) id).execute().get(); } catch (InterruptedException e) { Timber.d("Loading channel tag by id task got interrupted", e); } catch (ExecutionException e) { Timber.d("Loading channel tag by id task aborted", e); } return null; }
@Override @Nullable public Channel getItemById(Object id) { try { return new ChannelByIdTask(db, (int) id).execute().get(); } catch (InterruptedException e) { Timber.d("Loading channel by id task got interrupted", e); } catch (ExecutionException e) { Timber.d("Loading channel by id task aborted", e); } return null; }
@Nullable public Program getLastItemByChannelId(int channelId) { try { return new ProgramByIdTask(db, channelId, LOAD_LAST_IN_CHANNEL).execute().get(); } catch (InterruptedException e) { Timber.d("Loading last programs in channel task got interrupted", e); } catch (ExecutionException e) { Timber.d("Loading last program in channel task aborted", e); } return null; }
public int getItemCount() { try { return new RecordingCountTask(db).execute().get(); } catch (InterruptedException e) { Timber.d("Loading recording count task got interrupted", e); } catch (ExecutionException e) { Timber.d("Loading recording count task aborted", e); } return 0; }
@Override public TimerRecording getItemById(Object id) { try { return new TimerRecordingByIdTask(db, (String) id).execute().get(); } catch (InterruptedException e) { Timber.d("Loading timer recording by id task got interrupted", e); } catch (ExecutionException e) { Timber.d("Loading timer recording by id task aborted", e); } return null; }
public int getItemCount() { try { return new ChannelTagCountTask(db).execute().get(); } catch (InterruptedException e) { Timber.d("Loading channel tag count task got interrupted", e); } catch (ExecutionException e) { Timber.d("Loading channel tag count task aborted", e); } return 0; }