use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method scopeServicesArePersistedToStateBundle.
@Test
public void scopeServicesArePersistedToStateBundle() {
final Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final Service service = new Service();
TestKeyWithScope testKeyWithScope = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService(SERVICE_TAG, service);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
backstack.setup(History.of(testKeyWithScope));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.hasService(testKeyWithScope.getScopeTag(), SERVICE_TAG)).isTrue();
StateBundle stateBundle = backstack.toBundle();
// noinspection ConstantConditions
// backstack.getScopesTag() is internal
assertThat(stateBundle.getBundle(Backstack.getScopesTag()).getBundle(testKeyWithScope.getScopeTag()).getBundle(SERVICE_TAG).getInt("blah")).isEqualTo(5);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method gettingNonExistentServiceThrows.
@Test
public void gettingNonExistentServiceThrows() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.single(testKey2));
assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isFalse();
backstack.setStateChanger(stateChanger);
assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isTrue();
try {
backstack.getService(testKey2, "d'oh");
Assert.fail();
} catch (IllegalArgumentException e) {
// OK!
}
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method lookupServiceWithOverlapsWorks.
@Test
public void lookupServiceWithOverlapsWorks() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final Service service1 = new Service();
final Service service2 = new Service();
TestKeyWithScope beep = new TestKeyWithScope("beep") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService(SERVICE_TAG, service1);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
TestKeyWithScope boop = new TestKeyWithScope("boop") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService(SERVICE_TAG, service2);
}
@Nonnull
@Override
public String getScopeTag() {
return "boop";
}
};
backstack.setup(History.of(beep, boop));
assertThat(backstack.hasScope("beep")).isFalse();
assertThat(backstack.hasScope("boop")).isFalse();
assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
backstack.setStateChanger(stateChanger);
assertThat(backstack.hasScope("beep")).isTrue();
assertThat(backstack.hasScope("boop")).isTrue();
assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
assertThat(backstack.hasService("boop", SERVICE_TAG)).isTrue();
assertThat(backstack.<Object>lookupService(SERVICE_TAG)).isSameAs(service2);
backstack.goBack();
assertThat(backstack.hasScope("beep")).isTrue();
assertThat(backstack.hasScope("boop")).isFalse();
assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
assertThat(backstack.<Object>lookupService(SERVICE_TAG)).isSameAs(service1);
backstack.setHistory(History.single(testKey1), StateChange.REPLACE);
assertThat(backstack.hasScope("beep")).isFalse();
assertThat(backstack.hasScope("boop")).isFalse();
assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
try {
backstack.lookupService(SERVICE_TAG);
Assert.fail();
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains("does not exist in any scope");
// OK!
}
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method scopedServicesCanBeSetBeforeInitialStateChange.
@Test
public void scopedServicesCanBeSetBeforeInitialStateChange() {
Backstack backstack = new Backstack();
backstack.setup(History.of(testKey1));
backstack.setScopedServices(new ServiceProvider());
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
try {
backstack.setScopedServices(new ServiceProvider());
Assert.fail();
} catch (IllegalStateException e) {
// OK!
}
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method activationIsCalledOnlyOnce.
@Test
public void activationIsCalledOnlyOnce() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final List<Object> activatedServices = new ArrayList<>();
final List<Object> deactivatedServices = new ArrayList<>();
class MyService implements ScopedServices.Activated {
@Override
public void onServiceActive() {
activatedServices.add(this);
}
@Override
public void onServiceInactive() {
deactivatedServices.add(this);
}
}
final MyService service1 = new MyService();
TestKeyWithScope beep = new TestKeyWithScope("beep") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE1", service1);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
backstack.setup(History.of(beep));
assertThat(activatedServices).isEmpty();
assertThat(deactivatedServices).isEmpty();
backstack.setStateChanger(stateChanger);
assertThat(activatedServices).containsExactly(service1);
backstack.removeStateChanger();
backstack.setStateChanger(stateChanger);
assertThat(activatedServices).containsExactly(service1);
}
Aggregations