Search in sources :

Example 16 with TestKeyWithScope

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

the class BackstackTest method exitScopeToExitsImplicitScopeCorrectlyAndAppendsIfNotFound.

@Test
public void exitScopeToExitsImplicitScopeCorrectlyAndAppendsIfNotFound() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    Object firstKey = new TestKey("firstKey");
    Object secondKey = new TestKey("secondKey");
    Object thirdKey = new TestKey("thirdKey");
    Object lastKey = new TestKey("lastKey");
    Object key = new TestKeyWithScope("blah") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    Object targetKey = new TestKey("targetKey");
    backstack.setup(History.of(firstKey, secondKey, thirdKey, key, lastKey));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
    assertThat(backstack.getHistory()).containsExactly(firstKey, secondKey, thirdKey, targetKey);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 17 with TestKeyWithScope

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

the class BackstackTest method exitScopeThrowsWhenScopeIsNotFound.

@Test
public void exitScopeThrowsWhenScopeIsNotFound() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    Object key = new TestKeyWithScope("blah") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    backstack.setup(History.of(key));
    try {
        backstack.exitScope("blahhhh");
        Assert.fail();
    } catch (IllegalStateException e) {
    // OK!
    }
}
Also used : TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 18 with TestKeyWithScope

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

the class ScopingAliasTest method aliasesWorkInMultipleScopes.

