/** * A helper method to concisely send a push message to a query. This method is equivalent to * ParsePush push = new ParsePush(); push.setMessage(message); push.setQuery(query); * push.sendInBackground(); * * @param message The message that will be shown in the notification. * @param query A ParseInstallation query which specifies the recipients of a push. * @return A task that is resolved when the message is sent. */ public static Task<Void> sendMessageInBackground(String message, ParseQuery<ParseInstallation> query) { ParsePush push = new ParsePush(); push.setQuery(query); push.setMessage(message); return push.sendInBackground(); }
/** * A helper method to concisely send a push to a query. This method is equivalent to ParsePush * push = new ParsePush(); push.setData(data); push.setQuery(query); * push.sendInBackground(callback); * * @param data The entire data of the push message. See the push guide for more details on the data * format. * @param query A ParseInstallation query which specifies the recipients of a push. * @param callback callback.done(e) is called when the send completes. */ public static void sendDataInBackground(JSONObject data, ParseQuery<ParseInstallation> query, SendCallback callback) { ParseTaskUtils.callbackOnMainThreadAsync(sendDataInBackground(data, query), callback); }
/** * A helper method to concisely send a push message to a query. This method is equivalent to * ParsePush push = new ParsePush(); push.setMessage(message); push.setQuery(query); * push.sendInBackground(callback); * * @param message The message that will be shown in the notification. * @param query A ParseInstallation query which specifies the recipients of a push. * @param callback callback.done(e) is called when the send completes. */ public static void sendMessageInBackground(String message, ParseQuery<ParseInstallation> query, SendCallback callback) { ParseTaskUtils.callbackOnMainThreadAsync(sendMessageInBackground(message, query), callback); }
/** * A helper method to concisely send a push to a query. This method is equivalent to ParsePush * push = new ParsePush(); push.setData(data); push.setQuery(query); push.sendInBackground(); * * @param data The entire data of the push message. See the push guide for more details on the data * format. * @param query A ParseInstallation query which specifies the recipients of a push. * @return A task that is resolved when the data is sent. */ public static Task<Void> sendDataInBackground(JSONObject data, ParseQuery<ParseInstallation> query) { ParsePush push = new ParsePush(); push.setQuery(query); push.setData(data); return push.sendInBackground(); }
@Test public void testSetPushTime() { ParsePush push = new ParsePush(); long time = System.currentTimeMillis() / 1000 + 1000; push.setPushTime(time); // Right now it is hard for us to test a builder, so we build a state to test the builder is // set correctly // We have to set message otherwise build() will throw an exception push.setMessage("message"); ParsePush.State state = push.builder.build(); assertEquals(time, state.pushTime().longValue()); }
@Test public void testSendInBackgroundSuccess() throws Exception { // Mock controller ParsePushController controller = mock(ParsePushController.class); when(controller.sendInBackground(any(ParsePush.State.class), anyString())) .thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerPushController(controller); // Make sample ParsePush data and call method ParsePush push = new ParsePush(); JSONObject data = new JSONObject(); data.put("key", "value"); List<String> channels = new ArrayList<>(); channels.add("test"); channels.add("testAgain"); push.builder.expirationTime((long) 1000) .data(data) .channelSet(channels); ParseTaskUtils.wait(push.sendInBackground()); // Make sure controller is executed and state parameter is correct ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class); verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString()); ParsePush.State state = stateCaptor.getValue(); assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE); assertEquals(2, state.channelSet().size()); assertTrue(state.channelSet().contains("test")); assertTrue(state.channelSet().contains("testAgain")); }
/** * Sends this push notification while blocking this thread until the push notification has * successfully reached the Parse servers. Typically, you should use {@link #sendInBackground()} * instead of this, unless you are managing your own threading. * * @throws ParseException Throws an exception if the server is inaccessible. */ public void send() throws ParseException { ParseTaskUtils.wait(sendInBackground()); }
@Test public void testClearExpiration() { ParsePush push = new ParsePush(); push.setExpirationTimeInterval(10000); // Right now it is hard for us to test a builder, so we build a state to test the builder is // set correctly // We have to set message otherwise build() will throw an exception push.setMessage("message"); // Make sure interval has value before clear ParsePush.State state = push.builder.build(); assertEquals(10000, state.expirationTimeInterval().longValue()); // Make sure interval is empty after clear push.clearExpiration(); state = push.builder.build(); assertNull(state.expirationTimeInterval()); push.setExpirationTime(200); // Make sure expiration time has value before clear state = push.builder.build(); assertEquals(200, state.expirationTime().longValue()); // Make sure interval is empty after clear push.clearExpiration(); state = push.builder.build(); assertNull(state.expirationTime()); }
@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"); }
/** * Removes 'channel' from the 'channels' list in the current {@link ParseInstallation} and saves * it in a background thread. * * @param channel The channel to unsubscribe from. * @param callback The SaveCallback that is called after the Installation is saved. */ public static void unsubscribeInBackground(String channel, SaveCallback callback) { ParseTaskUtils.callbackOnMainThreadAsync(unsubscribeInBackground(channel), callback); }
@Test public void testSetQuery() throws Exception { ParsePush push = new ParsePush(); ParseQuery<ParseInstallation> query = ParseInstallation.getQuery(); query.getBuilder() .whereEqualTo("foo", "bar"); push.setQuery(query); // Right now it is hard for us to test a builder, so we build a state to test the builder is // set correctly // We have to set message otherwise build() will throw an exception push.setMessage("message"); ParsePush.State state = push.builder.build(); ParseQuery.State<ParseInstallation> queryState = state.queryState(); JSONObject queryStateJson = queryState.toJSON(PointerEncoder.get()); assertEquals("bar", queryStateJson.getJSONObject("where").getString("foo")); }
/** * 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 testSetExpirationTime() { ParsePush push = new ParsePush(); push.setExpirationTime(10000); // Right now it is hard for us to test a builder, so we build a state to test the builder is // set correctly // We have to set message otherwise build() will throw an exception push.setMessage("message"); ParsePush.State state = push.builder.build(); assertEquals(10000, state.expirationTime().longValue()); }
@Test public void testSetExpirationTimeInterval() { ParsePush push = new ParsePush(); push.setExpirationTimeInterval(10000); // Right now it is hard for us to test a builder, so we build a state to test the builder is // set correctly // We have to set message otherwise build() will throw an exception push.setMessage("message"); ParsePush.State state = push.builder.build(); assertEquals(10000, state.expirationTimeInterval().longValue()); }
@Test public void testSetChannel() { ParsePush push = new ParsePush(); push.setChannel("test"); // Right now it is hard for us to test a builder, so we build a state to test the builder is // set correctly // We have to set message otherwise build() will throw an exception push.setMessage("message"); ParsePush.State state = push.builder.build(); assertEquals(1, state.channelSet().size()); assertTrue(state.channelSet().contains("test")); }
@Test public void testSetChannels() { ParsePush push = new ParsePush(); List<String> channels = new ArrayList<>(); channels.add("test"); channels.add("testAgain"); push.setChannels(channels); // Right now it is hard for us to test a builder, so we build a state to test the builder is // set correctly // We have to set message otherwise build() will throw an exception push.setMessage("message"); ParsePush.State state = push.builder.build(); assertEquals(2, state.channelSet().size()); assertTrue(state.channelSet().contains("test")); assertTrue(state.channelSet().contains("testAgain")); }
@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 testSetData() throws Exception { ParsePush push = new ParsePush(); JSONObject data = new JSONObject(); data.put("key", "value"); data.put("keyAgain", "valueAgain"); push.setData(data); // Right now it is hard for us to test a builder, so we build a state to test the builder is // set correctly ParsePush.State state = push.builder.build(); assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE); }
@Test public void testSetMessage() throws Exception { ParsePush push = new ParsePush(); push.setMessage("test"); // Right now it is hard for us to test a builder, so we build a state to test the builder is // set correctly ParsePush.State state = push.builder.build(); JSONObject data = state.data(); assertEquals("test", data.getString(ParsePush.KEY_DATA_MESSAGE)); }
@Test public void testSendSuccess() throws Exception { // Mock controller ParsePushController controller = mock(ParsePushController.class); when(controller.sendInBackground(any(ParsePush.State.class), anyString())) .thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerPushController(controller); // Make sample ParsePush data and call method ParsePush push = new ParsePush(); JSONObject data = new JSONObject(); data.put("key", "value"); List<String> channels = new ArrayList<>(); channels.add("test"); channels.add("testAgain"); push.builder.expirationTime((long) 1000) .data(data) .channelSet(channels); push.send(); // Make sure controller is executed and state parameter is correct ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class); verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString()); ParsePush.State state = stateCaptor.getValue(); assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE); assertEquals(2, state.channelSet().size()); assertTrue(state.channelSet().contains("test")); assertTrue(state.channelSet().contains("testAgain")); }