Skip to content

Commit

Permalink
Adding IdelResource for testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin committed Feb 18, 2019
1 parent 299b960 commit d48bc5e
Showing 1 changed file with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,58 +1,71 @@
package com.mapbox.android.core.location;

import android.content.Context;
import android.os.Looper;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingRegistry;
import android.support.test.espresso.idling.CountingIdlingResource;
import android.support.test.rule.GrantPermissionRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

import static java.util.concurrent.TimeUnit.SECONDS;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
public class LocationEngineInstrumentedTest {
private static final long INTERVAL = 1000L;

private CountingIdlingResource idlingResource;
private static LocationEngine[] foregroundLocationEngines = {
getGoogleEngine(),
// getGoogleEngine(),
getMapboxEngine()};

@Rule
public GrantPermissionRule permissionRule =
GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);

@Before
public void beforeTest() {
idlingResource = new CountingIdlingResource("name");
IdlingRegistry.getInstance().register(idlingResource);
}

@After
public void afterTest() {
IdlingRegistry.getInstance().unregister(idlingResource);
}

@Test
public void getLastLocation() throws Exception {
for (LocationEngine engine : foregroundLocationEngines) {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<LocationEngineResult> resultRef = new AtomicReference<>();

engine.getLastLocation(getCallback(resultRef, latch));
assertTrue(latch.await(5, SECONDS));

engine.getLastLocation(getCallback(resultRef, idlingResource));
LocationEngineResult result = resultRef.get();
assertNotNull(result);
assertNotNull(result.getLastLocation());
}
}

@Test
public void requestAndRemoveLocationUpdates() throws Exception {
for (LocationEngine engine : foregroundLocationEngines) {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<LocationEngineResult> resultRef = new AtomicReference<>();
LocationEngineCallback<LocationEngineResult> callback = getCallback(resultRef, latch);

engine.requestLocationUpdates(getRequest(INTERVAL, LocationEngineRequest.PRIORITY_LOW_POWER), callback, null);
assertTrue(latch.await(5, SECONDS));
LocationEngineCallback<LocationEngineResult> callback = getCallback(resultRef, idlingResource);
// idlingResource.increment();

engine.requestLocationUpdates(getRequest(INTERVAL, LocationEngineRequest.PRIORITY_LOW_POWER), callback, Looper.getMainLooper());
Espresso.onIdle();
LocationEngineResult result = resultRef.get();
assertNotNull(result);
assertNotNull(result.getLastLocation());
engine.removeLocationUpdates(callback);
}
Expand Down Expand Up @@ -83,16 +96,17 @@ private static LocationEngineRequest getRequest(long interval, int priority) {

private static LocationEngineCallback<LocationEngineResult> getCallback(
final AtomicReference<LocationEngineResult> resultRef,
final CountDownLatch latch) {
final CountingIdlingResource resource) {
return new LocationEngineCallback<LocationEngineResult>() {
@Override
public void onSuccess(LocationEngineResult result) {
resultRef.set(result);
latch.countDown();
resource.decrement();
}

@Override
public void onFailure(Exception exception) {
resource.decrement();
exception.printStackTrace();
}
};
Expand Down

0 comments on commit d48bc5e

Please sign in to comment.