@Test
public void aliasesWorkInMultipleScopes() {
    final Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    class MyService {

        private final String id;

        MyService(String id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "MyService{" + "id=" + id + '}';
        }
    }
    final Object service0 = new MyService("service0");
    final Object serviceShared0123P1P2P3 = new MyService("serviceShared0123P1P2P3");
    backstack.setGlobalServices(GlobalServices.builder().addService("service0", service0).addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3).addAlias("alias0", service0).addAlias("aliasShared0123P1P2P3", serviceShared0123P1P2P3).build());
    final Object service1 = new MyService("service1");
    final Object service2 = new MyService("service2");
    final Object service3 = new MyService("service3");
    final Object serviceShared12 = new MyService("serviceShared12");
    final Object serviceShared13 = new MyService("serviceShared13");
    final Object serviceShared23 = new MyService("serviceShared23");
    final Object serviceShared123 = new MyService("serviceShared123");
    final Object serviceShared1P1 = new MyService("serviceShared1P1");
    final Object serviceShared1P2 = new MyService("serviceShared1P2");
    final Object serviceShared1P3 = new MyService("serviceShared1P3");
    final Object serviceShared2P1 = new MyService("serviceShared2P1");
    final Object serviceShared2P2 = new MyService("serviceShared2P2");
    final Object serviceShared2P3 = new MyService("serviceShared2P3");
    final Object serviceShared3P1 = new MyService("serviceShared3P1");
    final Object serviceShared3P2 = new MyService("serviceShared3P2");
    final Object serviceShared3P3 = new MyService("serviceShared3P3");
    final Object serviceP1 = new MyService("serviceP1");
    final Object serviceP2 = new MyService("serviceP2");
    final Object serviceP3 = new MyService("serviceP3");
    TestKeyWithScope beep = new TestKeyWithScope("scope1") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addService("service1", service1);
            serviceBinder.addService("serviceShared12", serviceShared12);
            serviceBinder.addService("serviceShared13", serviceShared13);
            serviceBinder.addService("serviceShared123", serviceShared123);
            serviceBinder.addService("serviceShared1P1", serviceShared1P1);
            serviceBinder.addService("serviceShared1P2", serviceShared1P2);
            serviceBinder.addService("serviceShared1P3", serviceShared1P3);
            serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
        }
    };
    TestKeyWithExplicitParent boop = new TestKeyWithExplicitParent("scope2") {

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.of("parent1", "parent2");
        }

        @Override
        protected void bindParentServices(ServiceBinder serviceBinder) {
            if ("parent1".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("serviceP1", serviceP1);
                serviceBinder.addService("serviceShared1P1", serviceShared1P1);
                serviceBinder.addService("serviceShared2P1", serviceShared2P1);
                serviceBinder.addService("serviceShared3P1", serviceShared3P1);
                serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
                serviceBinder.addAlias("aliasP1", serviceP1);
                serviceBinder.addAlias("aliasShared1P1", serviceShared1P1);
                serviceBinder.addAlias("aliasShared2P1", serviceShared2P1);
                serviceBinder.addAlias("aliasShared3P1", serviceShared3P1);
                serviceBinder.addAlias("aliasShared0123P1P2P3", serviceShared0123P1P2P3);
            }
            if ("parent2".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("serviceP2", serviceP2);
                serviceBinder.addService("serviceShared1P2", serviceShared1P2);
                serviceBinder.addService("serviceShared2P2", serviceShared2P2);
                serviceBinder.addService("serviceShared3P2", serviceShared3P2);
                serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
                serviceBinder.addService("serviceP2", serviceP2);
                serviceBinder.addService("serviceShared1P2", serviceShared1P2);
                serviceBinder.addService("serviceShared2P2", serviceShared2P2);
                serviceBinder.addService("serviceShared3P2", serviceShared3P2);
                serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
            }
        }

        @Override
        protected void bindOwnServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service2", service2);
            serviceBinder.addService("serviceShared12", serviceShared12);
            serviceBinder.addService("serviceShared23", serviceShared23);
            serviceBinder.addService("serviceShared123", serviceShared123);
            serviceBinder.addService("serviceShared2P1", serviceShared2P1);
            serviceBinder.addService("serviceShared2P2", serviceShared2P2);
            serviceBinder.addService("serviceShared2P3", serviceShared2P3);
            serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
        }
    };
    TestKeyWithExplicitParent braap = new TestKeyWithExplicitParent("scope3") {

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.of("parent1", "parent3");
        }

        @Override
        protected void bindParentServices(ServiceBinder serviceBinder) {
            if ("parent1".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("serviceP1", serviceP1);
                serviceBinder.addService("serviceShared1P1", serviceShared1P1);
                serviceBinder.addService("serviceShared2P1", serviceShared2P1);
                serviceBinder.addService("serviceShared3P1", serviceShared3P1);
                serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
            }
            if ("parent3".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("serviceP3", serviceP3);
                serviceBinder.addService("serviceShared1P3", serviceShared1P3);
                serviceBinder.addService("serviceShared2P3", serviceShared2P3);
                serviceBinder.addService("serviceShared3P3", serviceShared3P3);
                serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
            }
        }

        @Override
        protected void bindOwnServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service3", service3);
            serviceBinder.addService("serviceShared13", serviceShared13);
            serviceBinder.addService("serviceShared23", serviceShared23);
            serviceBinder.addService("serviceShared123", serviceShared123);
            serviceBinder.addService("serviceShared3P1", serviceShared3P1);
            serviceBinder.addService("serviceShared3P2", serviceShared3P2);
            serviceBinder.addService("serviceShared3P3", serviceShared3P3);
            serviceBinder.addService("serviceShared0123P1P2P3", serviceShared0123P1P2P3);
        }
    };
    /*                      GLOBAL
         *                                PARENT1
         *                        PARENT2        PARENT3
         *   BEEP               BOOP                 BRAAP
         */
    backstack.setup(History.of(beep, boop, braap));
    StateChanger stateChanger = new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    };
    backstack.setStateChanger(stateChanger);
    // then
    assertThat(backstack.hasService("parent1", "serviceP1")).isTrue();
    assertThat(backstack.hasService("parent1", "aliasP1")).isTrue();
    assertThat(backstack.canFindFromScope("parent1", "aliasP1")).isTrue();
    assertThat(backstack.canFindFromScope("parent1", "aliasP1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindService("aliasP1")).isTrue();
    assertThat(backstack.lookupFromScope("parent1", "aliasP1")).isSameAs(serviceP1);
    assertThat(backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.ALL)).isSameAs(serviceP1);
    assertThat(backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT)).isSameAs(serviceP1);
    assertThat(backstack.lookupService("aliasP1")).isSameAs(serviceP1);
    // then
    assertThat(backstack.hasService(GlobalServices.SCOPE_TAG, "service0")).isTrue();
    assertThat(backstack.hasService(GlobalServices.SCOPE_TAG, "alias0")).isTrue();
    assertThat(backstack.canFindFromScope(GlobalServices.SCOPE_TAG, "alias0")).isTrue();
    assertThat(backstack.canFindFromScope(GlobalServices.SCOPE_TAG, "alias0", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope(GlobalServices.SCOPE_TAG, "alias0", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindService("alias0")).isTrue();
    assertThat(backstack.lookupFromScope(GlobalServices.SCOPE_TAG, "alias0")).isSameAs(service0);
    assertThat(backstack.lookupFromScope(GlobalServices.SCOPE_TAG, "alias0", ScopeLookupMode.ALL)).isSameAs(service0);
    assertThat(backstack.lookupFromScope(GlobalServices.SCOPE_TAG, "alias0", ScopeLookupMode.EXPLICIT)).isSameAs(service0);
    assertThat(backstack.lookupService("alias0")).isSameAs(service0);
    // ALSO
    backstack.goBack();
    backstack.goBack();
    // then
    assertThat(backstack.hasService("parent1", "serviceP1")).isFalse();
    assertThat(backstack.hasService("parent1", "aliasP1")).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "aliasP1")).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "aliasP1", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindService("aliasP1")).isFalse();
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "aliasP1");
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.ALL);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.ALL);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "aliasP1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupService("aliasP1");
        }
    });
}
Also used : Action(com.zhuinden.simplestack.helpers.Action) Nonnull(javax.annotation.Nonnull) TestKeyWithExplicitParent(com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent) TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 19 with TestKeyWithScope

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

the class ScopingBackEventDispatchTest method onBackDispatchHandlesBackDispatchesToCorrectActiveChainAfterMoveToTop.

