-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,709 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
library/src/test/java/com/patloew/rxlocation/ActivityOnSubscribeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.patloew.rxlocation; | ||
|
||
import android.app.PendingIntent; | ||
|
||
import com.google.android.gms.common.ConnectionResult; | ||
import com.google.android.gms.common.api.Status; | ||
import com.google.android.gms.location.LocationServices; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import io.reactivex.Single; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
@SuppressWarnings("MissingPermission") | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareOnlyThisForTest({ LocationServices.class, com.google.android.gms.location.ActivityRecognition.class, Status.class, ConnectionResult.class, BaseRx.class }) | ||
public class ActivityOnSubscribeTest extends BaseOnSubscribeTest { | ||
|
||
@Mock PendingIntent pendingIntent; | ||
|
||
@Override | ||
@Before | ||
public void setup() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
super.setup(); | ||
} | ||
|
||
// ActivityRequestUpdatesSingle | ||
|
||
@Test | ||
public void ActivityRequestUpdatesSingle_Success() { | ||
ActivityRequestUpdatesSingle single = PowerMockito.spy(new ActivityRequestUpdatesSingle(rxLocation, 1L, pendingIntent, null, null)); | ||
|
||
setPendingResultValue(status); | ||
when(status.isSuccess()).thenReturn(true); | ||
when(activityRecognitionApi.requestActivityUpdates(apiClient, 1L, pendingIntent)).thenReturn(pendingResult); | ||
|
||
setupBaseSingleSuccess(single); | ||
|
||
assertSingleValue(Single.create(single).test(), status); | ||
} | ||
|
||
@Test | ||
public void ActivityRequestUpdatesSingle_StatusException() { | ||
ActivityRequestUpdatesSingle single = PowerMockito.spy(new ActivityRequestUpdatesSingle(rxLocation, 1L, pendingIntent, null, null)); | ||
|
||
setPendingResultValue(status); | ||
when(status.isSuccess()).thenReturn(false); | ||
when(activityRecognitionApi.requestActivityUpdates(apiClient, 1L, pendingIntent)).thenReturn(pendingResult); | ||
|
||
setupBaseSingleSuccess(single); | ||
|
||
assertError(Single.create(single).test(), StatusException.class); | ||
} | ||
|
||
// ActivityRemoveUpdatesSingle | ||
|
||
@Test | ||
public void ActivityRemoveUpdatesSingle_Success() { | ||
ActivityRemoveUpdatesSingle single = PowerMockito.spy(new ActivityRemoveUpdatesSingle(rxLocation, pendingIntent, null, null)); | ||
|
||
setPendingResultValue(status); | ||
when(status.isSuccess()).thenReturn(true); | ||
when(activityRecognitionApi.removeActivityUpdates(apiClient, pendingIntent)).thenReturn(pendingResult); | ||
|
||
setupBaseSingleSuccess(single); | ||
|
||
assertSingleValue(Single.create(single).test(), status); | ||
} | ||
|
||
@Test | ||
public void ActivityRemoveUpdatesSingle_StatusException() { | ||
ActivityRemoveUpdatesSingle single = PowerMockito.spy(new ActivityRemoveUpdatesSingle(rxLocation, pendingIntent, null, null)); | ||
|
||
setPendingResultValue(status); | ||
when(status.isSuccess()).thenReturn(false); | ||
when(activityRecognitionApi.removeActivityUpdates(apiClient, pendingIntent)).thenReturn(pendingResult); | ||
|
||
setupBaseSingleSuccess(single); | ||
|
||
assertError(Single.create(single).test(), StatusException.class); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
library/src/test/java/com/patloew/rxlocation/ActivityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.patloew.rxlocation; | ||
|
||
import android.app.PendingIntent; | ||
|
||
import com.google.android.gms.common.ConnectionResult; | ||
import com.google.android.gms.common.api.Status; | ||
import com.google.android.gms.location.LocationServices; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import io.reactivex.Single; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static org.mockito.Mockito.times; | ||
|
||
@SuppressWarnings("MissingPermission") | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareOnlyThisForTest({ Single.class, LocationServices.class, com.google.android.gms.location.ActivityRecognition.class, Status.class, ConnectionResult.class }) | ||
public class ActivityTest extends BaseTest { | ||
|
||
@Mock PendingIntent pendingIntent; | ||
|
||
@Override | ||
@Before | ||
public void setup() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
PowerMockito.spy(Single.class); | ||
super.setup(); | ||
} | ||
|
||
// Request Updates | ||
|
||
@Test | ||
public void Activity_RequestUpdates() throws Exception { | ||
ArgumentCaptor<ActivityRequestUpdatesSingle> captor = ArgumentCaptor.forClass(ActivityRequestUpdatesSingle.class); | ||
|
||
long detectionIntervalMillis = 123L; | ||
rxLocation.activity().requestUpdates(detectionIntervalMillis,pendingIntent); | ||
rxLocation.activity().requestUpdates(detectionIntervalMillis,pendingIntent, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); | ||
|
||
PowerMockito.verifyStatic(times(2)); | ||
Single.create(captor.capture()); | ||
|
||
ActivityRequestUpdatesSingle single = captor.getAllValues().get(0); | ||
assertEquals(detectionIntervalMillis, single.detectionIntervalMillis); | ||
assertEquals(pendingIntent, single.pendingIntent); | ||
assertNoTimeoutSet(single); | ||
|
||
single = captor.getAllValues().get(1); | ||
assertEquals(detectionIntervalMillis, single.detectionIntervalMillis); | ||
assertEquals(pendingIntent, single.pendingIntent); | ||
assertTimeoutSet(single); | ||
} | ||
|
||
// Remove Updates | ||
|
||
@Test | ||
public void Activity_RemoveUpdates() throws Exception { | ||
ArgumentCaptor<ActivityRemoveUpdatesSingle> captor = ArgumentCaptor.forClass(ActivityRemoveUpdatesSingle.class); | ||
|
||
rxLocation.activity().removeUpdates(pendingIntent); | ||
rxLocation.activity().removeUpdates(pendingIntent, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); | ||
|
||
PowerMockito.verifyStatic(times(2)); | ||
Single.create(captor.capture()); | ||
|
||
ActivityRemoveUpdatesSingle single = captor.getAllValues().get(0); | ||
assertEquals(pendingIntent, single.pendingIntent); | ||
assertNoTimeoutSet(single); | ||
|
||
single = captor.getAllValues().get(1); | ||
assertEquals(pendingIntent, single.pendingIntent); | ||
assertTimeoutSet(single); | ||
} | ||
|
||
|
||
} |
109 changes: 109 additions & 0 deletions
109
library/src/test/java/com/patloew/rxlocation/BaseMaybeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.patloew.rxlocation; | ||
|
||
import android.support.v4.content.ContextCompat; | ||
|
||
import com.google.android.gms.common.ConnectionResult; | ||
import com.google.android.gms.common.api.Api; | ||
import com.google.android.gms.common.api.GoogleApiClient; | ||
import com.google.android.gms.common.api.Status; | ||
import com.google.android.gms.location.ActivityRecognition; | ||
import com.google.android.gms.location.LocationServices; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Matchers; | ||
import org.mockito.MockitoAnnotations; | ||
import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import io.reactivex.Maybe; | ||
import io.reactivex.MaybeEmitter; | ||
import io.reactivex.SingleEmitter; | ||
import io.reactivex.observers.TestObserver; | ||
|
||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.spy; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareOnlyThisForTest({ ContextCompat.class, Status.class, LocationServices.class, ActivityRecognition.class, ConnectionResult.class, SingleEmitter.class }) | ||
public class BaseMaybeTest extends BaseOnSubscribeTest { | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
super.setup(); | ||
} | ||
|
||
@Test | ||
public void BaseObservable_ApiClient_Connected() { | ||
final Object object = new Object(); | ||
BaseMaybe<Object> single = spy(new BaseMaybe<Object>(ctx, new Api[] {}, null) { | ||
@Override | ||
protected void onGoogleApiClientReady(GoogleApiClient apiClient, MaybeEmitter<? super Object> emitter) { | ||
emitter.onSuccess(object); | ||
} | ||
}); | ||
|
||
doAnswer(invocation -> { | ||
BaseRx.ApiClientConnectionCallbacks callbacks = invocation.getArgumentAt(0, BaseRx.ApiClientConnectionCallbacks.class); | ||
callbacks.setClient(apiClient); | ||
callbacks.onConnected(null); | ||
return apiClient; | ||
}).when(single).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); | ||
|
||
TestObserver<Object> sub = Maybe.create(single).test(); | ||
|
||
sub.assertValue(object); | ||
sub.assertComplete(); | ||
} | ||
|
||
@Test | ||
public void BaseObservable_ApiClient_ConnectionSuspended() { | ||
final Object object = new Object(); | ||
BaseMaybe<Object> single = spy(new BaseMaybe<Object>(ctx, new Api[] {}, null) { | ||
@Override | ||
protected void onGoogleApiClientReady(GoogleApiClient apiClient, MaybeEmitter<? super Object> emitter) { | ||
emitter.onSuccess(object); | ||
} | ||
}); | ||
|
||
doAnswer(invocation -> { | ||
BaseRx.ApiClientConnectionCallbacks callbacks = invocation.getArgumentAt(0, BaseRx.ApiClientConnectionCallbacks.class); | ||
callbacks.setClient(apiClient); | ||
callbacks.onConnectionSuspended(0); | ||
return apiClient; | ||
}).when(single).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); | ||
|
||
TestObserver<Object> sub = Maybe.create(single).test(); | ||
|
||
sub.assertNoValues(); | ||
sub.assertError(GoogleAPIConnectionSuspendedException.class); | ||
} | ||
|
||
@Test | ||
public void BaseObservable_ApiClient_ConnectionFailed() { | ||
final Object object = new Object(); | ||
BaseMaybe<Object> single = spy(new BaseMaybe<Object>(ctx, new Api[] {}, null) { | ||
@Override | ||
protected void onGoogleApiClientReady(GoogleApiClient apiClient, MaybeEmitter<? super Object> emitter) { | ||
emitter.onSuccess(object); | ||
} | ||
}); | ||
|
||
doReturn(false).when(connectionResult).hasResolution(); | ||
|
||
doAnswer(invocation -> { | ||
BaseRx.ApiClientConnectionCallbacks callbacks = invocation.getArgumentAt(0, BaseRx.ApiClientConnectionCallbacks.class); | ||
callbacks.setClient(apiClient); | ||
callbacks.onConnectionFailed(connectionResult); | ||
return apiClient; | ||
}).when(single).createApiClient(Matchers.any(BaseRx.ApiClientConnectionCallbacks.class)); | ||
|
||
TestObserver<Object> sub = Maybe.create(single).test(); | ||
|
||
sub.assertNoValues(); | ||
sub.assertError(GoogleAPIConnectionException.class); | ||
} | ||
} |
Oops, something went wrong.