use of com.zhuinden.simplestack.helpers.ServiceProvider 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.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method lookupServiceNoOverlapsWorks.
@Test
public void lookupServiceNoOverlapsWorks() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final Service service = new Service();
TestKeyWithScope beep = new TestKeyWithScope("beep") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService(SERVICE_TAG, service);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
TestKeyWithScope boop = new TestKeyWithScope("boop") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
}
@Nonnull
@Override
public String getScopeTag() {
return "boop";
}
};
backstack.setup(History.of(beep, boop));
assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
backstack.setStateChanger(stateChanger);
assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
assertThat(backstack.hasService("boop", SERVICE_TAG)).isFalse();
assertThat(backstack.<Object>lookupService(SERVICE_TAG)).isSameAs(service);
backstack.goBack();
assertThat(backstack.hasService("beep", SERVICE_TAG)).isTrue();
assertThat(backstack.<Object>lookupService(SERVICE_TAG)).isSameAs(service);
backstack.setHistory(History.single(testKey1), StateChange.REPLACE);
assertThat(backstack.hasService("beep", SERVICE_TAG)).isFalse();
try {
backstack.lookupService(SERVICE_TAG);
Assert.fail();
} catch (IllegalStateException e) {
assertThat(e.getMessage()).contains("does not exist in any scope");
// OK!
}
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method scopeIsCreatedForScopeKeys.
@Test
public void scopeIsCreatedForScopeKeys() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
backstack.setup(History.single(testKey2));
assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isFalse();
backstack.setStateChanger(stateChanger);
assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isTrue();
Service service = backstack.getService(testKey2, SERVICE_TAG);
assertThat(service).isSameAs(services.get(SERVICE_TAG));
}
use of com.zhuinden.simplestack.helpers.ServiceProvider 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.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method serviceProviderCanBeSetWorksCorrectly.
@Test
public void serviceProviderCanBeSetWorksCorrectly() {
Backstack backstack = new Backstack();
assertThat(backstack.canSetScopeProviders()).isTrue();
backstack.setup(History.of(testKey1));
assertThat(backstack.canSetScopeProviders()).isTrue();
backstack.setScopedServices(new ServiceProvider());
backstack.setGlobalServices(GlobalServices.builder().build());
assertThat(backstack.canSetScopeProviders()).isTrue();
backstack.setStateChanger(new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
});
assertThat(backstack.canSetScopeProviders()).isFalse();
try {
backstack.setScopedServices(new ServiceProvider());
Assert.fail();
} catch (IllegalStateException e) {
// OK!
}
}
Aggregations