Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TestScopeProvider + build fixes (#49)
* Fix TestLifecycleScopeProvider compilation This didn't compile before * Fix test * Fix formatting in TestLifecycleScopeProvider * Add TestScopeProvider * Add Tests for TestScopeProvider * Remove object instantiation and just have a single emit() * Remove emitError() APIs * Remove emitComplete() Also not really necessary
- Loading branch information
Showing
4 changed files
with
241 additions
and
94 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
66 changes: 66 additions & 0 deletions
66
autodispose/src/main/java/com/uber/autodispose/TestScopeProvider.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,66 @@ | ||
/* | ||
* Copyright (c) 2017. Uber Technologies | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.uber.autodispose; | ||
|
||
import io.reactivex.Maybe; | ||
import io.reactivex.subjects.MaybeSubject; | ||
|
||
import static com.uber.autodispose.ScopeUtil.LifecycleEndNotification.INSTANCE; | ||
|
||
/** | ||
* ScopeProvider implementation for testing. You can either back it with your own instance, or just | ||
* stub it in place and use its public emit APIs. | ||
*/ | ||
public final class TestScopeProvider implements ScopeProvider { | ||
|
||
/** | ||
* Creates a new provider backed by an internal MaybeSubject. Useful for stubbing or if you only | ||
* want to use the emit APIs | ||
* | ||
* @return the created TestScopeProvider. | ||
*/ | ||
public static TestScopeProvider create() { | ||
return create(MaybeSubject.create()); | ||
} | ||
|
||
/** | ||
* Creates a new provider backed by {@code delegate}. | ||
* | ||
* @param delegate the delegate to back this with. | ||
* @return the created TestScopeProvider. | ||
*/ | ||
public static TestScopeProvider create(Maybe<?> delegate) { | ||
return new TestScopeProvider(delegate); | ||
} | ||
|
||
private final MaybeSubject<Object> innerMaybe = MaybeSubject.create(); | ||
|
||
private TestScopeProvider(Maybe<?> delegate) { | ||
delegate.subscribe(innerMaybe); | ||
} | ||
|
||
@Override public Maybe<?> requestScope() { | ||
return innerMaybe; | ||
} | ||
|
||
/** | ||
* Emits a success event, just a simple Object. | ||
*/ | ||
public void emit() { | ||
innerMaybe.onSuccess(INSTANCE); | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
autodispose/src/test/java/com/uber/autodispose/TestScopeProviderTest.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,76 @@ | ||
/* | ||
* Copyright (c) 2017. Uber Technologies | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.uber.autodispose; | ||
|
||
import io.reactivex.observers.TestObserver; | ||
import io.reactivex.subjects.MaybeSubject; | ||
import org.junit.Test; | ||
|
||
public class TestScopeProviderTest { | ||
|
||
private final TestObserver<Object> o = new TestObserver<>(); | ||
|
||
@Test public void noArgs() { | ||
TestScopeProvider provider = TestScopeProvider.create(); | ||
provider.requestScope() | ||
.subscribe(o); | ||
|
||
provider.emit(); | ||
o.assertValueCount(1); | ||
} | ||
|
||
@Test public void delegateArg() { | ||
MaybeSubject<Integer> s = MaybeSubject.create(); | ||
TestScopeProvider provider = TestScopeProvider.create(s); | ||
provider.requestScope() | ||
.subscribe(o); | ||
|
||
provider.emit(); | ||
o.assertValueCount(1); | ||
} | ||
|
||
@Test public void delegateArgEmits() { | ||
MaybeSubject<Integer> s = MaybeSubject.create(); | ||
TestScopeProvider provider = TestScopeProvider.create(s); | ||
provider.requestScope() | ||
.subscribe(o); | ||
|
||
s.onSuccess(1); | ||
o.assertValueCount(1); | ||
o.assertValue(1); | ||
} | ||
|
||
@Test public void delegateArg_error() { | ||
MaybeSubject<Integer> s = MaybeSubject.create(); | ||
TestScopeProvider provider = TestScopeProvider.create(s); | ||
provider.requestScope() | ||
.subscribe(o); | ||
|
||
s.onError(new IllegalArgumentException()); | ||
o.assertError(IllegalArgumentException.class); | ||
} | ||
|
||
@Test public void delegateArg_complete() { | ||
MaybeSubject<Integer> s = MaybeSubject.create(); | ||
TestScopeProvider provider = TestScopeProvider.create(s); | ||
provider.requestScope() | ||
.subscribe(o); | ||
|
||
s.onComplete(); | ||
o.assertComplete(); | ||
} | ||
} |