use of com.zhuinden.simplestack.helpers.TestKeyWithScope 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.TestKeyWithScope 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.TestKeyWithScope 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.TestKeyWithScope 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);
}
use of com.zhuinden.simplestack.helpers.TestKeyWithScope in project simple-stack by Zhuinden.
the class ScopingBackEventDispatchTest method onBackDispatchToHandlesBackCalledInReverseOrder.
@Test
public void onBackDispatchToHandlesBackCalledInReverseOrder() {
final HandlesBackOnce service1 = new HandlesBackOnce();
final HandlesBackOnce service2 = new HandlesBackOnce();
Object key = new TestKeyWithScope("key") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service1", service1);
serviceBinder.addService("service2", service2);
}
};
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("service1")).isSameAs(service1);
assertThat(backstack.lookupService("service2")).isSameAs(service2);
assertThat(service1.handledBackOnce).isEqualTo(false);
assertThat(service2.handledBackOnce).isEqualTo(false);
boolean handled = backstack.goBack();
assertThat(handled).isEqualTo(true);
assertThat(service1.handledBackOnce).isEqualTo(false);
assertThat(service2.handledBackOnce).isEqualTo(true);
handled = backstack.goBack();
assertThat(handled).isEqualTo(true);
assertThat(service1.handledBackOnce).isEqualTo(true);
assertThat(service2.handledBackOnce).isEqualTo(true);
handled = backstack.goBack();
assertThat(handled).isEqualTo(false);
}
Aggregations