Search in sources :

Example 6 with TestKeyWithOnlyParentServices

use of com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices in project simple-stack by Zhuinden.

the class ScopingBackEventDispatchTest method onBackDispatchHandlesBackHandlesInActiveParentScopeEvenIfTopIsNotScopeKeyAndExplicitParentIsSharedFromPreviousKey.

@Test
public void onBackDispatchHandlesBackHandlesInActiveParentScopeEvenIfTopIsNotScopeKeyAndExplicitParentIsSharedFromPreviousKey() {
    final HandlesBackOnce parentService = new HandlesBackOnce();
    Object key1 = new TestKeyWithOnlyParentServices("key1", History.of(HandlesBackOnce.class.getName())) {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            if (HandlesBackOnce.class.getName().equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService(HandlesBackOnce.class.getName(), parentService);
            }
        }
    };
    Object key2 = new TestKeyWithOnlyParentServices("key2", History.of(HandlesBackOnce.class.getName())) {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            if (HandlesBackOnce.class.getName().equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService(HandlesBackOnce.class.getName(), parentService);
            }
        }
    };
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(key1, key2));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.lookupService(HandlesBackOnce.class.getName())).isSameAs(parentService);
    assertThat(backstack.getHistory()).containsExactly(key1, key2);
    assertThat(parentService.handledBackOnce).isFalse();
    boolean handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(parentService.handledBackOnce).isTrue();
    assertThat(backstack.getHistory()).containsExactly(key1, key2);
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(backstack.getHistory()).containsExactly(key1);
    handled = backstack.goBack();
    assertThat(handled).isFalse();
}
Also used : TestKeyWithOnlyParentServices(com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Aggregations

ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)6 TestKeyWithOnlyParentServices (com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices)6 Test (org.junit.Test)6 TestKey (com.zhuinden.simplestack.helpers.TestKey)4