use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method serviceBinderSucceedsIfRootBackstackIsAnAlias.
@Test
public void serviceBinderSucceedsIfRootBackstackIsAnAlias() {
final String serviceTag = "backstack";
final Backstack backstack = new Backstack();
TestKeyWithScope testKeyWithScope = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addAlias(serviceTag, backstack);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.of(testKeyWithScope));
backstack.setStateChanger(stateChanger);
assertThat(backstack.lookupService(serviceTag)).isSameAs(backstack);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method scopeIsDestroyedForClearedScopeKeys.
@Test
public void scopeIsDestroyedForClearedScopeKeys() {
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();
backstack.setHistory(History.of(testKey1), StateChange.REPLACE);
assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isFalse();
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method finalizingScopeTwiceShouldBeNoOp.
@Test
public void finalizingScopeTwiceShouldBeNoOp() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.of(testKey2));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isTrue();
backstack.finalizeScopes();
try {
backstack.finalizeScopes();
} catch (Throwable e) {
Assert.fail("Should be no-op.");
}
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method lookupServiceFromScopeWorks.
@Test
public void lookupServiceFromScopeWorks() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final Object service1 = new Object();
final Object service2 = new Object();
final Object service0 = new Object();
final Object service3 = new Object();
class Key1 extends TestKey implements HasServices {
Key1(String name) {
super(name);
}
protected Key1(Parcel in) {
super(in);
}
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.hasService("service0")).isFalse();
assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service0")).isFalse();
serviceBinder.addService("service0", service0);
assertThat(serviceBinder.hasService("service0")).isTrue();
assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service0")).isTrue();
assertThat(serviceBinder.getService("service0")).isSameAs(service0);
assertThat(serviceBinder.lookupService("service0")).isSameAs(service0);
assertThat(serviceBinder.lookupFromScope(serviceBinder.getScopeTag(), "service0")).isSameAs(service0);
assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service")).isFalse();
serviceBinder.addService("service", service1);
assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service")).isTrue();
assertThat(serviceBinder.lookupFromScope(serviceBinder.getScopeTag(), "service")).isSameAs(service1);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
}
class Key2 extends TestKey implements HasServices {
Key2(String name) {
super(name);
}
protected Key2(Parcel in) {
super(in);
}
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.lookupService("service0")).isSameAs(service0);
assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service")).isTrue();
assertThat(serviceBinder.lookupService("service")).isSameAs(service1);
serviceBinder.addService("service", service2);
// the mostimportant assertion here
assertThat(serviceBinder.lookupService("service")).isSameAs(service2);
assertThat(serviceBinder.lookupFromScope("beep", "service")).isSameAs(service1);
serviceBinder.addService("service3", service3);
}
@Nonnull
@Override
public String getScopeTag() {
return "boop";
}
}
backstack.setup(History.of(new Key1("beep"), new Key2("boop")));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.lookupService("service")).isSameAs(service2);
assertThat(backstack.canFindService("service3")).isTrue();
assertThat(backstack.canFindFromScope("boop", "service3")).isTrue();
assertThat(backstack.lookupFromScope("boop", "service3")).isSameAs(service3);
assertThat(backstack.lookupFromScope("beep", "service")).isSameAs(service1);
assertThat(backstack.lookupFromScope("boop", "service")).isSameAs(service2);
backstack.goBack();
assertThat(backstack.canFindFromScope("boop", "service3")).isFalse();
assertThat(backstack.lookupService("service")).isSameAs(service1);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method nonExistentServiceShouldReturnFalseAndThrow.
@Test
public void nonExistentServiceShouldReturnFalseAndThrow() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.of(testKey2));
assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isFalse();
try {
backstack.getService(testKey2, SERVICE_TAG);
Assert.fail();
} catch (IllegalArgumentException e) {
// OK!
}
}
Aggregations