-
Notifications
You must be signed in to change notification settings - Fork 20
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
11 changed files
with
316 additions
and
21 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
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,57 @@ | ||
package com.patloew.rxfit; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import com.google.android.gms.fitness.data.Goal; | ||
import com.google.android.gms.fitness.request.GoalsReadRequest; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import rx.Observable; | ||
import rx.Single; | ||
import rx.functions.Func1; | ||
|
||
/* Copyright 2016 Patrick Löwenstein | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. */ | ||
public class Goals { | ||
|
||
private final RxFit rxFit; | ||
|
||
Goals(RxFit rxFit) { | ||
this.rxFit = rxFit; | ||
} | ||
|
||
|
||
// read current | ||
|
||
public Observable<Goal> readCurrent(@NonNull GoalsReadRequest goalsReadRequest) { | ||
return readCurrentInternal(goalsReadRequest, null, null); | ||
} | ||
|
||
public Observable<Goal> readCurrent(@NonNull GoalsReadRequest goalsReadRequest, long timeout, @NonNull TimeUnit timeUnit) { | ||
return readCurrentInternal(goalsReadRequest, timeout, timeUnit); | ||
} | ||
|
||
private Observable<Goal> readCurrentInternal(GoalsReadRequest goalsReadRequest, Long timeout, TimeUnit timeUnit) { | ||
return Single.create(new GoalsReadCurrentSingle(rxFit, goalsReadRequest, timeout, timeUnit)) | ||
.flatMapObservable(new Func1<List<Goal>, Observable<? extends Goal>>() { | ||
@Override | ||
public Observable<? extends Goal> call(List<Goal> goals) { | ||
return Observable.from(goals); | ||
} | ||
}); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
library/src/main/java/com/patloew/rxfit/GoalsReadCurrentSingle.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,52 @@ | ||
package com.patloew.rxfit; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import com.google.android.gms.common.api.GoogleApiClient; | ||
import com.google.android.gms.common.api.ResultCallback; | ||
import com.google.android.gms.fitness.Fitness; | ||
import com.google.android.gms.fitness.data.Goal; | ||
import com.google.android.gms.fitness.request.GoalsReadRequest; | ||
import com.google.android.gms.fitness.result.GoalsResult; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import rx.SingleSubscriber; | ||
|
||
/* Copyright 2016 Patrick Löwenstein | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. */ | ||
class GoalsReadCurrentSingle extends BaseSingle<List<Goal>> { | ||
|
||
final GoalsReadRequest goalsReadRequest; | ||
|
||
GoalsReadCurrentSingle(RxFit rxFit, GoalsReadRequest goalsReadRequest, Long timeout, TimeUnit timeUnit) { | ||
super(rxFit, timeout, timeUnit); | ||
this.goalsReadRequest = goalsReadRequest; | ||
} | ||
|
||
@Override | ||
protected void onGoogleApiClientReady(GoogleApiClient apiClient, final SingleSubscriber<? super List<Goal>> subscriber) { | ||
setupFitnessPendingResult(Fitness.GoalsApi.readCurrentGoals(apiClient, goalsReadRequest), new ResultCallback<GoalsResult>() { | ||
@Override | ||
public void onResult(@NonNull GoalsResult goalsResult) { | ||
if (!goalsResult.getStatus().isSuccess()) { | ||
subscriber.onError(new StatusException(goalsResult.getStatus())); | ||
} else { | ||
subscriber.onSuccess(goalsResult.getGoals()); | ||
} | ||
} | ||
}); | ||
} | ||
} |
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
104 changes: 104 additions & 0 deletions
104
library/src/test/java/com/patloew/rxfit/GoalsOnSubscribeTest.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,104 @@ | ||
package com.patloew.rxfit; | ||
|
||
import android.app.PendingIntent; | ||
import android.support.v4.content.ContextCompat; | ||
|
||
import com.google.android.gms.common.ConnectionResult; | ||
import com.google.android.gms.common.api.GoogleApiClient; | ||
import com.google.android.gms.common.api.Status; | ||
import com.google.android.gms.fitness.Fitness; | ||
import com.google.android.gms.fitness.data.DataPoint; | ||
import com.google.android.gms.fitness.data.DataSource; | ||
import com.google.android.gms.fitness.data.DataType; | ||
import com.google.android.gms.fitness.data.Goal; | ||
import com.google.android.gms.fitness.request.DataSourcesRequest; | ||
import com.google.android.gms.fitness.request.GoalsReadRequest; | ||
import com.google.android.gms.fitness.request.OnDataPointListener; | ||
import com.google.android.gms.fitness.request.SensorRequest; | ||
import com.google.android.gms.fitness.result.DataSourcesResult; | ||
import com.google.android.gms.fitness.result.GoalsResult; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Matchers; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; | ||
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import rx.Observable; | ||
import rx.Single; | ||
import rx.observers.TestSubscriber; | ||
|
||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@SuppressStaticInitializationFor("com.google.android.gms.fitness.Fitness") | ||
@PrepareOnlyThisForTest({ ContextCompat.class, Fitness.class, Status.class, ConnectionResult.class, BaseRx.class }) | ||
public class GoalsOnSubscribeTest extends BaseOnSubscribeTest { | ||
|
||
@Mock GoalsReadRequest goalsReadRequest; | ||
@Mock Goal goal; | ||
|
||
@Override | ||
@Before | ||
public void setup() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
super.setup(); | ||
} | ||
|
||
// GoalsReadCurrentSingle | ||
|
||
@Test | ||
public void GoalsReadCurrentSingle_Success() { | ||
TestSubscriber<List<Goal>> sub = new TestSubscriber<>(); | ||
GoalsResult goalsResult = Mockito.mock(GoalsResult.class); | ||
|
||
GoalsReadCurrentSingle single = PowerMockito.spy(new GoalsReadCurrentSingle(rxFit, goalsReadRequest, null, null)); | ||
|
||
List<Goal> goalList = new ArrayList<>(); | ||
goalList.add(goal); | ||
when(goalsResult.getGoals()).thenReturn(goalList); | ||
|
||
setPendingResultValue(goalsResult); | ||
when(goalsResult.getStatus()).thenReturn(status); | ||
when(status.isSuccess()).thenReturn(true); | ||
when(goalsApi.readCurrentGoals(apiClient, goalsReadRequest)).thenReturn(pendingResult); | ||
|
||
setupBaseSingleSuccess(single); | ||
Single.create(single).subscribe(sub); | ||
|
||
assertSingleValue(sub, goalList); | ||
} | ||
|
||
@Test | ||
public void GoalsReadCurrentSingle_StatusException() { | ||
TestSubscriber<List<Goal>> sub = new TestSubscriber<>(); | ||
GoalsResult goalsResult = Mockito.mock(GoalsResult.class); | ||
|
||
GoalsReadCurrentSingle single = PowerMockito.spy(new GoalsReadCurrentSingle(rxFit, goalsReadRequest, null, null)); | ||
|
||
List<Goal> goalList = new ArrayList<>(); | ||
goalList.add(goal); | ||
when(goalsResult.getGoals()).thenReturn(goalList); | ||
|
||
setPendingResultValue(goalsResult); | ||
when(goalsResult.getStatus()).thenReturn(status); | ||
when(status.isSuccess()).thenReturn(false); | ||
when(goalsApi.readCurrentGoals(apiClient, goalsReadRequest)).thenReturn(pendingResult); | ||
|
||
setupBaseSingleSuccess(single); | ||
Single.create(single).subscribe(sub); | ||
|
||
assertError(sub, StatusException.class); | ||
} | ||
} |
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,69 @@ | ||
package com.patloew.rxfit; | ||
|
||
import android.app.PendingIntent; | ||
import android.support.v4.content.ContextCompat; | ||
|
||
import com.google.android.gms.common.ConnectionResult; | ||
import com.google.android.gms.common.api.Status; | ||
import com.google.android.gms.fitness.Fitness; | ||
import com.google.android.gms.fitness.data.DataType; | ||
import com.google.android.gms.fitness.request.DataSourcesRequest; | ||
import com.google.android.gms.fitness.request.GoalsReadRequest; | ||
import com.google.android.gms.fitness.request.SensorRequest; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest; | ||
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import rx.Observable; | ||
import rx.Single; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertNull; | ||
import static org.mockito.Mockito.atLeast; | ||
import static org.mockito.Mockito.times; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@SuppressStaticInitializationFor("com.google.android.gms.fitness.Fitness") | ||
@PrepareOnlyThisForTest({ Observable.class, Single.class, ContextCompat.class, Fitness.class, Status.class, ConnectionResult.class }) | ||
public class GoalsTest extends BaseTest { | ||
|
||
@Override | ||
@Before | ||
public void setup() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
PowerMockito.spy(Single.class); | ||
PowerMockito.mockStatic(Observable.class); | ||
super.setup(); | ||
} | ||
|
||
// Read Current | ||
|
||
@Test | ||
public void Goals_ReadCurrent() throws Exception { | ||
ArgumentCaptor<GoalsReadCurrentSingle> captor = ArgumentCaptor.forClass(GoalsReadCurrentSingle.class); | ||
|
||
final GoalsReadRequest request = Mockito.mock(GoalsReadRequest.class); | ||
rxFit.goals().readCurrent(request); | ||
rxFit.goals().readCurrent(request, TIMEOUT_TIME, TIMEOUT_TIMEUNIT); | ||
|
||
PowerMockito.verifyStatic(atLeast(2)); | ||
Single.create(captor.capture()); | ||
|
||
GoalsReadCurrentSingle single = captor.getAllValues().get(0); | ||
assertEquals(request, single.goalsReadRequest); | ||
assertNoTimeoutSet(single); | ||
|
||
single = captor.getAllValues().get(2); | ||
assertEquals(request, single.goalsReadRequest); | ||
assertTimeoutSet(single); | ||
} | ||
|
||
} |
Oops, something went wrong.