use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class ScopingTest method deactivatedIsCalledInReverseOrder.
@Test
public void deactivatedIsCalledInReverseOrder() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final List<Object> deactivatedServices = new ArrayList<>();
class MyService implements ScopedServices.Activated {
@Override
public void onServiceActive() {
}
@Override
public void onServiceInactive() {
deactivatedServices.add(this);
}
}
final MyService service1 = new MyService();
final MyService service2 = new MyService();
final MyService service3 = new MyService();
final MyService service4 = new MyService();
final MyService service5 = new MyService();
final MyService service6 = new MyService();
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);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
TestKeyWithScope boop = new TestKeyWithScope("beep") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE4", service4);
serviceBinder.addService("SERVICE5", service5);
serviceBinder.addService("SERVICE6", service6);
}
@Nonnull
@Override
public String getScopeTag() {
return "boop";
}
};
backstack.setup(History.of(beep));
assertThat(deactivatedServices).isEmpty();
backstack.setStateChanger(stateChanger);
backstack.goTo(boop);
assertThat(deactivatedServices).containsExactly(service3, service2, service1);
TestKey bye = new TestKey("bye");
backstack.setHistory(History.of(bye), StateChange.REPLACE);
assertThat(deactivatedServices).containsExactly(service3, service2, service1, service6, service5, service4);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class ScopingTest method scopeCreationAndDestructionHappensInForwardAndReverseOrder.
@Test
public void scopeCreationAndDestructionHappensInForwardAndReverseOrder() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final List<Object> serviceRegistered = new ArrayList<>();
final List<Object> serviceUnregistered = new ArrayList<>();
class MyService implements ScopedServices.Registered {
@Override
public void onServiceRegistered() {
serviceRegistered.add(this);
}
@Override
public void onServiceUnregistered() {
serviceUnregistered.add(this);
}
}
final MyService service1 = new MyService();
final MyService service2 = new MyService();
TestKeyWithScope beep = new TestKeyWithScope("beep") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE1", service1);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
TestKeyWithScope boop = new TestKeyWithScope("boop") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE2", service2);
}
@Nonnull
@Override
public String getScopeTag() {
return "boop";
}
};
TestKey bye = new TestKey("bye");
backstack.setup(History.of(beep, boop));
assertThat(serviceRegistered).isEmpty();
assertThat(serviceUnregistered).isEmpty();
backstack.setStateChanger(stateChanger);
assertThat(serviceRegistered).containsExactly(service1, service2);
assertThat(serviceUnregistered).isEmpty();
backstack.setHistory(History.of(bye), StateChange.REPLACE);
assertThat(serviceRegistered).containsExactly(service1, service2);
assertThat(serviceUnregistered).containsExactly(service2, service1);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class ScopingTest method navigationIsPossibleAndEnqueuedDuringActivationDispatch.
@Test
public void navigationIsPossibleAndEnqueuedDuringActivationDispatch() {
final TestKey destination = new TestKey("destination");
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
class MyService implements ScopedServices.Activated {
private final Backstack backstack;
public MyService(Backstack backstack) {
this.backstack = backstack;
}
@Override
public void onServiceActive() {
backstack.setHistory(History.of(destination), StateChange.REPLACE);
}
@Override
public void onServiceInactive() {
}
}
final MyService service = new MyService(backstack);
TestKeyWithOnlyParentServices beep = new TestKeyWithOnlyParentServices("beep", History.of("registration")) {
@Override
public void bindServices(ServiceBinder serviceBinder) {
if (serviceBinder.getScopeTag().equals("registration")) {
serviceBinder.addService("SERVICE", service);
}
}
};
TestKeyWithScope boop = new TestKeyWithScope("boop") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
};
backstack.setup(History.of(boop));
backstack.setStateChanger(stateChanger);
backstack.setHistory(History.of(beep), StateChange.REPLACE);
assertThat(backstack.getHistory()).containsExactly(destination);
}
use of com.zhuinden.simplestack.helpers.TestKey in project simple-stack by Zhuinden.
the class ScopingTest method activatedWorks.
@Test
public void activatedWorks() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final List<Object> activatedServices = new ArrayList<>();
final List<Object> deactivatedServices = new ArrayList<>();
class MyService implements ScopedServices.Activated {
@Override
public void onServiceActive() {
activatedServices.add(this);
}
@Override
public void onServiceInactive() {
deactivatedServices.add(this);
}
}
final MyService service1 = new MyService();
final MyService service2 = new MyService();
TestKeyWithScope beep = new TestKeyWithScope("beep") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE1", service1);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
TestKeyWithScope boop = new TestKeyWithScope("boop") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE2", service2);
}
@Nonnull
@Override
public String getScopeTag() {
return "boop";
}
};
TestKey bye = new TestKey("bye");
backstack.setup(History.of(beep, boop));
assertThat(activatedServices).isEmpty();
assertThat(deactivatedServices).isEmpty();
backstack.setStateChanger(stateChanger);
assertThat(activatedServices).containsExactly(service2);
assertThat(deactivatedServices).isEmpty();
backstack.goBack();
assertThat(activatedServices).containsExactly(service2, service1);
assertThat(deactivatedServices).containsExactly(service2);
backstack.setHistory(History.of(bye), StateChange.REPLACE);
assertThat(activatedServices).containsExactly(service2, service1);
assertThat(deactivatedServices).containsExactly(service2, service1);
}
use of com.zhuinden.simplestack.helpers.TestKey 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);
}
Aggregations