use of com.zhuinden.simplestack.helpers.TestKeyWithScope 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.TestKeyWithScope in project simple-stack by Zhuinden.
the class BackstackTest method exitScopeExitsImplicitScopeCorrectly.
@Test
public void exitScopeExitsImplicitScopeCorrectly() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
Object firstKey = new TestKey("firstKey");
Object lastKey = new TestKey("lastKey");
Object key = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
backstack.setup(History.of(firstKey, key, lastKey));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
backstack.exitScope("blah");
assertThat(backstack.getHistory()).containsExactly(firstKey);
}
use of com.zhuinden.simplestack.helpers.TestKeyWithScope in project simple-stack by Zhuinden.
the class BackstackTest method exitScopeToExitsExplicitScopesCorrectly.
@Test
public void exitScopeToExitsExplicitScopesCorrectly() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
Object firstKey = new TestKey("firstKey");
Object secondKey = new TestKey("secondKey");
Object lastKey = new TestKey("lastKey");
Object key1 = new TestKeyWithExplicitParent("key1") {
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("blah", "parentKey1");
}
@Override
protected void bindParentServices(ServiceBinder serviceBinder) {
}
@Override
protected void bindOwnServices(ServiceBinder serviceBinder) {
}
};
Object key2 = new TestKeyWithExplicitParent("key2") {
@Nonnull
@Override
public List<String> getParentScopes() {
return History.of("blah", "parentKey2");
}
@Override
protected void bindParentServices(ServiceBinder serviceBinder) {
}
@Override
protected void bindOwnServices(ServiceBinder serviceBinder) {
}
};
backstack.setup(History.of(firstKey, secondKey, key1, key2, lastKey));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
Object targetKey = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
assertThat(backstack.getHistory()).containsExactly(firstKey, secondKey, targetKey);
}
use of com.zhuinden.simplestack.helpers.TestKeyWithScope in project simple-stack by Zhuinden.
the class BackstackTest method exitScopeToThrowsWhenScopeIsNotFound.
@Test
public void exitScopeToThrowsWhenScopeIsNotFound() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
Object key = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
backstack.setup(History.of(key));
Object targetKey = new TestKey("target");
try {
backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
Assert.fail();
} catch (IllegalStateException e) {
// OK!
}
}
use of com.zhuinden.simplestack.helpers.TestKeyWithScope in project simple-stack by Zhuinden.
the class BackstackTest method exitScopeToDefaultsToJumpToRootIfRootHasScope.
@Test
public void exitScopeToDefaultsToJumpToRootIfRootHasScope() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
Object key = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
backstack.setup(History.of(key));
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
backstack.exitScopeTo("blah", key, StateChange.BACKWARD);
assertThat(backstack.getHistory()).containsExactly(key);
}
Aggregations