Search in sources :

Example 56 with ServiceProvider

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

the class ScopingTest method serviceBinderSucceedsIfRootBackstackIsAnAlias.

@Test
public void serviceBinderSucceedsIfRootBackstackIsAnAlias() {
    final String serviceTag = "backstack";
    final Backstack backstack = new Backstack();
    TestKeyWithScope testKeyWithScope = new TestKeyWithScope("blah") {

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.getScopeTag()).isEqualTo(getScopeTag());
            serviceBinder.addAlias(serviceTag, backstack);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "beep";
        }
    };
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(testKeyWithScope));
    backstack.setStateChanger(stateChanger);
    assertThat(backstack.lookupService(serviceTag)).isSameAs(backstack);
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 57 with ServiceProvider

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

the class ScopingTest method scopeIsDestroyedForClearedScopeKeys.

@Test
public void scopeIsDestroyedForClearedScopeKeys() {
    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();
    backstack.setHistory(History.of(testKey1), StateChange.REPLACE);
    assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isFalse();
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 58 with ServiceProvider

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

the class ScopingTest method finalizingScopeTwiceShouldBeNoOp.

@Test
public void finalizingScopeTwiceShouldBeNoOp() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(testKey2));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isTrue();
    backstack.finalizeScopes();
    try {
        backstack.finalizeScopes();
    } catch (Throwable e) {
        Assert.fail("Should be no-op.");
    }
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 59 with ServiceProvider

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

the class ScopingTest method lookupServiceFromScopeWorks.

@Test
public void lookupServiceFromScopeWorks() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    final Object service1 = new Object();
    final Object service2 = new Object();
    final Object service0 = new Object();
    final Object service3 = new Object();
    class Key1 extends TestKey implements HasServices {

        Key1(String name) {
            super(name);
        }

        protected Key1(Parcel in) {
            super(in);
        }

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.hasService("service0")).isFalse();
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service0")).isFalse();
            serviceBinder.addService("service0", service0);
            assertThat(serviceBinder.hasService("service0")).isTrue();
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service0")).isTrue();
            assertThat(serviceBinder.getService("service0")).isSameAs(service0);
            assertThat(serviceBinder.lookupService("service0")).isSameAs(service0);
            assertThat(serviceBinder.lookupFromScope(serviceBinder.getScopeTag(), "service0")).isSameAs(service0);
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service")).isFalse();
            serviceBinder.addService("service", service1);
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service")).isTrue();
            assertThat(serviceBinder.lookupFromScope(serviceBinder.getScopeTag(), "service")).isSameAs(service1);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "beep";
        }
    }
    class Key2 extends TestKey implements HasServices {

        Key2(String name) {
            super(name);
        }

        protected Key2(Parcel in) {
            super(in);
        }

        @Override
        public void bindServices(ServiceBinder serviceBinder) {
            assertThat(serviceBinder.lookupService("service0")).isSameAs(service0);
            assertThat(serviceBinder.canFindFromScope(serviceBinder.getScopeTag(), "service")).isTrue();
            assertThat(serviceBinder.lookupService("service")).isSameAs(service1);
            serviceBinder.addService("service", service2);
            // the mostimportant assertion here
            assertThat(serviceBinder.lookupService("service")).isSameAs(service2);
            assertThat(serviceBinder.lookupFromScope("beep", "service")).isSameAs(service1);
            serviceBinder.addService("service3", service3);
        }

        @Nonnull
        @Override
        public String getScopeTag() {
            return "boop";
        }
    }
    backstack.setup(History.of(new Key1("beep"), new Key2("boop")));
    backstack.setStateChanger(new StateChanger() {

        @Override
        public void handleStateChange(@Nonnull StateChange stateChange, @Nonnull Callback completionCallback) {
            completionCallback.stateChangeComplete();
        }
    });
    assertThat(backstack.lookupService("service")).isSameAs(service2);
    assertThat(backstack.canFindService("service3")).isTrue();
    assertThat(backstack.canFindFromScope("boop", "service3")).isTrue();
    assertThat(backstack.lookupFromScope("boop", "service3")).isSameAs(service3);
    assertThat(backstack.lookupFromScope("beep", "service")).isSameAs(service1);
    assertThat(backstack.lookupFromScope("boop", "service")).isSameAs(service2);
    backstack.goBack();
    assertThat(backstack.canFindFromScope("boop", "service3")).isFalse();
    assertThat(backstack.lookupService("service")).isSameAs(service1);
}
Also used : TestKey(com.zhuinden.simplestack.helpers.TestKey) Parcel(android.os.Parcel) HasServices(com.zhuinden.simplestack.helpers.HasServices) ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) Test(org.junit.Test)

Example 60 with ServiceProvider

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

the class ScopingTest method nonExistentServiceShouldReturnFalseAndThrow.

@Test
public void nonExistentServiceShouldReturnFalseAndThrow() {
    Backstack backstack = new Backstack();
    backstack.setScopedServices(new ServiceProvider());
    backstack.setup(History.of(testKey2));
    assertThat(backstack.hasService(testKey2, SERVICE_TAG)).isFalse();
    try {
        backstack.getService(testKey2, SERVICE_TAG);
        Assert.fail();
    } catch (IllegalArgumentException e) {
    // OK!
    }
}
Also used : ServiceProvider(com.zhuinden.simplestack.helpers.ServiceProvider) 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