/** * Adds 'channel' to the 'channels' list in the current {@link ParseInstallation} and saves it in * a background thread. * * @param channel The channel to subscribe to. * @param callback The SaveCallback that is called after the Installation is saved. */ public static void subscribeInBackground(String channel, SaveCallback callback) { ParseTaskUtils.callbackOnMainThreadAsync(subscribeInBackground(channel), callback); }
@Test public void testSubscribeInBackgroundSuccess() throws Exception { ParsePushChannelsController controller = mock(ParsePushChannelsController.class); when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); ParseTaskUtils.wait(ParsePush.subscribeInBackground("test")); verify(controller, times(1)).subscribeInBackground("test"); }
@Test public void testSubscribeInBackgroundFail() throws Exception { ParsePushChannelsController controller = mock(ParsePushChannelsController.class); ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); Task<Void> pushTask = ParsePush.subscribeInBackground("test"); pushTask.waitForCompletion(); verify(controller, times(1)).subscribeInBackground("test"); assertTrue(pushTask.isFaulted()); assertSame(exception, pushTask.getError()); }
@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 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"); }
@NonNull public static Completable subscribe(@NonNull final String channel) { return RxTask.completable(() -> ParsePush.subscribeInBackground(channel)); }