@Test
public void onBackDispatchHandlesBackDispatchesToCorrectActiveChainAfterMoveToTop() {
    final HandlesBackOnce previousService = new HandlesBackOnce();
    final HandlesBackOnce nextService = new HandlesBackOnce();
    Object previousKey = new TestKeyWithScope("previous") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service", previousService);
        }
    };
    Object nextKey = new TestKeyWithScope("next") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service", nextService);
        }
    };
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(previousKey, nextKey));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.lookupService("service")).isSameAs(nextService);
    backstack.moveToTop(previousKey);
    assertThat(backstack.getHistory()).containsExactly(nextKey, previousKey);
    assertThat(backstack.lookupService("service")).isSameAs(previousService);
    boolean handled;
    assertThat(previousService.handledBackOnce).isFalse();
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(previousService.handledBackOnce).isTrue();
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(backstack.getHistory()).containsExactly(nextKey);
    assertThat(nextService.handledBackOnce).isFalse();
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(nextService.handledBackOnce).isTrue();
    handled = backstack.goBack();
    assertThat(handled).isFalse();
}
Also used : TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 20 with TestKeyWithScope

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

the class ScopingBackEventDispatchTest method onBackDispatchHandlesBackHandlesInActiveParentScope.

@Test
public void onBackDispatchHandlesBackHandlesInActiveParentScope() {
    final HandlesBackOnce previousService = new HandlesBackOnce();
    Object previousKey = new TestKeyWithScope("previousKey") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service1", previousService);
        }
    };
    final HandlesBackOnce parentService1 = new HandlesBackOnce();
    final HandlesBackOnce parentService2 = new HandlesBackOnce();
    final HandlesBackOnce service1 = new HandlesBackOnce();
    final HandlesBackOnce service2 = new HandlesBackOnce();
    Object key = new TestKeyWithExplicitParent("key") {

        @Override
        protected void bindParentServices(ServiceBinder serviceBinder) {
            if ("parentScope".equals(serviceBinder.getScopeTag())) {
                serviceBinder.addService("parentService1", parentService1);
                serviceBinder.addService("parentService2", parentService2);
            }
        }

        @Override
        protected void bindOwnServices(ServiceBinder serviceBinder) {
            serviceBinder.addService("service1", service1);
            serviceBinder.addService("service2", service2);
        }

        @Nonnull
        @Override
        public List<String> getParentScopes() {
            return History.of("parentScope");
        }
    };
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(previousKey, key));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.lookupService("parentService1")).isSameAs(parentService1);
    assertThat(backstack.lookupService("parentService2")).isSameAs(parentService2);
    assertThat(backstack.lookupService("service1")).isSameAs(service1);
    assertThat(backstack.lookupService("service2")).isSameAs(service2);
    boolean handled;
    assertThat(parentService1.handledBackOnce).isFalse();
    assertThat(parentService2.handledBackOnce).isFalse();
    assertThat(service1.handledBackOnce).isFalse();
    assertThat(service2.handledBackOnce).isFalse();
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(parentService1.handledBackOnce).isFalse();
    assertThat(parentService2.handledBackOnce).isFalse();
    assertThat(service1.handledBackOnce).isFalse();
    assertThat(service2.handledBackOnce).isTrue();
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(parentService1.handledBackOnce).isFalse();
    assertThat(parentService2.handledBackOnce).isFalse();
    assertThat(service1.handledBackOnce).isTrue();
    assertThat(service2.handledBackOnce).isTrue();
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(parentService1.handledBackOnce).isFalse();
    assertThat(parentService2.handledBackOnce).isTrue();
    assertThat(service1.handledBackOnce).isTrue();
    assertThat(service2.handledBackOnce).isTrue();
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(parentService1.handledBackOnce).isTrue();
    assertThat(parentService2.handledBackOnce).isTrue();
    assertThat(service1.handledBackOnce).isTrue();
    assertThat(service2.handledBackOnce).isTrue();
    assertThat(previousService.handledBackOnce).isFalse();
    assertThat(backstack.getHistory()).containsExactly(previousKey, key);
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(backstack.getHistory()).containsExactly(previousKey);
    handled = backstack.goBack();
    assertThat(handled).isTrue();
    assertThat(previousService.handledBackOnce).isTrue();
    handled = backstack.goBack();
    assertThat(handled).isFalse();
}
Also used : TestKeyWithExplicitParent(com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent) TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Aggregations

ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)25 TestKeyWithScope (com.zhuinden.simplestack.helpers.TestKeyWithScope)25 Test (org.junit.Test)25 TestKey (com.zhuinden.simplestack.helpers.TestKey)7 Nonnull (javax.annotation.Nonnull)4 TestKeyWithExplicitParent (com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent)3 ArrayList (java.util.ArrayList)3 Parcel (android.os.Parcel)2 HasParentServices (com.zhuinden.simplestack.helpers.HasParentServices)2 Action (com.zhuinden.simplestack.helpers.Action)1 StateBundle (com.zhuinden.statebundle.StateBundle)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nullable (javax.annotation.Nullable)1