use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingAliasTest method aliasesWorkInMultipleScopes.
@Test
public void aliasesWorkInMultipleScopes() {
final Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
class MyService {
private final String id;
MyService(String id) {
this.id = id;
}
@Override
public String toString() {
return "MyService{" + "id=" + id + '}';
}
}
final Object service0 = new MyService("service0");
final Object serviceShared0123P1P2P3 = new MyService("serviceShared0123P1P2P3");
backstack.setGlobalServices(GlobalServices.builder().addService("service0", service0).addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3).addAlias("alias0", service0).addAlias("aliasShared0123P1P2P3", serviceShared0123P1P2P3).build());
final Object service1 = new MyService("service1");
final Object service2 = new MyService("service2");
final Object service3 = new MyService("service3");
final Object serviceShared12 = new MyService("serviceShared12");
final Object serviceShared13 = new MyService("serviceShared13");
final Object serviceShared23 = new MyService("serviceShared23");
final Object serviceShared123 = new MyService("serviceShared123");
final Object serviceShared1P1 = new MyService("serviceShared1P1");
final Object serviceShared1P2 = new MyService("serviceShared1P2");
final Object serviceShared1P3 = new MyService("serviceShared1P3");
final Object serviceShared2P1 = new MyService("serviceShared2P1");
final Object serviceShared2P2 = new MyService("serviceShared2P2");
final Object serviceShared2P3 = new MyService("serviceShared2P3");
final Object serviceShared3P1 = new MyService("serviceShared3P1");
final Object serviceShared3P2 = new MyService("serviceShared3P2");
final Object serviceShared3P3 = new MyService("serviceShared3P3");
final Object serviceP1 = new MyService("serviceP1");
final Object serviceP2 = new MyService("serviceP2");
final Object serviceP3 = new MyService("serviceP3");
TestKeyWithScope beep = new TestKeyWithScope("scope1") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("service1", service1);
serviceBinder.addService("serviceShared12", serviceShared12);
serviceBinder.addService("serviceShared13", serviceShared13);
serviceBinder.addService("serviceShared123", serviceShared123);
serviceBinder.addService("serviceShared1P1", serviceShared1P1);
serviceBinder.addService("serviceShared1P2", serviceShared1P2);
serviceBinder.addService("serviceShared1P3", serviceShared1P3);
serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
}
};
TestKeyWithExplicitParent boop = new TestKeyWithExplicitParent("scope2") {
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("parent1", "parent2");
}
@Override
protected void bindParentServices(ServiceBinder serviceBinder) {
if ("parent1".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("serviceP1", serviceP1);
serviceBinder.addService("serviceShared1P1", serviceShared1P1);
serviceBinder.addService("serviceShared2P1", serviceShared2P1);
serviceBinder.addService("serviceShared3P1", serviceShared3P1);
serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
serviceBinder.addAlias("aliasP1", serviceP1);
serviceBinder.addAlias("aliasShared1P1", serviceShared1P1);
serviceBinder.addAlias("aliasShared2P1", serviceShared2P1);
serviceBinder.addAlias("aliasShared3P1", serviceShared3P1);
serviceBinder.addAlias("aliasShared0123P1P2P3", serviceShared0123P1P2P3);
}
if ("parent2".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("serviceP2", serviceP2);
serviceBinder.addService("serviceShared1P2", serviceShared1P2);
serviceBinder.addService("serviceShared2P2", serviceShared2P2);
serviceBinder.addService("serviceShared3P2", serviceShared3P2);
serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
serviceBinder.addService("serviceP2", serviceP2);
serviceBinder.addService("serviceShared1P2", serviceShared1P2);
serviceBinder.addService("serviceShared2P2", serviceShared2P2);
serviceBinder.addService("serviceShared3P2", serviceShared3P2);
serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
}
}
@Override
protected void bindOwnServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service2", service2);
serviceBinder.addService("serviceShared12", serviceShared12);
serviceBinder.addService("serviceShared23", serviceShared23);
serviceBinder.addService("serviceShared123", serviceShared123);
serviceBinder.addService("serviceShared2P1", serviceShared2P1);
serviceBinder.addService("serviceShared2P2", serviceShared2P2);
serviceBinder.addService("serviceShared2P3", serviceShared2P3);
serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
}
};
TestKeyWithExplicitParent braap = new TestKeyWithExplicitParent("scope3") {
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("parent1", "parent3");
}
@Override
protected void bindParentServices(ServiceBinder serviceBinder) {
if ("parent1".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("serviceP1", serviceP1);
serviceBinder.addService("serviceShared1P1", serviceShared1P1);
serviceBinder.addService("serviceShared2P1", serviceShared2P1);
serviceBinder.addService("serviceShared3P1", serviceShared3P1);
serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
}
if ("parent3".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("serviceP3", serviceP3);
serviceBinder.addService("serviceShared1P3", serviceShared1P3);
serviceBinder.addService("serviceShared2P3", serviceShared2P3);
serviceBinder.addService("serviceShared3P3", serviceShared3P3);
serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
}
}
@Override
protected void bindOwnServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service3", service3);
serviceBinder.addService("serviceShared13", serviceShared13);
serviceBinder.addService("serviceShared23", serviceShared23);
serviceBinder.addService("serviceShared123", serviceShared123);
serviceBinder.addService("serviceShared3P1", serviceShared3P1);
serviceBinder.addService("serviceShared3P2", serviceShared3P2);
serviceBinder.addService("serviceShared3P3", serviceShared3P3);
serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
}
};
/* GLOBAL
* PARENT1
* PARENT2 PARENT3
* BEEP BOOP BRAAP
*/
backstack.setup(History.of(beep, boop, braap));
StateChanger stateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
};
backstack.setStateChanger(stateChanger);
// then
assertThat(backstack.hasService("parent1", "serviceP1")).isTrue();
assertThat(backstack.hasService("parent1", "aliasP1")).isTrue();
assertThat(backstack.canFindFromScope("parent1", "aliasP1")).isTrue();
assertThat(backstack.canFindFromScope("parent1", "aliasP1", ScopeLookupMode.ALL)).isTrue();
assertThat(backstack.canFindFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT)).isTrue();
assertThat(backstack.canFindService("aliasP1")).isTrue();
assertThat(backstack.lookupFromScope("parent1", "aliasP1")).isSameAs(serviceP1);
assertThat(backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.ALL)).isSameAs(serviceP1);
assertThat(backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT)).isSameAs(serviceP1);
assertThat(backstack.lookupService("aliasP1")).isSameAs(serviceP1);
// then
assertThat(backstack.hasService(GlobalServices.SCOPE_TAG, "service0")).isTrue();
assertThat(backstack.hasService(GlobalServices.SCOPE_TAG, "alias0")).isTrue();
assertThat(backstack.canFindFromScope(GlobalServices.SCOPE_TAG, "alias0")).isTrue();
assertThat(backstack.canFindFromScope(GlobalServices.SCOPE_TAG, "alias0", ScopeLookupMode.ALL)).isTrue();
assertThat(backstack.canFindFromScope(GlobalServices.SCOPE_TAG, "alias0", ScopeLookupMode.EXPLICIT)).isTrue();
assertThat(backstack.canFindService("alias0")).isTrue();
assertThat(backstack.lookupFromScope(GlobalServices.SCOPE_TAG, "alias0")).isSameAs(service0);
assertThat(backstack.lookupFromScope(GlobalServices.SCOPE_TAG, "alias0", ScopeLookupMode.ALL)).isSameAs(service0);
assertThat(backstack.lookupFromScope(GlobalServices.SCOPE_TAG, "alias0", ScopeLookupMode.EXPLICIT)).isSameAs(service0);
assertThat(backstack.lookupService("alias0")).isSameAs(service0);
// ALSO
backstack.goBack();
backstack.goBack();
// then
assertThat(backstack.hasService("parent1", "serviceP1")).isFalse();
assertThat(backstack.hasService("parent1", "aliasP1")).isFalse();
assertThat(backstack.canFindFromScope("parent1", "aliasP1")).isFalse();
assertThat(backstack.canFindFromScope("parent1", "aliasP1", ScopeLookupMode.ALL)).isFalse();
assertThat(backstack.canFindFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT)).isFalse();
assertThat(backstack.canFindService("aliasP1")).isFalse();
assertThrows(new Action() {
@Override
public void doSomething() {
backstack.lookupFromScope("parent1", "aliasP1");
}
});
assertThrows(new Action() {
@Override
public void doSomething() {
backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.ALL);
}
});
assertThrows(new Action() {
@Override
public void doSomething() {
backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT);
}
});
assertThrows(new Action() {
@Override
public void doSomething() {
backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.ALL);
}
});
assertThrows(new Action() {
@Override
public void doSomething() {
backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT);
}
});
assertThrows(new Action() {
@Override
public void doSomething() {
backstack.lookupService("aliasP1");
}
});
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingBackEventDispatchTest method onBackDispatchHandlesBackDispatchesToCorrectActiveChainAfterMoveToTop.
@Test
public void onBackDispatchHandlesBackDispatchesToCorrectActiveChainAfterMoveToTop() {
final HandlesBackOnce previousService = new HandlesBackOnce();
final HandlesBackOnce nextService = new HandlesBackOnce();
Object previousKey = new TestKeyWithScope("previous") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service", previousService);
}
};
Object nextKey = new TestKeyWithScope("next") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service", nextService);
}
};
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.of(previousKey, nextKey));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.lookupService("service")).isSameAs(nextService);
backstack.moveToTop(previousKey);
assertThat(backstack.getHistory()).containsExactly(nextKey, previousKey);
assertThat(backstack.lookupService("service")).isSameAs(previousService);
boolean handled;
assertThat(previousService.handledBackOnce).isFalse();
handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(previousService.handledBackOnce).isTrue();
handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(backstack.getHistory()).containsExactly(nextKey);
assertThat(nextService.handledBackOnce).isFalse();
handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(nextService.handledBackOnce).isTrue();
handled = backstack.goBack();
assertThat(handled).isFalse();
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingBackEventDispatchTest method onBackDispatchHandlesBackHandlesInActiveParentScope.
@Test
public void onBackDispatchHandlesBackHandlesInActiveParentScope() {
final HandlesBackOnce previousService = new HandlesBackOnce();
Object previousKey = new TestKeyWithScope("previousKey") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service1", previousService);
}
};
final HandlesBackOnce parentService1 = new HandlesBackOnce();
final HandlesBackOnce parentService2 = new HandlesBackOnce();
final HandlesBackOnce service1 = new HandlesBackOnce();
final HandlesBackOnce service2 = new HandlesBackOnce();
Object key = new TestKeyWithExplicitParent("key") {
@Override
protected void bindParentServices(ServiceBinder serviceBinder) {
if ("parentScope".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("parentService1", parentService1);
serviceBinder.addService("parentService2", parentService2);
}
}
@Override
protected void bindOwnServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service1", service1);
serviceBinder.addService("service2", service2);
}
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("parentScope");
}
};
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.of(previousKey, key));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.lookupService("parentService1")).isSameAs(parentService1);
assertThat(backstack.lookupService("parentService2")).isSameAs(parentService2);
assertThat(backstack.lookupService("service1")).isSameAs(service1);
assertThat(backstack.lookupService("service2")).isSameAs(service2);
boolean handled;
assertThat(parentService1.handledBackOnce).isFalse();
assertThat(parentService2.handledBackOnce).isFalse();
assertThat(service1.handledBackOnce).isFalse();
assertThat(service2.handledBackOnce).isFalse();
handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(parentService1.handledBackOnce).isFalse();
assertThat(parentService2.handledBackOnce).isFalse();
assertThat(service1.handledBackOnce).isFalse();
assertThat(service2.handledBackOnce).isTrue();
handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(parentService1.handledBackOnce).isFalse();
assertThat(parentService2.handledBackOnce).isFalse();
assertThat(service1.handledBackOnce).isTrue();
assertThat(service2.handledBackOnce).isTrue();
handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(parentService1.handledBackOnce).isFalse();
assertThat(parentService2.handledBackOnce).isTrue();
assertThat(service1.handledBackOnce).isTrue();
assertThat(service2.handledBackOnce).isTrue();
handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(parentService1.handledBackOnce).isTrue();
assertThat(parentService2.handledBackOnce).isTrue();
assertThat(service1.handledBackOnce).isTrue();
assertThat(service2.handledBackOnce).isTrue();
assertThat(previousService.handledBackOnce).isFalse();
assertThat(backstack.getHistory()).containsExactly(previousKey, key);
handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(backstack.getHistory()).containsExactly(previousKey);
handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(previousService.handledBackOnce).isTrue();
handled = backstack.goBack();
assertThat(handled).isFalse();
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingBackEventDispatchTest method onBackStackGoBackDispatchesBackToActiveScope.
@Test
public void onBackStackGoBackDispatchesBackToActiveScope() {
final HandlesBackOnce service1 = new HandlesBackOnce();
final HandlesBackOnce service2 = new HandlesBackOnce();
Object key1 = new TestKeyWithScope("key1") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service1", service1);
}
};
Object key2 = new TestKeyWithScope("key2") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
serviceBinder.addService("service2", service2);
}
};
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.of(key1, key2));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(service2.handledBackOnce).isEqualTo(false);
boolean handledBack = backstack.goBack();
assertThat(handledBack).isEqualTo(true);
assertThat(service2.handledBackOnce).isEqualTo(true);
assertThat(backstack.getHistory()).containsExactly(key1, key2);
assertThat(backstack.getHistory()).containsExactly(key1, key2);
assertThat(service2.handledBackOnce).isEqualTo(true);
handledBack = backstack.goBack();
assertThat(handledBack).isEqualTo(true);
assertThat(service2.handledBackOnce).isEqualTo(true);
assertThat(backstack.getHistory()).containsExactly(key1);
assertThat(service1.handledBackOnce).isEqualTo(false);
handledBack = backstack.goBack();
assertThat(handledBack).isEqualTo(true);
assertThat(service1.handledBackOnce).isEqualTo(true);
handledBack = backstack.goBack();
assertThat(handledBack).isEqualTo(false);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingBackEventDispatchTest method onBackDispatchHandlesBackHandlesInActiveParentScopeEvenIfTopIsNotScopeKey.
@Test
public void onBackDispatchHandlesBackHandlesInActiveParentScopeEvenIfTopIsNotScopeKey() {
final HandlesBackOnce parentService = new HandlesBackOnce();
Object key = new TestKeyWithOnlyParentServices("key", History.of("parentScope")) {
@Override
public void bindServices(ServiceBinder serviceBinder) {
if ("parentScope".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("parentService", parentService);
}
}
};
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.lookupService("parentService")).isSameAs(parentService);
assertThat(parentService.handledBackOnce).isFalse();
boolean handled = backstack.goBack();
assertThat(handled).isTrue();
assertThat(parentService.handledBackOnce).isTrue();
handled = backstack.goBack();
assertThat(handled).isFalse();
}
Aggregations