Search in sources :

Example 16 with Capture

use of bolts.Capture in project Parse-SDK-Android by ParsePlatform.

the class ParsePushTest method testSendDataInBackgroundWithCallback.

@Test
public void testSendDataInBackgroundWithCallback() 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
    JSONObject data = new JSONObject();
    data.put("key", "value");
    data.put("keyAgain", "valueAgain");
    ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
    query.getBuilder().whereEqualTo("foo", "bar");
    final Semaphore done = new Semaphore(0);
    final Capture<Exception> exceptionCapture = new Capture<>();
    ParsePush.sendDataInBackground(data, query, new SendCallback() {

        @Override
        public void done(ParseException e) {
            exceptionCapture.set(e);
            done.release();
        }
    });
    // Make sure controller is executed and state parameter is correct
    assertNull(exceptionCapture.get());
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
    verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString());
    ParsePush.State state = stateCaptor.getValue();
    // Verify query state
    ParseQuery.State<ParseInstallation> queryState = state.queryState();
    JSONObject queryStateJson = queryState.toJSON(PointerEncoder.get());
    assertEquals("bar", queryStateJson.getJSONObject("where").getString("foo"));
    // Verify data
    assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE);
}
Also used : Semaphore(java.util.concurrent.Semaphore) Capture(bolts.Capture) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 17 with Capture

use of bolts.Capture in project Parse-SDK-Android by ParsePlatform.

the class ParsePushTest method testUnsubscribeInBackgroundWithCallbackFail.

@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<>();
    push.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");
}
Also used : Semaphore(java.util.concurrent.Semaphore) Capture(bolts.Capture) Test(org.junit.Test)

Example 18 with Capture

use of bolts.Capture in project Parse-SDK-Android by ParsePlatform.

the class ParsePushTest method testSendInBackgroundWithCallbackFail.

@Test
public void testSendInBackgroundWithCallbackFail() throws Exception {
    // Mock controller
    ParsePushController controller = mock(ParsePushController.class);
    final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error");
    when(controller.sendInBackground(any(ParsePush.State.class), anyString())).thenReturn(Task.<Void>forError(exception));
    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).pushToIOS(true).channelSet(channels);
    final Semaphore done = new Semaphore(0);
    final Capture<Exception> exceptionCapture = new Capture<>();
    push.sendInBackground(new SendCallback() {

        @Override
        public void done(ParseException e) {
            exceptionCapture.set(e);
            done.release();
        }
    });
    // Make sure controller is executed and state parameter is correct
    assertSame(exception, exceptionCapture.get());
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
    verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString());
    ParsePush.State state = stateCaptor.getValue();
    assertTrue(state.pushToIOS());
    assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE);
    assertEquals(2, state.channelSet().size());
    assertTrue(state.channelSet().contains("test"));
    assertTrue(state.channelSet().contains("testAgain"));
}
Also used : ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Semaphore(java.util.concurrent.Semaphore) Capture(bolts.Capture) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Aggregations

Capture (bolts.Capture)18 Test (org.junit.Test)12 Semaphore (java.util.concurrent.Semaphore)8 JSONObject (org.json.JSONObject)5 Intent (android.content.Intent)4 Continuation (bolts.Continuation)4 Task (bolts.Task)4 ArrayList (java.util.ArrayList)4 JSONException (org.json.JSONException)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Cursor (android.database.Cursor)2 TaskCompletionSource (bolts.TaskCompletionSource)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ContentValues (android.content.ContentValues)1 Location (android.location.Location)1 LocationListener (android.location.LocationListener)1 LocationManager (android.location.LocationManager)1 Bundle (android.os.Bundle)1