@Override public Boolean call() { return remaining.get().size() > 0; } }, new Continuation<Void, Task<Void>>() {
@Test public void testRightCallbackExecuted() { final Capture<Boolean> capture = new Capture(false); final CallbackManagerImpl callbackManagerImpl = new CallbackManagerImpl(); callbackManagerImpl.registerCallback( 123, new CallbackManagerImpl.Callback() { @Override public boolean onActivityResult(int resultCode, Intent data) { capture.set(true); return true; } }); callbackManagerImpl.registerCallback( 456, new CallbackManagerImpl.Callback() { @Override public boolean onActivityResult(int resultCode, Intent data) { return false; } }); callbackManagerImpl.onActivityResult( 123, 1, new Intent()); assertTrue(capture.get()); }
@Test public void testCallbackExecuted() { final Capture<Boolean> capture = new Capture(false); final CallbackManagerImpl callbackManagerImpl = new CallbackManagerImpl(); callbackManagerImpl.registerCallback( CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode(), new CallbackManagerImpl.Callback() { @Override public boolean onActivityResult(int resultCode, Intent data) { capture.set(true); return true; } }); callbackManagerImpl.onActivityResult( FacebookSdk.getCallbackRequestCodeOffset(), 1, new Intent()); assertTrue(capture.get()); }
@Test public void testStaticCallbackExecuted() { final Capture<Boolean> capture = new Capture(false); final CallbackManagerImpl callbackManagerImpl = new CallbackManagerImpl(); callbackManagerImpl.registerStaticCallback( CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode(), new CallbackManagerImpl.Callback() { @Override public boolean onActivityResult(int resultCode, Intent data) { capture.set(true); return true; } }); callbackManagerImpl.onActivityResult( FacebookSdk.getCallbackRequestCodeOffset(), 1, new Intent()); assertTrue(capture.get()); }
@Override public Task<Cursor> then(Task<String> task) { uuid.set(task.getResult()); String[] args = {uuid.get()}; return db.queryAsync(OfflineSQLiteOpenHelper.TABLE_OBJECTS, select, where, args); } }).onSuccess(new Continuation<Cursor, String>() {
@Override public Task<Void> then(Task<Void> task) { String where = OfflineSQLiteOpenHelper.KEY_UUID + "=?"; String[] args = {uuid.get()}; return db.deleteAsync(OfflineSQLiteOpenHelper.TABLE_OBJECTS, where, args); } }).onSuccessTask(new Continuation<Void, Task<Void>>() {
@Override public String then(Task<Cursor> task) { Cursor cursor = task.getResult(); cursor.moveToFirst(); if (cursor.isAfterLast()) { cursor.close(); throw new IllegalStateException("Attempted to find non-existent uuid " + uuid.get()); } String json = cursor.getString(0); cursor.close(); return json; } });
@Override public void onLocationChanged(Location location) { if (location == null) { return; } timeoutFuture.get().cancel(true); tcs.trySetResult(location); manager.removeUpdates(this); }
for (ParseObject obj : remaining.get()) { if (obj.canBeSerialized()) { current.add(obj);
@Override public Task<Void> then(Task<String> task) { String sessionToken = task.getResult(); return getAnalyticsController().trackAppOpenedInBackground(pushHash.get(), sessionToken); } });
/** * This is kind of like ParseTaskUtils.wait(), except that it gives up the CommandCache's lock * while the task is running, and reclaims it before returning. */ private <T> T waitForTaskWithoutLock(Task<T> task) throws ParseException { synchronized (lock) { final Capture<Boolean> finished = new Capture<>(false); task.continueWith(new Continuation<T, Void>() { @Override public Void then(Task<T> task) { finished.set(true); synchronized (lock) { lock.notifyAll(); } return null; } }, Task.BACKGROUND_EXECUTOR); while (!finished.get()) { try { lock.wait(); } catch (InterruptedException ie) { shouldStop = true; } } return ParseTaskUtils.wait(task); // Just to get the return value and maybe rethrow. } }
@Override protected boolean visit(Object value) { if (value instanceof ParseFile) { ParseFile file = (ParseFile) value; if (file.isDirty()) { result.set(false); } } if (value instanceof ParseObject) { ParseObject object = (ParseObject) value; if (object.getObjectId() == null) { result.set(false); } } // Continue to traverse only if it can still be serialized. return result.get(); } }.setYieldRoot(false).setTraverseParseObjects(true).traverse(this);
return result.get();
@Test public void testUnsubscribeInBackgroundWithCallbackSuccess() throws Exception { final ParsePushChannelsController controller = mock(ParsePushChannelsController.class); when(controller.unsubscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); final Semaphore done = new Semaphore(0); final Capture<Exception> exceptionCapture = new Capture<>(); ParsePush.unsubscribeInBackground("test", new SaveCallback() { @Override public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertNull(exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).unsubscribeInBackground("test"); }
@Test public void testSubscribeInBackgroundWithCallbackSuccess() throws Exception { final ParsePushChannelsController controller = mock(ParsePushChannelsController.class); when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); ParsePush push = new ParsePush(); final Semaphore done = new Semaphore(0); final Capture<Exception> exceptionCapture = new Capture<>(); ParsePush.subscribeInBackground("test", new SaveCallback() { @Override public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertNull(exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).subscribeInBackground("test"); }
@Test public void testUnsubscribeInBackgroundWithCallbackFail() throws Exception { ParsePushChannelsController controller = mock(ParsePushChannelsController.class); final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.unsubscribeInBackground(anyString())) .thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); ParsePush push = new ParsePush(); final Semaphore done = new Semaphore(0); final Capture<Exception> exceptionCapture = new Capture<>(); ParsePush.unsubscribeInBackground("test", new SaveCallback() { @Override public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertSame(exception, exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).unsubscribeInBackground("test"); }
@Test public void testSubscribeInBackgroundWithCallbackFail() throws Exception { ParsePushChannelsController controller = mock(ParsePushChannelsController.class); final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); ParsePush push = new ParsePush(); final Semaphore done = new Semaphore(0); final Capture<Exception> exceptionCapture = new Capture<>(); ParsePush.subscribeInBackground("test", new SaveCallback() { @Override public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertSame(exception, exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).subscribeInBackground("test"); }
assertNull(exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
assertNull(exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);