use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method activationHappensEvenWithForceExecutedStateChangeAndInitializeStateChange.
@Test
public void activationHappensEvenWithForceExecutedStateChangeAndInitializeStateChange() {
Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final List<Object> activatedServices = new ArrayList<>();
final List<Object> deactivatedServices = new ArrayList<>();
final AtomicReference<StateChanger.Callback> callback = new AtomicReference<>();
StateChanger pendingStateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
callback.set(completionCallback);
}
};
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();
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(activatedServices).isEmpty();
backstack.setStateChanger(pendingStateChanger);
callback.get().stateChangeComplete();
assertThat(activatedServices).containsExactly(service1, service2, service3);
backstack.setHistory(History.of(boop), StateChange.BACKWARD);
backstack.removeStateChanger();
backstack.executePendingStateChange();
backstack.setStateChanger(pendingStateChanger);
callback.get().stateChangeComplete();
assertThat(activatedServices).containsExactly(service1, service2, service3, service4, service5, service6);
assertThat(deactivatedServices).containsExactly(service3, service2, service1);
}
use of com.zhuinden.simplestack.helpers.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method scopesAreFinalizedWhenActivityIsFinishing.
@Test
public void scopesAreFinalizedWhenActivityIsFinishing() {
Activity activity = Mockito.mock(Activity.class);
Mockito.when(activity.isFinishing()).thenReturn(true);
final Service service = new Service();
TestKeyWithScope testKeyWithScope = new TestKeyWithScope("blah") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService(SERVICE_TAG, service);
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
StateChanger stateChanger = new StateChanger() {
@Override
public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
completionCallback.stateChangeComplete();
}
};
BackstackDelegate backstackDelegate = new BackstackDelegate();
backstackDelegate.setScopedServices(activity, new ServiceProvider());
backstackDelegate.onCreate(null, null, History.of(testKeyWithScope));
backstackDelegate.setStateChanger(stateChanger);
backstackDelegate.onPostResume();
backstackDelegate.onPause();
assertThat(backstackDelegate.hasScope("beep")).isTrue();
assertThat(backstackDelegate.hasService(testKeyWithScope, SERVICE_TAG)).isTrue();
assertThat(service.didServiceRegister).isTrue();
assertThat(service.didServiceUnregister).isFalse();
backstackDelegate.onDestroy();
assertThat(backstackDelegate.hasScope("beep")).isFalse();
assertThat(backstackDelegate.hasService(testKeyWithScope, SERVICE_TAG)).isFalse();
assertThat(service.didServiceRegister).isTrue();
assertThat(service.didServiceUnregister).isTrue();
}
use of com.zhuinden.simplestack.helpers.ServiceProvider 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.ServiceProvider in project simple-stack by Zhuinden.
the class ScopingTest method scopedServicesCanRetrieveBackstackFromServiceBinder.
@Test
public void scopedServicesCanRetrieveBackstackFromServiceBinder() {
final Backstack backstack = new Backstack();
backstack.setScopedServices(new ServiceProvider());
final AtomicReference<Backstack> ref = new AtomicReference<>();
class MyService {
}
final MyService service1 = new MyService();
TestKeyWithScope beep = new TestKeyWithScope("beep") {
@Override
public void bindServices(ServiceBinder serviceBinder) {
assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
serviceBinder.addService("SERVICE1", service1);
ref.set(serviceBinder.getBackstack());
}
@Nonnull
@Override
public String getScopeTag() {
return "beep";
}
};
backstack.setup(History.of(beep));
backstack.setStateChanger(stateChanger);
assertThat(backstack).isSameAs(ref.get());
}
use of com.zhuinden.simplestack.helpers.ServiceProvider 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);
}
Aggregations