Search in sources :

Example 31 with ServiceProvider

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

the class ScopeLookupModeTest method lookupModesWork.

@Test
public void lookupModesWork() {
    final Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    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")));
    assertThat(backstack.canFindFromScope("boop", "service")).isFalse();
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    // default (ALL)
    assertThat(backstack.canFindFromScope("beep", "service1")).isTrue();
    assertThat(backstack.canFindFromScope("beep", "service2")).isFalse();
    assertThat(backstack.canFindFromScope("beep", "parentService1")).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService2")).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service1")).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service2")).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "parentService1")).isTrue();
    assertThat(backstack.canFindFromScope("parent1", "parentService2")).isFalse();
    assertThat(backstack.canFindFromScope("boop", "service1")).isTrue();
    assertThat(backstack.canFindFromScope("boop", "service2")).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService1")).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService2")).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service1")).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service2")).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "parentService1")).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "parentService2")).isTrue();
    // ALL specified
    assertThat(backstack.canFindFromScope("beep", "service1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "service2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("beep", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service1", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("parent1", "parentService2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("boop", "service1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("boop", "service2", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService2", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service2", ScopeLookupMode.ALL)).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "parentService1", ScopeLookupMode.ALL)).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "parentService2", ScopeLookupMode.ALL)).isTrue();
    // EXPLICIT specified
    assertThat(backstack.canFindFromScope("beep", "service1", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "service2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("beep", "parentService1", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("beep", "parentService2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "service2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent1", "parentService1", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("parent1", "parentService2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("boop", "service1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("boop", "service2", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("boop", "parentService1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("boop", "parentService2", ScopeLookupMode.EXPLICIT)).isTrue();
    assertThat(backstack.canFindFromScope("parent2", "service1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "service2", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "parentService1", ScopeLookupMode.EXPLICIT)).isFalse();
    assertThat(backstack.canFindFromScope("parent2", "parentService2", ScopeLookupMode.EXPLICIT)).isTrue();
    // default (ALL)
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("beep", "service2");
        }
    });
    assertThat(backstack.lookupFromScope("beep", "service1")).isSameAs(service1);
    assertThat(backstack.lookupFromScope("beep", "parentService1")).isSameAs(parentService1);
    assertThrows(new Action() {

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

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

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "service2");
        }
    });
    assertThat(backstack.lookupFromScope("parent1", "parentService1")).isSameAs(parentService1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "parentService2");
        }
    });
    assertThat(backstack.lookupFromScope("boop", "service1")).isSameAs(service1);
    assertThat(backstack.lookupFromScope("boop", "service2")).isSameAs(service2);
    assertThat(backstack.lookupFromScope("boop", "parentService1")).isSameAs(parentService1);
    assertThat(backstack.lookupFromScope("boop", "parentService2")).isSameAs(parentService2);
    assertThat(backstack.lookupFromScope("parent2", "service1")).isSameAs(service1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent2", "service2");
        }
    });
    assertThat(backstack.lookupFromScope("parent2", "parentService1")).isSameAs(parentService1);
    assertThat(backstack.lookupFromScope("parent2", "parentService2")).isSameAs(parentService2);
    // ALL specified
    assertThat(backstack.lookupFromScope("beep", "service1", ScopeLookupMode.ALL)).isSameAs(service1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("beep", "service2", ScopeLookupMode.ALL);
        }
    });
    assertThat(backstack.lookupFromScope("beep", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
    assertThrows(new Action() {

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

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

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

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent1", "parentService2", ScopeLookupMode.ALL);
        }
    });
    assertThat(backstack.lookupFromScope("boop", "service1", ScopeLookupMode.ALL)).isSameAs(service1);
    assertThat(backstack.lookupFromScope("boop", "service2", ScopeLookupMode.ALL)).isSameAs(service2);
    assertThat(backstack.lookupFromScope("boop", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
    assertThat(backstack.lookupFromScope("boop", "parentService2", ScopeLookupMode.ALL)).isSameAs(parentService2);
    assertThat(backstack.lookupFromScope("parent2", "service1", ScopeLookupMode.ALL)).isSameAs(service1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent2", "service2", ScopeLookupMode.ALL);
        }
    });
    assertThat(backstack.lookupFromScope("parent2", "parentService1", ScopeLookupMode.ALL)).isSameAs(parentService1);
    assertThat(backstack.lookupFromScope("parent2", "parentService2", ScopeLookupMode.ALL)).isSameAs(parentService2);
    // EXPLICIT specified
    assertThat(backstack.lookupFromScope("beep", "service1", ScopeLookupMode.EXPLICIT)).isSameAs(service1);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("beep", "service2", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThat(backstack.lookupFromScope("beep", "parentService1", ScopeLookupMode.EXPLICIT)).isSameAs(parentService1);
    assertThrows(new Action() {

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

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

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

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

        @Override
        public void doSomething() {
            backstack.lookupFromScope("boop", "service1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThat(backstack.lookupFromScope("boop", "service2", ScopeLookupMode.EXPLICIT)).isSameAs(service2);
    assertThrows(new Action() {

        @Override
        public void doSomething() {
            backstack.lookupFromScope("boop", "parentService1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThat(backstack.lookupFromScope("boop", "parentService2", ScopeLookupMode.EXPLICIT)).isSameAs(parentService2);
    assertThrows(new Action() {

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

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

        @Override
        public void doSomething() {
            backstack.lookupFromScope("parent2", "parentService1", ScopeLookupMode.EXPLICIT);
        }
    });
    assertThat(backstack.lookupFromScope("parent2", "parentService2", ScopeLookupMode.EXPLICIT)).isSameAs(parentService2);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Action(com.zhuinden.simplestack.helpers.Action) HasParentServices(com.zhuinden.simplestack.helpers.HasParentServices) Parcel(android.os.Parcel) HasServices(com.zhuinden.simplestack.helpers.HasServices) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 32 with ServiceProvider

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

the class BackstackTest method exitScopeExitsImplicitScopeCorrectly.

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

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    backstack.setup(History.of(firstKey, key, lastKey));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    backstack.exitScope("blah");
    assertThat(backstack.getHistory()).containsExactly(firstKey);
}
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 33 with ServiceProvider

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

the class BackstackTest method exitScopeToExitsExplicitScopesCorrectly.

@Test
public void exitScopeToExitsExplicitScopesCorrectly() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    Object firstKey = new TestKey("firstKey");
    Object secondKey = new TestKey("secondKey");
    Object lastKey = new TestKey("lastKey");
    Object key1 = new TestKeyWithExplicitParent("key1") {

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

        @Override
        protected void bindParentServices(ServiceBinder serviceBinder) {
        }

        @Override
        protected void bindOwnServices(ServiceBinder serviceBinder) {
        }
    };
    Object key2 = new TestKeyWithExplicitParent("key2") {

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

        @Override
        protected void bindParentServices(ServiceBinder serviceBinder) {
        }

        @Override
        protected void bindOwnServices(ServiceBinder serviceBinder) {
        }
    };
    backstack.setup(History.of(firstKey, secondKey, key1, key2, lastKey));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    Object targetKey = new TestKeyWithScope("blah") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
    assertThat(backstack.getHistory()).containsExactly(firstKey, secondKey, targetKey);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) TestKeyWithScope(com.zhuinden.simplestack.helpers.TestKeyWithScope) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) TestKeyWithExplicitParent(com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent) Test(org.junit.Test)

Example 34 with ServiceProvider

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

the class BackstackTest method exitScopeToThrowsWhenScopeIsNotFound.

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

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    backstack.setup(History.of(key));
    Object targetKey = new TestKey("target");
    try {
        backstack.exitScopeTo("blah", targetKey, StateChange.FORWARD);
        Assert.fail();
    } catch (IllegalStateException e) {
    // OK!
    }
}
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 35 with ServiceProvider

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

the class BackstackTest method exitScopeToExitsExplicitScopeCorrectlyAndGoesBackIfFound.

@Test
public void exitScopeToExitsExplicitScopeCorrectlyAndGoesBackIfFound() {
    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 TestKeyWithOnlyParentServices("key", History.of("blah")) {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
        }
    };
    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", secondKey, StateChange.BACKWARD);
    assertThat(backstack.getHistory()).containsExactly(firstKey, secondKey);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) TestKeyWithOnlyParentServices(com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices) Test(org.junit.Test)

Aggregations

ServiceProvider (com.zhuinden.simplestack.helpers.ServiceProvider)72 Test (org.junit.Test)72 TestKey (com.zhuinden.simplestack.helpers.TestKey)26 TestKeyWithScope (com.zhuinden.simplestack.helpers.TestKeyWithScope)25 ArrayList (java.util.ArrayList)13 Parcel (android.os.Parcel)9 Nonnull (javax.annotation.Nonnull)9 HasParentServices (com.zhuinden.simplestack.helpers.HasParentServices)8 HasServices (com.zhuinden.simplestack.helpers.HasServices)7 TestKeyWithOnlyParentServices (com.zhuinden.simplestack.helpers.TestKeyWithOnlyParentServices)6 TestKeyWithExplicitParent (com.zhuinden.simplestack.helpers.TestKeyWithExplicitParent)4 StateBundle (com.zhuinden.statebundle.StateBundle)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Activity (android.app.Activity)2 Action (com.zhuinden.simplestack.helpers.Action)2 Nullable (javax.annotation.Nullable)1