use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method scopedServiceCallbackIsCalledCorrectly.
@Test
public void scopedServiceCallbackIsCalledCorrectly() {
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));
assertThat(service.didServiceRegister).isFalse();
assertThat(service.didServiceUnregister).isFalse();
backstack.setStateChanger(stateChanger);
assertThat(service.didServiceRegister).isTrue();
assertThat(service.didServiceUnregister).isFalse();
backstack.setHistory(History.single(testKey1), StateChange.REPLACE);
assertThat(service.didServiceRegister).isTrue();
assertThat(service.didServiceUnregister).isTrue();
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingAliasTest method bundleableNotCalledForAlias.
@Test
public void bundleableNotCalledForAlias() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
class Service implements Bundleable {
private int saved = 0;
private int restored = 0;
@Nonnull
@Override
public StateBundle toBundle() {
saved++;
return new StateBundle();
}
@Override
public void fromBundle(@Nullable StateBundle bundle) {
if (bundle != null) {
restored++;
}
}
}
final Service service = new Service();
TestKeyWithScope boop = new TestKeyWithScope("boop") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service", service);
serviceBinder.addAlias("alias", service);
}
};
backstack.setup(History.of(boop));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.hasService("boop", "service")).isTrue();
assertThat(backstack.hasService("boop", "alias")).isTrue();
StateBundle bundle = backstack.toBundle();
Backstack backstack2 = new Backstack();
backstack2.setScopedServices(new ServiceProvider());
backstack2.setup(History.of(boop));
backstack2.fromBundle(bundle);
backstack2.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(service.saved).isEqualTo(1);
assertThat(service.restored).isEqualTo(1);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingAliasTest method aliasesWork.
@Test
public void aliasesWork() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final Object service = new Object();
TestKeyWithScope boop = new TestKeyWithScope("boop") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service", service);
assertThat(serviceBinder.hasService("alias")).isFalse();
serviceBinder.addAlias("alias", service);
assertThat(serviceBinder.hasService("alias")).isTrue();
}
};
backstack.setup(History.of(boop));
assertThat(backstack.hasService("boop", "service")).isFalse();
assertThat(backstack.hasService("boop", "alias")).isFalse();
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
// then
assertThat(backstack.hasService("boop", "service")).isTrue();
assertThat(backstack.hasService("boop", "alias")).isTrue();
assertThat(backstack.getService("boop", "service")).isSameAs(service);
assertThat(backstack.getService("boop", "alias")).isSameAs(service);
assertThat(backstack.canFindFromScope("boop", "alias")).isTrue();
assertThat(backstack.canFindFromScope("boop", "alias", ScopeLookupMode.ALL)).isTrue();
assertThat(backstack.canFindFromScope("boop", "alias", ScopeLookupMode.EXPLICIT)).isTrue();
assertThat(backstack.canFindService("alias")).isTrue();
assertThat(backstack.lookupFromScope("boop", "alias")).isSameAs(service);
assertThat(backstack.lookupFromScope("boop", "alias", ScopeLookupMode.ALL)).isSameAs(service);
assertThat(backstack.lookupFromScope("boop", "alias", ScopeLookupMode.EXPLICIT)).isSameAs(service);
assertThat(backstack.lookupService("alias")).isSameAs(service);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingBackEventDispatchTest method onBackDispatchToHandlesBackCalledOnceForMultipleRegistrations.
@Test
public void onBackDispatchToHandlesBackCalledOnceForMultipleRegistrations() {
class BackCounter implements ScopedServices.HandlesBack {
private int count = 0;
@Override
public boolean onBackEvent() {
count++;
return false;
}
}
final BackCounter service = new BackCounter();
Object key = new TestKeyWithScope("key") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service1", service);
serviceBinder.addService("service2", service);
}
};
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.of(key));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
boolean handled = backstack.goBack();
assertThat(handled).isFalse();
assertThat(service.count).isEqualTo(1);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingBackEventDispatchTest method onBackDispatchToHandlesBackNotCalledOnAlias.
@Test
public void onBackDispatchToHandlesBackNotCalledOnAlias() {
final HandlesBackOnce service = new HandlesBackOnce();
Object key = new TestKeyWithScope("key") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addAlias("service", service);
}
};
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.of(key));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.getHistory()).containsExactly(key);
assertThat(backstack.lookupService("service")).isSameAs(service);
boolean handled = backstack.goBack();
assertThat(handled).isEqualTo(false);
}
Aggregations