Search in sources :

Example 6 with Capture

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

the class LocationNotifier method getCurrentLocationAsync.

/**
   * Asynchronously gets the current location of the device.
   *
   * This will request location updates from the best provider that match the given criteria
   * and return the first location received.
   *
   * You can customize the criteria to meet your specific needs.
   * * For higher accuracy, you can set {@link Criteria#setAccuracy(int)}, however result in longer
   *   times for a fix.
   * * For better battery efficiency and faster location fixes, you can set
   *   {@link Criteria#setPowerRequirement(int)}, however, this will result in lower accuracy.
   *
   * @param context
   *          The context used to request location updates.
   * @param timeout
   *          The number of milliseconds to allow before timing out.
   * @param criteria
   *          The application criteria for selecting a location provider.
   *
   * @see android.location.LocationManager#getBestProvider(android.location.Criteria, boolean)
   * @see android.location.LocationManager#requestLocationUpdates(String, long, float, android.location.LocationListener)
   */
/* package */
static Task<Location> getCurrentLocationAsync(Context context, long timeout, Criteria criteria) {
    final TaskCompletionSource<Location> tcs = new TaskCompletionSource<>();
    final Capture<ScheduledFuture<?>> timeoutFuture = new Capture<ScheduledFuture<?>>();
    final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    final LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            if (location == null) {
                return;
            }
            timeoutFuture.get().cancel(true);
            tcs.trySetResult(location);
            manager.removeUpdates(this);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    timeoutFuture.set(ParseExecutors.scheduled().schedule(new Runnable() {

        @Override
        public void run() {
            tcs.trySetError(new ParseException(ParseException.TIMEOUT, "Location fetch timed out."));
            manager.removeUpdates(listener);
        }
    }, timeout, TimeUnit.MILLISECONDS));
    String provider = manager.getBestProvider(criteria, true);
    if (provider != null) {
        manager.requestLocationUpdates(provider, /* minTime */
        0, /* minDistance */
        0.0f, listener);
    }
    if (fakeLocation != null) {
        listener.onLocationChanged(fakeLocation);
    }
    return tcs.getTask();
}
Also used : LocationManager(android.location.LocationManager) TaskCompletionSource(bolts.TaskCompletionSource) Bundle(android.os.Bundle) LocationListener(android.location.LocationListener) Capture(bolts.Capture) ScheduledFuture(java.util.concurrent.ScheduledFuture) Location(android.location.Location)

Example 7 with Capture

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

the class ParsePushTest method testSubscribeInBackgroundWithCallbackFail.

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

Example 8 with Capture

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

the class ParsePushTest method testSendInBackgroundWithCallbackSuccess.

@Test
public void testSendInBackgroundWithCallbackSuccess() 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).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
    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();
    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)

Example 9 with Capture

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

the class ParsePushTest method testSubscribeInBackgroundWithCallbackSuccess.

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

Example 10 with Capture

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

the class ParsePushTest method testUnsubscribeInBackgroundWithCallbackSuccess.

@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);
    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();
        }
    });
    assertNull(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)

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