use of com.zhuinden.simplestack.helpers.ServiceProvider 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);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingGlobalScopeTest method serviceLifecycleCallbacksWork.
@Test
public void serviceLifecycleCallbacksWork() {
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 Object service0 = new MyService(0);
backstack.setGlobalServices(GlobalServices.builder().addService("SERVICE0", service0).build());
final Object service1 = new MyService(1);
final Object service2 = new MyService(2);
final Object service3 = new MyService(3);
final Object service4 = new MyService(4);
final Object service5 = new MyService(5);
final Object service6 = new MyService(6);
final Object service7 = new MyService(7);
final Object service8 = new MyService(8);
final Object service9 = new MyService(9);
TestKeyWithScope beep = new TestKeyWithScope("beep") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE1", service1);
serviceBinder.addService("SERVICE2", service2);
serviceBinder.addService("SERVICE3", service3);
}
};
TestKeyWithScope boop = new TestKeyWithScope("boop") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE4", service4);
serviceBinder.addService("SERVICE5", service5);
serviceBinder.addService("SERVICE6", service6);
}
};
TestKeyWithScope braap = new TestKeyWithScope("braap") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE7", service7);
serviceBinder.addService("SERVICE8", service8);
serviceBinder.addService("SERVICE9", service9);
}
};
backstack.setup(History.of(beep, boop));
StateChanger stateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
};
backstack.setStateChanger(stateChanger);
backstack.goTo(braap);
// just to make sure
backstack.removeStateChanger();
// just to make sure
backstack.setStateChanger(stateChanger);
backstack.goBack();
backstack.finalizeScopes();
assertThat(events).containsExactly(Pair.of(service0, ServiceEvent.CREATE), Pair.of(service1, ServiceEvent.CREATE), Pair.of(service2, ServiceEvent.CREATE), Pair.of(service3, ServiceEvent.CREATE), Pair.of(service4, ServiceEvent.CREATE), Pair.of(service5, ServiceEvent.CREATE), Pair.of(service6, ServiceEvent.CREATE), Pair.of(service0, ServiceEvent.ACTIVE), Pair.of(service4, ServiceEvent.ACTIVE), Pair.of(service5, ServiceEvent.ACTIVE), Pair.of(service6, ServiceEvent.ACTIVE), Pair.of(service7, ServiceEvent.CREATE), Pair.of(service8, ServiceEvent.CREATE), Pair.of(service9, ServiceEvent.CREATE), Pair.of(service7, ServiceEvent.ACTIVE), Pair.of(service8, ServiceEvent.ACTIVE), Pair.of(service9, ServiceEvent.ACTIVE), Pair.of(service6, ServiceEvent.INACTIVE), Pair.of(service5, ServiceEvent.INACTIVE), Pair.of(service4, ServiceEvent.INACTIVE), Pair.of(service4, ServiceEvent.ACTIVE), Pair.of(service5, ServiceEvent.ACTIVE), Pair.of(service6, ServiceEvent.ACTIVE), Pair.of(service9, ServiceEvent.INACTIVE), Pair.of(service8, ServiceEvent.INACTIVE), Pair.of(service7, ServiceEvent.INACTIVE), Pair.of(service9, ServiceEvent.DESTROY), Pair.of(service8, ServiceEvent.DESTROY), Pair.of(service7, ServiceEvent.DESTROY), Pair.of(service6, ServiceEvent.INACTIVE), Pair.of(service5, ServiceEvent.INACTIVE), Pair.of(service4, ServiceEvent.INACTIVE), Pair.of(service0, ServiceEvent.INACTIVE), Pair.of(service6, ServiceEvent.DESTROY), Pair.of(service5, ServiceEvent.DESTROY), Pair.of(service4, ServiceEvent.DESTROY), Pair.of(service3, ServiceEvent.DESTROY), Pair.of(service2, ServiceEvent.DESTROY), Pair.of(service1, ServiceEvent.DESTROY), Pair.of(service0, ServiceEvent.DESTROY));
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingGlobalScopeTest method globalScopeLookupPrefersImplicitsToGlobal.
@Test
public void globalScopeLookupPrefersImplicitsToGlobal() {
final Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final Object globalService = new Object();
backstack.setGlobalServices(GlobalServices.builder().addService("parentService1", 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", "parentService1", ScopeLookupMode.EXPLICIT)).isTrue();
assertThat(backstack.canFindFromScope("beep", "parentService1", ScopeLookupMode.EXPLICIT)).isTrue();
assertThat(backstack.lookupFromScope("boop", "parentService1", ScopeLookupMode.EXPLICIT)).isSameAs(globalService);
assertThat(backstack.lookupFromScope("beep", "parentService1", ScopeLookupMode.EXPLICIT)).isSameAs(parentService1);
assertThat(backstack.canFindFromScope("boop", "parentService1", ScopeLookupMode.ALL)).isTrue();
assertThat(backstack.canFindFromScope("beep", "parentService1", ScopeLookupMode.ALL)).isTrue();
assertThat(backstack.lookupFromScope("boop", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
assertThat(backstack.lookupFromScope("beep", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
assertThat(backstack.lookupService("parentService1")).isSameAs(parentService1);
backstack.setHistory(History.of(new Key2("boop")), StateChange.REPLACE);
assertThat(backstack.lookupService("parentService1")).isSameAs(globalService);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopeLookupModeTest method findScopesForKeyWorks.
@Test
public void findScopesForKeyWorks() {
final Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
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", "parent3");
}
}
Object key1 = new Key1("beep");
Object key2 = new Key2("boop");
backstack.setup(History.of(key1, key2));
assertThat(backstack.findScopesForKey(key1, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(key2, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(key1, ScopeLookupMode.ALL)).isEmpty();
assertThat(backstack.findScopesForKey(key2, ScopeLookupMode.ALL)).isEmpty();
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.findScopesForKey(key1, ScopeLookupMode.EXPLICIT)).containsExactly("beep", "parent1");
assertThat(backstack.findScopesForKey(key2, ScopeLookupMode.EXPLICIT)).containsExactly("boop", "parent3", "parent2");
assertThat(backstack.findScopesForKey(key1, ScopeLookupMode.ALL)).containsExactly("beep", "parent1");
assertThat(backstack.findScopesForKey(key2, ScopeLookupMode.ALL)).containsExactly("boop", "parent3", "parent2", "beep", "parent1");
backstack.moveToTop(key1);
assertThat(backstack.findScopesForKey(key1, ScopeLookupMode.EXPLICIT)).containsExactly("beep", "parent1");
assertThat(backstack.findScopesForKey(key2, ScopeLookupMode.EXPLICIT)).containsExactly("boop", "parent3", "parent2");
assertThat(backstack.findScopesForKey(key1, ScopeLookupMode.ALL)).containsExactly("beep", "parent1", "boop", "parent3", "parent2");
assertThat(backstack.findScopesForKey(key2, ScopeLookupMode.ALL)).containsExactly("boop", "parent3", "parent2");
backstack.moveToTop(key2);
assertThat(backstack.findScopesForKey(key1, ScopeLookupMode.EXPLICIT)).containsExactly("beep", "parent1");
assertThat(backstack.findScopesForKey(key2, ScopeLookupMode.EXPLICIT)).containsExactly("boop", "parent3", "parent2");
assertThat(backstack.findScopesForKey(key1, ScopeLookupMode.ALL)).containsExactly("beep", "parent1");
assertThat(backstack.findScopesForKey(key2, ScopeLookupMode.ALL)).containsExactly("boop", "parent3", "parent2", "beep", "parent1");
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopeLookupModeTest method findScopesForKeyOtherSetup.
@Test
public void findScopesForKeyOtherSetup() {
abstract class TestKeyWithScope extends TestKey implements HasServices {
TestKeyWithScope(String name) {
super(name);
}
protected TestKeyWithScope(Parcel in) {
super(in);
}
@Nonnull
@Override
public String getScopeTag() {
return name;
}
}
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");
backstack.setGlobalServices(GlobalServices.builder().addService("service0", service0).build());
TestKeyWithScope beep = new TestKeyWithScope("scope1") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
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) {
}
@Override
void bindOwnServices(ServiceBinder serviceBinder) {
}
};
TestKeyWithExplicitParent braap = new TestKeyWithExplicitParent("scope3") {
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("parent1", "parent3");
}
@Override
void bindParentServices(ServiceBinder serviceBinder) {
}
@Override
void bindOwnServices(ServiceBinder serviceBinder) {
}
};
/* 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);
assertThat(backstack.findScopesForKey(beep, ScopeLookupMode.ALL)).containsExactly("scope1", GlobalServices.SCOPE_TAG);
assertThat(backstack.findScopesForKey(beep, ScopeLookupMode.EXPLICIT)).containsExactly("scope1", GlobalServices.SCOPE_TAG);
assertThat(backstack.findScopesForKey(boop, ScopeLookupMode.ALL)).containsExactly("scope2", "parent2", "parent1", "scope1", GlobalServices.SCOPE_TAG);
assertThat(backstack.findScopesForKey(boop, ScopeLookupMode.EXPLICIT)).containsExactly("scope2", "parent2", "parent1", GlobalServices.SCOPE_TAG);
assertThat(backstack.findScopesForKey(braap, ScopeLookupMode.ALL)).containsExactly("scope3", "parent3", "parent1", "scope2", "parent2", "scope1", GlobalServices.SCOPE_TAG);
assertThat(backstack.findScopesForKey(braap, ScopeLookupMode.EXPLICIT)).containsExactly("scope3", "parent3", "parent1", GlobalServices.SCOPE_TAG);
backstack.finalizeScopes();
assertThat(backstack.findScopesForKey(beep, ScopeLookupMode.ALL)).isEmpty();
assertThat(backstack.findScopesForKey(beep, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(boop, ScopeLookupMode.ALL)).isEmpty();
assertThat(backstack.findScopesForKey(boop, ScopeLookupMode.EXPLICIT)).isEmpty();
assertThat(backstack.findScopesForKey(braap, ScopeLookupMode.ALL)).isEmpty();
assertThat(backstack.findScopesForKey(braap, ScopeLookupMode.EXPLICIT)).isEmpty();
}
Aggregations