use of com.zhuinden.simplestack.helpers.HasParentServices in project simple-stack by Zhuinden.
the class ScopingGlobalScopeTest method globalScopeLookupWorks.
@Test
public void globalScopeLookupWorks() {
final Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final Object globalService = new Object();
backstack.setGlobalServices(GlobalServices.builder().addService("parentService2", globalService).build());
final Object parentService1 = new Object();
final Object parentService2 = new Object();
final Object service1 = new Object();
final Object service2 = new Object();
class Key1 extends TestKey implements HasServices, HasParentServices {
Key1(String name) {
super(name);
}
protected Key1(Parcel in) {
super(in);
}
@Override
public void bindServices(ServiceBinder serviceBinder) {
if ("parent1".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("parentService1", parentService1);
} else if (name.equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("service1", service1);
}
}
@Nonnull
@Override
public String getScopeTag() {
return name;
}
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("parent1");
}
}
class Key2 extends TestKey implements HasServices, HasParentServices {
Key2(String name) {
super(name);
}
protected Key2(Parcel in) {
super(in);
}
@Override
public void bindServices(ServiceBinder serviceBinder) {
if ("parent2".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("parentService2", parentService2);
} else if (name.equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("service2", service2);
}
}
@Nonnull
@Override
public String getScopeTag() {
return name;
}
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("parent2");
}
}
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.canFindFromScope("boop", "parentService2", ScopeLookupMode.EXPLICIT)).isTrue();
assertThat(backstack.canFindFromScope("beep", "parentService2", ScopeLookupMode.EXPLICIT)).isTrue();
assertThat(backstack.lookupFromScope("boop", "parentService2", ScopeLookupMode.EXPLICIT)).isSameAs(parentService2);
assertThat(backstack.lookupFromScope("beep", "parentService2", ScopeLookupMode.EXPLICIT)).isSameAs(globalService);
assertThat(backstack.canFindFromScope("boop", "parentService2", ScopeLookupMode.ALL)).isTrue();
assertThat(backstack.canFindFromScope("beep", "parentService2", ScopeLookupMode.ALL)).isTrue();
assertThat(backstack.lookupFromScope("boop", "parentService2", ScopeLookupMode.ALL)).isSameAs(parentService2);
assertThat(backstack.lookupFromScope("beep", "parentService2", ScopeLookupMode.ALL)).isSameAs(globalService);
assertThat(backstack.lookupService("parentService2")).isSameAs(parentService2);
backstack.goBack();
assertThat(backstack.lookupService("parentService2")).isSameAs(globalService);
}
use of com.zhuinden.simplestack.helpers.HasParentServices in project simple-stack by Zhuinden.
the class ScopingGlobalScopeTest method newHistoryShouldReinitializeScopes.
@Test
public void newHistoryShouldReinitializeScopes() {
final List<Pair<Object, ServiceEvent>> events = new ArrayList<>();
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
class MyService implements ScopedServices.Activated, ScopedServices.Registered {
private int id = 0;
MyService(int id) {
this.id = id;
}
@Override
public void onServiceActive() {
events.add(Pair.of((Object) this, ServiceEvent.ACTIVE));
}
@Override
public void onServiceInactive() {
events.add(Pair.of((Object) this, ServiceEvent.INACTIVE));
}
@Override
public String toString() {
return "MyService{" + "id=" + id + '}';
}
@Override
public void onServiceRegistered() {
events.add(Pair.of((Object) this, ServiceEvent.CREATE));
}
@Override
public void onServiceUnregistered() {
events.add(Pair.of((Object) this, ServiceEvent.DESTROY));
}
}
final MyService globalService = new MyService(0);
final MyService explicitParentService = new MyService(1);
final MyService implicitParentService = new MyService(2);
final MyService currentScopeService = new MyService(3);
abstract class TestKeyWithExplicitParent extends TestKeyWithScope implements HasParentServices {
TestKeyWithExplicitParent(String name) {
super(name);
}
protected TestKeyWithExplicitParent(Parcel in) {
super(in);
}
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("explicitParentScope");
}
@Override
public final void bindServices(ServiceBinder serviceBinder) {
if ("explicitParentScope".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("explicitParentService", explicitParentService);
}
if (name.equals(serviceBinder.getScopeTag())) {
bindOwnServices(serviceBinder);
}
}
abstract void bindOwnServices(ServiceBinder serviceBinder);
}
TestKeyWithScope beep = new TestKeyWithExplicitParent("beep") {
@Override
void bindOwnServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("implicitParentService", implicitParentService);
}
};
TestKeyWithScope boop = new TestKeyWithExplicitParent("boop") {
@Override
void bindOwnServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("currentScopeService", currentScopeService);
}
};
backstack.setGlobalServices(GlobalServices.builder().addService("globalService", globalService).build());
backstack.setup(History.of(beep, boop));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.canFindService("globalService")).isTrue();
assertThat(backstack.canFindService("currentScopeService")).isTrue();
assertThat(backstack.canFindService("implicitParentService")).isTrue();
assertThat(backstack.canFindService("explicitParentService")).isTrue();
backstack.finalizeScopes();
assertThat(backstack.canFindService("currentScopeService")).isFalse();
assertThat(backstack.canFindService("implicitParentService")).isFalse();
assertThat(backstack.canFindService("explicitParentService")).isFalse();
assertThat(backstack.canFindService("globalService")).isFalse();
backstack.setHistory(History.of(beep, boop), StateChange.REPLACE);
assertThat(backstack.canFindService("globalService")).isTrue();
assertThat(backstack.canFindService("currentScopeService")).isTrue();
assertThat(backstack.canFindService("implicitParentService")).isTrue();
assertThat(backstack.canFindService("explicitParentService")).isTrue();
assertThat(backstack.hasService("explicitParentScope", "explicitParentService")).isTrue();
assertThat(events).containsExactly(Pair.of((Object) globalService, ServiceEvent.CREATE), Pair.of((Object) explicitParentService, ServiceEvent.CREATE), Pair.of((Object) implicitParentService, ServiceEvent.CREATE), Pair.of((Object) currentScopeService, ServiceEvent.CREATE), Pair.of((Object) globalService, ServiceEvent.ACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.INACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.INACTIVE), Pair.of((Object) globalService, ServiceEvent.INACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.DESTROY), Pair.of((Object) implicitParentService, ServiceEvent.DESTROY), Pair.of((Object) explicitParentService, ServiceEvent.DESTROY), Pair.of((Object) globalService, ServiceEvent.DESTROY), Pair.of((Object) globalService, ServiceEvent.CREATE), Pair.of((Object) explicitParentService, ServiceEvent.CREATE), Pair.of((Object) implicitParentService, ServiceEvent.CREATE), Pair.of((Object) currentScopeService, ServiceEvent.CREATE), Pair.of((Object) globalService, ServiceEvent.ACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.ACTIVE));
// this is just to check things, the test'simportant part is the one above
backstack.goBack();
assertThat(events).containsExactly(Pair.of((Object) globalService, ServiceEvent.CREATE), Pair.of((Object) explicitParentService, ServiceEvent.CREATE), Pair.of((Object) implicitParentService, ServiceEvent.CREATE), Pair.of((Object) currentScopeService, ServiceEvent.CREATE), Pair.of((Object) globalService, ServiceEvent.ACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.INACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.INACTIVE), Pair.of((Object) globalService, ServiceEvent.INACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.DESTROY), Pair.of((Object) implicitParentService, ServiceEvent.DESTROY), Pair.of((Object) explicitParentService, ServiceEvent.DESTROY), Pair.of((Object) globalService, ServiceEvent.DESTROY), Pair.of((Object) globalService, ServiceEvent.CREATE), Pair.of((Object) explicitParentService, ServiceEvent.CREATE), Pair.of((Object) implicitParentService, ServiceEvent.CREATE), Pair.of((Object) currentScopeService, ServiceEvent.CREATE), Pair.of((Object) globalService, ServiceEvent.ACTIVE), Pair.of((Object) explicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.ACTIVE), Pair.of((Object) implicitParentService, ServiceEvent.ACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.INACTIVE), Pair.of((Object) currentScopeService, ServiceEvent.DESTROY));
}
use of com.zhuinden.simplestack.helpers.HasParentServices in project simple-stack by Zhuinden.
the class ScopingRegisteredCallbackTest method registeredWorks.
@Test
public void registeredWorks() {
final List<Pair<Object, ? extends ServiceEvent>> events = new ArrayList<>();
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
class MyService implements ScopedServices.Activated, ScopedServices.Registered {
private final String id;
MyService(String id) {
this.id = id;
}
@Override
public void onServiceActive() {
events.add(Pair.of((Object) this, new ActiveEvent()));
}
@Override
public void onServiceInactive() {
events.add(Pair.of((Object) this, new InactiveEvent()));
}
@Override
public void onServiceRegistered() {
events.add(Pair.of((Object) this, new RegisterEvent()));
}
@Override
public void onServiceUnregistered() {
events.add(Pair.of((Object) this, new UnregisterEvent()));
}
@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).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);
}
};
abstract class TestKeyWithExplicitParent extends TestKeyWithScope implements HasParentServices {
TestKeyWithExplicitParent(String name) {
super(name);
}
protected TestKeyWithExplicitParent(Parcel in) {
super(in);
}
@Override
public final void bindServices(ServiceBinder serviceBinder) {
if (name.equals(serviceBinder.getScopeTag())) {
bindOwnServices(serviceBinder);
} else {
bindParentServices(serviceBinder);
}
}
abstract void bindParentServices(ServiceBinder serviceBinder);
abstract void bindOwnServices(ServiceBinder serviceBinder);
}
TestKeyWithExplicitParent boop = new TestKeyWithExplicitParent("scope2") {
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("parent1", "parent2");
}
@Override
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 ("parent2".equals(serviceBinder.getScopeTag())) {
serviceBinder.addService("serviceP2", serviceP2);
serviceBinder.addService("serviceShared1P2", serviceShared1P2);
serviceBinder.addService("serviceShared2P2", serviceShared2P2);
serviceBinder.addService("serviceShared3P2", serviceShared3P2);
serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
}
}
@Override
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
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
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);
backstack.goBack();
backstack.goBack();
backstack.finalizeScopes();
backstack.goTo(beep);
assertThat(events).containsExactly(Pair.of(service0, new RegisterEvent()), Pair.of(serviceShared0123P1P2P3, new RegisterEvent()), Pair.of(service1, new RegisterEvent()), Pair.of(serviceShared12, new RegisterEvent()), Pair.of(serviceShared13, new RegisterEvent()), Pair.of(serviceShared123, new RegisterEvent()), Pair.of(serviceShared1P1, new RegisterEvent()), Pair.of(serviceShared1P2, new RegisterEvent()), Pair.of(serviceShared1P3, new RegisterEvent()), Pair.of(serviceP1, new RegisterEvent()), Pair.of(serviceShared2P1, new RegisterEvent()), Pair.of(serviceShared3P1, new RegisterEvent()), Pair.of(serviceP2, new RegisterEvent()), Pair.of(serviceShared2P2, new RegisterEvent()), Pair.of(serviceShared3P2, new RegisterEvent()), Pair.of(service2, new RegisterEvent()), Pair.of(serviceShared23, new RegisterEvent()), Pair.of(serviceShared2P3, new RegisterEvent()), Pair.of(serviceP3, new RegisterEvent()), Pair.of(serviceShared3P3, new RegisterEvent()), Pair.of(service3, new RegisterEvent()), Pair.of(service0, new ActiveEvent()), Pair.of(serviceShared0123P1P2P3, new ActiveEvent()), Pair.of(serviceP1, new ActiveEvent()), Pair.of(serviceShared1P1, new ActiveEvent()), Pair.of(serviceShared2P1, new ActiveEvent()), Pair.of(serviceShared3P1, new ActiveEvent()), Pair.of(serviceP3, new ActiveEvent()), Pair.of(serviceShared1P3, new ActiveEvent()), Pair.of(serviceShared2P3, new ActiveEvent()), Pair.of(serviceShared3P3, new ActiveEvent()), Pair.of(service3, new ActiveEvent()), Pair.of(serviceShared13, new ActiveEvent()), Pair.of(serviceShared23, new ActiveEvent()), Pair.of(serviceShared123, new ActiveEvent()), Pair.of(serviceShared3P2, new ActiveEvent()), Pair.of(serviceP2, new ActiveEvent()), Pair.of(serviceShared1P2, new ActiveEvent()), Pair.of(serviceShared2P2, new ActiveEvent()), Pair.of(service2, new ActiveEvent()), Pair.of(serviceShared12, new ActiveEvent()), Pair.of(serviceShared13, new InactiveEvent()), Pair.of(service3, new InactiveEvent()), Pair.of(serviceShared3P3, new InactiveEvent()), Pair.of(serviceShared1P3, new InactiveEvent()), Pair.of(serviceP3, new InactiveEvent()), Pair.of(service3, new UnregisterEvent()), Pair.of(serviceShared3P3, new UnregisterEvent()), Pair.of(serviceP3, new UnregisterEvent()), Pair.of(service1, new ActiveEvent()), Pair.of(serviceShared13, new ActiveEvent()), Pair.of(serviceShared1P3, new ActiveEvent()), Pair.of(serviceShared2P3, new InactiveEvent()), Pair.of(serviceShared23, new InactiveEvent()), Pair.of(service2, new InactiveEvent()), Pair.of(serviceShared3P2, new InactiveEvent()), Pair.of(serviceShared2P2, new InactiveEvent()), Pair.of(serviceP2, new InactiveEvent()), Pair.of(serviceShared3P1, new InactiveEvent()), Pair.of(serviceShared2P1, new InactiveEvent()), Pair.of(serviceP1, new InactiveEvent()), Pair.of(serviceShared2P3, new UnregisterEvent()), Pair.of(serviceShared23, new UnregisterEvent()), Pair.of(service2, new UnregisterEvent()), Pair.of(serviceShared3P2, new UnregisterEvent()), Pair.of(serviceShared2P2, new UnregisterEvent()), Pair.of(serviceP2, new UnregisterEvent()), Pair.of(serviceShared3P1, new UnregisterEvent()), Pair.of(serviceShared2P1, new UnregisterEvent()), Pair.of(serviceP1, new UnregisterEvent()), Pair.of(serviceShared1P3, new InactiveEvent()), Pair.of(serviceShared1P2, new InactiveEvent()), Pair.of(serviceShared1P1, new InactiveEvent()), Pair.of(serviceShared123, new InactiveEvent()), Pair.of(serviceShared13, new InactiveEvent()), Pair.of(serviceShared12, new InactiveEvent()), Pair.of(service1, new InactiveEvent()), Pair.of(serviceShared0123P1P2P3, new InactiveEvent()), Pair.of(service0, new InactiveEvent()), Pair.of(serviceShared1P3, new UnregisterEvent()), Pair.of(serviceShared1P2, new UnregisterEvent()), Pair.of(serviceShared1P1, new UnregisterEvent()), Pair.of(serviceShared123, new UnregisterEvent()), Pair.of(serviceShared13, new UnregisterEvent()), Pair.of(serviceShared12, new UnregisterEvent()), Pair.of(service1, new UnregisterEvent()), Pair.of(serviceShared0123P1P2P3, new UnregisterEvent()), Pair.of(service0, new UnregisterEvent()), // restoration to 'beep'
Pair.of(service0, new RegisterEvent()), Pair.of(serviceShared0123P1P2P3, new RegisterEvent()), Pair.of(service1, new RegisterEvent()), Pair.of(serviceShared12, new RegisterEvent()), Pair.of(serviceShared13, new RegisterEvent()), Pair.of(serviceShared123, new RegisterEvent()), Pair.of(serviceShared1P1, new RegisterEvent()), Pair.of(serviceShared1P2, new RegisterEvent()), Pair.of(serviceShared1P3, new RegisterEvent()), Pair.of(service0, new ActiveEvent()), Pair.of(serviceShared0123P1P2P3, new ActiveEvent()), Pair.of(service1, new ActiveEvent()), Pair.of(serviceShared12, new ActiveEvent()), Pair.of(serviceShared13, new ActiveEvent()), Pair.of(serviceShared123, new ActiveEvent()), Pair.of(serviceShared1P1, new ActiveEvent()), Pair.of(serviceShared1P2, new ActiveEvent()), Pair.of(serviceShared1P3, new ActiveEvent()));
}
Aggregations